/
bot.go
249 lines (219 loc) · 5.74 KB
/
bot.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
package main
import (
"errors"
"log"
"sync"
"time"
ble "tinygo.org/x/bluetooth"
)
const (
StatusWriteError = iota
StatusOK
StatusError
StatusBusy
StatusVersionIncompatible
StatusUnsupportedCommand
StatusLowBattery
StatusDeviceEncrypted
StatusDeviceUnencrypted
StatusPasswordError
StatusUnsupportedEncryption
StatusNoNearbyDevice
StatusNoNetwork
)
type BotOpenOptions struct {
ConnectTries int
DiscoverServiceTries int
DiscoverCharacteristicsTries int
}
const (
defaultConnectTries = 3
defaultDiscoverServiceTries = 3
defaultDiscoverCharacteristicsTries = 3
)
var serviceFilter = makeServiceFilter("cba20d00-224d-11e6-9fb8-0002a5d5c51b")
var (
ActionPress = []byte{0x57, 0x01, 0x0}
ActionGetInfo = []byte{0x57, 0x02, 0x0}
)
type Bot struct {
Address ble.Address
device *ble.Device
service *ble.DeviceService
writeChar *ble.DeviceCharacteristic
notifyChar *ble.DeviceCharacteristic
notifyChan chan byte
ready bool
userCount int
mu sync.Mutex
closeTimer *time.Timer
}
func makeServiceFilter(serviceRawUUID string) []ble.UUID {
serviceUUID, _ := ble.ParseUUID(serviceRawUUID)
return []ble.UUID{serviceUUID}
}
func (bot *Bot) Press() byte {
bot.mu.Lock()
defer bot.mu.Unlock()
log.Println("Pressing", bot.Address.String())
return bot.act(ActionPress)
}
func (bot *Bot) GetInfo() byte {
log.Println("Getting info is not supported yet", bot.Address.String())
return StatusError
}
func (bot *Bot) act(action []byte) byte {
if !bot.ready {
log.Println("Bot is not ready", bot.Address.String())
return StatusWriteError
}
_, err := bot.writeChar.Write(action)
if err != nil {
log.Println("Failed writing characteristic", bot.Address.String(), err.Error())
return StatusWriteError
}
return <-bot.notifyChan
}
func (bot *Bot) Open(opts *BotOpenOptions) error {
bot.mu.Lock()
defer bot.mu.Unlock()
if bot.closeTimer != nil {
bot.closeTimer.Stop()
}
bot.userCount++
if bot.userCount > 1 {
log.Println("New bot user. Count:", bot.userCount, bot.Address.String())
return nil
}
if bot.ready {
log.Println("Closing canceled. Count:", bot.userCount, bot.Address.String())
return nil
}
if opts == nil {
opts = &BotOpenOptions{}
}
err := bot.connect(opts.connectTries())
if err != nil {
return err
}
err = bot.getService(opts.discoverServiceTries())
if err != nil {
bot.device.Disconnect()
return err
}
err = bot.getChars(opts.discoverCharacteristicsTries())
if err != nil {
bot.device.Disconnect()
return err
}
bot.notifyChan = make(chan byte, 1)
err = bot.notifyChar.EnableNotifications(bot.notification)
if err != nil {
log.Println("Failed enabling notifications", bot.Address, err.Error())
bot.device.Disconnect()
return err
}
bot.ready = true
log.Println("Bot ready", bot.Address)
return nil
}
func (bot *Bot) notification(buf []byte) {
log.Println("Notification", buf[0], bot.Address.String())
bot.notifyChan <- buf[0]
}
func (bot *Bot) ScheduleClosing() {
bot.mu.Lock()
defer bot.mu.Unlock()
bot.userCount--
if bot.userCount > 0 {
log.Println("Bot user left. Count:", bot.userCount, bot.Address.String())
return
}
bot.closeTimer = time.NewTimer(15 * time.Second)
go func() {
<-bot.closeTimer.C
bot.close()
}()
log.Println("Scheduled closing", bot.Address.String())
}
func (bot *Bot) close() {
bot.mu.Lock()
defer bot.mu.Unlock()
if bot.userCount > 0 {
log.Println("Closing interrupted", bot.Address.String())
return
}
bot.ready = false
close(bot.notifyChan)
bot.device.Disconnect()
bot.notifyChar = nil
bot.writeChar = nil
bot.service = nil
bot.device = nil
log.Println("Bot closed", bot.Address.String())
}
func (bot *Bot) connect(tries int) error {
for try := 1; try <= tries; try++ {
log.Println("Trying to connect", try, bot.Address)
device, err := adapter.Connect(bot.Address, ble.ConnectionParams{})
if err == nil {
bot.device = device
log.Println("Connected", bot.Address)
return nil
}
log.Println("Connecting error", bot.Address, err.Error())
}
log.Println("Failed connecting", bot.Address)
return errors.New("failed connecting")
}
func (bot *Bot) getService(tries int) error {
for try := 1; try <= tries; try++ {
log.Println("Trying to discover services", try, bot.Address)
services, err := bot.device.DiscoverServices(serviceFilter)
if err == nil {
bot.service = &services[0]
log.Println("Service discovered", bot.Address)
return nil
}
log.Println("Service discovering error", bot.Address, err.Error())
}
log.Println("Failed discovering services", bot.Address)
return errors.New("failed discovering services")
}
func (bot *Bot) getChars(tries int) error {
for try := 1; try <= tries; try++ {
log.Println("Trying to discover characteristics", try, bot.Address)
chars, err := bot.service.DiscoverCharacteristics(nil)
if err == nil && len(chars) == 2 {
bot.notifyChar = &chars[0]
bot.writeChar = &chars[1]
log.Println("Characteristics discovered", bot.Address)
return nil
}
if err != nil {
log.Println("Characteristics discovering error", bot.Address, err.Error())
} else {
log.Println("Got 0 characteristics", bot.Address)
}
}
log.Println("Failed discovering characteristics", bot.Address)
return errors.New("failed discovering characteristics")
}
func (opts BotOpenOptions) connectTries() int {
if opts.ConnectTries > 0 {
return opts.ConnectTries
}
return defaultConnectTries
}
func (opts BotOpenOptions) discoverServiceTries() int {
if opts.DiscoverServiceTries > 0 {
return opts.DiscoverServiceTries
}
return defaultDiscoverServiceTries
}
func (opts BotOpenOptions) discoverCharacteristicsTries() int {
if opts.DiscoverCharacteristicsTries > 0 {
return opts.DiscoverCharacteristicsTries
}
return defaultDiscoverCharacteristicsTries
}