Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / findfamily-tracker / tools / bench_provision.py
bench_provision.py89 lines · 3.0 KiBRaw
| 1 | """ |
| 2 | Bench-provision a FindFamily tracker from a PC and verify its beacon. |
| 3 | |
| 4 | Stands in for the phone so the firmware's crypto and mode switch can be checked |
| 5 | independently of the app: an epoch id that doesn't match what this script computes is a |
| 6 | firmware bug, whereas a matching id the phone can't see is a scanning problem. Those two |
| 7 | look identical from inside the app. |
| 8 | |
| 9 | Not part of the firmware build. Run with a BLE-capable PC: |
| 10 | python tools/bench_provision.py |
| 11 | """ |
| 12 | |
| 13 | import asyncio |
| 14 | import hashlib |
| 15 | import hmac |
| 16 | import struct |
| 17 | import sys |
| 18 | import time |
| 19 | |
| 20 | from bleak import BleakClient, BleakScanner |
| 21 | |
| 22 | UNPROVISIONED_SVC = "6b1d2f01-4b3a-4c7e-9a10-1f2e3d4c5b6a" |
| 23 | BEACON_SVC = "6b1d2f00-4b3a-4c7e-9a10-1f2e3d4c5b6a" |
| 24 | PROVISION_CHR = "6b1d2f02-4b3a-4c7e-9a10-1f2e3d4c5b6a" |
| 25 | |
| 26 | EPOCH_SECONDS = 900 |
| 27 | TRACKER_USER_ID = 0x0123456789ABCDEF |
| 28 | SECRET = bytes(range(32)) |
| 29 | |
| 30 | |
| 31 | def epoch_id(secret: bytes, epoch: int) -> bytes: |
| 32 | """Mirror of TrackerProtocol.epochId / ff_epoch_id.""" |
| 33 | return hmac.new(secret, b"fftrk1" + struct.pack(">Q", epoch), hashlib.sha256).digest()[:16] |
| 34 | |
| 35 | |
| 36 | async def main() -> int: |
| 37 | print(f"scanning for an unprovisioned tracker ({UNPROVISIONED_SVC})...") |
| 38 | device = await BleakScanner.find_device_by_filter( |
| 39 | lambda d, ad: UNPROVISIONED_SVC.lower() in [u.lower() for u in ad.service_uuids], |
| 40 | timeout=15.0, |
| 41 | ) |
| 42 | if device is None: |
| 43 | print("no tracker in pairing mode found (long-press the button and retry)") |
| 44 | return 1 |
| 45 | print(f"found {device.address}") |
| 46 | |
| 47 | now = int(time.time()) |
| 48 | blob = struct.pack(">Q", TRACKER_USER_ID) + SECRET + struct.pack(">Q", now) |
| 49 | assert len(blob) == 48, len(blob) |
| 50 | |
| 51 | async with BleakClient(device) as client: |
| 52 | print(f"writing {len(blob)}-byte provisioning blob (unix={now})") |
| 53 | await client.write_gatt_char(PROVISION_CHR, blob, response=True) |
| 54 | print("write acknowledged") |
| 55 | |
| 56 | expected = epoch_id(SECRET, now // EPOCH_SECONDS) |
| 57 | print(f"expected epochId = {expected.hex()}") |
| 58 | |
| 59 | print(f"scanning for the beacon ({BEACON_SVC})...") |
| 60 | seen: dict = {} |
| 61 | |
| 62 | def on_adv(dev, ad): |
| 63 | data = ad.service_data.get(BEACON_SVC.lower()) |
| 64 | if data: |
| 65 | seen[dev.address] = data |
| 66 | |
| 67 | scanner = BleakScanner(detection_callback=on_adv) |
| 68 | await scanner.start() |
| 69 | await asyncio.sleep(15.0) |
| 70 | await scanner.stop() |
| 71 | |
| 72 | if not seen: |
| 73 | print("no beacon heard. Check the firmware log first: it prints the live epoch id") |
| 74 | print("every second and marks whether it is '(on air)' or '(NOT advertising)'.") |
| 75 | print("If it says NOT advertising, the tracker never started the beacon and this") |
| 76 | print("is a firmware problem, not a reception one.") |
| 77 | return 1 |
| 78 | |
| 79 | ok = False |
| 80 | for addr, data in seen.items(): |
| 81 | got, battery = data[:16], data[16] if len(data) > 16 else -1 |
| 82 | match = "MATCH" if got == expected else "MISMATCH" |
| 83 | print(f"{addr}: epochId={got.hex()} battery={battery}% -> {match}") |
| 84 | ok = ok or got == expected |
| 85 | return 0 if ok else 1 |
| 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | sys.exit(asyncio.run(main())) |