Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from myoung34/from_bytes
Browse files Browse the repository at this point in the history
Use int.from_bytes instead of homegrown conversion
  • Loading branch information
myoung34 authored Mar 8, 2020
2 parents fd1df7c + 7d42a5a commit 66eb6b7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
5 changes: 0 additions & 5 deletions tests/test_blescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 5 additions & 21 deletions tilty/blescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,12 @@ 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'
# 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):
Expand Down Expand Up @@ -85,9 +69,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 {}

0 comments on commit 66eb6b7

Please sign in to comment.