-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathble.py
614 lines (498 loc) · 16.4 KB
/
ble.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
from uuid import *
import pexpect
import re
import time
class char_desc:
'''
charachteristic description class.
'''
def __init__(self, handle, uuid):
'''(char_desc, int, str)->None
Init this char_desc.
'''
self.handle = handle
self.uuid = uuid
def __str__(self):
'''(char_desc)->str
Covert this char_desc to a str.
'''
return '\t\t0x{0:04x}\t{1}\t{2}\n'.format(self.handle, self.uuid,
uuid_to_desc(self.uuid))
class characteristic:
'''
charachteristic class.
'''
def __init__(self, handle, properties, value, uuid):
'''(characteristic, int, int, int, str)->None
Init this characteristic.
'''
self.properties = properties
self.handle = handle
self.value = value
self.uuid = uuid
self.end = 0xffff
self.descs = {}
def __str__(self):
'''(characteristic)->str
Covert this characteristic to a str.
'''
'''
Covert properties to a str.
'''
p = self.properties
o = 'BRwWNIAE'
s = '0x{0:02x}('.format(p)
for i in range(8):
if(p & (1 << i)):
s = s + o[i]
else:
s = s + ' '
s = s + ')'
'''
The characteristic itself.
'''
ret = '\t0x{0:04x}\t{1}\t{2}\t0x{3:04x}\t0x{4:04x}\t{5}\n'.format( \
self.handle, self.uuid, s, self.value, self.end,
uuid_to_desc(self.uuid))
'''
All char_descs of the characteristic.
'''
if len(self.descs) == 0:
return ret
ret = ret + '\t\tHandle\tUUID\t\t\t\t\tDescription\n'
for item in self.descs:
ret = ret + str(self.descs[item])
return ret
def add_char_desc(self, d):
'''(characteristic, char_desc)->None
Add a char_desc to this characteristic.
'''
if d not in self.descs:
self.descs[d.uuid] = d
class service:
'''
service class.
'''
def __init__(self, handle, end, uuid):
'''(service, int, int, str)->None
Init this service.
'''
self.handle = handle
self.end = end
self.uuid= uuid
self.chars = {}
def __str__(self):
'''(service)->str
Covert this characteristic to a str.
'''
'''
The service itself.
'''
ret = '0x{0:04x}\t{1}\t0x{2:04x}\t{3}\n'.format(self.handle,
self.uuid, self.end, uuid_to_desc(self.uuid))
'''
All charactersitcs of this service.
'''
if len(self.chars) == 0:
return ret
ret = ret + '\tHandle\tUUID\t\t\t\t\t'
ret = ret + 'Properties\tValue\tEnd\tDescription\n'
for item in self.chars:
ret = ret + str(self.chars[item])
return ret
def add_characteristic(self, c):
'''(service, characteristic)->None
Add a characteristic to this service.
'''
if c not in self.chars:
self.chars[c.uuid] = c
class device:
'''
device class, represent a BLE peripheral.
'''
def __init__(self):
'''(device, str)->None
Init this device.
'''
self.name = None
self.address = None
self.session = None
self.services = {}
def __str__(self):
'''(device)->str
Covert this device to str
'''
if len(self.services) == 0:
return None
'''
All sercices on this device
'''
ret = 'Handle\tUUID\t\t\t\t\tEnd\tDescription\n'
for item in self.services:
ret = ret + str(self.services[item])
return ret
def scan(self, timeout = 10):
'''(device, int)->None
Scan all LE peripherals, stop when timeout.
'''
print('Scanning...')
dev = []
end_time = time.time() + timeout
'''
Stat scan.
'''
se = pexpect.spawn('hcitool lescan', timeout = 5)
patten = '(([\da-fA-F]){2}:){5}([\da-fA-F]){2} .*(?=\r\n)'
'''
Read a record.
'''
i = se.expect([patten, pexpect.TIMEOUT, pexpect.EOF])
while i == 0:
'''
Scan timeout.
'''
if time.time() >= end_time:
se.sendcontrol('C')
'''
Save and print a record.
'''
info = se.after
if not info in dev:
dev.append(info)
print(''.join(('\t', info)))
'''
Read next record.
'''
i = se.expect([patten, pexpect.TIMEOUT, pexpect.EOF])
def find(self, name, timeout = 10):
'''(device, str, int)->boolean
Find an LE peripheral which matches the specified name.
Init self.name and self.address with the device info.
'''
print('Finding...')
'''
Start scan
'''
se = pexpect.spawn('hcitool lescan', timeout = timeout)
patten = ''.join(('(([\da-fA-F]){2}:){5}([\da-fA-F]){2}(?= ', name, ')'))
'''
Wait for the device.
'''
try:
se.expect(patten)
except pexpect.TIMEOUT:
se.sendcontrol('C')
print('FIND: TIMEOUT')
return False
except pexpect.EOF:
print('FIND: FAILED')
return False
'''
Set the name and address info of this device.
'''
self.name = name
self.address = se.after
se.sendcontrol('C')
return True
def disconnect(self):
'''(device)->None
Disconnect this device.
'''
if self.session.isalive():
self.session.sendcontrol('C')
self.session = None
self.services = {}
def connect(self):
'''(device)->boolean
Connect to this device. Start the interactive session for gatttool.
Precondition: self.address != None.
'''
if self.address == None:
print('CONNECT: Device address not specified.')
return False
print (''.join(('Connecting to ', self.address, ' ...')))
'''
Start gatttool in interactive mode.
'''
self.session = pexpect.spawn('gatttool -b {} -I'.format(self.address))
try:
'''
Wait till the gatttool starts.
'''
self.session.expect('\[LE\]>')
'''
Send 'connect' command.
'''
self.session.sendline('connect')
'''
Wait till the connect command finish.
'''
self.session.expect('Connection successful')
'''
Wait for [LE]> or TIMEOUT.
'''
self.session.expect('\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('CONNECT: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('CONNECT: FAILED')
return False
return True
def add_service(self, s):
'''(device, service)->None
Add a service to this device.
'''
if s.uuid not in self.services:
self.services[s.uuid] = s
def discover_service(self, uuid = None):
'''(device, str)->service
Discover primary services specified by uuid.
Precondition: self.session != None.
'''
if self.session == None:
print('PRIMARY: No peripheral connected.')
return None
'''
Check the service cache first.
'''
if uuid != None and uuid in self.services:
return self.services[uuid]
'''
No lucky, perform primary service discovery
'''
try:
'''
Send 'primary' command.
'''
self.session.sendline('primary')
'''
Wait till the command finish or TIMEOUT.
'''
self.session.expect('\[LE\]>.+\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('PRIMARY: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('PRIMARY: FAILED')
return False
'''
Process the command result.
Find and save all primary services.
'''
p = re.compile('.*(0x[\da-fA-F]{4}).*(0x[\da-fA-F]{4}).*([\da-fA-F]{8}(-[\da-fA-F]{4}){3}-[\da-fA-F]{12})')
mm = p.findall(self.session.after)
for m in mm:
hnd = int(m[0].replace('0x',''), 16)
end = int(m[1].replace('0x',''), 16)
s = service(hnd, end, m[2])
self.add_service(s)
'''
Check the service cache again.
'''
if uuid != None and uuid in self.services:
return self.services[uuid]
return None
def discover_characteristic(self, service, uuid = None):
'''(device, service, str)->characteristic
Discover characteristics specified by uuid in the specified service.
Precondition: self.session != None.
'''
if self.session == None:
print('CHARACTERISTIC: No peripheral connected.')
return None
if service == None:
print('CHARACTERISTIC: No service specified.')
return None
'''
Check the characteristics cache first.
'''
if uuid != None and uuid in service.chars:
return service.chars[uuid]
'''
No lucky, perform characteristic discovery
'''
cmd = 'characteristics 0x{0:04x} 0x{1:04x}'.format(service.handle,
service.end)
try:
self.session.sendline(cmd)
self.session.expect('\[LE\]>.+\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('CHARACTERISTIC: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('CHARACTERISTIC: FAILED')
return False
p = re.compile('.*(0x[\da-fA-F]{4}).*(0x[\da-fA-F]{2}).*(0x[\da-fA-F]{4}).*([\da-fA-F]{8}(-[\da-fA-F]{4}){3}-[\da-fA-F]{12})')
mm = p.findall(self.session.after)
for m in mm:
handle = int(m[0].replace('0x', ''), 16)
properties = int(m[1].replace('0x', ''), 16)
value = int(m[2].replace('0x', ''), 16)
c = characteristic(handle, properties, value, m[3])
service.add_characteristic(c)
'''
Calculate the end handle for all characterstics in this service.
'''
for i in service.chars:
service.chars[i].end = service.end
for n in service.chars:
if service.chars[i].handle < service.chars[n].handle\
< service.chars[i].end:
service.chars[i].end = service.chars[n].handle - 1
'''
Check the characteristics cache again.
'''
if uuid != None and uuid in service.chars:
return service.chars[uuid]
return None
def discover_char_desc(self, characteristic, uuid = None):
'''(device, characteristic, str)->char_desc
Discover char_descs specified by uuid in the specified characteristic.
Precondition: self.session != None.
'''
if self.session == None:
print('CHAR_DESC: No peripheral connected.')
return None
if service == None:
print('CHAR_DESC: No characteristic specified.')
return None
'''
Check the char_descs cache first.
'''
if uuid != None and uuid in characteristic.descs:
return characteristic.descs[uuid]
'''
No lucky, perform characteristic discovery
'''
start = characteristic.value + 1
end = characteristic.end
cmd = 'char-desc 0x{0:04x} 0x{1:04x}'.format(start, end)
try:
self.session.sendline(cmd)
self.session.expect('\[LE\]>.+\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('CHAR_DESC: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('CHAR_DESC: FAILED')
return False
p = re.compile('.*(0x[\da-fA-F]{4}).*([\da-fA-F]{4})')
mm = p.findall(self.session.after)
for m in mm:
handle = int(m[0].replace('0x', ''), 16)
id = stduuid(m[1])
cd = char_desc(handle, id)
characteristic.add_char_desc(cd)
'''
Check the char_descs cache again.
'''
if uuid != None and uuid in characteristic.descs:
return characteristic.descs[uuid]
return None
def char_read(self, handle):
'''(device, int)->[int]
Read a handle.
Precondition: self.session != None.
'''
if self.session == None:
print('CHAR_READ: No peripheral connected.')
return None
cmd = 'char-read-hnd 0x{0:04x}'.format(handle)
try:
self.session.sendline(cmd)
self.session.expect('\[LE\]>.*\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('\tCHAR_READ: TIMEOUT')
return value
except pexpect.EOF:
self.disconnect()
print('\tCHAR_READ: FAILED')
return value
p = re.compile(' ([\da-fA-F]{2})')
m= p.findall(self.session.after)
return [int(i, 16) for i in m]
def char_write_req(self, handle, value):
'''(device, int, int)->boolean
Write a handle.
Precondition: self.session != None.
'''
if self.session == None:
print('CHAR_WRITE_REQ: No peripheral connected.')
return None
cmd = 'char-write-req 0x{0:04x} {1}'.format(handle, value)
try:
self.session.sendline(cmd)
self.session.expect('\[LE\]>.*\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('\tCHAR_WRITE_REQ: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('\tCHAR_WRITE_REQ: FAILED')
return False
p = re.compile('success')
m= p.findall(self.session.after)
if m == []:
return False
return True
def char_write_cmd(self, handle, value):
'''(device, int, int)->boolean
Write a handle without response.
Precondition: self.session != None.
'''
if self.session == None:
print('CHAR_WRITE_CMD: No peripheral connected.')
return None
cmd = 'char-write-cmd 0x{0:04x} {1}'.format(handle, value)
try:
self.session.sendline(cmd)
self.session.expect('\[LE\]>.*\[LE\]>')
except pexpect.TIMEOUT:
self.disconnect()
print('\tCHAR_WRITE_CMD: TIMEOUT')
return False
except pexpect.EOF:
self.disconnect()
print('\tCHAR_WRITE_CMD: FAILED')
return False
return True
def char_write_expect_notificition(self, handle, value):
'''(device, int, int)->(int, []) of handle-value pairs
Write a handle without response, and wait for a notification.
Precondition: self.session != None.
'''
if self.session == None:
print('CHAR_WRITE_EXPECT_NOTIFICATION: No peripheral connected.')
return None
cmd = 'char-write-cmd 0x{0:04x} {1}'.format(handle, value)
try:
self.session.sendline(cmd)
self.session.expect('Notification handle = 0x([\da-fA-F]{4}) value: .*\r\n')
except pexpect.TIMEOUT:
print('\tCHAR_WRITE_EXPECT_NOTIFICATION: TIMEOUT')
return None, None
except pexpect.EOF:
self.disconnect()
print('\tCHAR_WRITE_EXPECT_NOTIFICATION: FAILED')
return None, None
p = re.compile('Notification handle = 0x([\da-fA-F]{4}) value: ')
m= p.findall(self.session.after)
if m == []:
return None, None
handle = int(m[0], 16)
p = re.compile(' ([\da-fA-F]{2})')
m= p.findall(self.session.after)
if m == []:
return None, None
return (handle, [int(i, 16) for i in m])