Merge ~sylvain-pineau/checkbox-support:eddystone-scanner into checkbox-support:master
- Git
- lp:~sylvain-pineau/checkbox-support
- eddystone-scanner
- Merge into master
Proposed by
Sylvain Pineau
Status: | Merged |
---|---|
Approved by: | Sylvain Pineau |
Approved revision: | 6b02a8bfe37b28f69054617a78da4866006047d1 |
Merged at revision: | 52937e937a410bc379b56b8b5f4d5f554d381a68 |
Proposed branch: | ~sylvain-pineau/checkbox-support:eddystone-scanner |
Merge into: | checkbox-support:master |
Diff against target: |
1794 lines (+1748/-0) 7 files modified
checkbox_support/scripts/eddystone_scanner.py (+87/-0) checkbox_support/vendor/__init__.py (+0/-0) checkbox_support/vendor/aioblescan/LICENSE.txt (+20/-0) checkbox_support/vendor/aioblescan/__init__.py (+2/-0) checkbox_support/vendor/aioblescan/aioblescan.py (+1275/-0) checkbox_support/vendor/aioblescan/eddystone.py (+362/-0) setup.py (+2/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Sylvain Pineau (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
New console script to scan for Eddystone URL beacon advertisements.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/checkbox_support/scripts/eddystone_scanner.py b/checkbox_support/scripts/eddystone_scanner.py | |||
2 | 0 | new file mode 100644 | 0 | new file mode 100644 |
3 | index 0000000..3ee398a | |||
4 | --- /dev/null | |||
5 | +++ b/checkbox_support/scripts/eddystone_scanner.py | |||
6 | @@ -0,0 +1,87 @@ | |||
7 | 1 | #!/usr/bin/env python3 | ||
8 | 2 | # encoding: UTF-8 | ||
9 | 3 | # Copyright (c) 2018 Canonical Ltd. | ||
10 | 4 | # | ||
11 | 5 | # Authors: | ||
12 | 6 | # Sylvain Pineau <sylvain.pineau@canonical.com> | ||
13 | 7 | # | ||
14 | 8 | # This program is free software: you can redistribute it and/or modify | ||
15 | 9 | # it under the terms of the GNU General Public License as published by | ||
16 | 10 | # the Free Software Foundation, either version 3 of the License, or | ||
17 | 11 | # (at your option) any later version. | ||
18 | 12 | # | ||
19 | 13 | # This program is distributed in the hope that it will be useful, | ||
20 | 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
21 | 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
22 | 16 | # GNU General Public License for more details. | ||
23 | 17 | # | ||
24 | 18 | # You should have received a copy of the GNU General Public License | ||
25 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
26 | 20 | |||
27 | 21 | import argparse | ||
28 | 22 | import asyncio | ||
29 | 23 | |||
30 | 24 | from checkbox_support.vendor.aioblescan import create_bt_socket | ||
31 | 25 | from checkbox_support.vendor.aioblescan import BLEScanRequester | ||
32 | 26 | from checkbox_support.vendor.aioblescan import HCI_Cmd_LE_Advertise | ||
33 | 27 | from checkbox_support.vendor.aioblescan import HCI_Event | ||
34 | 28 | from checkbox_support.vendor.aioblescan.eddystone import EddyStone | ||
35 | 29 | |||
36 | 30 | |||
37 | 31 | def main(): | ||
38 | 32 | parser = argparse.ArgumentParser( | ||
39 | 33 | description="Track BLE advertised packets") | ||
40 | 34 | parser.add_argument("-D", "--device", default='hci0', | ||
41 | 35 | help="Select the hciX device to use " | ||
42 | 36 | "(default hci0).") | ||
43 | 37 | |||
44 | 38 | async def timeout(): | ||
45 | 39 | await asyncio.sleep(10.0) | ||
46 | 40 | |||
47 | 41 | def ble_process(data): | ||
48 | 42 | ev = HCI_Event() | ||
49 | 43 | ev.decode(data) | ||
50 | 44 | advertisement = EddyStone().decode(ev) | ||
51 | 45 | if advertisement: | ||
52 | 46 | print("EddyStone URL: {}".format(advertisement['url'])) | ||
53 | 47 | for task in asyncio.Task.all_tasks(): | ||
54 | 48 | task.cancel() | ||
55 | 49 | |||
56 | 50 | try: | ||
57 | 51 | opts = parser.parse_args() | ||
58 | 52 | except Exception as e: | ||
59 | 53 | parser.error("Error: " + str(e)) | ||
60 | 54 | return 1 | ||
61 | 55 | event_loop = asyncio.get_event_loop() | ||
62 | 56 | # First create and configure a STREAM socket | ||
63 | 57 | try: | ||
64 | 58 | mysocket = create_bt_socket(int(opts.device.replace('hci', ''))) | ||
65 | 59 | except OSError as e: | ||
66 | 60 | print(e) | ||
67 | 61 | return 1 | ||
68 | 62 | # Create a connection with the STREAM socket | ||
69 | 63 | fac = event_loop._create_connection_transport( | ||
70 | 64 | mysocket, BLEScanRequester, None, None) | ||
71 | 65 | # Start it | ||
72 | 66 | conn, btctrl = event_loop.run_until_complete(fac) | ||
73 | 67 | # Attach processing | ||
74 | 68 | btctrl.process = ble_process | ||
75 | 69 | # Probe | ||
76 | 70 | btctrl.send_scan_request() | ||
77 | 71 | try: | ||
78 | 72 | event_loop.run_until_complete(timeout()) | ||
79 | 73 | return 1 | ||
80 | 74 | except asyncio.CancelledError: | ||
81 | 75 | return 0 | ||
82 | 76 | except KeyboardInterrupt: | ||
83 | 77 | return 1 | ||
84 | 78 | finally: | ||
85 | 79 | btctrl.stop_scan_request() | ||
86 | 80 | command = HCI_Cmd_LE_Advertise(enable=False) | ||
87 | 81 | btctrl.send_command(command) | ||
88 | 82 | conn.close() | ||
89 | 83 | event_loop.close() | ||
90 | 84 | |||
91 | 85 | |||
92 | 86 | if __name__ == '__main__': | ||
93 | 87 | raise SystemExit(main()) | ||
94 | diff --git a/checkbox_support/vendor/__init__.py b/checkbox_support/vendor/__init__.py | |||
95 | 0 | new file mode 100644 | 88 | new file mode 100644 |
96 | index 0000000..e69de29 | |||
97 | --- /dev/null | |||
98 | +++ b/checkbox_support/vendor/__init__.py | |||
99 | diff --git a/checkbox_support/vendor/aioblescan/LICENSE.txt b/checkbox_support/vendor/aioblescan/LICENSE.txt | |||
100 | 1 | new file mode 100644 | 89 | new file mode 100644 |
101 | index 0000000..cb44740 | |||
102 | --- /dev/null | |||
103 | +++ b/checkbox_support/vendor/aioblescan/LICENSE.txt | |||
104 | @@ -0,0 +1,20 @@ | |||
105 | 1 | The MIT License (MIT) | ||
106 | 2 | |||
107 | 3 | Copyright © 2017 François Wautier | ||
108 | 4 | |||
109 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
110 | 6 | oftware and associated documentation files (the "Software"), to deal in the Software | ||
111 | 7 | without restriction, including without limitation the rights to use, copy, modify, | ||
112 | 8 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
113 | 9 | permit persons to whom the Software is furnished to do so, subject to the following | ||
114 | 10 | conditions: | ||
115 | 11 | |||
116 | 12 | The above copyright notice and this permission notice shall be included in all copies | ||
117 | 13 | or substantial portions of the Software. | ||
118 | 14 | |||
119 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
120 | 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
121 | 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | ||
122 | 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT ORxi | ||
123 | 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
124 | 20 | DEALINGS IN THE SOFTWARE. | ||
125 | diff --git a/checkbox_support/vendor/aioblescan/__init__.py b/checkbox_support/vendor/aioblescan/__init__.py | |||
126 | 0 | new file mode 100644 | 21 | new file mode 100644 |
127 | index 0000000..5fdf345 | |||
128 | --- /dev/null | |||
129 | +++ b/checkbox_support/vendor/aioblescan/__init__.py | |||
130 | @@ -0,0 +1,2 @@ | |||
131 | 1 | from .aioblescan import * | ||
132 | 2 | __version__ = '0.2.1' | ||
133 | diff --git a/checkbox_support/vendor/aioblescan/aioblescan.py b/checkbox_support/vendor/aioblescan/aioblescan.py | |||
134 | 0 | new file mode 100644 | 3 | new file mode 100644 |
135 | index 0000000..b0d9d0d | |||
136 | --- /dev/null | |||
137 | +++ b/checkbox_support/vendor/aioblescan/aioblescan.py | |||
138 | @@ -0,0 +1,1275 @@ | |||
139 | 1 | #!/usr/bin/env python3 | ||
140 | 2 | # -*- coding:utf-8 -*- | ||
141 | 3 | # | ||
142 | 4 | # This application is simply a python only Bluetooth LE Scan command with | ||
143 | 5 | # decoding of advertised packets | ||
144 | 6 | # | ||
145 | 7 | # Copyright (c) 2017 François Wautier | ||
146 | 8 | # | ||
147 | 9 | # Note large part of this code was taken from scapy and other opensource software | ||
148 | 10 | # | ||
149 | 11 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
150 | 12 | # of this software and associated documentation files (the "Software"), to deal | ||
151 | 13 | # in the Software without restriction, including without limitation the rights | ||
152 | 14 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
153 | 15 | # of the Software, and to permit persons to whom the Software is furnished to do so, | ||
154 | 16 | # subject to the following conditions: | ||
155 | 17 | # | ||
156 | 18 | # The above copyright notice and this permission notice shall be included in all copies | ||
157 | 19 | # or substantial portions of the Software. | ||
158 | 20 | # | ||
159 | 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
160 | 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
161 | 23 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
162 | 24 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
163 | 25 | # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
164 | 26 | # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE | ||
165 | 27 | |||
166 | 28 | import socket, asyncio, sys | ||
167 | 29 | from struct import pack, unpack, calcsize | ||
168 | 30 | |||
169 | 31 | |||
170 | 32 | #A little bit of HCI | ||
171 | 33 | HCI_COMMAND = 0x01 | ||
172 | 34 | HCI_ACL_DATA = 0x02 | ||
173 | 35 | HCI_SCO_DATA = 0x03 | ||
174 | 36 | HCI_EVENT = 0x04 | ||
175 | 37 | HCI_VENDOR = 0x05 | ||
176 | 38 | |||
177 | 39 | PRINT_INDENT=" " | ||
178 | 40 | |||
179 | 41 | CMD_SCAN_REQUEST = 0x200c #mixing the OGF in with that HCI shift | ||
180 | 42 | |||
181 | 43 | # | ||
182 | 44 | EDDY_UUID=b"\xfe\xaa" #Google UUID | ||
183 | 45 | |||
184 | 46 | #Generated from https://www.uuidgenerator.net/ 906ed6ab-6785-4eab-9847-bf9889c098ae alternative is 668997f8-4acd-48ea-b35b-749e54215860 | ||
185 | 47 | MY_UUID = b'\x90\x6e\xd6\xab\x67\x85\x4e\xab\x98\x47\xbf\x98\x89\xc0\x98\xae' | ||
186 | 48 | #MY_UUID = b'\x66\x89\x97\xf8\x4a\xcd\x48\xea\xb3\x5b\x74\x9e\x54\x21\x58\x60' | ||
187 | 49 | # | ||
188 | 50 | # Let's define some useful types | ||
189 | 51 | # | ||
190 | 52 | class MACAddr: | ||
191 | 53 | """Class representing a MAC address. | ||
192 | 54 | |||
193 | 55 | :param name: The name of the instance | ||
194 | 56 | :type name: str | ||
195 | 57 | :param mac: the mac address. | ||
196 | 58 | :type mac: str | ||
197 | 59 | :returns: MACAddr instance. | ||
198 | 60 | :rtype: MACAddr | ||
199 | 61 | |||
200 | 62 | """ | ||
201 | 63 | def __init__(self,name,mac="00:00:00:00:00:00"): | ||
202 | 64 | self.name = name | ||
203 | 65 | self.val=mac.lower() | ||
204 | 66 | |||
205 | 67 | def encode (self): | ||
206 | 68 | """Encode the MAC address to a byte array. | ||
207 | 69 | |||
208 | 70 | :returns: The encoded version of the MAC address | ||
209 | 71 | :rtype: bytes | ||
210 | 72 | """ | ||
211 | 73 | return int(self.val.replace(":",""),16).to_bytes(6,"little") | ||
212 | 74 | |||
213 | 75 | def decode(self,data): | ||
214 | 76 | """Decode the MAC address from a byte array. | ||
215 | 77 | |||
216 | 78 | This will take the first 6 bytes from data and transform them into a MAC address | ||
217 | 79 | string representation. This will be assigned to the attribute "val". It then returns | ||
218 | 80 | the data stream minus the bytes consumed | ||
219 | 81 | |||
220 | 82 | :param data: The data stream containing the value to decode at its head | ||
221 | 83 | :type data: bytes | ||
222 | 84 | :returns: The datastream minus the bytes consumed | ||
223 | 85 | :rtype: bytes | ||
224 | 86 | """ | ||
225 | 87 | self.val=':'.join("%02x" % x for x in reversed(data[:6])) | ||
226 | 88 | return data[6:] | ||
227 | 89 | |||
228 | 90 | def __len__(self): | ||
229 | 91 | return 6 | ||
230 | 92 | |||
231 | 93 | def show(self,depth=0): | ||
232 | 94 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
233 | 95 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
234 | 96 | |||
235 | 97 | class Bool: | ||
236 | 98 | """Class representing a boolean value. | ||
237 | 99 | |||
238 | 100 | :param name: The name of the instance | ||
239 | 101 | :type name: str | ||
240 | 102 | :param val: the boolean value. | ||
241 | 103 | :type mac: bool | ||
242 | 104 | :returns: Bool instance. | ||
243 | 105 | :rtype: Bool | ||
244 | 106 | |||
245 | 107 | """ | ||
246 | 108 | def __init__(self,name,val=True): | ||
247 | 109 | self.name=name | ||
248 | 110 | self.val=val | ||
249 | 111 | |||
250 | 112 | def encode (self): | ||
251 | 113 | val=(self.val and b'\x01') or b'\x00' | ||
252 | 114 | return val | ||
253 | 115 | |||
254 | 116 | def decode(self,data): | ||
255 | 117 | self.val= data[:1]==b"\x01" | ||
256 | 118 | return data[1:] | ||
257 | 119 | |||
258 | 120 | def __len__(self): | ||
259 | 121 | return 1 | ||
260 | 122 | |||
261 | 123 | def show(self,depth=0): | ||
262 | 124 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
263 | 125 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
264 | 126 | |||
265 | 127 | class Byte: | ||
266 | 128 | """Class representing a single byte value. | ||
267 | 129 | |||
268 | 130 | :param name: The name of the instance | ||
269 | 131 | :type name: str | ||
270 | 132 | :param val: the single byte value. | ||
271 | 133 | :type val: byte | ||
272 | 134 | :returns: Byte instance. | ||
273 | 135 | :rtype: Byte | ||
274 | 136 | |||
275 | 137 | """ | ||
276 | 138 | def __init__(self,name,val=0): | ||
277 | 139 | self.name=name | ||
278 | 140 | self.val=val | ||
279 | 141 | |||
280 | 142 | def encode (self): | ||
281 | 143 | val=pack("<c",self.val) | ||
282 | 144 | return val | ||
283 | 145 | |||
284 | 146 | def decode(self,data): | ||
285 | 147 | self.val= unpack("<c",data[:1])[0] | ||
286 | 148 | return data[1:] | ||
287 | 149 | |||
288 | 150 | def __len__(self): | ||
289 | 151 | return 1 | ||
290 | 152 | |||
291 | 153 | def show(self,depth=0): | ||
292 | 154 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
293 | 155 | print("{}{}".format(PRINT_INDENT*(depth+1),":".join(map(lambda b: format(b, "02x"), self.val)))) | ||
294 | 156 | |||
295 | 157 | class EnumByte: | ||
296 | 158 | """Class representing a single byte value from a limited set of value | ||
297 | 159 | |||
298 | 160 | :param name: The name of the instance | ||
299 | 161 | :type name: str | ||
300 | 162 | :param val: the single byte value. | ||
301 | 163 | :type val: byte | ||
302 | 164 | :param loval: the list of possible values. | ||
303 | 165 | :type loval: dict | ||
304 | 166 | :returns: EnumByte instance. | ||
305 | 167 | :rtype: EnumByte | ||
306 | 168 | |||
307 | 169 | """ | ||
308 | 170 | def __init__(self,name,val=0,loval={0:"Undef"}): | ||
309 | 171 | self.name=name | ||
310 | 172 | self.val=val | ||
311 | 173 | self.loval=loval | ||
312 | 174 | |||
313 | 175 | def encode (self): | ||
314 | 176 | val=pack(">B",self.val) | ||
315 | 177 | return val | ||
316 | 178 | |||
317 | 179 | def decode(self,data): | ||
318 | 180 | self.val= unpack(">B",data[:1])[0] | ||
319 | 181 | return data[1:] | ||
320 | 182 | |||
321 | 183 | @property | ||
322 | 184 | def strval(self): | ||
323 | 185 | if self.val in self.loval: | ||
324 | 186 | return self.loval[self.val] | ||
325 | 187 | else: | ||
326 | 188 | return str(self.val) | ||
327 | 189 | |||
328 | 190 | def __len__(self): | ||
329 | 191 | return 1 | ||
330 | 192 | |||
331 | 193 | def show(self,depth=0): | ||
332 | 194 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
333 | 195 | if self.val in self.loval: | ||
334 | 196 | print("{}{}".format(PRINT_INDENT*(depth+1),self.loval[self.val])) | ||
335 | 197 | else: | ||
336 | 198 | print("{}Undef".format(PRINT_INDENT*(depth+1))) | ||
337 | 199 | |||
338 | 200 | class BitFieldByte: | ||
339 | 201 | """Class representing a single byte value as a bit field. | ||
340 | 202 | |||
341 | 203 | :param name: The name of the instance | ||
342 | 204 | :type name: str | ||
343 | 205 | :param val: the single byte value. | ||
344 | 206 | :type val: byte | ||
345 | 207 | :param loval: the list defining the name of the property represented by each bit. | ||
346 | 208 | :type loval: list | ||
347 | 209 | :returns: BitFieldByte instance. | ||
348 | 210 | :rtype: BitFieldByte | ||
349 | 211 | |||
350 | 212 | """ | ||
351 | 213 | def __init__(self,name,val=0,loval=["Undef"]*8): | ||
352 | 214 | self.name=name | ||
353 | 215 | self._val=val | ||
354 | 216 | self.loval=loval | ||
355 | 217 | |||
356 | 218 | def encode (self): | ||
357 | 219 | val=pack(">B",self._val) | ||
358 | 220 | return val | ||
359 | 221 | |||
360 | 222 | def decode(self,data): | ||
361 | 223 | self._val= unpack(">B",data[:1])[0] | ||
362 | 224 | return data[1:] | ||
363 | 225 | |||
364 | 226 | def __len__(self): | ||
365 | 227 | return 1 | ||
366 | 228 | |||
367 | 229 | @property | ||
368 | 230 | def val(self): | ||
369 | 231 | resu={} | ||
370 | 232 | for x in self.loval: | ||
371 | 233 | if x not in ["Undef","Reserv"]: | ||
372 | 234 | resu[x]=(self._val & mybit)>0 | ||
373 | 235 | return resu | ||
374 | 236 | |||
375 | 237 | def show(self,depth=0): | ||
376 | 238 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
377 | 239 | mybit=0x80 | ||
378 | 240 | for x in self.loval: | ||
379 | 241 | if x not in ["Undef","Reserv"]: | ||
380 | 242 | print("{}{}: {}".format(PRINT_INDENT*(depth+1),x, ((self._val & mybit) and "True") or False)) | ||
381 | 243 | mybit = mybit >>1 | ||
382 | 244 | |||
383 | 245 | class IntByte: | ||
384 | 246 | """Class representing a single byte as a signed integer. | ||
385 | 247 | |||
386 | 248 | :param name: The name of the instance | ||
387 | 249 | :type name: str | ||
388 | 250 | :param val: the integer value. | ||
389 | 251 | :type val: int | ||
390 | 252 | :returns: IntByte instance. | ||
391 | 253 | :rtype: IntByte | ||
392 | 254 | |||
393 | 255 | """ | ||
394 | 256 | def __init__(self,name,val=0): | ||
395 | 257 | self.name=name | ||
396 | 258 | self.val=val | ||
397 | 259 | |||
398 | 260 | def encode (self): | ||
399 | 261 | val=pack(">b",self.val) | ||
400 | 262 | return val | ||
401 | 263 | |||
402 | 264 | def decode(self,data): | ||
403 | 265 | self.val= unpack(">b",data[:1])[0] | ||
404 | 266 | return data[1:] | ||
405 | 267 | |||
406 | 268 | def __len__(self): | ||
407 | 269 | return 1 | ||
408 | 270 | |||
409 | 271 | def show(self,depth=0): | ||
410 | 272 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
411 | 273 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
412 | 274 | |||
413 | 275 | class UIntByte: | ||
414 | 276 | """Class representing a single byte as an unsigned integer. | ||
415 | 277 | |||
416 | 278 | :param name: The name of the instance | ||
417 | 279 | :type name: str | ||
418 | 280 | :param val: the integer value. | ||
419 | 281 | :type val: int | ||
420 | 282 | :returns: UIntByte instance. | ||
421 | 283 | :rtype: UIntByte | ||
422 | 284 | |||
423 | 285 | """ | ||
424 | 286 | def __init__(self,name,val=0): | ||
425 | 287 | self.name=name | ||
426 | 288 | self.val=val | ||
427 | 289 | |||
428 | 290 | def encode (self): | ||
429 | 291 | val=pack(">B",self.val) | ||
430 | 292 | return val | ||
431 | 293 | |||
432 | 294 | def decode(self,data): | ||
433 | 295 | self.val= unpack(">B",data[:1])[0] | ||
434 | 296 | return data[1:] | ||
435 | 297 | |||
436 | 298 | def __len__(self): | ||
437 | 299 | return 1 | ||
438 | 300 | |||
439 | 301 | def show(self,depth=0): | ||
440 | 302 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
441 | 303 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
442 | 304 | |||
443 | 305 | class ShortInt: | ||
444 | 306 | """Class representing 2 bytes as a signed integer. | ||
445 | 307 | |||
446 | 308 | :param name: The name of the instance | ||
447 | 309 | :type name: str | ||
448 | 310 | :param val: the integer value. | ||
449 | 311 | :type val: int | ||
450 | 312 | :param endian: Endianess of the bytes. "big" or no "big" (i.e. "little") | ||
451 | 313 | :type endian: str | ||
452 | 314 | :returns: ShortInt instance. | ||
453 | 315 | :rtype: ShortInt | ||
454 | 316 | |||
455 | 317 | """ | ||
456 | 318 | def __init__(self,name,val=0,endian="big"): | ||
457 | 319 | self.name=name | ||
458 | 320 | self.val=val | ||
459 | 321 | self.endian = endian | ||
460 | 322 | |||
461 | 323 | def encode (self): | ||
462 | 324 | if self.endian == "big": | ||
463 | 325 | val=pack(">h",self.val) | ||
464 | 326 | else: | ||
465 | 327 | val=pack("<h",self.val) | ||
466 | 328 | return val | ||
467 | 329 | |||
468 | 330 | def decode(self,data): | ||
469 | 331 | if self.endian == "big": | ||
470 | 332 | self.val= unpack(">h",data[:2])[0] | ||
471 | 333 | else: | ||
472 | 334 | self.val= unpack("<h",data[:2])[0] | ||
473 | 335 | return data[2:] | ||
474 | 336 | |||
475 | 337 | def __len__(self): | ||
476 | 338 | return 2 | ||
477 | 339 | |||
478 | 340 | def show(self,depth=0): | ||
479 | 341 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
480 | 342 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
481 | 343 | |||
482 | 344 | class UShortInt: | ||
483 | 345 | """Class representing 2 bytes as an unsigned integer. | ||
484 | 346 | |||
485 | 347 | :param name: The name of the instance | ||
486 | 348 | :type name: str | ||
487 | 349 | :param val: the integer value. | ||
488 | 350 | :type val: int | ||
489 | 351 | :param endian: Endianess of the bytes. "big" or no "big" (i.e. "little") | ||
490 | 352 | :type endian: str | ||
491 | 353 | :returns: UShortInt instance. | ||
492 | 354 | :rtype: UShortInt | ||
493 | 355 | |||
494 | 356 | """ | ||
495 | 357 | def __init__(self,name,val=0,endian="big"): | ||
496 | 358 | self.name=name | ||
497 | 359 | self.val=val | ||
498 | 360 | self.endian = endian | ||
499 | 361 | |||
500 | 362 | def encode (self): | ||
501 | 363 | if self.endian == "big": | ||
502 | 364 | val=pack(">H",self.val) | ||
503 | 365 | else: | ||
504 | 366 | val=pack("<H",self.val) | ||
505 | 367 | return val | ||
506 | 368 | |||
507 | 369 | def decode(self,data): | ||
508 | 370 | if self.endian == "big": | ||
509 | 371 | self.val= unpack(">H",data[:2])[0] | ||
510 | 372 | else: | ||
511 | 373 | self.val= unpack("<H",data[:2])[0] | ||
512 | 374 | return data[2:] | ||
513 | 375 | |||
514 | 376 | def __len__(self): | ||
515 | 377 | return 2 | ||
516 | 378 | |||
517 | 379 | def show(self,depth=0): | ||
518 | 380 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
519 | 381 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
520 | 382 | |||
521 | 383 | class LongInt: | ||
522 | 384 | """Class representing 4 bytes as a signed integer. | ||
523 | 385 | |||
524 | 386 | :param name: The name of the instance | ||
525 | 387 | :type name: str | ||
526 | 388 | :param val: the integer value. | ||
527 | 389 | :type val: int | ||
528 | 390 | :param endian: Endianess of the bytes. "big" or no "big" (i.e. "little") | ||
529 | 391 | :type endian: str | ||
530 | 392 | :returns: LongInt instance. | ||
531 | 393 | :rtype: LongInt | ||
532 | 394 | |||
533 | 395 | """ | ||
534 | 396 | def __init__(self,name,val=0,endian="big"): | ||
535 | 397 | self.name=name | ||
536 | 398 | self.val=val | ||
537 | 399 | self.endian = endian | ||
538 | 400 | |||
539 | 401 | def encode (self): | ||
540 | 402 | if self.endian == "big": | ||
541 | 403 | val=pack(">l",self.val) | ||
542 | 404 | else: | ||
543 | 405 | val=pack("<l",self.val) | ||
544 | 406 | return val | ||
545 | 407 | |||
546 | 408 | def decode(self,data): | ||
547 | 409 | if self.endian == "big": | ||
548 | 410 | self.val= unpack(">l",data[:4])[0] | ||
549 | 411 | else: | ||
550 | 412 | self.val= unpack("<l",data[:4])[0] | ||
551 | 413 | return data[4:] | ||
552 | 414 | |||
553 | 415 | def __len__(self): | ||
554 | 416 | return 4 | ||
555 | 417 | |||
556 | 418 | def show(self,depth=0): | ||
557 | 419 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
558 | 420 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
559 | 421 | |||
560 | 422 | class ULongInt: | ||
561 | 423 | """Class representing 4 bytes as an unsigned integer. | ||
562 | 424 | |||
563 | 425 | :param name: The name of the instance | ||
564 | 426 | :type name: str | ||
565 | 427 | :param val: the integer value. | ||
566 | 428 | :type val: int | ||
567 | 429 | :param endian: Endianess of the bytes. "big" or no "big" (i.e. "little") | ||
568 | 430 | :type endian: str | ||
569 | 431 | :returns: ULongInt instance. | ||
570 | 432 | :rtype: ULongInt | ||
571 | 433 | |||
572 | 434 | """ | ||
573 | 435 | def __init__(self,name,val=0,endian="big"): | ||
574 | 436 | self.name=name | ||
575 | 437 | self.val=val | ||
576 | 438 | self.endian = endian | ||
577 | 439 | |||
578 | 440 | def encode (self): | ||
579 | 441 | if self.endian == "big": | ||
580 | 442 | val=pack(">L",self.val) | ||
581 | 443 | else: | ||
582 | 444 | val=pack("<L",self.val) | ||
583 | 445 | return val | ||
584 | 446 | |||
585 | 447 | def decode(self,data): | ||
586 | 448 | if self.endian == "big": | ||
587 | 449 | self.val= unpack(">L",data[:4])[0] | ||
588 | 450 | else: | ||
589 | 451 | self.val= unpack("<L",data[:4])[0] | ||
590 | 452 | return data[4:] | ||
591 | 453 | |||
592 | 454 | def __len__(self): | ||
593 | 455 | return 4 | ||
594 | 456 | |||
595 | 457 | def show(self,depth=0): | ||
596 | 458 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
597 | 459 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
598 | 460 | |||
599 | 461 | class OgfOcf: | ||
600 | 462 | """Class representing the 2 bytes that specify the command in an HCI command packet. | ||
601 | 463 | |||
602 | 464 | :param name: The name of the instance | ||
603 | 465 | :type name: str | ||
604 | 466 | :param ogf: the Op-code Group (6 bits). | ||
605 | 467 | :type ogf: bytes | ||
606 | 468 | :param ocf: the Op-code Command (10 bits). | ||
607 | 469 | :type ocf: bytes | ||
608 | 470 | :returns: OgfOcf instance. | ||
609 | 471 | :rtype: OgfOcf | ||
610 | 472 | |||
611 | 473 | """ | ||
612 | 474 | def __init__(self,name,ogf=b"\x00",ocf=b"\x00"): | ||
613 | 475 | self.name=name | ||
614 | 476 | self.ogf= ogf | ||
615 | 477 | self.ocf= ocf | ||
616 | 478 | |||
617 | 479 | def encode (self): | ||
618 | 480 | val=pack("<H",(ord(self.ogf) << 10) | ord(self.ocf)) | ||
619 | 481 | return val | ||
620 | 482 | |||
621 | 483 | def decode(self,data): | ||
622 | 484 | val = unpack("<H",data[:len(self)])[0] | ||
623 | 485 | self.ogf =val>>10 | ||
624 | 486 | self.ocf = int(val - (self.ogf<<10)).to_bytes(1,"big") | ||
625 | 487 | self.ogf = int(self.ogf).to_bytes(1,"big") | ||
626 | 488 | return data[len(self):] | ||
627 | 489 | |||
628 | 490 | def __len__(self): | ||
629 | 491 | return calcsize("<H") | ||
630 | 492 | |||
631 | 493 | def show(self,depth=0): | ||
632 | 494 | print("{}Cmd Group:".format(PRINT_INDENT*depth)) | ||
633 | 495 | print("{}{}".format(PRINT_INDENT*(depth+1),self.ogf)) | ||
634 | 496 | print("{}Cmd Code:".format(PRINT_INDENT*depth)) | ||
635 | 497 | print("{}{}".format(PRINT_INDENT*(depth+1),self.ocf)) | ||
636 | 498 | |||
637 | 499 | class Itself: | ||
638 | 500 | """Class representing a byte array that need no manipulation. | ||
639 | 501 | |||
640 | 502 | :param name: The name of the instance | ||
641 | 503 | :type name: str | ||
642 | 504 | :returns: Itself instance. | ||
643 | 505 | :rtype: Itself | ||
644 | 506 | |||
645 | 507 | """ | ||
646 | 508 | def __init__(self,name): | ||
647 | 509 | self.name=name | ||
648 | 510 | self.val=b"" | ||
649 | 511 | |||
650 | 512 | def encode(self): | ||
651 | 513 | val=pack(">%ds"%len(self.val),self.val) | ||
652 | 514 | return val | ||
653 | 515 | |||
654 | 516 | def decode(self,data): | ||
655 | 517 | self.val=unpack(">%ds"%len(data),data)[0] | ||
656 | 518 | return b"" | ||
657 | 519 | |||
658 | 520 | def __len__(self): | ||
659 | 521 | return len(self.val) | ||
660 | 522 | |||
661 | 523 | def show(self,depth=0): | ||
662 | 524 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
663 | 525 | print("{}{}".format(PRINT_INDENT*(depth+1),":".join(map(lambda b: format(b, "02x"), self.val)))) | ||
664 | 526 | |||
665 | 527 | class String: | ||
666 | 528 | """Class representing a string. | ||
667 | 529 | |||
668 | 530 | :param name: The name of the instance | ||
669 | 531 | :type name: str | ||
670 | 532 | :returns: String instance. | ||
671 | 533 | :rtype: String | ||
672 | 534 | |||
673 | 535 | """ | ||
674 | 536 | def __init__(self,name): | ||
675 | 537 | self.name=name | ||
676 | 538 | self.val="" | ||
677 | 539 | |||
678 | 540 | def encode(self): | ||
679 | 541 | if isinstance(self.val,str): | ||
680 | 542 | self.val = self.val.encode() | ||
681 | 543 | val=pack(">%ds"%len(self.val),self.val) | ||
682 | 544 | return val | ||
683 | 545 | |||
684 | 546 | def decode(self,data): | ||
685 | 547 | self.val=data | ||
686 | 548 | return b"" | ||
687 | 549 | |||
688 | 550 | def __len__(self): | ||
689 | 551 | return len(self.val) | ||
690 | 552 | |||
691 | 553 | def show(self,depth=0): | ||
692 | 554 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
693 | 555 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
694 | 556 | |||
695 | 557 | |||
696 | 558 | class NBytes: | ||
697 | 559 | """Class representing a byte string. | ||
698 | 560 | |||
699 | 561 | :param name: The name of the instance | ||
700 | 562 | :type name: str | ||
701 | 563 | :param length: The length | ||
702 | 564 | :type length: int | ||
703 | 565 | :returns: NBytes instance. | ||
704 | 566 | :rtype: NBytes | ||
705 | 567 | |||
706 | 568 | """ | ||
707 | 569 | def __init__(self,name,length=2): | ||
708 | 570 | self.name=name | ||
709 | 571 | self.length=length | ||
710 | 572 | self.val=b"" | ||
711 | 573 | |||
712 | 574 | def encode(self): | ||
713 | 575 | val=pack(">%ds"%len(self.length),self.val) | ||
714 | 576 | return val | ||
715 | 577 | |||
716 | 578 | def decode(self,data): | ||
717 | 579 | self.val=unpack(">%ds"%self.length,data[:self.length])[0][::-1] | ||
718 | 580 | return data[self.length:] | ||
719 | 581 | |||
720 | 582 | def __len__(self): | ||
721 | 583 | return self.length | ||
722 | 584 | |||
723 | 585 | def show(self,depth=0): | ||
724 | 586 | if self.name: | ||
725 | 587 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
726 | 588 | print("{}{}".format(PRINT_INDENT*(depth+1),":".join(map(lambda b: format(b, "02x"), self.val)))) | ||
727 | 589 | |||
728 | 590 | def __eq__(self,b): | ||
729 | 591 | return self.val==b | ||
730 | 592 | |||
731 | 593 | class NBytes_List: | ||
732 | 594 | """Class representing a list of bytes string. | ||
733 | 595 | |||
734 | 596 | :param name: The name of the instance | ||
735 | 597 | :type name: str | ||
736 | 598 | :param bytes: Length of the bytes strings (2, 4 or 16) | ||
737 | 599 | :type bytes: int | ||
738 | 600 | :returns: NBytes_List instance. | ||
739 | 601 | :rtype: NBytes_List | ||
740 | 602 | |||
741 | 603 | """ | ||
742 | 604 | def __init__(self,name,bytes=2): | ||
743 | 605 | #Bytes should be one of 2, 4 or 16 | ||
744 | 606 | self.name=name | ||
745 | 607 | self.length=bytes | ||
746 | 608 | self.lonbytes = [] | ||
747 | 609 | |||
748 | 610 | def decode(self,data): | ||
749 | 611 | while data: | ||
750 | 612 | mynbyte=NBytes("",self.length) | ||
751 | 613 | data=mynbyte.decode(data) | ||
752 | 614 | self.lonbytes.append(mynbyte) | ||
753 | 615 | return data | ||
754 | 616 | |||
755 | 617 | def show(self,depth=0): | ||
756 | 618 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
757 | 619 | for x in self.lonbytes: | ||
758 | 620 | x.show(depth+1) | ||
759 | 621 | |||
760 | 622 | def __len__(self): | ||
761 | 623 | return len(self.lonbytes)+self.length | ||
762 | 624 | |||
763 | 625 | def __contains__(self,b): | ||
764 | 626 | for x in self.lonbytes: | ||
765 | 627 | if b == x: | ||
766 | 628 | return True | ||
767 | 629 | |||
768 | 630 | return False | ||
769 | 631 | |||
770 | 632 | class Float88: | ||
771 | 633 | """Class representing a 8.8 fixed point quantity. | ||
772 | 634 | |||
773 | 635 | :param name: The name of the instance | ||
774 | 636 | :type name: str | ||
775 | 637 | :returns: Float88 instance. | ||
776 | 638 | :rtype: Float88 | ||
777 | 639 | |||
778 | 640 | """ | ||
779 | 641 | def __init__(self,name): | ||
780 | 642 | self.name=name | ||
781 | 643 | self.val=0.0 | ||
782 | 644 | |||
783 | 645 | def encode (self): | ||
784 | 646 | val=pack(">h",int(self.val*256)) | ||
785 | 647 | return val | ||
786 | 648 | |||
787 | 649 | def decode(self,data): | ||
788 | 650 | self.val= unpack(">h",data)[0]/256.0 | ||
789 | 651 | return data[2:] | ||
790 | 652 | def __len__(self): | ||
791 | 653 | return 2 | ||
792 | 654 | |||
793 | 655 | def show(self,depth=0): | ||
794 | 656 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
795 | 657 | print("{}{}".format(PRINT_INDENT*(depth+1),self.val)) | ||
796 | 658 | |||
797 | 659 | |||
798 | 660 | |||
799 | 661 | |||
800 | 662 | class EmptyPayload: | ||
801 | 663 | def __init__(self): | ||
802 | 664 | pass | ||
803 | 665 | |||
804 | 666 | def encode(self): | ||
805 | 667 | return b"" | ||
806 | 668 | |||
807 | 669 | def decode(self,data): | ||
808 | 670 | return data | ||
809 | 671 | |||
810 | 672 | def __len__(self): | ||
811 | 673 | return 0 | ||
812 | 674 | |||
813 | 675 | def show(self,depth=0): | ||
814 | 676 | return | ||
815 | 677 | |||
816 | 678 | # | ||
817 | 679 | # Bluetooth starts here | ||
818 | 680 | # | ||
819 | 681 | |||
820 | 682 | class Packet: | ||
821 | 683 | """Class representing a generic HCI packet. | ||
822 | 684 | |||
823 | 685 | :param header: The packet header. | ||
824 | 686 | :type header: bytes | ||
825 | 687 | :returns: Packet instance. | ||
826 | 688 | :rtype: Packet | ||
827 | 689 | |||
828 | 690 | """ | ||
829 | 691 | """A generic packet that will be build fromparts""" | ||
830 | 692 | def __init__(self, header="\x00", fmt=">B"): | ||
831 | 693 | self.header = header | ||
832 | 694 | self.fmt = fmt | ||
833 | 695 | self.payload=[] | ||
834 | 696 | self.raw_data=None | ||
835 | 697 | |||
836 | 698 | def encode (self) : | ||
837 | 699 | return pack(self.fmt, self.header) | ||
838 | 700 | |||
839 | 701 | def decode (self, data): | ||
840 | 702 | try: | ||
841 | 703 | if unpack(self.fmt,data[:calcsize(self.fmt)])[0] == self.header: | ||
842 | 704 | self.raw_data=data | ||
843 | 705 | return data[calcsize(self.fmt):] | ||
844 | 706 | except: | ||
845 | 707 | pass | ||
846 | 708 | return None | ||
847 | 709 | |||
848 | 710 | def retrieve(self,aclass): | ||
849 | 711 | """Look for a specifc class/name in the packet""" | ||
850 | 712 | resu=[] | ||
851 | 713 | for x in self.payload: | ||
852 | 714 | try: | ||
853 | 715 | if isinstance(aclass,str): | ||
854 | 716 | if x.name == aclass: | ||
855 | 717 | resu.append(x) | ||
856 | 718 | else: | ||
857 | 719 | if isinstance(x,aclass): | ||
858 | 720 | resu.append(x) | ||
859 | 721 | |||
860 | 722 | resu+=x.retrieve(aclass) | ||
861 | 723 | except: | ||
862 | 724 | pass | ||
863 | 725 | return resu | ||
864 | 726 | # | ||
865 | 727 | # Commands | ||
866 | 728 | # | ||
867 | 729 | |||
868 | 730 | class HCI_Command(Packet): | ||
869 | 731 | """Class representing a command HCI packet. | ||
870 | 732 | |||
871 | 733 | :param ogf: the Op-code Group (6 bits). | ||
872 | 734 | :type ogf: bytes | ||
873 | 735 | :param ocf: the Op-code Command (10 bits). | ||
874 | 736 | :type ocf: bytes | ||
875 | 737 | :returns: HCI_Command instance. | ||
876 | 738 | :rtype: HCI_Command | ||
877 | 739 | |||
878 | 740 | """ | ||
879 | 741 | |||
880 | 742 | def __init__(self,ogf,ocf): | ||
881 | 743 | super().__init__(HCI_COMMAND) | ||
882 | 744 | self.cmd = OgfOcf("command",ogf,ocf) | ||
883 | 745 | self.payload = [] | ||
884 | 746 | |||
885 | 747 | def encode(self): | ||
886 | 748 | pld=b"" | ||
887 | 749 | for x in self.payload: | ||
888 | 750 | pld+=x.encode() | ||
889 | 751 | plen=len(pld) | ||
890 | 752 | pld=b"".join([super().encode(),self.cmd.encode(),pack(">B",plen),pld]) | ||
891 | 753 | return pld | ||
892 | 754 | |||
893 | 755 | def show(self,depth=0): | ||
894 | 756 | self.cmd.show(depth) | ||
895 | 757 | for x in self.payload: | ||
896 | 758 | x.show(depth+1) | ||
897 | 759 | |||
898 | 760 | class HCI_Cmd_LE_Scan_Enable(HCI_Command): | ||
899 | 761 | """Class representing a command HCI command to enable/disable BLE scanning. | ||
900 | 762 | |||
901 | 763 | :param enable: enable/disable scanning. | ||
902 | 764 | :type enable: bool | ||
903 | 765 | :param filter_dups: filter duplicates. | ||
904 | 766 | :type filter_dups: bool | ||
905 | 767 | :returns: HCI_Cmd_LE_Scan_Enable instance. | ||
906 | 768 | :rtype: HCI_Cmd_LE_Scan_Enable | ||
907 | 769 | |||
908 | 770 | """ | ||
909 | 771 | |||
910 | 772 | def __init__(self,enable=True,filter_dups=True): | ||
911 | 773 | super(self.__class__, self).__init__(b"\x08",b"\x0c") | ||
912 | 774 | self.payload.append(Bool("enable",enable)) | ||
913 | 775 | self.payload.append(Bool("filter",filter_dups)) | ||
914 | 776 | |||
915 | 777 | class HCI_Cmd_LE_Set_Scan_Params(HCI_Command): | ||
916 | 778 | """Class representing an HCI command to set the scanning parameters. | ||
917 | 779 | |||
918 | 780 | This will set a number of parameters related to the scanning functions. For the | ||
919 | 781 | interval and window, it will always silently enforce the Specs that says it should be >= 2.5 ms | ||
920 | 782 | and <= 10.24s. It will also silently enforce window <= interval | ||
921 | 783 | |||
922 | 784 | :param scan_type: Type of scanning. 0 => Passive (default) | ||
923 | 785 | 1 => Active | ||
924 | 786 | :type scan_type: int | ||
925 | 787 | :param interval: Time in ms between the start of a scan and the next scan start. Default 10 | ||
926 | 788 | :type interval: int/float | ||
927 | 789 | :param window: maximum advertising interval in ms. Default 10 | ||
928 | 790 | :type window: int.float | ||
929 | 791 | :param oaddr_type: Type of own address Value 0 => public (default) | ||
930 | 792 | 1 => Random | ||
931 | 793 | 2 => Private with public fallback | ||
932 | 794 | 3 => Private with random fallback | ||
933 | 795 | :type oaddr_type: int | ||
934 | 796 | :param filter: How white list filter is applied. 0 => No filter (Default) | ||
935 | 797 | 1 => sender must be in white list | ||
936 | 798 | 2 => Similar to 0. Some directed advertising may be received. | ||
937 | 799 | 3 => Similar to 1. Some directed advertising may be received. | ||
938 | 800 | :type filter: int | ||
939 | 801 | :returns: HCI_Cmd_LE_Scan_Params instance. | ||
940 | 802 | :rtype: HCI_Cmd_LE_Scan_Params | ||
941 | 803 | |||
942 | 804 | """ | ||
943 | 805 | |||
944 | 806 | def __init__(self,scan_type=0x0,interval=10, window=750, oaddr_type=0,filter=0): | ||
945 | 807 | |||
946 | 808 | super(self.__class__, self).__init__(b"\x08",b"\x0b") | ||
947 | 809 | self.payload.append(EnumByte("scan type",scan_type, | ||
948 | 810 | {0: "Passive", | ||
949 | 811 | 1: "Active"})) | ||
950 | 812 | self.payload.append(UShortInt("Interval",int(round(min(10240,max(2.5,interval))/0.625)),endian="little")) | ||
951 | 813 | self.payload.append(UShortInt("Window",int(round(min(10240,max(2.5,min(interval,window)))/0.625)),endian="little")) | ||
952 | 814 | self.payload.append(EnumByte("own addresss type",oaddr_type, | ||
953 | 815 | {0: "Public", | ||
954 | 816 | 1: "Random", | ||
955 | 817 | 2: "Private IRK or Public", | ||
956 | 818 | 3: "Private IRK or Random"})) | ||
957 | 819 | self.payload.append(EnumByte("filter policy",filter, | ||
958 | 820 | {0: "None", | ||
959 | 821 | 1: "Sender In White List", | ||
960 | 822 | 2: "Almost None", | ||
961 | 823 | 3: "SIWL and some"})) | ||
962 | 824 | |||
963 | 825 | |||
964 | 826 | class HCI_Cmd_LE_Advertise(HCI_Command): | ||
965 | 827 | """Class representing a command HCI command to enable/disable BLE advertising. | ||
966 | 828 | |||
967 | 829 | :param enable: enable/disable advertising. | ||
968 | 830 | :type enable: bool | ||
969 | 831 | :returns: HCI_Cmd_LE_Scan_Enable instance. | ||
970 | 832 | :rtype: HCI_Cmd_LE_Scan_Enable | ||
971 | 833 | |||
972 | 834 | """ | ||
973 | 835 | |||
974 | 836 | def __init__(self,enable=True): | ||
975 | 837 | super(self.__class__, self).__init__(b"\x08",b"\x0a") | ||
976 | 838 | self.payload.append(Bool("enable",enable)) | ||
977 | 839 | |||
978 | 840 | class HCI_Cmd_LE_Set_Advertised_Msg(HCI_Command): | ||
979 | 841 | """Class representing an HCI command to set the advertised content. | ||
980 | 842 | |||
981 | 843 | :param enable: enable/disable advertising. | ||
982 | 844 | :type enable: bool | ||
983 | 845 | :returns: HCI_Cmd_LE_Scan_Enable instance. | ||
984 | 846 | :rtype: HCI_Cmd_LE_Scan_Enable | ||
985 | 847 | |||
986 | 848 | """ | ||
987 | 849 | |||
988 | 850 | def __init__(self,msg=EmptyPayload()): | ||
989 | 851 | super(self.__class__, self).__init__(b"\x08",b"\x08") | ||
990 | 852 | self.payload.append(msg) | ||
991 | 853 | |||
992 | 854 | class HCI_Cmd_LE_Set_Advertised_Params(HCI_Command): | ||
993 | 855 | """Class representing an HCI command to set the advertised parameters. | ||
994 | 856 | |||
995 | 857 | This will set a number of parameters relted to the advertising functions. For the | ||
996 | 858 | min and max intervals, it will always silently enforce the Specs that says it should be >= 20ms | ||
997 | 859 | and <= 10.24s. It will also silently enforce interval_max >= interval_min | ||
998 | 860 | |||
999 | 861 | :param interval_min: minimum advertising interval in ms. Default 500 | ||
1000 | 862 | :type interval_min: int/float | ||
1001 | 863 | :param interval_max: maximum advertising interval in ms. Default 750 | ||
1002 | 864 | :type interval_max: int/float | ||
1003 | 865 | :param adv_type: Type of advertising. Value 0 +> Connectable, Scannable advertising | ||
1004 | 866 | 1 => Connectable directed advertising (High duty cycle) | ||
1005 | 867 | 2 => Scannable Undirected advertising | ||
1006 | 868 | 3 => Non connectable undirected advertising (default) | ||
1007 | 869 | :type adv_type: int | ||
1008 | 870 | :param oaddr_type: Type of own address Value 0 => public (default) | ||
1009 | 871 | 1 => Random | ||
1010 | 872 | 2 => Private with public fallback | ||
1011 | 873 | 3 => Private with random fallback | ||
1012 | 874 | :type oaddr_type: int | ||
1013 | 875 | :param paddr_type: Type of peer address Value 0 => public (default) | ||
1014 | 876 | 1 => Random | ||
1015 | 877 | :type paddr_type: int | ||
1016 | 878 | :param peer_addr: Peer MAC address Default 00:00:00:00:00:00 | ||
1017 | 879 | :type peer_addr: str | ||
1018 | 880 | :param cmap: Channel map. A bit field dfined as "Channel 37","Channel 38","Channel 39","RFU","RFU","RFU","RFU","RFU" | ||
1019 | 881 | Default value is 0x7. The value 0x0 is RFU. | ||
1020 | 882 | :type cmap: int | ||
1021 | 883 | :param filter: How white list filter is applied. 0 => No filter (Default) | ||
1022 | 884 | 1 => scan are filtered | ||
1023 | 885 | 2 => Connection are filtered | ||
1024 | 886 | 3 => scan and connection are filtered | ||
1025 | 887 | :type filter: int | ||
1026 | 888 | :returns: HCI_Cmd_LE_Scan_Params instance. | ||
1027 | 889 | :rtype: HCI_Cmd_LE_Scan_Params | ||
1028 | 890 | |||
1029 | 891 | """ | ||
1030 | 892 | |||
1031 | 893 | def __init__(self,interval_min=500, interval_max=750, | ||
1032 | 894 | adv_type=0x3, oaddr_type=0, paddr_type=0, | ||
1033 | 895 | peer_addr="00:00:00:00:00:00", cmap=0x7, filter=0): | ||
1034 | 896 | |||
1035 | 897 | super(self.__class__, self).__init__(b"\x08",b"\x06") | ||
1036 | 898 | self.payload.append(UShortInt("Adv minimum",int(round(min(10240,max(20,interval_min))/0.625)),endian="little")) | ||
1037 | 899 | self.payload.append(UShortInt("Adv maximum",int(round(min(10240,max(20,max(interval_min,interval_max)))/0.625)),endian="little")) | ||
1038 | 900 | self.payload.append(EnumByte("adv type",adv_type, | ||
1039 | 901 | {0: "ADV_IND", | ||
1040 | 902 | 1: "ADV_DIRECT_IND high", | ||
1041 | 903 | 2: "ADV_SCAN_IND", | ||
1042 | 904 | 3: "ADV_NONCONN_IND", | ||
1043 | 905 | 4: "ADV_DIRECT_IND low"})) | ||
1044 | 906 | self.payload.append(EnumByte("own addresss type",paddr_type, | ||
1045 | 907 | {0: "Public", | ||
1046 | 908 | 1: "Random", | ||
1047 | 909 | 2: "Private IRK or Public", | ||
1048 | 910 | 3: "Private IRK or Random"})) | ||
1049 | 911 | self.payload.append(EnumByte("peer addresss type",oaddr_type, | ||
1050 | 912 | {0: "Public", | ||
1051 | 913 | 1: "Random"})) | ||
1052 | 914 | self.payload.append(MACAddr("peer",mac=peer_addr)) | ||
1053 | 915 | self.payload.append(BitFieldByte("Channels",cmap,["Channel 37","Channel 38","Channel 39","RFU","RFU","RFU","RFU", "RFU"])) | ||
1054 | 916 | |||
1055 | 917 | self.payload.append(EnumByte("filter policy",filter, | ||
1056 | 918 | {0: "None", | ||
1057 | 919 | 1: "Scan", | ||
1058 | 920 | 2: "Connection", | ||
1059 | 921 | 3: "Scan and Connection"})) | ||
1060 | 922 | |||
1061 | 923 | class HCI_Cmd_Reset(HCI_Command): | ||
1062 | 924 | """Class representing an HCI command to reset the adapater. | ||
1063 | 925 | |||
1064 | 926 | |||
1065 | 927 | :returns: HCI_Cmd_Reset instance. | ||
1066 | 928 | :rtype: HCI_Cmd_Reset | ||
1067 | 929 | |||
1068 | 930 | """ | ||
1069 | 931 | |||
1070 | 932 | def __init__(self): | ||
1071 | 933 | super(self.__class__, self).__init__(b"\x03",b"\x03") | ||
1072 | 934 | |||
1073 | 935 | |||
1074 | 936 | #### | ||
1075 | 937 | # HCI EVents | ||
1076 | 938 | #### | ||
1077 | 939 | |||
1078 | 940 | class HCI_Event(Packet): | ||
1079 | 941 | |||
1080 | 942 | def __init__(self,code=0,payload=[]): | ||
1081 | 943 | super().__init__(HCI_EVENT) | ||
1082 | 944 | self.payload.append(Byte("code")) | ||
1083 | 945 | self.payload.append(UIntByte("length")) | ||
1084 | 946 | |||
1085 | 947 | def decode(self,data): | ||
1086 | 948 | data=super().decode(data) | ||
1087 | 949 | if data is None: | ||
1088 | 950 | return None | ||
1089 | 951 | |||
1090 | 952 | for x in self.payload: | ||
1091 | 953 | x.decode(data[:len(x)]) | ||
1092 | 954 | data=data[len(x):] | ||
1093 | 955 | code=self.payload[0] | ||
1094 | 956 | length=self.payload[1].val | ||
1095 | 957 | if code.val==b"\x0e": | ||
1096 | 958 | ev = HCI_CC_Event() | ||
1097 | 959 | data=ev.decode(data) | ||
1098 | 960 | self.payload.append(ev) | ||
1099 | 961 | elif code.val==b"\x3e": | ||
1100 | 962 | ev = HCI_LE_Meta_Event() | ||
1101 | 963 | data=ev.decode(data) | ||
1102 | 964 | self.payload.append(ev) | ||
1103 | 965 | else: | ||
1104 | 966 | ev=Itself("Payload") | ||
1105 | 967 | data=ev.decode(data) | ||
1106 | 968 | self.payload.append(ev) | ||
1107 | 969 | return data | ||
1108 | 970 | |||
1109 | 971 | def show(self,depth=0): | ||
1110 | 972 | print("{}HCI Event:".format(PRINT_INDENT*depth)) | ||
1111 | 973 | for x in self.payload: | ||
1112 | 974 | x.show(depth+1) | ||
1113 | 975 | |||
1114 | 976 | |||
1115 | 977 | class HCI_CC_Event(Packet): | ||
1116 | 978 | """Command Complete event""" | ||
1117 | 979 | def __init__(self): | ||
1118 | 980 | self.name="Command Completed" | ||
1119 | 981 | self.payload=[UIntByte("allow pkt"),OgfOcf("cmd"),Itself("resp code")] | ||
1120 | 982 | |||
1121 | 983 | |||
1122 | 984 | def decode(self,data): | ||
1123 | 985 | for x in self.payload: | ||
1124 | 986 | data=x.decode(data) | ||
1125 | 987 | return data | ||
1126 | 988 | |||
1127 | 989 | def show(self,depth=0): | ||
1128 | 990 | for x in self.payload: | ||
1129 | 991 | x.show(depth+1) | ||
1130 | 992 | |||
1131 | 993 | class HCI_LE_Meta_Event(Packet): | ||
1132 | 994 | def __init__(self): | ||
1133 | 995 | self.name="LE Meta" | ||
1134 | 996 | self.payload=[Byte("code")] | ||
1135 | 997 | |||
1136 | 998 | def decode(self,data): | ||
1137 | 999 | for x in self.payload: | ||
1138 | 1000 | data=x.decode(data) | ||
1139 | 1001 | code=self.payload[0] | ||
1140 | 1002 | if code.val==b"\x02": | ||
1141 | 1003 | ev=HCI_LEM_Adv_Report() | ||
1142 | 1004 | data=ev.decode(data) | ||
1143 | 1005 | self.payload.append(ev) | ||
1144 | 1006 | else: | ||
1145 | 1007 | ev=Itself("Payload") | ||
1146 | 1008 | data=ev.decode(data) | ||
1147 | 1009 | self.payload.append(ev) | ||
1148 | 1010 | return data | ||
1149 | 1011 | |||
1150 | 1012 | def show(self,depth=0): | ||
1151 | 1013 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
1152 | 1014 | for x in self.payload: | ||
1153 | 1015 | x.show(depth+1) | ||
1154 | 1016 | |||
1155 | 1017 | |||
1156 | 1018 | class HCI_LEM_Adv_Report(Packet): | ||
1157 | 1019 | def __init__(self): | ||
1158 | 1020 | self.name="Adv Report" | ||
1159 | 1021 | self.payload=[UIntByte("num reports"), | ||
1160 | 1022 | EnumByte("ev type",0,{0:"generic adv", 3:"no connection adv", 4:"scan rsp"}), | ||
1161 | 1023 | EnumByte("addr type",0,{0:"public", 1:"random"}), | ||
1162 | 1024 | MACAddr("peer"),UIntByte("length")] | ||
1163 | 1025 | |||
1164 | 1026 | |||
1165 | 1027 | def decode(self,data): | ||
1166 | 1028 | |||
1167 | 1029 | for x in self.payload: | ||
1168 | 1030 | data=x.decode(data) | ||
1169 | 1031 | #Now we have a sequence of len, type data with possibly a RSSI byte at the end | ||
1170 | 1032 | while len(data) > 1: | ||
1171 | 1033 | length=UIntByte("sublen") | ||
1172 | 1034 | data=length.decode(data) | ||
1173 | 1035 | code=EIR_Hdr() | ||
1174 | 1036 | data=code.decode(data) | ||
1175 | 1037 | |||
1176 | 1038 | if code.val == 0x01: | ||
1177 | 1039 | #Flag | ||
1178 | 1040 | myinfo=BitFieldByte("flags",0,["Undef","Undef","Simul LE - BR/EDR (Host)","Simul LE - BR/EDR (Control.)","BR/EDR Not Supported", | ||
1179 | 1041 | "LE General Disc.","LE Limited Disc."]) | ||
1180 | 1042 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1181 | 1043 | self.payload.append(myinfo) | ||
1182 | 1044 | elif code.val == 0x02: | ||
1183 | 1045 | myinfo=NBytes_List("Incomplete uuids",2) | ||
1184 | 1046 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1185 | 1047 | self.payload.append(myinfo) | ||
1186 | 1048 | elif code.val == 0x03: | ||
1187 | 1049 | myinfo=NBytes_List("Complete uuids",2) | ||
1188 | 1050 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1189 | 1051 | self.payload.append(myinfo) | ||
1190 | 1052 | elif code.val == 0x04: | ||
1191 | 1053 | myinfo=NBytes_List("Incomplete uuids",4) | ||
1192 | 1054 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1193 | 1055 | self.payload.append(myinfo) | ||
1194 | 1056 | elif code.val == 0x05: | ||
1195 | 1057 | myinfo=NBytes_List("Complete uuids",4) | ||
1196 | 1058 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1197 | 1059 | self.payload.append(myinfo) | ||
1198 | 1060 | elif code.val == 0x06: | ||
1199 | 1061 | myinfo=NBytes_List("Incomplete uuids",16) | ||
1200 | 1062 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1201 | 1063 | self.payload.append(myinfo) | ||
1202 | 1064 | elif code.val == 0x07: | ||
1203 | 1065 | myinfo=NBytes_List("Complete uuids",16) | ||
1204 | 1066 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1205 | 1067 | self.payload.append(myinfo) | ||
1206 | 1068 | elif code.val == 0x14: | ||
1207 | 1069 | myinfo=NBytes_List("Service Solicitation uuid",2) | ||
1208 | 1070 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1209 | 1071 | self.payload.append(myinfo) | ||
1210 | 1072 | elif code.val == 0x16: | ||
1211 | 1073 | myinfo=Adv_Data("Advertised Data",2) | ||
1212 | 1074 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1213 | 1075 | self.payload.append(myinfo) | ||
1214 | 1076 | elif code.val == 0x1f: | ||
1215 | 1077 | myinfo=NBytes_List("Service Solicitation uuid",4) | ||
1216 | 1078 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1217 | 1079 | self.payload.append(myinfo) | ||
1218 | 1080 | elif code.val == 0x20: | ||
1219 | 1081 | myinfo=Adv_Data("Advertised Data",4) | ||
1220 | 1082 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1221 | 1083 | self.payload.append(myinfo) | ||
1222 | 1084 | elif code.val == 0x15: | ||
1223 | 1085 | myinfo=NBytes_List("Service Solicitation uuid",16) | ||
1224 | 1086 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1225 | 1087 | self.payload.append(myinfo) | ||
1226 | 1088 | elif code.val == 0x21: | ||
1227 | 1089 | myinfo=Adv_Data("Advertised Data",16) | ||
1228 | 1090 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1229 | 1091 | self.payload.append(myinfo) | ||
1230 | 1092 | elif code.val == 0x08: | ||
1231 | 1093 | myinfo=String("Short Name") | ||
1232 | 1094 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1233 | 1095 | self.payload.append(myinfo) | ||
1234 | 1096 | elif code.val == 0x09: | ||
1235 | 1097 | myinfo=String("Complete Name") | ||
1236 | 1098 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1237 | 1099 | self.payload.append(myinfo) | ||
1238 | 1100 | else: | ||
1239 | 1101 | myinfo=Itself("Payload for %s"%code.strval) | ||
1240 | 1102 | xx=myinfo.decode(data[:length.val-len(code)]) | ||
1241 | 1103 | self.payload.append(myinfo) | ||
1242 | 1104 | |||
1243 | 1105 | data=data[length.val-len(code):] | ||
1244 | 1106 | if data: | ||
1245 | 1107 | myinfo=IntByte("rssi") | ||
1246 | 1108 | data=myinfo.decode(data) | ||
1247 | 1109 | self.payload.append(myinfo) | ||
1248 | 1110 | return data | ||
1249 | 1111 | |||
1250 | 1112 | def show(self,depth=0): | ||
1251 | 1113 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
1252 | 1114 | for x in self.payload: | ||
1253 | 1115 | x.show(depth+1) | ||
1254 | 1116 | |||
1255 | 1117 | class EIR_Hdr(Packet): | ||
1256 | 1118 | def __init__(self): | ||
1257 | 1119 | self.type= EnumByte("type", 0, { | ||
1258 | 1120 | 0x01: "flags", | ||
1259 | 1121 | 0x02: "incomplete_list_16_bit_svc_uuids", | ||
1260 | 1122 | 0x03: "complete_list_16_bit_svc_uuids", | ||
1261 | 1123 | 0x04: "incomplete_list_32_bit_svc_uuids", | ||
1262 | 1124 | 0x05: "complete_list_32_bit_svc_uuids", | ||
1263 | 1125 | 0x06: "incomplete_list_128_bit_svc_uuids", | ||
1264 | 1126 | 0x07: "complete_list_128_bit_svc_uuids", | ||
1265 | 1127 | 0x08: "shortened_local_name", | ||
1266 | 1128 | 0x09: "complete_local_name", | ||
1267 | 1129 | 0x0a: "tx_power_level", | ||
1268 | 1130 | 0x0d: "class_of_device", | ||
1269 | 1131 | 0x0e: "simple_pairing_hash", | ||
1270 | 1132 | 0x0f: "simple_pairing_rand", | ||
1271 | 1133 | 0x10: "sec_mgr_tk", | ||
1272 | 1134 | 0x11: "sec_mgr_oob_flags", | ||
1273 | 1135 | 0x12: "slave_conn_intvl_range", | ||
1274 | 1136 | 0x17: "pub_target_addr", | ||
1275 | 1137 | 0x18: "rand_target_addr", | ||
1276 | 1138 | 0x19: "appearance", | ||
1277 | 1139 | 0x1a: "adv_intvl", | ||
1278 | 1140 | 0x1b: "le_addr", | ||
1279 | 1141 | 0x1c: "le_role", | ||
1280 | 1142 | 0x14: "list_16_bit_svc_sollication_uuids", | ||
1281 | 1143 | 0x1f: "list_32_bit_svc_sollication_uuids", | ||
1282 | 1144 | 0x15: "list_128_bit_svc_sollication_uuids", | ||
1283 | 1145 | 0x16: "svc_data_16_bit_uuid", | ||
1284 | 1146 | 0x20: "svc_data_32_bit_uuid", | ||
1285 | 1147 | 0x21: "svc_data_128_bit_uuid", | ||
1286 | 1148 | 0x22: "sec_conn_confirm", | ||
1287 | 1149 | 0x23: "sec_conn_rand", | ||
1288 | 1150 | 0x24: "uri", | ||
1289 | 1151 | 0xff: "mfg_specific_data", | ||
1290 | 1152 | }) | ||
1291 | 1153 | |||
1292 | 1154 | def decode(self,data): | ||
1293 | 1155 | return self.type.decode(data) | ||
1294 | 1156 | |||
1295 | 1157 | def show(self): | ||
1296 | 1158 | return self.type.show() | ||
1297 | 1159 | |||
1298 | 1160 | @property | ||
1299 | 1161 | def val(self): | ||
1300 | 1162 | return self.type.val | ||
1301 | 1163 | |||
1302 | 1164 | @property | ||
1303 | 1165 | def strval(self): | ||
1304 | 1166 | return self.type.strval | ||
1305 | 1167 | |||
1306 | 1168 | def __len__(self): | ||
1307 | 1169 | return len(self.type) | ||
1308 | 1170 | |||
1309 | 1171 | class Adv_Data(Packet): | ||
1310 | 1172 | def __init__(self,name,length): | ||
1311 | 1173 | self.name=name | ||
1312 | 1174 | self.length=length | ||
1313 | 1175 | self.payload=[] | ||
1314 | 1176 | |||
1315 | 1177 | def decode(self,data): | ||
1316 | 1178 | myinfo=NBytes("Service Data uuid",self.length) | ||
1317 | 1179 | data=myinfo.decode(data) | ||
1318 | 1180 | self.payload.append(myinfo) | ||
1319 | 1181 | if data: | ||
1320 | 1182 | myinfo=Itself("Adv Payload") | ||
1321 | 1183 | data=myinfo.decode(data) | ||
1322 | 1184 | self.payload.append(myinfo) | ||
1323 | 1185 | return data | ||
1324 | 1186 | |||
1325 | 1187 | def show(self,depth=0): | ||
1326 | 1188 | print("{}{}:".format(PRINT_INDENT*depth,self.name)) | ||
1327 | 1189 | for x in self.payload: | ||
1328 | 1190 | x.show(depth+1) | ||
1329 | 1191 | |||
1330 | 1192 | def __len__(self): | ||
1331 | 1193 | resu=0 | ||
1332 | 1194 | for x in self.payload: | ||
1333 | 1195 | resu+=len(x) | ||
1334 | 1196 | return resu | ||
1335 | 1197 | |||
1336 | 1198 | |||
1337 | 1199 | |||
1338 | 1200 | # | ||
1339 | 1201 | # The defs are over. Now the realstuffs | ||
1340 | 1202 | # | ||
1341 | 1203 | |||
1342 | 1204 | def create_bt_socket(interface=0): | ||
1343 | 1205 | exceptions = [] | ||
1344 | 1206 | sock = None | ||
1345 | 1207 | try: | ||
1346 | 1208 | sock = socket.socket(family=socket.AF_BLUETOOTH, | ||
1347 | 1209 | type=socket.SOCK_RAW, | ||
1348 | 1210 | proto=socket.BTPROTO_HCI) | ||
1349 | 1211 | sock.setblocking(False) | ||
1350 | 1212 | sock.setsockopt(socket.SOL_HCI, socket.HCI_FILTER, pack("IIIh2x", 0xffffffff,0xffffffff,0xffffffff,0)) #type mask, event mask, event mask, opcode | ||
1351 | 1213 | try: | ||
1352 | 1214 | sock.bind((interface,)) | ||
1353 | 1215 | except OSError as exc: | ||
1354 | 1216 | exc = OSError( | ||
1355 | 1217 | exc.errno, 'error while attempting to bind on ' | ||
1356 | 1218 | 'interface {!r}: {}'.format( | ||
1357 | 1219 | interface, exc.strerror)) | ||
1358 | 1220 | exceptions.append(exc) | ||
1359 | 1221 | except OSError as exc: | ||
1360 | 1222 | if sock is not None: | ||
1361 | 1223 | sock.close() | ||
1362 | 1224 | exceptions.append(exc) | ||
1363 | 1225 | except: | ||
1364 | 1226 | if sock is not None: | ||
1365 | 1227 | sock.close() | ||
1366 | 1228 | raise | ||
1367 | 1229 | if len(exceptions) == 1: | ||
1368 | 1230 | raise exceptions[0] | ||
1369 | 1231 | elif len(exceptions) > 1: | ||
1370 | 1232 | model = str(exceptions[0]) | ||
1371 | 1233 | if all(str(exc) == model for exc in exceptions): | ||
1372 | 1234 | raise exceptions[0] | ||
1373 | 1235 | raise OSError('Multiple exceptions: {}'.format( | ||
1374 | 1236 | ', '.join(str(exc) for exc in exceptions))) | ||
1375 | 1237 | return sock | ||
1376 | 1238 | |||
1377 | 1239 | ########### | ||
1378 | 1240 | |||
1379 | 1241 | class BLEScanRequester(asyncio.Protocol): | ||
1380 | 1242 | '''Protocol handling the requests''' | ||
1381 | 1243 | def __init__(self): | ||
1382 | 1244 | self.transport = None | ||
1383 | 1245 | self.smac = None | ||
1384 | 1246 | self.sip = None | ||
1385 | 1247 | self.process = self.default_process | ||
1386 | 1248 | |||
1387 | 1249 | def connection_made(self, transport): | ||
1388 | 1250 | self.transport = transport | ||
1389 | 1251 | command=HCI_Cmd_LE_Set_Scan_Params() | ||
1390 | 1252 | self.transport.write(command.encode()) | ||
1391 | 1253 | |||
1392 | 1254 | def connection_lost(self, exc): | ||
1393 | 1255 | super().connection_lost(exc) | ||
1394 | 1256 | |||
1395 | 1257 | def send_scan_request(self): | ||
1396 | 1258 | '''Sending LE scan request''' | ||
1397 | 1259 | command=HCI_Cmd_LE_Scan_Enable(True,False) | ||
1398 | 1260 | self.transport.write(command.encode()) | ||
1399 | 1261 | |||
1400 | 1262 | def stop_scan_request(self): | ||
1401 | 1263 | '''Sending LE scan request''' | ||
1402 | 1264 | command=HCI_Cmd_LE_Scan_Enable(False,False) | ||
1403 | 1265 | self.transport.write(command.encode()) | ||
1404 | 1266 | |||
1405 | 1267 | def send_command(self,command): | ||
1406 | 1268 | '''Sending an arbitrary command''' | ||
1407 | 1269 | self.transport.write(command.encode()) | ||
1408 | 1270 | |||
1409 | 1271 | def data_received(self, packet): | ||
1410 | 1272 | self.process(packet) | ||
1411 | 1273 | |||
1412 | 1274 | def default_process(self,data): | ||
1413 | 1275 | pass | ||
1414 | diff --git a/checkbox_support/vendor/aioblescan/eddystone.py b/checkbox_support/vendor/aioblescan/eddystone.py | |||
1415 | 0 | new file mode 100644 | 1276 | new file mode 100644 |
1416 | index 0000000..3cc0d2c | |||
1417 | --- /dev/null | |||
1418 | +++ b/checkbox_support/vendor/aioblescan/eddystone.py | |||
1419 | @@ -0,0 +1,362 @@ | |||
1420 | 1 | #!/usr/bin/env python3 | ||
1421 | 2 | # -*- coding:utf-8 -*- | ||
1422 | 3 | # | ||
1423 | 4 | # This file deal with EddyStone formated message | ||
1424 | 5 | # | ||
1425 | 6 | # Copyright (c) 2017 François Wautier | ||
1426 | 7 | # | ||
1427 | 8 | # Note part of this code was adapted from PyBeacon (https://github.com/nirmankarta/PyBeacon) | ||
1428 | 9 | # | ||
1429 | 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
1430 | 11 | # of this software and associated documentation files (the "Software"), to deal | ||
1431 | 12 | # in the Software without restriction, including without limitation the rights | ||
1432 | 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
1433 | 14 | # of the Software, and to permit persons to whom the Software is furnished to do so, | ||
1434 | 15 | # subject to the following conditions: | ||
1435 | 16 | # | ||
1436 | 17 | # The above copyright notice and this permission notice shall be included in all copies | ||
1437 | 18 | # or substantial portions of the Software. | ||
1438 | 19 | # | ||
1439 | 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
1440 | 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
1441 | 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
1442 | 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
1443 | 24 | # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
1444 | 25 | # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE | ||
1445 | 26 | |||
1446 | 27 | import checkbox_support.vendor.aioblescan as aios | ||
1447 | 28 | from urllib.parse import urlparse | ||
1448 | 29 | from enum import Enum | ||
1449 | 30 | |||
1450 | 31 | # | ||
1451 | 32 | EDDY_UUID=b"\xfe\xaa" #Google UUID | ||
1452 | 33 | |||
1453 | 34 | class ESType(Enum): | ||
1454 | 35 | """Enumerator for Eddystone types.""" | ||
1455 | 36 | uid = 0x00 | ||
1456 | 37 | url = 0x10 | ||
1457 | 38 | tlm = 0x20 | ||
1458 | 39 | eid = 0x30 | ||
1459 | 40 | |||
1460 | 41 | url_schemes = [ | ||
1461 | 42 | ("http",True), | ||
1462 | 43 | ("https",True), | ||
1463 | 44 | ("http",False), | ||
1464 | 45 | ("https",False), | ||
1465 | 46 | ] | ||
1466 | 47 | |||
1467 | 48 | url_domain = [ | ||
1468 | 49 | "com","org","edu","net","info","biz","gov"] | ||
1469 | 50 | |||
1470 | 51 | class EddyStone(object): | ||
1471 | 52 | """Class defining the content of an EddyStone advertisement. | ||
1472 | 53 | |||
1473 | 54 | Here the param type will depend on the type. | ||
1474 | 55 | |||
1475 | 56 | For URL it should be a string with a compatible URL. | ||
1476 | 57 | |||
1477 | 58 | For UID it is a dictionary with 2 keys, "namespace" and "instance", values are bytes . | ||
1478 | 59 | |||
1479 | 60 | For TLM it shall be an dictionary with 4 keys: "battery","temperature", "count" and "uptime". | ||
1480 | 61 | Any missing key shall be replaced by its default value. | ||
1481 | 62 | |||
1482 | 63 | For EID it should me a bytes string of length 8 | ||
1483 | 64 | |||
1484 | 65 | :param type: The type of EddyStone advertisement. From ESType | ||
1485 | 66 | :type type: ESType | ||
1486 | 67 | :oaram param: The payload corresponding to the type | ||
1487 | 68 | |||
1488 | 69 | """ | ||
1489 | 70 | |||
1490 | 71 | def __init__(self, type=ESType.url, param="https://goo.gl/m9UiEA"): | ||
1491 | 72 | self.power = 0 | ||
1492 | 73 | self.payload = [] #As defined in https://github.com/google/eddystone/blob/master/protocol-specification.md | ||
1493 | 74 | self.payload.append(aios.Byte("Flag Length",b'\x02')) | ||
1494 | 75 | self.payload.append(aios.Byte("Flag Data Type",b'\x01')) | ||
1495 | 76 | self.payload.append(aios.Byte("Flag Data",b'\x1a')) | ||
1496 | 77 | self.payload.append(aios.Byte("Length UUID services",b'\x03')) | ||
1497 | 78 | self.payload.append(aios.Byte("Complete List UUID Service",b'\x03')) | ||
1498 | 79 | self.payload.append(aios.Byte("Eddystone UUID",b'\xaa')) | ||
1499 | 80 | self.payload.append(aios.Byte("...",b'\xfe')) | ||
1500 | 81 | self.service_data_length = aios.IntByte("Service Data length",4) | ||
1501 | 82 | self.payload.append(self.service_data_length) | ||
1502 | 83 | self.payload.append(aios.Byte("Service Data data type value",b'\x16')) | ||
1503 | 84 | self.payload.append(aios.Byte("Eddystone UUID",b'\xaa')) | ||
1504 | 85 | self.payload.append(aios.Byte("...",b'\xfe')) | ||
1505 | 86 | self.type = aios.EnumByte("type",type.value,{ESType.uid.value:"Eddystone-UID", | ||
1506 | 87 | ESType.url.value:"Eddystone-URL", | ||
1507 | 88 | ESType.tlm.value:"Eddystone-TLM", | ||
1508 | 89 | ESType.eid.value:"Eddystone-EID"}) | ||
1509 | 90 | self.payload.append(self.type) | ||
1510 | 91 | self.parsed_payload = b'' | ||
1511 | 92 | self.type_payload=param | ||
1512 | 93 | |||
1513 | 94 | def change_type(self, type, param): | ||
1514 | 95 | self.type.val=type.value | ||
1515 | 96 | self.type_payload=param | ||
1516 | 97 | self.service_data_length.val=4 | ||
1517 | 98 | self.parsed_payload = b'' | ||
1518 | 99 | |||
1519 | 100 | def change_type_payload(self, param): | ||
1520 | 101 | self.type_payload=param | ||
1521 | 102 | self.service_data_length.val=4 | ||
1522 | 103 | self.parsed_payload = b'' | ||
1523 | 104 | |||
1524 | 105 | def url_encoder(self): | ||
1525 | 106 | encodedurl = [] | ||
1526 | 107 | encodedurl.append(aios.IntByte("Tx Power",self.power)) | ||
1527 | 108 | asisurl="" | ||
1528 | 109 | myurl = urlparse(self.type_payload) | ||
1529 | 110 | myhostname = myurl.hostname | ||
1530 | 111 | mypath = myurl.path | ||
1531 | 112 | if (myurl.scheme,myhostname.startswith("www.")) in url_schemes: | ||
1532 | 113 | encodedurl.append(aios.IntByte("URL Scheme", | ||
1533 | 114 | url_schemes.index((myurl.scheme,myhostname.startswith("www."))))) | ||
1534 | 115 | if myhostname.startswith("www."): | ||
1535 | 116 | myhostname = myhostname[4:] | ||
1536 | 117 | |||
1537 | 118 | extval=None | ||
1538 | 119 | if myhostname.split(".")[-1] in url_domain: | ||
1539 | 120 | extval = url_domain.index(myhostname.split(".")[-1]) | ||
1540 | 121 | myhostname = ".".join(myhostname.split(".")[:-1]) | ||
1541 | 122 | if extval and not mypath.startswith("/"): | ||
1542 | 123 | extval+=7 | ||
1543 | 124 | else: | ||
1544 | 125 | if myurl.port is None: | ||
1545 | 126 | if extval: | ||
1546 | 127 | mypath = mypath[1:] | ||
1547 | 128 | else: | ||
1548 | 129 | extval += 7 | ||
1549 | 130 | encodedurl.append(aios.String("URL string")) | ||
1550 | 131 | encodedurl[-1].val = myhostname | ||
1551 | 132 | if extval: | ||
1552 | 133 | encodedurl.append(aios.IntByte("URL Extention",extval)) | ||
1553 | 134 | |||
1554 | 135 | if myurl.port: | ||
1555 | 136 | asisurl += ":"+str(myurl.port)+mypath | ||
1556 | 137 | asisurl += mypath | ||
1557 | 138 | if myurl.params: | ||
1558 | 139 | asisurl += ";"+myurl.params | ||
1559 | 140 | if myurl.query: | ||
1560 | 141 | asisurl += "?"+myurl.query | ||
1561 | 142 | if myurl.fragment: | ||
1562 | 143 | asisurl += "#"+myurl.fragment | ||
1563 | 144 | encodedurl.append(aios.String("Rest of URL")) | ||
1564 | 145 | encodedurl[-1].val = asisurl | ||
1565 | 146 | tlength=0 | ||
1566 | 147 | for x in encodedurl: #Check the payload length | ||
1567 | 148 | tlength += len(x) | ||
1568 | 149 | if tlength > 19: #Actually 18 but we have tx power | ||
1569 | 150 | raise Exception("Encoded url too long (max 18 bytes)") | ||
1570 | 151 | self.service_data_length.val += tlength #Update the payload length | ||
1571 | 152 | return encodedurl | ||
1572 | 153 | |||
1573 | 154 | |||
1574 | 155 | def uid_encoder(self): | ||
1575 | 156 | encodedurl = [] | ||
1576 | 157 | encodedurl.append(aios.IntByte("Tx Power",self.power)) | ||
1577 | 158 | encodedurl.append(aios.NBytes("Namespace",10)) | ||
1578 | 159 | encodedurl[-1].val = self.type_payload["namespace"] | ||
1579 | 160 | encodedurl.append(aios.NBytes("Instance",6)) | ||
1580 | 161 | encodedurl[-1].val = self.type_payload["instance"] | ||
1581 | 162 | encodedurl.append(aios.NBytes("RFU",2)) | ||
1582 | 163 | encodedurl[-1].val = b'\x00\x00' | ||
1583 | 164 | self.service_data_length.val = 23 #Update the payload length/ways the same for uid | ||
1584 | 165 | return encodedurl | ||
1585 | 166 | |||
1586 | 167 | def tlm_encoder(self): | ||
1587 | 168 | encodedurl = [] | ||
1588 | 169 | encodedurl.append(aios.NBytes("VBATT",2)) | ||
1589 | 170 | if "battery" in self.type_payload: | ||
1590 | 171 | encodedurl[-1].val = self.type_payload["battery"] | ||
1591 | 172 | else: | ||
1592 | 173 | encodedurl[-1].val = -128 | ||
1593 | 174 | encodedurl.append(aios.Float88("Temperature")) | ||
1594 | 175 | if "temperature" in self.type_payload: | ||
1595 | 176 | encodedurl[-1].val = self.type_payload["temperature"] | ||
1596 | 177 | else: | ||
1597 | 178 | encodedurl[-1].val = -128 | ||
1598 | 179 | |||
1599 | 180 | encodedurl.append(aios.ULongInt("Count")) | ||
1600 | 181 | if "count" in self.type_payload: | ||
1601 | 182 | encodedurl[-1].val = self.type_payload["count"] | ||
1602 | 183 | else: | ||
1603 | 184 | encodedurl[-1].val = 0 | ||
1604 | 185 | |||
1605 | 186 | encodedurl.append(aios.ULongInt("Uptime")) | ||
1606 | 187 | if "uptime" in self.type_payload: | ||
1607 | 188 | encodedurl[-1].val = self.type_payload["uptime"] | ||
1608 | 189 | else: | ||
1609 | 190 | encodedurl[-1].val = 0 | ||
1610 | 191 | return encodedurl | ||
1611 | 192 | |||
1612 | 193 | |||
1613 | 194 | def eid_encoder(self): | ||
1614 | 195 | encodedurl = [] | ||
1615 | 196 | encodedurl.append(aios.IntByte("Tx Power",self.power)) | ||
1616 | 197 | encodedurl.append(aios.NBytes("Namespace",8)) | ||
1617 | 198 | encodedurl[-1].val = self.type_payload | ||
1618 | 199 | self.service_data_length.val = 13 | ||
1619 | 200 | return encodedurl | ||
1620 | 201 | |||
1621 | 202 | |||
1622 | 203 | def encode(self): | ||
1623 | 204 | #Generate the payload | ||
1624 | 205 | if self.type.val == ESType.uid.value: | ||
1625 | 206 | espayload = self.uid_encoder() | ||
1626 | 207 | elif self.type.val == ESType.url.value: | ||
1627 | 208 | espayload = self.url_encoder() | ||
1628 | 209 | elif self.type.val == ESType.tlm.value: | ||
1629 | 210 | espayload = self.tlm_encoder() | ||
1630 | 211 | elif self.type.val == ESType.eid.value: | ||
1631 | 212 | espayload = self.eid_encoder() | ||
1632 | 213 | encmsg=b'' | ||
1633 | 214 | for x in self.payload+espayload: | ||
1634 | 215 | encmsg += x.encode() | ||
1635 | 216 | mylen=aios.IntByte("Length",len(encmsg)) | ||
1636 | 217 | encmsg = mylen.encode()+encmsg | ||
1637 | 218 | for x in range(32-len(encmsg)): | ||
1638 | 219 | encmsg+=b'\x00' | ||
1639 | 220 | return encmsg | ||
1640 | 221 | |||
1641 | 222 | def decode(self, packet): | ||
1642 | 223 | """Check a parsed packet and figure out if it is an Eddystone Beacon. | ||
1643 | 224 | If it is , return the relevant data as a dictionary. | ||
1644 | 225 | |||
1645 | 226 | Return None, it is not an Eddystone Beacon advertising packet""" | ||
1646 | 227 | |||
1647 | 228 | ssu=packet.retrieve("Complete uuids") | ||
1648 | 229 | found=False | ||
1649 | 230 | for x in ssu: | ||
1650 | 231 | if EDDY_UUID in x: | ||
1651 | 232 | found=True | ||
1652 | 233 | break | ||
1653 | 234 | if not found: | ||
1654 | 235 | return None | ||
1655 | 236 | |||
1656 | 237 | found=False | ||
1657 | 238 | adv=packet.retrieve("Advertised Data") | ||
1658 | 239 | for x in adv: | ||
1659 | 240 | luuid=x.retrieve("Service Data uuid") | ||
1660 | 241 | for uuid in luuid: | ||
1661 | 242 | if EDDY_UUID == uuid: | ||
1662 | 243 | found=x | ||
1663 | 244 | break | ||
1664 | 245 | if found: | ||
1665 | 246 | break | ||
1666 | 247 | |||
1667 | 248 | |||
1668 | 249 | if not found: | ||
1669 | 250 | return None | ||
1670 | 251 | |||
1671 | 252 | try: | ||
1672 | 253 | top=found.retrieve("Adv Payload")[0] | ||
1673 | 254 | except: | ||
1674 | 255 | return None | ||
1675 | 256 | #Rebuild that part of the structure | ||
1676 | 257 | found.payload.remove(top) | ||
1677 | 258 | #Now decode | ||
1678 | 259 | result={} | ||
1679 | 260 | data=top.val | ||
1680 | 261 | etype = aios.EnumByte("type",self.type.val,{ESType.uid.value:"Eddystone-UID", | ||
1681 | 262 | ESType.url.value:"Eddystone-URL", | ||
1682 | 263 | ESType.tlm.value:"Eddystone-TLM", | ||
1683 | 264 | ESType.eid.value:"Eddystone-EID"}) | ||
1684 | 265 | data=etype.decode(data) | ||
1685 | 266 | found.payload.append(etype) | ||
1686 | 267 | if etype.val== ESType.uid.value: | ||
1687 | 268 | power=aios.IntByte("tx_power") | ||
1688 | 269 | data=power.decode(data) | ||
1689 | 270 | found.payload.append(power) | ||
1690 | 271 | result["tx_power"]=power.val | ||
1691 | 272 | |||
1692 | 273 | nspace=aios.Itself("namespace") | ||
1693 | 274 | xx=nspace.decode(data[:10]) #According to https://github.com/google/eddystone/tree/master/eddystone-uid | ||
1694 | 275 | data=data[10:] | ||
1695 | 276 | found.payload.append(nspace) | ||
1696 | 277 | result["name space"]=nspace.val | ||
1697 | 278 | |||
1698 | 279 | nspace=aios.Itself("instance") | ||
1699 | 280 | xx=nspace.decode(data[:6]) #According to https://github.com/google/eddystone/tree/master/eddystone-uid | ||
1700 | 281 | data=data[6:] | ||
1701 | 282 | found.payload.append(nspace) | ||
1702 | 283 | result["instance"]=nspace.val | ||
1703 | 284 | |||
1704 | 285 | elif etype.val== ESType.url.value: | ||
1705 | 286 | power=aios.IntByte("tx_power") | ||
1706 | 287 | data=power.decode(data) | ||
1707 | 288 | found.payload.append(power) | ||
1708 | 289 | result["tx_power"]=power.val | ||
1709 | 290 | |||
1710 | 291 | url=aios.EnumByte("type",0,{0x00:"http://www.",0x01:"https://www.",0x02:"http://",0x03:"https://"}) | ||
1711 | 292 | data=url.decode(data) | ||
1712 | 293 | result["url"]=url.strval | ||
1713 | 294 | for x in data: | ||
1714 | 295 | if bytes([x]) == b"\x00": | ||
1715 | 296 | result["url"]+=".com/" | ||
1716 | 297 | elif bytes([x]) == b"\x01": | ||
1717 | 298 | result["url"]+=".org/" | ||
1718 | 299 | elif bytes([x]) == b"\x02": | ||
1719 | 300 | result["url"]+=".edu/" | ||
1720 | 301 | elif bytes([x]) == b"\x03": | ||
1721 | 302 | result["url"]+=".net/" | ||
1722 | 303 | elif bytes([x]) == b"\x04": | ||
1723 | 304 | result["url"]+=".info/" | ||
1724 | 305 | elif bytes([x]) == b"\x05": | ||
1725 | 306 | result["url"]+=".biz/" | ||
1726 | 307 | elif bytes([x]) == b"\x06": | ||
1727 | 308 | result["url"]+=".gov/" | ||
1728 | 309 | elif bytes([x]) == b"\x07": | ||
1729 | 310 | result["url"]+=".com" | ||
1730 | 311 | elif bytes([x]) == b"\x08": | ||
1731 | 312 | result["url"]+=".org" | ||
1732 | 313 | elif bytes([x]) == b"\x09": | ||
1733 | 314 | result["url"]+=".edu" | ||
1734 | 315 | elif bytes([x]) == b"\x10": | ||
1735 | 316 | result["url"]+=".net" | ||
1736 | 317 | elif bytes([x]) == b"\x11": | ||
1737 | 318 | result["url"]+=".info" | ||
1738 | 319 | elif bytes([x]) == b"\x12": | ||
1739 | 320 | result["url"]+=".biz" | ||
1740 | 321 | elif bytes([x]) == b"\x13": | ||
1741 | 322 | result["url"]+=".gov" | ||
1742 | 323 | else: | ||
1743 | 324 | result["url"]+=chr(x) #x.decode("ascii") #Yep ASCII only | ||
1744 | 325 | url=aios.String("url") | ||
1745 | 326 | url.decode(result["url"]) | ||
1746 | 327 | found.payload.append(url) | ||
1747 | 328 | elif etype.val== ESType.tlm.value: | ||
1748 | 329 | myinfo=aios.IntByte("version") | ||
1749 | 330 | data=myinfo.decode(data) | ||
1750 | 331 | found.payload.append(myinfo) | ||
1751 | 332 | myinfo=aios.ShortInt("battery") | ||
1752 | 333 | data=myinfo.decode(data) | ||
1753 | 334 | result["battery"]=myinfo.val | ||
1754 | 335 | found.payload.append(myinfo) | ||
1755 | 336 | myinfo=aios.Float88("temperature") | ||
1756 | 337 | data=myinfo.decode(data) | ||
1757 | 338 | found.payload.append(myinfo) | ||
1758 | 339 | result["temperature"]=myinfo.val | ||
1759 | 340 | myinfo=aios.LongInt("pdu count") | ||
1760 | 341 | data=myinfo.decode(data) | ||
1761 | 342 | found.payload.append(myinfo) | ||
1762 | 343 | result["pdu count"]=myinfo.val | ||
1763 | 344 | myinfo=aios.LongInt("uptime") | ||
1764 | 345 | data=myinfo.decode(data) | ||
1765 | 346 | found.payload.append(myinfo) | ||
1766 | 347 | result["uptime"]=myinfo.val*100 #in msecs | ||
1767 | 348 | return result | ||
1768 | 349 | #elif etype.val== ESType.tlm.eid: | ||
1769 | 350 | else: | ||
1770 | 351 | result["data"]=data | ||
1771 | 352 | xx=Itself("data") | ||
1772 | 353 | xx.decode(data) | ||
1773 | 354 | found.payload.append(xx) | ||
1774 | 355 | |||
1775 | 356 | rssi=packet.retrieve("rssi") | ||
1776 | 357 | if rssi: | ||
1777 | 358 | result["rssi"]=rssi[-1].val | ||
1778 | 359 | mac=packet.retrieve("peer") | ||
1779 | 360 | if mac: | ||
1780 | 361 | result["mac address"]=mac[-1].val | ||
1781 | 362 | return result | ||
1782 | diff --git a/setup.py b/setup.py | |||
1783 | index c2517fd..153e8e0 100755 | |||
1784 | --- a/setup.py | |||
1785 | +++ b/setup.py | |||
1786 | @@ -90,6 +90,8 @@ setup( | |||
1787 | 90 | "checkbox_support.scripts.nmea_test:main"), | 90 | "checkbox_support.scripts.nmea_test:main"), |
1788 | 91 | ("checkbox-support-snap_connect=" | 91 | ("checkbox-support-snap_connect=" |
1789 | 92 | "checkbox_support.scripts.snap_connect:main"), | 92 | "checkbox_support.scripts.snap_connect:main"), |
1790 | 93 | ("checkbox-support-eddystone_scanner=" | ||
1791 | 94 | "checkbox_support.scripts.eddystone_scanner:main"), | ||
1792 | 93 | ], | 95 | ], |
1793 | 94 | }, | 96 | }, |
1794 | 95 | ) | 97 | ) |
Tested last week on a large set of devices used to run SRU tests in cert lab and devices running UC.
Self-approved