Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / findfamily-tracker / README.md
| 1 | # FindFamily UWB tracker firmware |
| 2 | |
| 3 | Zephyr / nRF Connect SDK firmware for a FindFamily item tracker. Target: **Qorvo |
| 4 | DWM3001CDK** — a DWM3001C module (Nordic **nRF52833** + Qorvo **DW3110**) on a dev kit |
| 5 | with an on-board J-Link OB. |
| 6 | |
| 7 | What it does today: binds to the phone over BLE GATT and broadcasts a rotating, |
| 8 | unlinkable epoch id so any FindFamily phone that walks past can report the tracker's |
| 9 | location back to its owner. UWB precision finding is written but **not compiled** — see |
| 10 | [UWB](#uwb-precision-finding) below. |
| 11 | |
| 12 | The wire formats here are not local decisions; they mirror the phone. See |
| 13 | `findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBle.kt` (the contract), |
| 14 | `TrackerProtocol.kt` (epoch ids) and `TrackerUwbKeys.kt` (session material), plus |
| 15 | `FINDFAMILY_UWB_TRACKER_DESIGN.md` and `FINDFAMILY_UWB_TRACKER_HARDWARE.md` at the repo |
| 16 | root. Changing a format means changing it in both places at once. |
| 17 | |
| 18 | > This directory is invisible to the repo's builds: `settings.gradle.kts` uses an explicit |
| 19 | > `include()` list, `Cargo.toml` an explicit `members` list, and CI discovers modules by |
| 20 | > looking for `build.gradle.kts`. Nothing here needs excluding anywhere. |
| 21 | |
| 22 | --- |
| 23 | |
| 24 | ## Layout |
| 25 | |
| 26 | | File | Role | |
| 27 | |---|---| |
| 28 | | `src/ff_tracker.h` | Shared types + the wire-format constants that mirror the phone | |
| 29 | | `src/main.c` | Mode state machine, button handling, once-a-second beacon refresh | |
| 30 | | `src/tracker_store.c` | NVS persistence: user id, secret, time base | |
| 31 | | `src/tracker_epoch.c` | `epochId = HMAC-SHA256(secret, "fftrk1" ‖ u64be(epoch))[..16]`, time base | |
| 32 | | `src/tracker_ble.c` | Two advertising modes + the GATT server | |
| 33 | | `src/tracker_sts.c` | HKDF-SHA256 STS key + UWB address (`CONFIG_FF_TRACKER_UWB`) | |
| 34 | | `src/tracker_uwb.c` | FiRa responder session on the DW3110 (`CONFIG_FF_TRACKER_UWB`) | |
| 35 | |
| 36 | Crypto goes through **PSA Crypto** (nrf_security/mbedTLS) rather than anything |
| 37 | hand-rolled, because the epoch id has to be byte-identical to the Kotlin |
| 38 | (`TrackerProtocol.epochId`) and Rust (`tracker_epoch_id`) implementations. A one-byte |
| 39 | disagreement doesn't fail loudly — it just means no sighting ever resolves. |
| 40 | |
| 41 | --- |
| 42 | |
| 43 | ## Toolchain |
| 44 | |
| 45 | ```powershell |
| 46 | winget install NordicSemiconductor.nrfutil |
| 47 | nrfutil install toolchain-manager |
| 48 | nrfutil toolchain-manager install --ncs-version v3.4.0 |
| 49 | ``` |
| 50 | |
| 51 | Plus **SEGGER J-Link** (from segger.com) for the on-board probe. Install it even if the |
| 52 | kit already enumerates: without the SEGGER driver the probe comes up as |
| 53 | `BULK interface ... Status: Error` and nothing can flash it. |
| 54 | |
| 55 | Then fetch the SDK **outside this repo** (~10 GB; `release.sh` requires a clean tree and |
| 56 | runs `git add .`, so a west workspace inside the repo would be a problem): |
| 57 | |
| 58 | ```powershell |
| 59 | mkdir C:\ncs\v3.4.0 |
| 60 | nrfutil toolchain-manager launch --ncs-version v3.4.0 -- ` |
| 61 | west init -m https://github.com/nrfconnect/sdk-nrf --mr v3.4.0 C:/ncs/v3.4.0 |
| 62 | cd C:\ncs\v3.4.0 |
| 63 | nrfutil toolchain-manager launch --ncs-version v3.4.0 -- west update --narrow -o=--depth=1 |
| 64 | ``` |
| 65 | |
| 66 | ## Build and flash |
| 67 | |
| 68 | The board is supported upstream as `decawave_dwm3001cdk/nrf52833`, so no custom board |
| 69 | definition is needed. Build from inside the west workspace, with the build directory |
| 70 | outside the repo: |
| 71 | |
| 72 | ```powershell |
| 73 | cd C:\ncs\v3.4.0 |
| 74 | nrfutil toolchain-manager launch --ncs-version v3.4.0 -- ` |
| 75 | west build -b decawave_dwm3001cdk/nrf52833 -d C:/ncs/ff-build ` |
| 76 | <path-to-repo>/firmware/findfamily-tracker --pristine |
| 77 | |
| 78 | nrfutil toolchain-manager launch --ncs-version v3.4.0 -- west flash -d C:/ncs/ff-build |
| 79 | ``` |
| 80 | |
| 81 | Logs come out of the on-board J-Link CDC UART at 115200 (COM4 on this machine). |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ## Behaviour |
| 86 | |
| 87 | **On boot:** loads persisted state. Provisioned *and* time-synced → beacon mode. |
| 88 | Otherwise → pairing mode, so a fresh board is bindable without touching the button. |
| 89 | |
| 90 | **Button** (the kit's Button 1, `sw0`): |
| 91 | - long press (≥ 2 s) → pairing mode for `CONFIG_FF_TRACKER_PAIRING_TIMEOUT_S`, then back to |
| 92 | beaconing. This does **not** re-bind: a provisioned tracker refuses provisioning writes |
| 93 | (the anti-theft lock), and nothing unbinds one today — `ff_store_clear()` has no caller. |
| 94 | - short press → mute/unmute the beacon |
| 95 | |
| 96 | **Advertising** uses two modes on one state machine, arranged so only the phone's beacon |
| 97 | scanner needed changing: |
| 98 | |
| 99 | - **Pairing** — *legacy* connectable advertising of the unprovisioned service |
| 100 | (`…2f01…`). Flags + a 128-bit UUID is 21 bytes, comfortably inside the legacy budget, |
| 101 | so `TrackerProvisioner.unprovisioned()` still finds it with default `ScanSettings`. |
| 102 | - **Beacon** — *extended* **non-connectable** advertising of `[16B epochId][1B battery%]` |
| 103 | as service data under `…2f00…`. This one **cannot** be legacy: the AD structure costs |
| 104 | `1B len + 1B type + 16B UUID + 17B payload = 35 bytes` against a 31-byte limit, and the |
| 105 | UUID can't be shortened because the phone looks the service data up by it. Secondary PHY |
| 106 | stays at 1M so the phone needs no coded-PHY scan config. Connectability is a **third, |
| 107 | legacy, dataless set** running alongside it, so the owner can still write UWB session |
| 108 | params after binding — a connectable extended set cannot carry advertising data at all, |
| 109 | which is why the two are separate. See `beacon_param` in `src/tracker_ble.c`. |
| 110 | |
| 111 | **Time.** The board has no battery-backed RTC, but `epochId` needs `unix_seconds / 900`, |
| 112 | so the provisioning blob carries a timestamp and time is kept as |
| 113 | `unix_at_provision + k_uptime_get()` (LFCLK-backed, survives sleep). The current time is |
| 114 | re-persisted to NVS once per epoch, so a reset costs at most one epoch of drift rather |
| 115 | than the whole time base. A tracker with **no** time base refuses to beacon and returns to |
| 116 | pairing mode instead of broadcasting ids nobody is searching for. |
| 117 | |
| 118 | The owner only searches `TrackerProtocol.recentEpochIds(back = 8)` ≈ 2.25 h, so a tracker |
| 119 | left unpowered long enough for its persisted time to go stale needs re-provisioning to |
| 120 | resync. |
| 121 | |
| 122 | --- |
| 123 | |
| 124 | ## Bench verification without the phone |
| 125 | |
| 126 | The log prints the live epoch id every second: |
| 127 | |
| 128 | ``` |
| 129 | <inf> ff_ble: epoch=1985934 id=96592d51... batt=100% |
| 130 | ``` |
| 131 | |
| 132 | Cross-check it against an independent computation from the same secret and timestamp — |
| 133 | `epochId = HMAC-SHA256(secret, "fftrk1" ‖ u64be(epoch))[..16]`. This separates "the |
| 134 | firmware's crypto is wrong" from "the phone isn't hearing the advertisement", which |
| 135 | otherwise look identical. |
| 136 | |
| 137 | --- |
| 138 | |
| 139 | ## UWB precision finding |
| 140 | |
| 141 | `src/tracker_sts.c` and `src/tracker_uwb.c` build under `CONFIG_FF_TRACKER_UWB`, which |
| 142 | needs the licensed Qorvo **DW3_QM33 SDK**. It is not vendored here; only the glue is, in |
| 143 | `firmware/qorvo-uwb/` (a Zephyr module). Point two environment variables at an extracted |
| 144 | copy and build with the `uwb.conf` fragment: |
| 145 | |
| 146 | ```powershell |
| 147 | $env:QORVO_SDK_DIR = "C:/qorvo" # contains Libs/uwb-stack |
| 148 | $env:QORVO_DRIVER_DIR = "<...>/Drivers/API/Shared/dwt_uwb_driver" |
| 149 | |
| 150 | cd C:\ncs\v3.4.0 |
| 151 | nrfutil toolchain-manager launch --ncs-version v3.4.0 -- ` |
| 152 | west build -b decawave_dwm3001cdk/nrf52833 -d C:/ncs/ff-uwb-build ` |
| 153 | <repo>/firmware/findfamily-tracker --pristine ` |
| 154 | -- "-DEXTRA_CONF_FILE=uwb.conf" ` |
| 155 | "-DZEPHYR_EXTRA_MODULES=<repo>/firmware/qorvo-uwb" |
| 156 | ``` |
| 157 | |
| 158 | Quote the `-D` arguments: PowerShell otherwise splits `uwb.conf` at the dot. |
| 159 | |
| 160 | ### State: the stack comes up; ranging does not work yet |
| 161 | |
| 162 | Verified on hardware: |
| 163 | - the image builds and links clean and BLE crowd-finding is unaffected — the beacon keeps |
| 164 | working in the UWB image; |
| 165 | - the DW3110 answers a direct device-id read over SPI with **`0xdeca0302`**, logged at |
| 166 | startup. That clears the wiring, the `dw3110` devicetree node, chip-select handling and |
| 167 | the SPI mode; |
| 168 | - `qplatform_init()` now succeeds and the Qorvo stack initialises, so ranging has been |
| 169 | attempted end to end against a phone. It fails: the phone reports UCI RangingStatus |
| 170 | `0x21` (RX timeout) on every round for its whole 10 s and then drops the controlee, with |
| 171 | nothing logged on the tracker side. |
| 172 | |
| 173 | That failure is being worked through against `docs/DISCREPANCIES.md`, which orders the |
| 174 | candidates and cites each one. Nothing in the current tree has been re-verified on hardware |
| 175 | since those fixes landed, so treat FiRa interop as unproven. |
| 176 | |
| 177 | ### What the glue replaces, and why |
| 178 | |
| 179 | Qorvo ships Zephyr backends but they cannot be used as they are: |
| 180 | |
| 181 | | Vendor file | Problem | Replacement | |
| 182 | |---|---|---| |
| 183 | | `qhal/src/zephyr/qspi.c` | every function returns `QERR_ENOTSUP` — a placeholder | `src/qspi_zephyr.c` | |
| 184 | | `qhal/src/zephyr/qgpio.c` | defines `gpio_is_ready_dt`/`gpio_add_callback_dt`, which Zephyr now provides | `src/qgpio_zephyr.c` | |
| 185 | | `qplatform/qm33_qhal_zephyr/qplatform.c` | treats `spi_config.cs` as a pointer (by value since Zephyr 3.5); requires a `dw35720` node label | `src/qplatform_zephyr.c` | |
| 186 | | `qhal/src/qm33/qpwr.c`, `qhal/src/qrtc.c` | same, or route through `persistent_time`, which is nrfx-only | `src/qhal_extras_zephyr.c` | |
| 187 | | `qosal/src/zephyr/qmalloc.c` | uses libc malloc, which collides with picolibc's in this build | `src/qmalloc_zephyr.c` (k_malloc) | |
| 188 | | `Src/UWB/mcps_crypto.c` | built on Nordic's nrf_crypto, a second crypto provider | `src/mcps_crypto_psa.c` (PSA) | |
| 189 | |
| 190 | `include-compat/` additionally papers over qosal being written against Zephyr 3.1: the |
| 191 | pre-3.0 unprefixed include paths, and the internal `log_msg2` logging symbols renamed in |
| 192 | Zephyr 3.2. That directory is the most likely thing to break on an NCS upgrade. |
| 193 | |
| 194 | The vendor's DWM3001CDK calibration code (`platform_l1_config.c`) is compiled as-is — it |
| 195 | reads this module's factory antenna delays and crystal trim out of the DW3110's OTP, and |
| 196 | those values are what make ranging accurate. |
| 197 | |
| 198 | ### Interop is still the open risk |
| 199 | |
| 200 | Even once the stack initialises, matching Android's `CONFIG_UNICAST_DS_TWR` against a |
| 201 | hand-configured responder means aligning STS, RFRAME format, and ranging-round and block |
| 202 | timing. Under static STS the timing parameters are configured identically on both ends |
| 203 | rather than negotiated, so `SLOT_DURATION_RSTU`, `BLOCK_DURATION_MS` and |
| 204 | `ROUND_DURATION_SLOTS` in `src/tracker_uwb.c` are the first things to vary if the phone |
| 205 | will not range. |