-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
158 lines (135 loc) · 2.81 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gokbus
import (
"time"
"github.com/tarm/serial"
)
// KBUS is the main interface to communicate with the BMW I/K-Bus
type KBUS struct {
Path string
Baud int
ReadChannel chan Packet
WriteChannel chan Packet
ErrorChannel chan error
Port *serial.Port
}
// New returns a new KBUS device interface
func New(devicePath string, baudrate int) (*KBUS, error) {
serialConfig := serial.Config{
Name: devicePath,
Baud: baudrate,
Size: serial.DefaultSize,
StopBits: serial.Stop1,
Parity: serial.ParityEven,
ReadTimeout: time.Millisecond * 200,
}
s, err := serial.OpenPort(&serialConfig)
if err != nil {
return nil, err
}
newKbus := &KBUS{
Path: devicePath,
Baud: baudrate,
Port: s,
ReadChannel: make(chan Packet, 1),
WriteChannel: make(chan Packet, 1),
ErrorChannel: make(chan error, 1),
}
return newKbus, nil
}
// Start will begin reading and writing packets to the KBus with the provided channels
func (kbus *KBUS) Start() {
go func() {
for {
newPacket, err := kbus.ReadPacket()
if err != nil {
kbus.addError(err)
continue
}
// Only write new packet when the bus is empty
if newPacket.isEmpty {
select {
case p := <-kbus.WriteChannel:
go kbus.WritePacket(p)
continue
default:
continue
}
}
select {
case kbus.ReadChannel <- newPacket:
continue
default:
continue
}
}
}()
}
// Close the KBUS port
func (kbus *KBUS) Close() {
kbus.Port.Close()
}
// ReadPacket will block until the next Packet is recieved or times out because of an empty bus
func (kbus *KBUS) ReadPacket() (Packet, error) {
var err error
p := Packet{}
source, err := kbus.getSourceByte()
if err != nil {
return p, err
}
if source == nil {
p.isEmpty = true
return p, nil
}
p.Source = source[0]
length, err := kbus.readBytes()
if err != nil {
return p, err
}
if length == nil {
p.isEmpty = true
return p, nil
}
p.length = length[0]
destination, err := kbus.readBytes()
if err != nil {
return p, err
}
if destination == nil {
p.isEmpty = true
return p, nil
}
p.Destination = destination[0]
data, err := kbus.getDataBytes(int(p.length) - 2)
if err != nil {
return p, err
}
if data == nil {
p.isEmpty = true
return p, nil
}
p.Data = data
checksum, err := kbus.readBytes()
if err != nil {
return p, err
}
if checksum == nil {
p.isEmpty = true
return p, nil
}
p.checksum = checksum[0]
return p, nil
}
// WritePacket will attempt to write a packet to the kbus
func (kbus *KBUS) WritePacket(p Packet) (int, error) {
p.addLength().addChecksum() // ensure metadata is valid
flatPacket := p.Flatten()
n, err := kbus.Port.Write(flatPacket)
if err != nil {
return 0, err
}
err = kbus.Port.Flush()
if err != nil {
return 0, err
}
return n, nil
}