forked from Will1604/infinitive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.go
336 lines (286 loc) · 8.2 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"bytes"
"encoding/binary"
"reflect"
"time"
log "github.com/Sirupsen/logrus"
"github.com/tarm/serial"
)
const (
devTSTAT = uint16(0x2001)
devHeatPump = uint16(0x5001)
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
reportDuration time.Duration
tempUnit uint8
readCount, readErrors, tableMismatches uint32
}
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)
// set up reporting of read quality
if p.reportDuration == 0 {
p.reportDuration = time.Hour * 24
}
ticker := time.NewTicker(p.reportDuration)
go func() {
for range ticker.C {
p.logReadErrorSummary()
}
}()
go p.reader()
go p.broker()
return nil
}
func (p *InfinityProtocol) resetCounts() {
p.readCount = 0
p.readErrors = 0
p.tableMismatches = 0
}
func (p *InfinityProtocol) logReadErrorSummary() {
readErrorPercent := 100.0 * float32(p.readErrors) / float32(p.readCount)
tableMismatchErrorPercent := 100.0 * float32(p.tableMismatches) / float32(p.readCount)
log.Printf("protocol report for the last %v", p.reportDuration)
log.Printf("Total reads: %d", p.readCount)
log.Printf("Errors reading data: %d %9.2f%%", p.readErrors, readErrorPercent)
log.Printf("Mismatched tables: %d %9.2f%%", p.tableMismatches, tableMismatchErrorPercent)
p.resetCounts()
}
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 {
// Shouldn't a failure of any of the above tests return false from send?
// As written the function would return true if a successful response even if there were other problems
// Or am I missing some important point?
p.readCount++
// The following seems to be redundant with the test in performAction
// but mismatched tables are getting through to here despite that test
reqTable := requestData[0:3]
resTable := act.responseFrame.data[0:3]
if !bytes.Equal(reqTable, resTable) {
log.Printf("Table mismatch in read, found: %x expected: %x", resTable, reqTable)
p.tableMismatches++
return false
}
dataStart := 6
if bytes.Equal(requestData, []byte{0x00, 0x01, 0x04}) {
dataStart = 3
}
r := bytes.NewReader(act.responseFrame.data[dataStart:])
err := binary.Read(r, binary.BigEndian, response)
if err != nil {
log.Printf("Read failed:", err)
log.Printf("Data was %v :%x", reflect.TypeOf(response), act.responseFrame.data)
p.readErrors++
return false
}
if bytes.Equal(resTable, []byte{0x00, 0x3B, 0x02}) {
p.tempUnit = act.responseFrame.data[4]
}
}
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)
}
func (p *InfinityProtocol) tempUnitStr() string {
if p.tempUnit&0x1 == 0x1 {
return "C"
} else {
return "F"
}
}