Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / FINDFAMILY_UWB_TRACKER_DESIGN.md

FINDFAMILY_UWB_TRACKER_DESIGN.md14.8 KiBRenderedCodeRaw
1# FindFamily Custom UWB Tracker — Design
2
3Design for adding **custom, DIY UWB item-trackers** to FindFamily: binding a
4tracker to its owner, and a global crowd-finding network so any phone running
5FindFamily can anonymously report a tracker's location back to its owner —
6plus phone-native UWB precision finding for the last few meters.
7
8Hardware target: **nRF52 MCU + Qorvo DW3000** (FiRa-capable UWB). The reference
9build is a **Qorvo DWM3001CDK** (nRF52833 + DW3110); firmware lives in
10`firmware/findfamily-tracker/`.
11
12---
13
14## Implementation status (v1 scaffold, DEV_BUILD-gated)
15
16A first full-stack scaffold is implemented behind `BuildConfig.DEV_BUILD`
17(true for `assembleDev`/`assembleDebug`, false for `assembleRelease`). Chosen
18crypto: **rotating-hash + server-served ML-KEM key** (max reuse; server can
19link a rotating id → tracker, accepted tradeoff).
20
21Client (`findfamily/`):
22- `build.gradle.kts` — `DEV_BUILD` BuildConfig flag; `src/dev/AndroidManifest.xml` — BLE permissions (dev only).
23- `data/User.kt` (`UserKind`, `kind`) + `data/FFDatabase.kt` (migration 9→10).
24- `tracker/` — `TrackerFeature` (gate), `TrackerProtocol` (epoch-id HMAC + report seal/open, unit-tested), `TrackerStore` (owner secret/private-key store), `TrackerBle` (BLE contract), `TrackerBeaconScanner` (finder scan), `TrackerProvisioner` (GATT binding), `TrackerBinder` (bind flow), `TrackerReporting` (finder upload + owner fetch), `TrackerUwbGatt` (FiRa-over-GATT seam).
25- `util/Networking.kt` — opcodes 0x06–0x0B + `registerTracker`/`resolveTrackerBundle`/`uploadTrackerReport`/`fetchTrackerReports`.
26- `util/LocationTrackingService.kt` — finder scan + owner report polling (gated).
27- UI — dev-only "Add tracker" FAB (`ui/MainPage.kt`) + `ui/dialogs/AddTrackerDialog.kt` + `Route.AddTrackerDialog`.
28- Test — `src/test/.../tracker/TrackerProtocolTest.kt`.
29
30Server (`~/Documents/code/location_share_server`):
31- `src/handlers/findfamily.rs` — same opcodes 0x06–0x0B (TRACKER_REGISTER / RESOLVE / REPORT_PUT / REPORT_GET) with HMAC epoch-id, lazy TTL purge, unit tests.
32- `src/state.rs`, `src/main.rs` — `ff_trackers` + `ff_reports` maps.
33
34Remaining seams: DULT anti-stalking detector; and UWB ranging end to end — the
35phone side is written (`TrackerUwbGatt.startRanging`) and the firmware side
36(`src/tracker_uwb.c`) is written but **staged behind `CONFIG_FF_TRACKER_UWB`
37(default `n`) and never compiled**, because the licensed Qorvo DW3xxx driver is
38not vendored. BLE crowd-finding firmware is implemented and flashable.
39
40---
41
42## 1. Locked decisions
43
44| Area | Decision | Rationale |
45|---|---|---|
46| Tracker identity | Model as `User(kind = TRACKER)` | Reuses map pins, Room DB, the E2E relay, and `processIncomingLocations` with near-zero UI churn — the whole app already keys on `userid`. |
47| Crowd-finding crypto | **Rotating-hash + server-served ML-KEM key** | The originally locked "Option B" (rotating per-epoch EC keys, §5 below) was **not** what shipped. Option B hides the tracker from the server structurally; what shipped lets the server link a rotating id → tracker, which is a real weakening, accepted in exchange for reusing the existing PQC identity plumbing wholesale instead of adding an X25519 path to the app, the server and the firmware. Revisit if the threat model tightens. |
48| UWB | Phone-native **FiRa** ranging; **Android-only** OOB for v1 | The phone's UWB radio speaks FiRa; Android interop only needs matched session params exchanged out-of-band (no Apple MFi/NI needed yet). |
49| UWB hardware | **Qorvo DW3110** (DWM3000 module, built on DW3110) + own MCU + **Qorvo FiRa stack** | DW3110 is 802.15.4z with STS, channels 5 & 9 → real FiRa sessions that range with phones. RYUW122 as a *sealed module* runs proprietary AT-command ranging and can't interop with phones. |
50| Provisioning | **BLE GATT** | Same radio as the crowd-finding beacon; wireless binding. |
51
52---
53
54## 2. Why not the RYUW122 as-is, and why UWB (not BLE) for finding
55
56- The RYUW122 is built on a **Qorvo DW3000** (FiRa-capable silicon), but it is a
57 sealed module whose onboard MCU runs **Reyax's proprietary AT-command**
58 ranging over UART. That firmware is not FiRa and (almost certainly) not
59 reflashable — so a phone cannot range with it. The fix is to keep the DW3000
60 chip but use a board whose MCU **we** flash with a FiRa stack.
61- **UWB is not a discovery radio** — it has no advertising/beaconing mode. The
62 global "any phone notices it" network therefore rides **BLE advertising**
63 (discovery + crowd reports), while **UWB/FiRa** is used only for the owner's
64 precision finding once nearby. This mirrors how commercial UWB trackers work.
65
66### Chip choice (locked): Qorvo DW3110
67
68- **DW3110** — 802.15.4z + **STS**, channels **5 & 9** → interoperates with
69 phone-native UWB. This is the silicon inside the **DWM3000** module.
70- **Single RF port → no on-chip PDoA/AoA.** This is fine: the **phone** measures
71 angle with its own multi-antenna array (`UwbController.stream()` reads
72 azimuth/elevation from the phone's `RangingData`); the tracker only has to be a
73 FiRa **responder**. Distance + arrow still work. The dual-port **DW3210** would
74 only be needed if the *tracker* had to sense direction (not required here).
75- **Module vs. bare chip**: start with the **DWM3000 module** (RF/antenna/
76 shielding done + pre-tested) to skip RF headaches; drop to the bare **DW3110**
77 on a custom PCB later if needed.
78- **Rejected: DW1000** — first-gen 802.15.4-2011, **no STS**, no channel 9;
79 cannot range with phones (same trap as the sealed RYUW122).
80
81### Android FiRa OOB — the key simplification
82To interop with an **iPhone**, an accessory must implement Apple's Nearby
83Interaction Accessory Protocol (the `UwbAccessoryProtocol.kt` stub) and, for a
84product, MFi. To interop with **Android**, the phone (FiRa **controller /
85initiator**) picks the session params and hands them to the tracker (FiRa
86**controlee / responder**) over any side channel. FindFamily already generates
87exactly these params in `UwbController.openController()` and already exchanges
88them for phone↔phone over its WebSocket. For the tracker we deliver them over
89**BLE GATT** instead. This is meaningfully less work than a full MFi accessory.
90
91Two of those params are deliberately **not** handed over: the **STS key** and the
92tracker's **UWB address**. The GATT link to a tracker is unbonded and
93unencrypted, so a stranger standing nearby could write their own key and range
94against someone else's tracker. Both sides instead derive them from the bind-time
95beacon secret (`TrackerUwbKeys.kt` on the phone, `src/tracker_sts.c` in the
96firmware), and the per-find write carries only address, session id, channel and
97preamble — 8 bytes, useless without the secret.
98
99---
100
101## 3. Entity model
102
103Add a discriminator to `findfamily/src/main/java/com/vayunmathur/findfamily/data/User.kt`:
104
105```
106enum class UserKind { PERSON, TRACKER }
107// User gains: val kind: UserKind = UserKind.PERSON
108```
109
110A tracker `User`:
111- has its own random 64-bit `userid` and PQC bundle;
112- is **owner-only** custodian of the tracker's **private** PQC bundle + **beacon
113 master secret** (stored in DataStore, never synced);
114- is non-interactive: no sharing toggles, no waypoint auto-toggle, no heartbeat
115 publish. It only ever appears as a received location (a map pin).
116
117A tracker report is just a `LocationValue` with `userid = tracker_userid`, so it
118renders through the existing `processIncomingLocations` path unchanged.
119
120---
121
122## 4. Binding (goal a) — BLE GATT provisioning
123
1241. Unprovisioned tracker advertises a BLE "new tracker" GATT service.
1252. Phone scans → shows "New tracker found" → user taps **Bind**.
1263. Phone mints locally:
127 - random `userid`,
128 - PQC keypair — reuse `Networking.generatePqcKeyPair()`,
129 - 32-byte `beacon_secret`,
130 - a long-term **UWB STS/session key** derived from `beacon_secret`.
1314. Phone writes over a GATT **provisioning characteristic** a
132 `[8B userid BE][32B beacon_secret][8B unix_seconds BE]` blob. The trailing
133 timestamp is the tracker's only source of wall-clock time — it has no
134 battery-backed RTC, and epoch ids are a function of `unix_seconds / 900`, so
135 without it the tracker beacons ids outside the owner's search window and
136 resolves to nothing. No UWB params are sent: channel and preamble come with each
137 find, and the STS key and UWB address are derived (§2).
1385. Phone persists `User(kind = TRACKER)` + private bundle + secret + the tracker's
139 BLE address (needed to re-open GATT for a UWB find).
1406. **Ownership = possession of the private key + secret.** Unbind requires the
141 secret (anti-theft lock), so a found tracker can't be silently re-bound.
142
143The tracker never runs PQC — it only derives rotating beacon keys (cheap). All
144heavy crypto stays on phones.
145
146---
147
148## 5. Crowd finding (goal b) — as shipped
149
150> The per-epoch EC scheme described in this section is the **original Option B
151> design and is not what shipped**. What shipped is the rotating-hash scheme in
152> `TrackerProtocol.kt`: `epochId = HMAC-SHA256(beacon_secret, "fftrk1" ||
153> u64_be(epoch))[..16]`, with the location sealed to the tracker's ML-KEM public
154> bundle which the server serves to finders. This section is kept because the
155> privacy properties it describes are the ones to aim for if the scheme is
156> revisited (see §1).
157
158### 5.1 Beacon (tracker)
159Every epoch (~15 min):
160```
161priv_epoch = HKDF(beacon_secret, epoch)
162pub_epoch = priv_epoch · G // X25519 (recommended curve)
163```
164BLE-advertise `pub_epoch` (+ a battery byte). No static identifier is ever
165broadcast, so the tracker can't be followed by ID. Cost on-device: one HKDF +
166one EC scalar-mult per epoch.
167
168### 5.2 Finder (any FindFamily phone)
169- Add a **BLE scan callback** in `LocationTrackingService`, right beside
170 `handleUwbEnvelopes()` (the foreground service is already the natural host).
171- On hearing a tracker beacon:
172 - build a `LocationValue` at the finder's **own** current GPS,
173 - `ciphertext = ECIES(pub_epoch, LocationValue)`,
174 - upload `{ SHA256(pub_epoch), ciphertext }` to the new report-upload endpoint.
175- Rate-limit / dedup per `(pub_epoch, coarse-time)`; require decent GPS accuracy.
176- The finder never learns whose tracker it is.
177
178### 5.3 Server (new work — the critical piece outside this repo)
179Two endpoints on `api.maproj.org`:
180- `report-upload { hash, ciphertext }`
181- `report-fetch-by-hash [hashes] -> [ciphertext]`
182with a retention window (e.g. 7 days) + abuse/rate-limit policy. The server
183sees only opaque hashes and ciphertext — it cannot link reports to a tracker or
184an owner.
185
186### 5.4 Owner retrieval
187- Recompute the expected `SHA256(pub_epoch)` for the last N epochs.
188- Fetch matching ciphertexts, decrypt with the per-epoch `priv_epoch`.
189- Emit a `LocationValue` with `userid = tracker_userid` → flows through the
190 existing `processIncomingLocations` → renders as the tracker's map pin. **No
191 map/UI changes required.**
192
193---
194
195### 5.5 On-air format (as shipped)
196The advertisement carries `[16B epochId][1B battery%]` as **service data** under the
197128-bit beacon service UUID. That does **not** fit a legacy advertisement:
198`Service Data — 128-bit UUID` costs `1B length + 1B type + 16B UUID = 18B`, so with
199the 17-byte payload it is **35 bytes against the 31-byte legacy limit**. The full
20016-byte UUID has to be on air because the phone reads it back via
201`ScanRecord.getServiceData(ParcelUuid)`, so no legacy encoding works. The beacon is
202therefore a **BLE 5 extended advertisement** (secondary PHY 1M, connectable so the
203owner can still write UWB session params), and finder phones must scan with
204`ScanSettings.Builder().setLegacy(false)`.
205
206Pairing mode stays **legacy** — flags + a 128-bit service UUID is only 21 bytes — so
207discovery of an unprovisioned tracker works on default scan settings.
208
209---
210
211## 6. UWB precision finding (owner, FiRa OOB)
212
213Owner-only, once near the tracker:
214- Reuse `UwbController.openController()` to mint FiRa params (phone = controller
215 / initiator).
216- Deliver params to the tracker over a **BLE GATT session characteristic**
217 (instead of the WS `UwbEnvelope`). Because the STS key was derived at bind
218 time, the per-find handoff carries only channel/slot.
219- Both start the FiRa session; `UwbController.stream()` yields distance + AoA
220 arrow **unchanged**.
221- The `UwbAccessoryProtocol` stubs stay parked for a future iOS phase.
222
223This reuses the entire ranging path; only the *handshake transport* changes
224(BLE GATT vs. WebSocket envelope).
225
226---
227
228## 7. Anti-stalking (DULT)
229
230Independent of the above and important to ship: a finder-side detector that
231flags a **foreign beacon persisting across time and space** ("unknown tracker
232traveling with you"), aligned with the cross-industry DULT specification.
233
234---
235
236## 8. Firmware responsibilities (DW3110 + own MCU)
237
238Implemented in `firmware/findfamily-tracker/` (Zephyr / nRF Connect SDK):
239
240- BLE GATT server: provisioning characteristic + per-find UWB-session
241 characteristic (`src/tracker_ble.c`).
242- BLE beacon: rotating epoch id per epoch, HMAC-SHA256 via PSA Crypto so it is
243 byte-identical to the Kotlin and Rust sides (`src/tracker_epoch.c`).
244- NVS persistence of the provisioning blob and time base (`src/tracker_store.c`).
245- Qorvo FiRa **responder / controlee** session on the DW3000 — written but staged
246 behind `CONFIG_FF_TRACKER_UWB` (default `n`) (`src/tracker_uwb.c`).
247- **Critical-path risk**: a reliable FiRa responder with solid phone interop
248 (STS / timing) is a multi-week embedded effort, and Qorvo stack access
249 typically needs registration/NDA. The Android app-side work is comparatively
250 small.
251
252---
253
254## 9. Phasing
255
2561. **Entity + binding** — `User.kind`, BLE GATT provisioning, key/secret
257 generation & storage.
2582. **Crowd network** — beacon format, finder BLE scan + upload, server
259 endpoints, owner fetch/decrypt → map.
2603. **UWB precision find** — BLE GATT param handoff + firmware FiRa responder +
261 wiring into `UwbSessionManager`.
2624. **Anti-stalking** detector.
263
264---
265
266## 10. Open items to confirm
267
268- ECIES curve — **X25519** is the clean pick.
269- Epoch length vs. beacon-privacy / retrieval-cost tradeoff.
270- Server retention window + abuse / rate-limit policy.
271- Ownership of the server-side change (the one piece outside this repo).
272- Verify (datasheet / Reyax) there is truly no FiRa/NI mode or reflash path on
273 the RYUW122 before fully committing to new hardware.
274
275---
276
277## 11. Key existing code this builds on
278
279- `findfamily/.../data/User.kt` — entity to extend with `kind`.
280- `findfamily/.../data/LocationValue.kt` — report payload (already `userid`-keyed).
281- `findfamily/.../util/Networking.kt` — PQC identity, relay, `generatePqcKeyPair()`.
282- `findfamily/.../util/LocationTrackingService.kt` — foreground host for the BLE
283 scan + `processIncomingLocations` (renders tracker pins for free).
284- `findfamily/.../uwb/UwbController.kt` — FiRa param generation + `stream()`.
285- `findfamily/.../util/UwbSessionManager.kt` — session state machine to wire the
286 BLE GATT handshake into.
287- `findfamily/.../uwb/UwbAccessoryProtocol.kt` — parked for future iOS interop.