From b2d3a8a640559e0318482f870e31ecaca9cf8883 Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Sat, 7 Mar 2020 14:36:53 -0600 Subject: [PATCH 1/2] Use int.from_bytes instead of homegrown conversion --- tilty/blescan.py | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/tilty/blescan.py b/tilty/blescan.py index 97cbfed..7957c2c 100644 --- a/tilty/blescan.py +++ b/tilty/blescan.py @@ -10,19 +10,6 @@ def get_socket(device_id): return bluez.hci_open_dev(device_id) -def number_packet(pkt): - # for(each byte) -> (start at 256 offset) -> byte as int * index -> add up - # b'\x89=' -> loop -> 137*256 -> + -> 61 -> 35133 - _int = 0 - multiple = 256 - for i in range(len(pkt)): - # 35072 - # 61 - _int += struct.unpack("B", pkt[i:i+1])[0] * multiple - multiple = 1 - return _int # 35133 - - def string_packet(pkt): # UUID is 16 Bytes # b'\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' @@ -85,9 +72,9 @@ def parse_packet(pkt): subevent, = struct.unpack("B", pkt[3:4]) # b'\x02' -> (2,) if subevent == 0x02: # if 0x02 (2) -> all iBeacons use this return { - 'mac': packed_bdaddr_to_string(pkt[3:9]), # mac -> 6 bytes -> b'\x02\x01\x03\x01w\t' # noqa - 'uuid': string_packet(pkt[-22:-6]), # uuid -> 16bytes -> b'\xa4\x95\xbb0\xc5\xb1KD\xb5\x12\x13p\xf0-t\xde' # noqa - 'major': number_packet(pkt[-6:-4]), # major -> 2 bytes -> b'\x00B' # noqa - 'minor': number_packet(pkt[-4:-2]), # minor -> 2 bytes -> b'\x03\xf7' # noqa + 'mac': packed_bdaddr_to_string(pkt[3:9]), # mac -> 6 bytes -> b'\x02\x01\x03\x01w\t' # noqa + 'uuid': string_packet(pkt[-22:-6]), # uuid -> 16bytes -> b'\xa4\x95\xbb0\xc5\xb1KD\xb5\x12\x13p\xf0-t\xde' # noqa + 'major': int.from_bytes(pkt[-6:-4], "big"), # major -> 2 bytes -> b'\x00B' # noqa + 'minor': int.from_bytes(pkt[-4:-2], "big"), # minor -> 2 bytes -> b'\x03\xf7' # noqa } return {} From 7d42a5a89d49bdf27be22e4d85d86ab4c7cff9f7 Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Sun, 8 Mar 2020 11:07:32 -0500 Subject: [PATCH 2/2] Simplify byte conversions --- tests/test_blescan.py | 5 ----- tilty/blescan.py | 5 +---- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/test_blescan.py b/tests/test_blescan.py index 1269b52..1c74537 100644 --- a/tests/test_blescan.py +++ b/tests/test_blescan.py @@ -2,11 +2,6 @@ from tilty import blescan -def test_number_packet(): - assert blescan.number_packet(b'\x89=') == 35133 - assert blescan.number_packet(b'\x98\xc7') == 39111 - - def test_string_packet(): assert blescan.string_packet(b'\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') == 'fe000000000000000000000000000000' # noqa assert blescan.string_packet(b'\x93\xe2\xfdD\x1b\xafhOH\xef>mn\x91\xcb\x14') == '93e2fd441baf684f48ef3e6d6e91cb14' # noqa diff --git a/tilty/blescan.py b/tilty/blescan.py index 7957c2c..2734f08 100644 --- a/tilty/blescan.py +++ b/tilty/blescan.py @@ -15,10 +15,7 @@ def string_packet(pkt): # b'\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' # so len() is 16 # loop over each byte, get it to hex, build up the string (uuid is 32 chars, 16bytes) # noqa - _str = "" - for i in range(len(pkt)): # 0-16 loop - _str += "%02x" % struct.unpack("B", pkt[i:i+1])[0] - return _str + return ''.join(["%02x" % int.from_bytes(pkt[i:i+1], "big") for i in range(len(pkt))]) # noqa def packed_bdaddr_to_string(bdaddr_packed):