From c1e923f1b13e77b84ba537fec49e09749bd6c34b Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Wed, 4 Mar 2020 16:41:06 -0600 Subject: [PATCH 1/2] Add docs to blescan as well as a few tests --- pyproject.toml | 1 - tests/test_blescan.py | 14 ++++++++++ tilty/blescan.py | 60 +++++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 tests/test_blescan.py diff --git a/pyproject.toml b/pyproject.toml index 55552ba..0619680 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,6 @@ commands = isort -c -rc tilty -sp {toxinidir} pylint --rcfile {toxinidir}/.pylintrc -r n tilty py.test --cov-config .coveragerc --cov tilty --cov-report term-missing --cov-report xml --junitxml junit.xml tests {posargs} - coveralls whitelist_externals = make bash pylint diff --git a/tests/test_blescan.py b/tests/test_blescan.py new file mode 100644 index 0000000..def1602 --- /dev/null +++ b/tests/test_blescan.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +from tilty import blescan + + +def test_number_packet(): + assert blescan.number_packet(b'\x89=') == 35133 + + +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 + + +def test_packed_bdaddr_to_string(): + assert blescan.packed_bdaddr_to_string(b'ID\x8b\xea&b') == '62:26:ea:8b:44:49' # noqa diff --git a/tilty/blescan.py b/tilty/blescan.py index 07132ef..cdab628 100644 --- a/tilty/blescan.py +++ b/tilty/blescan.py @@ -39,29 +39,31 @@ def getBLESocket(devID): return bluez.hci_open_dev(devID) -def returnnumberpacket(pkt): +def number_packet(pkt): + # b'\x89=' myInteger = 0 multiple = 256 for i in range(len(pkt)): + # 35072 + # 61 myInteger += struct.unpack("B",pkt[i:i+1])[0] * multiple multiple = 1 - return myInteger + return myInteger # 35133 -def returnstringpacket(pkt): +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) myString = ""; - for i in range(len(pkt)): + for i in range(len(pkt)): # 0-16 loop myString += "%02x" %struct.unpack("B",pkt[i:i+1])[0] return myString -def get_packed_bdaddr(bdaddr_string): - packable_addr = [] - addr = bdaddr_string.split(':') - addr.reverse() - for b in addr: - packable_addr.append(int(b, 16)) - return struct.pack(" b'b&\xea\x8bDI' + # decode to int -> (98, 38, 234, 139, 68, 73) , join by : as hex -> '62:26:ea:8b:44:49' return ':'.join('%02x'%i for i in struct.unpack("+\x02\x01\x03\x01r\xed\x08S\x84=\x1f\x1e\xff\x06\x00\x01\t \x02)\xa7\x93\xe2\xfdD\x1b\xafhOH\xef>mn\x91\xcb\x14\x02$\x98\xc7\xef\xb3' + # | | | | | | | | | | | + # | event|sub|#rep\ | | mac addr | | uuid | major\ minor| | + ptype, event, plen = struct.unpack("BBB", pkt[:3]) # b'\x04>(' -> (4, 62, 40) + if event == LE_META_EVENT: # 62 -> 0x3e -> HCI Event: LE Meta Event (0x3e) plen 39 + subevent, = struct.unpack("B", pkt[3:4]) # b'\x02' -> (2,) + # chop off \x04>+\x02 (the event + subevent) + pkt = pkt[4:] # b'\x01\x03\x01r\xed\x08S\x84=\x1f\x1e\xff\x06\x00\x01\t \x02)\xa7\x93\xe2\xfdD\x1b\xafhOH\xef>mn\x91\xcb\x14\x02$\x98\xc7\xef\xb3' + if subevent == EVT_LE_ADVERTISING_REPORT: # if 0x02 (2) -> all iBeacons use this + num_reports = struct.unpack("B", pkt[0:1])[0] # b'\x01' -> (1,) -> number of reports to receive for i in range(0, num_reports): # build the return string beacons.append({ - 'mac': packed_bdaddr_to_string(pkt[report_pkt_offset + 3:report_pkt_offset + 9]), - 'uuid': returnstringpacket(pkt[report_pkt_offset - 22: report_pkt_offset - 6]), - 'minor': returnnumberpacket(pkt[report_pkt_offset - 4: report_pkt_offset - 2]), - 'major': returnnumberpacket(pkt[report_pkt_offset - 6: report_pkt_offset - 4]), + 'mac': packed_bdaddr_to_string(pkt[3:9]), # b'r\xed\x08S\x84=' + 'uuid': string_packet(pkt[22:6]), # b'\x93\xe2\xfdD\x1b\xafhOH\xef>mn\x91\xcb\x14' + 'minor': number_packet(pkt[4:2]), # b'\x98\xc7' + 'major': number_packet(pkt[6:4]), # b'\x02$' }) sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter ) return beacons From 3816107ed98df66268d5d2720585efbbd51728c6 Mon Sep 17 00:00:00 2001 From: Marcus Young Date: Wed, 4 Mar 2020 16:45:45 -0600 Subject: [PATCH 2/2] Remove coveralls from deploy/release actions --- .github/workflows/deploy.yml | 4 ---- .github/workflows/release.yml | 4 ---- 2 files changed, 8 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e14d2a0..7d6748f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -28,10 +28,6 @@ jobs: run: echo "::set-env name=PYTHONPATH::$(pwd)" - name: pytest run: py.test --cov-config .coveragerc --cov tilty --cov-report term-missing --cov-report xml --junitxml junit.xml tests - - name: coveralls - run: coveralls - env: - COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} latest_deploy: runs-on: ubuntu-latest needs: [test] diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 521ffd0..c729915 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,10 +27,6 @@ jobs: run: echo "::set-env name=PYTHONPATH::$(pwd)" - name: pytest run: py.test --cov-config .coveragerc --cov tilty --cov-report term-missing --cov-report xml --junitxml junit.xml tests - - name: coveralls - run: coveralls - env: - COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} upload_to_pypi: runs-on: ubuntu-latest needs: [test]