forked from giejay/nukiPyBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatttool.py
633 lines (552 loc) · 22.2 KB
/
gatttool.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
from __future__ import print_function
import functools
import itertools
import re
import logging
import platform
import signal
import sys
import time
import threading
import subprocess
from uuid import UUID
from contextlib import contextmanager
from pygatt.exceptions import NotConnectedError, BLEError, NotificationTimeout
from pygatt.backends import BLEBackend, Characteristic, BLEAddressType
from pygatt.backends.backend import DEFAULT_CONNECT_TIMEOUT_S
from .device import GATTToolBLEDevice
DEFAULT_RECONNECT_DELAY = 1.0
log = logging.getLogger(__name__)
def _hex_value_parser(x):
return bytearray.fromhex(x)
def is_windows():
return platform.system() == 'Windows'
try:
import pexpect
except Exception as err:
if not is_windows():
print("WARNING:", err, file=sys.stderr)
def at_most_one_device(func):
"""Every connection-specific function on the backend takes an instance of
GATTToolBLEDevice as the first argument - this decorator will raise an
exception if that device is not what the backend thinks is the currently
connected device.
"""
@functools.wraps(func)
def wrapper(self, connected_device, *args, **kwargs):
if connected_device != self._connected_device:
raise NotConnectedError()
return func(self, *args, **kwargs)
return wrapper
class GATTToolReceiver(threading.Thread):
"""
Observe pygatttool stdout in separate thread and dispatch events /
callbacks.
"""
def __init__(self, connection, parent_aliveness):
super(GATTToolReceiver, self).__init__()
self.daemon = True
self._connection = connection
self._parent_aliveness = parent_aliveness
self._event_vector = {
'notification': {
'patterns': [r'Notification handle = .*? \r'],
},
'indication': {
'patterns': [r'Indication handle = .*? \r'],
},
'disconnected': {
'patterns': [
r'.*Disconnected',
r'.*Invalid file descriptor',
]
},
'char_written': {
'patterns': [
r'Characteristic value (was )?written successfully',
r'Characteristic Write Request failed: A timeout occured',
]
},
'value': {
'patterns': [r'value: .*? \r']
},
'value/descriptor': {
'patterns': [r'value/descriptor: .*? \r']
},
'discover': {
'patterns': [
r'handle: 0x([a-fA-F0-9]{4}), '
'char properties: 0x[a-fA-F0-9]{2}, '
'char value handle: 0x([a-fA-F0-9]{4}), '
'uuid: ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]'
'{4}-[0-9a-f]{12})\r\n', # noqa
]
},
'connect': {
'patterns': [r'Connection successful.*\[LE\]>']
},
'mtu': {
'patterns': [
r'MTU was exchanged successfully: (\d+)'
]
}
}
for event in self._event_vector.values():
event["event"] = threading.Event()
event["before"] = None
event["after"] = None
event["match"] = None
event["callback"] = []
def run(self):
items = sorted(itertools.chain.from_iterable(
[[(pattern, event)
for pattern in event["patterns"]]
for event in self._event_vector.values()])
)
patterns = [item[0] for item in items]
events = [item[1] for item in items]
log.info('Running...')
while self._parent_aliveness.is_set():
try:
event_index = self._connection.expect(patterns, timeout=.5)
except pexpect.TIMEOUT:
continue
except (NotConnectedError, pexpect.EOF):
self._event_vector["disconnected"]["event"].set()
break
event = events[event_index]
event["before"] = self._connection.before
event["after"] = self._connection.after
event["match"] = self._connection.match
event["event"].set()
for clb in event["callback"]:
clb(event)
log.info("Listener thread finished")
def clear(self, event):
"""
Clear event
"""
self._event_vector[event]["event"].clear()
def is_set(self, event):
return self._event_vector[event]["event"].is_set()
def wait(self, event, timeout=None):
"""
Wait for event to be trigerred
"""
if not self._event_vector[event]["event"].wait(timeout):
raise NotificationTimeout()
def register_callback(self, event, callback):
"""
Call the callback function when event happens. Event wrapper
is passed as argument.
"""
self._event_vector[event]["callback"].append(callback)
def remove_callback(self, event, callback):
"""
Remove a registered callback, so it is no longer called when an
event happens.
"""
if callback in self._event_vector[event]["callback"]:
self._event_vector[event]["callback"].remove(callback)
def last_value(self, event, value_type):
"""
Retrieve last value that saved by the event
"""
return self._event_vector[event][value_type]
@contextmanager
def event(self, event, timeout=None):
"""
Clear an event, execute context and then wait for event
>>> with gtr.event("connect", 10):
>>> gtb.send(connect_command)
"""
self.clear(event)
yield
self.wait(event, timeout)
class GATTToolBackend(BLEBackend):
"""
Backend to pygatt that uses BlueZ's interactive gatttool CLI prompt.
"""
def __init__(self, hci_device='hci0', gatttool_logfile=None,
cli_options=None, search_window_size=None, max_read=None):
"""
Initialize.
hci_device -- the hci_device to use with GATTTool.
gatttool_logfile -- an optional filename to store raw gatttool
input and output.
search_window_size -- integer (optional); size in bytes of the
search window that is used by `pexpect.expect`. This value
should not exceed max_read
max_read -- integer; number of bytes to read into gatt buffer at
a time. Defaults to ~2000
"""
if is_windows():
raise BLEError("The GATTToolBackend requires BlueZ, "
"which is not available in Windows")
self._hci_device = hci_device
self._cli_options = cli_options
self._connected_device = None
self._gatttool_logfile = gatttool_logfile
self._receiver = None
self._con = None # gatttool interactive session
self._characteristics = {}
self._running = threading.Event()
self._address = None
self._send_lock = threading.Lock()
self._auto_reconnect = False
self._reconnecting = False
self._search_window_size = search_window_size
self._scan = None
self._max_read = max_read
def sendline(self, command):
"""
send a raw command to gatttool
"""
with self._send_lock:
self._con.sendline(command)
def supports_unbonded(self):
return False
def start(self, reset_on_start=True, initialization_timeout=5):
"""
Run gatttool to prepare for sending commands and monitoring the CLI tool
output.
:param bool reset_on_start: Perhaps due to a bug in gatttol or pygatt,
but if the bluez backend isn't restarted, it can sometimes lock up
the computer when trying to make a connection to HCI device.
:param int initialization_timeout: Seconds to wait for the gatttool
prompt. This should appear almost instantly, but on some HCI devices
it may take longer to start up.
"""
if self._con and self._running.is_set():
self.stop()
self._running.set()
if reset_on_start:
# Without restarting, sometimes when trying to bond with the
# GATTTool backend, the entire computer will lock up.
self.reset()
# Start gatttool interactive session for device
args = [
'gatttool',
self._cli_options,
'-i',
self._hci_device,
'-I'
]
gatttool_cmd = ' '.join([arg for arg in args if arg])
log.debug('gatttool_cmd=%s', gatttool_cmd)
if self._max_read:
self._con = pexpect.spawn(
gatttool_cmd, logfile=self._gatttool_logfile,
searchwindowsize=self._search_window_size,
maxread=self._max_read
)
else:
self._con = pexpect.spawn(
gatttool_cmd, logfile=self._gatttool_logfile,
searchwindowsize=self._search_window_size,
)
# Wait for the interactive prompt
self._con.expect(r'\[LE\]>', timeout=initialization_timeout)
# Start the notification receiving thread
self._receiver = GATTToolReceiver(self._con, self._running)
self._receiver.daemon = True
self._receiver.register_callback("disconnected", self._disconnect)
for event in ["notification", "indication"]:
self._receiver.register_callback(
event,
self._handle_notification_string
)
self._receiver.start()
def stop(self):
"""
Disconnects any connected device, stops the background receiving thread
and closes the spawned gatttool process.
disconnect.
"""
self.disconnect(self._connected_device)
if self._running.is_set():
log.info('Stopping')
self._running.clear()
if self._con and self._con.isalive():
while True:
if not self._con.isalive():
break
self.sendline('exit')
time.sleep(0.1)
self._con.close()
self._con = None
def scan(self, timeout=10, run_as_root=False):
"""
By default, scanning with gatttool requires root privileges.
If you don't want to require root, you must add a few
'capabilities' to your system. If you have libcap installed, run this to
enable normal users to perform LE scanning:
setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
If you do use root, the hcitool subprocess becomes more difficult to
terminate cleanly, and may leave your Bluetooth adapter in a bad state.
"""
cmd = 'hcitool -i %s lescan' % self._hci_device
if run_as_root:
cmd = 'sudo %s' % cmd
log.info("Starting BLE scan")
self._scan = scan = pexpect.spawn(cmd)
# "lescan" doesn't exit, so we're forcing a timeout here:
try:
scan.expect('foooooo', timeout=timeout)
except pexpect.EOF:
before_eof = scan.before.decode('utf-8', 'replace')
if "No such device" in before_eof:
message = "No BLE adapter found"
elif "Set scan parameters failed: Input/output error" in before_eof:
message = ("BLE adapter requires reset after a scan as root"
"- call adapter.reset()")
else:
message = "Unexpected error when scanning: %s" % before_eof
log.error(message)
raise BLEError(message)
except pexpect.TIMEOUT:
devices = {}
for line in scan.before.decode('utf-8', 'replace').split('\r\n'):
if 'sudo' in line:
raise BLEError("Enable passwordless sudo for 'hcitool' "
"before scanning")
match = re.match(
r'(([0-9A-Fa-f][0-9A-Fa-f]:?){6}) (\(?.+\)?)', line)
if match is not None:
address = match.group(1)
name = match.group(3)
if name == "(unknown)":
name = None
if address in devices:
if (devices[address]['name'] is None) and (name is not
None):
log.info("Discovered name of %s as %s",
address, name)
devices[address]['name'] = name
else:
log.info("Discovered %s (%s)", address, name)
devices[address] = {
'address': address,
'name': name
}
log.info("Found %d BLE devices", len(devices))
return [device for device in devices.values()]
finally:
self.kill()
return []
def kill(self):
if self._scan is None:
return
# Wait for lescan to exit cleanly, otherwise it leaves the BLE
# adapter in a bad state and the device must be reset through BlueZ.
# This will not work if run_as_root was used, since this process
# itself doesn't have permission to terminate a process running as
# root (hcitool itself). We recommend using the setcap tool to allow
# scanning as a non-root user:
#
# $ sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
try:
self._scan.kill(signal.SIGINT)
self._scan.wait()
except OSError:
log.error("Unable to gracefully stop the scan - "
"BLE adapter may need to be reset.")
def connect(self, address, timeout=DEFAULT_CONNECT_TIMEOUT_S,
address_type=BLEAddressType.public, auto_reconnect=False):
log.info('Connecting to %s with timeout=%s', address, timeout)
self.sendline('sec-level low')
self._address = address
self._auto_reconnect = auto_reconnect
try:
cmd = 'connect {0} {1}'.format(self._address, address_type.name)
with self._receiver.event("connect", timeout):
self.sendline(cmd)
except NotificationTimeout:
message = "Timed out connecting to {0} after {1} seconds.".format(
self._address, timeout
)
log.error(message)
raise NotConnectedError(message)
self._connected_device = GATTToolBLEDevice(address, self)
return self._connected_device
def clear_bond(self, address=None):
"""Use the 'bluetoothctl' program to erase a stored BLE bond.
"""
con = pexpect.spawn('bluetoothctl')
try:
con.expect("bluetooth", timeout=1)
log.info("Clearing bond for %s", address)
con.sendline("remove " + address.upper())
con.expect(
["Device has been removed", "# "],
timeout=.5
)
except pexpect.TIMEOUT:
log.error("Unable to remove bonds for %s: %s",
address, con.before)
finally:
con.close(True)
log.info("Removed bonds for %s", address)
def _disconnect(self, event):
if self._connected_device is not None and self._auto_reconnect:
# this is called as a callback from the pexpect thread
# the reconnection process has to be started in parallel, otherwise
# the call is never finished
log.info("Connection to %s lost. Reconnecting...", self._address)
reconnect_thread = threading.Thread(target=self.reconnect,
args=(self._connected_device, ))
reconnect_thread.start()
else:
try:
self.disconnect(self._connected_device)
except NotConnectedError:
pass
@at_most_one_device
def reconnect(self, timeout=DEFAULT_CONNECT_TIMEOUT_S):
while self._auto_reconnect:
log.info("Connecting to %s with timeout=%s", self._address,
timeout)
try:
cmd = "connect"
with self._receiver.event("connect", timeout):
self.sendline(cmd)
# reenable all notifications
self._connected_device.resubscribe_all()
log.info("Connection to %s reestablished.")
break # finished reconnecting
except NotificationTimeout:
message = ("Timed out connecting to {0} after {1} seconds. "
"Retrying in {2} seconds".format(
self._address, timeout,
DEFAULT_RECONNECT_DELAY))
log.info(message)
time.sleep(DEFAULT_RECONNECT_DELAY)
@at_most_one_device
def disconnect(self, *args, **kwargs):
self._auto_reconnect = False # disables any running reconnection
if not self._receiver.is_set("disconnected"):
self.sendline('disconnect')
self._connected_device = None
# TODO maybe call a disconnected callback on the device instance, so the
# device knows if it was asynchronously disconnected?
@at_most_one_device
def bond(self, *args, **kwargs):
log.info('Bonding')
self.sendline('sec-level medium')
def _save_charecteristic_callback(self, event):
match = event["match"]
try:
value_handle = int(match.group(2), 16)
char_uuid = match.group(3).strip().decode('ascii')
self._characteristics[UUID(char_uuid)] = Characteristic(
char_uuid, value_handle
)
log.debug(
"Found characteristic %s, value handle: 0x%x",
char_uuid,
value_handle
)
except AttributeError:
pass
@at_most_one_device
def discover_characteristics(self, timeout=5):
self._characteristics = {}
self._receiver.register_callback(
"discover",
self._save_charecteristic_callback,
)
self.sendline('characteristics')
max_time = time.time() + timeout
while not self._characteristics and time.time() < max_time:
time.sleep(.5)
# Sleep one extra second in case we caught characteristic
# in the middle
time.sleep(5)
if not self._characteristics:
raise NotConnectedError("Characteristic discovery failed")
return self._characteristics
def _handle_notification_string(self, event):
msg = event["after"]
if not msg:
log.warn("Blank message received in notification, ignored")
return
match_obj = re.match(r'.* handle = (0x[0-9a-f]+) value:(.*)',
msg.decode('utf-8'))
if match_obj is None:
log.warn("Unable to parse notification string, ignoring: %s", msg)
return
handle = int(match_obj.group(1), 16)
values = _hex_value_parser(match_obj.group(2).strip())
if self._connected_device is not None:
self._connected_device.receive_notification(handle, values)
@at_most_one_device
def char_write_handle(self, handle, value, wait_for_response=True,
timeout=30):
"""
Writes a value to a given characteristic handle.
:param handle:
:param value:
:param wait_for_response: If true, performs an attribute write. If
false, sends a command and expects no acknowledgement from the
device.
"""
cmd = 'char-write-{0} 0x{1:02x} {2}'.format(
'req' if wait_for_response else 'cmd',
handle,
''.join("{0:02x}".format(byte) for byte in value),
)
log.debug('Sending cmd=%s', cmd)
if wait_for_response:
try:
with self._receiver.event("char_written", timeout=timeout):
self.sendline(cmd)
except NotificationTimeout:
log.error("No response received", exc_info=True)
raise
else:
self.sendline(cmd)
log.info('Sent cmd=%s', cmd)
@at_most_one_device
def char_read(self, uuid, timeout=1):
"""
Reads a Characteristic by uuid.
:param uuid: UUID of Characteristic to read.
:type uuid: str
:return: bytearray of result.
:rtype: bytearray
"""
with self._receiver.event("value", timeout=timeout):
self.sendline('char-read-uuid %s' % uuid)
rval = self._receiver.last_value("value", "after").split()[1:]
return bytearray([int(x, 16) for x in rval])
@at_most_one_device
def char_read_handle(self, handle, timeout=4):
"""
Reads a Characteristic by handle.
:param handle: handle of Characteristic to read.
:type handle: str
:return: bytearray of result.
:rtype: bytearray
"""
with self._receiver.event("value/descriptor", timeout=timeout):
self.sendline('char-read-hnd %s' % handle)
rval = self._receiver.last_value("value/descriptor", "after"
).split()[1:]
return bytearray([int(x, 16) for x in rval])
@at_most_one_device
def exchange_mtu(self, mtu, timeout=1):
cmd = 'mtu {}'.format(mtu)
log.debug('Requesting MTU: {}'.format(mtu))
with self._receiver.event('mtu', timeout=timeout):
self.sendline(cmd)
try:
rval = self._receiver.last_value("mtu", "after").split()[-1]
except ValueError:
log.error('MTU exchange failed: "{}"'.format(rval))
raise
log.debug('MTU exhange successful: {}'.format(rval))
return rval
def reset(self):
subprocess.Popen(["sudo", "systemctl", "restart", "bluetooth"]).wait()
subprocess.Popen([
"sudo", "hciconfig", self._hci_device, "reset"]).wait()