Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / findfamily-tracker / src / tracker_uwb.c
tracker_uwb.c842 lines · 30.5 KiBRaw
| 1 | /* |
| 2 | * FiRa responder/controlee session on the DW3110, via Qorvo's uwbstack. |
| 3 | * |
| 4 | * The phone is the FiRa controller/initiator: it mints the session id, channel, |
| 5 | * preamble and its own MAC, and drives the ranging rounds. This device is the |
| 6 | * controlee/responder. Session material is derived, not received — see tracker_sts.c |
| 7 | * and TrackerUwbKeys.kt — so the 8 bytes written over GATT per find are only address, |
| 8 | * session id, channel and preamble. |
| 9 | * |
| 10 | * Built only when CONFIG_FF_TRACKER_UWB=y, which needs the licensed Qorvo SDK; see |
| 11 | * README.md and firmware/qorvo-uwb/. |
| 12 | */ |
| 13 | #include "ff_tracker.h" |
| 14 | |
| 15 | #include <zephyr/kernel.h> |
| 16 | #include <zephyr/logging/log.h> |
| 17 | #include <zephyr/sys/byteorder.h> |
| 18 | |
| 19 | /* Qorvo uwbstack. */ |
| 20 | #include <ff_qorvo_l1_config.h> |
| 21 | #include <l1_config.h> |
| 22 | #include <llhw.h> |
| 23 | #include <net/fira_region_params.h> |
| 24 | #include <qplatform.h> |
| 25 | #include <quwbs/fbs/defs.h> |
| 26 | #include <uwbmac/fira_helper.h> |
| 27 | #include <uwbmac/uwbmac.h> |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | LOG_MODULE_REGISTER(ff_uwb, LOG_LEVEL_INF); |
| 32 | |
| 33 | /* |
| 34 | * The stack calls back from MAC context, so ranging results are only logged here. |
| 35 | * |
| 36 | * 8 KB because uwbmac_poll_events runs the whole MAC event loop on this stack — frame |
| 37 | * assembly, crypto and region callbacks all nest below it. 4 KB tripped the MPU stack |
| 38 | * guard immediately on the first poll. |
| 39 | */ |
| 40 | #define UWB_THREAD_STACK_SIZE 8192 |
| 41 | /* |
| 42 | * Below the session thread that creates it (6), and below every vendor MAC thread: qosal |
| 43 | * runs CRITICAL=0..IDLE=6 (qthread.h) and firmware/qorvo-uwb/src/qthread_zephyr.c maps |
| 44 | * that straight onto K_PRIO_PREEMPT(0..6), so the MAC occupies Zephyr 0..6. At 5 this |
| 45 | * thread preempted its own creator the instant k_thread_create() returned, which left |
| 46 | * fira_helper_start_session() — the call immediately after start_polling() — reachable |
| 47 | * only if the poll loop happened to give the CPU back. |
| 48 | */ |
| 49 | #define UWB_THREAD_PRIORITY 7 |
| 50 | |
| 51 | /* |
| 52 | * Ranging timing, and the STS/PHY settings that go with it. |
| 53 | * |
| 54 | * These are NOT free choices: under static STS none of it is negotiated, so the controlee |
| 55 | * has to be configured identically to the controller or it listens on the wrong schedule |
| 56 | * and never hears a poll. The values below were read off the wire — Android's UWB HAL logs |
| 57 | * its raw UCI SESSION_SET_APP_CONFIG, and these are the TLVs it sends for |
| 58 | * RangingParams CONFIG_UNICAST_DS_TWR: |
| 59 | * |
| 60 | * SLOT_DURATION 2400 RSTU (2 ms) |
| 61 | * RANGING_DURATION 120 ms <- block duration |
| 62 | * SLOTS_PER_RR 6 |
| 63 | * HOPPING_MODE 1 <- enabled |
| 64 | * |
| 65 | * Guessing these from the FiRa defaults instead (200 ms / 25 slots / no hopping) produced a |
| 66 | * session where the phone reported UCI status 0x21, RX timeout, on every round. |
| 67 | */ |
| 68 | #define SLOT_DURATION_RSTU 2400 |
| 69 | #define BLOCK_DURATION_MS 120 |
| 70 | #define ROUND_DURATION_SLOTS 6 |
| 71 | #define ROUND_HOPPING true |
| 72 | |
| 73 | static struct uwbmac_context *uwbmac_ctx; |
| 74 | static struct fira_context fira_ctx; |
| 75 | static uint32_t session_handle; |
| 76 | static bool stack_ready; |
| 77 | static bool session_active; |
| 78 | |
| 79 | static K_THREAD_STACK_DEFINE(uwb_thread_stack, UWB_THREAD_STACK_SIZE); |
| 80 | static struct k_thread uwb_thread; |
| 81 | static atomic_t poll_running; |
| 82 | /* Only ever touched from the session thread, which is the only caller of start/stop. */ |
| 83 | static bool poll_thread_created; |
| 84 | /* |
| 85 | * k_uptime_get_32() at session start, then at every notification. Written from MAC |
| 86 | * context and read by the session thread, so it goes through atomic_t. |
| 87 | */ |
| 88 | static atomic_t last_activity_ms; |
| 89 | |
| 90 | /* |
| 91 | * Session setup runs on its own thread rather than on the caller's. |
| 92 | * |
| 93 | * The fira_helper_* and uwbmac_start calls block, and at least one of them can block |
| 94 | * indefinitely when the stack is unhappy. Called straight from the main loop — which is |
| 95 | * also what refreshes the beacon and drives the mode state machine — a stall there took |
| 96 | * the whole tracker off the air, so crowd-finding died along with ranging. Keeping it on a |
| 97 | * separate thread means the worst a wedged UWB session can do is lose UWB. |
| 98 | * |
| 99 | * 8 KB for the same reason as the poll thread: fira_helper_open, init_session, the 23 |
| 100 | * setters and uwbmac_start all nest MAC work onto this stack, and 4 KB was half what that |
| 101 | * was measured to need. |
| 102 | */ |
| 103 | #define UWB_SESSION_THREAD_STACK_SIZE 8192 |
| 104 | #define UWB_SESSION_THREAD_PRIORITY 6 |
| 105 | |
| 106 | /* |
| 107 | * A find is about 10 s of ranging, and the phone drops the GATT link before ranging even |
| 108 | * starts (TrackerUwbGatt.writeSessionParams disconnects as soon as the write completes), |
| 109 | * so a BLE disconnect is not a teardown signal — using one would kill the session before |
| 110 | * the first round. Stop instead once the session has been silent this long: either the |
| 111 | * find finished, or it never worked. Without this the radio and a stale session stay up |
| 112 | * until the next params write or a reset. |
| 113 | */ |
| 114 | #define SESSION_IDLE_TIMEOUT_MS 30000 |
| 115 | |
| 116 | static K_THREAD_STACK_DEFINE(uwb_session_stack, UWB_SESSION_THREAD_STACK_SIZE); |
| 117 | static struct k_thread uwb_session_thread_data; |
| 118 | static K_SEM_DEFINE(session_request, 0, 1); |
| 119 | static struct ff_uwb_params requested_params; |
| 120 | static uint8_t requested_secret[FF_SECRET_LEN]; |
| 121 | |
| 122 | static void uwb_session_thread(void *a, void *b, void *c); |
| 123 | |
| 124 | /* |
| 125 | * Board calibration hook, from the vendor's DWM3001CDK support (built by |
| 126 | * firmware/qorvo-uwb). It loads this module's factory calibration — antenna delays, |
| 127 | * crystal trim, TX power — out of the DW3110's OTP. l1_config_init() returns |
| 128 | * QERR_ENOTSUP without it. |
| 129 | */ |
| 130 | extern struct l1_config_platform_ops l1_config_platform_ops; |
| 131 | |
| 132 | /* |
| 133 | * Bring-up diagnostic from firmware/qorvo-uwb: reads DEV_ID straight over Zephyr's SPI, |
| 134 | * bypassing the Qorvo driver. A DW3110 answers 0xDECA0302. |
| 135 | */ |
| 136 | uint32_t ff_qorvo_read_dev_id(void); |
| 137 | |
| 138 | /* |
| 139 | * Bring-up instrumentation for the DW3110 interrupt (firmware/qorvo-uwb). llhw arms the |
| 140 | * interrupt inside llhw_init(), before any session exists. If the line is already asserted by |
| 141 | * the time it is armed, the rising-edge trigger can never fire and the MAC is never told a |
| 142 | * frame arrived - which is indistinguishable, in a capture, from a radio that heard nothing. |
| 143 | * Reported at three fixed points rather than periodically, so it cannot flood the capture. |
| 144 | */ |
| 145 | int ff_qorvo_irq_pin_level(void); |
| 146 | void ff_qorvo_gpio_irq_stats(uint32_t *events, uint32_t *arm_calls); |
| 147 | void ff_qorvo_spi_isr_stats(uint32_t *total, uint32_t *in_isr); |
| 148 | void ff_qorvo_timer_stats(uint32_t *starts, uint32_t *stops, uint32_t *expiries, |
| 149 | uint32_t *last_us); |
| 150 | |
| 151 | static void log_irq_diag(const char *when) |
| 152 | { |
| 153 | uint32_t events = 0; |
| 154 | uint32_t arm_calls = 0; |
| 155 | uint32_t xfers = 0; |
| 156 | uint32_t xfers_isr = 0; |
| 157 | uint32_t t_starts = 0; |
| 158 | uint32_t t_stops = 0; |
| 159 | uint32_t t_expiries = 0; |
| 160 | uint32_t t_last_us = 0; |
| 161 | |
| 162 | ff_qorvo_gpio_irq_stats(&events, &arm_calls); |
| 163 | ff_qorvo_spi_isr_stats(&xfers, &xfers_isr); |
| 164 | ff_qorvo_timer_stats(&t_starts, &t_stops, &t_expiries, &t_last_us); |
| 165 | LOG_INF("IRQ diag (%s): line level=%d, armed %u time(s), ISR entries=%u, " |
| 166 | "spi xfers=%u (%u from ISR)", when, ff_qorvo_irq_pin_level(), arm_calls, |
| 167 | events, xfers, xfers_isr); |
| 168 | LOG_INF("IRQ diag (%s): timer starts=%u stops=%u expiries=%u last_us=%u", when, |
| 169 | t_starts, t_stops, t_expiries, t_last_us); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Decoders for the two enums that distinguish the failures this bring-up has to tell |
| 174 | * apart: "never heard anything" (RX_TIMEOUT) from "heard it, STS wrong" |
| 175 | * (RX_PHY_STS_FAILED) from "the region rejected a parameter" (the ERROR_INVALID_* reason |
| 176 | * codes). Switches rather than a table so the compiler ties each name to its enumerator. |
| 177 | * Only the codes a static-STS unicast controlee can produce are listed; anything else |
| 178 | * still prints as a number. |
| 179 | */ |
| 180 | static const char *ranging_status_name(uint8_t status) |
| 181 | { |
| 182 | switch (status) { |
| 183 | case QUWBS_FBS_STATUS_RANGING_SUCCESS: |
| 184 | return "SUCCESS"; |
| 185 | case QUWBS_FBS_STATUS_RANGING_TX_FAILED: |
| 186 | return "TX_FAILED"; |
| 187 | case QUWBS_FBS_STATUS_RANGING_RX_TIMEOUT: |
| 188 | return "RX_TIMEOUT"; |
| 189 | case QUWBS_FBS_STATUS_RANGING_RX_PHY_DEC_FAILED: |
| 190 | return "RX_PHY_DEC_FAILED"; |
| 191 | case QUWBS_FBS_STATUS_RANGING_RX_PHY_TOA_FAILED: |
| 192 | return "RX_PHY_TOA_FAILED"; |
| 193 | case QUWBS_FBS_STATUS_RANGING_RX_PHY_STS_FAILED: |
| 194 | return "RX_PHY_STS_FAILED"; |
| 195 | case QUWBS_FBS_STATUS_RANGING_RX_MAC_DEC_FAILED: |
| 196 | return "RX_MAC_DEC_FAILED"; |
| 197 | case QUWBS_FBS_STATUS_RANGING_RX_MAC_IE_DEC_FAILED: |
| 198 | return "RX_MAC_IE_DEC_FAILED"; |
| 199 | case QUWBS_FBS_STATUS_RANGING_RX_MAC_IE_MISSING: |
| 200 | return "RX_MAC_IE_MISSING"; |
| 201 | case QUWBS_FBS_STATUS_RANGING_INTERNAL_ERROR: |
| 202 | return "INTERNAL_ERROR"; |
| 203 | default: |
| 204 | return "?"; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static const char *session_state_name(enum quwbs_fbs_session_state state) |
| 209 | { |
| 210 | switch (state) { |
| 211 | case QUWBS_FBS_SESSION_STATE_INIT: |
| 212 | return "INIT"; |
| 213 | case QUWBS_FBS_SESSION_STATE_DEINIT: |
| 214 | return "DEINIT"; |
| 215 | case QUWBS_FBS_SESSION_STATE_ACTIVE: |
| 216 | return "ACTIVE"; |
| 217 | case QUWBS_FBS_SESSION_STATE_IDLE: |
| 218 | return "IDLE"; |
| 219 | default: |
| 220 | return "?"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static const char *reason_code_name(enum quwbs_fbs_reason_code code) |
| 225 | { |
| 226 | switch (code) { |
| 227 | case QUWBS_FBS_REASON_CODE_SUCCESS: |
| 228 | /* Also SESSION_MANAGEMENT_COMMANDS, which shares the value 0. */ |
| 229 | return "SUCCESS/STATE_CHANGE_REQUESTED"; |
| 230 | case QUWBS_FBS_REASON_CODE_MAX_RANGING_ROUND_RETRY_COUNT_REACHED: |
| 231 | return "MAX_RANGING_ROUND_RETRY_COUNT_REACHED"; |
| 232 | case QUWBS_FBS_REASON_CODE_MAX_NUMBER_OF_MEASUREMENTS_REACHED: |
| 233 | return "MAX_NUMBER_OF_MEASUREMENTS_REACHED"; |
| 234 | case QUWBS_FBS_REASON_CODE_ERROR_SLOT_LENGTH_NOT_SUPPORTED: |
| 235 | return "ERROR_SLOT_LENGTH_NOT_SUPPORTED"; |
| 236 | case QUWBS_FBS_REASON_CODE_ERROR_INSUFFICIENT_SLOTS_PER_RR: |
| 237 | return "ERROR_INSUFFICIENT_SLOTS_PER_RR"; |
| 238 | case QUWBS_FBS_REASON_CODE_ERROR_MAC_ADDRESS_MODE_NOT_SUPPORTED: |
| 239 | return "ERROR_MAC_ADDRESS_MODE_NOT_SUPPORTED"; |
| 240 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_RANGING_DURATION: |
| 241 | return "ERROR_INVALID_RANGING_DURATION"; |
| 242 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_STS_CONFIG: |
| 243 | return "ERROR_INVALID_STS_CONFIG"; |
| 244 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_RFRAME_CONFIG: |
| 245 | return "ERROR_INVALID_RFRAME_CONFIG"; |
| 246 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_PREAMBLE_CODE_INDEX: |
| 247 | return "ERROR_INVALID_PREAMBLE_CODE_INDEX"; |
| 248 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_SFD_ID: |
| 249 | return "ERROR_INVALID_SFD_ID"; |
| 250 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_PHR_DATA_RATE: |
| 251 | return "ERROR_INVALID_PHR_DATA_RATE"; |
| 252 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_PREAMBLE_DURATION: |
| 253 | return "ERROR_INVALID_PREAMBLE_DURATION"; |
| 254 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_STS_LENGTH: |
| 255 | return "ERROR_INVALID_STS_LENGTH"; |
| 256 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_NUM_OF_STS_SEGMENTS: |
| 257 | return "ERROR_INVALID_NUM_OF_STS_SEGMENTS"; |
| 258 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_DST_ADDRESS_LIST: |
| 259 | return "ERROR_INVALID_DST_ADDRESS_LIST"; |
| 260 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_RESULT_REPORT_CONFIG: |
| 261 | return "ERROR_INVALID_RESULT_REPORT_CONFIG"; |
| 262 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_RANGING_ROUND_CONTROL_CONFIG: |
| 263 | return "ERROR_INVALID_RANGING_ROUND_CONTROL_CONFIG"; |
| 264 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_RANGING_ROUND_USAGE: |
| 265 | return "ERROR_INVALID_RANGING_ROUND_USAGE"; |
| 266 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_MULTI_NODE_MODE: |
| 267 | return "ERROR_INVALID_MULTI_NODE_MODE"; |
| 268 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_SCHEDULE_MODE: |
| 269 | return "ERROR_INVALID_SCHEDULE_MODE"; |
| 270 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_PRF_MODE: |
| 271 | return "ERROR_INVALID_PRF_MODE"; |
| 272 | case QUWBS_FBS_REASON_CODE_ERROR_INVALID_DEVICE_ROLE: |
| 273 | return "ERROR_INVALID_DEVICE_ROLE"; |
| 274 | case QUWBS_FBS_REASON_CODE_ERROR_UWB_INITIATION_TIME_EXPIRED: |
| 275 | return "ERROR_UWB_INITIATION_TIME_EXPIRED"; |
| 276 | case QUWBS_FBS_REASON_CODE_ERROR_DRIVER_DOWN: |
| 277 | return "ERROR_DRIVER_DOWN"; |
| 278 | case QUWBS_FBS_REASON_CODE_ERROR_NOMEM: |
| 279 | return "ERROR_NOMEM"; |
| 280 | default: |
| 281 | return "?"; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static void on_fira_notification(enum fira_helper_cb_type cb_type, const void *content, |
| 286 | void *user_data) |
| 287 | { |
| 288 | ARG_UNUSED(user_data); |
| 289 | |
| 290 | switch (cb_type) { |
| 291 | case FIRA_HELPER_CB_TYPE_TWR_RANGE_NTF: { |
| 292 | const struct fira_twr_ranging_results *res = content; |
| 293 | |
| 294 | for (int i = 0; i < res->n_measurements; i++) { |
| 295 | const struct fira_twr_measurements *m = &res->measurements[i]; |
| 296 | |
| 297 | /* |
| 298 | * Only a measurement that worked counts as the session being |
| 299 | * alive (see SESSION_IDLE_TIMEOUT_MS). A responder that is |
| 300 | * hearing nothing may still report a round per block with |
| 301 | * RX_TIMEOUT, and treating that as activity would keep a session |
| 302 | * that has never worked up forever. |
| 303 | */ |
| 304 | if (m->status == QUWBS_FBS_STATUS_RANGING_SUCCESS) { |
| 305 | atomic_set(&last_activity_ms, |
| 306 | (atomic_val_t)k_uptime_get_32()); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * The phone computes the arrow from its own multi-antenna array; |
| 311 | * the tracker is single-port and has no AoA to report. Logging the |
| 312 | * distance is purely so the bench can see the session converge. |
| 313 | */ |
| 314 | LOG_INF("range: peer=%04x status=%u (%s) distance=%dcm rssi=%u", |
| 315 | m->short_addr, m->status, ranging_status_name(m->status), |
| 316 | m->distance_cm, m->rssi); |
| 317 | } |
| 318 | break; |
| 319 | } |
| 320 | case FIRA_HELPER_CB_TYPE_SESSION_STATUS_NTF: { |
| 321 | const struct fbs_helper_session_status_ntf *ntf = content; |
| 322 | |
| 323 | /* Not periodic, so it cannot hold the idle timeout off indefinitely. */ |
| 324 | atomic_set(&last_activity_ms, (atomic_val_t)k_uptime_get_32()); |
| 325 | LOG_INF("session %u state=%d (%s) reason=%d (%s)", ntf->session_handle, |
| 326 | ntf->state, session_state_name(ntf->state), ntf->reason_code, |
| 327 | reason_code_name(ntf->reason_code)); |
| 328 | break; |
| 329 | } |
| 330 | default: |
| 331 | LOG_DBG("fira notification type %d", cb_type); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static void uwb_poll_thread(void *a, void *b, void *c) |
| 337 | { |
| 338 | enum qerr last_r = QERR_SUCCESS; |
| 339 | bool logged = false; |
| 340 | |
| 341 | ARG_UNUSED(a); |
| 342 | ARG_UNUSED(b); |
| 343 | ARG_UNUSED(c); |
| 344 | |
| 345 | while (atomic_get(&poll_running)) { |
| 346 | /* Drives the MAC event loop; notifications are dispatched from here. */ |
| 347 | enum qerr r = uwbmac_poll_events(uwbmac_ctx, 100000); |
| 348 | |
| 349 | if (!logged || r != last_r) { |
| 350 | /* |
| 351 | * uwbmac.h:331-336 documents a non-zero timeout as blocking, but |
| 352 | * :328-329 also says the call only exists when uwbmac_init() was |
| 353 | * passed a NULL event_loop_ops — a parameter this delivery's |
| 354 | * uwbmac_init() does not have. So the return value is the only way |
| 355 | * to know whether the pump works here at all; an immediate error |
| 356 | * means nothing is dispatching notifications. |
| 357 | */ |
| 358 | LOG_INF("uwbmac_poll_events: %d", r); |
| 359 | last_r = r; |
| 360 | logged = true; |
| 361 | } |
| 362 | /* |
| 363 | * Yield unconditionally, in case the call turns out not to block. This is |
| 364 | * defence, not the mechanism: session-start liveness comes from this |
| 365 | * thread running *below* the session thread (see UWB_THREAD_PRIORITY). |
| 366 | * 1 ms against a 120 ms ranging block costs nothing, and the tick here is |
| 367 | * 32768 Hz so it really is ~1 ms. |
| 368 | */ |
| 369 | k_sleep(K_MSEC(1)); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Started immediately after uwbmac_start(), because fira_helper_start_session() is a |
| 375 | * request/response over the MAC and needs its events dispatched to complete. |
| 376 | */ |
| 377 | static void start_polling(void) |
| 378 | { |
| 379 | if (poll_thread_created) { |
| 380 | /* |
| 381 | * A previous stop_polling() could not join it. If it has exited since, |
| 382 | * replace it; otherwise re-arm the one that is there, because creating a |
| 383 | * second thread over a live k_thread would corrupt the scheduler. A failed |
| 384 | * join only says the thread has not terminated — if it is wedged inside |
| 385 | * uwbmac_poll_events() this session has no working pump, hence the warning. |
| 386 | */ |
| 387 | if (k_thread_join(&uwb_thread, K_NO_WAIT) != 0) { |
| 388 | LOG_WRN("reusing a poll thread that would not exit; events may not " |
| 389 | "be dispatched"); |
| 390 | atomic_set(&poll_running, 1); |
| 391 | return; |
| 392 | } |
| 393 | poll_thread_created = false; |
| 394 | } |
| 395 | atomic_set(&poll_running, 1); |
| 396 | k_thread_create(&uwb_thread, uwb_thread_stack, UWB_THREAD_STACK_SIZE, |
| 397 | uwb_poll_thread, NULL, NULL, NULL, UWB_THREAD_PRIORITY, 0, |
| 398 | K_NO_WAIT); |
| 399 | k_thread_name_set(&uwb_thread, "ff_uwb"); |
| 400 | poll_thread_created = true; |
| 401 | } |
| 402 | |
| 403 | static void stop_polling(void) |
| 404 | { |
| 405 | if (!poll_thread_created) { |
| 406 | return; |
| 407 | } |
| 408 | atomic_set(&poll_running, 0); |
| 409 | /* |
| 410 | * The loop can be inside a poll with a 100 ms timeout, so give it room. If it |
| 411 | * somehow does not come back, leave the thread object alone and let |
| 412 | * start_polling() sort it out; the alternative is creating a second thread over a |
| 413 | * live k_thread. |
| 414 | */ |
| 415 | if (k_thread_join(&uwb_thread, K_MSEC(500)) != 0) { |
| 416 | LOG_ERR("poll thread did not exit within 500 ms"); |
| 417 | return; |
| 418 | } |
| 419 | poll_thread_created = false; |
| 420 | } |
| 421 | |
| 422 | int ff_uwb_init(void) |
| 423 | { |
| 424 | enum qerr r; |
| 425 | |
| 426 | if (stack_ready) { |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Order matters and is not obvious. qplatform_init() brings up SPI/GPIO and probes |
| 432 | * the DW3110, which has to happen first: l1_config_init()'s reset_to_default hook |
| 433 | * reads this module's calibration out of the transceiver's OTP, and without a |
| 434 | * probed driver that dereferences an unset interface pointer and bus-faults. |
| 435 | */ |
| 436 | r = qplatform_init(); |
| 437 | if (r != QERR_SUCCESS) { |
| 438 | LOG_ERR("qplatform_init: %d", r); |
| 439 | /* |
| 440 | * Read the id straight off the bus to tell a wiring or SPI-mode problem |
| 441 | * apart from the driver rejecting what it read, which qplatform_init's |
| 442 | * return code alone cannot. Only on the failure path: qplatform_init resets |
| 443 | * and wakes the transceiver, so before it runs the part is still asleep and |
| 444 | * this reads zero even when everything is fine. |
| 445 | */ |
| 446 | LOG_ERR("DEV_ID read directly: 0x%08x (expect 0xdeca0302)", |
| 447 | ff_qorvo_read_dev_id()); |
| 448 | return -EIO; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Must precede l1_config_init, which stores its defaults back to this storage. |
| 453 | * As shipped it points into the image, where the store would target flash. |
| 454 | */ |
| 455 | ff_qorvo_l1_config_use_ram_storage(); |
| 456 | |
| 457 | r = l1_config_init(&l1_config_platform_ops); |
| 458 | if (r != QERR_SUCCESS) { |
| 459 | LOG_ERR("l1_config_init: %d", r); |
| 460 | qplatform_deinit(); |
| 461 | return -EIO; |
| 462 | } |
| 463 | r = llhw_init(); |
| 464 | if (r != QERR_SUCCESS) { |
| 465 | LOG_ERR("llhw_init: %d", r); |
| 466 | l1_config_deinit(); |
| 467 | qplatform_deinit(); |
| 468 | return -EIO; |
| 469 | } |
| 470 | r = uwbmac_init(&uwbmac_ctx); |
| 471 | if (r != QERR_SUCCESS) { |
| 472 | LOG_ERR("uwbmac_init: %d", r); |
| 473 | llhw_deinit(); |
| 474 | l1_config_deinit(); |
| 475 | qplatform_deinit(); |
| 476 | return -EIO; |
| 477 | } |
| 478 | |
| 479 | stack_ready = true; |
| 480 | LOG_INF("Qorvo uwbstack up; DW3110 identified"); |
| 481 | log_irq_diag("after llhw_init"); |
| 482 | |
| 483 | k_thread_create(&uwb_session_thread_data, uwb_session_stack, |
| 484 | UWB_SESSION_THREAD_STACK_SIZE, uwb_session_thread, NULL, NULL, NULL, |
| 485 | UWB_SESSION_THREAD_PRIORITY, 0, K_NO_WAIT); |
| 486 | k_thread_name_set(&uwb_session_thread_data, "ff_uwb_sess"); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Push a filled session_parameters into the session. The stack has no bulk setter — |
| 492 | * only a bulk *getter* — so each field goes in individually, the same way the vendor |
| 493 | * SDK's fira_set_session_parameters does it. |
| 494 | */ |
| 495 | #define SET_PARAM(field) \ |
| 496 | do { \ |
| 497 | enum qerr _r = fira_helper_set_session_##field(&fira_ctx, session_handle, \ |
| 498 | sp->field); \ |
| 499 | if (_r != QERR_SUCCESS) { \ |
| 500 | LOG_ERR("set " #field " failed: %d", _r); \ |
| 501 | return -EIO; \ |
| 502 | } \ |
| 503 | } while (0) |
| 504 | |
| 505 | static int apply_session_params(const struct session_parameters *sp) |
| 506 | { |
| 507 | enum qerr r; |
| 508 | |
| 509 | SET_PARAM(channel_number); |
| 510 | SET_PARAM(preamble_code_index); |
| 511 | SET_PARAM(sfd_id); |
| 512 | SET_PARAM(phr_data_rate); |
| 513 | SET_PARAM(prf_mode); |
| 514 | SET_PARAM(device_type); |
| 515 | SET_PARAM(device_role); |
| 516 | SET_PARAM(multi_node_mode); |
| 517 | SET_PARAM(rframe_config); |
| 518 | SET_PARAM(slot_duration_rstu); |
| 519 | SET_PARAM(block_duration_ms); |
| 520 | SET_PARAM(round_duration_slots); |
| 521 | SET_PARAM(ranging_round_usage); |
| 522 | SET_PARAM(round_hopping); |
| 523 | SET_PARAM(block_stride_length); |
| 524 | SET_PARAM(schedule_mode); |
| 525 | SET_PARAM(result_report_config); |
| 526 | SET_PARAM(ranging_round_control); |
| 527 | SET_PARAM(report_rssi); |
| 528 | |
| 529 | r = fira_helper_set_session_vupper64(&fira_ctx, session_handle, |
| 530 | (uint8_t *)sp->vupper64); |
| 531 | if (r != QERR_SUCCESS) { |
| 532 | LOG_ERR("set vupper64 failed: %d", r); |
| 533 | return -EIO; |
| 534 | } |
| 535 | r = fira_helper_set_session_short_address(&fira_ctx, session_handle, sp->short_addr); |
| 536 | if (r != QERR_SUCCESS) { |
| 537 | LOG_ERR("set short_address failed: %d", r); |
| 538 | return -EIO; |
| 539 | } |
| 540 | r = fira_helper_set_session_destination_short_addresses( |
| 541 | &fira_ctx, session_handle, sp->n_destination_short_address, |
| 542 | (uint16_t *)sp->destination_short_address); |
| 543 | if (r != QERR_SUCCESS) { |
| 544 | LOG_ERR("set destination_short_addresses failed: %d", r); |
| 545 | return -EIO; |
| 546 | } |
| 547 | r = fira_helper_set_session_measurement_sequence(&fira_ctx, session_handle, |
| 548 | &sp->meas_seq); |
| 549 | if (r != QERR_SUCCESS) { |
| 550 | LOG_ERR("set measurement_sequence failed: %d", r); |
| 551 | return -EIO; |
| 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | #undef SET_PARAM |
| 557 | |
| 558 | int ff_uwb_start(const struct ff_uwb_params *params, const uint8_t *secret) |
| 559 | { |
| 560 | /* |
| 561 | * Zero-init is load-bearing. apply_session_params() never pushes sts_config, |
| 562 | * preamble_duration, psdu_data_rate, sts_length, number_of_sts_segments or |
| 563 | * enable_diagnostics, so the FiRa region's own defaults govern those and the |
| 564 | * struct values are only a starting point. Adding setters for them would change |
| 565 | * behaviour silently: fira_helper_set_session_sts_config(.., 0x01) selects Dynamic |
| 566 | * STS rather than Static (fira_helper.h:2036-2050), and sp.preamble_duration == 0 |
| 567 | * is FIRA_PREAMBLE_DURATION_32, not 64. |
| 568 | */ |
| 569 | struct session_parameters sp = { 0 }; |
| 570 | struct fbs_session_init_rsp rsp = { 0 }; |
| 571 | uint8_t vupper64[FIRA_VUPPER64_SIZE]; |
| 572 | uint8_t own_address[2]; |
| 573 | uint16_t own_short_addr; |
| 574 | uint16_t peer_short_addr; |
| 575 | enum qerr r; |
| 576 | int rc; |
| 577 | |
| 578 | if (!stack_ready) { |
| 579 | return -EAGAIN; |
| 580 | } |
| 581 | if (session_active) { |
| 582 | ff_uwb_stop(); |
| 583 | } |
| 584 | |
| 585 | /* Both are derived from the bind-time secret, never taken from the wire. */ |
| 586 | rc = ff_sts_key(secret, params->session_id, vupper64); |
| 587 | if (rc) { |
| 588 | return rc; |
| 589 | } |
| 590 | rc = ff_uwb_address(secret, own_address); |
| 591 | if (rc) { |
| 592 | return rc; |
| 593 | } |
| 594 | /* |
| 595 | * Little-endian, i.e. byte 0 is the low byte. That is the 802.15.4 convention and |
| 596 | * what the vendor's own BLE-provisioned accessory does with an out-of-band address: |
| 597 | * AR2U16(x) = ((x[1] << 8) | x[0]) (QANI fira_niq.c:43,218-220). The phone hands the |
| 598 | * same two bytes to UwbAddress.fromBytes() without documenting an order, so the |
| 599 | * summary log below prints both readings to settle it against a RangingSession log. |
| 600 | */ |
| 601 | own_short_addr = sys_get_le16(own_address); |
| 602 | peer_short_addr = sys_get_le16(params->peer_address); |
| 603 | |
| 604 | r = uwbmac_set_short_addr(uwbmac_ctx, own_short_addr); |
| 605 | if (r != QERR_SUCCESS) { |
| 606 | LOG_ERR("uwbmac_set_short_addr: %d", r); |
| 607 | return -EIO; |
| 608 | } |
| 609 | /* |
| 610 | * Passing true *disables* hardware address filtering (uwbmac.h:580-590; the comment |
| 611 | * at fira_niq.c:125 says the opposite of what its own argument does). Without it the |
| 612 | * DW3110 drops the phone's polls before the MAC sees them if the two ends disagree |
| 613 | * about the address byte order, and there is no error on either side to say so. |
| 614 | * QANI, the only vendor BLE-provisioned responder, does the same. |
| 615 | */ |
| 616 | r = uwbmac_set_promiscuous_mode(uwbmac_ctx, true); |
| 617 | if (r != QERR_SUCCESS) { |
| 618 | LOG_ERR("uwbmac_set_promiscuous_mode: %d", r); |
| 619 | return -EIO; |
| 620 | } |
| 621 | |
| 622 | /* Per-step markers from here on: any of these vendor calls can be the one that does |
| 623 | * not return, and which one it is only shows in what the trace stops after. */ |
| 624 | LOG_INF("fira_helper_open"); |
| 625 | r = fira_helper_open(&fira_ctx, uwbmac_ctx, on_fira_notification, "endless", 0, NULL); |
| 626 | if (r != QERR_SUCCESS) { |
| 627 | LOG_ERR("fira_helper_open: %d", r); |
| 628 | return -EIO; |
| 629 | } |
| 630 | /* Must happen while the MAC is stopped. */ |
| 631 | LOG_INF("fira_helper_set_scheduler"); |
| 632 | r = fira_helper_set_scheduler(&fira_ctx); |
| 633 | if (r != QERR_SUCCESS) { |
| 634 | LOG_ERR("fira_helper_set_scheduler: %d", r); |
| 635 | goto close; |
| 636 | } |
| 637 | |
| 638 | LOG_INF("fira_helper_init_session"); |
| 639 | r = fira_helper_init_session(&fira_ctx, params->session_id, |
| 640 | QUWBS_FBS_SESSION_TYPE_RANGING_NO_IN_BAND_DATA, &rsp); |
| 641 | if (r != QERR_SUCCESS || rsp.status_code != QUWBS_FBS_STATUS_OK) { |
| 642 | /* The qerr return and the FBS status are separate answers; a session that |
| 643 | * was refused reports it in rsp.status_code only. */ |
| 644 | LOG_ERR("fira_helper_init_session: %d (status %d)", r, rsp.status_code); |
| 645 | goto close; |
| 646 | } |
| 647 | session_handle = rsp.session_handle; |
| 648 | |
| 649 | /* Roles: the phone controls and initiates, we respond. */ |
| 650 | sp.device_type = QUWBS_FBS_DEVICE_TYPE_CONTROLEE; |
| 651 | sp.device_role = QUWBS_FBS_DEVICE_ROLE_RESPONDER; |
| 652 | |
| 653 | /* From the phone, per find. */ |
| 654 | sp.channel_number = params->channel; |
| 655 | sp.preamble_code_index = params->preamble_index; |
| 656 | sp.short_addr = own_short_addr; |
| 657 | sp.n_destination_short_address = 1; |
| 658 | sp.destination_short_address[0] = peer_short_addr; |
| 659 | /* |
| 660 | * The derived 8 bytes are [2B VendorID][6B STATIC_STS_IV], the order Android hands |
| 661 | * to setSessionKeyInfo. The Qorvo struct holds the opposite layout — a union over |
| 662 | * { static_sts_iv[6]; vendor_id[2]; } (fira_helper.h:382-397) — and its own NI |
| 663 | * accessory fills the array as a full reversal of VendorID||StaticStsIv |
| 664 | * (fira_niq.c:228-235). Follow that: a straight memcpy puts VendorID into |
| 665 | * static_sts_iv[0..1] and the STS silently never matches (tracker_sts.c). |
| 666 | */ |
| 667 | for (size_t i = 0; i < FIRA_VUPPER64_SIZE; i++) { |
| 668 | sp.vupper64[i] = vupper64[FIRA_VUPPER64_SIZE - 1 - i]; |
| 669 | } |
| 670 | |
| 671 | /* One initiator, one responder, deferred DS-TWR: the FiRa shape of Android's |
| 672 | * CONFIG_UNICAST_DS_TWR. */ |
| 673 | sp.multi_node_mode = FIRA_MULTI_NODE_MODE_UNICAST; |
| 674 | sp.ranging_round_usage = FIRA_RANGING_ROUND_USAGE_DSTWR_DEFERRED; |
| 675 | sp.schedule_mode = FIRA_SCHEDULE_MODE_TIME_SCHEDULED; |
| 676 | sp.rframe_config = FIRA_RFRAME_CONFIG_SP3; |
| 677 | sp.sfd_id = FIRA_SFD_ID_2; |
| 678 | sp.prf_mode = FIRA_PRF_MODE_BPRF; |
| 679 | sp.phr_data_rate = FIRA_PHR_DATA_RATE_850K; |
| 680 | sp.slot_duration_rstu = SLOT_DURATION_RSTU; |
| 681 | sp.block_duration_ms = BLOCK_DURATION_MS; |
| 682 | sp.round_duration_slots = ROUND_DURATION_SLOTS; |
| 683 | sp.round_hopping = ROUND_HOPPING; |
| 684 | sp.block_stride_length = 0; |
| 685 | sp.report_rssi = 1; |
| 686 | |
| 687 | /* Report time-of-flight; no AoA, the DW3110 has a single RF port. */ |
| 688 | sp.result_report_config = fira_helper_bool_to_result_report_config(true, false, false, false); |
| 689 | sp.ranging_round_control = fira_helper_bool_to_ranging_round_control(true, false); |
| 690 | |
| 691 | /* Single range-only measurement step, all antenna sets left to the driver. */ |
| 692 | sp.meas_seq.n_steps = 1; |
| 693 | sp.meas_seq.steps[0].type = FIRA_MEASUREMENT_TYPE_RANGE; |
| 694 | sp.meas_seq.steps[0].n_measurements = 1; |
| 695 | sp.meas_seq.steps[0].rx_ant_set_nonranging = 0xff; |
| 696 | sp.meas_seq.steps[0].rx_ant_sets_ranging[0] = 0xff; |
| 697 | sp.meas_seq.steps[0].rx_ant_sets_ranging[1] = 0xff; |
| 698 | sp.meas_seq.steps[0].tx_ant_set_nonranging = 0xff; |
| 699 | sp.meas_seq.steps[0].tx_ant_set_ranging = 0xff; |
| 700 | |
| 701 | LOG_INF("applying session parameters"); |
| 702 | rc = apply_session_params(&sp); |
| 703 | if (rc) { |
| 704 | goto deinit; |
| 705 | } |
| 706 | |
| 707 | LOG_INF("uwbmac_start"); |
| 708 | r = uwbmac_start(uwbmac_ctx); |
| 709 | if (r != QERR_SUCCESS) { |
| 710 | LOG_ERR("uwbmac_start: %d", r); |
| 711 | goto deinit; |
| 712 | } |
| 713 | start_polling(); |
| 714 | |
| 715 | LOG_INF("starting FiRa session"); |
| 716 | r = fira_helper_start_session(&fira_ctx, session_handle); |
| 717 | if (r != QERR_SUCCESS) { |
| 718 | LOG_ERR("fira_helper_start_session: %d", r); |
| 719 | /* Same order as ff_uwb_stop(): deinit while the pump is up, then take the |
| 720 | * pump down while the MAC is still running. */ |
| 721 | (void)fira_helper_deinit_session(&fira_ctx, session_handle); |
| 722 | stop_polling(); |
| 723 | (void)uwbmac_stop(uwbmac_ctx); |
| 724 | goto close; |
| 725 | } |
| 726 | LOG_INF("FiRa session started"); |
| 727 | log_irq_diag("session started"); |
| 728 | |
| 729 | session_active = true; |
| 730 | atomic_set(&last_activity_ms, (atomic_val_t)k_uptime_get_32()); |
| 731 | LOG_INF("FiRa responder started: session=%08x ch=%u preamble=%u own=%04x peer=%04x " |
| 732 | "(peer bytes %02x%02x, big-endian reading would be %04x)", |
| 733 | params->session_id, params->channel, params->preamble_index, own_short_addr, |
| 734 | peer_short_addr, params->peer_address[0], params->peer_address[1], |
| 735 | sys_get_be16(params->peer_address)); |
| 736 | return 0; |
| 737 | |
| 738 | deinit: |
| 739 | /* Only reached before uwbmac_start(), so there is no pump to shut down here. */ |
| 740 | (void)fira_helper_deinit_session(&fira_ctx, session_handle); |
| 741 | close: |
| 742 | fira_helper_close(&fira_ctx); |
| 743 | return -EIO; |
| 744 | } |
| 745 | |
| 746 | void ff_uwb_stop(void) |
| 747 | { |
| 748 | if (!session_active) { |
| 749 | return; |
| 750 | } |
| 751 | /* Before teardown, so this reports the state at the end of the ACTIVE window. */ |
| 752 | log_irq_diag("session teardown"); |
| 753 | (void)fira_helper_stop_session(&fira_ctx, session_handle); |
| 754 | (void)fira_helper_deinit_session(&fira_ctx, session_handle); |
| 755 | /* |
| 756 | * Between those two and uwbmac_stop(): they are request/response over the MAC and |
| 757 | * need the pump, while nothing in the vendor API says what uwbmac_poll_events() |
| 758 | * does on a *stopped* MAC. Keeping the pump's lifetime inside |
| 759 | * [uwbmac_start, uwbmac_stop) means never finding out. |
| 760 | */ |
| 761 | stop_polling(); |
| 762 | (void)uwbmac_stop(uwbmac_ctx); |
| 763 | fira_helper_close(&fira_ctx); |
| 764 | session_active = false; |
| 765 | LOG_INF("FiRa session stopped"); |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Runs session setup off the main loop; see the note by uwb_session_stack. One request at a |
| 770 | * time is enough — the phone writes params once per find, and a second write while a session |
| 771 | * is starting is better dropped than queued behind a possibly-wedged one. |
| 772 | * |
| 773 | * Teardown lives here too, so that every fira_helper_* call in this file is made from this |
| 774 | * one thread and needs no locking. |
| 775 | */ |
| 776 | static void uwb_session_thread(void *a, void *b, void *c) |
| 777 | { |
| 778 | ARG_UNUSED(a); |
| 779 | ARG_UNUSED(b); |
| 780 | ARG_UNUSED(c); |
| 781 | |
| 782 | while (true) { |
| 783 | /* Nothing to do until the phone writes params — unless a session is up, |
| 784 | * in which case wake up often enough to notice it going quiet. */ |
| 785 | k_timeout_t wait = session_active ? K_MSEC(1000) : K_FOREVER; |
| 786 | |
| 787 | if (k_sem_take(&session_request, wait) == 0) { |
| 788 | LOG_INF("session request picked up: starting FiRa setup"); |
| 789 | (void)ff_uwb_start(&requested_params, requested_secret); |
| 790 | continue; |
| 791 | } |
| 792 | if (session_active && |
| 793 | (k_uptime_get_32() - (uint32_t)atomic_get(&last_activity_ms)) > |
| 794 | (uint32_t)SESSION_IDLE_TIMEOUT_MS) { |
| 795 | LOG_WRN("no UWB activity for %d ms: stopping the session", |
| 796 | SESSION_IDLE_TIMEOUT_MS); |
| 797 | ff_uwb_stop(); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | int ff_uwb_on_params(const uint8_t *data, size_t len, const uint8_t *secret) |
| 803 | { |
| 804 | if (len != FF_UWB_PARAMS_LEN) { |
| 805 | return -EINVAL; |
| 806 | } |
| 807 | if (!stack_ready) { |
| 808 | return -EAGAIN; |
| 809 | } |
| 810 | /* |
| 811 | * Reject what the stack would reject anyway, so an out-of-range value says so here |
| 812 | * instead of surfacing as an unexplained session-start failure. Only channels 5 and |
| 813 | * 9 exist in this stack, and BPRF preamble indices are 9..12 |
| 814 | * (FIRA_PCODE_BPRF_MIN/MAX, whose header notes the wider 9..24 range elsewhere in |
| 815 | * the SDK does not apply here). |
| 816 | */ |
| 817 | if (data[6] != 5U && data[6] != 9U) { |
| 818 | LOG_WRN("UWB params rejected: channel %u (expected 5 or 9)", data[6]); |
| 819 | return -EINVAL; |
| 820 | } |
| 821 | if (data[7] < FIRA_PCODE_BPRF_MIN || data[7] > FIRA_PCODE_BPRF_MAX) { |
| 822 | LOG_WRN("UWB params rejected: preamble index %u (expected %d..%d)", data[7], |
| 823 | FIRA_PCODE_BPRF_MIN, FIRA_PCODE_BPRF_MAX); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | |
| 827 | /* TrackerUwbGatt.encodeSessionParams: [2B addr][4B sessionId BE][1B ch][1B pre] */ |
| 828 | requested_params.peer_address[0] = data[0]; |
| 829 | requested_params.peer_address[1] = data[1]; |
| 830 | requested_params.session_id = sys_get_be32(&data[2]); |
| 831 | requested_params.channel = data[6]; |
| 832 | requested_params.preamble_index = data[7]; |
| 833 | memcpy(requested_secret, secret, sizeof(requested_secret)); |
| 834 | |
| 835 | /* Hand off and return immediately: the caller is the main loop. */ |
| 836 | LOG_INF("UWB session requested: peer=%02x%02x session=%08x ch=%u preamble=%u", |
| 837 | requested_params.peer_address[0], requested_params.peer_address[1], |
| 838 | requested_params.session_id, requested_params.channel, |
| 839 | requested_params.preamble_index); |
| 840 | k_sem_give(&session_request); |
| 841 | return 0; |
| 842 | } |