-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserialstream.go
271 lines (235 loc) · 6.57 KB
/
serialstream.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
package main
import (
"bytes"
"encoding/binary"
"time"
)
const maxSerialFrameLength = 80 // Max. frame length according to Hamlib.
const serialRxSeqBufLength = 100 * time.Millisecond
type serialStream struct {
common streamCommon
sendSeq uint16
rxSeqBuf seqBuf
rxSeqBufEntryChan chan seqBufEntry
receivedSerialData bool
lastReceivedSeq uint16
readFromSerialPort struct {
buf bytes.Buffer
frameStarted bool
frameTimeout *time.Timer
}
deinitNeededChan chan bool
deinitFinishedChan chan bool
}
func (s *serialStream) send(d []byte) error {
l := byte(len(d))
p := append([]byte{0x15 + l, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0xc1, l, 0x00, byte(s.sendSeq >> 8), byte(s.sendSeq)}, d...)
if err := s.common.pkt0.sendTrackedPacket(&s.common, p); err != nil {
return err
}
s.sendSeq++
return nil
}
func (s *serialStream) sendOpenClose(close bool) error {
var magic byte
if close {
magic = 0x00
} else {
magic = 0x05
}
p := []byte{0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
byte(s.common.remoteSID >> 24), byte(s.common.remoteSID >> 16), byte(s.common.remoteSID >> 8), byte(s.common.remoteSID),
0xc0, 0x01, 0x00, byte(s.sendSeq >> 8), byte(s.sendSeq), magic}
if err := s.common.pkt0.sendTrackedPacket(&s.common, p); err != nil {
return err
}
s.sendSeq++
return nil
}
func (s *serialStream) handleRxSeqBufEntry(e seqBufEntry) {
gotSeq := uint16(e.seq)
if s.receivedSerialData {
// Out of order packets can happen if we receive a retransmitted packet, but too late.
if s.rxSeqBuf.compareSeq(e.seq, seqNum(s.lastReceivedSeq)) != larger {
log.Debug("got out of order pkt seq #", e.seq)
return
}
expectedSeq := s.lastReceivedSeq + 1
if expectedSeq != gotSeq {
var missingPkts int
if gotSeq > expectedSeq {
missingPkts = int(gotSeq) - int(expectedSeq)
} else {
missingPkts = int(gotSeq) + 65536 - int(expectedSeq)
}
netstat.reportLoss(missingPkts)
log.Error("lost ", missingPkts, " packets")
}
}
s.lastReceivedSeq = gotSeq
s.receivedSerialData = true
if s.common.pkt0.isPkt0(e.data) {
return
}
e.data = e.data[21:]
if !civControl.decode(e.data) {
return
}
if serialPort.write != nil {
serialPort.write <- e.data
}
if serialTCPSrv.isClientConnected() {
serialTCPSrv.toClient <- e.data
}
}
func (s *serialStream) handleSerialPacket(r []byte) error {
gotSeq := binary.LittleEndian.Uint16(r[6:8])
return s.rxSeqBuf.add(seqNum(gotSeq), r)
}
func (s *serialStream) handleRead(r []byte) error {
// We add both idle pkt0 and serial data to the seqbuf.
if s.common.pkt0.isIdlePkt0(r) || (len(r) >= 22 && r[16] == 0xc1 && r[0]-0x15 == r[17]) {
return s.handleSerialPacket(r)
}
return nil
}
func (s *serialStream) gotDataForRadio(r []byte) {
for len(r) > 0 && !s.readFromSerialPort.frameStarted {
if s.readFromSerialPort.buf.Len() > 1 {
s.readFromSerialPort.buf.Reset()
}
if s.readFromSerialPort.buf.Len() == 0 {
// Cut until we find the frame start byte.
for r[0] != 0xfe {
r = r[1:]
if len(r) == 0 {
return
}
}
// Found the first start byte.
s.readFromSerialPort.buf.WriteByte(r[0])
r = r[1:]
}
if len(r) > 0 && s.readFromSerialPort.buf.Len() == 1 {
if r[0] != 0xfe {
s.readFromSerialPort.buf.Reset()
r = r[1:]
} else {
// Found the second start byte.
s.readFromSerialPort.buf.WriteByte(r[0])
r = r[1:]
s.readFromSerialPort.frameTimeout.Reset(100 * time.Millisecond)
s.readFromSerialPort.frameStarted = true
}
}
}
for _, b := range r {
s.readFromSerialPort.buf.WriteByte(b)
if b == 0xfc || b == 0xfd || s.readFromSerialPort.buf.Len() == maxSerialFrameLength {
if err := s.send(s.readFromSerialPort.buf.Bytes()); err != nil {
reportError(err)
}
if !s.readFromSerialPort.frameTimeout.Stop() {
<-s.readFromSerialPort.frameTimeout.C
}
s.readFromSerialPort.buf.Reset()
s.readFromSerialPort.frameStarted = false
break
}
}
}
func (s *serialStream) loop() {
if enableSerialDevice {
for {
select {
case r := <-serialPort.read:
s.gotDataForRadio(r)
case r := <-s.common.readChan:
if err := s.handleRead(r); err != nil {
reportError(err)
}
case e := <-s.rxSeqBufEntryChan:
s.handleRxSeqBufEntry(e)
case r := <-serialTCPSrv.fromClient:
s.gotDataForRadio(r)
case <-s.readFromSerialPort.frameTimeout.C:
s.readFromSerialPort.buf.Reset()
s.readFromSerialPort.frameStarted = false
case <-s.deinitNeededChan:
s.deinitFinishedChan <- true
return
}
}
} else {
for {
select {
case r := <-s.common.readChan:
if err := s.handleRead(r); err != nil {
reportError(err)
}
case e := <-s.rxSeqBufEntryChan:
s.handleRxSeqBufEntry(e)
case r := <-serialTCPSrv.fromClient:
s.gotDataForRadio(r)
case <-s.readFromSerialPort.frameTimeout.C:
s.readFromSerialPort.buf.Reset()
s.readFromSerialPort.frameStarted = false
case <-s.deinitNeededChan:
s.deinitFinishedChan <- true
return
}
}
}
}
func (s *serialStream) init(devName string) error {
if err := s.common.init("serial", serialStreamPort); err != nil {
return err
}
if enableSerialDevice {
if err := serialPort.initIfNeeded(devName); err != nil {
return err
}
}
if err := serialTCPSrv.initIfNeeded(); err != nil {
return err
}
if err := s.common.start(); err != nil {
return err
}
s.common.pkt7.startPeriodicSend(&s.common, 1, false)
s.common.pkt0.init(&s.common)
s.common.pkt0.startPeriodicSend(&s.common)
if err := s.sendOpenClose(false); err != nil {
return err
}
log.Print("stream started")
s.rxSeqBufEntryChan = make(chan seqBufEntry)
s.rxSeqBuf.init(serialRxSeqBufLength, 0xffff, 0, s.rxSeqBufEntryChan, s.common.requestRetransmit)
s.deinitNeededChan = make(chan bool)
s.deinitFinishedChan = make(chan bool)
s.readFromSerialPort.frameTimeout = time.NewTimer(0)
<-s.readFromSerialPort.frameTimeout.C
civControl.deinit()
civControl = civControlStruct{}
if err := civControl.init(s); err != nil {
return err
}
go s.loop()
return nil
}
func (s *serialStream) deinit() {
if s.common.conn != nil {
_ = s.sendOpenClose(true)
}
if s.deinitNeededChan != nil {
s.deinitNeededChan <- true
<-s.deinitFinishedChan
}
civControl.deinit()
s.common.deinit()
s.rxSeqBuf.deinit()
}