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

Modern-Apps.git / firmware / findfamily-tracker / docs / DISCREPANCIES.md

DISCREPANCIES.md39.6 KiBRenderedCodeRaw
1# FindFamily UWB tracker — spec vs. as-built discrepancies
2
3Diff of `docs/INTENDED_BEHAVIOUR.md` (written from documentation only) against
4`docs/AS_BUILT.md` (written from source only), with **every claim carried into this report
5re-verified against the actual source, the Qorvo SDK at `C:\qorvo`, and the NCS tree at
6`C:\ncs\v3.4.0`** before being asserted. Where an input document is wrong, it is corrected
7here explicitly (§6).
8
9Immediate goal: FiRa ranging between an Android controller/initiator and this DW3110
10controlee/responder, **without regressing BLE crowd-finding, which currently works.**
11Ordered blockers first.
12
13## Line numbers used here
14
15`src/tracker_uwb.c` line numbers in this document are **working-tree** (531 lines, i.e.
16including the uncommitted `k_sleep` hunk assessed in §5). `AS_BUILT.md` cites HEAD
17(`0cdc09026`, 519 lines); for HEAD, subtract 11 from any line ≥ 172. All other files are
18identical at HEAD and in the working tree.
19
20## Verification status of each claim class
21
22| Class | How verified |
23|---|---|
24| Firmware behaviour | Read directly: `src/*.c`, `src/*.h`, `prj.conf`, `uwb.conf`, `Kconfig`, `firmware/qorvo-uwb/src/*.c` |
25| Phone behaviour | Read directly: `TrackerUwbGatt.kt`, `TrackerUwbKeys.kt`, `UwbController.kt` |
26| Vendor API contracts | Read directly in `C:\qorvo` (`uwbmac.h`, `fira_helper.h`, `fira_region_params.h`, `qthread.h`, `fira_niq.c`, `fira_default_params.h`) |
27| Zephyr/NCS defaults | Read directly in `C:\ncs\v3.4.0` (`kernel/Kconfig`, `include/zephyr/kernel.h`, `subsys/logging/*`) |
28| Android UCI config | **Given as observed fact** in the task; not re-derived |
29| `qplatform_uwb_interrupt_enable()` caller | **Not verifiable** — closed-source `llhw`. Kept as an open item (M5) |
30
31---
32
33# BLOCKERS
34
35## B1. The FiRa session start is unreachable behind a higher-priority poll thread — and there is no evidence it ever ran
36
37**Spec requires** (INTENDED §6.5, verified against `fira_app.c` / `fira_niq.c`):
38`uwbmac_start()` **then** `fira_helper_start_session()`, and the session must actually reach
39ACTIVE before the controller polls (§6.7 / OQ-19).
40
41**Code does:** `ff_uwb_start()` runs on the session thread at **priority 6**
42(`tracker_uwb.c:93`). At `tracker_uwb.c:451` — after `uwbmac_start()` (`:444`) and **before**
43`fira_helper_start_session()` (`:454`) — it calls `start_polling()`, which
44`k_thread_create(..., UWB_THREAD_PRIORITY, 0, K_NO_WAIT)` a poll thread at **priority 5**
45(`:49`, `:188-190`). Priority 5 is numerically lower, i.e. higher priority, than its own
46creator, and both are preemptible, so **the new thread preempts the session thread at the
47moment of creation**. `fira_helper_start_session()` is only reached if the poll loop yields.
48
49Zephyr time-slicing does not save this: `CONFIG_TIMESLICING` is default `y` with
50`CONFIG_TIMESLICE_SIZE` 20 ms (`C:\ncs\v3.4.0\zephyr\kernel\Kconfig:679-692`), but slicing
51only rotates threads of **equal** priority.
52
53**Runtime consequence:** the responder session is never started, so the DW3110 is not
54listening in the phone's rounds. Android, as controller, transmits and gets nothing back:
55**UCI RangingStatus 0x21 (RX timeout), Distance 65535, on every round for 10 s, then drops
56the controlee** — precisely the observed symptom. Crowd-finding is unaffected because the
57beacon refresh runs on `main` at priority 0 (`main.c:215-272`), which also keeps feeding the
58watchdog (`main.c:216`) — hence a silent board with **no reset**, also as observed.
59
60This is ranked first not because it is the most interesting defect but because it is the only
61one **proven by the observed run**: none of the seven `printk` markers in `ff_uwb_start`
62(`:370, 377, 384, 436, 442, 453, 460`) ever appeared. Had `ff_uwb_start` completed, the
63session thread would have returned to `k_sem_take(K_FOREVER)` (`:500`) and released the CPU,
64at which point the log thread would have flushed the whole trace. It did not. Therefore
65`ff_uwb_start` did not complete, therefore no session was started, therefore every parameter,
66STS and endianness theory below is currently **unfalsifiable**.
67
68Two mechanisms are consistent with that, and B1's fix covers both:
691. the poll loop does not yield (the premise of the uncommitted delta, §5); or
702. the session thread is stuck inside a vendor call between `fira_helper_open` (`:371`) and
71 `fira_helper_start_session` (`:454`) — the exact hazard `tracker_uwb.c:84-90` was written
72 to contain.
73
74A third-party fault is ruled out: an MPU/bus fault would have halted or rebooted the board
75(`k_fatal_halt` spins with interrupts off; `CONFIG_RESET_ON_FATAL_ERROR` defaults `y`), and
76neither happened. It would also have *printed*: the fatal path calls `LOG_PANIC()` before
77halting (`C:\ncs\v3.4.0\zephyr\kernel\fatal.c:42-44`), which flushes the deferred buffer
78synchronously, so a fault would have dumped the whole missing trace plus "Halting system"
79rather than producing silence. The silence is therefore positive evidence of starvation rather
80than of a crash.
81
82**Fix:**
83- `src/tracker_uwb.c:49` — `#define UWB_THREAD_PRIORITY 5` → **`7`**. This puts the pump
84 below the session thread (6) *and* below every vendor MAC thread: `enum qthread_priority`
85 runs `CRITICAL = 0 … IDLE = 6` (`C:\qorvo\Libs\uwb-stack\libs\qosal\include\qthread.h:42-51`)
86 and `qthread_zephyr.c:96` maps it order-preservingly onto `K_PRIO_PREEMPT`, so MAC threads
87 occupy Zephyr 0..6 and the poll thread currently sits **inside** that band.
88- `src/tracker_uwb.c:160` — stop discarding the result; log it once
89 (`(void)` → capture and `LOG_INF` on first call, or on change). If it returns an error
90 immediately, the pump is unavailable in this delivery (see §6.2) and the thread should be
91 deleted rather than tuned.
92- Keep the `k_sleep(K_MSEC(1))` at `:171` (§5) — but as defence, not as the mechanism that
93 makes the session start work.
94
95## B2. Every diagnostic is destroyed at exactly the moment it is needed
96
97**Spec requires** (INTENDED §12 step 2, and its whole investigation order): decode the
98tracker's own `SESSION_STATUS_NTF` reason codes *before* guessing at parameters, because they
99distinguish "never heard anything" from "heard it, STS wrong" from "parameter rejected".
100
101**Code does:** `CONFIG_LOG_MODE_DEFERRED=y` (`prj.conf:74`) with no priority override, so the
102log processing thread runs at `K_LOWEST_APPLICATION_THREAD_PRIO`
103(`C:\ncs\v3.4.0\zephyr\subsys\logging\log_core.c:60-64,1013`) =
104`K_LOWEST_THREAD_PRIO − 1` = `CONFIG_NUM_PREEMPT_PRIORITIES − 1` = **14**
105(`kernel.h:58-61`, `kernel/Kconfig:52-55`). The two UWB threads are at 5 and 6, `main` at 0
106(`kernel/Kconfig:70-73`). Worse, the `printk()` calls in `ff_uwb_start` are **not** an escape
107hatch: `CONFIG_LOG_PRINTK` is `default y if PRINTK`
108(`C:\ncs\v3.4.0\zephyr\subsys\logging\Kconfig.processing:8-13`), so printk is routed into the
109same deferred pipeline and is lost with everything else.
110
111**Runtime consequence:** any CPU-bound or long-running condition in a thread at priority
1121..13 makes the board go permanently silent while the watchdog keeps being fed — an
113observation that looks like a hang, is not one, and erases the record of what happened. This
114is the reason the whole bring-up is currently blind, and it is why B1 could only be inferred
115rather than read off a log.
116
117**Severity:** BLOCKER. It does not itself stop ranging, but it blocks the diagnosis of
118everything that does, and every experiment below is worthless until it is fixed. It is also
119the cheapest fix in this document.
120
121**Fix:** `prj.conf` — add `CONFIG_LOG_MODE_IMMEDIATE=y` for bring-up (replacing
122`CONFIG_LOG_MODE_DEFERRED=y` at `prj.conf:74`), or keep deferred and add
123`CONFIG_LOG_PROCESS_THREAD_CUSTOM_PRIORITY=y` + `CONFIG_LOG_PROCESS_THREAD_PRIORITY=3`.
124Immediate mode is preferable while chasing B1/B3/B4 because it removes the log thread — and
125therefore the whole starvation mechanism — from the picture, rather than merely re-ranking it.
126Then extend `on_fira_notification` (`tracker_uwb.c:139-145`) to print the reason-code name, not
127just the integer.
128
129> **Corrected after implementation review (`fixer`).** An earlier revision justified immediate
130> mode on the grounds that it "also survives a subsequent hard fault". That was wrong: deferred
131> mode survives one too, because the fatal path calls `LOG_PANIC()` before halting
132> (`C:\ncs\v3.4.0\zephyr\kernel\fatal.c:42-44`), which switches the backends to synchronous
133> panic mode and flushes what is buffered. The distinction that actually matters here is the
134> opposite one: `LOG_PANIC()` only fires on a fatal error, and the observed failure had none —
135> the board neither halted nor reset — so nothing ever triggered a flush and the buffered trace
136> was simply never emitted. Starvation, not faulting, is what loses the evidence, and only
137> immediate mode is immune to it.
138
139## B3. vUpper64 is laid into the vendor struct in the wrong order — guaranteed STS mismatch
140
141**Spec requires** (INTENDED §6.8 / TRAP-11): treat the derived 8 bytes as
142`[2B VendorID][6B STATIC_STS_IV]` and lay them into the Qorvo struct the way the vendor's own
143BLE-provisioned accessory does.
144
145**Code does:** `memcpy(sp.vupper64, vupper64, sizeof(vupper64))` (`tracker_uwb.c:403`) with
146the 8 bytes exactly as HKDF produced them, documented at `:348-352` as
147`[2B VendorID][6B STATIC_STS_IV]` (matching `TrackerUwbKeys.kt:27-33` and
148`setSessionKeyInfo`).
149
150**Verified against the SDK:** the field is a union whose struct arm is
151`uint8_t static_sts_iv[6]` **first**, then `uint8_t vendor_id[2]`
152(`fira_helper.h:382-397`, with `FIRA_VENDOR_ID_SIZE 2` / `FIRA_STATIC_STS_IV_SIZE 6` /
153`FIRA_VUPPER64_SIZE 8` at `fira_region_params.h:18-20`). Qorvo's own NI accessory fills it as
154a **full byte reversal** of `VendorID‖StaticStsIv`
155(`C:\qorvo\Projects\FreeRTOS\QANI\Common\src\fira\fira_niq.c:228-235`:
156`vupper64[7]=Vendor_ID[0] … vupper64[0]=Static_STS_IV[5]`). The firmware's straight `memcpy`
157therefore puts the VendorID bytes into `static_sts_iv[0..1]` and `STATIC_STS_IV[4..5]` into
158`vendor_id[0..1]`.
159
160**Runtime consequence:** the tracker's static-STS key material differs from the phone's, so
161SP3 frames do not correlate in either direction. The phone reports RX timeout on every round;
162the tracker logs nothing, because a wrong STS is not an error, exactly as
163`tracker_sts.c:10-12` warns. Indistinguishable from B1 from the phone's side — which is why
164B1 must be fixed first.
165
166**Fix:** `src/tracker_uwb.c:403` — replace the `memcpy` with a reversal:
167`for (int i = 0; i < FIRA_VUPPER64_SIZE; i++) sp.vupper64[i] = vupper64[7 - i];`
168and correct the comment at `:348-352`, which currently asserts the layout the struct does not
169have. Confidence is high (vendor reference + verified struct layout) but not absolute — the
170Android side of the mapping is not citable from anything on this machine — so if ranging still
171fails after B1+B3, the straight `memcpy` is the one alternative worth re-testing, and it is a
172two-build experiment.
173
174## B4. The short-address byte order is unverified, and the hardware address filter is on
175
176**Spec requires** (INTENDED §6.6 / OQ-22, §6.7 / TRAP-10): the tracker's session
177`short_addr` must equal what the phone uses as `peerAddress`, `destination_short_address[0]`
178must equal the phone's own address, and `uwbmac_set_short_addr()` must be called or the
179DW3110's hardware filter silently discards the polls.
180
181**Code does:** `uwbmac_set_short_addr()` **is** called (`tracker_uwb.c:364`) — TRAP-10's
182primary form is not present, and `AS_BUILT` §7.4 is right that both sides agree on the byte
183*sequence*. But the firmware additionally commits to a **little-endian** reading of both
1842-byte arrays (`sys_get_le16`, `:361-362`) while the phone passes the same bytes to
185`UwbAddress.fromBytes()` (`UwbController.kt:163,176`) without any documented byte order, and
186nothing in this repo ever converts a `UwbAddress` to an integer. Promiscuous mode is not
187enabled.
188
189**Runtime consequence:** if `android.ranging.uwb.UwbAddress` is big-endian, *both* addresses
190are byte-swapped relative to the tracker, the hardware filter drops every poll frame before
191the MAC sees it, and the phone reports RX timeout with nothing at all on the tracker side.
192The phone↔phone path cannot have caught this: it exchanges the same bytes symmetrically, so
193the tracker is the first consumer that reinterprets them as an integer.
194
195Supporting (not conclusive) evidence for the firmware being right: Qorvo's NI accessory reads
196the OOB address little-endian too — `#define AR2U16(x) ((x[1] << 8) | x[0])`,
197`fira_niq.c:43,186,218,220`.
198
199**Fix:** two parts, both cheap.
2001. `src/tracker_uwb.c:364` — add `uwbmac_set_promiscuous_mode(uwbmac_ctx, true)` for bring-up
201 (`uwbmac.h:581-590`; `fira_niq.c:125-126` does exactly this, and its comment there is
202 wrong — passing `true` *disables* filtering). This removes the hardware filter as a
203 variable while B3 is being settled.
2042. `src/tracker_uwb.c:361-362` — log both interpretations
205 (`sys_get_le16` and `sys_get_be16`) alongside the addresses at `:463`, then confirm against
206 `RangingSession` logs which one the phone used, and pin it down in `ff_tracker.h:26` so the
207 wire format is no longer implicit.
208
209## B5. Exactly one GATT handover per mode change — every later find fails before UWB is reached
210
211**Spec requires** (INTENDED §1.4, §3.1): the GATT characteristics must remain usable for the
212life of the device so the owner can write per-find UWB parameters to a bound tracker
213(DESIGN:203-204, HW:159-161).
214
215**Code does:** connectability lives on its own legacy set, `connect_adv`, started once inside
216`ff_ble_set_mode(BEACON)` (`tracker_ble.c:525-528`). A connectable advertising set stops when
217a connection is established — verified in the host: `bt_hci_le_adv_set_terminated()` clears
218`BT_ADV_ENABLED` (`C:\ncs\v3.4.0\zephyr\subsys\bluetooth\host\adv.c:2153`) — and **nothing in
219this NCS version restarts it**: `adv_resume` / `BT_ADV_PERSIST` do not exist anywhere in
220`zephyr/subsys/bluetooth/host` (grepped `*.c` and `*.h`; the auto-resume path older Zephyr had
221for the legacy API is gone). Restarting is therefore entirely the application's job. The one
222recovery path is `pending_readvertise`, and it is doubly dead:
223- it is only set when `desired_mode != current_mode` (`tracker_ble.c:139-141`), and
224 `ff_ble_set_mode(BEACON)` returns 0 with `current_mode == desired_mode == BEACON` even when
225 `connect_adv` fails (`:525-528`) — and with `CONFIG_BT_MAX_CONN=2` (`prj.conf:16`) that
226 failure effectively cannot occur anyway, so the flag is never set in practice;
227- even if it were set, `main.c:228-231` calls `ff_ble_set_mode(ff_ble_desired_mode())`, which
228 early-returns at `tracker_ble.c:458` because `mode == current_mode`.
229
230**Runtime consequence:** `TrackerUwbGatt.writeSessionParams` disconnects as soon as the write
231completes (`TrackerUwbGatt.kt:143-149`), so after the very first find the tracker is no longer
232connectable. The second and every subsequent find fails at "service not found"/connect
233timeout, never reaching UWB at all. In bring-up terms: **every iteration needs a power cycle**,
234which is why this is a blocker rather than a MAJOR. Crowd-finding is unaffected (the beacon
235set keeps running), so this does not regress what works today.
236
237**Fix:** `src/tracker_ble.c:123-142` — in `on_disconnected`, set `pending_readvertise`
238unconditionally when `current_mode == FF_BLE_MODE_BEACON`, and add a
239`restart_connectable_adv()` that starts `connect_adv` directly (tolerating `-EALREADY`)
240instead of routing through `ff_ble_set_mode`, whose `mode == current_mode` short-circuit makes
241it unusable for this. `AS_BUILT` §1.3 identified this; the second half of the mechanism (the
242early return) is added here.
243
244---
245
246# MAJOR
247
248## M1. The session thread has half the stack the author determined this work needs
249
250`uwb_session_thread` runs the whole `ff_uwb_start` path — `fira_helper_open`,
251`init_session`, 23 setters, `uwbmac_start`, `fira_helper_start_session` — on **4096** bytes
252(`tracker_uwb.c:92`), while the comment at `:44-47` records that the *same* nested MAC work
253needed 8192 on the poll thread because "4 KB tripped the MPU stack guard immediately on the
254first poll". Consequence: an MPU fault during session setup, which on this configuration
255halts or reboots rather than logging. Not what happened in the observed run (no reset), but
256it is the next failure to appear once B1 lets the path run further.
257**Fix:** `tracker_uwb.c:92` — `4096` → `8192`, and re-check RAM (README records 47 %).
258
259## M2. Nothing ever stops a UWB session, and the poll thread outlives the MAC
260
261`ff_uwb_stop()` is called from exactly one place, the top of `ff_uwb_start()`
262(`tracker_uwb.c:344-346`); nothing stops ranging on BLE disconnect, on ranging failure or on
263a timeout. `poll_running` is set once (`:187`) and **never cleared**, so after
264`ff_uwb_stop()`'s `uwbmac_stop()` (`:482`) the poll thread keeps calling
265`uwbmac_poll_events()` on a stopped MAC, and a later `start_polling()` is a no-op. Consequence:
266the radio and session stay up indefinitely after the phone has finished (battery, and a stale
267session that the next find must tear down first), plus a reachable
268poll-a-stopped-MAC state whose behaviour nothing in this repo specifies.
269**Fix:** clear `poll_running` in `ff_uwb_stop()` (`tracker_uwb.c:475-486`) and join the poll
270thread **after** the `uwbmac_stop()` call, not before — `fira_helper_stop_session()` and
271`fira_helper_deinit_session()` still need the pump alive to complete. Trigger the stop from a
272**bounded idle timeout** (no ranging notification for N seconds, checked on the session
273thread), **not** from the BLE disconnect.
274
275> **Corrected after implementation review (`fixer`).** An earlier revision of this item
276> suggested calling `ff_uwb_stop()` from `on_disconnected` (`tracker_ble.c:123`). That would
277> have been actively harmful: `TrackerUwbGatt.writeSessionParams` disconnects as soon as the
278> write completes — `finish()` calls `gatt.disconnect()`/`close()` (`TrackerUwbGatt.kt:143-149`)
279> — and `startRanging` only calls `ctrl.stream(...)` afterwards (`:93-106`), so the GATT link
280> is **always** down before the first ranging round. A disconnect-triggered teardown would
281> stop the session before ranging began, every time. The same
282> disconnect-then-range ordering is cited in B5 above, and this item contradicted it; the idle
283> timeout was always the sound half of the fix.
284
285## M3. Long-press pairing mode is a dead end that takes the tracker off the crowd network
286
287`main.c:9-11` and README:91 both document long press → pairing mode "so an already-bound
288tracker can be re-bound or have its clock resynced". None of that works:
289- `on_provision_write` refuses every write while `state->provisioned`
290 (`tracker_ble.c:197-201`), so a re-bind is impossible;
291- `ff_store_clear()` exists (`tracker_store.c:137`, declared "Wipes provisioning so the device
292 can be re-bound", `ff_tracker.h:61-62`) and is called from **nowhere**;
293- `CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S` (`Kconfig:26-33`) promises a fallback to the previous
294 mode and is referenced **nowhere**, so pairing mode never ends.
295
296Consequence: one accidental long press stops the beacon and keeps it stopped — crowd-finding
297is off until a short press or a reboot — while the pairing advertisement it puts on air can
298never be acted on. The spec is not innocent here either: INTENDED OQ-9 records that DESIGN:141
299claims an anti-theft lock with no unbind mechanism defined anywhere, so the refusal at
300`tracker_ble.c:197` is the *only* implemented half of a two-half design.
301**Fix (smallest coherent one):** implement the timeout in `main.c:215-272` (track pairing entry
302time, revert to `ff_ble_desired_mode()`'s previous value after
303`CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S`), and either delete the Kconfig symbol and the
304long-press re-bind claim, or define an unbind that requires proof of the secret and calls
305`ff_store_clear()`. Do not simply relax `tracker_ble.c:197` — that would remove the only
306anti-theft behaviour that exists.
307
308## M4. No failure on the tracker side is ever visible to the phone
309
310The `…2f03…` characteristic declares `BT_GATT_CHRC_NOTIFY` (`tracker_ble.c:305-309`) and a
311CCC (`:309`), and nothing ever notifies. `on_uwb_write` acknowledges the write
312(`:270-272`) long before the session is attempted; `ff_uwb_start`'s return is discarded by
313`uwb_session_thread` (`tracker_uwb.c:502`); `main.c:236-239` discards `ff_uwb_on_params`'s
314return; and `fira_helper_init_session`'s `rsp.status_code` is never read
315(`tracker_uwb.c:385-391`). Consequence: the phone starts its 10 s of polling with no idea
316whether a responder exists — INTENDED OQ-7/OQ-19's "design gap, not just an implementation
317gap", and the reason the observed failure produced no actionable information on either side.
318**Fix:** notify a one-byte status on `…2f03…` from the session thread after
319`fira_helper_start_session` succeeds or fails, and have `TrackerUwbGatt.startRanging`
320(`TrackerUwbGatt.kt:93-96`) wait for it before calling `ctrl.stream(...)`. This also fixes the
321responder-first ordering question (OQ-19) rather than leaving it to luck.
322
323## M5. Nothing in this tree enables the DW3110 interrupt (INTENDED OQ-17), and it cannot be ruled out from here
324
325`qplatform_init()` disables the UWB IRQ, and `qplatform_uwb_interrupt_enable()` has no visible
326caller in the SDK; our glue does not call it either (checked all of
327`firmware/qorvo-uwb/src/*.c` — `qplatform_zephyr.c` only supplies the `qm33_rstn`/`qm33_irq`/
328SPI externs). If `llhw` does not call it internally, the tracker sees no DW3110 interrupts at
329all and can never respond. Kept as MAJOR rather than a blocker because it is unverifiable
330statically and because B1 already explains the observed run; it becomes the leading suspect
331the moment B1/B2 are fixed and the trace still shows no `SESSION_STATUS_NTF`.
332**Fix (diagnostic):** after `llhw_init()` (`tracker_uwb.c:236-242`), log the IRQ pin state and
333try an explicit `qplatform_uwb_interrupt_enable()`; if behaviour changes, keep it.
334
335## M6. `round_hopping = true` is a Qorvo code path no vendor sample exercises
336
337**Spec requires** (INTENDED OQ-30, added after this diff was raised): `round_hopping` must
338match, and Android sets `HOPPING_MODE=1`.
339
340**Code does:** the right thing — `ROUND_HOPPING true` (`tracker_uwb.c:71`), matching the
341phone. The problem is not the value; it is that **the Qorvo side of it is unvalidated**.
342`FIRA_DEFAULT_ROUND_HOPPING` is `false`, the CLI only enables hopping behind an explicit
343`-HOP` argument (`fira_app.c:281-285`), and QANI — the only vendor BLE-provisioned responder —
344computes `round_duration_slots = 1 + Round_Duration_RSTU / Slot_Duration_RSTU` with the
345comment *"+1 slot to satisfy corner case when the # of RR is exact the same as the # of slots
346in TWR. **This is fine because hopping is disabled in the NI protocol**"*
347(`fira_niq.c:206`, verified). So the vendor's own accessory reference explicitly depends on
348hopping being off, and this stack's controlee-side hop-sequence derivation is exercised by no
349sample code.
350
351**Runtime consequence:** if the controlee's hop sequence diverges from the controller's, the
352responder listens on the wrong channel/slot for some blocks and not others.
353
354**Why this is MAJOR and not a blocker:** the observed failure is 100 % from the first round to
355the last (RX timeout on *every* round for 10 s), which is the signature of "no responder" or
356"wrong key", not of a hop-sequence disagreement — a wrong hop sequence would produce
357intermittent success or a failure that starts after the first block. Promote this immediately
358if, after B1–B4, ranging works for the first block(s) and then stops.
359**Fix (if reached):** temporarily set `ROUND_HOPPING false` (`tracker_uwb.c:71`) *and* have the
360phone pass a non-hopping config, purely to isolate; do not ship the mismatch, since Android
361enables hopping and nothing negotiates it.
362
363## M7. Diagnostics are configured off in the one place they would pay for themselves
364
365`sp.enable_diagnostics = false` (`tracker_uwb.c:420`) and, since `apply_session_params`
366(`:277-326`) never pushes it, it is not even sent. `report_rssi = 1` is pushed (`:299`,
367`:419`). INTENDED §6.7 lists diagnostics as "extremely useful for this bug".
368**Fix:** set `true` and add a `SET_PARAM(enable_diagnostics)` (or the explicit setter) while
369bringing up; revert before shipping.
370
371---
372
373# MINOR
374
375## m1. `phr_data_rate` is assigned from the wrong enum family
376
377`sp.phr_data_rate = FIRA_PRF_MODE_BPRF;` (`tracker_uwb.c:413`) — a copy of the line above it.
378Verified harmless *today*: `FIRA_PRF_MODE_BPRF == 0` and `FIRA_PHR_DATA_RATE_850K == 0`
379(`fira_region_params.h:277-281, 347-350`), and 850 kbit/s is what Android's config leaves at
380default. It is still a type confusion sitting on one of the 19 must-match fields, so it will
381break silently the day either enum is reordered or the phone selects 6M81.
382**Fix:** `tracker_uwb.c:413` → `sp.phr_data_rate = FIRA_PHR_DATA_RATE_850K;`
383
384## m2. Five must-match fields ride on zero-initialisation with nothing asserting them
385
386`sts_config`, `preamble_duration`, `psdu_data_rate`, `sts_length` and
387`number_of_sts_segments` are left at the `= { 0 }` value (`tracker_uwb.c:332`) and never
388pushed. That is the correct approach for `sts_config` — INTENDED TRAP-9 is right that
389`fira_helper_set_session_sts_config(…, 0x01)` would select **Dynamic** STS, and the firmware
390correctly never calls it — but note `sp.preamble_duration` is left at `0`, which is
391`FIRA_PREAMBLE_DURATION_32` (`fira_region_params.h:288-291`), not 64. It only works because
392the value is never transmitted and the region default governs.
393**Fix:** comment the reliance at `tracker_uwb.c:332` naming the five fields, so a later
394"tidy-up" that adds setters cannot silently select 32-symbol preambles or Dynamic STS.
395
396## m3. `channel` and `preamble` from GATT are used unvalidated
397
398`ff_uwb_on_params` (`tracker_uwb.c:506-530`) accepts any byte. INTENDED OQ-23/OQ-24: this
399stack accepts only channel 5/9 and BPRF preamble 9..12. Harmless today (the phone sends 9/10,
400`UwbController.kt:462,464`), but an out-of-range value currently surfaces only as an
401unexplained session-start failure.
402**Fix:** reject outside `{5, 9}` / `9..12` in `ff_uwb_on_params` with a distinct log line.
403
404## m4. `uwbmac_poll_events()`'s return value is discarded
405
406`(void)uwbmac_poll_events(...)` (`tracker_uwb.c:160`). This single value settles the
407disagreement between the two input documents about whether the pump blocks (§6.2), and it is
408thrown away.
409**Fix:** log it once. Covered by B1's fix list; listed separately because it is worth doing
410even if nothing else changes.
411
412## m5. Three `return 0` paths in `main()` exit before the watchdog is armed
413
414`psa_crypto_init`, `ff_store_init` and `ff_ble_init` failures return from `main()`
415(`main.c:171-191`) before `watchdog_init()` (`:194`), leaving a live board that is
416permanently silent with no self-recovery — the exact scenario `main.c:104-109` says the
417watchdog exists to prevent.
418**Fix:** arm the watchdog first, or `sys_reboot()` on those paths (`CONFIG_REBOOT=y` is
419already set, `prj.conf:85`, and `sys_reboot` is never called).
420
421## m6. Reserved-address nudge is asymmetric on the phone side
422
423`TrackerUwbKeys.kt:56-58` checks both `0x0000` and `0xFFFF`; `tracker_sts.c:99-101` matches
424it exactly. But `UwbController.openController()`/`openControlee()` check only `0x0000`
425(`UwbController.kt:88-91, 115-118`) despite the comment above them, so the phone can hand the
426tracker `0xFFFF` as its own address. 1-in-65536, phone-side, but it is a real
427`destination_short_address` that FiRa reserves.
428**Fix:** `UwbController.kt:88-91` and `:115-118` — add the `0xFFFF` case.
429
430## m7. README and DESIGN describe a state the hardware has moved past
431
432README:156-171 says `qplatform_init()` returns `QERR_EADDRNOTAVAIL` and "**No ranging has been
433attempted**"; DESIGN:36-38 says the UWB path is "staged behind `CONFIG_FF_TRACKER_UWB`
434(default `n`) and never compiled". Both are now false: the stack initialises, the DW3110
435answers `0xdeca0302`, and ranging has been attempted end to end. Anyone reading either
436document first will mis-order their debugging.
437**Fix:** update README §"State" and DESIGN §"Implementation status"/§8 once B1–B3 land.
438
439---
440
441# COSMETIC
442
443- **c1.** `tracker_uwb.c:13-18` says the timing parameters "are FiRa/Qorvo defaults, which is
444 the best available guess"; `:52-67`, 34 lines later, says they "were read off the wire" from
445 Android's UCI log and that the defaults produced status 0x21. Mutually exclusive accounts of
446 the same four constants in one file header. The second is the one the hardware evidence
447 supports — delete the first. (`AS_BUILT` §6.5 flagged this; it is cosmetic only because the
448 *values* are right.)
449- **c2.** README:100-105 describes the beacon as "extended **connectable** advertising", while
450 the implementation deliberately splits connectability onto a separate legacy set and
451 documents why at `tracker_ble.c:336-348`. DESIGN:196-204 has the same stale claim. Since the
452 split is the whole reason B5 exists, the docs should say so.
453- **c3.** `battery_percent()` returns a constant 100 (`main.c:95-98`), matching INTENDED OQ-12,
454 with the reasoning written down. Leave it; it is a documented placeholder, not a defect.
455- **c4.** `meas_seq` antenna sets are `0xff` assigned into `int8_t` fields (`tracker_uwb.c:430-434`),
456 i.e. `-1`. This matches the vendor's "driver default" convention; noted only because
457 `AS_BUILT` §6.5 flags the sign conversion.
458
459---
460
461# 5. Assessment of the uncommitted 1 ms `k_sleep`
462
463`tracker_uwb.c:161-171` (11 added lines: a 10-line comment plus `k_sleep(K_MSEC(1));`).
464
465**Is yielding there correct? Yes.** A preemptible thread at priority 5 that never blocks
466starves priorities 6..14 indefinitely, including the session thread that created it (B1) and
467the log thread at 14 (B2). Some yield is required for the current thread layout to be sound at
468all.
469
470**Is 1 ms appropriate given a 120 ms ranging block? Yes, comfortably.** Slot-precise work does
471not happen here: the DW3xxx ISR runs from the GPIO interrupt callback and loops until the IRQ
472line deasserts (INTENDED §6.4, `qplatform.c:153-157`), so what the poll loop dispatches is
473deferred notification delivery, not TX scheduling. 1 ms is ~0.8 % of a block and ~0.04 % of a
4742400 RSTU slot; the added dispatch latency is invisible. On this target the tick is 32768 Hz
475so `K_MSEC(1)` really is ~1 ms rather than being rounded up to a tick boundary — worth
476re-checking `CONFIG_SYS_CLOCK_TICKS_PER_SEC` if the board config is ever changed, since at
477100 Hz this would silently become 10 ms.
478
479**Does it explain log output stopping without a watchdog reset? It explains it exactly — but
480its stated premise is contradicted by the vendor header, and the fix does not depend on the
481premise being right.**
482- The mechanism it describes is real and verified: log thread at 14, UWB threads at 5/6, main
483 at 0 feeding an 8 s watchdog every 1 s (`main.c:113,216`), `printk` deferred as well
484 (`CONFIG_LOG_PRINTK` default `y`). Output stopping dead with no reset is precisely that
485 signature. **This part of the comment is correct and is the most valuable thing in the
486 delta.**
487- Its premise — that `uwbmac_poll_events` "returns immediately when there is nothing to
488 dispatch" — contradicts `uwbmac.h:331-336`: *"Passing a value greated than 0 will make the
489 function block until the timeout is reached when there is no pending event."* `AS_BUILT`
490 §6.3 is right to flag this. It is not settled, though: the same header says at `:328-329`
491 that the function "is only available if you passed a NULL `event_loop_ops` to
492 `uwbmac_init()`", and no `event_loop_ops` symbol exists anywhere in this delivery, so
493 whether the call is functional-and-blocking or returns an error immediately in *this* build
494 is unknown. One logged return value decides it (m4).
495
496**Verdict: keep it, and do not stop there.** It is necessary-but-insufficient. Even with the
497sleep, the poll thread still outranks the session thread that creates it, so session-start
498liveness depends on a sleep rather than on the priority order — and the poll thread still sits
499inside the vendor MAC threads' 0..6 priority band. Land the sleep together with
500`UWB_THREAD_PRIORITY` 5 → 7 (B1) and the logging change (B2). Rated **correct fix, wrong
501justification, incomplete scope** — not a defect to revert.
502
503---
504
505# 6. Corrections to the two input documents
506
507Both documents are unusually careful and most of what follows is a refinement rather than an
508error. These are the places where one of them would mislead a reader.
509
510**Status:** all three items below were raised with the authors by message rather than decided
511unilaterally, and both authors have since amended their documents. The disagreements are
512recorded here anyway, because the reasoning is what justifies the blocker ordering above.
513
514## 6.1 `INTENDED_BEHAVIOUR.md` §6.7 — three of the "must match exactly" values were wrong
515
516The table gives `block_duration_ms` **200**, `round_duration_slots` **25** and `round_hopping`
517**false**. Those are the *vendor sample application's* defaults — verified at
518`C:\qorvo\Src\Apps\Src\fira\Inc\fira_default_params.h`
519(`FIRA_DEFAULT_BLOCK_DURATION_MS 200`, `FIRA_DEFAULT_ROUND_DURATION_SLOTS 25`,
520`FIRA_DEFAULT_ROUND_HOPPING false`) — not what Android programs. The observed UCI
521`SESSION_SET_APP_CONFIG` is `RANGING_DURATION=120`, `SLOTS_PER_RR=6`, `HOPPING_MODE=1`,
522`SLOT_DURATION=2400`. **The firmware's `120 / 6 / true` (`tracker_uwb.c:69-71`) is correct and
523the spec's numbers would break a working configuration.** The document is honest about this
524risk (§8 says the table is "the Qorvo-side requirement set … a prior is not a citation"), but
525the table itself does not carry the caveat, and it is exactly the kind of table someone
526copies. Everything else in §6.7 held up against the headers.
527
528**Resolved:** `spec` accepted this and rewrote the three rows against the UCI capture, added
529§6.7.1 recording the provenance, and — usefully — turned the correction into a new finding,
530**OQ-30**, that `round_hopping = true` is a vendor-untested path. That is carried into this
531report as **M6**, and it is a better outcome than the correction itself.
532
533## 6.2 `AS_BUILT.md` §6.3 — right about the header, incomplete about the consequence
534
535The vendor-header quote is accurate and the delta's comment does contradict it. But the
536conclusion "the HEAD loop blocks up to 100 ms per iteration and does not starve anything" rests
537on a contract the same header undercuts two paragraphs earlier (`:328-329`, no
538`event_loop_ops` symbol exists — which is `INTENDED` OQ-18). More importantly, the priority
539defect in B1 does not depend on the answer: a thread created at priority 5 from a priority-6
540thread preempts its creator immediately regardless of what it then does, so the ordering
541`start_polling()` → `fira_helper_start_session()` is unsound either way.
542
543**Resolved:** `asbuilt` accepted this and rewrote §6.3/§6.3.1 to state that the header settles
544neither possibility, and to record the preemption relationship and that the delta is
545"load-bearing for liveness" in the non-blocking case. The two documents and this one now agree.
546
547## 6.3 `AS_BUILT.md` §10 summary — "`pending_readvertise` is never set" is a shorthand
548
549The body (§1.3) states it precisely: set only when `desired_mode != current_mode`. The full
550mechanism, added here, is that **both** halves are broken — the trigger effectively cannot
551fire (`CONFIG_BT_MAX_CONN=2` removes the `-ENOMEM` case that would make the modes differ) and
552the recovery is a no-op if it does (`ff_ble_set_mode` returns 0 early when
553`mode == current_mode`, `tracker_ble.c:458`). Fixing only the trigger would not restore
554connectability. See B5.
555
556**Resolved:** `asbuilt` §1.3 (lines 144-148) already carried the decisive case — a connection
557accepted *while in beacon mode* stops `connect_adv` with both modes still `BEACON` — so the
558disagreement was only with the §10 summary's shorthand. The early-return half
559(`tracker_ble.c:458`) is this document's addition and is what makes the fix in B5 bigger than
560one line.
561
562## 6.4 Both documents, on "re-binding is impossible"
563
564`AS_BUILT` reports the code fact (nothing clears `provisioned`; `ff_store_clear()` unused);
565`INTENDED` OQ-9 reports the documentation fact (DESIGN:141 claims an anti-theft lock with no
566unbind protocol defined). Neither on its own conveys the runtime consequence, which is what
567makes it MAJOR: a long press stops the beacon, so the tracker leaves the crowd network, and
568the pairing advertisement it enters can never be acted upon and never times out. See M3.
569
570---
571
572# 7. Checked and found consistent — do not "fix" these
573
574Recorded so the fix pass does not churn code that is already right.
575
576| Item | Status |
577|---|---|
578| All 19 "must match exactly" FiRa fields vs. Android's observed UCI config | **All 19 match.** Verified enumerator values in `fira_region_params.h`: `DSTWR_DEFERRED`=2, `UNICAST`=0, `TIME_SCHEDULED`=1, `SP3`=3, `SFD_ID_2`=2, `BPRF`=0, `PHR_DATA_RATE_850K`=0; and `fira_helper_bool_to_ranging_round_control(true,false)` = `BIT(0)|BIT(1)` = **0x03** = Android's `RANGING_ROUND_CONTROL=3` (`fira_helper.h:2003-2010`), `bool_to_result_report_config(true,false,false,false)` = **0x01** = Android's `RESULT_REPORT_CONFIG=1`. **A parameter-value mismatch is not the bug.** The one field whose *value* matches but whose *implementation* is unvalidated is `round_hopping` — see M6 |
579| Stack bring-up order `qplatform_init` → `l1_config_init` → `llhw_init` → `uwbmac_init` | Matches INTENDED §6.1 exactly (`tracker_uwb.c:209-250`), with correct reverse-order cleanup on each failure |
580| Per-session order: `set_short_addr` → `open` → `set_scheduler` → `init_session` → params → `uwbmac_start` → `start_session` | Matches INTENDED §6.5 (`tracker_uwb.c:364-454`); `set_scheduler` is before `uwbmac_start` as required |
581| `session_id` vs `session_handle` (TRAP-7) | Correct: `rsp.session_handle` is used for every setter and for start/stop/deinit |
582| `session_parameters` zero-init (TRAP-8) | Correct: `= { 0 }` at `tracker_uwb.c:332` |
583| Never calling `fira_helper_set_session_sts_config` (TRAP-9) | Correct — the setter that would select Dynamic STS is not called |
584| `mcps_crypto_*` completeness (TRAP-5) | All ~16 symbols implemented on PSA in `firmware/qorvo-uwb/src/mcps_crypto_psa.c` (CMAC-128/256, CCM* create/destroy/encrypt/decrypt/inout, ECB, `get_random`) |
585| `l1_config` OTP calibration hook (TRAP-4) | Vendor `DWM3001CDK/platform_l1_config.c` + `Common/helper_platform_l1_config.c` are compiled (`firmware/qorvo-uwb/CMakeLists.txt:140-141`) and `l1_config_init(&l1_config_platform_ops)` is passed real ops |
586| `uwbmac_set_short_addr` present (TRAP-10, primary form) | Called at `tracker_uwb.c:364` |
587| HKDF derivation vs `TrackerUwbKeys.kt` | Info strings, 32-zero-byte salt, `u32_be(sessionId)`, 8/2-byte outputs and the `0x0000`/`0xFFFF` nudge all match (`tracker_sts.c:27-103` vs `TrackerUwbKeys.kt:23-67`) |
588| Two-radio BLE design (extended beacon + legacy pairing + separate connectable set) | Sound, and the reason it is split is documented; **do not merge the sets** while fixing B5 |
589| Watchdog covering `main` only | Intentional and documented (`main.c:102-112`); it is what kept crowd-finding alive through the observed failure |
590
591---
592
593# 8. Recommended order of work
594
5951. **B2** (logging) — one Kconfig line; without it nothing below can be verified.
5962. **B1** (poll thread priority 5 → 7, keep the `k_sleep`, log the poll return) — then re-run
597 and confirm the seven `printk` markers and a `SESSION_STATUS_NTF` appear.
5983. If the session now reaches ACTIVE and the phone still times out: **B3** (reverse
599 `vupper64`), then **B4** (promiscuous mode + address byte order), in that order — B3 is one
600 line and has vendor precedent; B4 is a two-build experiment.
6014. **M5** if there is still no `SESSION_STATUS_NTF` at all after B1/B2 — that would mean no
602 DW3110 interrupts. **M6** instead if ranging starts and then stops, rather than never
603 working — that would mean the hop sequence.
6045. **B5** and **M1** before any longer bench session, so iterations stop needing a power cycle
605 and the session path stops running on half the stack it needs.
6066. **M2/M3/M4/M7**, then the MINORs.