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

Modern-Apps.git / firmware / findfamily-tracker / src / ff_tracker.h

ff_tracker.h155 lines · 5.4 KiBRaw
1/*
2 * FindFamily UWB tracker — internal interfaces.
3 *
4 * The wire formats here are fixed by the phone side; see
5 * findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerBle.kt and
6 * TrackerProtocol.kt. Any change has to be made in both places at once.
7 */
8#ifndef FF_TRACKER_H_
9#define FF_TRACKER_H_
10
11#include <stdbool.h>
12#include <stdint.h>
13#include <stddef.h>
14
15/* TrackerProtocol.SECRET_LEN */
16#define FF_SECRET_LEN 32
17/* TrackerProtocol.EPOCH_ID_LEN */
18#define FF_EPOCH_ID_LEN 16
19/* TrackerProtocol.EPOCH_SECONDS */
20#define FF_EPOCH_SECONDS 900U
21
22/* TrackerBle.PROVISION_BLOB_LEN / PROVISION_BLOB_LEN_NO_TIME */
23#define FF_PROVISION_BLOB_LEN 48
24#define FF_PROVISION_BLOB_LEN_NO_TIME 40
25
26/* TrackerUwbGatt.encodeSessionParams:
27 * [2B addr little-endian][4B sessionId BE][1B ch][1B preamble] */
28#define FF_UWB_PARAMS_LEN 8
29
30/* Service data payload: [16B epochId][1B battery%] */
31#define FF_BEACON_DATA_LEN (FF_EPOCH_ID_LEN + 1)
32
33/* Everything the tracker persists across resets. */
34struct ff_tracker_state {
35 uint64_t tracker_user_id;
36 uint8_t secret[FF_SECRET_LEN];
37 bool provisioned;
38 /*
39 * Wall-clock seconds at the moment `uptime_at_base_ms` was taken. Zero means
40 * "no time base": the tracker cannot compute an epoch id anyone is looking for
41 * and stays quiet rather than beaconing garbage.
42 */
43 uint64_t unix_at_base;
44 int64_t uptime_at_base_ms;
45};
46
47/* ---- tracker_store.c: NVS-backed persistence ---------------------------- */
48
49int ff_store_init(void);
50
51/* Loads persisted state into `out`. Returns 0 even when nothing is stored yet, in
52 * which case `out->provisioned` is false. */
53int ff_store_load(struct ff_tracker_state *out);
54
55int ff_store_save_provisioning(uint64_t tracker_user_id, const uint8_t *secret,
56 uint64_t unix_seconds);
57
58/* Re-persists just the time base. Called once per epoch so a reset costs at most one
59 * epoch of drift instead of the whole time base. */
60int ff_store_save_time(uint64_t unix_seconds);
61
62/* Wipes provisioning so the device can be re-bound. */
63int ff_store_clear(void);
64
65/* ---- tracker_epoch.c: rotating beacon id ------------------------------- */
66
67/* Current wall-clock seconds, or 0 if there is no time base yet. */
68uint64_t ff_now_unix(const struct ff_tracker_state *st);
69
70/* epoch = unix_seconds / FF_EPOCH_SECONDS */
71uint64_t ff_current_epoch(const struct ff_tracker_state *st);
72
73/*
74 * epochId = HMAC-SHA256(secret, "fftrk1" || u64_be(epoch))[..16].
75 * Mirrors TrackerProtocol.epochId; `out` must hold FF_EPOCH_ID_LEN bytes.
76 */
77int ff_epoch_id(const uint8_t *secret, uint64_t epoch, uint8_t *out);
78
79/* ---- tracker_ble.c: advertising + GATT --------------------------------- */
80
81enum ff_ble_mode {
82 /* Nothing on air: unprovisioned and not pairing, or muted by the user. */
83 FF_BLE_MODE_IDLE,
84 /* Legacy connectable adv of the unprovisioned service, awaiting a bind. */
85 FF_BLE_MODE_PAIRING,
86 /* Extended non-connectable adv carrying the rotating epoch id, plus a separate
87 * legacy connectable set so the owner can still write UWB session params. */
88 FF_BLE_MODE_BEACON,
89};
90
91int ff_ble_init(struct ff_tracker_state *state);
92
93int ff_ble_set_mode(enum ff_ble_mode mode);
94
95enum ff_ble_mode ff_ble_mode(void);
96
97/*
98 * GATT writes are handled on the Bluetooth RX thread, whose stack is nowhere near big
99 * enough for PSA crypto plus starting an advertisement — doing that work inline faults
100 * with a stack overflow. The write callbacks therefore only record what arrived, and the
101 * main loop drains it. Both of these return true once per event and clear it.
102 */
103bool ff_ble_take_provision_event(void);
104
105/* Copies FF_UWB_PARAMS_LEN bytes into `out` when a session-params write is pending. */
106bool ff_ble_take_uwb_params(uint8_t *out);
107
108/*
109 * True once after a disconnect. Both cases need it: a mode we could not start while the
110 * connection slot was taken, and — in beacon mode — the connectable set the controller
111 * stopped when it accepted that connection.
112 */
113bool ff_ble_take_readvertise_event(void);
114
115/*
116 * Puts advertising back to what it should be after a disconnect. Not the same as
117 * ff_ble_set_mode(): that returns early when the mode is already current, which is exactly
118 * the case where the connectable set needs restarting.
119 */
120int ff_ble_readvertise(void);
121
122/* Recomputes the epoch id and pushes fresh service data into the beacon adv set. */
123int ff_ble_refresh_beacon(uint8_t battery_percent);
124
125/* ---- UWB (CONFIG_FF_TRACKER_UWB) --------------------------------------- */
126
127#ifdef CONFIG_FF_TRACKER_UWB
128/*
129 * HKDF-SHA256 derivations mirroring TrackerUwbKeys.kt. `out` must hold 8 bytes for
130 * the STS key and 2 for the address.
131 */
132int ff_sts_key(const uint8_t *secret, uint32_t session_id, uint8_t *out);
133int ff_uwb_address(const uint8_t *secret, uint8_t *out);
134
135struct ff_uwb_params {
136 uint8_t peer_address[2]; /* the phone's UWB MAC, from the GATT write */
137 uint32_t session_id;
138 uint8_t channel;
139 uint8_t preamble_index;
140};
141
142int ff_uwb_init(void);
143
144/* Starts a FiRa responder/controlee session. Session key and our own address are
145 * derived from `secret`, never taken from the wire. */
146int ff_uwb_start(const struct ff_uwb_params *params, const uint8_t *secret);
147
148void ff_uwb_stop(void);
149#endif /* CONFIG_FF_TRACKER_UWB */
150
151/* Handles a write to the UWB session characteristic. Defined in tracker_uwb.c when
152 * CONFIG_FF_TRACKER_UWB is set, and stubbed in tracker_ble.c otherwise. */
153int ff_uwb_on_params(const uint8_t *data, size_t len, const uint8_t *secret);
154
155#endif /* FF_TRACKER_H_ */