/
protocol.go
278 lines (232 loc) · 6.48 KB
/
protocol.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"bytes"
"encoding/binary"
"time"
log "github.com/sirupsen/logrus"
"github.com/tarm/serial"
)
const (
devTSTAT = uint16(0x2001)
devSAM = uint16(0x9201)
)
const responseTimeout = 200
const responseRetries = 5
type snoopCallback func(*InfinityFrame)
type InfinityProtocolRawRequest struct {
data *[]byte
}
type InfinityProtocolSnoop struct {
srcMin uint16
srcMax uint16
cb snoopCallback
}
type InfinityProtocol struct {
device string
port *serial.Port
responseCh chan *InfinityFrame
actionCh chan *Action
snoops []InfinityProtocolSnoop
}
type Action struct {
requestFrame *InfinityFrame
responseFrame *InfinityFrame
ok bool
ch chan bool
}
var readTimeout = time.Second * 5
func (p *InfinityProtocol) openSerial() error {
log.Printf("opening serial interface: %s", p.device)
if p.port != nil {
p.port.Close()
}
c := &serial.Config{Name: p.device, Baud: 38400, ReadTimeout: readTimeout}
var err error
p.port, err = serial.OpenPort(c)
if err != nil {
return err
}
return nil
}
func (p *InfinityProtocol) Open() error {
err := p.openSerial()
if err != nil {
return err
}
p.responseCh = make(chan *InfinityFrame, 32)
p.actionCh = make(chan *Action)
go p.reader()
go p.broker()
return nil
}
func (p *InfinityProtocol) handleFrame(frame *InfinityFrame) *InfinityFrame {
log.Printf("read frame: %s", frame)
switch frame.op {
case opRESPONSE:
if frame.dst == devSAM {
p.responseCh <- frame
}
if len(frame.data) > 3 {
for _, s := range p.snoops {
if frame.src >= s.srcMin && frame.src <= s.srcMax {
s.cb(frame)
}
}
}
case opWRITE:
if frame.src == devTSTAT && frame.dst == devSAM {
return writeAck
}
}
return nil
}
func (p *InfinityProtocol) reader() {
defer panic("exiting InfinityProtocol reader, this should never happen")
msg := []byte{}
buf := make([]byte, 1024)
for {
if p.port == nil {
msg = []byte{}
p.openSerial()
}
n, err := p.port.Read(buf)
if n == 0 || err != nil {
log.Printf("error reading from serial port: %s", err.Error())
if p.port != nil {
p.port.Close()
}
p.port = nil
continue
}
// log.Printf("%q", buf[:n])
msg = append(msg, buf[:n]...)
// log.Printf("buf len is: %v", len(msg))
for {
if len(msg) < 10 {
break
}
l := int(msg[4]) + 10
if len(msg) < l {
break
}
buf := msg[:l]
frame := &InfinityFrame{}
if frame.decode(buf) {
response := p.handleFrame(frame)
if response != nil {
p.sendFrame(response.encode())
}
// Intentionally didn't do msg = msg[l:] to avoid potential
// memory leak. Not sure if it makes a difference...
msg = msg[:copy(msg, msg[l:])]
} else {
// Corrupt message, move ahead one byte and continue parsing
msg = msg[:copy(msg, msg[1:])]
}
}
}
}
func (p *InfinityProtocol) broker() {
defer panic("exiting InfinityProtocol broker, this should never happen")
for {
// log.Debug("entering action select")
select {
case action := <-p.actionCh:
p.performAction(action)
case <-p.responseCh:
log.Warn("dropping unexpected response")
}
}
}
func (p *InfinityProtocol) performAction(action *Action) {
log.Infof("encoded frame: %s", action.requestFrame)
encodedFrame := action.requestFrame.encode()
p.sendFrame(encodedFrame)
ticker := time.NewTicker(time.Millisecond * responseTimeout)
defer ticker.Stop()
for tries := 0; tries < responseRetries; {
select {
case res := <-p.responseCh:
if res.src != action.requestFrame.dst {
continue
}
reqTable := action.requestFrame.data[0:3]
resTable := res.data[0:3]
if action.requestFrame.op == opREAD && !bytes.Equal(reqTable, resTable) {
log.Printf("got response for incorrect table, is: %x expected: %x", resTable, reqTable)
continue
}
action.responseFrame = res
// log.Printf("got response!")
action.ok = true
action.ch <- true
// log.Printf("sent action!")
return
case <-ticker.C:
log.Debug("timeout waiting for response, retransmitting frame")
p.sendFrame(encodedFrame)
tries++
}
}
log.Printf("action timed out")
action.ch <- false
}
func (p *InfinityProtocol) send(dst uint16, op uint8, requestData []byte, response interface{}) bool {
f := InfinityFrame{src: devSAM, dst: dst, op: op, data: requestData}
act := &Action{requestFrame: &f, ch: make(chan bool)}
// Send action to action handling goroutine
p.actionCh <- act
// Wait for response
ok := <-act.ch
if ok && op == opREAD && act.responseFrame != nil && act.responseFrame.data != nil && len(act.responseFrame.data) > 6 {
raw, ok := response.(InfinityProtocolRawRequest)
if ok {
log.Printf(">>>> handling a RawRequest")
*raw.data = append(*raw.data, act.responseFrame.data[6:]...)
log.Printf("raw data length is: %d", len(*raw.data))
} else {
r := bytes.NewReader(act.responseFrame.data[6:])
binary.Read(r, binary.BigEndian, response)
}
// log.Printf("%+v", data)
}
return ok
}
func (p *InfinityProtocol) Write(dst uint16, table []byte, addr []byte, params interface{}) bool {
buf := new(bytes.Buffer)
buf.Write(table[:])
buf.Write(addr[:])
binary.Write(buf, binary.BigEndian, params)
return p.send(dst, opWRITE, buf.Bytes(), nil)
}
func (p *InfinityProtocol) WriteTable(dst uint16, table InfinityTable, flags uint8) bool {
addr := table.addr()
fl := []byte{0x00, 0x00, flags}
return p.Write(dst, addr[:], fl, table)
}
func (p *InfinityProtocol) Read(dst uint16, addr InfinityTableAddr, params interface{}) bool {
return p.send(dst, opREAD, addr[:], params)
}
func (p *InfinityProtocol) ReadTable(dst uint16, table InfinityTable) bool {
addr := table.addr()
return p.send(dst, opREAD, addr[:], table)
}
func (p *InfinityProtocol) sendFrame(buf []byte) bool {
// Ensure we're not in the middle of reopening the serial port due to an error.
if p.port == nil {
return false
}
log.Debugf("transmitting frame: %x", buf)
_, err := p.port.Write(buf)
if err != nil {
log.Errorf("error writing to serial: %s", err.Error())
p.port.Close()
p.port = nil
return false
}
return true
}
func (p *InfinityProtocol) snoopResponse(srcMin uint16, srcMax uint16, cb snoopCallback) {
s := InfinityProtocolSnoop{srcMin: srcMin, srcMax: srcMax, cb: cb}
p.snoops = append(p.snoops, s)
}