-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblesync.py
446 lines (358 loc) · 13.5 KB
/
blesync.py
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
from collections import deque
from bluetooth import BLE, UUID
import machine
from micropython import const, schedule
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_IRQ_GATTS_READ_REQUEST = const(4)
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
_IRQ_PERIPHERAL_CONNECT = const(7)
_IRQ_PERIPHERAL_DISCONNECT = const(8)
_IRQ_GATTC_SERVICE_RESULT = const(9)
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)
def _register_callback(irq, callback):
_callbacks[irq].append(callback)
def _event(irq, data, key):
_events[irq][key].append(data)
def _call_callbacks(irq_data):
irq, data = irq_data
for callback in _callbacks[irq]:
callback(*data)
def _callback(irq, data):
schedule(_call_callbacks, (irq, data))
def _register_event(irq, key, bufferlen=1):
_events[irq][key] = deque(tuple(), bufferlen)
# TODO
# def _irq_gatts_read_request(data):
# # A central has issued a read. Note: this is a hard IRQ.
# # Return None to deny the read.
# # Note: This event is not supported on ESP32.
# conn_handle, attr_handle = data
# _event(_IRQ_GATTS_READ_REQUEST, data, conn_handle)
_events = {
_IRQ_SCAN_RESULT: {},
_IRQ_SCAN_DONE: {},
_IRQ_PERIPHERAL_CONNECT: {},
_IRQ_PERIPHERAL_DISCONNECT: {},
_IRQ_GATTC_SERVICE_RESULT: {},
_IRQ_GATTC_SERVICE_DONE: {},
_IRQ_GATTC_CHARACTERISTIC_RESULT: {},
_IRQ_GATTC_CHARACTERISTIC_DONE: {},
_IRQ_GATTC_DESCRIPTOR_RESULT: {},
_IRQ_GATTC_DESCRIPTOR_DONE: {},
_IRQ_GATTC_READ_RESULT: {},
_IRQ_GATTC_WRITE_DONE: {},
}
_callbacks = {
_IRQ_CENTRAL_CONNECT: [],
_IRQ_CENTRAL_DISCONNECT: [],
_IRQ_PERIPHERAL_DISCONNECT: [],
_IRQ_GATTS_WRITE: [],
_IRQ_GATTC_NOTIFY: [],
_IRQ_GATTC_INDICATE: [],
# _IRQ_GATTS_READ_REQUEST: _irq_gatts_read_request,
}
def _irq(event, data):
if event in (
_IRQ_CENTRAL_CONNECT,
_IRQ_CENTRAL_DISCONNECT
):
# A central has connected to this peripheral.
# A central has disconnected from this peripheral.
conn_handle, addr_type, addr = data
data = conn_handle, addr_type, bytes(addr)
_callback(event, data)
elif event == _IRQ_PERIPHERAL_DISCONNECT:
# A central has disconnected from this peripheral or connect timeout
if data == (65535, 255, b'\x00\x00\x00\x00\x00\x00'):
# connect timeout
_event(event, None, None)
else:
conn_handle, addr_type, addr = data
data = conn_handle, addr_type, bytes(addr)
_callback(event, data)
elif event == _IRQ_GATTS_WRITE:
# A central has written to this characteristic or descriptor.
# conn_handle, attr_handle = data
_callback(event, data)
elif event in (_IRQ_GATTC_NOTIFY, _IRQ_GATTC_INDICATE):
# A peripheral has sent a notify request.
# A peripheral has sent an indicate request.
conn_handle, value_handle, notify_data = data
data = conn_handle, value_handle, bytes(notify_data)
_callback(event, data)
elif event == _IRQ_SCAN_RESULT:
# A single scan result.
addr_type, addr, adv_type, rssi, adv_data = data
data = addr_type, bytes(addr), adv_type, rssi, bytes(adv_data)
_event(event, data, None)
elif event == _IRQ_SCAN_DONE:
# A single scan result.
_event(event, None, None)
elif event == _IRQ_PERIPHERAL_CONNECT:
# A successful gap_connect().
conn_handle, addr_type, addr = data
key = addr_type, bytes(addr)
_event(event, conn_handle, key)
elif event == _IRQ_GATTC_SERVICE_RESULT:
# Called for each service found by gattc_discover_services().
conn_handle, start_handle, end_handle, uuid = data
data = start_handle, end_handle, UUID(uuid)
_event(event, data, conn_handle)
elif event == _IRQ_GATTC_SERVICE_DONE:
# Called once service discovery is complete.
# Note: Status will be zero on success, implementation-specific value otherwise.
conn_handle, status = data
_event(event, status, conn_handle)
elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
# Called for each characteristic found by gattc_discover_services().
conn_handle, def_handle, value_handle, properties, uuid = data
data = def_handle, value_handle, properties, UUID(uuid)
_event(event, data, conn_handle)
elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
# Called once service discovery is complete.
# Note: Status will be zero on success, implementation-specific value otherwise.
conn_handle, status = data
_event(event, status, conn_handle)
elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
# Called for each descriptor found by gattc_discover_descriptors().
conn_handle, dsc_handle, uuid = data
data = dsc_handle, UUID(uuid)
_event(event, data, conn_handle)
elif event == _IRQ_GATTC_DESCRIPTOR_DONE:
# Called once service discovery is complete.
# Note: Status will be zero on success, implementation-specific value otherwise.
conn_handle, status = data
_event(event, status, conn_handle)
elif event == _IRQ_GATTC_READ_RESULT:
# A gattc_read() has completed.
conn_handle, value_handle, char_data = data
key = conn_handle, value_handle
_event(event, bytes(char_data), key)
elif event == _IRQ_GATTC_READ_DONE:
# A gattc_read() has completed.
# Note: The value_handle will be zero on btstack (but present on NimBLE).
# Note: Status will be zero on success, implementation-specific value otherwise.
# conn_handle, value_handle, status = data
# key = conn_handle, value_handle
# data = status
return # TODO
elif event == _IRQ_GATTC_WRITE_DONE:
# A gattc_write() has completed.
# Note: The value_handle will be zero on btstack (but present on NimBLE).
# Note: Status will be zero on success, implementation-specific value otherwise.
conn_handle, value_handle, status = data
key = conn_handle, value_handle
_event(event, status, key)
else:
return
def _wait_for_event(irq, key, event_exception_class=None):
event_queue = _events[irq][key]
while not event_queue:
machine.idle()
event_result = event_queue.popleft()
if event_exception_class and event_result:
raise event_exception_class(event_result)
return event_result
def _wait_for_disjunct_events(irq_1, key_1, irq_2, key_2):
event_queue_1 = _events[irq_1][key_1]
event_queue_2 = _events[irq_2][key_2]
while True:
if event_queue_1:
return event_queue_1.popleft(), None
elif event_queue_2:
return None, event_queue_2.popleft()
machine.idle()
_ble = BLE()
config = _ble.config
gap_advertise = _ble.gap_advertise
gatts_register_services = _ble.gatts_register_services
gatts_read = _ble.gatts_read
gatts_write = _ble.gatts_write
gatts_set_buffer = _ble.gatts_set_buffer
gap_disconnect = _ble.gap_disconnect
def _results_until_done(
event_result, event_done, event_done_exception_class, event_key, func, args
):
_register_event(event_result, event_key, bufferlen=100)
_register_event(event_done, event_key)
func(*args)
results_queue = _events[event_result][event_key]
done_queue = _events[event_done][event_key]
while True:
while results_queue:
yield results_queue.popleft()
if done_queue:
done_status = done_queue.popleft()
if done_status:
raise event_done_exception_class(done_status)
return
machine.idle()
def gap_scan(duration_ms, interval_us=None, window_us=None):
"""
if it is interrupted during the iteration, the close() has to be called
see https://github.com/micropython/micropython/issues/6183
Example:
scan_iter = scan(
duration_ms=duration_ms,
interval_us=interval_us,
window_us=window_us,
)
for device in scan_iter:
scan_iter.close()
return device
"""
assert not (interval_us is None and window_us is not None), \
"Argument window_us has to be specified if interval_us is specified"
args = [duration_ms]
if interval_us is not None:
args.append(interval_us)
if window_us is not None:
args.append(window_us)
try:
yield from _results_until_done(
_IRQ_SCAN_RESULT,
_IRQ_SCAN_DONE,
None,
None,
func=_ble.gap_scan,
args=args
)
except GeneratorExit:
_ble.gap_scan(None)
_wait_for_event(_IRQ_SCAN_DONE, None)
def gatts_notify(conn_handle, handle, data=None):
if data is None:
return _ble.gatts_notify(conn_handle, handle)
return _ble.gatts_notify(conn_handle, handle, data)
def activate():
if not _ble.active():
_ble.active(True)
_ble.irq(_irq)
def deactivate():
if _ble.active():
_ble.active(False)
def is_active():
return _ble.active()
class GapConnectTimeoutError(Exception):
pass
def gap_connect(addr_type, addr, timeout_ms=2000):
_register_event(_IRQ_PERIPHERAL_CONNECT, (addr_type, addr))
_register_event(_IRQ_PERIPHERAL_DISCONNECT, None)
_ble.gap_connect(addr_type, addr, timeout_ms)
conn_handle, _ = _wait_for_disjunct_events(
_IRQ_PERIPHERAL_CONNECT, (addr_type, addr),
_IRQ_PERIPHERAL_DISCONNECT, None
)
if conn_handle is None:
raise GapConnectTimeoutError
return conn_handle
class GattcDiscoverServicesError(Exception):
pass
def gattc_discover_services(conn_handle, uuid=None):
args = [conn_handle]
if uuid is not None:
args.append(uuid)
return list(_results_until_done(
event_result=_IRQ_GATTC_SERVICE_RESULT,
event_done=_IRQ_GATTC_SERVICE_DONE,
event_done_exception_class=GattcDiscoverServicesError,
event_key=conn_handle,
func=_ble.gattc_discover_services,
args=args
))
class GattcDiscoverCharacteristicsError(Exception):
pass
def gattc_discover_characteristics(
conn_handle,
start_handle,
end_handle,
):
# TODO uuid argument
return list(_results_until_done(
event_result=_IRQ_GATTC_CHARACTERISTIC_RESULT,
event_done=_IRQ_GATTC_CHARACTERISTIC_DONE,
event_done_exception_class=GattcDiscoverCharacteristicsError,
event_key=conn_handle,
func=_ble.gattc_discover_characteristics,
args=(conn_handle, start_handle, end_handle)
))
class GattcDiscoverDescriptorError(Exception):
pass
def gattc_discover_descriptors(conn_handle, start_handle, end_handle):
return list(_results_until_done(
event_result=_IRQ_GATTC_DESCRIPTOR_RESULT,
event_done=_IRQ_GATTC_DESCRIPTOR_DONE,
event_done_exception_class=GattcDiscoverDescriptorError,
event_key=conn_handle,
func=_ble.gattc_discover_descriptors,
args=(conn_handle, start_handle, end_handle)
))
class GattcReadError(Exception):
pass
def gattc_read(conn_handle, value_handle):
# conn_handle, value_handle, char_data
_register_event(_IRQ_GATTC_READ_RESULT, (conn_handle, value_handle))
_ble.gattc_read(conn_handle, value_handle)
return _wait_for_event(
_IRQ_GATTC_READ_RESULT,
(conn_handle, value_handle),
GattcReadError
)
class GattcWriteError(Exception):
pass
def gattc_write(conn_handle, value_handle, data, ack=False):
# wait for return status of write if ack is True
# otherwise return None immediately
_register_event(_IRQ_GATTC_WRITE_DONE, (conn_handle, value_handle))
_ble.gattc_write(conn_handle, value_handle, data, ack)
if ack:
return _wait_for_event(
_IRQ_GATTC_WRITE_DONE,
(conn_handle, value_handle),
GattcWriteError
)
def on_central_connect(callback):
# A central has connected to this peripheral.
# conn_handle, addr_type, addr
return _register_callback(_IRQ_CENTRAL_CONNECT, callback)
def on_central_disconnect(callback):
# A central has disconnected from this peripheral.
# conn_handle, addr_type, addr
_register_callback(_IRQ_CENTRAL_DISCONNECT, callback)
return callback
def on_peripherial_disconnect(callback):
# Connected peripheral has disconnected.
# conn_handle, addr_type, addr
_register_callback(_IRQ_PERIPHERAL_DISCONNECT, callback)
return callback
def on_gatts_write(callback):
# A central has written to this characteristic or descriptor.
# conn_handle, value_handle
_register_callback(_IRQ_GATTS_WRITE, callback)
return callback
def on_gattc_notify(callback):
# A peripheral has sent a notify request.
# conn_handle, value_handle, notify_data
_register_callback(_IRQ_GATTC_NOTIFY, callback)
return callback
def on_gattc_indicate(callback):
# A peripheral has sent an indicate request.
# conn_handle, value_handle, notify_data
_register_callback(_IRQ_GATTC_INDICATE, callback)
return callback
# TODO
# def on_gatts_read_request(conn_handle):
# _add_callback(_callback_gatts_read_request, conn_handle,
# gatts_read_request_callback)