forked from tinygo-org/bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_darwin.go
More file actions
268 lines (227 loc) · 7.7 KB
/
Copy pathadapter_darwin.go
File metadata and controls
268 lines (227 loc) · 7.7 KB
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
package bluetooth
import (
"errors"
"sync"
"time"
"github.com/tinygo-org/cbgo"
)
var _ BLEAdapter = (*Adapter)(nil)
// Adapter is a connection to BLE devices.
type Adapter struct {
cmd *centralManagerDelegate
pmd *peripheralManagerDelegate
cm cbgo.CentralManager
pm cbgo.PeripheralManager
peripheralFoundHandler func(*Adapter, ScanResult)
scanChan chan error
poweredChan chan error
// connectMap is a mapping of peripheralId -> chan cbgo.Peripheral,
// used to allow multiple callers to call Connect concurrently.
connectMap sync.Map
connectHandler func(device Device, connected bool)
}
// DefaultAdapter is the default adapter on the system.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{
cm: cbgo.NewCentralManager(nil),
pm: cbgo.NewPeripheralManager(nil),
connectMap: sync.Map{},
connectHandler: func(device Device, connected bool) {
return
},
}
// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
//
// poweredChan is cleared on both success and timeout paths so
// a subsequent Enable() on the same Adapter can run again.
func (a *Adapter) Enable() error {
if a.poweredChan != nil {
return errors.New("already calling Enable function")
}
a.poweredChan = make(chan error, 1)
// Set delegate before checking state — a fresh CBCentralManager
// can fire DidUpdateState before SetDelegate, losing the event.
a.cmd = ¢ralManagerDelegate{a: a}
a.cm.SetDelegate(a.cmd)
if a.cm.State() != cbgo.ManagerStatePoweredOn {
select {
case err := <-a.poweredChan:
if err != nil {
a.poweredChan = nil
return err
}
case <-time.NewTimer(10 * time.Second).C:
a.poweredChan = nil
return errors.New("timeout enabling CentralManager")
}
}
// drain any extra powered-on events from channel
for len(a.poweredChan) > 0 {
<-a.poweredChan
}
a.pmd = &peripheralManagerDelegate{a: a}
a.pm.SetDelegate(a.pmd)
a.poweredChan = nil
return nil
}
// Reset tears down CoreBluetooth managers so a subsequent Enable()
// rebuilds them from scratch. Useful for recovering from stale
// CBPeripheral handles, adapter switching, and test cleanup.
//
// Caller must ensure no Scan/Connect/DiscoverServices is in flight.
// After Reset, call Enable() to create fresh managers.
//
// Note: process-level CoreBluetooth state (e.g. the advertisement
// deduplication table) survives Reset — only process exit clears it.
func (a *Adapter) Reset() error {
if a.scanChan != nil {
_ = a.StopScan()
}
// Unblock goroutines parked in Connect — closing the chan
// yields a zero Peripheral so Connect returns an error.
a.connectMap.Range(func(key, value any) bool {
a.connectMap.Delete(key)
if ch, ok := value.(chan cbgo.Peripheral); ok {
defer func() { _ = recover() }()
close(ch)
}
return true
})
a.cm = cbgo.NewCentralManager(nil)
a.pm = cbgo.NewPeripheralManager(nil)
a.cmd = nil
a.pmd = nil
a.poweredChan = nil
a.scanChan = nil
a.peripheralFoundHandler = nil
return nil
}
// CentralManager delegate functions
type centralManagerDelegate struct {
cbgo.CentralManagerDelegateBase
a *Adapter
}
// CentralManagerDidUpdateState when central manager state updated.
func (cmd *centralManagerDelegate) CentralManagerDidUpdateState(cmgr cbgo.CentralManager) {
var event error
switch cmgr.State() {
case cbgo.ManagerStatePoweredOn:
event = nil
case cbgo.ManagerStatePoweredOff:
event = errors.New("bluetooth is powered off")
case cbgo.ManagerStateUnsupported:
event = errors.New("bluetooth is not supported on this device")
case cbgo.ManagerStateUnauthorized:
event = errors.New("bluetooth is not authorized for this app")
default:
return
}
// Non-blocking send: poweredChan may be nil after Enable
// completes, or already buffered.
select {
case cmd.a.poweredChan <- event:
default:
}
}
// DidDiscoverPeripheral when peripheral is discovered.
func (cmd *centralManagerDelegate) DidDiscoverPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral,
advFields cbgo.AdvFields, rssi int) {
if cmd.a.peripheralFoundHandler != nil {
sr := makeScanResult(prph, advFields, rssi)
cmd.a.peripheralFoundHandler(cmd.a, sr)
}
}
// DidDisconnectPeripheral when peripheral is disconnected.
func (cmd *centralManagerDelegate) DidDisconnectPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral, err error) {
id := prph.Identifier().String()
addr := Address{}
uuid, _ := ParseUUID(id)
addr.UUID = uuid
cmd.a.connectHandler(Device{Address: addr}, false)
// like with DidConnectPeripheral, check if we have a chan allocated for this and send through the peripheral
// this will only be true if the receiving side is still waiting for a connection to complete
if ch, ok := cmd.a.connectMap.LoadAndDelete(id); ok {
ch.(chan cbgo.Peripheral) <- prph
}
}
// DidConnectPeripheral when peripheral is connected.
func (cmd *centralManagerDelegate) DidConnectPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral) {
id := prph.Identifier().String()
// Check if we have a chan allocated for this peripheral, and remove it
// from the map if so (it's single-use, will be garbage collected after
// receiver receives the peripheral).
//
// If we don't have a chan allocated, the receiving side timed out, so
// ignore this connection.
if ch, ok := cmd.a.connectMap.LoadAndDelete(id); ok {
// Unblock now that we're connected.
ch.(chan cbgo.Peripheral) <- prph
}
}
// DidFailToConnectPeripheral when peripheral connection fails.
func (cmd *centralManagerDelegate) DidFailToConnectPeripheral(cmgr cbgo.CentralManager, prph cbgo.Peripheral, err error) {
id := prph.Identifier().String()
// Send the peripheral through so Connect can check its state
// and return the appropriate error.
if ch, ok := cmd.a.connectMap.LoadAndDelete(id); ok {
ch.(chan cbgo.Peripheral) <- prph
}
}
// makeScanResult creates a ScanResult when peripheral is found.
func makeScanResult(prph cbgo.Peripheral, advFields cbgo.AdvFields, rssi int) ScanResult {
uuid, _ := ParseUUID(prph.Identifier().String())
var serviceUUIDs []UUID
for _, u := range advFields.ServiceUUIDs {
parsedUUID, _ := ParseUUID(u.String())
serviceUUIDs = append(serviceUUIDs, parsedUUID)
}
var manufacturerData []ManufacturerDataElement
if len(advFields.ManufacturerData) > 2 {
// Note: CoreBluetooth seems to assume there can be only one
// manufacturer data fields in an advertisement packet, while the
// specification allows multiple such fields. See the Bluetooth Core
// Specification Supplement, table 1.1:
// https://www.bluetooth.com/specifications/css-11/
manufacturerID := uint16(advFields.ManufacturerData[0])
manufacturerID += uint16(advFields.ManufacturerData[1]) << 8
manufacturerData = append(manufacturerData, ManufacturerDataElement{
CompanyID: manufacturerID,
Data: advFields.ManufacturerData[2:],
})
}
var serviceData []ServiceDataElement
for _, svcData := range advFields.ServiceData {
cbgoUUID := svcData.UUID
uuid, err := ParseUUID(cbgoUUID.String())
if err != nil {
continue
}
serviceData = append(serviceData, ServiceDataElement{
UUID: uuid,
Data: svcData.Data,
})
}
// Peripheral UUID is randomized on macOS, which means to
// different centrals it will appear to have a different UUID.
return ScanResult{
RSSI: int16(rssi),
Address: Address{
UUID: uuid,
},
AdvertisementPayload: &advertisementFields{
AdvertisementFields{
LocalName: advFields.LocalName,
ServiceUUIDs: serviceUUIDs,
ManufacturerData: manufacturerData,
ServiceData: serviceData,
},
},
}
}
// PeripheralManager delegate functions
type peripheralManagerDelegate struct {
cbgo.PeripheralManagerDelegateBase
a *Adapter
}