-
Notifications
You must be signed in to change notification settings - Fork 2
/
ant.go
366 lines (297 loc) · 9.39 KB
/
ant.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* ant.go
*
* Copyright (c) 2021-2023 Stavros Avramidis (@purpl3F0x). All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
*/
package ant
import (
"encoding/binary"
"fmt"
"log"
"time"
)
type Driver interface {
Open() error
Close()
Read(b []byte) (int, error)
Write(b []byte) (int, error)
BufferSize() int
}
type Ant struct {
driver Driver
buffer Packet
read chan *Message
write chan *Message
writeInTimeslot chan *Message
stopper chan struct{}
decoder chan byte
done chan struct{}
}
func MakeAnt(dev Driver, read chan *Message) (ant *Ant) {
ant = &Ant{
driver: dev,
read: read,
write: make(chan *Message),
// writeInTimeslot: make(chan *Message),
stopper: make(chan struct{}),
decoder: make(chan byte),
done: make(chan struct{}),
}
return ant
}
func (dev *Ant) Start() (e error) {
log.Println("Starting Device")
e = dev.driver.Open()
if e != nil {
return e
}
dev.buffer = make(Packet, dev.driver.BufferSize())
go dev.loop()
go dev.decodeLoop()
go dev.readLoop()
return e
}
func (dev *Ant) Stop() {
dev.stopper <- struct{}{}
dev.buffer = nil
// Wait for loops to finish
<-dev.done
<-dev.done
}
func (dev *Ant) loop() {
// ticker := time.NewTicker(time.Millisecond)
defer func() { dev.done <- struct{}{} }()
defer dev.driver.Close()
defer close(dev.decoder)
defer close(dev.write)
// defer ticker.Stop()
// defer close(dev.writeInTimeslot)
defer log.Println("Loop stopped!")
log.Println("Loop Started")
for {
select {
case <-dev.stopper:
return
case d := <-dev.write:
m := d.Encode()
log.Println("Writing: ", m)
_, err := dev.driver.Write(m)
if err != nil {
log.Println(err)
}
time.Sleep(time.Nanosecond)
}
}
}
func (dev *Ant) readLoop() {
ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if i, err := dev.driver.Read(dev.buffer); err == nil {
for _, v := range dev.buffer[:i] {
dev.decoder <- v
}
}
}
}
}
func (dev *Ant) decodeLoop() {
defer func() { dev.done <- struct{}{} }()
defer close(dev.read)
for {
// Wait for TX Sync
if sync, ok := <-dev.decoder; !ok {
return
} else if sync != MESG_TX_SYNC {
continue
}
// Get content length (+1byte type + 1byte checksum)
length, ok := <-dev.decoder
if !ok {
continue
}
buf := make(Packet, length+4)
buf[0] = MESG_TX_SYNC
buf[1] = length
for i := 2; i < int(length+4); i++ {
if buf[i], ok = <-dev.decoder; !ok {
return
}
}
// Check message integrity
msg, err := Decode(buf)
if err != nil {
log.Println(err)
continue
}
log.Println("Read:", msg)
// if dev.read != nil {
// dev.read <- msg
//
// }
// select {
// case dev.read <- msg:
// default:
// }
}
}
// //////////////////////////////////////////////////////////////////////////////////////
// Config Messages
// //////////////////////////////////////////////////////////////////////////////////////
func (dev *Ant) UnAssignChannel(channel uint8) {
message := NewMessage(MESG_UNASSIGN_CHANNEL_ID, Packet{channel})
dev.write <- message
}
func (dev *Ant) AssignChannel(channel uint8, channelType uint8, networkNumber uint8) {
message := NewMessage(MESG_ASSIGN_CHANNEL_ID, Packet{channel, channelType, networkNumber})
dev.write <- message
}
func (dev *Ant) AssignChannelExt(channel uint8, channelType uint8, networkNumber uint8, ExtFlags uint8) {
message := NewMessage(MESG_ASSIGN_CHANNEL_ID, Packet{channel, channelType, networkNumber, ExtFlags})
dev.write <- message
}
func (dev *Ant) SetChannelId(channel uint8, deviceNum uint16, deviceType uint8, transmissionType uint8) {
payload := [5]byte{channel, 0, 0, deviceType, transmissionType}
binary.LittleEndian.PutUint16(payload[1:], uint16(deviceNum))
message := NewMessage(MESG_CHANNEL_ID_ID, payload[:])
dev.write <- message
}
func (dev *Ant) SetChannelPeriod(channel uint8, messagePeriod uint16) {
payload := [3]byte{channel, 0, 0}
binary.LittleEndian.PutUint16(payload[1:], uint16(messagePeriod))
message := NewMessage(MESG_CHANNEL_MESG_PERIOD_ID, payload[:])
dev.write <- message
}
func (dev *Ant) SetChannelSearchTimeout(channel uint8, messagePeriod uint8) {
message := NewMessage(MESG_CHANNEL_SEARCH_TIMEOUT_ID, Packet{channel, messagePeriod})
dev.write <- message
}
func (dev *Ant) SetChannelRFFreq(channel uint8, rfFreq uint8) {
message := NewMessage(MESG_CHANNEL_RADIO_FREQ_ID, Packet{channel, rfFreq})
dev.write <- message
}
func (dev *Ant) SetNetworkKey(channel uint8, key [8]uint8) {
payload := [9]byte{channel}
copy(payload[1:], key[:])
message := NewMessage(MESG_NETWORK_KEY_ID, payload[:])
dev.write <- message
}
func (dev *Ant) SetTransmitPower(power uint8) {
message := NewMessage(MESG_CHANNEL_RADIO_TX_POWER_ID, Packet{0, power & RADIO_TX_POWER_LVL_MASK})
dev.write <- message
}
func (dev *Ant) SetSearchWaveform(channel uint8, searchWaveform uint16) {
if searchWaveform != 316 && searchWaveform != 97 {
panic("The search waveform to be set. One of these two values only. (316 or 97)")
}
payload := [3]byte{channel}
binary.LittleEndian.PutUint16(payload[1:], uint16(searchWaveform))
message := NewMessage(MESG_RADIO_TX_POWER_ID, payload[:])
dev.write <- message
}
// //////////////////////////////////////////////////////////////////////////////////////
// ANT Control messages
// //////////////////////////////////////////////////////////////////////////////////////
func (dev *Ant) ResetSystem() {
message := NewMessage(MESG_SYSTEM_RESET_ID, Packet{0})
dev.write <- message
}
func (dev *Ant) OpenChannel(channel uint8) {
message := NewMessage(MESG_OPEN_CHANNEL_ID, Packet{channel})
dev.write <- message
}
func (dev *Ant) CloseChannel(channel uint8) {
message := NewMessage(MESG_CLOSE_CHANNEL_ID, Packet{channel})
dev.write <- message
}
func (dev *Ant) RequestMessage(channel uint8, messageId uint8) {
message := NewMessage(MESG_REQUEST_SIZE, Packet{channel, messageId})
dev.write <- message
}
func (dev *Ant) WriteMessage(messageID uint8, data Packet) {
message := NewMessage(messageID, data)
dev.write <- message
}
// //////////////////////////////////////////////////////////////////////////////////////
// The following are the synchronous RF event functions used to update the synchronous data sent over a channel
// //////////////////////////////////////////////////////////////////////////////////////
func (dev *Ant) SendBroadcastData(channel uint8, data Packet) {
if len(data) != 8 {
panic(fmt.Sprint("Data length should be 8 not ", len(data)))
}
payload := [9]byte{channel}
copy(payload[1:], data)
message := NewMessage(MESG_BROADCAST_DATA_ID, payload[:])
dev.write <- message
}
func (dev *Ant) SendAcknowledgedData(channel uint8, data Packet) {
if len(data) != 8 {
panic(fmt.Sprint("Data length should be 8 not ", len(data)))
}
payload := [9]byte{channel}
copy(payload[1:], data)
message := NewMessage(MESG_ACKNOWLEDGED_DATA_ID, payload[:])
dev.writeInTimeslot <- message
}
func (dev *Ant) SendBurstTransferPacket(channelSeq uint8, data Packet) {
if len(data) != 8 {
panic(fmt.Sprint("Data length should be 8 not ", len(data)))
}
payload := [9]byte{channelSeq}
copy(payload[1:], data)
message := NewMessage(MESG_BURST_DATA_ID, payload[:])
dev.writeInTimeslot <- message
}
func (dev *Ant) SendBurstTransfer(channel uint8, data Packet) {
if len(data)%8 != 0 {
panic("Data length should be multiple of 8 not ")
}
packets := uint8(len(data) / 8)
for i := uint8(0); i < packets; i++ {
sequence := ((i - 1) % 3) + 1
if i == 0 {
sequence = 0
} else if i == packets-1 {
sequence |= 0b100
}
channelSeq := channel | sequence<<5
dev.SendBurstTransferPacket(channelSeq, data[i*8:i*8+8])
}
}
// //////////////////////////////////////////////////////////////////////////////////////
// The following functions are used with version 2 modules
// //////////////////////////////////////////////////////////////////////////////////////
func (dev *Ant) AddChannelID(channel uint8, deviceNum uint16, deviceType uint8, transmissionType uint8, index uint8) {
payload := [6]byte{channel, 0, 0, deviceType, transmissionType, index}
binary.LittleEndian.PutUint16(payload[1:], uint16(deviceNum))
message := NewMessage(MESG_ID_LIST_ADD_ID, payload[:])
dev.write <- message
}
func (dev *Ant) ConfigList(channel uint8, listSize uint8, exclude uint8) {
message := NewMessage(MESG_ID_LIST_ADD_ID, Packet{channel, listSize, exclude})
dev.write <- message
}
func (dev *Ant) OpenRxScanMode() {
message := NewMessage(MESG_OPEN_RX_SCAN_ID, Packet{0, 1}) // [0-Channel, 1-Enable]
dev.write <- message
}
// //////////////////////////////////////////////////////////////////////////////////////
// The following functions are used with AP2 modules (not AP1 or AT3)
// //////////////////////////////////////////////////////////////////////////////////////