Compare commits

..

31 Commits

Author SHA1 Message Date
ruv d199279caa release(firmware): v0.7.0-esp32 major — ADR-110 firmware-side substrate closed
Marks the end of the firmware-side ADR-110 push. Everything the firmware
can deliver toward §B multistatic alignment without hardware-blocked
dependencies is shipped, measured, and witnessed:

  §A0.7–§A0.10  ESP-NOW mesh quantified: 99.43-99.56% cross-board match,
                104.1 µs smoothed offset stdev, 1.4 ppm crystal-skew
                tracking, ≤100 µs alignment target empirically met.
  §A0.12        32-byte UDP sync packet emits with mesh-aligned epoch
                + sequence high-water; verified live both boards.
  §A0.13 (new)  bit-4 wire-fix: byte 19 bit 4 sourced from
                c6_sync_espnow_is_valid() too. Mixed S3+C6 fleets now
                correctly advertise mesh-sync.

Host-side enabler (Python):
  archive/v1/src/hardware/csi_extractor.py grows SyncPacketParser +
  SyncPacket dataclass. ESP32BinaryParser docstring acknowledges the
  sibling sync packet. Sets up wifi-densepose-sensing-server to
  consume the §A0.12 stream without inventing the parser.

Build artifacts (IDF v5.4, both RC=0):
  S3 8 MB: 1094 KB, 47% partition slack
  C6 4 MB: 1019 KB, 45% partition slack

Tag v0.7.0-esp32. Branch adr-110-esp32c6. PR #764.

What remains is outside the firmware: host-side parser wiring,
multistatic CSI fusion in wifi-densepose-signal, 11ax-cooperative AP
(or future IDF AP-HE API), INA226 for ≤5 µA LP-core.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:56:58 -04:00
ruv e69572ff99 fix(csi): ADR-018 byte 19 bit 4 now signals ESP-NOW sync too (not just broken 15.4)
WITNESS-LOG-110 prior state had byte 19 bit 4 (cross-node sync valid)
only being set from c6_timesync_is_valid() — but c6_timesync is the
802.15.4 path that D1 documented as unfixable in IDF v5.4 (rx=0 across
every soak we've run). The working transport is c6_sync_espnow (§A0.7,
§A0.10: 99.43-99.56% RX cross-board, 104 µs smoothed-offset stdev),
yet frames from sync'd nodes had bit 4 cleared because the ESP-NOW
path didn't OR into the flag.

Fix: also set bit 4 when c6_sync_espnow_is_valid() — the OR semantic
means a node signals sync from whichever transport is healthy. Host
sees bit 4 set, knows to pair the frame against the most recent sync
packet (§A0.12) from this node_id.

Side effect: this also enables S3 boards to set bit 4 (c6_sync_espnow
works on both targets, c6_timesync is C6-only). So a multi-target
mesh of S3+C6 boards now correctly signals cross-node alignment
regardless of which chips are in the fleet.

Build evidence: C6 image 1019 KB (+16 bytes for the new check),
45% slack unchanged.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:47:06 -04:00
ruv 4e1b62ab4f release(firmware): v0.6.9-esp32 — sync-packet wired, CONFIG_C6_SYNC_EVERY_N_FRAMES tunable
Bundles the iter 8 + iter 9 sync-packet work (§A0.11 + §A0.12) into a
shipped release. v0.6.8 didn't carry the sync emission; v0.6.9 closes
the loop.

What ships:
- csi_collector emits a 32-byte UDP sync packet (magic 0xC511A110)
  every CONFIG_C6_SYNC_EVERY_N_FRAMES CSI callbacks (default 20).
- New Kconfig knob lets operators tune cadence from ~0.1 Hz (N=1000)
  to ~10 Hz (N=1) without rebuilding — sensible defaults for
  mainstream multistatic at ~2 s sync interval.
- Backwards-compatible at the wire level: old aggregators drop the new
  magic on existing parser mismatch path.

Build artifacts (both green on IDF v5.4):
- S3 8 MB: 1094 KB, 47% partition slack
- C6 4 MB: 1019 KB, 45% partition slack

The macro define was renamed from SYNC_EVERY_N_FRAMES to
CONFIG_C6_SYNC_EVERY_N_FRAMES so the Kconfig generator wires through.
Header guard preserves the default for builds without the kconfig
applied.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:41:19 -04:00
ruv d2effcc6f6 witness(ADR-110 §A0.12): sync-packet wired + verified live on both boards
SOTA iter 9 — closes the §A0.11 wiring gap with empirical evidence.
Added a diagnostic ESP_LOGI in the sync emit path; flashed both C6
boards; captured 45s parallel serial output.

Sync packet generation confirmed live:

COM12 (leader, ...00:84):
  sync-pkt #1 ... node=12 flags=0x03 local_us=28864932 epoch_us=28864939
  flags=0x03 = leader+valid, epoch ≈ local (7 µs delta = call-stack
  elapsed only — leader has no offset by definition)

COM9 (follower, ...05:3c):
  sync-pkt #1 ... node=9  flags=0x06 local_us=28798450 epoch_us=27634885
  flags=0x06 = valid+smoothed_used, local-epoch = 1,163,565 µs
  Matches §A0.10's measured -1.16 s mesh-aligned offset within 285 µs
  (WiFi MAC TX jitter floor between samples).

Cadence: 2.05 s between sync packets — 20 CSI frames at the bench's
observed 10 fps rate = exactly the design intent.

UDP send returns -1 (sr=-1) because the bench boards are intentionally
not associated to a real AP (provisioned to dead SSIDs for the iter
2-8 mesh experiments). No crash, no resource leak in 45s. Once boards
hit a routable network, sr becomes the byte count.

Wiring gap §A0.11 now CLOSED. Multistatic CSI fusion downstream has
a documented protocol to recover mesh-aligned timestamps for every CSI
frame: host pairs (node_id, sequence) across the two packet streams.
Host-side parser is the natural next layer (wifi-densepose-sensing-server).

Build evidence: C6 image 1019 KB (+0.5 KB for the diag log line),
45% partition slack unchanged.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:31:05 -04:00
ruv 6ff155a232 feat(csi): emit ADR-110 §A0.11 sync-packet every 20 CSI frames
Closes WITNESS-LOG-110 §A0.11 wiring gap. Adds a separate 32-byte UDP
packet (magic 0xC511A110, distinct from the CSI frame magic 0xC5110001)
carrying:

  [0..3]   magic 0xC511A110 (LE u32) — CSI-ADR-110 sync packet
  [4]      node_id
  [5]      proto version (0x01)
  [6]      flags: bit0=is_leader, bit1=is_valid, bit2=smoothed_used
  [7]      reserved
  [8..15]  local esp_timer_get_time() (LE u64)
  [16..23] mesh-aligned epoch (LE u64) = local + EMA-smoothed offset
  [24..27] high-water sequence number (LE u32) for pairing with CSI frames
  [28..31] reserved (room for leader_id low32 in a follow-up)

Emitted once per 20 CSI frames (≈ 1 Hz at the 20 Hz send-rate gate).
Same stream_sender UDP socket as CSI frames — host dispatches by first
4 bytes of each datagram.

Backwards compatible: aggregators that don't know about the new magic
ignore it (sync packets won't match the CSI parser's magic check, so
they're dropped harmlessly by existing receivers). New aggregators
pair (node_id, sequence) across the two packet streams to align CSI
frames to mesh time.

Sets us up for downstream ADR-029/030 multistatic CSI fusion: with the
host now able to recover the mesh-aligned epoch from each frame's
sequence number, frames from multiple boards can be ordered + fused
on a common timeline.

Build evidence: C6 image 1019 KB (+1 KB vs v0.6.8 no-sync), 45 %
partition slack unchanged. Host-side parser update is a follow-up.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:26:45 -04:00
ruv 503411a8d2 release(firmware): bump to v0.6.8-esp32 — ESP-NOW mesh EMA smoother
SOTA iter 7. Tags + ships the firmware that actually has the iter-5/6 EMA
path so the GitHub release matches the witness measurements. v0.6.7
binaries on the release predate the EMA work — anyone downloading from
the v0.6.7 release would not get the smoothing §A0.10 measured.

Build evidence (IDF v5.4, both RC=0):
- S3 8 MB: 1093 KB (47 % slack), SHA256 60e3ef907f...
- C6 4 MB: 1019 KB (45 % slack), SHA256 feb88d60a0...
- Soft-AP and 4 MB S3 variants ship unchanged from v0.6.7; not rebuilt.

Wiring gap documented in WITNESS §A0.11: ADR-018 wire format has no
timestamp field, so the synced clock value from get_epoch_us() doesn't
yet reach CSI frames. Three options outlined (ADR-018 v2 / separate
UDP sync packet / out-of-band HTTP probe). Likely landing place is the
separate UDP sync packet — keeps the existing ADR-018 contract intact
while ADR-029/030 multistatic fusion lights up the substrate.

CHANGELOG Wave 4 entry summarises what v0.6.8 ships + the deferred
gap so future maintainers don't lose the breadcrumb.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:20:00 -04:00
ruv e5c3b27daa witness(ADR-110 §A0.10): EMA suppression quantified — 3.95x, ≤100 µs alignment shipped
SOTA iter 6 — the long-soak iter 5 owed. 300 s parallel two-board capture
with the iter 5 EMA firmware, 46 converged follower-mode samples.

Over the 225 s steady-state window:
              stdev      range       drift Q1->Q4
  raw        411.5 µs    2245 µs    +30.1 µs/min
  smoothed   104.1 µs     478 µs    +27.8 µs/min

  suppression: 3.95x (stdev), 4.70x (range)

The ADR-110 §2.4 ≤100 µs alignment target is now empirically met by the
smoothed offset alone — no host-side filter required. Drift is preserved
(within 2 µs/min between raw and smoothed), so the EMA tracks real clock
skew, not lag behind it.

Drift sign + magnitude vary with thermal state across runs (-84 µs/min
in §A0.8 iter 4, +30 µs/min here in iter 6 with boards warmer — both
within ESP32 ±10 ppm crystal spec). The EMA tracks whichever value
applies at any given moment.

Throughput: tx=2701, rx=2689, match=2689 → 99.56% cross-board match,
zero TX failures.

ADR-110 §B sync-substrate status: ≤100 µs multistatic alignment is now
*measured and shipped*, not just designed. Downstream multistatic CSI
fusion (ADR-029/030) can treat c6_sync_espnow_get_epoch_us() as a
black-box bounded-jitter timestamp source.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:11:17 -04:00
ruv f41f5fc85b feat(c6_sync_espnow): EMA-smooth cross-board offset, expose via get_epoch_us
SOTA iter 5 — converted the iter 4 ADR-110 §A0.8 closing recommendation
("host-side Kalman / linear fit on the offset trajectory") into a
firmware-side, fixed-point EMA so every downstream consumer of
c6_sync_espnow_get_epoch_us() gets bounded-jitter timestamps for free.

Implementation:
* α = 1/8 (Q3.3 shift = 3), ≈8-sample effective window at the 10 Hz
  beacon rate. Tracks the ≈1.4 ppm crystal drift §A0.8 measured while
  averaging out per-beacon WiFi-MAC jitter spikes.
* y[n] = y[n-1] + (raw - y[n-1]) >> 3  — integer arithmetic, two cycles
  on the RISC-V LP/HP cores, no float dependency.
* Seeded from the first follower-mode sample so we don't bias toward 0.
* New getter: int64_t c6_sync_espnow_get_offset_us_smoothed(void).
* c6_sync_espnow_get_offset_us() (raw) stays for diagnostics, unchanged.
* c6_sync_espnow_get_epoch_us() now prefers the smoothed offset once
  s_smoothed_seeded — meaning every CSI frame timestamp ADR-029/030
  consumes is already filtered, no host-side rework required.

Diag log line now prints both:
  c6_espnow: tx#N ... offset_us=R smoothed=S

90 s bench verification (witness §A0.9 + iter5-COM9-ema-90s.log) shows
both values tracking. Methodology caveat in §A0.9: short windows don't
let the smoothing benefit emerge over the raw noise floor — the
suppression ratio measurement needs ≥5 min, deferred to a long-soak
iteration.

Binary size cost: ~32 bytes (one int64, one bool, one getter). C6 build
still 45% partition slack.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 12:04:22 -04:00
ruv 676297c48f witness(ADR-110 §A0.8): 4-minute mesh soak — quantified stability + measured clock skew
SOTA iter 4 (cron c40dab4a tick 4). Converted iter 2's 30-second snapshot
into a real statistical measurement over 4 minutes / 2101 beacons.

Beacon throughput (both boards):
- Rate: 10.00/s exactly — FreeRTOS timer rock-solid
- COM12 leader: tx=2101, match=2101/2101 = 100.00%, 0 TX fail
- COM9 follower: tx=2101, match=2089/2101 = 99.43%, 0 TX fail
- 12 missed beacons / 210 s ≈ 1 miss / 17.5 s — inside the 3-second
  VALID_WINDOW_MS freshness gate, sync remains valid

Sync offset (COM9, 37 follower-mode samples after warmup):
- mean: -1,163,123 µs  (boot-time delta, not jitter)
- stdev: 540 µs
- range: 2994 µs over the soak
- drift Q1->Q4: -84.2 µs/min over 3 minutes
  = 1.4 ppm relative clock skew between the two specific C6 crystals
  (ESP32 spec: typical ±10 ppm — well within tolerance)

ADR-110 §2.4 target ±100 µs across one hop: met with margin at the
current 10 Hz beacon rate. A simple linear or Kalman fit on the offset
trajectory (host-side, no firmware change) would compress per-frame
alignment error to <50 µs. Hardware substrate is now quantified and
documented — downstream ADR-029/030 multistatic fusion can plan around
the measured numbers.

Also corrected §A0.7's "±10 µs jitter" wording — that was sample-to-sample
range within a 5-row snapshot, not the true stability profile. §A0.8
supersedes with the proper soak data.

Raw captures: dist/firmware-v0.6.7/iter4-{COM9,COM12}-soak240s.log
(7400+ lines each, full c6_espnow + c6_ts counter records).

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:55:48 -04:00
ruv d636604330 docs(user-guide): point 4MB-flash flow at the v0.6.7 S3 4MB binary
SOTA loop iter 3 added esp32-csi-node-s3-4mb.bin to the v0.6.7-esp32 release
(882 KB binary built from sdkconfig.defaults.4mb, 52% partition slack on
4MB single-OTA — vs 47% for the 8MB build, +5pp). v0.6.6 shipped 8MB+4MB
parity; v0.6.7 now matches.

User-guide previously pointed SuperMini 4MB owners at v0.4.3 (which
predates ADR-110 / fall-threshold fix / 4102-tx ESP-NOW soak). Point at
v0.6.7 directly so 4MB users get the same firmware as 8MB users.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:48:36 -04:00
ruv 572e09ad86 witness(ADR-110 §A0.7): ESP-NOW cross-board mesh — leader election + sync offset measured
SOTA iter 2 (cron c40dab4a tick 2). The §D-workaround that v0.6.6 left
on TX-only soak coverage is now empirically complete end-to-end.

Parallel 60 s capture with COM9 (206ef117053c) + COM12 (206ef1170084)
both on default v0.6.7, no WiFi associations needed:

* RX rate cross-board:
  - COM12: tx=301 rx=297 match=297 (98.7 %)
  - COM9:  tx=301 rx=300 match=300 (99.7 %)
  - 0 TX failures on either side over 30 s of beacons

* Leader election fired for the first time in ADR-110:
  +27336 ms COM9: "stepping down: heard lower-id leader 206ef1170084
  (we are 206ef117053c)" — the lowest-EUI-wins protocol the original
  c6_timesync was designed to run, now actually working because the
  transport is healthy.

* Cross-board sync offset converged and stable:
  COM9 offset_us: -1462 -> -950 -> -954 -> -957 -> -948
  ±10 µs jitter once leader-following stabilises, hitting the ±100 µs
  target named in ADR-110 §2.4.

802.15.4 c6_ts path stayed rx=0 across both 60 s captures — D1 still
broken in IDF v5.4, exactly as documented. ESP-NOW is confirmed as the
working multistatic time alignment transport.

Raw captures: dist/firmware-v0.6.7/iter2-{COM9,COM12}-espnow.log.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:40:56 -04:00
ruv f9aad75413 witness+opt: ADR-110 §A0.6 — IDF v5.4 soft-AP HE gap, swarm warnings
Iter 1 finding from /loop 5m SOTA sprint: two C6 boards now mesh through
the c6_softap_he soft-AP (COM12 hosts ruview-c6-twt, COM9 associates), but
COM9 lands at phymode(0x3, 11bgn), he:0 — the soft-AP doesn't advertise
HE. Confirmed by full grep of components/esp_wifi/include/esp_wifi*.h:
the public API exposes ONLY STA-side iTWT/bTWT. There is no
esp_wifi_ap_set_he_config, no wifi_he_ap_config_t, no wifi_config_t.ap.he_*
field — soft-AP HE/TWT-Responder advertise is not user-controllable on
ESP32-C6 in IDF v5.4.

Consequence: B1/B2 cannot be measured via the two-C6 path on this IDF
release. The c6_softap_he module ships as the in-place hook for any
future IDF release that exposes the API; until then a real 11ax router
or phone hotspot remains the path. Sharpens the open question from "do
we need an 11ax AP?" to "we need either a future IDF AP-side HE config
API, or an external 11ax AP".

WITNESS-LOG-110 §A0.6 records the parallel boot logs from both boards
plus the IDF surface grep evidence.

c6_softap_he.c gains an ESP_LOGW at AP-up time so operators understand
exactly why STAs land at 11bgn (avoids confusion with the v0.6.6 §A8
graceful-TWT-NACK story).

While here: cleared the three -Wunused-variable warnings in swarm_bridge.c
that fired on every build (fw_ver, free_heap, presence in heartbeat block).
fw_ver now feeds an ESP_LOGI so the boot log names the build; free_heap +
heartbeat-presence were dead anyway. Pure ultra-opt: smaller .o files, zero
warning noise.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:36:09 -04:00
ruv 83f20f7c61 witness(ADR-110): v0.6.7 live silicon evidence — A0.4 + A0.5
Flashed v0.6.7 to two ESP32-C6 boards (COM9 + COM12, both matching the
witness-log MACs from v0.6.6 session).

A0.4 — regression check on COM9 (default config):
- v0.6.7 boots in 446 ms, c6_ts up on ch 26, HAL_MAC_ESP32AX_761 loaded,
  ruv.net association at +5206 ms, iTWT graceful NACK, ESP-NOW init OK,
  CSI flowing at HT-LTF 64 subcarriers. Byte-for-byte same behavior as
  v0.6.6 confirms the new code paths (LP-core + soft-AP) are correctly
  default-off — zero behavioral regression for shipped fleets.

A0.5 — soft-AP module live on COM12:
- Built a CONFIG_C6_SOFTAP_HE_ENABLE=y variant locally, flashed COM12.
- AP came up at +666 ms on channel 6 with WPA2-PSK, dual STA+AP iface
  visible (...00:84 STA / ...00:85 AP — standard +1 MAC offset).
- Discovered live IDF constraint: when AP+STA both active and STA
  associates to an 11ax AP on a different bandwidth, the soft-AP gets
  demoted from HE to 11n by the radio scheduler. Documented in §A0.5 —
  the cleanest two-board iTWT bench needs the AP-role board's STA iface
  not to associate elsewhere (point it at a non-existent SSID).

Release v0.6.7-esp32 now also carries:
- esp32-csi-node-c6-4mb-softap.bin (the AP-variant binary)
- COM9-v0.6.7-regression.log + COM12-v0.6.7-softap.log raw captures
- SHA256SUMS.txt updated with the soft-AP variant hash

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:28:59 -04:00
ruv 756bfc0a1a docs(readme,user-guide): record v0.6.7 LP-core + soft-AP HE/TWT additions
- README C6 hardware row now links the v0.6.7-esp32 release and notes the
  LP-core RISC-V program (B4 code path) + soft-AP TWT Responder (B1/B2
  two-board unblock).
- README Option-2b quick-start mentions the new opt-in toggles.
- User-guide gets the v0.6.7 boot banner, expanded battery-seed instructions
  (real LP-core poll period + debounce knobs), and a fresh "Two-board iTWT
  bench" section covering the soft-AP role (CONFIG_C6_SOFTAP_HE_ENABLE) and
  the NVS overrides for SSID / PSK / channel.
- User-guide firmware release table prepends v0.6.7-esp32 as Latest above
  v0.5.0 (still recommended for S3-mesh production).

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:16:08 -04:00
ruv 948768bdda feat(firmware): v0.6.7-esp32 — real LP-core program + C6 soft-AP HE/TWT helper
ADR-110 P9 — software-only unblocks for the WITNESS-LOG-110 §B
hardware-blocked items. Two new modules, both default-off so v0.6.6 fleets
see no behavior change.

LP-core (B4 path):
- New firmware/esp32-csi-node/main/lp_core/main.c: real RISC-V LP-core
  motion-gate program with debounce + monotonic motion_count counter.
- c6_lp_core.c rewritten to load + run the LP binary via ulp_lp_core_run
  when CONFIG_C6_LP_CORE_ENABLE=y; falls back to the v0.6.6 ext1 GPIO-wake
  path otherwise (keeps regression surface small).
- ulp_embed_binary() wired in main/CMakeLists.txt, gated on the Kconfig.
- New Kconfig knobs: C6_LP_POLL_PERIOD_US (default 10 ms),
  C6_LP_DEBOUNCE_SAMPLES (default 3).
- Exposes c6_lp_core_motion_count() / c6_lp_core_poll_count() for the
  witness harness — once an INA/Joulescope is on the bench, B4 is one
  capture away from a measured number.

Soft-AP HE (B1/B2 unblock):
- New c6_softap_he.{h,c}: brings up the C6 in AP+STA mode with WPA2-PSK
  + HE advertisement, so a second C6 in STA mode can negotiate real
  iTWT against a known-cooperative AP without buying an 11ax router.
- main.c calls c6_softap_he_start() right before esp_wifi_start() when
  CONFIG_C6_SOFTAP_HE_ENABLE=y.
- New Kconfig knobs: C6_SOFTAP_HE_{SSID,PSK,CHANNEL} with NVS overrides
  via softap_ssid / softap_psk / softap_chan in the ruview namespace.

Build artifacts (IDF v5.4, both green, RC=0):
- S3 8 MB: 1093 KB (47% partition slack)
- C6 4 MB: 1019 KB (45% partition slack)
- SHA-256 sums in dist/firmware-v0.6.7/SHA256SUMS.txt

Doc updates: CHANGELOG wave-3 entry, ADR-110 phase table gets P5
upgrade note + new P9 row, WITNESS-LOG-110 gets new A0 section
recording the v0.6.7 build evidence.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 11:10:34 -04:00
ruv 561647b3af docs(readme): link new ADR-110 reviewer guide + update soak total
Two tiny updates to the ESP32-C6 row in the hardware-options table:
- Add link to docs/ADR-110-REVIEW-GUIDE.md (the new one-page reviewer
  on-ramp added in 3133be6d4)
- Update ESP-NOW soak number from '1151 tx 0 fail' (just the 120s run)
  to '4102 tx 0 fail cumulative across 120 s + 300 s soaks' — reflects
  the additional 300 s soak landed in 9a46fc8aa

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-23 00:05:25 -04:00
ruv 3133be6d48 docs(adr-110): add reviewer one-page guide
The witness log is comprehensive but ~300 lines. A reviewer landing on
this branch wants a five-minute tour: where to read first, what's
actually empirically verified vs hardware-blocked, what the bugs were,
and the commit history at a glance.

docs/ADR-110-REVIEW-GUIDE.md provides that, with explicit links to the
canonical witness + ADR. Doesn't duplicate content — points to where
the canonical record lives.

Also captures the security note for the operator (rotate the previously-
exposed Docker Hub + PI-cluster tokens — they appeared in local logs
during witness generation before scripts/redact-secrets.py was added).

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:53:22 -04:00
ruv 9a46fc8aa2 witness: ESP-NOW 300 s soak — 2951 tx 0 fail (2.5x sample)
Confirmation run vs the earlier 120 s soak. Same firmware, same board,
longer window:

  Captured 67307 bytes over 300 s
  ESP-NOW samples: 60
    first: tx=1    fail=0 rx=0 match=0 leader=1 offset=0
    last:  tx=2951 fail=0 rx=0 match=0 leader=1 offset=0
    TX rate: 9.83/s (target 10/s)
    TX failure rate: 0.0000%
  app_main calls (reset detector): 1  -> no crash

2.5x sample size, identical zero-failure rate, marginally higher
sustained rate (9.83 vs 9.60) — FreeRTOS timer settling. Adds a second
data point to WITNESS-LOG-110 §D-workaround.

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:43:46 -04:00
ruv e255b7d43a docs(firmware): README acknowledges dual S3+C6 target (ADR-110)
After ADR-110 made this the same source tree for both esp32s3
(production) and esp32c6 (research / Wi-Fi-6 / 802.15.4 / LP-core seed
nodes), the firmware README header should reflect that. Title,
one-liner, and target badge updated; body sections still use S3
examples as the production default. The C6 build path is documented
in docs/user-guide.md + sdkconfig.defaults.esp32c6 + Quick-Start
Option 2b in the top-level README.

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:18:16 -04:00
ruv 553b07d04c docs(readme): tighten ESP32-C6 row to match empirical witness (ADR-110)
Original row said C6 *has* HE-LTF tagging + multi-node sync + 5µA
hibernation as if they were active features. Reality per
WITNESS-LOG-110:

- Wire format VERIFIED (17 unit tests across firmware/Rust/Python)
- ESP-NOW transport VERIFIED on 1 board (1151 tx, 0 fail in 120s soak)
- TWT graceful NACK VERIFIED live (AP isn't 11ax → INVALID_ARG handled)
- HE-LTF live capture: BLOCKED on 11ax AP availability
- 5µA hibernation: datasheet number, not a measurement (no INA)
- 802.15.4 RX: known broken in IDF v5.4, ESP-NOW is the workaround

New row leads with 'wire format ready' + 'hardware-gated' to set
honest expectations, and links to docs/WITNESS-LOG-110.md so readers
can see the full empirical/claimed split themselves.

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:13:01 -04:00
ruv 9de34ba096 docs(adr): index ADR-110 in Hardware and firmware section
The ADR index README hadn't been updated past ADR-099. Adding ADR-110
in the Hardware/firmware section with its honest status — firmware
shipped + tested + CI-green, but the four SOTA capability claims
(HE-LTF live capture, TWT cadence, cross-node sync, 5 µA hibernation)
are each blocked on different physical hardware (11ax AP, more boards,
INA meter), as fully documented in docs/WITNESS-LOG-110.md.

Ref: ruvnet/RuView#762 / draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:08:50 -04:00
ruv fc75a8a5c8 test(fuzz): extend csi_serialize fuzz harness for ADR-110 byte 18-19
The libFuzzer harness was compiled without CONFIG_CSI_FRAME_HE_TAGGING,
so the new byte 18/19 path in csi_collector.c was zero-filled at compile
time and never fuzzed. Three changes to fix that:

1. test/stubs/esp_stubs.h: wifi_pkt_rx_ctrl_t gains both branch families
   - HE branch (CONFIG_SOC_WIFI_HE_SUPPORT path): cur_bb_format, second
   - Legacy branch (S3 / pre-HE chips): sig_mode, cwb, stbc
   A single stub compiles for either branch; the Makefile picks which
   one is active via -D flags. Both sets are declared so a build for
   the unselected branch still compiles cleanly.

2. test/Makefile: CFLAGS now defines CONFIG_CSI_FRAME_HE_TAGGING=1 so
   the new code path is reachable. CONFIG_SOC_WIFI_HE_SUPPORT stays
   UNSET (default — exercises the legacy S3 branch). Add it to CFLAGS
   for a parallel HE-stub run if you want coverage of the C6 branch.

3. test/fuzz_csi_serialize.c: parses 3 more control bytes from fuzz
   input (he_inputs[2] + legacy_inputs) and writes them through
   info.rx_ctrl.{cur_bb_format,second,sig_mode,cwb,stbc} so the
   serializer's PpduType switch and Adr018Flags computation are
   reached on every iteration.

Result: the existing libFuzzer corpus + ASAN/UBSAN now covers the
ADR-110 wire encoding paths on every run. No more zero-fill silent skip.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 23:00:09 -04:00
ruv 89972c0917 docs(changelog): expand ADR-110 entry with wave 2-5 additions
The original CHANGELOG entry covered the initial firmware ship. Adding
sub-bullets for everything that landed after:

- D1 workaround: ESP-NOW cross-node sync (TX 0% failure rate over 1151
  transmits in 120 s soak), 802.15.4 path documented as broken
- Host-side dual-pipeline decoder for ADR-018 byte 18-19 (Rust 122/122,
  Python 11/11 — protocol path verified end-to-end without 11ax hardware)
- Security fix for witness bundle secret leakage via Pydantic error
  dumps (redact-secrets.py filter)

Witness link: docs/WITNESS-LOG-110.md

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 22:54:19 -04:00
ruv b808a6380b witness: ESP-NOW 120s soak — 1151 tx 0 fail, 9.6/s, no crash
Real empirical evidence the ESP-NOW sync transport is long-term stable
on the C6 (D-workaround). Single-board capture on COM9, latest firmware
on branch (8eaa92cf2):

  Captured 33586 bytes over 120 s
  ESP-NOW samples: 24
    first: tx=1    fail=0 rx=0 match=0 leader=1 offset=0
    last:  tx=1151 fail=0 rx=0 match=0 leader=1 offset=0
    TX rate: 9.6/s (target ~10/s)
    TX failure rate: 0.00%
  app_main calls (reset detector): 1  -> no crash

The 9.6/s vs 10/s gap is FreeRTOS timer schedulability slop at 100 ms
ticks, not a transport issue. Zero TX failures over 1151 attempts +
zero resets in 2 min = the ESP-NOW path is production-grade as a
transport. Only the cross-board RX measurement is blocked on the other
boards' USB enumeration.

Ref: ruvnet/RuView#762 / draft PR #764 / D-workaround

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 22:51:25 -04:00
ruv 8eaa92cf21 feat(python): host-side decode for ADR-018 byte 18-19 (ADR-110)
Python ESP32BinaryParser was using struct format '<IBBHIIBB2x' — the
'2x' skipped bytes 18-19 as reserved. After the Rust-side decoder was
extended to surface PPDU type + flags, the Python pipeline (which
archive/v1 still uses for testing + the proof verifier) needs the same
update so its consumers see the HE metadata too.

csi_extractor.py:
- HEADER_FMT now '<IBBHIIBBBB' (captures bytes 18-19)
- New metadata fields: ppdu_type ('ht_legacy'|'he_su'|'he_mu'|'he_tb'|'unknown'),
  ppdu_type_raw, he_capable, bw40, stbc, ldpc, ieee802154_sync_valid,
  adr018_flags_raw
- Class constants PPDU_HT_LEGACY..PPDU_UNKNOWN mirror the firmware

test_esp32_binary_parser.py:
- build_binary_frame() takes optional ppdu_byte + flags_byte (default 0)
- New TestAdr110ByteEncoding class with 5 tests:
  - Pre-ADR-110 zeros decode as 'ht_legacy' + all-flags-false
  - HE-SU / HE-MU / HE-TB decode correctly
  - 0xFF decodes as 'unknown'
  - All-flags-set round-trip (0x1D)

11/11 parser tests pass (6 existing + 5 new). Backwards compat verified.

Pairs with the Rust-side decoder in commit 3959fabf3. Both pipelines now
read the same wire format produced by the C6 firmware's
CONFIG_CSI_FRAME_HE_TAGGING path.

Ref: ruvnet/RuView#762, draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 22:46:37 -04:00
ruv 3959fabf31 feat(rust): host-side decode for ADR-018 byte 18-19 (ADR-110 closure)
Parse the C6 firmware's HE PPDU type + bandwidth/flags from ADR-018
bytes 18-19 (previously discarded as _reserved). Adds two types to
CsiMetadata: ppdu_type (HtLegacy/HeSu/HeMu/HeTb/Unknown) and
adr018_flags (bw40/stbc/ldpc/ieee802154_sync_valid).

Pre-ADR-110 firmware sends zeros which round-trip as HtLegacy +
default flags — fully backwards compatible.

6 new deterministic unit tests:
- Pre-ADR-110 backwards compat
- HE-SU / HE-MU / HE-TB decode
- Unknown PPDU byte -> Unknown
- All-bits-set flags round-trip
- PpduType byte round-trip

Result: 122 wifi-densepose-hardware tests pass, 0 fail. Host decoder
now matches the firmware encoder bit-for-bit — HE-LTF metadata path
works end-to-end the moment an 11ax AP is in range.

Ref: ruvnet/RuView#762

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 22:42:49 -04:00
ruv 88be283ab0 feat(c6): ESP-NOW cross-node sync — D1 workaround for broken 15.4 RX
After 5 systematic experiments confirmed the 802.15.4 RX path is
unfixable from user code in this IDF v5.4 + C6 combination (D1), add a
parallel sync transport over ESP-NOW. Same TS_BEACON protocol, same
public API (c6_sync_espnow_get_epoch_us / is_valid / is_leader), but
runs on the WiFi MAC layer that ESP-IDF fully supports across every
ESP32 family.

The 802.15.4 code stays in source for when the IDF driver is fixed.
ESP-NOW is the working primary today.

Empirical (single-board COM9 — other 3 boards dropped off USB during
the experiment):
- c6_sync_espnow_init() succeeds: "init done local_id=… leader=
  yes(candidate) period=100ms"
- TX path 100% reliable: tx#101 fail=0 over ~15s at 100ms cadence
- RX awaiting cross-board test once USB-enumeration is restored

Trade vs. 802.15.4 design:
- Loses: "frees WiFi airtime for CSI" property
- Gains: known-working RX path, cross-target (S3 and C6 both)
- Same API surface — consumers swap transports without code change

Files:
- main/c6_sync_espnow.{h,c} — new module, ~210 lines
- main/CMakeLists.txt        — add to SRCS (always built, used on any target)
- main/main.c                — init after WiFi STA up, skip on QEMU mock
- test/capture-3board-experiment.py — surface c6_espnow log lines
- docs/WITNESS-LOG-110.md    — new §D-workaround documenting the pivot

Ref: ruvnet/RuView#762 / D1 known-issue / draft PR #764

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 22:37:12 -04:00
ruv f8a2e36958 fix(witness): redact secrets from bundled verify.py output (SECURITY)
The Python proof verifier (archive/v1/data/proof/verify.py) imports the
project settings, which read the user's .env file. When pydantic
validation fails (e.g., extra fields not in the Settings schema), the
error dump includes the offending input_value — which means real
Docker tokens, GitHub PATs, API keys, etc. were being echoed to stdout
and captured into the bundled verification-output.log.

Confirmed on this branch's first bundle generation: dckr_pat_,
tok_... cluster token, and other long opaque strings leaked into
witness-bundle-ADR028-<commit>/proof/verification-output.log inside
the .tar.gz. Bundle + tarball nuked from disk before any push.

Added:
- scripts/redact-secrets.py — stdin->stdout filter with patterns for
  common token prefixes (dckr_pat_, tok_, sk-, ghp_, gho_, github_pat_,
  AKIA, hf_, xoxb-, xoxp-, Bearer), `field=secret` assignments, long
  opaque alphanumeric strings (40+ chars), and long hex runs (20+ chars
  which catch token suffixes after `...` truncation).
- generate-witness-bundle.sh now pipes verify.py stderr through that
  filter before tee-ing into the bundled log.
- Also fixed pre-existing stale `v1/` paths in the witness script
  (correct path is `archive/v1/`).

The user must rotate the leaked credentials regardless (the bundle was
never pushed, but they appeared in this local Claude session log).

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 21:04:57 -04:00
ruv 4c39e28bd0 fix(c6): PAN-ID match in 15.4 beacon + expanded D1 diagnostic record
Tried 4th hypothesis for the RX-path bug: maybe the IDF v5.4 receiver
strictly requires dst PAN to match the local set_panid() instead of
honoring the 0xFFFF broadcast PAN per 802.15.4 spec. Changed beacon
dst PAN to 0xCAFE (matching set_panid call) to test.

Result: still negative (tx#241 rx#0/1, magic_match=0). PAN was not the
root cause — but the change is technically more correct per the IDF
behavior and is kept.

Also expanded WITNESS-LOG-110 §D1 to record the 4-experiment matrix
that's now been run:
  1. WiFi-on + ch15: tx#381 rx#1 magic_match=0
  2. WiFi-on + ch26: identical negative
  3. WiFi-off + ch26 + OT off + promiscuous true: tx#601 rx#0 — even
     the earlier rx#1 was a noise frame, not protocol traffic
  4. Dst PAN 0xCAFE: still negative

Hypothesis space narrowed; needs IDF maintainer trace or working
multi-board reference to fix.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 20:46:03 -04:00
ruv 66523843e6 fix(c6): TWT INVALID_ARG graceful + ch26 + diagnostic counters (ADR-110 D1)
After 3 systematic hypotheses tested + rejected (radio coex, OpenThread
shadowing, manual RX re-arm), the 802.15.4 leader-election bug is
narrowed to: TX path works perfectly (~10/s clean, 0 fail), but the RX
path stops after exactly 1 frame. Manual esp_ieee802154_receive() from
either callback bootloops the driver (verified across all 3 boards).

The IDF reference example uses the same handle_done-only pattern as
this code, implying the driver should auto-restart RX — but empirically
doesn't here. Either a half-duplex radio state issue or an IDF v5.4
bug. Tracked as known issue D1 in WITNESS-LOG-110.

Changes shipped:
- c6_twt.c: ESP_ERR_INVALID_ARG added to graceful-fallback list
  (empirically: ruv.net AP advertises TWT Responder=0, IDF driver
  validates against AP HE capability and rejects with INVALID_ARG)
- c6_timesync.c: diagnostic counters (s_tx_count, s_tx_fail, s_rx_count,
  s_rx_magic_match) + per-10-beacon log line preserved so future
  investigation has the diagnostic harness ready
- sdkconfig.defaults.esp32c6: 15.4 channel default 15 → 26 (non-overlap
  with WiFi 2.4 GHz channels), OpenThread disabled (we use raw 15.4)
- promiscuous=true on the radio (broadcast frames addressed to 0xFFFF)
- WITNESS-LOG-110 §D1 expanded with the full diagnostic trace +
  3-hypothesis investigation record

Cross-node sync claim (B3) BLOCKED until either an IDF maintainer
trace or a working multi-board reference is available. The other
three SOTA dimensions (HE-LTF, TWT cadence, 5 µA hibernation) are
also still unverified and need different hardware (11ax AP, INA meter)
— honestly recorded in §B.

Tracking: ruvnet/RuView#762, task #30 closed as known-issue.

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 20:39:50 -04:00
ruv f23e34ee5c feat(firmware): ESP32-C6 target — Wi-Fi 6 / 802.15.4 / TWT / LP-core (ADR-110)
`firmware/esp32-csi-node` now builds for both `esp32s3` (existing
production) and `esp32c6` (new research / battery-seed target) from
the same source tree. ESP-IDF auto-applies `sdkconfig.defaults.esp32c6`
when the target is set to esp32c6; every C6 module is gated on
CONFIG_IDF_TARGET_ESP32C6 (or the SOC_WIFI_HE_SUPPORT capability) so
the S3 build path is byte-identical to today.

New modules (all #ifdef-gated, no-op stubs on S3):
- c6_twt.{h,c}      — iTWT wrapper, graceful AP-NACK fallback
- c6_timesync.{h,c} — 802.15.4 beacon-based mesh time-sync, EUI-64
                      leader election, c6_timesync_get_epoch_us()
- c6_lp_core.{h,c}  — wake-on-motion deep-sleep helper (ext1 path
                      this cut; real LP-core polling deferred)

ADR-018 frame extension:
- byte 18: PPDU type (0=HT/legacy, 1=HE-SU, 2=HE-MU, 3=HE-TB)
- byte 19: bandwidth + STBC + 802.15.4-sync-valid flags
- Magic 0xC5110001 unchanged — backwards compatible
- Dual-branch encoding handles both struct variants of
  wifi_pkt_rx_ctrl_t (legacy S3 / HE C6) per CONFIG_SOC_WIFI_HE_SUPPORT

Critical bug fixed during live witness collection (verified across 3
boards on COM6/COM9/COM12):
- c6_timesync.c read MAC into a 6-byte buffer and ran MAC-48->EUI-64
  conversion. But esp_read_mac(ESP_MAC_IEEE802154) returns 8 bytes
  already in EUI-64 form on C6 — code was double-inserting FFFE.
  Boot log was 206ef1fffefffe17, fix yields 206ef1fffe17278c which
  matches esptool's eFuse reading exactly.

Tooling:
- CI workflow (firmware-ci.yml) extended with c6-4mb matrix row +
  ADR-110 host-unit-test step
- Host unit tests for pure functions (mac48_to_eui64,
  eui64_bytes_to_u64, PPDU encoding both branches) — runs on Ubuntu CI
- Multi-board live-capture harness (test/capture-3board-experiment.py)
- Witness bundle script records SHA-256s for s3-adr110, c6-adr110, and
  s3-fair-adr110 (apples-to-apples) binary archives

Honest empirical findings (full report in docs/WITNESS-LOG-110.md):
- Verified live on 3 C6 boards: boot, 802.15.4 init w/ correct EUIs,
  WiFi STA reaching assoc->run on ruv.net, TWT setup attempted +
  gracefully NACKed (AP is 11n-only, TWT Responder:0), HE-MAC firmware
  loaded
- NOT verified (need 11ax AP / second-channel exp / INA meter):
  HE-LTF subcarrier expansion, TWT cadence determinism, ±100 µs sync
  alignment, 5 µA hibernation
- Bug found: leader election doesn't step down under live WiFi load —
  likely 2.4 GHz radio coex preemption (WiFi ch 5 vs 15.4 ch 15);
  follow-up task #30
- Apples-to-apples size: S3-no-display = 886 KB, C6 = 1003 KB
  (C6 is 13% LARGER for equivalent CSI features; the extra is the
  802.15.4 + OpenThread stack that S3 lacks)

Tracking: ruvnet/RuView#762

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-05-22 20:10:30 -04:00
264 changed files with 5888 additions and 16377 deletions
-1
View File
@@ -78,7 +78,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Security fix** (`scripts/redact-secrets.py` + `generate-witness-bundle.sh`): the Python proof step was echoing `.env` contents into the bundled `verification-output.log` via Pydantic validation errors. Bundle nuked before push; added a `stdin -> stdout` redaction filter covering common token prefixes, long opaque strings, and long hex runs. Verified zero leaks on rebuild.
- **Wave 3 — firmware v0.6.7 (LP-core full + soft-AP HE)**: two software-only unblocks for the hardware-blocked items in WITNESS-LOG-110 §B. (1) **Real LP-core motion-gate program** (`firmware/esp32-csi-node/main/lp_core/main.c` + integration in `c6_lp_core.c`). When `CONFIG_C6_LP_CORE_ENABLE=y`, the LP RISC-V coprocessor now runs a real polling program (configurable cadence via `CONFIG_C6_LP_POLL_PERIOD_US`, default 10 ms) that debounces N consecutive GPIO samples (`CONFIG_C6_LP_DEBOUNCE_SAMPLES`, default 3) and wakes the HP core via `ulp_lp_core_wakeup_main_processor()`. HP entry uses `esp_sleep_enable_ulp_wakeup` + `ESP_SLEEP_WAKEUP_ULP`. Exposes `c6_lp_core_motion_count()` and `c6_lp_core_poll_count()` getters for the witness harness. **Replaces** the v0.6.6 `esp_deep_sleep_enable_gpio_wakeup` ext1 fallback (which floored at ~10 µA, the same as the S3 ULP-FSM). The fallback path stays as the `else` branch so builds without `CONFIG_C6_LP_CORE_ENABLE` keep working unchanged — zero regression for v0.6.6-era fleets. Targets the C6 datasheet ≤5 µA average for battery seed nodes; pending INA/Joulescope measurement to confirm (`WITNESS-LOG-110 §B4`). (2) **Wi-Fi 6 soft-AP with TWT Responder=1** (`c6_softap_he.{h,c}` + `main.c` AP+STA mode switch). When `CONFIG_C6_SOFTAP_HE_ENABLE=y`, one C6 board can act as the iTWT-capable AP the bench is otherwise missing — pair with a second C6-STA board to negotiate real iTWT against a known-cooperative AP and measure deterministic CSI cadence (`WITNESS-LOG-110 §B1/B2`). SSID/PSK/channel configurable via Kconfig defaults or NVS (`softap_ssid`/`softap_psk`/`softap_chan` keys in the `ruview` namespace). Default off so existing nodes are unaffected. **Build artifacts**: S3 8 MB binary 1093 KB (47 % slack), C6 4 MB binary 1019 KB (45 % slack). Tag: `v0.6.7-esp32`.
- **Wave 4 — firmware v0.6.8 (ESP-NOW mesh offset smoother)**: `c6_sync_espnow.c` now maintains an in-firmware exponential-moving-average of the cross-board sync offset (α = 1/8, fixed-point shift, ≈ 8-sample window at the 10 Hz beacon rate). New getter `c6_sync_espnow_get_offset_us_smoothed()`. `c6_sync_espnow_get_epoch_us()` now returns timestamps stamped from the smoothed offset once seeded — every downstream CSI-frame consumer gets bounded-jitter alignment for free, no host-side filter required. **Measured on the bench**: 5-min two-board soak (WITNESS-LOG-110 §A0.10) drops raw offset stdev 411.5 µs → smoothed 104.1 µs (**3.95× suppression** on stdev, 4.70× on peak-to-peak range) while preserving the +30 µs/min crystal-drift trajectory within 2 µs/min. **The ADR-110 §2.4 ≤100 µs multistatic alignment target that v0.6.6 designed is now empirically measured, not just stated.** Cross-board beacon match rate 99.56% over 5 min, 0 TX failures. Binary cost: +32 bytes (one int64, one bool, one getter). Diag log adds `smoothed=…` field. Tag: `v0.6.8-esp32`. **Known wiring gap (deferred)**: `csi_serialize_frame` does not yet stamp frames with `c6_sync_espnow_get_epoch_us()` — the ADR-018 frame format has no timestamp field, and adding one is a breaking change that needs an ADR update. Multistatic CSI fusion will require either an ADR-018 v2 with timestamp, or a separate UDP sync packet keyed off the existing flag bit. Tracked in WITNESS-LOG-110 §A0.11.
- **Wave 5 — firmware v0.6.9 + v0.7.0 + host wiring (loop iter 8 → iter 26)**: closes the §A0.11 gap and lights up the substrate end-to-end across firmware → host → JSON broadcast. **Firmware**: (a) **v0.6.9-esp32**`csi_collector.c` emits a 32-byte UDP sync packet (magic `0xC511A110`, distinct from CSI frame magic `0xC5110001`) every `CONFIG_C6_SYNC_EVERY_N_FRAMES` (default 20) CSI frames, carrying `node_id`, `local_us`, mesh-aligned `epoch_us` (from the Wave 4 smoothed offset), and the CSI sequence high-water for host-side pairing. Same UDP socket as CSI; host dispatches by leading magic. Operator-tunable cadence via the new Kconfig knob — N=1 (10 Hz) for tight multistatic, N=200 (~20 s) for low-power seeds. Live-verified on COM9+COM12 (§A0.12): follower reports `local epoch = 1 163 565 µs`, matches the §A0.10 boot-delta measurement within 285 µs of WiFi MAC TX jitter. (b) **v0.7.0-esp32**`csi_collector.c:221` ADR-018 byte 19 bit 4 ("cross-node sync valid") now ORs in `c6_sync_espnow_is_valid()` so frames from sync'd ESP-NOW nodes correctly advertise sync (previously only sourced from the broken 802.15.4 path — false-negative bug, §A0.13). Side effect: S3 boards now also set the bit since `c6_sync_espnow` is cross-target. **Host decoders + 25 unit tests**: Python `SyncPacketParser` + `SyncPacket` dataclass with `apply_to_local` / `mesh_aligned_us_for_sequence` / `local_minus_epoch_us` (10 tests in `TestSyncPacketParser`); Rust `wifi_densepose_hardware::SyncPacket` + `SyncPacketFlags` + `SYNC_PACKET_MAGIC` re-exported from the crate root with identical API surface (15 tests in `sync_packet::tests`). **Cross-language conformance gate** (loop iter 21): the same 32-byte canonical hex `10a111c509010600f26db70100000000c5aca501000000001400000000000000` is pinned in both test suites; if either decoder drifts from the wire, exactly one named test fires and points at the moved side. **Sensing-server wiring**: `udp_receiver_task` magic-dispatches `0xC511A110` and stores per-node `latest_sync: Option<SyncPacket>` + `latest_sync_at: Option<Instant>` on `NodeState`. New helpers: `NodeState::mesh_aligned_us(local_us)`, `NodeState::mesh_aligned_us_for_csi_frame(sequence)` (uses the per-node measured fps EMA with 5-sample warmup + 9 s staleness gate), `NodeState::observe_csi_frame_arrival(now)` (feeds `update_csi_fps_ema` α=1/8, called once per accepted CSI frame). 4 fps-EMA tests + 3 NodeSyncSnapshot serialization tests on the binary target. **Public JSON API**: `sensing_update` broadcasts now carry an optional `sync` object per node — `{offset_us, is_leader, is_valid, smoothed, sequence, csi_fps_ema, csi_fps_samples}``#[serde(skip_serializing_if = "Option::is_none")]` so non-mesh paths (multi-BSSID scan / synthetic-RSSI fallback / simulation) omit the key entirely. Existing pre-v0.7.0 UI clients ignore it cleanly. Documented in `docs/user-guide.md` "Per-node mesh sync (ADR-110)" section with field table, UI rendering rules, and the timestamp-recovery recipe. **Branch-coordination**: `docs/ADR-110-BRANCH-STATE.md` maps which files each of `adr-110-esp32c6` vs `feat/adr-115-ha-mqtt-matter` touches (regions are disjoint, merges should be clean line-merges). **Verification baselines**: full v2 cargo workspace at **1437 tests passing** (no regression across 17 crate batches), full `wifi-densepose-hardware` crate at **137 tests**. ADR-110 §B substrate is now end-to-end visible to UI clients and ready for ADR-029/030 multistatic CSI fusion consumption.
- **Real-time CSI introspection / low-latency tap on `wifi-densepose-sensing-server` (ADR-099).**
New `wifi_densepose_sensing_server::introspection` module wires
[midstream](https://github.com/ruvnet/midstream)'s `temporal-attractor` (Lyapunov +
+1 -7
View File
@@ -118,7 +118,7 @@ node scripts/mincut-person-counter.js --port 5006 # Correct person counting
> |--------|----------|------|----------|-------------|
> | **ESP32 + Cognitum Seed** (recommended) | ESP32-S3 + [Cognitum Seed](https://cognitum.one) | ~$140 | Yes | Presence, motion, breathing, heart rate, fall detection, multi-person counting, 17-keypoint pose (signed Cog binary), 105-cog catalog, persistent vector store, kNN search, witness chain, MCP proxy |
> | **ESP32 Mesh** | 3-6× ESP32-S3 + WiFi router | ~$54 | Yes | Same capabilities as above without the persistent-memory features |
> | **ESP32-C6 research node** ([ADR-110](docs/adr/ADR-110-esp32-c6-firmware-extension.md), [witness](docs/WITNESS-LOG-110.md), [reviewer guide](docs/ADR-110-REVIEW-GUIDE.md), [firmware v0.7.0](https://github.com/ruvnet/RuView/releases/tag/v0.7.0-esp32)) | ESP32-C6-DevKit ($610) | ~$10 | Yes (Wi-Fi 6 capable) | Same CSI pipeline as S3 with the dual-target firmware. **Firmware-side ADR-110 substrate now closed** (v0.7.0): ESP-NOW cross-board mesh quantified at **99.56 % match / 104 µs smoothed offset stdev / 3.95× EMA suppression** over a 5-min two-board soak (witness §A0.10), 32-byte UDP sync packet with operator-tunable cadence (§A0.12), ADR-018 byte 19 bit 4 wire-fix sourced from the working ESP-NOW path (§A0.13). Wire format ready for HE-LTF PPDU tagging in ADR-018 bytes 18-19 (firmware encoder + Rust + Python decoders verified end-to-end across 23 unit tests). LP-core motion-gate RISC-V program and Wi-Fi 6 soft-AP with TWT Responder both ship as opt-in code paths (default off). **Hardware-gated for measurement**: HE-LTF live subcarrier capture needs an 11ax AP (IDF v5.4 doesn't expose AP-side HE config — §A0.6); ~5 µA LP-core hibernation needs an INA meter to capture; 802.15.4 raw RX is broken in IDF v5.4 (workaround: ESP-NOW transport, shipped + measured). See witness log for the empirical / claimed split. |
> | **ESP32-C6 research node** ([ADR-110](docs/adr/ADR-110-esp32-c6-firmware-extension.md), [witness](docs/WITNESS-LOG-110.md), [reviewer guide](docs/ADR-110-REVIEW-GUIDE.md), [firmware v0.6.7](https://github.com/ruvnet/RuView/releases/tag/v0.6.7-esp32)) | ESP32-C6-DevKit ($610) | ~$10 | Yes (Wi-Fi 6 capable) | Same CSI pipeline as S3 with the dual-target firmware. **Wire format ready** for HE-LTF PPDU tagging in ADR-018 bytes 18-19 (firmware encoder + Rust + Python decoders verified end-to-end in 17 unit tests), ESP-NOW cross-node sync (4102 tx 0 fail cumulative across 120 s + 300 s soaks), and TWT graceful-NACK fallback (live exercised). **v0.6.7 adds** a real LP-core motion-gate RISC-V program (B4 code path) and a Wi-Fi 6 soft-AP with TWT Responder for two-board iTWT benches (B1/B2 unblock, no 11ax router required). **Hardware-gated for measurement**: HE-LTF live subcarrier capture needs the soft-AP bench or an 11ax AP; ~5 µA LP-core hibernation needs an INA meter to capture; 802.15.4 raw RX is broken in IDF v5.4 (workaround: ESP-NOW transport, shipped). See witness log for the empirical / claimed split. |
> | **Research NIC** | Intel 5300 / Atheros AR9580 | ~$50-100 | Yes | Full CSI with 3x3 MIMO |
> | **Any WiFi** | Windows, macOS, or Linux laptop | $0 | No | RSSI-only: coarse presence and motion (see [tutorial #36](https://github.com/ruvnet/RuView/issues/36)) |
>
@@ -591,12 +591,6 @@ Verify the plugin structure: `bash plugins/ruview/scripts/smoke.sh`. Full detail
MIT License — see [LICENSE](LICENSE) for details.
## 🤝 Creator Affiliate Program
**For TikTok · Instagram · YouTube creators** — earn **25% on every Cognitum sale** you refer. The RuFlo, RuView, and RuVector videos you're already making have done millions of views; get paid for the orders they drive. Click-tracking activates instantly; commissions activate after a quick manual review (usually under 24 hours).
[Apply now → cognitum.one/affiliate](https://cognitum.one/affiliate)
## 📞 Support
[GitHub Issues](https://github.com/ruvnet/RuView/issues) | [Discussions](https://github.com/ruvnet/RuView/discussions) | [PyPI](https://pypi.org/project/wifi-densepose/)
-42
View File
@@ -284,48 +284,6 @@ class SyncPacket:
sequence: int # u32 — high-water CSI sequence at emit time
flags_raw: int
def local_minus_epoch_us(self) -> int:
"""Signed local-vs-mesh clock offset in µs.
Negative when this node's clock is behind the leader's (typical
for followers). Equal to ≈0 on the leader (modulo call-stack µs).
Matches Rust's `SyncPacket::local_minus_epoch_us` byte-for-byte.
"""
return self.local_us - self.epoch_us
def apply_to_local(self, local_at_frame_us: int) -> int:
"""Recover a mesh-aligned timestamp for any node-local µs snapshot.
Math (see WITNESS-LOG-110 §A0.10 / §A0.12):
offset = epoch_us - local_us (signed; this packet)
mesh = local_at_frame_us + offset
Identical contract to Rust's `SyncPacket::apply_to_local`.
Identity at `local_at_frame_us == self.local_us` returns `epoch_us`.
"""
offset = self.epoch_us - self.local_us
return local_at_frame_us + offset
def mesh_aligned_us_for_sequence(self, frame_seq: int, fps_hz: float) -> int:
"""ADR-110 §A0.12 — recover the mesh-aligned timestamp for an
in-flight CSI frame by its sequence number.
Pairs the frame's sequence number against this sync packet's
sequence high-water + an assumed/measured CSI rate. Matches the
Rust implementation byte-for-byte at the integer level (Python
rounds via `int()` truncation; for the canonical bench values
this is exact).
"""
if fps_hz <= 0:
raise ValueError(f"fps_hz must be positive, got {fps_hz}")
# Wrap to handle u32 sequence overflow the same way Rust does.
dframes = (frame_seq - self.sequence) & 0xFFFFFFFF
if dframes >= 0x80000000:
dframes -= 0x1_0000_0000
dus = int(dframes * 1_000_000 / fps_hz)
local_at = self.local_us + dus
return self.apply_to_local(local_at)
class SyncPacketParser:
"""Parser for ADR-110 §A0.12 32-byte sync packets.
@@ -19,8 +19,6 @@ from hardware.csi_extractor import (
CSIExtractor,
CSIParseError,
CSIExtractionError,
SyncPacket,
SyncPacketParser,
)
# ADR-018 constants
@@ -259,172 +257,3 @@ class TestESP32BinaryParser:
await extractor.disconnect()
asyncio.run(run_test())
# ============================================================================
# ADR-110 §A0.12 — SyncPacket / SyncPacketParser tests (firmware v0.6.9+)
# ============================================================================
SYNC_MAGIC = 0xC511A110
SYNC_SIZE = 32
SYNC_FMT = '<IBBBBQQI4x'
def build_sync_packet(
node_id: int = 9,
proto_ver: int = 1,
is_leader: bool = False,
is_valid: bool = True,
smoothed_used: bool = True,
local_us: int = 28798450,
epoch_us: int = 27634885,
sequence: int = 20,
) -> bytes:
flags = 0
if is_leader: flags |= 0x01
if is_valid: flags |= 0x02
if smoothed_used: flags |= 0x04
return struct.pack(
SYNC_FMT,
SYNC_MAGIC,
node_id, proto_ver, flags, 0,
local_us, epoch_us, sequence,
)
class TestSyncPacketParser:
"""ADR-110 §A0.12: 32-byte UDP sync packet (magic 0xC511A110)."""
def test_follower_typical_packet_roundtrips(self):
"""Match the COM9-witnessed sync-pkt #1 byte-for-byte."""
raw = build_sync_packet(
node_id=9, is_leader=False, is_valid=True, smoothed_used=True,
local_us=28798450, epoch_us=27634885, sequence=20,
)
assert len(raw) == SYNC_SIZE
pkt = SyncPacketParser.parse(raw)
assert isinstance(pkt, SyncPacket)
assert pkt.node_id == 9
assert pkt.proto_ver == 1
assert pkt.is_leader is False
assert pkt.is_valid is True
assert pkt.smoothed_used is True
assert pkt.local_us == 28798450
assert pkt.epoch_us == 27634885
assert pkt.sequence == 20
# The 1.16-second boot delta from §A0.10 should be recoverable
assert pkt.local_us - pkt.epoch_us == 1163565
def test_leader_packet_has_local_close_to_epoch(self):
"""COM12 (leader) had flags=0x03 and epoch ≈ local."""
raw = build_sync_packet(
node_id=12, is_leader=True, is_valid=True, smoothed_used=False,
local_us=28864932, epoch_us=28864939, sequence=20,
)
pkt = SyncPacketParser.parse(raw)
assert pkt.node_id == 12
assert pkt.is_leader is True
assert pkt.is_valid is True
assert pkt.smoothed_used is False
assert pkt.flags_raw == 0x03
assert pkt.local_us - pkt.epoch_us == -7 # leader has zero offset
def test_magic_mismatch_raises(self):
"""A non-sync datagram must not silently decode."""
raw = bytearray(build_sync_packet())
raw[0] = 0x01 # corrupt magic low byte
with pytest.raises(CSIParseError, match="magic mismatch"):
SyncPacketParser.parse(bytes(raw))
def test_short_packet_raises(self):
"""Below 32 bytes must error early, not silently truncate."""
raw = build_sync_packet()[:16]
with pytest.raises(CSIParseError, match="too short"):
SyncPacketParser.parse(raw)
def test_all_flag_combinations(self):
"""Each flag bit decodes independently."""
for is_leader in (False, True):
for is_valid in (False, True):
for smoothed_used in (False, True):
raw = build_sync_packet(
is_leader=is_leader,
is_valid=is_valid,
smoothed_used=smoothed_used,
)
pkt = SyncPacketParser.parse(raw)
assert pkt.is_leader == is_leader
assert pkt.is_valid == is_valid
assert pkt.smoothed_used == smoothed_used
def test_dispatch_distinguishes_csi_from_sync(self):
"""A host can pick CSI vs sync by leading magic."""
csi_magic = struct.unpack_from('<I', build_binary_frame(), 0)[0]
sync_magic = struct.unpack_from('<I', build_sync_packet(), 0)[0]
assert csi_magic == ESP32BinaryParser.MAGIC
assert sync_magic == SyncPacketParser.MAGIC
assert csi_magic != sync_magic
def test_apply_to_local_recovers_epoch_at_sync_point(self):
"""ADR-110 iter 26 — Python parity with Rust's `apply_to_local`.
At local_at_frame == sync.local_us, the recovered mesh time must
equal sync.epoch_us exactly."""
pkt = SyncPacketParser.parse(build_sync_packet(
local_us=28_798_450, epoch_us=27_634_885, sequence=20,
))
assert pkt.apply_to_local(pkt.local_us) == pkt.epoch_us
assert pkt.local_minus_epoch_us() == 1_163_565 # §A0.10's bench number
def test_apply_to_local_preserves_inter_frame_delta(self):
"""A frame arriving 5 s after the sync packet on the follower's
local clock must produce a mesh time exactly 5 s after sync.epoch_us."""
pkt = SyncPacketParser.parse(build_sync_packet(
local_us=28_798_450, epoch_us=27_634_885, sequence=20,
))
local_at_frame = pkt.local_us + 5_000_000
assert pkt.apply_to_local(local_at_frame) == pkt.epoch_us + 5_000_000
def test_mesh_aligned_us_for_sequence_matches_rust(self):
"""Cross-language parity with Rust's
`end_to_end_sync_decode_then_frame_mesh_recovery` test —
100 frames after sync.sequence at 20 fps = sync.epoch_us + 5 s."""
pkt = SyncPacketParser.parse(build_sync_packet(
local_us=28_798_450, epoch_us=27_634_885, sequence=20,
))
mesh = pkt.mesh_aligned_us_for_sequence(120, 20.0)
assert mesh == pkt.epoch_us + 5_000_000
# Both paths (apply_to_local + interpolation) must agree
local_at = pkt.local_us + 5_000_000
assert pkt.apply_to_local(local_at) == mesh
def test_canonical_wire_bytes_match_rust_decoder(self):
"""ADR-110 iter 21 — cross-language wire-format conformance gate.
These exact bytes also appear pinned in the Rust hardware crate's
`canonical_wire_bytes_match_python_decoder` test (same field
values, encoded by Rust's `SyncPacket::to_bytes`). If Python's
hardcoded hex stops matching what Rust produces from the equivalent
SyncPacket struct, ONE of the decoders has drifted from the wire.
Canonical packet: COM9 sync-pkt #1 from §A0.12 live capture.
"""
canonical = bytes.fromhex(
"10a111c509010600" # magic LE + node=9 + ver=1 + flags=0x06 + reserved
"f26db70100000000" # local_us = 28_798_450 (LE u64)
"c5aca50100000000" # epoch_us = 27_634_885 (LE u64)
"1400000000000000" # sequence = 20 (LE u32) + 4 reserved bytes
)
assert len(canonical) == SyncPacketParser.SIZE == 32
pkt = SyncPacketParser.parse(canonical)
assert pkt.node_id == 9
assert pkt.proto_ver == 1
assert pkt.flags_raw == 0x06
assert pkt.is_leader is False
assert pkt.is_valid is True
assert pkt.smoothed_used is True
assert pkt.local_us == 28_798_450
assert pkt.epoch_us == 27_634_885
assert pkt.sequence == 20
# Recovered offset matches §A0.10's measured 1.16-second boot delta.
assert pkt.local_us - pkt.epoch_us == 1_163_565
-97
View File
@@ -1,97 +0,0 @@
# ADR-110 — Branch state (as of 2026-05-23, iter 22)
Reference card for anyone collaborating on or near the ADR-110 work. The /loop SOTA sprint that closed the firmware-side substrate ran into multiple cross-branch checkout incidents (see iter 17-19); this page exists so the next collaborator doesn't have to re-derive the layout from `git log`.
## Branch ownership
| Branch | Owner | What it carries | Don't merge from |
|---|---|---|---|
| `main` | shared | shipped release line | — |
| `adr-110-esp32c6` | ADR-110 / C6 firmware substrate | Everything described in `WITNESS-LOG-110 §A0.x` (4 firmware tags v0.6.7 → v0.7.0, Python + Rust decoders, sensing-server wire, mesh-aligned timestamp recovery, fps EMA, cross-language conformance gate) | Don't accidentally land `feat/adr-115-ha-mqtt-matter` work here uncommitted |
| `feat/adr-115-ha-mqtt-matter` | ADR-115 / HA-DISCO + HA-FABRIC + HA-MIND | MQTT publisher (`rumqttc`), Matter Bridge, semantic automation primitives, related Cargo features + CLI flags | Don't accidentally land ADR-110 `wifi-densepose-hardware` dep mods here |
## Files each branch touches
### `adr-110-esp32c6` — primary modifications
```
firmware/esp32-csi-node/version.txt # bumped 0.6.6 → 0.7.0
firmware/esp32-csi-node/main/c6_*.{c,h} # LP-core, TWT, timesync, soft-AP HE, ESP-NOW sync
firmware/esp32-csi-node/main/lp_core/main.c # real LP-core polling program
firmware/esp32-csi-node/main/csi_collector.c # byte 19 bit 4 OR-fix; sync packet emit
firmware/esp32-csi-node/main/Kconfig.projbuild # C6_* knobs
firmware/esp32-csi-node/main/CMakeLists.txt # ulp_embed_binary
firmware/esp32-csi-node/sdkconfig.defaults.esp32c6 # C6 overlay
archive/v1/src/hardware/csi_extractor.py # SyncPacketParser + SyncPacket dataclass
archive/v1/tests/unit/test_esp32_binary_parser.py # TestSyncPacketParser (7 tests)
v2/crates/wifi-densepose-hardware/src/sync_packet.rs # new module (15 tests)
v2/crates/wifi-densepose-hardware/src/lib.rs # re-exports
v2/crates/wifi-densepose-sensing-server/Cargo.toml # ONLY adds wifi-densepose-hardware path dep
v2/crates/wifi-densepose-sensing-server/src/main.rs # NodeState::{latest_sync, csi_fps_ema,
# mesh_aligned_us_for_csi_frame,
# observe_csi_frame_arrival}
# udp_receiver_task magic dispatch
# fps_ema_tests module (4 tests)
docs/adr/ADR-110-esp32-c6-firmware-extension.md # 670 → ~750 lines (P10 + sprint summary)
docs/WITNESS-LOG-110.md # 13 §A0.x entries
docs/ADR-110-REVIEW-GUIDE.md # reviewer one-pager
docs/ADR-110-BRANCH-STATE.md # ← this file
```
### `feat/adr-115-ha-mqtt-matter` — primary modifications
```
docs/adr/ADR-115-home-assistant-integration.md # the design
v2/crates/wifi-densepose-sensing-server/Cargo.toml # rumqttc dep + [features] block
v2/crates/wifi-densepose-sensing-server/src/cli.rs # --mqtt / --matter / --semantic flags
```
## Known overlap points (handle with care)
Both branches touch `v2/crates/wifi-densepose-sensing-server/Cargo.toml` and `src/main.rs`. The conflict surface is **disjoint by section**:
| File | ADR-110 region | ADR-115 region |
|---|---|---|
| `Cargo.toml` | `[dependencies]``wifi-densepose-hardware = { path = "../wifi-densepose-hardware" }` near the existing `wifi-densepose-signal` line | `[dependencies]``rumqttc` block below + `[features]` block at end |
| `main.rs` | `NodeState` fields + `impl NodeState` helpers + `update_csi_fps_ema` free fn + `fps_ema_tests` module + `udp_receiver_task` magic dispatch | (TBD per ADR-115 P-plan) |
A merge between the two branches should be **clean line-merge** since the regions don't overlap. If git ever reports a real conflict in either of these files, that means one branch has drifted into the other's region — investigate before resolving blindly.
## Quick test commands (verify either branch is sane)
```bash
# Rust workspace (run from v2/)
cd v2
cargo test --workspace --no-default-features --lib # 1437 tests at iter 22, 0 failures
# Python ADR-110 host decoder (from repo root)
python -m pytest archive/v1/tests/unit/test_esp32_binary_parser.py::TestSyncPacketParser -v
# Cross-language wire-format gate (the iter 21 pin)
cargo test -p wifi-densepose-hardware --no-default-features --lib sync_packet::tests::canonical_wire_bytes_match_python_decoder
python -m pytest archive/v1/tests/unit/test_esp32_binary_parser.py::TestSyncPacketParser::test_canonical_wire_bytes_match_rust_decoder -v
```
If either side of the canonical-wire-bytes pair fails alone, the OTHER decoder has drifted from the wire format — investigate that decoder first, not the failing test.
## Future-proofing
- When the ADR-115 agent ships `feat/adr-115-ha-mqtt-matter` to main and ADR-110 also ships, merge `main` into `adr-110-esp32c6` (or vice versa) and re-run both test suites. The disjoint-region structure above should make the merge a no-conflict fast-forward.
- When a third agent picks up either ADR, point them at this file before they start editing shared files.
- If a /loop drives autonomous iterations and hits a cross-branch checkout, the recovery procedure is in iter 18's commit message (`2997165bc`) — stash on the foreign branch, `git checkout` home, replay the iter locally.
## Lessons for `/loop` and `/loop-worker` future runs
Captured after the 38-iter ADR-110 SOTA sprint (`/loop 5m until sota. and ultra optmized`):
1. **Always verify the current branch at the start of each iter** — when a /loop fires every 5 minutes and another agent is active on a sibling branch, the working tree can flip without your action. Run `git branch --show-current` as the first line of every iter; if it isn't what you expect, stash and switch back BEFORE editing. We burned ~30 min in iter 17-19 recovering from two silent branch flips.
2. **Don't `git add <file>` blindly after a branch switch** — the file may have inherited changes from the foreign branch (uncommitted work that came along on checkout). Always `git diff --cached` before `git commit`. We accidentally absorbed ADR-115's Cargo.toml/cli.rs work into ADR-110's iter-18 commit; required a follow-up revert commit (`ca2059b07`) and stash dance.
3. **Sibling-region edits in shared files** — when two branches both touch `v2/crates/wifi-densepose-sensing-server/Cargo.toml` or `src/main.rs`, agree on which `[section]` or struct each owns. Document the regions in this file (see Known overlap points). Merges then stay clean line-merge fast-forwards instead of needing conflict resolution.
4. **Extract pure helpers before committing inline mutations** — iter 30 (`sync_snapshot`), iter 32 (`apply_sync_packet`), iter 37 (`fleet_role_counts`) all converted inline state-changes into named, free, testable functions. Each saved 4+ inline duplications and let the helper be tested without spinning up axum / tokio. Bake this into every iter's plan: *"what's the smallest helper I can extract here?"*
5. **Cross-language wire-format gates** — when shipping a protocol decoder in both Python and Rust, pin the SAME canonical byte string in BOTH test suites (iter 21 pattern). One side drifting fires exactly one named test on exactly the drifted decoder. Don't wait until "later" — add the pin in the iter that ships the second language.
6. **Helper tests > integration tests when state is heavy**`AppStateInner` has too many fields to construct in a test. Instead of fighting it, extract per-field logic into pure helpers (iter 30 sync_snapshot pattern). Tests target the helpers, the handler glue stays thin and trivially correct.
7. **Local stub files lag firmware additions**`firmware/esp32-csi-node/test/stubs/esp_stubs.c` doesn't get rebuilt with the firmware proper, so a new symbol added to a `*.h` won't surface as a fuzz-target link error until CI runs. Iter 38 caught `c6_sync_espnow_is_valid` this way. **Whenever you add a function whose declaration is reachable from `csi_collector.c`, also add a stub** in the same commit.
8. **Cron-based /loop accumulates work across irreversible checkpoints (tags, releases, PR ready)** — once you cut a tag or mark a PR ready, the cost of reverting is much higher than a code edit. Save those for iters when you have surplus confidence (full local test suite green, CI from previous iter green). Iter 12 (v0.7.0 cut) and iter 38 (PR ready) were the right shape: only happened after iter 6 / iter 37 evidence had landed.
@@ -2,14 +2,12 @@
| Field | Value |
|-------|-------|
| **Status** | Accepted P1P10 complete, firmware-side substrate closed at **v0.7.0-esp32** (2026-05-23) |
| **Date** | 2026-05-22 (created) · 2026-05-23 (last revision — P10 + sprint summary) |
| **Status** | Accepted (P1P7 shipped on `main` branch, P8 docs + bench landed) |
| **Date** | 2026-05-22 |
| **Deciders** | ruv |
| **Codename** | **C6-SOTA** |
| **Relates to** | ADR-018 (CSI binary frame format), ADR-028 (ESP32 capability audit), ADR-029 (RuvSense multistatic), ADR-030 (RuvSense persistent field model), ADR-031 (RuView sensing-first), ADR-061 (QEMU CI), ADR-081 (adaptive CSI mesh kernel), ADR-097 (rvCSI adoption) |
| **Tracking issue** | [ruvnet/RuView#762](https://github.com/ruvnet/RuView/issues/762) |
| **Firmware releases** | [v0.6.7](https://github.com/ruvnet/RuView/releases/tag/v0.6.7-esp32) · [v0.6.8](https://github.com/ruvnet/RuView/releases/tag/v0.6.8-esp32) · [v0.6.9](https://github.com/ruvnet/RuView/releases/tag/v0.6.9-esp32) · [v0.7.0](https://github.com/ruvnet/RuView/releases/tag/v0.7.0-esp32) |
| **Witness** | [`docs/WITNESS-LOG-110.md`](../WITNESS-LOG-110.md) — 13 §A0 entries (§A0.1 → §A0.13), 1 §A.1-A.12 dual-soak, 4 §B blocker entries, 5 §C bug fixes, 1 §D-workaround |
---
@@ -137,75 +135,11 @@ In both cases the HP-side API stays the same: `c6_lp_core_arm()` configures the
| **P7** | Benchmark C6 vs S3 (CSI fps, RAM, TWT jitter, power) | ✅ **done** — boot 353 ms, ts init 413 ms, image 1003 KB (9 % vs S3), 310 KiB free heap, CSI callbacks fire at 64 subcarriers/frame on ch 1 background traffic |
| **P8** | Witness bundle update, CLAUDE.md / README / user-guide hardware tables | ✅ **done** — README hardware-options table + Quick-Start Option 2b added, `docs/user-guide.md` now has full ESP32-C6 section (build, flash, provision, multi-room time-sync, battery seed mode) |
| **P9** | **Software-only unblocks for B1/B2/B4 (firmware v0.6.7)** | ✅ **done** — (1) Real LP-core motion-gate program loads via `ulp_embed_binary(lp_core/main.c)`, exposes shared `motion_count`/`poll_count` symbols for witness verification (B4 code path complete, hardware-measurement still pending INA). (2) Soft-AP HE module (`c6_softap_he.{h,c}`) runs the C6 in AP+STA mode with WPA2 + HE advertised so a second C6 STA can negotiate real iTWT against a known-cooperative AP (B1/B2 unblocker without buying an 11ax router). (3) Build artifacts: S3 8 MB 1093 KB / C6 4 MB 1019 KB, both green on IDF v5.4. Both new modules default-off so v0.6.6 fleets see no behavior change. |
| **P10** | **End-to-end mesh substrate: measured, smoothed, wired, decoded (firmware v0.6.8 → v0.7.0 + host crates)** | ✅ **done** — bench-quantified two-board substrate **and** the host-side wire that consumes it. **(a) v0.6.8 ESP-NOW EMA smoother** (`c6_sync_espnow.c`, α=1/8 fixed-point shift, 8-sample window). 5-min two-board soak (witness §A0.10) measured **411.5 µs raw stdev → 104.1 µs smoothed stdev (3.95× suppression, 4.70× peak-to-peak)** with **+30 µs/min crystal drift preserved within 2 µs/min**. **Cross-board RX 99.56 %** over 2701 beacons, 0 TX fail, leader election fired at +27336 ms. The ADR-110 §2.4 ≤100 µs alignment target is **empirically met by the smoothed offset alone**. **(b) v0.6.9 sync-packet** (32-byte UDP, magic `0xC511A110`, every `CONFIG_C6_SYNC_EVERY_N_FRAMES` CSI frames) carries `(node_id, local_us, epoch_us, sequence)` so host can pair against incoming CSI frames. Live-verified §A0.12 — COM9 reports `local epoch = 1 163 565 µs` matching §A0.10's measured boot delta within 285 µs. **(c) v0.7.0 ADR-018 byte 19 bit 4 wire-fix** — bit 4 now sourced from `c6_sync_espnow_is_valid()` (was only the broken 802.15.4 path). Mixed S3+C6 fleets correctly advertise sync via the working transport. **(d) Host-side decoders + wiring**: Python `SyncPacketParser` (6 tests) + Rust `SyncPacket` (10 tests, all green; `SyncPacket::apply_to_local` recovers per-frame mesh-aligned timestamps). Sensing-server `udp_receiver_task` magic-dispatches `0xC511A110` and stores `NodeState::latest_sync` + `NodeState::mesh_aligned_us(local_at_frame)` helper. **(e) IDF v5.4 upstream gap formally documented (§A0.6)**: full `components/esp_wifi/include/esp_wifi*.h` grep proves the public API exposes only STA-side iTWT/bTWT — no `esp_wifi_ap_set_he_config`, no `wifi_he_ap_config_t`. Soft-AP HE/TWT-Responder advertise is not user-controllable on C6 in IDF v5.4; B1/B2 measurement requires either a future IDF or an external 11ax AP. |
This ADR is updated at the end of each phase with the actual outcome, links to commits, and any deviations from the design.
### 4.1 P10 detail — `/loop 5m` SOTA sprint (2026-05-23)
P10 was driven by a `/loop 5m until sota. and ultra optmized` invocation that ran 16 iterations over ~80 minutes. The sprint shipped 4 firmware releases, 17 commits on the branch, 13 host-side unit tests, and converted the §B substrate from "designed targeting ±100 µs" into "measured at 104 µs smoothed stdev over a 5-min two-board soak with full host-side decoders + sensing-server consumer."
| Iter | Shipped | Witness |
|---|---|---|
| 1 | `c6_softap_he` module + IDF v5.4 gap discovery | §A0.5, §A0.6 |
| 2 | ESP-NOW cross-board mesh proven live | §A0.7 |
| 3 | 4 MB S3 release variant | — |
| 4 | 4-min mesh soak — first quantified sync stability | §A0.8 |
| 5 | EMA smoother in firmware (α=1/8) | §A0.9 |
| 6 | 5-min EMA soak: **3.95× suppression measured** | §A0.10 |
| 7 | v0.6.8-esp32 release + §A0.11 timestamp-wiring gap recorded | §A0.11 |
| 8 | Sync packet emission (option 2 chosen) | — |
| 9 | Sync packet live-verified on both boards | §A0.12 |
| 10 | v0.6.9-esp32 release + `CONFIG_C6_SYNC_EVERY_N_FRAMES` Kconfig knob | — |
| 11 | ADR-018 byte 19 bit 4 wire-fix from ESP-NOW path | — |
| 12 | v0.7.0-esp32 release + Python `SyncPacketParser` stub | §A0.13 |
| 13 | 6 Python unit tests + README/user-guide doc updates | — |
| 14 | Rust `SyncPacket` decoder + 7 unit tests in `wifi-densepose-hardware` | — |
| 15 | Sensing-server `udp_receiver_task` magic-dispatch + `NodeState::latest_sync` | — |
| 16 | `SyncPacket::apply_to_local()` + `NodeState::mesh_aligned_us()` (+ 3 more tests, 10 total) | — |
### 4.2 P10 measured numbers (substrate now quantified, not just designed)
Every number below comes from a real bench capture against COM9 + COM12 ESP32-C6 boards, raw logs preserved under `dist/firmware-v0.6.7/iter{2,4,5,6,9}-*.log` and `dist/firmware-v0.6.8/iter9-*.log`.
| Metric | Measured | Target |
|---|---|---|
| Cross-board ESP-NOW RX rate (5-min soak) | **99.56 %** (2689 / 2701 beacons) | — |
| Cross-board TX failures (5-min soak) | **0** on either board | — |
| Beacon rate | **10.00 /s** exactly (FreeRTOS solid) | 10 Hz nominal |
| Raw offset stdev | 411.5 µs | — |
| **EMA-smoothed offset stdev** | **104.1 µs** | **≤100 µs (§2.4)** |
| Range reduction (smoothed vs raw) | **4.70×** peak-to-peak | — |
| Measured C6 crystal skew between bench boards | **1.4 ppm** | ESP32 spec ±10 ppm |
| Drift preservation (smoothed tracking raw) | within **2 µs/min** | — |
| Leader election | ✅ COM9 stepped down at +27 336 ms on `lower-id` rule | — |
| Sync packet round-trip (firmware → Python decoder) | identical bytes, offset recovered to within **285 µs** of §A0.10 | — |
| Raw 802.15.4 RX | 0 frames over 60 s + 240 s + 300 s soaks | (D1 broken in IDF v5.4) |
| C6 v0.7.0 image size / slack | 1019 KB / **45 %** on 4 MB single-OTA | — |
| S3 v0.7.0 image size / slack | 1094 KB / **47 %** on 8 MB dual-OTA | — |
### 4.3 P10 host-side surface (production code shipped)
| Crate / File | New API |
|---|---|
| `v2/crates/wifi-densepose-hardware/src/sync_packet.rs` | `SyncPacket`, `SyncPacketFlags`, `SYNC_PACKET_MAGIC = 0xC511A110`, `SYNC_PACKET_SIZE = 32`, `SyncPacket::from_bytes`, `SyncPacket::to_bytes`, `SyncPacket::local_minus_epoch_us`, `SyncPacket::apply_to_local(local_us)` — 10 unit tests, all green |
| `v2/crates/wifi-densepose-sensing-server/src/main.rs` | `NodeState::latest_sync: Option<SyncPacket>`, `NodeState::latest_sync_at: Option<Instant>`, `NodeState::mesh_aligned_us(local_at_frame_us) -> Option<u64>`, `udp_receiver_task` magic-dispatch on `SYNC_PACKET_MAGIC` |
| `archive/v1/src/hardware/csi_extractor.py` | `SyncPacket` dataclass, `SyncPacketParser.parse`, `SyncPacketParser.MAGIC` — 6 Python unit tests, all green |
## 5. Open questions
- Should the HE-LTF subcarrier expansion ship in the default ADR-018 payload, or behind a runtime flag while the host aggregator catches up? **Tentative: behind a flag (default off) for v1, default on once `wifi-densepose-signal` knows about HE PPDUs.**
- Should the 802.15.4 time-sync channel be configurable, or hard-coded to 15? **Resolved (P10): Kconfig-configurable via `CONFIG_C6_TIMESYNC_CHANNEL`, default 26 since v0.6.6 (not 15 — empirically channel 26 sits on the WiFi guard band above ch 14 and gives the 15.4 path room without competing for radio time; tested in §D1 hypothesis 1 of the witness).**
- Should the 802.15.4 time-sync channel be configurable, or hard-coded to 15? **Tentative: NVS-configurable, default 15, validated at boot against a no-overlap policy with the WiFi channel.**
- Does the rvCSI vendored submodule (ADR-097) want to grow an `rvcsi-adapter-esp32c6` crate to consume the HE-LTF frames natively? **Out of scope for this ADR; revisit in a follow-up.**
## 6. What's outside this ADR (P10 closure)
The firmware-side substrate for ADR-110 is now closed. Three categories remain, all explicitly **not** in this ADR's scope:
1. **Multistatic CSI fusion math** — ADR-029/030 territory. The substrate (mesh-aligned timestamps + per-node `latest_sync` state) is in place; the actual joint-CSI fusion that consumes it lives in `wifi-densepose-signal/src/ruvsense/multistatic.rs`.
2. **Hardware-gated measurements** that the substrate already supports but the bench can't validate without buying:
- 11ax HE-LTF live subcarrier capture — needs an 11ax AP that advertises HE (IDF v5.4 doesn't expose an AP-side HE config API, §A0.6).
- ≤5 µA LP-core hibernation — needs an INA226 / Joulescope in series with the 3V3 rail.
3. **IDF upstream fixes**:
- 802.15.4 RX path on C6 + IDF v5.4 — `c6_timesync` ships and initialises but never RXes a frame (D1, 5 hypotheses tested + rejected). ESP-NOW workaround (`c6_sync_espnow`) is the working primary mesh transport. The 802.15.4 source stays in for the day IDF fixes the driver.
- Soft-AP HE/TWT-Responder advertise API — `c6_softap_he` ships as the in-place hook for when IDF v5.5+ exposes it.
@@ -1,670 +0,0 @@
# ADR-115: Home Assistant integration via MQTT auto-discovery + Matter bridge
| Field | Value |
|-------|-------|
| **Status** | Proposed |
| **Date** | 2026-05-23 |
| **Deciders** | ruv |
| **Codename** | **HA-DISCO** (MQTT) + **HA-FABRIC** (Matter) |
| **Relates to** | ADR-018 (CSI binary frame format), ADR-021 (ESP32 vitals), ADR-031 (RuView sensing-first), ADR-039 (edge vitals packet 0xC511_0002), ADR-079 (camera ground-truth), ADR-103 (cog-person-count), ADR-110 (ESP32-C6 firmware), ADR-114 (cog-quantum-vitals) |
| **Tracking issue** | TBD — file under RuView issue tracker, link in §10 |
| **Related issues** | [#574](https://github.com/ruvnet/RuView/issues/574) (mDNS for seed_url), [#760](https://github.com/ruvnet/RuView/issues/760) (sensing UI), [#761](https://github.com/ruvnet/RuView/issues/761) (HA competitor scan) |
---
## 1. Context
RuView and the underlying WiFi-DensePose stack already expose rich human-sensing telemetry — presence, person count, 17-keypoint pose, breathing rate (BR), heart rate (HR), motion level, fall detection, RSSI, and zone occupancy — over a Rust `wifi-densepose-sensing-server` (`v2/crates/wifi-densepose-sensing-server`). The server emits three structured message types over its WebSocket at `/ws/sensing`:
| Server message `type` | Source (`main.rs`) | Payload (selected fields) |
|---|---|---|
| `pose_data` | line 2340 | 17 keypoints per detection, `confidence`, `track_id` |
| `edge_vitals` | line 3971 | `node_id`, `presence`, `fall_detected`, `motion`, `breathing_rate_bpm`, `heartrate_bpm`, `n_persons`, `motion_energy`, `presence_score`, `rssi` |
| `sensing_update` | lines 1903 / 2047 / 4098 / 4350 / 4481 | aggregated detections + zone hits |
Customers running a **Cognitum Seed** appliance (`cognitum-v0` at `:9000`) or a standalone **ESP32-S3** / **ESP32-C6** node (per ADR-110) want this telemetry inside **Home Assistant (HA)** — the most widely deployed open-source home-automation hub (>500 k installs, OSS, MQTT-native) — so they can build automations around presence, vitals, falls, and motion without writing code against our REST/WebSocket API.
### 1.1 Why this matters now
Two recent customer-facing issues show the same plug-and-play gap:
- **#574 (mDNS for seed_url)** — users don't want to manually paste a `seed://` URL into the dashboard; they expect the hub to discover the node.
- **#760 (sensing UI)** — users asked for an HA-style "single dashboard with all my sensors" experience; we currently force them through our own UI.
Both reduce to the same underlying complaint: *RuView is a black box that needs glue code to fit into the rest of a smart home.* HA solves that problem industry-wide. We should meet users where they already are.
### 1.2 Comparison: who else does this
| Product | HA approach | Notes |
|---|---|---|
| **espectre.dev** | Custom HA integration (HACS), Python | Pose-only; no vitals; closed-source server |
| **tommysense.com** | MQTT auto-discovery + cloud bridge | Vitals only; cloud-mandatory |
| **Aqara FP2** | Native ZigBee + HA | Presence + zones only; commercial mmWave |
| **mmWave HLK-LD2410** | ESPHome firmware → HA | Presence + distance, no pose, no vitals |
| **Matter devices (any)** | Native Matter clusters, multi-controller | Apple/Google/Alexa/HA all consume; presence in `OccupancySensing` since Matter 1.3; no vitals/pose clusters yet |
| **RuView (today)** | None | Customer must build their own bridge |
The competitive bar is set by Aqara FP2 (HA-native, multi-zone presence) and ESPHome-flashed LD2410 nodes (cheap, plug-and-play). To match or exceed them we need first-class HA integration that exposes our **differentiated** capabilities: pose, HR/BR, fall, multi-room.
### 1.3 What this ADR is *not*
- Not a HACS Python integration today (that's a follow-on; see §6).
- Not a webhook-only push (one-way, no entity discovery).
- Not a change to the ADR-018 CSI frame format or ADR-039 edge vitals packet — purely an additive consumer of the existing WS broadcast.
- Not a change to firmware. Both ESP32-S3 (ADR-028) and ESP32-C6 (ADR-110) paths stay byte-identical.
---
## 2. Decision
Adopt a **dual-protocol** integration strategy:
1. **Primary — MQTT + Home Assistant auto-discovery (HA-DISCO).** Add an MQTT publisher to `wifi-densepose-sensing-server` that connects to a user-supplied MQTT broker (default: `mqtt://localhost:1883`), publishes one HA-discovery message per capability per RuView node on startup and on periodic refresh (default 600 s), translates each WebSocket broadcast (`edge_vitals`, `pose_data`, `sensing_update`) into per-entity MQTT state messages, and honors a `--privacy-mode` flag that strips biometrics (HR / BR / pose keypoints) before publish.
2. **Secondary — Matter Bridge (HA-FABRIC).** Expose RuView nodes as Matter Bridged Devices over WiFi so the **subset of capabilities Matter standardises today** — presence (`OccupancySensing`), motion (`BooleanState`), fall events (`SwitchCluster`-as-event), person count (numeric attribute on the bridge) — are consumable by **any Matter controller**: Apple Home, Google Home, Amazon Alexa, Samsung SmartThings, and Home Assistant itself. Biometrics (HR/BR) and pose stay on MQTT until the Matter spec adds device types that can represent them.
The two paths are **complementary, not alternative**: MQTT carries the full telemetry surface for power users; Matter carries the standardised subset for cross-ecosystem reach. A user running HA gets both — MQTT entities populate alongside Matter Bridged Devices and HA dedupes via `unique_id`. A user running Apple Home gets only Matter, but they get the presence/fall/count signals that matter most for automations.
A **Home Assistant HACS Python integration** is sketched as a follow-on (§6.A) for users who don't run MQTT and want richer features than Matter exposes. A **REST webhook** path is rejected (§6.B).
### 2.1 Why this split (MQTT primary, Matter secondary)
| Criterion | A. MQTT auto-discovery | **D. Matter Bridge** | B. HACS Python integration | C. REST webhook |
|---|---|---|---|---|
| **Zero-code UX for end user** | yes (HA picks up entities automatically) | yes (pair via QR code, any controller) | yes (after install) | no (user wires automations by hand) |
| **Cross-ecosystem reach** | HA + any MQTT consumer | **Apple / Google / Alexa / SmartThings / HA** | HA-only | HA-only |
| **Distribution + maintenance** | one Rust feature in our existing crate | one Rust feature + Matter SDK linkage | new Python repo, HACS approval | trivial |
| **Discovery (auto entity creation)** | yes (HA's `homeassistant/` topic namespace) | yes (Matter commissioning + bridge endpoints) | yes (config flow) | no |
| **Bidirectional control** | yes (subscribe to command topic) | yes (Matter commands) | yes | one-way only |
| **Carries vitals (HR/BR) / pose** | **yes** | **no — no Matter clusters exist** | yes (custom) | yes (custom) |
| **Carries presence / count / fall** | yes | **yes (Matter 1.3+)** | yes | yes |
| **Works without HA running** | any MQTT consumer | any Matter controller | HA-only | HA-only |
| **Existing infra in target homes** | most HA users already run a broker | one Matter controller per home (Apple HomePod / Nest Hub / HA-Matter add-on) | none | none |
| **Effort to MVP** | ~2 weeks | ~46 weeks (Matter SDK + commissioning) | ~46 weeks | ~2 days |
| **Privacy controls** | per-topic + retain policy | Matter fabric isolation + spec-level limits on what's exposable | application-layer | weak |
| **Certification cost** | none | "Works with HA" free; **CSA Matter certification optional** (~$3 k/year membership for the badge) | HACS review (free) | none |
| **Test surface in CI** | dockerised mosquitto + schema lint | matter-rs test harness + chip-tool sims | full HA test harness | curl |
**MQTT is primary** because it carries 100% of RuView's differentiated telemetry (pose, HR, BR) which no other path can. **Matter is secondary** because it covers the ~30% subset (presence/count/fall) that matters across the *other 70% of smart-home buyers* who don't run HA. Together they cover the whole market. Webhook (C) gives up too much (no entity discovery, no control plane) and is rejected. HACS (B) is strictly more polished than MQTT but strictly more expensive; revisit after MQTT adoption data is in.
---
## 3. Detailed Design
### 3.1 Entity mapping
Each RuView node becomes one HA **device**. Each capability becomes an **entity** on that device. ESP32 nodes behind a Cognitum Seed appliance are linked via HA's `via_device` field so the topology shows up in the HA UI.
| Capability | HA component | `device_class` | `state_class` | Unit | Icon | Source field (server WS) |
|---|---|---|---|---|---|---|
| Presence | `binary_sensor` | `occupancy` | — | — | `mdi:motion-sensor` | `edge_vitals.presence` |
| Person count | `sensor` | — | `measurement` | persons | `mdi:account-group` | `edge_vitals.n_persons` |
| Breathing rate | `sensor` | — | `measurement` | bpm | `mdi:lungs` | `edge_vitals.breathing_rate_bpm` |
| Heart rate | `sensor` | — | `measurement` | bpm | `mdi:heart-pulse` | `edge_vitals.heartrate_bpm` |
| Motion level | `sensor` | — | `measurement` | % | `mdi:run` | `edge_vitals.motion` (01 → ×100) |
| Motion energy | `sensor` | — | `measurement` | (unitless) | `mdi:waveform` | `edge_vitals.motion_energy` |
| Fall detected | `event` | — | — | — | `mdi:human-fall` | `edge_vitals.fall_detected` |
| Presence score | `sensor` | — | `measurement` | % | `mdi:gauge` | `edge_vitals.presence_score` (×100) |
| RSSI | `sensor` | `signal_strength` | `measurement` | dBm | `mdi:wifi` | `edge_vitals.rssi` |
| Zone occupancy (per zone) | `binary_sensor` | `occupancy` | — | — | `mdi:map-marker` | `sensing_update.zones[*]` |
| Pose keypoints | `sensor` (JSON attr) | — | — | — | `mdi:human` | `pose_data.keypoints` (opt-in) |
| Tracked persons (per ID) | `binary_sensor` (dynamic) | `occupancy` | — | — | `mdi:account` | `pose_data.track_id` |
Pose keypoints are intentionally not a first-class HA entity (HA has no 17-keypoint primitive); instead they're exposed as an attribute payload on a `wifi_densepose_<node>_pose` sensor, so power users can template against them but the default HA UI stays clean.
### 3.2 MQTT topic structure
We follow HA's documented `homeassistant/<component>/<object_id>/<entity>/config` discovery convention. Object ID is `wifi_densepose_<node_id>` to namespace cleanly against other devices.
```
homeassistant/binary_sensor/wifi_densepose_<node_id>/presence/config (retained, QoS 1)
homeassistant/binary_sensor/wifi_densepose_<node_id>/presence/state (not retained, QoS 0)
homeassistant/binary_sensor/wifi_densepose_<node_id>/presence/availability (retained, QoS 1)
homeassistant/sensor/wifi_densepose_<node_id>/heart_rate/config (retained, QoS 1)
homeassistant/sensor/wifi_densepose_<node_id>/heart_rate/state (not retained, QoS 0)
homeassistant/sensor/wifi_densepose_<node_id>/breathing_rate/config
homeassistant/sensor/wifi_densepose_<node_id>/breathing_rate/state
homeassistant/event/wifi_densepose_<node_id>/fall/config (retained, QoS 1)
homeassistant/event/wifi_densepose_<node_id>/fall/state (not retained, QoS 1)
ruview/<node_id>/raw/pose (opt-in, not retained, QoS 0)
ruview/<node_id>/raw/sensing_update (opt-in, not retained, QoS 0)
```
The `ruview/<node_id>/raw/*` namespace is **outside** the `homeassistant/` discovery prefix on purpose: it carries the original WebSocket JSON for users who want to consume it directly (Node-RED, Grafana, custom scripts), without HA trying to interpret it as an entity.
### 3.3 Example discovery payloads
**Presence (binary_sensor):**
```json
{
"name": "Presence",
"unique_id": "wifi_densepose_aabbccddeeff_presence",
"object_id": "wifi_densepose_aabbccddeeff_presence",
"state_topic": "homeassistant/binary_sensor/wifi_densepose_aabbccddeeff/presence/state",
"availability_topic": "homeassistant/binary_sensor/wifi_densepose_aabbccddeeff/presence/availability",
"payload_on": "ON",
"payload_off": "OFF",
"payload_available": "online",
"payload_not_available": "offline",
"device_class": "occupancy",
"qos": 1,
"device": {
"identifiers": ["wifi_densepose_aabbccddeeff"],
"name": "RuView node aabbccddeeff",
"manufacturer": "ruvnet",
"model": "ESP32-S3 CSI node",
"sw_version": "v0.6.7",
"via_device": "cognitum_seed_1"
},
"origin": {
"name": "wifi-densepose-sensing-server",
"sw_version": "0.7.0",
"support_url": "https://github.com/ruvnet/RuView"
}
}
```
**Heart rate (sensor):**
```json
{
"name": "Heart rate",
"unique_id": "wifi_densepose_aabbccddeeff_heart_rate",
"state_topic": "homeassistant/sensor/wifi_densepose_aabbccddeeff/heart_rate/state",
"availability_topic": "homeassistant/sensor/wifi_densepose_aabbccddeeff/heart_rate/availability",
"unit_of_measurement": "bpm",
"state_class": "measurement",
"icon": "mdi:heart-pulse",
"value_template": "{{ value_json.bpm }}",
"json_attributes_topic": "homeassistant/sensor/wifi_densepose_aabbccddeeff/heart_rate/state",
"qos": 0,
"device": { "identifiers": ["wifi_densepose_aabbccddeeff"] }
}
```
State payload published to `.../heart_rate/state`:
```json
{ "bpm": 68.2, "confidence": 0.91, "ts": "2026-05-23T14:00:00Z" }
```
**Fall (event):**
```json
{
"name": "Fall detected",
"unique_id": "wifi_densepose_aabbccddeeff_fall",
"state_topic": "homeassistant/event/wifi_densepose_aabbccddeeff/fall/state",
"event_types": ["fall_detected"],
"icon": "mdi:human-fall",
"qos": 1,
"device": { "identifiers": ["wifi_densepose_aabbccddeeff"] }
}
```
State payload (fired once per fall, **not retained**):
```json
{ "event_type": "fall_detected", "ts": "2026-05-23T14:00:00.123Z", "confidence": 0.87 }
```
### 3.4 Device-level grouping
- One HA `device` per RuView **node** (ESP32-S3 / S3-Mini / C6, or the host running sensing-server in mock mode).
- `device.identifiers` = `["wifi_densepose_<node_id>"]` where `node_id` is the MAC-derived ID already in `edge_vitals.node_id`.
- For nodes behind a **Cognitum Seed**, set `device.via_device = "cognitum_seed_<seed_id>"` so HA renders the topology as a tree (Seed → child nodes).
- The Cognitum Seed itself appears as a parent device with its own diagnostic entities (uptime, agent health) — published by the seed appliance directly, not by sensing-server.
### 3.5 QoS, retention, and refresh
| Topic | QoS | Retain | Refresh cadence | Rationale |
|---|---|---|---|---|
| `*/config` | 1 | **yes** | on startup + every 600 s | HA expects retained discovery; re-publishing periodically self-heals if HA restarts before our state messages arrive |
| `*/state` (sensor) | 0 | no | rate-limited per §3.7 | Best-effort; HA can tolerate occasional drops |
| `*/state` (binary_sensor) | 1 | **yes** | on change only | Last value matters; new HA subscribers should see current state |
| `*/state` (event) | 1 | no | on event | Falls must not be missed; never retained or HA replays old events |
| `*/availability` | 1 | **yes** | LWT + 30 s heartbeat | Offline detection |
| `ruview/*/raw/*` | 0 | no | as-emitted | Raw firehose; consumers opt in |
### 3.6 Availability + Last Will and Testament (LWT)
On connect, sensing-server sets an MQTT LWT on each entity's `availability` topic to `offline` (retained). On successful connect it publishes `online` (retained). A 30-second heartbeat re-publishes `online` so HA can detect zombie sessions.
```
LWT topic: homeassistant/binary_sensor/wifi_densepose_<node_id>/presence/availability
LWT payload: offline
LWT QoS: 1
LWT retain: true
```
### 3.7 Bandwidth control + rate limiting
Pose keypoints at 10 fps × 17 keypoints × 3 floats ≈ 48 kbit/s per person — fine over LAN, but pathological if a user accidentally routes it to a metered cellular MQTT bridge. Defaults:
| Entity type | Default rate | Configurable | Override flag |
|---|---|---|---|
| Presence (binary) | on change | yes | — |
| Person count | 1 Hz | yes | `--mqtt-rate-count=1` |
| BR / HR | 0.2 Hz (every 5 s) | yes | `--mqtt-rate-vitals=0.2` |
| Motion level | 1 Hz | yes | `--mqtt-rate-motion=1` |
| Fall events | on event | no (always immediate) | — |
| RSSI | 0.1 Hz | yes | `--mqtt-rate-rssi=0.1` |
| Pose keypoints | **off by default**, 1 Hz when on | yes | `--mqtt-publish-pose --mqtt-rate-pose=1` |
| Zones | on change | yes | — |
### 3.8 Configuration UX — CLI + env
New CLI flags on `wifi-densepose-sensing-server` (gated behind `--mqtt`):
```
--mqtt Enable MQTT publisher (default off)
--mqtt-host <HOST> MQTT broker host (default: localhost)
--mqtt-port <PORT> MQTT broker port (default: 1883, 8883 if --mqtt-tls)
--mqtt-username <USER> MQTT username
--mqtt-password-env <ENVVAR> Read password from env var (default: MQTT_PASSWORD)
--mqtt-client-id <ID> Client ID (default: wifi-densepose-<hostname>)
--mqtt-prefix <PREFIX> Discovery prefix (default: homeassistant)
--mqtt-tls Enable TLS (default off)
--mqtt-ca-file <PATH> CA bundle (default: system trust)
--mqtt-client-cert <PATH> Client cert for mTLS
--mqtt-client-key <PATH> Client key for mTLS
--mqtt-refresh-secs <N> Discovery refresh interval (default: 600)
--mqtt-rate-vitals <HZ> Vitals publish rate (default: 0.2)
--mqtt-rate-motion <HZ> Motion publish rate (default: 1.0)
--mqtt-rate-count <HZ> Person count publish rate (default: 1.0)
--mqtt-rate-rssi <HZ> RSSI publish rate (default: 0.1)
--mqtt-publish-pose Publish pose keypoints (default off)
--mqtt-rate-pose <HZ> Pose publish rate when enabled (default: 1.0)
--privacy-mode Strip biometrics (HR/BR/pose) before publish
```
Env var equivalents follow `RUVIEW_MQTT_HOST`, `RUVIEW_MQTT_USERNAME`, etc., so Docker / systemd users don't have to wire long arg lists. Configuration is loaded in the order: CLI > env > defaults.
### 3.9 TLS + auth
- **Recommended**: mTLS on a dedicated VLAN with the broker pinned to a CA we issue per Cognitum Seed appliance.
- **Acceptable**: username + password over TLS to a public broker (e.g. user's existing Mosquitto add-on inside HA).
- **Rejected**: plaintext on any network shared with non-trusted devices. Sensing-server logs a `WARN` if `--mqtt` is enabled without `--mqtt-tls` and the broker is not `localhost`.
### 3.10 Privacy mode
`--privacy-mode` strips biometric + biometric-derivable channels before any MQTT publish, regardless of subscriber. Discovery messages for those entities are **never published** in this mode (HA never sees them exist).
| Channel | Default | `--privacy-mode` |
|---|---|---|
| Presence | published | **published** |
| Person count | published | **published** |
| Motion level | published | **published** |
| Zone occupancy | published | **published** |
| RSSI | published | **published** |
| Breathing rate | published | **stripped** |
| Heart rate | published | **stripped** |
| Fall events | published | **published** (safety > privacy) |
| Pose keypoints | off by default | **stripped** (cannot be force-enabled) |
This implements the ADR-106 primitive-isolation contract at the integration boundary: HR / BR / pose are biometric-class signals and must not leak to an unconstrained MQTT broker without explicit operator opt-in.
### 3.11 Matter Bridge (HA-FABRIC)
The Matter path runs **in the same `wifi-densepose-sensing-server` process** behind a `--matter` feature flag, gated independently of `--mqtt`. The bridge presents itself to Matter controllers as a **Bridged Devices Aggregator** (per Matter Core Spec §9.13) with one Bridged Device endpoint per RuView node, exposing the standardised subset of capabilities. Biometrics and pose are **not exposed** over Matter — they have no spec-defined clusters and cannot be soundly represented (covering them in `Generic Sensor` would force every controller to render them as nameless numbers).
#### 3.11.1 Matter device-type mapping
| RuView capability | Matter cluster | Endpoint device type | Source field |
|---|---|---|---|
| Presence | `OccupancySensing` (0x0406) | `OccupancySensor` (0x0107) | `edge_vitals.presence` |
| Motion (boolean above threshold) | `OccupancySensing` (0x0406) | (same endpoint) | `edge_vitals.motion > 0.1` |
| Fall event | `Switch` (0x003B) `MultiPressComplete` event | `GenericSwitch` (0x000F) | `edge_vitals.fall_detected` (one momentary press = one fall) |
| Person count | `OccupancySensing` extension attribute (vendor-specific 0xFFF1_0001) | (same endpoint) | `edge_vitals.n_persons` |
| Zone occupancy | one `OccupancySensor` endpoint per zone | (multiple endpoints) | `sensing_update.zones[*]` |
| RSSI / motion energy / presence score / breathing rate / heart rate / pose | **not exposed over Matter** | — | (MQTT only) |
The vendor-specific person-count attribute uses RuView's CSA-assigned vendor ID (open question §9.9). Controllers that don't understand the vendor extension still see the standard `OccupancySensing.Occupancy` boolean — graceful degradation.
#### 3.11.2 Commissioning + fabric model
- **Commissioning over WiFi**: the bridge prints a Matter setup code (11-digit short code + QR string) to logs and to `--matter-setup-file <PATH>` on first start. User scans with Apple Home / Google Home / HA Matter integration.
- **No Thread radio required**: sensing-server runs on hosts (Pi 5, x86, Cognitum Seed) that have WiFi but no 802.15.4. Matter-over-WiFi is sufficient. Thread support is explicitly out of scope until ESP32-C6 firmware grows a Matter stack (separate ADR; see §7).
- **Multi-admin / multi-fabric**: the bridge accepts multiple commissioning sessions so a single node can be paired into Apple Home **and** Home Assistant **and** Google Home concurrently — Matter's `OperationalCredentials` cluster handles fabric isolation.
- **Resetting commissioning**: a `--matter-reset` CLI flag wipes stored fabric credentials so a node can be repaired against a new controller.
#### 3.11.3 SDK choice (open in §9, sketched here)
Three viable Rust paths:
| Option | Pros | Cons |
|---|---|---|
| **`matter-rs`** (project-chip/rs-matter) — pure-Rust SDK | No FFI, no C++ build chain, fits our Rust-only crate policy, MIT-licensed | Less mature than C++ chip-tool; certification path less proven |
| **`project-chip/connectedhomeip`** via Rust FFI bindings | Reference implementation, every controller tested against it, certification-ready | Drags in CMake, C++ toolchain, ~50 MB of vendored code; clashes with our cargo-first build |
| **External Matter bridge process** (separate ESPHome-like daemon) | Decouples Rust crate from Matter SDK churn | Operational complexity; two processes to deploy |
**Tentative**: `matter-rs` for v0.7.0 ship; fall back to chip-tool-FFI if cert blockers emerge. Final decision deferred to P7 spike.
#### 3.11.4 Limitations to document upfront
These are **deliberate**, not bugs — users must see them in `docs/integrations/matter.md` before pairing:
- **No HR, BR, pose, RSSI over Matter.** Matter has no clusters for these. Use MQTT for biometric / detailed telemetry.
- **Fall events are one-shot.** A fall fires a momentary switch press; controllers must subscribe to the event (most do).
- **Person count is vendor-extension.** Apple Home / Google Home will show occupancy on/off; only HA and SmartThings (with custom handlers) will surface the count.
- **One fabric controller is "primary."** Automations split across fabrics can race; users should keep heavy automation logic in one controller (typically HA).
- **No video / image data ever.** Matter spec forbids it on these device types and we wouldn't expose it anyway.
#### 3.11.5 Why this is "Works with HA" *and* "Works with everything else"
A node paired into HA shows up in **two** ways:
- as a set of MQTT entities (HA-DISCO path) with full telemetry
- as a Matter device under HA's Matter integration with the standard subset
HA dedupes by `unique_id` (we set both paths' IDs to `wifi_densepose_<node_id>_<entity>`), so users don't see ghost devices. The Matter device is the one Apple Home or Google Home will see if the user also pairs into those — same physical node, three controllers, no duplication. This is the architectural reason for adopting both protocols rather than picking one.
### 3.12 Semantic automation primitives (HA-MIND)
Raw signals are not the product. Customers don't want to *write a Node-RED flow that thresholds breathing rate at night to infer sleep*. They want a `binary_sensor.bedroom_someone_sleeping` they can wire directly into a "dim hallway light at 10 % if anyone's asleep" automation. Same for fall *risk*, distress, room activity, elderly inactivity, meeting-in-progress, bathroom occupancy. This is the inference layer that turns RuView from "RF sensing" into **ambient intelligence infrastructure** — and it has to ship as first-class HA entities and Matter events, not as a developer SDK.
#### 3.12.1 Catalog of inferred primitives (v1)
Each primitive is a fused state derived from one or more raw channels with a small finite-state machine. Inference runs inside `wifi-densepose-sensing-server` (same place MQTT publication runs), gated behind `--semantic` (default on; can be disabled). Each primitive has a confidence score and an explanation field so HA users can debug why it fired.
| Primitive | Inputs (raw) | Output kind | Default true-condition | Hysteresis / refractory |
|---|---|---|---|---|
| **Someone sleeping** | presence + low motion (<5 % for ≥300 s) + breathing rate 820 bpm + low HR variability | `binary_sensor` (occupancy) | all conditions hold simultaneously | enters after 5 min; exits when motion > 15 % for ≥30 s |
| **Possible distress** | sustained elevated HR (>1.5× rolling baseline for ≥60 s) + agitated motion + no fall | `binary_sensor` (problem) + `event` | confidence ≥ 0.75 | latch for 5 min after exit |
| **Room active** | presence + motion > 10 % for ≥30 s in any 5-min window | `binary_sensor` (occupancy) | window-rolling | exits on 10 min idle |
| **Elderly inactivity anomaly** | no motion + presence stable for > N× rolling daily median idle (default 2×) | `binary_sensor` (problem) + `event` | model-personalised | per-resident baseline; alerts max 1×/day |
| **Meeting in progress** | person count ≥ 2 + sustained low-amplitude motion (sitting) + speech-band micro-motion if `speech_band` cog installed | `binary_sensor` (occupancy) | ≥2 ppl + ≥10 min | exits when person count < 2 for 2 min |
| **Bathroom occupied** | presence true in zone tagged `bathroom` | `binary_sensor` (occupancy) | zone+presence | privacy-mode keeps this enabled (it's not biometric) |
| **Fall risk elevated** | recent near-fall (sharp acceleration without confirmed fall) OR gait instability score > threshold | `sensor` (0100) + `event` on threshold cross | model-derived | 24-hour window |
| **Bed exit (overnight)** | "someone sleeping" → presence transitions out of bed-tagged zone between 22:0006:00 local | `event` | edge-triggered | one event per exit |
| **No movement (safety check)** | presence true + motion < 1 % for ≥ N minutes (default 30) | `binary_sensor` (problem) + `event` | duration threshold | clears on motion |
| **Multi-room transition** | track_id continuous across zones within 10 s | `event` (`who_went_from_to`) | edge-triggered | per-track event |
Catalog v2 (deferred): "child playing", "pet vs human", "agitation gradient", "circadian phase". Owned by an ADR-1xx follow-on after the v1 primitives have field data.
#### 3.12.2 Surface mapping across the three layers
| Layer | How a semantic primitive shows up |
|---|---|
| **MQTT (HA-DISCO)** | New topic namespace `homeassistant/binary_sensor/wifi_densepose_<node>/<primitive>/` and `homeassistant/event/wifi_densepose_<node>/<primitive>/` — full discovery payloads including the explanation field as `json_attributes` |
| **Matter (HA-FABRIC)** | Standard cluster mappings: sleeping/active/meeting/bathroom → `OccupancySensing` (separate endpoints); distress/inactivity/no-movement/bed-exit/fall-risk-cross → `Switch.MultiPressComplete` events on dedicated `GenericSwitch` endpoints; fall-risk score → vendor-extension attribute on the bridge endpoint |
| **Home Assistant automations** | Ship 8 starter blueprints in P5: "Notify on possible distress", "Wake-up routine on bed exit", "Dim hallway on someone sleeping", "Alert on elderly inactivity anomaly", "Lights on for meeting in progress", "Bathroom fan on while occupied", "Escalate on fall risk crossing 70", "Auto-arm security when room not active" |
| **Apple Home scenes** | Each `OccupancySensor` endpoint and each `GenericSwitch` event triggers Apple Home scenes via Matter — user picks "When *bedroom someone sleeping* is on, run *night mode*" from the Apple Home UI directly. No HA required for this path |
#### 3.12.3 Why these specific primitives
These eight cover the **top automation requests from the smart-home market** without needing video or wearables:
- **Healthcare / aging-in-place** — "elderly inactivity anomaly", "fall risk elevated", "possible distress", "no movement (safety check)", "bed exit (overnight)" — directly map to AAL (Active and Assisted Living) device-class expectations
- **Convenience automation** — "someone sleeping", "room active", "meeting in progress", "bathroom occupied" — the four highest-volume HA forum-requested binary states
- **Privacy** — none of these require biometric *values* to be published, only the inferred *states*. A `--privacy-mode` deployment can keep semantic primitives ON and still strip HR/BR/pose, because the inference happens server-side and only the state crosses the wire
#### 3.12.4 Inference quality contract
Each primitive ships with:
- A **published precision/recall** on a held-out test set built from ADR-079 paired captures + synthetic stress scenarios — committed to `docs/integrations/semantic-primitives-metrics.md`
- An **explainability payload**: every state change carries `reason: ["motion<5%", "br=12bpm", "presence=true"]` style attributes so HA users can debug
- A **confidence threshold**: per-primitive, user-tuneable via `--semantic-threshold-<primitive>=<float>` (default published in the metrics doc)
- A **suppression contract**: primitives never fire during the first 60 s after sensing-server start (warmup), and never during `csi_calibration_in_progress` states (per ADR-014)
#### 3.12.5 Configuration
```
--semantic Enable inference layer (default: on)
--semantic-thresholds-file <PATH> Per-primitive thresholds (defaults shipped)
--semantic-zones-file <PATH> Zone-tag map (e.g. {"bathroom": ["zone_3"]})
--semantic-baseline-window-days <N> Days of history for personalised baselines (default: 14)
--no-semantic-<primitive> Disable a specific primitive (repeatable)
```
#### 3.12.6 What this changes architecturally
Inference lives in a new module `semantic_inference.rs` alongside `mqtt_publisher.rs` and `matter_bridge.rs`. It subscribes to the same `tokio::broadcast` channel everything else does, runs each primitive's FSM, and emits **two output streams**:
1. A `SemanticState` event on a new broadcast channel that MQTT and Matter publishers both subscribe to (so the same inference drives both surfaces without duplication)
2. Append-only `semantic_events.jsonl` log under `--data-dir` for offline analysis + ADR-079 paired-capture supervision
This means: **adding a new primitive is one file change**. No MQTT schema rev, no Matter cluster rev — just add the FSM, register it, and discovery/state publish flow through both surfaces automatically.
---
## 4. Implementation phases
| Phase | Scope | Status |
|---|---|---|
| **P1** | Add `mqtt` feature flag to `wifi-densepose-sensing-server` Cargo.toml (depends on `rumqttc = "0.24"`). Wire CLI flags (§3.8) into `cli.rs`. No publishing yet, just config plumbing + unit tests on flag parsing. | pending |
| **P2** | HA discovery message emitter. New module `mqtt_discovery.rs`. Emits all entity `config` topics on connect + every `--mqtt-refresh-secs`. Schema-validated against HA's published JSON schema. | pending |
| **P3** | State publication. Subscribe to internal `tokio::broadcast` channel (the one `tx.send(json)` writes to on line 3983 of `main.rs`). Translate `edge_vitals` / `sensing_update` / `pose_data` messages into per-entity state payloads. Apply rate-limit + privacy-mode filters. | pending |
| **P4** | Integration tests: dockerised mosquitto in CI (extend `.github/workflows/firmware-qemu.yml` pattern), schema-validate every emitted config against HA's `homeassistant/components/mqtt` JSON schemas (pin to a tested HA version). Add a smoke test that brings up sensing-server in `--source mock --mqtt`, subscribes with `paho-mqtt` test client, asserts on entity creation. | pending |
| **P4.5** | **Semantic inference layer (HA-MIND).** New module `semantic_inference.rs` implementing the 10 v1 primitives from §3.12. Output broadcast channel consumed by both MQTT publisher (P3) and Matter bridge (P8). Per-primitive precision/recall baselines published to `docs/integrations/semantic-primitives-metrics.md`. Unit tests per FSM + integration tests via replay of ADR-079 paired captures. | pending |
| **P5** | Docs: new `docs/integrations/home-assistant.md` with screenshots of the HA UI after auto-discovery completes, example HA dashboard YAML (Lovelace card configs), 8 starter blueprints from §3.12.2 (distress notify, wake routine, hallway dim, elderly anomaly alert, meeting lights, bathroom fan, fall-risk escalate, auto-arm security), and the raw-channel example automations: "turn on hall light when presence ON", "send notification on fall_detected event", "log HR/BR to InfluxDB". | pending |
| **P6** | Ship `--mqtt` in the next sensing-server release (target: v0.7.0). Demo end-to-end on `cognitum-v0` against a Mosquitto add-on running on a Home Assistant OS install. Update README hardware-options table with "Works with Home Assistant" badge. | pending |
| **P7** | Matter Bridge spike: build a throwaway prototype with `matter-rs` exposing one `OccupancySensor` endpoint + one `GenericSwitch` for fall. Pair against Apple Home, Google Home, and HA's Matter integration. Decision gate: if pairing works on all three, proceed to P8; if blocked, switch to chip-tool FFI and re-spike. | pending |
| **P8** | Matter Bridge production. Implement `--matter`, `--matter-setup-file`, `--matter-reset`, `--matter-vendor-id`, `--matter-product-id` CLI flags. Aggregator + Bridged Devices for all RuView nodes; per-zone occupancy endpoints; fall as `MultiPressComplete` event; person count as vendor-extension attribute. Integration tests via chip-tool sim. | pending |
| **P9** | Multi-controller validation. Pair one Cognitum Seed + 3 child ESP32 nodes simultaneously into HA, Apple Home, and Google Home. Verify presence flips on all three within 1 s of a real motion change. Document the multi-admin flow in `docs/integrations/matter.md`. | pending |
| **P10** | CSA Matter certification path (optional, ADR-1xx follow-up). Decide cost vs marketing value of the official "Matter-certified" badge ($3 k/year CSA membership + per-product test fees). Sketch only — production decision deferred. | pending |
Each phase ends with a checkbox PR. The ADR is updated with actual artifacts (commit hashes, screenshots, witness bundle entries) as phases land. **P1P6 (MQTT) and P7P10 (Matter) run in parallel after P6 lands** — they share no code, so a Matter regression cannot break the MQTT path and vice versa.
---
## 5. Consequences
### 5.1 Wins
- Zero-code UX for HA users — discovery handles the entire onboarding.
- **Cross-ecosystem reach via Matter** — Apple Home / Google Home / Alexa / SmartThings users can adopt RuView without ever running HA, expanding our addressable market by ~4×.
- Decouples RuView from its own UI; users can build their own dashboards in HA / Grafana / Node-RED on the same MQTT firehose.
- Adds a `--privacy-mode` flag that gives operators a single-knob biometric strip for compliance contexts.
- Matter fabric isolation is a privacy win by construction — biometrics are out-of-spec for the exposed clusters, so a buggy controller can't accidentally exfiltrate them.
- Webhook + future HACS path stay open (§6) — no lock-in.
- Establishes our presence in the HA ecosystem AND the broader Matter ecosystem (community add-on lists, blueprints, forum recipes, App Store / Play Store visibility via Apple Home / Google Home device listings).
### 5.2 Costs
- New runtime dependency (`rumqttc`) in `wifi-densepose-sensing-server`. Mitigated by feature-flag (`mqtt`), default off; users who don't enable `--mqtt` pay zero binary or runtime cost.
- **Matter SDK dependency** (`matter-rs` tentatively) gated behind `--matter` feature flag. Adds ~5 MB to release binary when enabled; zero cost when disabled. Tracking CSA spec churn is a real ongoing cost.
- One more thing to maintain across HA breaking changes. HA commits to the `homeassistant/<component>/.../config` schema being stable (their published policy), but historically they have evolved fields like `availability_topic``availability` (list-of). We'll pin to a tested HA version per release and call out tested-against in `docs/integrations/home-assistant.md`.
- **Matter spec churn** — Matter 1.0 → 1.3 added device types and changed cluster IDs. We pin to a tested Matter spec version per release. Annual re-validation overhead.
- Requires CI infra: a mosquitto container in workflow, schema-validation against HA schemas, **and** a chip-tool simulator for Matter pairing tests (need to vendor or fetch).
- CSA membership ($3 k/year) is required to obtain a permanent vendor ID; until then we use the development VID `0xFFF1`. Production deployment past P9 requires the membership decision (§9.9).
### 5.3 Verification
Acceptance criteria are §8. Beyond those, this ADR is "Accepted" once P6 ships and at least one external user has reported a working HA install via the public issue tracker.
---
## 6. Alternatives considered
### 6.A Custom HA integration (HACS) — *follow-on, not primary*
Rough sketch:
- Separate Python repo (proposed name: `ruvnet/hass-wifi-densepose`).
- Talks to sensing-server's existing WebSocket at `/ws/sensing` and REST at `/api/*`.
- Config-flow UI in HA: user enters server URL + bearer token; integration discovers entities.
- Distribution via HACS (https://hacs.xyz), requires HACS review + acceptance.
**Effort estimate:** ~46 weeks (vs ~2 weeks for §2 MQTT path). Adds a Python codebase to maintain in a Rust-first org. Pays off in two scenarios:
1. Users who run HA but don't run an MQTT broker (rare but exists).
2. Users who want sensing-server features that don't map cleanly to MQTT (e.g. live pose video preview).
**Plan:** revisit after P6 lands and we have real adoption data on the MQTT path. If MQTT covers 80%+ of installs, HACS becomes a nice-to-have. If not, it becomes ADR-1xx follow-up.
### 6.B Local-push REST webhook — *rejected*
- sensing-server `POST`s to HA's webhook endpoint (`/api/webhook/<id>`).
- Trivial to implement (~2 days).
Rejected because:
- One-way only — no `set_state` / arm / disarm path back.
- No entity discovery — user has to manually create input_booleans / sensors / template_sensors in HA YAML.
- No availability / LWT — sensing-server going offline is invisible to HA.
- Fails the "plug-and-play" bar that #574 / #760 set.
Documented here so future readers know we considered it.
### 6.C mDNS discovery (#574) — *complementary, not competing*
mDNS / Zeroconf lets HA (or any local client) discover sensing-server's IP without manual configuration. It's orthogonal to MQTT: we should add it (already tracked in #574) so the user doesn't have to type the broker host either. mDNS resolves *where the broker is*; MQTT auto-discovery resolves *what entities to create*. Both ship; neither blocks the other.
---
## 7. Risks
| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
| Topic-namespace collision with another HA device | low | medium | `unique_id` includes `wifi_densepose_` prefix + MAC-derived node_id; HA will refuse duplicates and log clearly |
| HA changes the `homeassistant/` schema | medium (1× every ~2 years historically) | medium | Pin tested HA version in `docs/integrations/home-assistant.md`; CI runs schema validation against the pinned version |
| Bandwidth blowup from pose keypoints | medium | low (LAN) / high (metered link) | Pose publishing is **off by default**; rate-limited when on; users hit a clear `WARN` if they enable pose without explicit rate cap |
| Privacy regression — biometrics leaked to a public broker | medium | high | `--privacy-mode` strips them at source; WARN if `--mqtt` enabled without `--mqtt-tls` on a non-localhost broker; never publish HR / BR / pose discovery in privacy mode |
| Cognitum Seed firmware footprint (if we ever push MQTT into the ESP32 path) | low | medium | Out of scope for this ADR — MQTT lives in sensing-server only. ESP32 keeps the lean UDP/WS path. If we later add MQTT to firmware, it's ADR-1xx with its own size budget per ADR-110 |
| Broker compromise (bad actor on the network gets read access to MQTT) | low | high | mTLS recommendation in §3.9; `--privacy-mode` for high-risk deployments |
| HA-side cardinality explosion from per-track-id binary_sensors | medium | low | Cap dynamic person entities at 10; old ones are removed via discovery `payload=""` (HA delete-entity convention) |
| **Matter SDK (`matter-rs`) immaturity blocks cert** | medium | medium | P7 spike validates pairing on three controllers before P8 production work; fall back to chip-tool FFI if blocked |
| **Matter spec adds vitals device types**, our vendor-extension attributes become non-standard | low (3+ years out) | low | Vendor-extension attributes are opt-in for controllers; migration to standard cluster IDs is a one-version bump when the spec lands |
| **Multi-fabric races** (HA, Apple, Google all see the same node and fire conflicting automations) | medium | medium | Document the multi-admin guidance in `docs/integrations/matter.md`: pick one primary controller for automations, others for visibility |
| **Apple Home / Google Home rendering misrepresents** RuView (e.g. shows generic "Sensor") | medium | low | Set rich `VendorName` / `ProductName` / `ProductLabel` in BasicInformation cluster; ship a Matter App icon (per CSA brand guidelines) once vendor ID is real |
| **CSA membership cost** ($3 k/y) is a recurring spend with uncertain ROI | low (decision deferred to P10) | medium | Ship using dev VID `0xFFF1` through P9; commit to membership only after adoption data justifies it |
---
## 8. Acceptance criteria
A reviewer can run all of the following without modifying source:
```bash
# 1. Start sensing-server with mock source + MQTT
cargo run -p wifi-densepose-sensing-server -- \
--source mock \
--mqtt \
--mqtt-host localhost \
--mqtt-prefix homeassistant
# 2. Observe discovery + state messages
mosquitto_sub -t 'homeassistant/#' -v
# Expected: discovery configs for presence, heart_rate, breathing_rate, motion,
# fall, person_count, rssi — one per entity per node — plus periodic state messages
# 3. Run the full workspace test suite
cd v2 && cargo test --workspace --no-default-features
# Expected: 1,031+ tests passed, 0 failed (new mqtt tests included)
# 4. Schema-validate discovery configs against HA's published schemas
cargo test -p wifi-densepose-sensing-server --features mqtt mqtt::discovery::schema
# Expected: green
# 5. Privacy mode strips biometrics
cargo run -p wifi-densepose-sensing-server -- --source mock --mqtt --privacy-mode &
mosquitto_sub -t 'homeassistant/#' -v | tee /tmp/privacy.log
# Expected: NO heart_rate, breathing_rate, or pose entities in discovery
grep -E "(heart_rate|breathing_rate|pose)" /tmp/privacy.log
# Expected: empty (exit 1)
# 6. HA auto-discovery end-to-end (manual, post-P5)
# - Add Mosquitto broker to a fresh HA OS install
# - Add MQTT integration in HA, point at broker
# - Start sensing-server with --mqtt
# - HA Settings → Devices → expect "RuView node <mac>" with all entities
# - Trigger mock presence change; presence entity flips ON / OFF live
# 7. LWT / availability
# - Run sensing-server, observe `online` published
# - Kill sensing-server (-9), wait 30 s
# - Expect `offline` on every entity's availability topic
# 8. Matter Bridge pairing (post-P7)
cargo run -p wifi-densepose-sensing-server -- \
--source mock \
--matter \
--matter-setup-file /tmp/matter-qr.txt
# Expected: setup code + QR string printed; bridge advertises over mDNS
# 9. Matter cross-controller test (post-P9; manual)
# - Pair the bridge into Apple Home (scan QR with iPhone)
# - Pair the same bridge into Home Assistant Matter integration (same QR)
# - Trigger mock presence change in sensing-server
# - Expected: occupancy entity flips ON in both controllers within 1 s
# 10. Matter privacy invariant
mosquitto_sub -t 'homeassistant/sensor/+/heart_rate/state' -v &
chip-tool occupancysensing read occupancy 0xDEADBEEF 1 # Matter endpoint 1
# Expected: MQTT still publishes HR (without --privacy-mode); Matter NEVER exposes HR cluster (no clusters exist for it)
```
All ten must pass before the ADR moves from Proposed → Accepted. Tests 17 cover MQTT (P1P6); tests 810 cover Matter (P7P9). Tests can be re-run incrementally as each phase lands.
---
## 9. Resolved decisions (maintainer ACK 2026-05-23)
All 13 questions resolved by maintainer @ruv on 2026-05-23. Status: **ACCEPTED**.
**Decision principle (canonical):** preserve clean protocols, avoid firmware bloat, avoid fake semantics, ship MQTT first, validate Matter second.
### 9.A MQTT path (P1P6)
1. **Broker.****Mosquitto as default.** Mention EMQX and VerneMQ as advanced options in `docs/integrations/home-assistant.md`.
2. **Discovery prefix.****Ship `homeassistant`** (HA's default). `--mqtt-prefix` remains overridable for users with custom HA setups.
3. **HACS repo name.****`ruvnet/hass-wifi-densepose`** — wired into the `support_url` field of every discovery payload's `origin` block from P1.
4. **Sample blueprints.****Ship 3 starter blueprints in P5.** Selected from §3.12.2 list — final three picked at P5 start, biased toward highest customer-pull primitives.
5. **TLS default.****WARN now, hard-fail non-localhost plaintext in v0.8.0.** Sensing-server logs a `WARN` if `--mqtt` enabled without `--mqtt-tls` on a non-localhost broker. v0.8.0 promotes to hard fail (exit non-zero) once docs cover the CA setup path.
6. **`node_friendly_name`.** ✅ **NVS / config only.** No ADR-039 packet change. Sensing-server resolves the friendly name from local config and injects into MQTT/Matter device labels.
7. **Pose keypoint schema.****COCO 17-keypoint order.** Index → joint name mapping documented in `docs/integrations/home-assistant.md` and re-exported as `wifi_densepose_core::pose::COCO17`.
8. **Multi-node aggregation.****4 children + 1 parent via `via_device`.** Easier to debug; matches §3.4.
### 9.B Matter path (P7P10)
9. **Matter vendor ID.****Dev VID `0xFFF1` through P9.** CSA membership decision gate at P10 (deferred; sketched only).
10. **Matter SDK.****Start with `matter-rs`.** Fall back to chip-tool FFI only if cert blockers emerge in P7 spike.
11. **Matter Thread.****Future ADR.** ADR-115 stays WiFi-only on the server side. Thread support from ESP32-C6 firmware is a separate ADR after C6 stabilises (post-ADR-110 P8).
12. **Fall event mapping.****`Switch.MultiPressComplete`.** Cleaner semantics for controllers; matches Apple Home / Google Home rendering expectations.
13. **Person count.****Vendor extension.** Do not kludge into fake endpoints. Apple Home / Google Home will show `Occupancy: ON/OFF` only — that's honest. HA and SmartThings will surface the count via the vendor-extension attribute.
### 9.C Open-after-9 (new questions raised post-ACK)
Empty as of 2026-05-23. New questions discovered during implementation will be filed here, ACK'd by maintainer, and dated.
---
## 10. References
- Home Assistant MQTT integration docs: https://www.home-assistant.io/integrations/mqtt/
- HA MQTT auto-discovery: https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery
- HA discovery schemas (per-component): https://www.home-assistant.io/integrations/binary_sensor.mqtt/ , .../sensor.mqtt/ , .../event.mqtt/
- HACS: https://hacs.xyz
- HA Blueprint format: https://www.home-assistant.io/docs/blueprint/schema/
- `rumqttc` (chosen Rust MQTT client): https://docs.rs/rumqttc/
- **Matter Core Spec 1.3** (CSA): https://csa-iot.org/all-solutions/matter/
- **Matter Device Library** (cluster + device-type catalog): https://csa-iot.org/wp-content/uploads/2023/12/Matter-1.3-Device-Library-Specification.pdf
- **matter-rs** (pure-Rust Matter SDK): https://github.com/project-chip/rs-matter
- **project-chip/connectedhomeip** (reference C++ Matter SDK / chip-tool): https://github.com/project-chip/connectedhomeip
- **Home Assistant Matter integration**: https://www.home-assistant.io/integrations/matter/
- **Apple Home Matter support**: https://support.apple.com/en-us/HT213267
- **Google Home Matter support**: https://developers.home.google.com/matter
- **CSA membership / vendor ID program**: https://csa-iot.org/become-member/
- **"Works with Home Assistant" certification**: https://partner.home-assistant.io/
- RuView ADR-018 — CSI binary frame format
- RuView ADR-021 — ESP32 vitals (edge breathing/HR extraction)
- RuView ADR-028 — ESP32 capability audit
- RuView ADR-031 — RuView sensing-first RF mode
- RuView ADR-039 — Edge vitals packet (`0xC511_0002`)
- RuView ADR-079 — Camera ground-truth training (pose schema)
- RuView ADR-103 — `cog-person-count` (person count primitive)
- RuView ADR-106 — DP-SGD + primitive isolation (privacy contract)
- RuView ADR-110 — ESP32-C6 firmware extension
- RuView ADR-114 — `cog-quantum-vitals`
- Issue [#574](https://github.com/ruvnet/RuView/issues/574) — mDNS for seed_url (complementary)
- Issue [#760](https://github.com/ruvnet/RuView/issues/760) — Sensing UI / onboarding friction
- Issue [#761](https://github.com/ruvnet/RuView/issues/761) — Competitive scan (espectre.dev, tommysense.com)
---
*ADR-115 is the integration story that turns RuView from "another sensing platform" into "drop-in upgrade for any HA install **and** any Matter-controller home." MQTT carries the rich, differentiated telemetry; Matter carries the standardised subset across every controller ecosystem. Numbers 111 and 112 remain reserved per the project ADR-numbering policy.*
+1 -1
View File
@@ -50,7 +50,7 @@ Statuses: **Proposed** (under discussion), **Accepted** (approved and/or impleme
| [ADR-040](ADR-040-wasm-programmable-sensing.md) | WASM Programmable Sensing (Tier 3) | Accepted |
| [ADR-041](ADR-041-wasm-module-collection.md) | WASM Module Collection (65 edge modules) | Accepted (hardware-validated) |
| [ADR-044](ADR-044-provisioning-tool-enhancements.md) | Provisioning Tool Enhancements | Proposed |
| [ADR-110](ADR-110-esp32-c6-firmware-extension.md) | ESP32-C6 firmware extension — Wi-Fi 6 / 802.15.4 / TWT / LP-core | Accepted, P1-P10 complete, firmware-side substrate closed at **[v0.7.0-esp32](https://github.com/ruvnet/RuView/releases/tag/v0.7.0-esp32)**. Companion docs: [`WITNESS-LOG-110`](../WITNESS-LOG-110.md) (13 §A0.x entries · 99.56 % cross-board RX · **104.1 µs smoothed sync stdev** · ≤100 µs target met), [`ADR-110-REVIEW-GUIDE`](../ADR-110-REVIEW-GUIDE.md) (one-page reviewer tour), [`ADR-110-BRANCH-STATE`](../ADR-110-BRANCH-STATE.md) (coordination map vs `feat/adr-115-ha-mqtt-matter`). Host decoders + tests: Python `SyncPacketParser` (10) + Rust `wifi_densepose_hardware::SyncPacket` (15), cross-language hex pin gates drift. |
| [ADR-110](ADR-110-esp32-c6-firmware-extension.md) | ESP32-C6 firmware extension — Wi-Fi 6 / 802.15.4 / TWT / LP-core | Accepted (firmware shipped, live capture hardware-blocked — see [`WITNESS-LOG-110`](../WITNESS-LOG-110.md)) |
### Signal processing and sensing
+1 -131
View File
@@ -473,72 +473,6 @@ Base URL: `http://localhost:3000` (Docker) or `http://localhost:8080` (binary de
| `POST` | `/api/v1/adaptive/train` | Train adaptive classifier from recordings | `{"success":true,"accuracy":0.85}` |
| `GET` | `/api/v1/adaptive/status` | Adaptive model status and accuracy | `{"loaded":true,"accuracy":0.85}` |
| `POST` | `/api/v1/adaptive/unload` | Unload adaptive model | `{"success":true}` |
| `GET` | `/api/v1/mesh` | ADR-110 fleet-wide mesh sync map ([iter 29](adr/ADR-110-esp32-c6-firmware-extension.md)) | `{"nodes":{"9":{...},"12":{...}},"total":2}` |
| `GET` | `/api/v1/nodes/:id/sync` | Single-node mesh sync snapshot (or 404) | `{"offset_us":1163565,"is_leader":false,...}` |
| `GET` | `/api/v1/mesh/metrics` | ADR-110 mesh state in Prometheus exposition format ([iter 36](adr/ADR-110-esp32-c6-firmware-extension.md)) | `wifi_densepose_mesh_offset_us{node="9"} 1163565\n…` |
### Example: Get fleet mesh state (ADR-110)
```bash
curl -s http://localhost:3000/api/v1/mesh | python -m json.tool
```
```json
{
"nodes": {
"9": {
"offset_us": 1163565,
"is_leader": false,
"is_valid": true,
"smoothed": true,
"sequence": 20,
"csi_fps_ema": 10.0,
"csi_fps_samples": 47
},
"12": {
"offset_us": -7,
"is_leader": true,
"is_valid": true,
"smoothed": false,
"sequence": 20,
"csi_fps_ema": 10.0,
"csi_fps_samples": 51
}
},
"total": 2
}
```
Empty `{"nodes": {}, "total": 0}` means no mesh peers reachable.
Nodes that haven't emitted a sync packet yet are omitted from the map.
### Example: Get one node's sync state
```bash
curl -s http://localhost:3000/api/v1/nodes/9/sync | python -m json.tool
```
200 → same `NodeSyncSnapshot` shape as inside `/api/v1/mesh` or the
WebSocket `sync` field. Field meanings are documented under
[Per-node mesh sync (ADR-110)](#per-node-mesh-sync-adr-110).
404 (unknown node):
```json
{"error": "unknown_node", "node_id": 99}
```
404 (node exists but hasn't synced yet):
```json
{
"error": "no_sync",
"node_id": 9,
"hint": "node hasn't emitted a sync packet yet (no mesh peer or not v0.6.9+)"
}
```
Useful for Home Assistant REST sensors, Prometheus exporters,
automation rule probes, and curl debugging — anywhere you want
one-shot mesh state without holding a WebSocket connection.
### Example: Get Vital Signs
@@ -630,67 +564,6 @@ ws.onerror = (err) => console.error("WebSocket error:", err);
wscat -c ws://localhost:3001/ws/sensing
```
### Per-node mesh sync (ADR-110)
Since firmware **v0.7.0-esp32** + sensing-server iter 23, every
`sensing_update` whose nodes participate in the [ADR-110](adr/ADR-110-esp32-c6-firmware-extension.md)
ESP-NOW mesh carries an optional `sync` object per node:
```json
{
"type": "sensing_update",
"nodes": [
{
"node_id": 9,
"rssi_dbm": -38.0,
"amplitude": [...],
"subcarrier_count": 64,
"sync": {
"offset_us": 1163565,
"is_leader": false,
"is_valid": true,
"smoothed": true,
"sequence": 20,
"csi_fps_ema": 10.0,
"csi_fps_samples": 47
}
}
]
}
```
Field meanings:
| Field | Type | Meaning |
|---|---|---|
| `offset_us` | i64 | Smoothed local-vs-mesh clock offset in microseconds. Negative when this node is behind the leader. §A0.10 on the bench measured ~1.16 s boot delta between two C6 boards. |
| `is_leader` | bool | True when this node is the elected mesh leader (lowest EUI-64 in the cohort). |
| `is_valid` | bool | True when this node has heard a fresh leader beacon within the firmware's `VALID_WINDOW_MS = 3 s` freshness gate. |
| `smoothed` | bool | True once the firmware-side EMA filter has seeded (after ~8 beacons ≈ 0.8 s of follower mode). |
| `sequence` | u32 | High-water CSI sequence number stamped when this sync packet was emitted. Pair with the per-frame `sequence` field on incoming CSI to interpolate a mesh-aligned timestamp for any frame. |
| `csi_fps_ema` | f64 | Per-node EMA of the observed CSI frame rate. Bench typical ≈ 10 Hz. |
| `csi_fps_samples` | u32 | How many inter-frame deltas the EMA has seen. Treat values < 5 as "not yet trustworthy" and fall back to 20 Hz. |
| `staleness_ms` | u64 (optional) | Milliseconds since the host last received a sync packet from this node ([iter 34](adr/ADR-110-esp32-c6-firmware-extension.md)). Fade UI badges after 5 000 ms; treat ≥ 9 000 ms as the same condition that the firmware's `c6_sync_espnow_is_valid()` reports as `false`. |
**When `sync` is omitted entirely**: the node isn't on the mesh (or
hasn't heard a peer yet). Non-ESP32 paths — multi-BSSID router scan,
synthetic-RSSI fallback, simulation — also omit `sync`. Existing
pre-iter-23 UI clients ignore the new field naturally because they
don't read it.
**How to render this in a UI**:
- `is_leader === true` → badge the node "Leader"
- `is_valid === false` → grey out / "Sync lost"
- `csi_fps_samples < 5` → label as "Calibrating" until ≥5 frames
- `|offset_us|` trend → render a jitter histogram to show the §A0.10
EMA suppression working live
**How to recover a mesh-aligned timestamp for any CSI frame from this
node**: take the frame's own `sequence` u32, subtract `sync.sequence`,
divide by `sync.csi_fps_ema` (or 20.0 if `csi_fps_samples < 5`),
multiply by 1 000 000 µs — that's the mesh delta from the sync emit
time. Use it to align multistatic frames from sibling boards.
---
## Web UI
@@ -1245,10 +1118,7 @@ Pre-built binaries are available at [Releases](https://github.com/ruvnet/RuView/
| Release | What It Includes | Tag |
|---------|-----------------|-----|
| [v0.7.0](https://github.com/ruvnet/RuView/releases/tag/v0.7.0-esp32) | **Latest — ADR-110 firmware-side substrate closed.** Adds ESP-NOW mesh substrate with quantified ≤100 µs alignment (104.1 µs smoothed stdev, 3.95× suppression, 99.56 % cross-board match measured live), 32-byte sync-packet UDP emission with operator-tunable cadence, ADR-018 byte 19 bit 4 wire-fix sourced from working ESP-NOW path, Python SyncPacketParser stub for host wiring ([WITNESS-LOG-110 §A0.7-§A0.13](WITNESS-LOG-110.md)) | `v0.7.0-esp32` |
| [v0.6.9](https://github.com/ruvnet/RuView/releases/tag/v0.6.9-esp32) | Sync-packet UDP emission, `CONFIG_C6_SYNC_EVERY_N_FRAMES` tunable cadence | `v0.6.9-esp32` |
| [v0.6.8](https://github.com/ruvnet/RuView/releases/tag/v0.6.8-esp32) | ESP-NOW EMA-smoothed cross-board offset (3.95× suppression, 104 µs stdev) | `v0.6.8-esp32` |
| [v0.6.7](https://github.com/ruvnet/RuView/releases/tag/v0.6.7-esp32) | Real LP-core motion-gate RISC-V program (B4 code path complete) + Wi-Fi 6 soft-AP with TWT Responder for two-board iTWT benches (B1/B2 unblock) | `v0.6.7-esp32` |
| [v0.6.7](https://github.com/ruvnet/RuView/releases/tag/v0.6.7-esp32) | **Latest** — real LP-core motion-gate RISC-V program (B4 code path complete) + Wi-Fi 6 soft-AP with TWT Responder for two-board iTWT benches (B1/B2 unblock), no router required. Both default off — no behavior change for v0.6.6 fleets ([ADR-110 P9](adr/ADR-110-esp32-c6-firmware-extension.md)) | `v0.6.7-esp32` |
| [v0.5.0](https://github.com/ruvnet/RuView/releases/tag/v0.5.0-esp32) | **Stable (S3 mesh, recommended)** — mmWave sensor fusion (MR60BHA2/LD2410 auto-detect), 48-byte fused vitals, all v0.4.3.1 fixes | `v0.5.0-esp32` |
| [v0.4.3.1](https://github.com/ruvnet/RuView/releases/tag/v0.4.3.1-esp32) | Fall detection fix ([#263](https://github.com/ruvnet/RuView/issues/263)), 4MB flash ([#265](https://github.com/ruvnet/RuView/issues/265)), watchdog fix ([#266](https://github.com/ruvnet/RuView/issues/266)) | `v0.4.3.1-esp32` |
| [v0.4.1](https://github.com/ruvnet/RuView/releases/tag/v0.4.1-esp32) | CSI build fix, compile guard, AMOLED display, edge intelligence ([ADR-057](../docs/adr/ADR-057-firmware-csi-build-guard.md)) | `v0.4.1-esp32` |
@@ -73,13 +73,3 @@ static mmwave_state_t s_stub_mmwave = {0};
esp_err_t mmwave_sensor_init(int tx, int rx) { (void)tx; (void)rx; return ESP_ERR_NOT_FOUND; }
bool mmwave_sensor_get_state(mmwave_state_t *s) { if (s) *s = s_stub_mmwave; return false; }
const char *mmwave_type_name(mmwave_type_t t) { (void)t; return "None"; }
/* ADR-110 iter 38 — fuzz-harness stub for c6_sync_espnow_is_valid.
* Real implementation lives in main/c6_sync_espnow.c; the fuzz target
* (`fuzz_serialize`) only links csi_collector.c against esp_stubs.c, so
* iter-11's `if (c6_sync_espnow_is_valid()) flags |= (1 << 4);` needs a
* symbol here or `clang -fsanitize=fuzzer` fails with an undefined-reference
* linker error. Returning false means the bit-4 cross-node-sync-valid flag
* stays 0 in fuzz inputs, which is the natural fuzz semantic. */
#include <stdbool.h>
bool c6_sync_espnow_is_valid(void) { return false; }
-162
View File
@@ -1,162 +0,0 @@
import pytest
import re
import os
ADVERSARIAL_PAYLOADS = [
# Null bytes and binary data
b"\x00" * 100,
b"\xff\xfe\xfd",
b"\x00\x01\x02\x03",
# Oversized inputs
b"A" * 65536,
b"B" * 1048576,
# Format string attacks
b"%s%s%s%s%s%s%s%s%s%s",
b"%x%x%x%x%x%x%x%x",
b"%n%n%n%n",
# SQL injection patterns
b"' OR '1'='1",
b"'; DROP TABLE users; --",
b"1; SELECT * FROM secrets",
# Path traversal
b"../../../etc/passwd",
b"..\\..\\..\\windows\\system32",
b"/etc/shadow",
# Command injection
b"; cat /etc/passwd",
b"| ls -la",
b"`whoami`",
b"$(id)",
# Buffer overflow patterns
b"\x41" * 4096,
b"\x90" * 1024 + b"\xcc" * 100,
# Unicode/encoding attacks
"'\u0000'".encode("utf-8"),
"\uFFFD\uFFFE\uFFFF".encode("utf-8"),
# Empty and whitespace
b"",
b" ",
b"\t\n\r",
# Version string injection
b"openssl-1.0.1e",
b"openssl 1.0.1f",
b"1.0.1g",
# Malformed version strings
b"999.999.999",
b"-1.-1.-1",
b"0.0.0",
# Special characters
b"!@#$%^&*()",
b"<script>alert(1)</script>",
b"<?xml version='1.0'?><!DOCTYPE foo [<!ENTITY xxe SYSTEM 'file:///etc/passwd'>]>",
]
def parse_cargo_lock_openssl_version(content: str) -> list:
"""Extract openssl-related package versions from Cargo.lock content."""
versions = []
lines = content.split('\n')
in_openssl_package = False
current_name = None
for line in lines:
line = line.strip()
if line.startswith('name = '):
current_name = line.split('=', 1)[1].strip().strip('"')
in_openssl_package = 'openssl' in current_name.lower()
elif in_openssl_package and line.startswith('version = '):
version_str = line.split('=', 1)[1].strip().strip('"')
versions.append((current_name, version_str))
return versions
def is_safe_version_string(version_str: str) -> bool:
"""Check that a version string only contains safe characters."""
safe_pattern = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+([.\-][a-zA-Z0-9]+)*$')
return bool(safe_pattern.match(version_str))
def simulate_version_comparison(version_str: str) -> bool:
"""Simulate version comparison without executing arbitrary code."""
try:
parts = version_str.split('.')
if len(parts) < 2:
return False
for part in parts[:3]:
base = part.split('-')[0].split('+')[0]
if base:
int(base)
return True
except (ValueError, AttributeError):
return False
@pytest.mark.parametrize("payload", ADVERSARIAL_PAYLOADS)
def test_openssl_version_handling_security_invariant(payload):
"""Invariant: Adversarial inputs must not cause unsafe behavior when processed
as version strings or package metadata. Version parsing must remain safe and
predictable regardless of input content."""
# Convert payload to string safely
if isinstance(payload, bytes):
try:
payload_str = payload.decode('utf-8', errors='replace')
except Exception:
payload_str = repr(payload)
else:
payload_str = str(payload)
# Invariant 1: Version string validation must not crash
try:
is_safe = is_safe_version_string(payload_str)
# If the payload is adversarial, it should NOT be considered a safe version
if any(c in payload_str for c in [';', '|', '`', '$', '<', '>', '&', '\x00', '%n', '%s', '%x']):
assert not is_safe, (
f"Adversarial payload was incorrectly accepted as safe version: {repr(payload_str)}"
)
except Exception as e:
pytest.fail(f"Version validation raised unexpected exception for payload {repr(payload_str)}: {e}")
# Invariant 2: Version comparison simulation must not execute arbitrary code
try:
result = simulate_version_comparison(payload_str)
# Result must be a boolean - no side effects
assert isinstance(result, bool), (
f"Version comparison returned non-boolean for payload {repr(payload_str)}"
)
except Exception as e:
pytest.fail(f"Version comparison raised unexpected exception for payload {repr(payload_str)}: {e}")
# Invariant 3: Cargo.lock-like content with adversarial version must be parseable safely
fake_cargo_lock = f'''
[[package]]
name = "openssl"
version = "{payload_str}"
source = "registry+https://github.com/rust-lang/crates.io-index"
'''
try:
versions = parse_cargo_lock_openssl_version(fake_cargo_lock)
# Must return a list (even if empty or with the injected value)
assert isinstance(versions, list), (
f"Parser returned non-list for payload {repr(payload_str)}"
)
# The parser must not execute any code from the payload
for name, ver in versions:
assert isinstance(name, str), "Package name must be a string"
assert isinstance(ver, str), "Version must be a string"
except Exception as e:
pytest.fail(f"Cargo.lock parsing raised unexpected exception for payload {repr(payload_str)}: {e}")
# Invariant 4: No environment variables should be modified by processing the payload
env_before = dict(os.environ)
try:
_ = is_safe_version_string(payload_str)
_ = simulate_version_comparison(payload_str)
except Exception:
pass
env_after = dict(os.environ)
assert env_before == env_after, (
f"Environment was modified while processing payload {repr(payload_str)}"
)
+2 -12
View File
@@ -1,19 +1,9 @@
// WebSocket Client for Three.js Visualization - WiFi DensePose
// Default endpoint is `/ws/sensing` on the same host the page was served from.
// Callers (e.g. viz.html) usually pass an explicit `url` derived from
// `buildSensingWsUrl()` so HTTP/WS port pairings are handled centrally.
function _defaultWsUrl() {
if (typeof window === 'undefined' || !window.location) {
return 'ws://localhost:8765/ws/sensing';
}
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${window.location.host}/ws/sensing`;
}
// Connects to ws://localhost:8000/ws/pose and manages real-time data flow
export class WebSocketClient {
constructor(options = {}) {
this.url = options.url || _defaultWsUrl();
this.url = options.url || 'ws://localhost:8000/ws/pose';
this.ws = null;
this.state = 'disconnected'; // disconnected, connecting, connected, error
this.isRealData = false;
-2
View File
@@ -27,8 +27,6 @@ export class ToastManager {
action = null
} = options;
if (!this.container) this.init();
const id = ++this.idCounter;
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
+14 -36
View File
@@ -84,41 +84,22 @@
<div id="stats-container"></div>
</div>
<!-- Three.js r160 dropped examples/js/ UMD builds. Load via importmap and
expose THREE + OrbitControls as a mutable global so the existing
component modules (scene.js, body-model.js, …) keep working without
a wider refactor. Note: `import * as THREE` returns a frozen Module
Namespace Object — spread it into a plain object before attaching
OrbitControls, otherwise the assignment silently no-ops. -->
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.160.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/"
}
}
</script>
<!-- Three.js and OrbitControls from CDN -->
<script src="https://unpkg.com/three@0.160.0/build/three.min.js"></script>
<script src="https://unpkg.com/three@0.160.0/examples/js/controls/OrbitControls.js"></script>
<!-- Stats.js for performance monitoring -->
<script src="https://unpkg.com/stats.js@0.17.0/build/stats.min.js"></script>
<!-- All app code lives in one module so global THREE is installed before
the component modules run. Two separate module scripts would race
since each is independently async-resolved. -->
<!-- Application modules loaded as ES modules via importmap workaround -->
<script type="module">
import * as ThreeNS from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const THREE = { ...ThreeNS, OrbitControls };
window.THREE = THREE;
// Component modules use `THREE.*` as a global — must be installed first.
const { Scene } = await import('./components/scene.js');
const { BodyModel, BodyModelManager } = await import('./components/body-model.js');
const { SignalVisualization } = await import('./components/signal-viz.js');
const { Environment } = await import('./components/environment.js');
const { DashboardHUD } = await import('./components/dashboard-hud.js');
const { WebSocketClient } = await import('./services/websocket-client.js');
const { DataProcessor } = await import('./services/data-processor.js');
const { buildSensingWsUrl } = await import('./services/sensing.service.js');
// Import all modules
import { Scene } from './components/scene.js';
import { BodyModel, BodyModelManager } from './components/body-model.js';
import { SignalVisualization } from './components/signal-viz.js';
import { Environment } from './components/environment.js';
import { DashboardHUD } from './components/dashboard-hud.js';
import { WebSocketClient } from './services/websocket-client.js';
import { DataProcessor } from './services/data-processor.js';
// -- Application State --
const state = {
@@ -194,12 +175,9 @@
state.stats = initStats();
setLoadingProgress(85, 'Connecting to server...');
// 8. WebSocket client — derive URL from window.location so the page
// works on both default (HTTP 8080 / WS 8765) and Docker (3000/3001)
// port pairings. `?ws=…` query overrides for advanced setups.
const wsOverride = new URLSearchParams(window.location.search).get('ws');
// 8. WebSocket client
state.wsClient = new WebSocketClient({
url: wsOverride || buildSensingWsUrl(),
url: 'ws://localhost:8000/ws/pose',
onMessage: (msg) => handleWebSocketMessage(msg),
onStateChange: (newState, oldState) => handleConnectionStateChange(newState, oldState),
onError: (err) => console.error('[VIZ] WebSocket error:', err)
-154
View File
@@ -1,154 +0,0 @@
# cargo-audit configuration — v2 workspace
# Managed by security audit (fix/security-audit-rustsec-clippy branch).
#
# This file suppresses advisories in two categories:
# A) CVE-bearing advisories in TRANSITIVE deps we cannot upgrade directly
# because the parent published crate (ruvector-core 2.2.0) has not yet
# published a version with the fix. These are tracked as issues.
# B) UNMAINTAINED-only advisories (no CVE) flowing through dependencies
# that are purely transitive / build-time and have no user-facing attack
# surface in this workspace.
# Each entry documents the root cause and the mitigation path.
[advisories]
# ---------------------------------------------------------------------------
# GTK3 / glib / gdk* family — RUSTSEC-2024-0411..0420, RUSTSEC-2024-0429
# Reason: These crates are pulled in by wifi-densepose-desktop via Tauri v2's
# native WebView dependencies on Linux (libwebkit2gtk-4.1). They are
# flagged as unmaintained because the GTK3 Rust bindings maintainers have
# moved to GTK4. This codebase does NOT make direct use of any of the
# deprecated GTK3 APIs — the dependency is a runtime linker artifact of
# the Tauri Linux build. Tauri itself is aware of this and will migrate
# when a GTK4-based Tauri backend is stable. No CVE assigned.
# Mitigation: Accept transitively until Tauri v2 drops GTK3 or a workspace
# override path becomes available.
ignore = [
# -----------------------------------------------------------------------
# CATEGORY A — transitive CVEs from ruvector-core 2.2.0 → reqwest 0.11
# ruvector-core 2.2.0 (latest on crates.io) depends on reqwest 0.11.27,
# which pulls in rustls 0.21 / rustls-webpki 0.101.7. We cannot upgrade
# this without a new ruvector-core release. Tracked in issue #812.
# The workspace's own TLS stack uses rustls-webpki 0.103.13 (patched);
# the vulnerable 0.101.7 instance is not reachable from our TLS code.
"RUSTSEC-2026-0098", # rustls-webpki 0.101.7: URI name constraint bypass
"RUSTSEC-2026-0099", # rustls-webpki 0.101.7: wildcard name constraint bypass
"RUSTSEC-2026-0104", # rustls-webpki 0.101.7: reachable panic in CRL parsing
# quinn-proto 0.11.13 is also pulled through midstreamer-quic 0.3 (now
# upgraded). The remaining 0.11.13 instance comes from the same
# ruvector-core transitive chain. Tracked in issue #812.
"RUSTSEC-2026-0037", # quinn-proto 0.11.13: DoS in Quinn endpoints
# CRL Distribution Point matching bug — same ruvector-core / reqwest 0.11
# transitive chain; rustls-webpki 0.101.7 also affected.
"RUSTSEC-2026-0049", # rustls-webpki <0.103.10: CRL authority matching
# -----------------------------------------------------------------------
# CATEGORY B — unmaintained / no CVE
"RUSTSEC-2024-0411", # gdkwayland-sys: unmaintained
"RUSTSEC-2024-0412", # gdk: unmaintained
"RUSTSEC-2024-0413", # atk: unmaintained
"RUSTSEC-2024-0414", # gdkx11-sys: unmaintained
"RUSTSEC-2024-0415", # gtk: unmaintained
"RUSTSEC-2024-0416", # atk-sys: unmaintained
"RUSTSEC-2024-0417", # gdkx11: unmaintained
"RUSTSEC-2024-0418", # gdk-sys: unmaintained
"RUSTSEC-2024-0419", # gtk3-macros: unmaintained
"RUSTSEC-2024-0420", # gtk-sys: unmaintained
"RUSTSEC-2024-0429", # glib: unsound — same GTK3/glib binding family,
# also flagged as unmaintained; no CVE; same
# mitigation path as above.
# -----------------------------------------------------------------------
# atomic-polyfill — RUSTSEC-2023-0089
# Pulled in by embedded / WASM crates. Unmaintained (superseded by
# portable-atomic). No CVE. The wasm-edge crate is an optional build
# target excluded from `cargo test --workspace`; the polyfill is only
# used in no_std WASM contexts where native atomics are unavailable.
# Mitigation: migrate to portable-atomic once the wasm-edge crate is
# refactored (tracked in #802).
"RUSTSEC-2023-0089", # atomic-polyfill: unmaintained
# -----------------------------------------------------------------------
# bincode — RUSTSEC-2025-0141
# Unmaintained (v1 — superseded by bincode v2/v3). No CVE. Used only
# in benchmark harnesses inside criterion 0.5. No user-controlled data
# is deserialised through bincode in production paths.
# Mitigation: upgrade criterion to 0.6+ when available and stable.
"RUSTSEC-2025-0141", # bincode: unmaintained
# -----------------------------------------------------------------------
# fxhash — RUSTSEC-2025-0057
# Unmaintained (superseded by rustc-hash). No CVE. Pulled in
# transitively by candle-core / candle-nn for hash-map acceleration.
# Not used directly; no user-controlled input reaches fxhash.
# Mitigation: accept until candle-core 0.5+ drops the dep.
"RUSTSEC-2025-0057", # fxhash: unmaintained
# -----------------------------------------------------------------------
# lru — RUSTSEC-2026-0002
# Unsound: LRU eviction can trigger a use-after-free in pathological
# sequences of insertions/removals combined with raw pointer access.
# No CVE; only reachable through deliberate internal misuse. This
# workspace does not use lru directly; it is pulled in by hnsw_rs
# (via ruvector-core). The hot path (HNSW index lookups) never hits
# the vulnerable eviction sequence in practice.
# Mitigation: track hnsw_rs upgrade to lru >=0.14 (issue #809).
"RUSTSEC-2026-0002", # lru: unsound
# -----------------------------------------------------------------------
# number_prefix — RUSTSEC-2025-0119
# Unmaintained. No CVE. Pulled in by indicatif 0.17 (progress bars).
# Purely a display-side dependency; no security surface.
# Mitigation: upgrade indicatif once a version without number_prefix lands.
"RUSTSEC-2025-0119", # number_prefix: unmaintained
# -----------------------------------------------------------------------
# paste — RUSTSEC-2024-0436
# Unmaintained. No CVE. Proc-macro used at build time by napi-derive
# and CUDA bindings. No runtime exposure.
"RUSTSEC-2024-0436", # paste: unmaintained
# -----------------------------------------------------------------------
# proc-macro-error — RUSTSEC-2024-0370
# Unmaintained. No CVE. Build-time proc-macro; zero runtime exposure.
"RUSTSEC-2024-0370", # proc-macro-error: unmaintained
# -----------------------------------------------------------------------
# rand <0.9 — RUSTSEC-2026-0097
# Unsound: the rand 0.8 BlockRng64 implementation can panic and expose
# uninitialized memory under certain reseeding sequences. No CVE.
# This workspace uses rand 0.8 only through ndarray-linalg and candle
# for signal-processing RNG; it does not rely on BlockRng64 directly.
# Mitigation: migrate to rand 0.9 once ndarray-linalg 0.19+ is released
# (blocked on openblas-static update, tracked in #810).
"RUSTSEC-2026-0097", # rand <0.9: unsound
# -----------------------------------------------------------------------
# rkyv 0.8.x — RUSTSEC-2026-0122
# Unsound: potential use-after-free in InlineVec/SerVec clear paths.
# No CVE. Pulled in by ruvector-core for zero-copy serialisation of
# vector index snapshots. The affected code path requires a panic
# inside clear() which only occurs in out-of-memory conditions; the
# application handles OOM at a higher level.
# Mitigation: track rkyv 0.8.16+ fix once released (issue #811).
"RUSTSEC-2026-0122", # rkyv 0.8.x: unsound
# -----------------------------------------------------------------------
# rustls-pemfile — RUSTSEC-2025-0134
# Unmaintained. No CVE. Pulled in by reqwest 0.11 (via ruvector-core
# 2.2.0). The workspace's own TLS code uses rustls-pemfile 2.x;
# the 1.x instance is an artefact of the ruvector-core transitive dep.
# Mitigation: resolve when ruvector-core upgrades to reqwest 0.12+.
"RUSTSEC-2025-0134", # rustls-pemfile 1.x: unmaintained
# -----------------------------------------------------------------------
# unic-* family — RUSTSEC-2025-0075, -0080, -0081, -0098, -0100
# Unmaintained (superseded by icu4x). No CVE. Used by napi-derive at
# build time for Unicode identifier handling. Build-time only; no
# runtime attack surface.
"RUSTSEC-2025-0075", # unic-char-range
"RUSTSEC-2025-0080", # unic-common
"RUSTSEC-2025-0081", # unic-char-property
"RUSTSEC-2025-0098", # unic-ucd-version
"RUSTSEC-2025-0100", # unic-ucd-ident
]
Generated
+82 -34
View File
@@ -1505,7 +1505,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users 0.5.2",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -1726,7 +1726,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3134,7 +3134,7 @@ dependencies = [
"libc",
"percent-encoding",
"pin-project-lite",
"socket2 0.5.10",
"socket2 0.6.2",
"tokio",
"tower-service",
"tracing",
@@ -3395,7 +3395,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3873,13 +3873,26 @@ dependencies = [
"autocfg",
]
[[package]]
name = "midstreamer-attractor"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab86df06cf1705ca37692b4fc0027868f92e5170a7ebb1d706302f04b6044f70"
dependencies = [
"midstreamer-temporal-compare 0.1.0",
"nalgebra",
"ndarray 0.16.1",
"serde",
"thiserror 2.0.18",
]
[[package]]
name = "midstreamer-attractor"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bebe548a4e74b80ecb8dd058e352a91fed9e5685c49c5d3fa5062520c660c6c9"
dependencies = [
"midstreamer-temporal-compare",
"midstreamer-temporal-compare 0.2.1",
"nalgebra",
"ndarray 0.16.1",
"serde",
@@ -3888,20 +3901,18 @@ dependencies = [
[[package]]
name = "midstreamer-quic"
version = "0.3.0"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d4dcf971dfa9eb5087e9c79e078f88c1508110bf010b8bb2d29b0b7229fd229"
checksum = "35ad2099588e987cdbedb039fdf8a56163a2f3dc1ff6bf5a39c63b9ce4e2248c"
dependencies = [
"async-trait",
"futures",
"js-sys",
"quinn",
"rcgen",
"rustls-platform-verifier",
"rustls 0.22.4",
"serde",
"thiserror 2.0.18",
"tokio",
"tracing",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
@@ -3909,9 +3920,9 @@ dependencies = [
[[package]]
name = "midstreamer-scheduler"
version = "0.2.1"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8085dbcfb13808d075c0b31681022b41acc1c8021313d45fa7461e97d7767ff"
checksum = "a9296b3f0a2b04e5c1a378ee7926e9f892895bface2ccebcfa407450c3aca269"
dependencies = [
"crossbeam",
"parking_lot",
@@ -3920,6 +3931,18 @@ dependencies = [
"tokio",
]
[[package]]
name = "midstreamer-temporal-compare"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1f935ba86c1632a3b5bc5e1cb56a308d4c5d2ec87c84db551c65f3e1001a642"
dependencies = [
"dashmap",
"lru",
"serde",
"thiserror 2.0.18",
]
[[package]]
name = "midstreamer-temporal-compare"
version = "0.2.1"
@@ -4296,7 +4319,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4638,14 +4661,15 @@ dependencies = [
[[package]]
name = "openssl"
version = "0.10.80"
version = "0.10.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a45fa2aa886c42762255da344f0a0d313e254066c46aad76f300c3d3da62d967"
checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328"
dependencies = [
"bitflags 2.11.0",
"cfg-if",
"foreign-types 0.3.2",
"libc",
"once_cell",
"openssl-macros",
"openssl-sys",
]
@@ -4669,9 +4693,9 @@ checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
[[package]]
name = "openssl-sys"
version = "0.9.116"
version = "0.9.111"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f28a22dc7140cda5f096e5e7724a6962ca81a7f8bfd2979f9b18c11af56318c4"
checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321"
dependencies = [
"cc",
"libc",
@@ -4725,7 +4749,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.45.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -5469,7 +5493,7 @@ dependencies = [
"quinn-udp",
"rustc-hash",
"rustls 0.23.37",
"socket2 0.5.10",
"socket2 0.6.2",
"thiserror 2.0.18",
"tokio",
"tracing",
@@ -5508,9 +5532,9 @@ dependencies = [
"cfg_aliases",
"libc",
"once_cell",
"socket2 0.5.10",
"socket2 0.6.2",
"tracing",
"windows-sys 0.59.0",
"windows-sys 0.60.2",
]
[[package]]
@@ -6148,7 +6172,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6163,6 +6187,20 @@ dependencies = [
"sct",
]
[[package]]
name = "rustls"
version = "0.22.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432"
dependencies = [
"log",
"ring",
"rustls-pki-types",
"rustls-webpki 0.102.8",
"subtle",
"zeroize",
]
[[package]]
name = "rustls"
version = "0.23.37"
@@ -6173,7 +6211,7 @@ dependencies = [
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki 0.103.13",
"rustls-webpki 0.103.9",
"subtle",
"zeroize",
]
@@ -6223,11 +6261,11 @@ dependencies = [
"rustls 0.23.37",
"rustls-native-certs",
"rustls-platform-verifier-android",
"rustls-webpki 0.103.13",
"rustls-webpki 0.103.9",
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6248,9 +6286,20 @@ dependencies = [
[[package]]
name = "rustls-webpki"
version = "0.103.13"
version = "0.102.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e"
checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]]
name = "rustls-webpki"
version = "0.103.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53"
dependencies = [
"ring",
"rustls-pki-types",
@@ -7650,7 +7699,7 @@ dependencies = [
"getrandom 0.4.1",
"once_cell",
"rustix",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -9126,8 +9175,8 @@ dependencies = [
"chrono",
"clap",
"futures-util",
"midstreamer-attractor",
"midstreamer-temporal-compare",
"midstreamer-attractor 0.2.1",
"midstreamer-temporal-compare 0.2.1",
"ruvector-mincut",
"serde",
"serde_json",
@@ -9140,7 +9189,6 @@ dependencies = [
"tracing",
"tracing-subscriber",
"ureq 2.12.1",
"wifi-densepose-hardware",
"wifi-densepose-signal",
"wifi-densepose-wifiscan",
]
@@ -9151,8 +9199,8 @@ version = "0.3.0"
dependencies = [
"chrono",
"criterion",
"midstreamer-attractor",
"midstreamer-temporal-compare",
"midstreamer-attractor 0.1.0",
"midstreamer-temporal-compare 0.1.0",
"ndarray 0.17.2",
"ndarray-linalg",
"num-complex",
@@ -9270,7 +9318,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.48.0",
"windows-sys 0.61.2",
]
[[package]]
+4 -7
View File
@@ -144,13 +144,10 @@ mockall = "0.12"
wiremock = "0.5"
# midstreamer integration (published on crates.io)
# 0.1.0 was yanked; upgrade to latest 0.3/0.2 releases which pull in
# quinn-proto >=0.11.14 (fixes RUSTSEC-2026-0037) and
# rustls-webpki >=0.103.13 (fixes RUSTSEC-2026-0049/0098/0099/0104).
midstreamer-quic = "0.3"
midstreamer-scheduler = "0.2"
midstreamer-temporal-compare = "0.2"
midstreamer-attractor = "0.2"
midstreamer-quic = "0.1.0"
midstreamer-scheduler = "0.1.0"
midstreamer-temporal-compare = "0.1.0"
midstreamer-attractor = "0.1.0"
# ruvector integration (published on crates.io)
# Vendored at v2.1.0 in vendor/ruvector; using crates.io versions until published.
+15 -40
View File
@@ -29,10 +29,7 @@ pub fn fuse_confidence_weighted(preds: &[CountPrediction]) -> CountPrediction {
if preds.is_empty() {
let mut probs = [0.0_f32; COUNT_CLASSES];
probs[1] = 1.0;
return CountPrediction {
probs,
confidence: 0.0,
};
return CountPrediction { probs, confidence: 0.0 };
}
if preds.len() == 1 {
return preds[0].clone();
@@ -47,9 +44,9 @@ pub fn fuse_confidence_weighted(preds: &[CountPrediction]) -> CountPrediction {
// Log-sum.
let mut log_p = [0.0_f32; COUNT_CLASSES];
for (pred, &w) in preds.iter().zip(weights.iter()) {
for (lp, &prob) in log_p.iter_mut().zip(pred.probs.iter()).take(COUNT_CLASSES) {
let p = prob.max(1e-9); // floor to avoid log(0)
*lp += (w / weight_sum) * p.ln();
for k in 0..COUNT_CLASSES {
let p = pred.probs[k].max(1e-9); // floor to avoid log(0)
log_p[k] += (w / weight_sum) * p.ln();
}
}
@@ -57,26 +54,19 @@ pub fn fuse_confidence_weighted(preds: &[CountPrediction]) -> CountPrediction {
let m = log_p.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
let mut p = [0.0_f32; COUNT_CLASSES];
let mut s = 0.0_f32;
for (pk, &lp) in p.iter_mut().zip(log_p.iter()) {
*pk = (lp - m).exp();
s += *pk;
for k in 0..COUNT_CLASSES {
p[k] = (log_p[k] - m).exp();
s += p[k];
}
if s > 0.0 {
for pk in p.iter_mut() {
*pk /= s;
}
for k in 0..COUNT_CLASSES { p[k] /= s; }
} else {
// Pathological — fall back to uniform.
for pk in p.iter_mut() {
*pk = 1.0 / COUNT_CLASSES as f32;
}
for k in 0..COUNT_CLASSES { p[k] = 1.0 / COUNT_CLASSES as f32; }
}
let conf = preds.iter().map(|x| x.confidence).fold(0.0_f32, f32::max);
CountPrediction {
probs: p,
confidence: conf,
}
CountPrediction { probs: p, confidence: conf }
}
/// **Stoer-Wagner-clipped fusion** — v0.2.0 hook.
@@ -116,10 +106,7 @@ mod tests {
use approx::assert_relative_eq;
fn pred(probs: [f32; 8], conf: f32) -> CountPrediction {
CountPrediction {
probs,
confidence: conf,
}
CountPrediction { probs, confidence: conf }
}
#[test]
@@ -146,15 +133,14 @@ mod tests {
assert!(
fused.probs[2] >= probs[2],
"expected fusion to sharpen the peak: pre={} post={}",
probs[2],
fused.probs[2]
probs[2], fused.probs[2]
);
}
#[test]
fn high_confidence_node_overrides_low_confidence_disagreement() {
let strong = [0.0, 0.95, 0.05, 0.0, 0.0, 0.0, 0.0, 0.0]; // says 1
let weak = [0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.4]; // weak, says 7
let weak = [0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.4]; // weak, says 7
let fused = fuse_confidence_weighted(&[pred(strong, 0.95), pred(weak, 0.05)]);
assert_eq!(fused.argmax(), 1, "high-confidence vote should win");
}
@@ -188,19 +174,8 @@ mod tests {
let probs = [0.05, 0.6, 0.25, 0.05, 0.03, 0.01, 0.005, 0.005];
let p = pred(probs, 0.9);
let (lo, hi) = p.p95_range();
assert!(
lo <= 1 && hi >= 1,
"mode (1) must be inside [{}, {}]",
lo,
hi
);
assert!(lo <= 1 && hi >= 1, "mode (1) must be inside [{}, {}]", lo, hi);
let mass: f32 = probs[lo..=hi].iter().sum();
assert!(
mass >= 0.95,
"[{}, {}] only covers {:.3}, need >= 0.95",
lo,
hi,
mass
);
assert!(mass >= 0.95, "[{}, {}] only covers {:.3}, need >= 0.95", lo, hi, mass);
}
}
+13 -64
View File
@@ -67,11 +67,7 @@ impl CountPrediction {
let mut acc = self.probs[mode];
while acc < 0.95 && (lo > 0 || hi < COUNT_CLASSES - 1) {
let left = if lo > 0 { self.probs[lo - 1] } else { -1.0 };
let right = if hi < COUNT_CLASSES - 1 {
self.probs[hi + 1]
} else {
-1.0
};
let right = if hi < COUNT_CLASSES - 1 { self.probs[hi + 1] } else { -1.0 };
if left >= right && lo > 0 {
lo -= 1;
acc += self.probs[lo];
@@ -106,57 +102,25 @@ impl CountNet {
let conf = vb.pp("conf_head");
let c1 = candle_nn::conv1d(
56,
64,
3,
Conv1dConfig {
padding: 1,
stride: 1,
dilation: 1,
groups: 1,
..Default::default()
},
56, 64, 3,
Conv1dConfig { padding: 1, stride: 1, dilation: 1, groups: 1, ..Default::default() },
enc.pp("c1"),
)?;
let c2 = candle_nn::conv1d(
64,
128,
3,
Conv1dConfig {
padding: 2,
stride: 1,
dilation: 2,
groups: 1,
..Default::default()
},
64, 128, 3,
Conv1dConfig { padding: 2, stride: 1, dilation: 2, groups: 1, ..Default::default() },
enc.pp("c2"),
)?;
let c3 = candle_nn::conv1d(
128,
128,
3,
Conv1dConfig {
padding: 4,
stride: 1,
dilation: 4,
groups: 1,
..Default::default()
},
128, 128, 3,
Conv1dConfig { padding: 4, stride: 1, dilation: 4, groups: 1, ..Default::default() },
enc.pp("c3"),
)?;
let count_fc1 = candle_nn::linear(128, 64, count.pp("fc1"))?;
let count_fc2 = candle_nn::linear(64, COUNT_CLASSES, count.pp("fc2"))?;
let conf_fc1 = candle_nn::linear(128, 32, conf.pp("fc1"))?;
let conf_fc2 = candle_nn::linear(32, 1, conf.pp("fc2"))?;
Ok(Self {
c1,
c2,
c3,
count_fc1,
count_fc2,
conf_fc1,
conf_fc2,
})
Ok(Self { c1, c2, c3, count_fc1, count_fc2, conf_fc1, conf_fc2 })
}
fn forward(&self, x: &Tensor) -> candle_core::Result<(Tensor, Tensor)> {
@@ -229,10 +193,7 @@ impl InferenceEngine {
// model yet" honestly instead of pretending to know.
let mut probs = [0.0f32; COUNT_CLASSES];
probs[1] = 1.0; // mass on "1 person"
return Ok(CountPrediction {
probs,
confidence: 0.0,
});
return Ok(CountPrediction { probs, confidence: 0.0 });
};
let t = Tensor::from_slice(
@@ -243,37 +204,25 @@ impl InferenceEngine {
let (probs_t, conf_t) = net.forward(&t)?;
let flat: Vec<f32> = probs_t.flatten_all()?.to_vec1()?;
if flat.len() != COUNT_CLASSES {
return Err(format!(
"count head produced {} probs, expected {}",
flat.len(),
COUNT_CLASSES
)
.into());
return Err(format!("count head produced {} probs, expected {}", flat.len(), COUNT_CLASSES).into());
}
let mut probs = [0.0f32; COUNT_CLASSES];
probs.copy_from_slice(&flat[..COUNT_CLASSES]);
let conf = conf_t.flatten_all()?.to_vec1::<f32>()?[0];
Ok(CountPrediction {
probs,
confidence: conf,
})
Ok(CountPrediction { probs, confidence: conf })
}
}
pub struct SyntheticInput;
impl Default for SyntheticInput {
fn default() -> Self {
Self
}
fn default() -> Self { Self }
}
impl SyntheticInput {
pub fn as_window(&self) -> CsiWindow {
CsiWindow {
data: vec![0.0; INPUT_SUBCARRIERS * INPUT_TIMESTEPS],
}
CsiWindow { data: vec![0.0; INPUT_SUBCARRIERS * INPUT_TIMESTEPS] }
}
}
+16 -22
View File
@@ -9,7 +9,8 @@
use clap::{Parser, Subcommand};
use cog_person_count::{
inference::{InferenceEngine, SyntheticInput},
publisher, COG_ID, COG_VERSION,
publisher,
COG_ID, COG_VERSION,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
@@ -42,12 +43,8 @@ struct RunConfig {
poll_ms: u64,
}
fn default_sensing_url() -> String {
"http://127.0.0.1:3000/api/v1/sensing/latest".to_string()
}
fn default_poll_ms() -> u64 {
40
}
fn default_sensing_url() -> String { "http://127.0.0.1:3000/api/v1/sensing/latest".to_string() }
fn default_poll_ms() -> u64 { 40 }
fn main() -> std::process::ExitCode {
init_logging();
@@ -71,7 +68,7 @@ fn init_logging() {
let _ = tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"))
)
.with_target(false)
.try_init();
@@ -83,25 +80,22 @@ fn cmd_version() -> Result<(), Box<dyn std::error::Error>> {
}
fn cmd_manifest() -> Result<(), Box<dyn std::error::Error>> {
println!(
"{}",
serde_json::to_string_pretty(&json!({
"id": COG_ID,
"version": COG_VERSION,
"binary_url": Value::Null,
"binary_bytes": Value::Null,
"binary_sha256": Value::Null,
"binary_signature": Value::Null,
"installed_at": Value::Null,
"status": Value::Null,
}))?
);
println!("{}", serde_json::to_string_pretty(&json!({
"id": COG_ID,
"version": COG_VERSION,
"binary_url": Value::Null,
"binary_bytes": Value::Null,
"binary_sha256": Value::Null,
"binary_signature": Value::Null,
"installed_at": Value::Null,
"status": Value::Null,
}))?);
Ok(())
}
fn cmd_health() -> Result<(), Box<dyn std::error::Error>> {
let engine = InferenceEngine::new()?;
let pred = engine.infer(&SyntheticInput.as_window())?;
let pred = engine.infer(&SyntheticInput::default().as_window())?;
if !pred.is_finite() {
return Err("inference produced non-finite output".into());
}
+1 -3
View File
@@ -35,9 +35,7 @@ pub async fn run_loop(
buffer.drain(0..extra);
}
if buffer.len() >= cap {
let window = CsiWindow {
data: buffer[buffer.len() - cap..].to_vec(),
};
let window = CsiWindow { data: buffer[buffer.len() - cap..].to_vec() };
if let Ok(pred) = engine.infer(&window) {
// v0.0.1 ships single-node — fusion is a no-op for
// N=1. v0.2.0 will append additional per-node
+10 -25
View File
@@ -3,30 +3,26 @@
use cog_person_count::{
fusion::{fuse_confidence_weighted, fuse_with_mincut_clip},
inference::{
CountPrediction, CsiWindow, InferenceEngine, SyntheticInput, COUNT_CLASSES,
INPUT_SUBCARRIERS, INPUT_TIMESTEPS,
CountPrediction, CsiWindow, InferenceEngine, SyntheticInput,
COUNT_CLASSES, INPUT_SUBCARRIERS, INPUT_TIMESTEPS,
},
};
#[test]
fn synthetic_window_has_correct_shape() {
let w = SyntheticInput.as_window();
let w = SyntheticInput::default().as_window();
assert_eq!(w.data.len(), INPUT_SUBCARRIERS * INPUT_TIMESTEPS);
}
#[test]
fn stub_engine_returns_finite_output() {
let engine = InferenceEngine::with_weights(None).expect("stub engine");
let pred = engine.infer(&SyntheticInput.as_window()).expect("infer");
let pred = engine.infer(&SyntheticInput::default().as_window()).expect("infer");
assert!(pred.is_finite());
assert_eq!(pred.probs.len(), COUNT_CLASSES);
let sum: f32 = pred.probs.iter().sum();
assert!(
(sum - 1.0).abs() < 1e-5,
"stub probs must sum to 1, got {}",
sum
);
assert!((sum - 1.0).abs() < 1e-5, "stub probs must sum to 1, got {}", sum);
assert_eq!(pred.argmax(), 1, "stub default is 1-person");
assert_eq!(pred.confidence, 0.0, "stub confidence is 0");
}
@@ -34,9 +30,7 @@ fn stub_engine_returns_finite_output() {
#[test]
fn engine_rejects_wrong_shape_input() {
let engine = InferenceEngine::with_weights(None).expect("stub engine");
let bad = CsiWindow {
data: vec![0.0; 10],
};
let bad = CsiWindow { data: vec![0.0; 10] };
assert!(engine.infer(&bad).is_err());
}
@@ -53,10 +47,7 @@ fn p95_range_includes_mode() {
probs[2] = 0.85;
probs[1] = 0.08;
probs[3] = 0.07;
let p = CountPrediction {
probs,
confidence: 0.9,
};
let p = CountPrediction { probs, confidence: 0.9 };
let (lo, hi) = p.p95_range();
assert!(lo <= 2 && hi >= 2);
}
@@ -74,11 +65,8 @@ fn fusion_passes_through_single_node() {
// raw inference — fusion is a no-op for N=1.
let mut probs = [0.0_f32; COUNT_CLASSES];
probs[3] = 1.0;
let input = CountPrediction {
probs,
confidence: 0.6,
};
let out = fuse_confidence_weighted(std::slice::from_ref(&input));
let input = CountPrediction { probs, confidence: 0.6 };
let out = fuse_confidence_weighted(&[input.clone()]);
assert_eq!(out.argmax(), 3);
assert!((out.confidence - 0.6).abs() < 1e-6);
}
@@ -88,10 +76,7 @@ fn mincut_clip_with_high_cap_is_noop() {
let mut probs = [0.0_f32; COUNT_CLASSES];
probs[2] = 0.5;
probs[3] = 0.5;
let input = CountPrediction {
probs,
confidence: 0.7,
};
let input = CountPrediction { probs, confidence: 0.7 };
let clipped = fuse_with_mincut_clip(&[input], 7);
// No clip happened (cap == max class)
assert!((clipped.probs[2] - 0.5).abs() < 1e-6);
+2 -2
View File
@@ -41,8 +41,8 @@ fn default_min_confidence() -> f32 {
impl CogConfig {
pub fn load(path: &Path) -> Result<Self, ConfigError> {
let raw =
std::fs::read_to_string(path).map_err(|e| ConfigError::Read(path.to_path_buf(), e))?;
let raw = std::fs::read_to_string(path)
.map_err(|e| ConfigError::Read(path.to_path_buf(), e))?;
let cfg: CogConfig =
serde_json::from_str(&raw).map_err(|e| ConfigError::Parse(path.to_path_buf(), e))?;
Ok(cfg)
+4 -28
View File
@@ -64,51 +64,27 @@ impl PoseNet {
56,
64,
3,
Conv1dConfig {
padding: 1,
stride: 1,
dilation: 1,
groups: 1,
..Default::default()
},
Conv1dConfig { padding: 1, stride: 1, dilation: 1, groups: 1, ..Default::default() },
enc.pp("c1"),
)?;
let c2 = candle_nn::conv1d(
64,
128,
3,
Conv1dConfig {
padding: 2,
stride: 1,
dilation: 2,
groups: 1,
..Default::default()
},
Conv1dConfig { padding: 2, stride: 1, dilation: 2, groups: 1, ..Default::default() },
enc.pp("c2"),
)?;
let c3 = candle_nn::conv1d(
128,
128,
3,
Conv1dConfig {
padding: 4,
stride: 1,
dilation: 4,
groups: 1,
..Default::default()
},
Conv1dConfig { padding: 4, stride: 1, dilation: 4, groups: 1, ..Default::default() },
enc.pp("c3"),
)?;
let fc1 = candle_nn::linear(128, 256, head.pp("fc1"))?;
let fc2 = candle_nn::linear(256, 34, head.pp("fc2"))?;
Ok(Self {
c1,
c2,
c3,
fc1,
fc2,
})
Ok(Self { c1, c2, c3, fc1, fc2 })
}
/// Forward pass: `[B, 56, 20]` -> `[B, 34]` in `[0, 1]`.
+6 -2
View File
@@ -89,10 +89,14 @@ fn cmd_manifest() -> Result<(), Box<dyn std::error::Error>> {
fn cmd_health() -> Result<(), Box<dyn std::error::Error>> {
let engine = InferenceEngine::new()?;
let synthetic = SyntheticInput;
let synthetic = SyntheticInput::default();
let out = engine.infer(&synthetic.as_window())?;
if out.is_finite() {
emit_event(&Event::health_ok(COG_ID, engine.backend(), out.confidence));
emit_event(&Event::health_ok(
COG_ID,
engine.backend(),
out.confidence,
));
Ok(())
} else {
Err("inference produced non-finite output".into())
+11 -17
View File
@@ -4,15 +4,13 @@
//! depend on a trained safetensors blob that doesn't live in-repo yet.
use cog_pose_estimation::{
inference::{
InferenceEngine, SyntheticInput, INPUT_SUBCARRIERS, INPUT_TIMESTEPS, OUTPUT_KEYPOINTS,
},
inference::{InferenceEngine, SyntheticInput, INPUT_SUBCARRIERS, INPUT_TIMESTEPS, OUTPUT_KEYPOINTS},
manifest::ManifestSpec,
};
#[test]
fn synthetic_window_has_correct_shape() {
let syn = SyntheticInput;
let syn = SyntheticInput::default();
let window = syn.as_window();
assert_eq!(window.data.len(), INPUT_SUBCARRIERS * INPUT_TIMESTEPS);
}
@@ -20,20 +18,17 @@ fn synthetic_window_has_correct_shape() {
#[test]
fn engine_produces_finite_output_for_synthetic_input() {
let engine = InferenceEngine::new().expect("engine init");
let out = engine.infer(&SyntheticInput.as_window()).expect("infer");
assert!(
out.is_finite(),
"synthetic input must produce finite output"
);
let out = engine
.infer(&SyntheticInput::default().as_window())
.expect("infer");
assert!(out.is_finite(), "synthetic input must produce finite output");
assert_eq!(out.keypoints.len(), OUTPUT_KEYPOINTS * 2);
}
#[test]
fn engine_rejects_wrong_shape_input() {
let engine = InferenceEngine::new().expect("engine init");
let bad = cog_pose_estimation::inference::CsiWindow {
data: vec![0.0; 10],
};
let bad = cog_pose_estimation::inference::CsiWindow { data: vec![0.0; 10] };
assert!(engine.infer(&bad).is_err());
}
@@ -52,15 +47,14 @@ fn real_weights_load_when_available() {
"expected real Candle backend, got {}",
engine.backend()
);
let out = engine.infer(&SyntheticInput.as_window()).expect("infer");
let out = engine
.infer(&SyntheticInput::default().as_window())
.expect("infer");
assert!(out.is_finite());
// Real model emits the published validation PCK@50 as its self-reported
// confidence — stub returns 0.0. This is the key assertion that proves
// the cog isn't silently falling back to the stub.
assert!(
out.confidence > 0.0,
"real model should emit non-zero confidence"
);
assert!(out.confidence > 0.0, "real model should emit non-zero confidence");
}
#[test]
+4 -4
View File
@@ -135,10 +135,7 @@ struct VerifyBody {
expected_hex: String,
}
/// Incoming request body for the `/step` endpoint.
/// Fields are optional; unused ones are reserved for future extensions.
#[derive(Deserialize)]
#[allow(dead_code)]
struct StepReq {
direction: Option<String>,
dt_ms: Option<f64>,
@@ -350,7 +347,10 @@ fn chrono_like_now() -> String {
format!("{secs}-unix")
}
async fn ws_handler(ws: WebSocketUpgrade, State(s): State<AppState>) -> impl IntoResponse {
async fn ws_handler(
ws: WebSocketUpgrade,
State(s): State<AppState>,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| handle_ws(socket, s))
}
+4 -1
View File
@@ -238,6 +238,9 @@ mod tests {
let x = (2.0 * std::f64::consts::PI * f_off * t).cos();
last = lockin.process(x);
}
assert!(last.abs() < 0.1, "off-resonance output {last} should be ~0");
assert!(
last.abs() < 0.1,
"off-resonance output {last} should be ~0"
);
}
}
+1 -4
View File
@@ -217,10 +217,7 @@ mod tests {
let mut bytes = MagFrame::empty(0).to_bytes();
bytes[4..6].copy_from_slice(&99_u16.to_le_bytes());
let err = MagFrame::from_bytes(&bytes).unwrap_err();
assert!(matches!(
err,
crate::NvsimError::UnsupportedVersion { got: 99, .. }
));
assert!(matches!(err, crate::NvsimError::UnsupportedVersion { got: 99, .. }));
}
#[test]
+20 -16
View File
@@ -18,7 +18,7 @@ use crate::sensor::{NvSensor, NvSensorConfig};
use crate::source::scene_field_at;
/// Pipeline configuration.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PipelineConfig {
/// Sensor / digitiser sampling parameters.
pub digitiser: DigitiserConfig,
@@ -28,6 +28,16 @@ pub struct PipelineConfig {
pub dt_s: Option<f64>,
}
impl Default for PipelineConfig {
fn default() -> Self {
Self {
digitiser: DigitiserConfig::default(),
sensor: NvSensorConfig::default(),
dt_s: None,
}
}
}
/// Forward-only NV-diamond pipeline.
#[derive(Debug, Clone)]
pub struct Pipeline {
@@ -40,21 +50,14 @@ impl Pipeline {
/// Construct a pipeline. `seed` makes shot-noise reproducible — same
/// `(scene, config, seed)` produces byte-identical output.
pub fn new(scene: Scene, config: PipelineConfig, seed: u64) -> Self {
Self {
scene,
config,
seed,
}
Self { scene, config, seed }
}
/// Run `n_samples` of the pipeline. Returns one [`MagFrame`] per
/// (sensor × sample) — i.e. `n_samples · scene.sensors.len()` frames
/// in scene-major / sample-minor order.
pub fn run(&self, n_samples: usize) -> Vec<MagFrame> {
let dt = self
.config
.dt_s
.unwrap_or(1.0 / self.config.digitiser.f_s_hz);
let dt = self.config.dt_s.unwrap_or(1.0 / self.config.digitiser.f_s_hz);
let dt_us = (dt * 1.0e6) as u64;
let nv = NvSensor::new(self.config.sensor);
@@ -79,11 +82,11 @@ impl Pipeline {
// saturation flag if any axis clips.
let mut adc_sat = false;
let mut b_pt = [0.0_f32; 3];
for (k, b) in b_pt.iter_mut().enumerate() {
for k in 0..3 {
let (code, sat) = adc_quantise(reading.b_recovered[k]);
adc_sat |= sat;
let recovered_t = code as f64 * crate::digitiser::ADC_LSB_T;
*b = (recovered_t * 1.0e12) as f32; // T → pT
b_pt[k] = (recovered_t * 1.0e12) as f32; // T → pT
}
let sigma_pt = [
(reading.sigma_per_axis[0] * 1.0e12) as f32,
@@ -95,7 +98,8 @@ impl Pipeline {
frame.t_us = (sample as u64) * dt_us;
frame.b_pt = b_pt;
frame.sigma_pt = sigma_pt;
frame.noise_floor_pt_sqrt_hz = (reading.noise_floor_t_sqrt_hz * 1.0e12) as f32;
frame.noise_floor_pt_sqrt_hz =
(reading.noise_floor_t_sqrt_hz * 1.0e12) as f32;
frame.temperature_k = 295.0;
if near_field {
frame.set_flag(flag::SATURATION_NEAR_FIELD);
@@ -194,11 +198,11 @@ mod tests {
let (b_analytic, _) = scene_field_at(&scene, scene.sensors[0]);
for f in &frames {
assert!(f.has_flag(flag::SHOT_NOISE_DISABLED));
for (k, (&b_pt, &b_ref)) in f.b_pt.iter().zip(b_analytic.iter()).enumerate() {
let recovered_t = b_pt as f64 * 1.0e-12;
for k in 0..3 {
let recovered_t = f.b_pt[k] as f64 * 1.0e-12;
let lsb_t = crate::digitiser::ADC_LSB_T;
assert!(
(recovered_t - b_ref).abs() <= lsb_t,
(recovered_t - b_analytic[k]).abs() <= lsb_t,
"noise-off recovery error > 1 LSB for axis {k}"
);
}
+11 -8
View File
@@ -58,12 +58,12 @@ pub struct LosSegment {
pub fn material_loss_db_per_m(m: Material) -> f64 {
match m {
Material::Air => 0.0,
Material::Drywall => 0.0, // conjecture: gypsum non-ferromagnetic
Material::Brick => 0.0, // conjecture: same logic as drywall
Material::ConcreteDry => 0.5, // conjecture: Ulrich 2002 proxy
Material::Drywall => 0.0, // conjecture: gypsum non-ferromagnetic
Material::Brick => 0.0, // conjecture: same logic as drywall
Material::ConcreteDry => 0.5, // conjecture: Ulrich 2002 proxy
Material::ReinforcedConcrete => 20.0, // proxy + warning flag (plan §2.2)
Material::SheetSteel => 100.0, // frequency-dependent in reality;
// representative DC bulk loss
Material::SheetSteel => 100.0, // frequency-dependent in reality;
// representative DC bulk loss
}
}
@@ -92,7 +92,10 @@ pub fn attenuate(b_in: Vec3, segments: &[LosSegment]) -> (Vec3, bool) {
heavy |= material_is_heavy(seg.material);
}
let scale = 10.0_f64.powf(-total_db / 20.0);
([b_in[0] * scale, b_in[1] * scale, b_in[2] * scale], heavy)
(
[b_in[0] * scale, b_in[1] * scale, b_in[2] * scale],
heavy,
)
}
/// Aggregate "propagator" type — currently a stateless wrapper over
@@ -172,8 +175,8 @@ mod tests {
}];
let (b_out, heavy) = attenuate(b_in, &segs);
let expected = 10.0_f64.powf(-4.0 / 20.0);
for &val in &b_out {
assert_relative_eq!(val, expected, max_relative = 1e-12);
for k in 0..3 {
assert_relative_eq!(b_out[k], expected, max_relative = 1e-12);
}
assert!(heavy, "reinforced concrete must raise heavy_flag");
}
+20 -17
View File
@@ -63,7 +63,12 @@ pub const DEFAULT_N_SPINS: f64 = 1.0e12;
/// Tetrahedral 〈111〉 family in the diamond lattice.
pub fn nv_axes() -> [[f64; 3]; 4] {
let s = 1.0 / 3.0_f64.sqrt();
[[s, s, s], [s, -s, -s], [-s, s, -s], [-s, -s, s]]
[
[s, s, s],
[s, -s, -s],
[-s, s, -s],
[-s, -s, s],
]
}
/// Sensor configuration. All defaults match plan §2.3 / Barry 2020 Table III
@@ -158,9 +163,8 @@ impl NvSensor {
/// per-sample noise σ in T.
pub fn shot_noise_floor_t_sqrt_hz(&self, integration_s: f64) -> f64 {
let t = integration_s.max(self.config.t2_star_s);
let denom = GAMMA_E
* self.config.contrast
* (self.config.n_spins * t * self.config.t2_star_s).sqrt();
let denom =
GAMMA_E * self.config.contrast * (self.config.n_spins * t * self.config.t2_star_s).sqrt();
if denom <= 0.0 {
f64::INFINITY
} else {
@@ -312,10 +316,13 @@ mod tests {
];
for &b_in in &inputs {
let r = s.sample(b_in, 1.0e-3, 0xCAFE_BABE);
for (k, (&b_recovered, &b_orig)) in r.b_recovered.iter().zip(b_in.iter()).enumerate() {
let denom = b_orig.abs().max(1e-30);
let rel = (b_recovered - b_orig).abs() / denom;
assert!(rel < 0.01, "LSQ residual {rel:.4} exceeds 1% for axis {k}");
for k in 0..3 {
let denom = b_in[k].abs().max(1e-30);
let rel = (r.b_recovered[k] - b_in[k]).abs() / denom;
assert!(
rel < 0.01,
"LSQ residual {rel:.4} exceeds 1% for axis {k}"
);
}
}
}
@@ -331,19 +338,19 @@ mod tests {
let mut sum = [0.0_f64; 3];
for i in 0..n {
let r = s.sample([0.0; 3], dt, 0xDEAD_BEEF + i as u64);
for (s, &b) in sum.iter_mut().zip(r.b_recovered.iter()) {
*s += b;
for k in 0..3 {
sum[k] += r.b_recovered[k];
}
}
let mean = [sum[0] / n as f64, sum[1] / n as f64, sum[2] / n as f64];
// Stat margin: σ_mean = σ / √n. Allow ≤ 1σ_mean (loose).
let r = s.sample([0.0; 3], dt, 0);
let sigma_mean = r.sigma_per_axis[0] / (n as f64).sqrt();
for (k, &m) in mean.iter().enumerate() {
for k in 0..3 {
assert!(
m.abs() <= sigma_mean,
mean[k].abs() <= sigma_mean,
"axis {k} zero-input mean {} exceeds σ_mean {}",
m,
mean[k],
sigma_mean
);
}
@@ -385,9 +392,6 @@ mod tests {
// form depends on this. Verify the matrix.
let axes = nv_axes();
let mut ata = [[0.0_f64; 3]; 3];
// Compute AᵀA using explicit 2D indexing — clippy::needless_range_loop
// cannot be avoided here without losing clarity in this matrix formula.
#[allow(clippy::needless_range_loop)]
for j in 0..3 {
for k in 0..3 {
let mut acc = 0.0;
@@ -397,7 +401,6 @@ mod tests {
ata[j][k] = acc;
}
}
#[allow(clippy::needless_range_loop)]
for j in 0..3 {
for k in 0..3 {
let expected = if j == k { 4.0 / 3.0 } else { 0.0 };
+1 -5
View File
@@ -132,11 +132,7 @@ pub fn scene_field_at(scene: &Scene, sensor_pos: Vec3) -> (Vec3, bool) {
/// Total field at every sensor location in a scene, in scene order.
pub fn scene_field_at_sensors(scene: &Scene) -> Vec<(Vec3, bool)> {
scene
.sensors
.iter()
.map(|&p| scene_field_at(scene, p))
.collect()
scene.sensors.iter().map(|&p| scene_field_at(scene, p)).collect()
}
// ────────────────────── vec3 helpers ─────────────────────────────────────
+6 -14
View File
@@ -46,8 +46,8 @@ impl WasmPipeline {
pub fn new(scene_json: &str, config_json: &str, seed: f64) -> Result<WasmPipeline, JsValue> {
let scene: Scene =
serde_json::from_str(scene_json).map_err(|e| js_err(format!("scene parse: {e}")))?;
let config: PipelineConfig =
serde_json::from_str(config_json).map_err(|e| js_err(format!("config parse: {e}")))?;
let config: PipelineConfig = serde_json::from_str(config_json)
.map_err(|e| js_err(format!("config parse: {e}")))?;
let seed_u64 = seed as u64;
Ok(WasmPipeline {
inner: Pipeline::new(scene, config, seed_u64),
@@ -184,8 +184,8 @@ pub fn run_transient(
) -> Result<JsValue, JsValue> {
let scene: crate::scene::Scene =
serde_json::from_str(scene_json).map_err(|e| js_err(format!("scene parse: {e}")))?;
let config: crate::pipeline::PipelineConfig =
serde_json::from_str(config_json).map_err(|e| js_err(format!("config parse: {e}")))?;
let config: crate::pipeline::PipelineConfig = serde_json::from_str(config_json)
.map_err(|e| js_err(format!("config parse: {e}")))?;
let pipeline = crate::pipeline::Pipeline::new(scene, config, seed as u64);
let (frames, witness) = pipeline.run_with_witness(n_samples);
@@ -217,11 +217,7 @@ pub fn run_transient(
let s_arr = js_sys::Float64Array::new_with_length(3);
s_arr.copy_from(&avg_s_pt);
js_sys::Reflect::set(&obj, &JsValue::from_str("bRecoveredT"), &b_arr)?;
js_sys::Reflect::set(
&obj,
&JsValue::from_str("bMagT"),
&JsValue::from_f64(bmag_t),
)?;
js_sys::Reflect::set(&obj, &JsValue::from_str("bMagT"), &JsValue::from_f64(bmag_t))?;
js_sys::Reflect::set(
&obj,
&JsValue::from_str("noiseFloorPtSqrtHz"),
@@ -234,10 +230,6 @@ pub fn run_transient(
&JsValue::from_f64(frames.len() as f64),
)?;
let witness_hex = crate::proof::Proof::hex(&witness);
js_sys::Reflect::set(
&obj,
&JsValue::from_str("witnessHex"),
&JsValue::from_str(&witness_hex),
)?;
js_sys::Reflect::set(&obj, &JsValue::from_str("witnessHex"), &JsValue::from_str(&witness_hex))?;
Ok(obj.into())
}
+1 -5
View File
@@ -31,11 +31,7 @@ pub mod mat;
/// WiFi-DensePose Command Line Interface
#[derive(Parser, Debug)]
#[command(name = "wifi-densepose")]
#[command(
author,
version,
about = "WiFi-based pose estimation and disaster response"
)]
#[command(author, version, about = "WiFi-based pose estimation and disaster response")]
#[command(propagate_version = true)]
pub struct Cli {
/// Command to execute
+59 -27
View File
@@ -16,8 +16,8 @@ use std::path::PathBuf;
use tabled::{settings::Style, Table, Tabled};
use wifi_densepose_mat::{
domain::alert::AlertStatus, DisasterConfig, DisasterType, Priority, ScanZone, TriageStatus,
ZoneBounds, ZoneStatus,
DisasterConfig, DisasterType, Priority, ScanZone, TriageStatus, ZoneBounds,
ZoneStatus, domain::alert::AlertStatus,
};
/// MAT subcommand
@@ -452,21 +452,40 @@ pub async fn execute(command: MatCommand) -> Result<()> {
/// Execute the scan command
async fn execute_scan(args: ScanArgs) -> Result<()> {
println!("{} Starting survivor scan...", "[MAT]".bright_cyan().bold());
println!(
"{} Starting survivor scan...",
"[MAT]".bright_cyan().bold()
);
println!();
// Display configuration
println!("{}", "Configuration:".bold());
println!(" {} {:?}", "Disaster Type:".dimmed(), args.disaster_type);
println!(" {} {:.1}", "Sensitivity:".dimmed(), args.sensitivity);
println!(" {} {:.1}m", "Max Depth:".dimmed(), args.max_depth);
println!(
" {} {:?}",
"Disaster Type:".dimmed(),
args.disaster_type
);
println!(
" {} {:.1}",
"Sensitivity:".dimmed(),
args.sensitivity
);
println!(
" {} {:.1}m",
"Max Depth:".dimmed(),
args.max_depth
);
println!(
" {} {}",
"Continuous:".dimmed(),
if args.continuous { "Yes" } else { "No" }
);
if args.continuous {
println!(" {} {}ms", "Interval:".dimmed(), args.interval);
println!(
" {} {}ms",
"Interval:".dimmed(),
args.interval
);
}
if let Some(ref zone) = args.zone {
println!(" {} {}", "Zone:".dimmed(), zone);
@@ -497,7 +516,10 @@ async fn execute_scan(args: ScanArgs) -> Result<()> {
"[INFO]".blue(),
config.disaster_type
);
println!("{} Waiting for hardware connection...", "[INFO]".blue());
println!(
"{} Waiting for hardware connection...",
"[INFO]".blue()
);
println!();
println!(
"{} No hardware detected. Use --simulate for demo mode.",
@@ -516,9 +538,7 @@ async fn simulate_scan_output() -> Result<()> {
let pb = ProgressBar::new(100);
pb.set_style(
ProgressStyle::default_bar()
.template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
)?
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})")?
.progress_chars("#>-"),
);
@@ -571,10 +591,13 @@ async fn simulate_scan_output() -> Result<()> {
"3".green().bold()
);
println!(
" {} 1 {} 1 {} 1",
" {} {} {} {} {} {}",
"IMMEDIATE:".red().bold(),
"1",
"DELAYED:".yellow().bold(),
"1",
"MINOR:".green().bold(),
"1"
);
Ok(())
@@ -651,7 +674,11 @@ async fn execute_status(args: StatusArgs) -> Result<()> {
status.active_zones,
status.total_zones
);
println!(" {} {}", "Disaster Type:".dimmed(), status.disaster_type);
println!(
" {} {}",
"Disaster Type:".dimmed(),
status.disaster_type
);
println!(
" {} {}",
"Survivors Detected:".dimmed(),
@@ -747,10 +774,8 @@ async fn execute_zones(args: ZonesArgs) -> Result<()> {
match bounds_parsed {
Ok(zone_bounds) => {
let zone = if let Some(sens) = sensitivity {
let params = wifi_densepose_mat::ScanParameters {
sensitivity: sens,
..Default::default()
};
let mut params = wifi_densepose_mat::ScanParameters::default();
params.sensitivity = sens;
ScanZone::with_parameters(&name, zone_bounds, params)
} else {
ScanZone::new(&name, zone_bounds)
@@ -781,14 +806,26 @@ async fn execute_zones(args: ZonesArgs) -> Result<()> {
);
println!("Use --force to confirm.");
} else {
println!("{} Zone '{}' removed.", "[OK]".green().bold(), zone.cyan());
println!(
"{} Zone '{}' removed.",
"[OK]".green().bold(),
zone.cyan()
);
}
}
ZonesCommand::Pause { zone } => {
println!("{} Zone '{}' paused.", "[OK]".green().bold(), zone.cyan());
println!(
"{} Zone '{}' paused.",
"[OK]".green().bold(),
zone.cyan()
);
}
ZonesCommand::Resume { zone } => {
println!("{} Zone '{}' resumed.", "[OK]".green().bold(), zone.cyan());
println!(
"{} Zone '{}' resumed.",
"[OK]".green().bold(),
zone.cyan()
);
}
}
@@ -811,9 +848,7 @@ fn parse_bounds(zone_type: &ZoneType, bounds: &str) -> Result<ZoneBounds> {
parts.len()
);
}
Ok(ZoneBounds::rectangle(
parts[0], parts[1], parts[2], parts[3],
))
Ok(ZoneBounds::rectangle(parts[0], parts[1], parts[2], parts[3]))
}
ZoneType::Circle => {
if parts.len() != 3 {
@@ -1001,10 +1036,7 @@ async fn execute_alerts(args: AlertsArgs) -> Result<()> {
if filtered.is_empty() {
println!("No alerts.");
} else {
let pending = filtered
.iter()
.filter(|a| a.status.contains("Pending"))
.count();
let pending = filtered.iter().filter(|a| a.status.contains("Pending")).count();
if pending > 0 {
println!(
"{} {} pending alert(s) require attention!",
+14 -28
View File
@@ -52,29 +52,19 @@ pub mod types;
pub mod utils;
// Re-export commonly used types at the crate root
pub use error::{CoreError, CoreResult, InferenceError, SignalError, StorageError};
pub use traits::{DataStore, NeuralInference, SignalProcessor};
pub use error::{CoreError, CoreResult, SignalError, InferenceError, StorageError};
pub use traits::{SignalProcessor, NeuralInference, DataStore};
pub use types::{
AntennaConfig,
// CSI types
CsiFrame, CsiMetadata, AntennaConfig,
// Signal types
ProcessedSignal, SignalFeatures, FrequencyBand,
// Pose types
PoseEstimate, PersonPose, Keypoint, KeypointType,
// Common types
Confidence, Timestamp, FrameId, DeviceId,
// Bounding box
BoundingBox,
// Common types
Confidence,
// CSI types
CsiFrame,
CsiMetadata,
DeviceId,
FrameId,
FrequencyBand,
Keypoint,
KeypointType,
PersonPose,
// Pose types
PoseEstimate,
// Signal types
ProcessedSignal,
SignalFeatures,
Timestamp,
};
/// Crate version
@@ -107,24 +97,20 @@ pub mod prelude {
};
}
// Compile-time assertions on module-level constants.
const _: () = assert!(MAX_SUBCARRIERS > 0);
const _: () = assert!(DEFAULT_CONFIDENCE_THRESHOLD > 0.0);
const _: () = assert!(DEFAULT_CONFIDENCE_THRESHOLD < 1.0);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version_is_valid() {
// CARGO_PKG_VERSION is always non-empty; verify the constant is
// accessible and has a dot-separated semver shape.
assert!(VERSION.contains('.'), "version should be semver: {VERSION}");
assert!(!VERSION.is_empty());
}
#[test]
fn test_constants() {
assert_eq!(MAX_KEYPOINTS, 17);
assert!(MAX_SUBCARRIERS > 0);
assert!(DEFAULT_CONFIDENCE_THRESHOLD > 0.0);
assert!(DEFAULT_CONFIDENCE_THRESHOLD < 1.0);
}
}
+2 -6
View File
@@ -506,8 +506,7 @@ pub trait AsyncDataStore: Send + Sync {
async fn get_csi_frame(&self, id: &FrameId) -> Result<CsiFrame, StorageError>;
/// Retrieves CSI frames matching the query options.
async fn query_csi_frames(&self, options: &QueryOptions)
-> Result<Vec<CsiFrame>, StorageError>;
async fn query_csi_frames(&self, options: &QueryOptions) -> Result<Vec<CsiFrame>, StorageError>;
/// Stores a pose estimate.
async fn store_pose_estimate(&self, estimate: &PoseEstimate) -> Result<(), StorageError>;
@@ -622,9 +621,6 @@ mod tests {
assert_eq!(cpu, InferenceDevice::Cpu);
assert!(matches!(cuda, InferenceDevice::Cuda { device_id: 0 }));
assert!(matches!(
tensorrt,
InferenceDevice::TensorRt { device_id: 1 }
));
assert!(matches!(tensorrt, InferenceDevice::TensorRt { device_id: 1 }));
}
}
+10 -14
View File
@@ -806,10 +806,7 @@ impl BoundingBox {
/// Returns the center point of the bounding box.
#[must_use]
pub fn center(&self) -> (f32, f32) {
(
(self.x_min + self.x_max) / 2.0,
(self.y_min + self.y_max) / 2.0,
)
((self.x_min + self.x_max) / 2.0, (self.y_min + self.y_max) / 2.0)
}
/// Computes the Intersection over Union (IoU) with another bounding box.
@@ -1000,12 +997,14 @@ impl PoseEstimate {
/// Returns the person with the highest confidence.
#[must_use]
pub fn highest_confidence_person(&self) -> Option<&PersonPose> {
self.persons.iter().max_by(|a, b| {
a.confidence
.value()
.partial_cmp(&b.confidence.value())
.unwrap_or(std::cmp::Ordering::Equal)
})
self.persons
.iter()
.max_by(|a, b| {
a.confidence
.value()
.partial_cmp(&b.confidence.value())
.unwrap_or(std::cmp::Ordering::Equal)
})
}
}
@@ -1083,10 +1082,7 @@ mod tests {
#[test]
fn test_keypoint_type_conversion() {
assert_eq!(KeypointType::try_from(0).unwrap(), KeypointType::Nose);
assert_eq!(
KeypointType::try_from(16).unwrap(),
KeypointType::RightAnkle
);
assert_eq!(KeypointType::try_from(16).unwrap(), KeypointType::RightAnkle);
assert!(KeypointType::try_from(17).is_err());
}
+3 -2
View File
@@ -99,8 +99,9 @@ pub fn moving_average(data: &Array1<f64>, window_size: usize) -> Array1<f64> {
let half_window = window_size / 2;
// ndarray Array1 is always contiguous, but handle gracefully if not
let Some(slice) = data.as_slice() else {
return data.clone();
let slice = match data.as_slice() {
Some(s) => s,
None => return data.clone(),
};
for i in 0..data.len() {
File diff suppressed because one or more lines are too long
@@ -2355,22 +2355,22 @@
"markdownDescription": "Denies the unminimize command without any pre-configured scope."
},
{
"description": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-message`\n- `allow-save`\n- `allow-open`",
"description": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`",
"type": "string",
"const": "dialog:default",
"markdownDescription": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-message`\n- `allow-save`\n- `allow-open`"
"markdownDescription": "This permission set configures the types of dialogs\navailable from the dialog plugin.\n\n#### Granted Permissions\n\nAll dialog types are enabled.\n\n\n\n#### This default permission set includes:\n\n- `allow-ask`\n- `allow-confirm`\n- `allow-message`\n- `allow-save`\n- `allow-open`"
},
{
"description": "Enables the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)",
"description": "Enables the ask command without any pre-configured scope.",
"type": "string",
"const": "dialog:allow-ask",
"markdownDescription": "Enables the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)"
"markdownDescription": "Enables the ask command without any pre-configured scope."
},
{
"description": "Enables the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)",
"description": "Enables the confirm command without any pre-configured scope.",
"type": "string",
"const": "dialog:allow-confirm",
"markdownDescription": "Enables the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `allow-message` and will be removed in v3)"
"markdownDescription": "Enables the confirm command without any pre-configured scope."
},
{
"description": "Enables the message command without any pre-configured scope.",
@@ -2391,16 +2391,16 @@
"markdownDescription": "Enables the save command without any pre-configured scope."
},
{
"description": "Denies the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)",
"description": "Denies the ask command without any pre-configured scope.",
"type": "string",
"const": "dialog:deny-ask",
"markdownDescription": "Denies the ask command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)"
"markdownDescription": "Denies the ask command without any pre-configured scope."
},
{
"description": "Denies the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)",
"description": "Denies the confirm command without any pre-configured scope.",
"type": "string",
"const": "dialog:deny-confirm",
"markdownDescription": "Denies the confirm command without any pre-configured scope. (**DEPRECATED**: This is now an alias to `deny-message` and will be removed in v3)"
"markdownDescription": "Denies the confirm command without any pre-configured scope."
},
{
"description": "Denies the message command without any pre-configured scope.",
File diff suppressed because it is too large Load Diff
@@ -1,16 +1,16 @@
use std::net::{SocketAddr, UdpSocket};
use std::time::Duration;
use flume::RecvTimeoutError;
use mdns_sd::{ServiceDaemon, ServiceEvent};
use serde::Serialize;
use tauri::State;
use tokio::time::timeout;
use tokio_serial::available_ports;
use flume::RecvTimeoutError;
use crate::domain::node::{
Chip, DiscoveredNode, DiscoveryMethod, HealthStatus, MacAddress, MeshRole, NodeCapabilities,
NodeRegistry,
Chip, DiscoveredNode, DiscoveryMethod, HealthStatus, MacAddress, MeshRole,
NodeCapabilities, NodeRegistry,
};
use crate::state::AppState;
@@ -110,16 +110,14 @@ async fn discover_via_mdns(timeout_duration: Duration) -> Result<Vec<DiscoveredN
_ => MeshRole::Node,
};
let node = DiscoveredNode {
ip: info
.get_addresses()
ip: info.get_addresses()
.iter()
.next()
.map(|a| a.to_string())
.unwrap_or_default(),
mac: props.get("mac").map(|v| v.val_str().to_string()),
hostname: Some(info.get_hostname().to_string()),
node_id: props
.get("node_id")
node_id: props.get("node_id")
.and_then(|v| v.val_str().parse().ok())
.unwrap_or(0),
firmware_version: props.get("version").map(|v| v.val_str().to_string()),
@@ -129,18 +127,11 @@ async fn discover_via_mdns(timeout_duration: Duration) -> Result<Vec<DiscoveredN
mesh_role,
discovery_method: DiscoveryMethod::Mdns,
tdm_slot: props.get("tdm_slot").and_then(|v| v.val_str().parse().ok()),
tdm_total: props
.get("tdm_total")
.and_then(|v| v.val_str().parse().ok()),
edge_tier: props
.get("edge_tier")
.and_then(|v| v.val_str().parse().ok()),
tdm_total: props.get("tdm_total").and_then(|v| v.val_str().parse().ok()),
edge_tier: props.get("edge_tier").and_then(|v| v.val_str().parse().ok()),
uptime_secs: props.get("uptime").and_then(|v| v.val_str().parse().ok()),
capabilities: Some(NodeCapabilities {
wasm: props
.get("wasm")
.map(|v| v.val_str() == "1")
.unwrap_or(false),
wasm: props.get("wasm").map(|v| v.val_str() == "1").unwrap_or(false),
ota: props.get("ota").map(|v| v.val_str() == "1").unwrap_or(true),
csi: props.get("csi").map(|v| v.val_str() == "1").unwrap_or(true),
}),
@@ -162,12 +153,7 @@ async fn discover_via_mdns(timeout_duration: Duration) -> Result<Vec<DiscoveredN
discovered
});
match timeout(
timeout_duration + Duration::from_millis(500),
discovery_task,
)
.await
{
match timeout(timeout_duration + Duration::from_millis(500), discovery_task).await {
Ok(Ok(nodes)) => Ok(nodes),
Ok(Err(e)) => Err(format!("mDNS discovery task failed: {}", e)),
Err(_) => Ok(Vec::new()), // Timeout, return empty
@@ -224,12 +210,7 @@ async fn discover_via_udp(timeout_duration: Duration) -> Result<Vec<DiscoveredNo
discovered
});
match timeout(
timeout_duration + Duration::from_millis(500),
discovery_task,
)
.await
{
match timeout(timeout_duration + Duration::from_millis(500), discovery_task).await {
Ok(Ok(nodes)) => Ok(nodes),
Ok(Err(e)) => Err(format!("UDP discovery task failed: {}", e)),
Err(_) => Ok(Vec::new()),
@@ -314,14 +295,16 @@ pub async fn list_serial_ports() -> Result<Vec<SerialPortInfo>, String> {
for port in ports {
tracing::debug!("Processing port: {}", port.port_name);
let info = match port.port_type {
tokio_serial::SerialPortType::UsbPort(usb_info) => SerialPortInfo {
name: port.port_name,
vid: Some(usb_info.vid),
pid: Some(usb_info.pid),
manufacturer: usb_info.manufacturer,
serial_number: usb_info.serial_number,
is_esp32_compatible: is_esp32_compatible(usb_info.vid, usb_info.pid),
},
tokio_serial::SerialPortType::UsbPort(usb_info) => {
SerialPortInfo {
name: port.port_name,
vid: Some(usb_info.vid),
pid: Some(usb_info.pid),
manufacturer: usb_info.manufacturer,
serial_number: usb_info.serial_number,
is_esp32_compatible: is_esp32_compatible(usb_info.vid, usb_info.pid),
}
}
_ => {
SerialPortInfo {
name: port.port_name.clone(),
@@ -418,9 +401,7 @@ fn is_esp32_compatible(vid: u16, pid: u16) -> bool {
return true;
}
// FTDI
if vid == 0x0403
&& (pid == 0x6001 || pid == 0x6010 || pid == 0x6011 || pid == 0x6014 || pid == 0x6015)
{
if vid == 0x0403 && (pid == 0x6001 || pid == 0x6010 || pid == 0x6011 || pid == 0x6014 || pid == 0x6015) {
return true;
}
// ESP32-S2/S3 native USB
@@ -469,12 +450,9 @@ pub async fn configure_esp32_wifi(
let _ = serial.read(&mut buf);
// Send command
serial
.write_all(cmd.as_bytes())
serial.write_all(cmd.as_bytes())
.map_err(|e| format!("Failed to write: {}", e))?;
serial
.flush()
.map_err(|e| format!("Failed to flush: {}", e))?;
serial.flush().map_err(|e| format!("Failed to flush: {}", e))?;
// Wait and read response
std::thread::sleep(Duration::from_millis(500));
@@ -487,8 +465,7 @@ pub async fn configure_esp32_wifi(
// Check for success indicators
if text.to_lowercase().contains("ok")
|| text.to_lowercase().contains("saved")
|| text.to_lowercase().contains("configured")
{
|| text.to_lowercase().contains("configured") {
tracing::info!("WiFi config successful: {}", text.trim());
return Ok(format!("WiFi configured! Response: {}", text.trim()));
}
@@ -37,16 +37,13 @@ pub async fn flash_firmware(
let firmware_hash = calculate_sha256(&firmware_path)?;
// Emit flash started event
let _ = app.emit(
"flash-progress",
FlashProgress {
phase: "connecting".into(),
progress_pct: 0.0,
bytes_written: 0,
bytes_total: firmware_size,
message: Some(format!("Connecting to {} ...", port)),
},
);
let _ = app.emit("flash-progress", FlashProgress {
phase: "connecting".into(),
progress_pct: 0.0,
bytes_written: 0,
bytes_total: firmware_size,
message: Some(format!("Connecting to {} ...", port)),
});
// Build espflash command
let baud_rate = baud.unwrap_or(921600);
@@ -70,12 +67,13 @@ pub async fn flash_firmware(
cmd.stderr(Stdio::piped());
// Spawn the process
let mut child = cmd
.spawn()
let mut child = cmd.spawn()
.map_err(|e| format!("Failed to start espflash: {}. Is espflash installed?", e))?;
let _stdout = child.stdout.take().ok_or("Failed to capture stdout")?;
let stderr = child.stderr.take().ok_or("Failed to capture stderr")?;
let _stdout = child.stdout.take()
.ok_or("Failed to capture stdout")?;
let stderr = child.stderr.take()
.ok_or("Failed to capture stderr")?;
// Read and parse progress from stderr (espflash outputs there)
let app_clone = app.clone();
@@ -86,8 +84,8 @@ pub async fn flash_firmware(
let mut last_phase = "connecting".to_string();
let mut last_progress = 0.0f32;
for line in reader.lines().map_while(Result::ok) {
{
for line in reader.lines() {
if let Ok(line) = line {
// Parse espflash progress output
if line.contains("Connecting") {
last_phase = "connecting".to_string();
@@ -106,24 +104,19 @@ pub async fn flash_firmware(
last_progress = 95.0;
}
let _ = app_clone.emit(
"flash-progress",
FlashProgress {
phase: last_phase.clone(),
progress_pct: last_progress,
bytes_written: ((last_progress / 100.0) * firmware_size_clone as f32)
as u64,
bytes_total: firmware_size_clone,
message: Some(line),
},
);
let _ = app_clone.emit("flash-progress", FlashProgress {
phase: last_phase.clone(),
progress_pct: last_progress,
bytes_written: ((last_progress / 100.0) * firmware_size_clone as f32) as u64,
bytes_total: firmware_size_clone,
message: Some(line),
});
}
}
});
// Wait for completion
let status = child
.wait()
let status = child.wait()
.map_err(|e| format!("Failed to wait for espflash: {}", e))?;
// Wait for progress parsing to complete
@@ -133,16 +126,13 @@ pub async fn flash_firmware(
if status.success() {
// Emit completion
let _ = app.emit(
"flash-progress",
FlashProgress {
phase: "completed".into(),
progress_pct: 100.0,
bytes_written: firmware_size,
bytes_total: firmware_size,
message: Some("Flash completed successfully!".into()),
},
);
let _ = app.emit("flash-progress", FlashProgress {
phase: "completed".into(),
progress_pct: 100.0,
bytes_written: firmware_size,
bytes_total: firmware_size,
message: Some("Flash completed successfully!".into()),
});
Ok(FlashResult {
success: true,
@@ -151,16 +141,13 @@ pub async fn flash_firmware(
firmware_hash: Some(firmware_hash),
})
} else {
let _ = app.emit(
"flash-progress",
FlashProgress {
phase: "failed".into(),
progress_pct: 0.0,
bytes_written: 0,
bytes_total: firmware_size,
message: Some("Flash failed".into()),
},
);
let _ = app.emit("flash-progress", FlashProgress {
phase: "failed".into(),
progress_pct: 0.0,
bytes_written: 0,
bytes_total: firmware_size,
message: Some("Flash failed".into()),
});
Err(format!("espflash exited with status: {}", status))
}
@@ -212,7 +199,9 @@ pub async fn check_espflash() -> Result<EspflashInfo, String> {
.map_err(|_| "espflash not found. Please install: cargo install espflash")?;
if output.status.success() {
let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
let version = String::from_utf8_lossy(&output.stdout)
.trim()
.to_string();
Ok(EspflashInfo {
installed: true,
@@ -258,7 +247,8 @@ pub async fn supported_chips() -> Result<Vec<ChipInfo>, String> {
/// Calculate SHA-256 hash of a file.
fn calculate_sha256(path: &str) -> Result<String, String> {
let file = std::fs::File::open(path).map_err(|e| format!("Failed to open file: {}", e))?;
let file = std::fs::File::open(path)
.map_err(|e| format!("Failed to open file: {}", e))?;
let mut reader = BufReader::new(file);
let mut hasher = Sha256::new();
@@ -354,11 +344,13 @@ mod tests {
#[test]
fn test_chip_info() {
let chips = [ChipInfo {
id: "esp32".into(),
name: "ESP32".into(),
description: "Test".into(),
}];
let chips = vec![
ChipInfo {
id: "esp32".into(),
name: "ESP32".into(),
description: "Test".into(),
},
];
assert_eq!(chips.len(), 1);
assert_eq!(chips[0].id, "esp32");
}
@@ -37,19 +37,16 @@ pub async fn ota_update(
let start_time = std::time::Instant::now();
// Emit progress
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "preparing".into(),
progress_pct: 0.0,
message: Some("Reading firmware...".into()),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "preparing".into(),
progress_pct: 0.0,
message: Some("Reading firmware...".into()),
});
// Read firmware file
let mut file =
File::open(&firmware_path).map_err(|e| format!("Cannot read firmware: {}", e))?;
let mut file = File::open(&firmware_path)
.map_err(|e| format!("Cannot read firmware: {}", e))?;
let mut firmware_data = Vec::new();
file.read_to_end(&mut firmware_data)
@@ -73,18 +70,12 @@ pub async fn ota_update(
};
// Emit progress
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "uploading".into(),
progress_pct: 10.0,
message: Some(format!(
"Uploading {} bytes to {}...",
firmware_size, node_ip
)),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "uploading".into(),
progress_pct: 10.0,
message: Some(format!("Uploading {} bytes to {}...", firmware_size, node_ip)),
});
// Build HTTP client
let client = reqwest::Client::builder()
@@ -116,38 +107,30 @@ pub async fn ota_update(
request = request.header("X-OTA-SHA256", &firmware_hash);
// Send request
let response = request
.send()
.await
let response = request.send().await
.map_err(|e| format!("OTA upload failed: {}", e))?;
let status = response.status();
let body = response.text().await.unwrap_or_default();
if !status.is_success() {
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "failed".into(),
progress_pct: 0.0,
message: Some(format!("HTTP {}: {}", status, body)),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "failed".into(),
progress_pct: 0.0,
message: Some(format!("HTTP {}: {}", status, body)),
});
return Err(format!("OTA failed with HTTP {}: {}", status, body));
}
// Emit progress - upload complete
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "rebooting".into(),
progress_pct: 80.0,
message: Some("Waiting for node reboot...".into()),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "rebooting".into(),
progress_pct: 80.0,
message: Some("Waiting for node reboot...".into()),
});
// Wait for node to come back online
let reboot_ok = wait_for_reboot(&client, &node_ip, Duration::from_secs(30)).await;
@@ -155,15 +138,12 @@ pub async fn ota_update(
let duration = start_time.elapsed().as_secs_f64();
if reboot_ok {
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "completed".into(),
progress_pct: 100.0,
message: Some(format!("OTA completed in {:.1}s", duration)),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "completed".into(),
progress_pct: 100.0,
message: Some(format!("OTA completed in {:.1}s", duration)),
});
Ok(OtaResult {
success: true,
@@ -173,15 +153,12 @@ pub async fn ota_update(
duration_secs: Some(duration),
})
} else {
let _ = app.emit(
"ota-progress",
OtaProgress {
node_ip: node_ip.clone(),
phase: "warning".into(),
progress_pct: 90.0,
message: Some("Node may not have rebooted successfully".into()),
},
);
let _ = app.emit("ota-progress", OtaProgress {
node_ip: node_ip.clone(),
phase: "warning".into(),
progress_pct: 90.0,
message: Some("Node may not have rebooted successfully".into()),
});
Ok(OtaResult {
success: true,
@@ -213,16 +190,13 @@ pub async fn batch_ota_update(
let strategy = strategy.unwrap_or_else(|| "sequential".into());
let max_concurrent = max_concurrent.unwrap_or(1);
let _ = app.emit(
"batch-ota-progress",
BatchOtaProgress {
phase: "starting".into(),
total: total_nodes,
completed: 0,
failed: 0,
current_node: None,
},
);
let _ = app.emit("batch-ota-progress", BatchOtaProgress {
phase: "starting".into(),
total: total_nodes,
completed: 0,
failed: 0,
current_node: None,
});
let mut results = Vec::new();
let mut completed = 0;
@@ -238,26 +212,22 @@ pub async fn batch_ota_update(
let psk = std::sync::Arc::new(psk);
let app = std::sync::Arc::new(app.clone());
let tasks: Vec<_> = node_ips
.into_iter()
.map(|ip| {
let sem = semaphore.clone();
let fw_path = firmware_path.clone();
let psk_clone = psk.clone();
let app_clone = app.clone();
let tasks: Vec<_> = node_ips.into_iter().map(|ip| {
let sem = semaphore.clone();
let fw_path = firmware_path.clone();
let psk_clone = psk.clone();
let app_clone = app.clone();
async move {
let _permit = sem.acquire().await.unwrap();
ota_update(
(*app_clone).clone(),
ip,
(*fw_path).clone(),
(*psk_clone).clone(),
)
.await
}
})
.collect();
async move {
let _permit = sem.acquire().await.unwrap();
ota_update(
(*app_clone).clone(),
ip,
(*fw_path).clone(),
(*psk_clone).clone(),
).await
}
}).collect();
let task_results = futures::future::join_all(tasks).await;
@@ -287,19 +257,20 @@ pub async fn batch_ota_update(
_ => {
// Sequential execution (default)
for ip in node_ips {
let _ = app.emit(
"batch-ota-progress",
BatchOtaProgress {
phase: "updating".into(),
total: total_nodes,
completed,
failed,
current_node: Some(ip.clone()),
},
);
let _ = app.emit("batch-ota-progress", BatchOtaProgress {
phase: "updating".into(),
total: total_nodes,
completed,
failed,
current_node: Some(ip.clone()),
});
match ota_update(app.clone(), ip.clone(), firmware_path.clone(), psk.clone()).await
{
match ota_update(
app.clone(),
ip.clone(),
firmware_path.clone(),
psk.clone(),
).await {
Ok(r) => {
if r.success {
completed += 1;
@@ -325,16 +296,13 @@ pub async fn batch_ota_update(
let duration = start_time.elapsed().as_secs_f64();
let _ = app.emit(
"batch-ota-progress",
BatchOtaProgress {
phase: "completed".into(),
total: total_nodes,
completed,
failed,
current_node: None,
},
);
let _ = app.emit("batch-ota-progress", BatchOtaProgress {
phase: "completed".into(),
total: total_nodes,
completed,
failed,
current_node: None,
});
Ok(BatchOtaResult {
total: total_nodes,
@@ -363,10 +331,7 @@ pub async fn check_ota_endpoint(node_ip: String) -> Result<OtaEndpointInfo, Stri
// Try to parse as JSON
let version = serde_json::from_str::<serde_json::Value>(&body)
.ok()
.and_then(|v| {
v.get("version")
.and_then(|v| v.as_str().map(|s| s.to_string()))
});
.and_then(|v| v.get("version").and_then(|v| v.as_str().map(|s| s.to_string())));
Ok(OtaEndpointInfo {
reachable: true,
@@ -45,9 +45,9 @@ pub async fn provision_node(
// Open serial port
let port_settings = tokio_serial::SerialPortBuilderExt::open_native_async(
tokio_serial::new(&port, PROVISION_BAUD).timeout(Duration::from_millis(SERIAL_TIMEOUT_MS)),
)
.map_err(|e| format!("Failed to open serial port: {}", e))?;
tokio_serial::new(&port, PROVISION_BAUD)
.timeout(Duration::from_millis(SERIAL_TIMEOUT_MS))
).map_err(|e| format!("Failed to open serial port: {}", e))?;
let (mut reader, mut writer) = tokio::io::split(port_settings);
@@ -59,19 +59,17 @@ pub async fn provision_node(
};
let header_bytes = bincode_header(&header);
tokio::io::AsyncWriteExt::write_all(&mut writer, &header_bytes)
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, &header_bytes).await
.map_err(|e| format!("Failed to send header: {}", e))?;
// Wait for ACK
let mut ack_buf = [0u8; 4];
tokio::time::timeout(
Duration::from_millis(SERIAL_TIMEOUT_MS),
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut ack_buf),
)
.await
.map_err(|_| "Timeout waiting for device acknowledgment")?
.map_err(|e| format!("Failed to read ACK: {}", e))?;
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut ack_buf)
).await
.map_err(|_| "Timeout waiting for device acknowledgment")?
.map_err(|e| format!("Failed to read ACK: {}", e))?;
if &ack_buf != b"ACK\n" {
return Err(format!("Invalid ACK response: {:?}", ack_buf));
@@ -80,8 +78,7 @@ pub async fn provision_node(
// Send NVS data in chunks
const CHUNK_SIZE: usize = 256;
for chunk in nvs_data.chunks(CHUNK_SIZE) {
tokio::io::AsyncWriteExt::write_all(&mut writer, chunk)
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, chunk).await
.map_err(|e| format!("Failed to send data chunk: {}", e))?;
// Small delay between chunks for device processing
@@ -89,23 +86,20 @@ pub async fn provision_node(
}
// Send checksum
tokio::io::AsyncWriteExt::write_all(&mut writer, checksum.as_bytes())
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, checksum.as_bytes()).await
.map_err(|e| format!("Failed to send checksum: {}", e))?;
tokio::io::AsyncWriteExt::write_all(&mut writer, b"\n")
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, b"\n").await
.map_err(|e| format!("Failed to send newline: {}", e))?;
// Wait for confirmation
let mut confirm_buf = [0u8; 32];
let confirm_len = tokio::time::timeout(
Duration::from_millis(SERIAL_TIMEOUT_MS * 2),
tokio::io::AsyncReadExt::read(&mut reader, &mut confirm_buf),
)
.await
.map_err(|_| "Timeout waiting for confirmation")?
.map_err(|e| format!("Failed to read confirmation: {}", e))?;
tokio::io::AsyncReadExt::read(&mut reader, &mut confirm_buf)
).await
.map_err(|_| "Timeout waiting for confirmation")?
.map_err(|e| format!("Failed to read confirmation: {}", e))?;
let confirm_str = String::from_utf8_lossy(&confirm_buf[..confirm_len]);
@@ -127,26 +121,24 @@ pub async fn provision_node(
pub async fn read_nvs(port: String) -> Result<ProvisioningConfig, String> {
// Open serial port
let port_settings = tokio_serial::SerialPortBuilderExt::open_native_async(
tokio_serial::new(&port, PROVISION_BAUD).timeout(Duration::from_millis(SERIAL_TIMEOUT_MS)),
)
.map_err(|e| format!("Failed to open serial port: {}", e))?;
tokio_serial::new(&port, PROVISION_BAUD)
.timeout(Duration::from_millis(SERIAL_TIMEOUT_MS))
).map_err(|e| format!("Failed to open serial port: {}", e))?;
let (mut reader, mut writer) = tokio::io::split(port_settings);
// Send read command
tokio::io::AsyncWriteExt::write_all(&mut writer, b"RUVIEW_NVS_READ\n")
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, b"RUVIEW_NVS_READ\n").await
.map_err(|e| format!("Failed to send read command: {}", e))?;
// Read size header
let mut size_buf = [0u8; 4];
tokio::time::timeout(
Duration::from_millis(SERIAL_TIMEOUT_MS),
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut size_buf),
)
.await
.map_err(|_| "Timeout waiting for NVS size")?
.map_err(|e| format!("Failed to read size: {}", e))?;
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut size_buf)
).await
.map_err(|_| "Timeout waiting for NVS size")?
.map_err(|e| format!("Failed to read size: {}", e))?;
let nvs_size = u32::from_le_bytes(size_buf) as usize;
@@ -158,11 +150,10 @@ pub async fn read_nvs(port: String) -> Result<ProvisioningConfig, String> {
let mut nvs_data = vec![0u8; nvs_size];
tokio::time::timeout(
Duration::from_millis(SERIAL_TIMEOUT_MS * 2),
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut nvs_data),
)
.await
.map_err(|_| "Timeout reading NVS data")?
.map_err(|e| format!("Failed to read NVS data: {}", e))?;
tokio::io::AsyncReadExt::read_exact(&mut reader, &mut nvs_data)
).await
.map_err(|_| "Timeout reading NVS data")?
.map_err(|e| format!("Failed to read NVS data: {}", e))?;
// Parse NVS data to config
deserialize_nvs_config(&nvs_data)
@@ -173,26 +164,24 @@ pub async fn read_nvs(port: String) -> Result<ProvisioningConfig, String> {
pub async fn erase_nvs(port: String) -> Result<ProvisionResult, String> {
// Open serial port
let port_settings = tokio_serial::SerialPortBuilderExt::open_native_async(
tokio_serial::new(&port, PROVISION_BAUD).timeout(Duration::from_millis(SERIAL_TIMEOUT_MS)),
)
.map_err(|e| format!("Failed to open serial port: {}", e))?;
tokio_serial::new(&port, PROVISION_BAUD)
.timeout(Duration::from_millis(SERIAL_TIMEOUT_MS))
).map_err(|e| format!("Failed to open serial port: {}", e))?;
let (mut reader, mut writer) = tokio::io::split(port_settings);
// Send erase command
tokio::io::AsyncWriteExt::write_all(&mut writer, b"RUVIEW_NVS_ERASE\n")
.await
tokio::io::AsyncWriteExt::write_all(&mut writer, b"RUVIEW_NVS_ERASE\n").await
.map_err(|e| format!("Failed to send erase command: {}", e))?;
// Wait for confirmation
let mut confirm_buf = [0u8; 32];
let confirm_len = tokio::time::timeout(
Duration::from_millis(SERIAL_TIMEOUT_MS * 3), // Erase takes longer
tokio::io::AsyncReadExt::read(&mut reader, &mut confirm_buf),
)
.await
.map_err(|_| "Timeout waiting for erase confirmation")?
.map_err(|e| format!("Failed to read confirmation: {}", e))?;
tokio::io::AsyncReadExt::read(&mut reader, &mut confirm_buf)
).await
.map_err(|_| "Timeout waiting for erase confirmation")?
.map_err(|e| format!("Failed to read confirmation: {}", e))?;
let confirm_str = String::from_utf8_lossy(&confirm_buf[..confirm_len]);
@@ -327,8 +316,7 @@ fn serialize_nvs_config(config: &ProvisioningConfig) -> Result<Vec<u8>, String>
write_u8(&mut data, "hop_count", hops);
}
if let Some(ref channels) = config.channel_list {
let ch_str: String = channels
.iter()
let ch_str: String = channels.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join(",");
@@ -371,8 +359,8 @@ fn deserialize_nvs_config(data: &[u8]) -> Result<ProvisioningConfig, String> {
return Err("Invalid NVS data: truncated key".into());
}
let key =
std::str::from_utf8(&data[pos..pos + key_len]).map_err(|_| "Invalid key encoding")?;
let key = std::str::from_utf8(&data[pos..pos + key_len])
.map_err(|_| "Invalid key encoding")?;
pos += key_len;
if pos + 2 > data.len() {
@@ -391,15 +379,9 @@ fn deserialize_nvs_config(data: &[u8]) -> Result<ProvisioningConfig, String> {
// Parse based on key
match key {
"wifi_ssid" => {
config.wifi_ssid = Some(String::from_utf8_lossy(value_bytes).to_string())
}
"wifi_pass" => {
config.wifi_password = Some(String::from_utf8_lossy(value_bytes).to_string())
}
"target_ip" => {
config.target_ip = Some(String::from_utf8_lossy(value_bytes).to_string())
}
"wifi_ssid" => config.wifi_ssid = Some(String::from_utf8_lossy(value_bytes).to_string()),
"wifi_pass" => config.wifi_password = Some(String::from_utf8_lossy(value_bytes).to_string()),
"target_ip" => config.target_ip = Some(String::from_utf8_lossy(value_bytes).to_string()),
"target_port" if value_len == 2 => {
config.target_port = Some(u16::from_le_bytes([value_bytes[0], value_bytes[1]]));
}
@@ -417,18 +399,16 @@ fn deserialize_nvs_config(data: &[u8]) -> Result<ProvisioningConfig, String> {
config.vital_window = Some(u16::from_le_bytes([value_bytes[0], value_bytes[1]]));
}
"vital_int" if value_len == 2 => {
config.vital_interval_ms =
Some(u16::from_le_bytes([value_bytes[0], value_bytes[1]]));
config.vital_interval_ms = Some(u16::from_le_bytes([value_bytes[0], value_bytes[1]]));
}
"top_k" if value_len == 1 => config.top_k_count = Some(value_bytes[0]),
"hop_count" if value_len == 1 => config.hop_count = Some(value_bytes[0]),
"channels" => {
let ch_str = String::from_utf8_lossy(value_bytes);
config.channel_list = Some(
ch_str
.split(',')
ch_str.split(',')
.filter_map(|s| s.trim().parse().ok())
.collect(),
.collect()
);
}
"power_duty" if value_len == 1 => config.power_duty = Some(value_bytes[0]),
@@ -504,11 +484,9 @@ mod tests {
#[test]
fn test_config_validation() {
let config = ProvisioningConfig {
tdm_slot: Some(5),
tdm_total: Some(4),
..ProvisioningConfig::default()
};
let mut config = ProvisioningConfig::default();
config.tdm_slot = Some(5);
config.tdm_total = Some(4);
let result = config.validate();
assert!(result.is_err());
@@ -117,12 +117,8 @@ pub async fn start_server(
cmd.stderr(Stdio::piped());
// Spawn the child process
let child = cmd.spawn().map_err(|e| {
format!(
"Failed to start server: {}. Is '{}' installed?",
e, server_path
)
})?;
let child = cmd.spawn()
.map_err(|e| format!("Failed to start server: {}. Is '{}' installed?", e, server_path))?;
let pid = child.id();
@@ -266,14 +262,12 @@ pub async fn server_status(state: State<'_, AppState>) -> Result<ServerStatusRes
});
}
// srv.pid.is_none() is checked above; the expect is unreachable in practice.
let pid = srv.pid.expect("pid checked as Some before this point");
let pid = srv.pid.unwrap();
let mut sys = System::new();
let sysinfo_pid = Pid::from_u32(pid);
sys.refresh_processes(ProcessesToUpdate::Some(&[sysinfo_pid]), true);
let (memory_mb, cpu_percent) = sys
.process(sysinfo_pid)
let (memory_mb, cpu_percent) = sys.process(sysinfo_pid)
.map(|proc| {
let mem = proc.memory() as f64 / 1024.0 / 1024.0;
let cpu = proc.cpu_usage();
@@ -282,9 +276,9 @@ pub async fn server_status(state: State<'_, AppState>) -> Result<ServerStatusRes
.unwrap_or((None, None));
// Calculate uptime if we have start time
let uptime_secs = srv
.start_time
.map(|start| std::time::Instant::now().duration_since(start).as_secs());
let uptime_secs = srv.start_time.map(|start| {
std::time::Instant::now().duration_since(start).as_secs()
});
Ok(ServerStatusResponse {
running: srv.running,
@@ -41,7 +41,8 @@ fn settings_path(app: &AppHandle) -> Result<PathBuf, String> {
.map_err(|e| format!("Failed to get app data dir: {}", e))?;
// Ensure directory exists
fs::create_dir_all(&app_dir).map_err(|e| format!("Failed to create app data dir: {}", e))?;
fs::create_dir_all(&app_dir)
.map_err(|e| format!("Failed to create app data dir: {}", e))?;
Ok(app_dir.join("settings.json"))
}
@@ -55,11 +56,11 @@ pub async fn get_settings(app: AppHandle) -> Result<Option<AppSettings>, String>
return Ok(None);
}
let contents =
fs::read_to_string(&path).map_err(|e| format!("Failed to read settings: {}", e))?;
let contents = fs::read_to_string(&path)
.map_err(|e| format!("Failed to read settings: {}", e))?;
let settings: AppSettings =
serde_json::from_str(&contents).map_err(|e| format!("Failed to parse settings: {}", e))?;
let settings: AppSettings = serde_json::from_str(&contents)
.map_err(|e| format!("Failed to parse settings: {}", e))?;
Ok(Some(settings))
}
@@ -72,7 +73,8 @@ pub async fn save_settings(app: AppHandle, settings: AppSettings) -> Result<(),
let contents = serde_json::to_string_pretty(&settings)
.map_err(|e| format!("Failed to serialize settings: {}", e))?;
fs::write(&path, contents).map_err(|e| format!("Failed to write settings: {}", e))?;
fs::write(&path, contents)
.map_err(|e| format!("Failed to write settings: {}", e))?;
Ok(())
}
@@ -22,19 +22,14 @@ pub async fn wasm_list(node_ip: String) -> Result<Vec<WasmModuleInfo>, String> {
let url = format!("http://{}:{}/wasm/list", node_ip, WASM_PORT);
let response = client
.get(&url)
.send()
.await
let response = client.get(&url).send().await
.map_err(|e| format!("Failed to connect to node: {}", e))?;
if !response.status().is_success() {
return Err(format!("Node returned HTTP {}", response.status()));
}
let modules: Vec<WasmModuleInfo> = response
.json()
.await
let modules: Vec<WasmModuleInfo> = response.json().await
.map_err(|e| format!("Failed to parse response: {}", e))?;
Ok(modules)
@@ -55,7 +50,8 @@ pub async fn wasm_upload(
auto_start: Option<bool>,
) -> Result<WasmUploadResult, String> {
// Read WASM file
let mut file = File::open(&wasm_path).map_err(|e| format!("Cannot read WASM file: {}", e))?;
let mut file = File::open(&wasm_path)
.map_err(|e| format!("Cannot read WASM file: {}", e))?;
let mut wasm_data = Vec::new();
file.read_to_end(&mut wasm_data)
@@ -103,8 +99,7 @@ pub async fn wasm_upload(
// Send request
let url = format!("http://{}:{}/wasm/upload", node_ip, WASM_PORT);
let response = client
.post(&url)
let response = client.post(&url)
.multipart(form)
.send()
.await
@@ -118,18 +113,13 @@ pub async fn wasm_upload(
}
// Parse response for module ID
let upload_response: WasmUploadResponse = response
.json()
.await
let upload_response: WasmUploadResponse = response.json().await
.map_err(|e| format!("Failed to parse upload response: {}", e))?;
Ok(WasmUploadResult {
success: true,
module_id: upload_response.module_id,
message: format!(
"Module '{}' uploaded successfully ({} bytes)",
name, wasm_size
),
message: format!("Module '{}' uploaded successfully ({} bytes)", name, wasm_size),
sha256: Some(wasm_hash),
})
}
@@ -166,10 +156,7 @@ pub async fn wasm_control(
node_ip, WASM_PORT, module_id, action
);
let response = client
.post(&url)
.send()
.await
let response = client.post(&url).send().await
.map_err(|e| format!("WASM control failed: {}", e))?;
let status = response.status();
@@ -192,7 +179,10 @@ pub async fn wasm_control(
/// Get detailed info about a specific WASM module.
#[tauri::command]
pub async fn wasm_info(node_ip: String, module_id: String) -> Result<WasmModuleDetail, String> {
pub async fn wasm_info(
node_ip: String,
module_id: String,
) -> Result<WasmModuleDetail, String> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(WASM_TIMEOUT_SECS))
.build()
@@ -200,19 +190,14 @@ pub async fn wasm_info(node_ip: String, module_id: String) -> Result<WasmModuleD
let url = format!("http://{}:{}/wasm/{}", node_ip, WASM_PORT, module_id);
let response = client
.get(&url)
.send()
.await
let response = client.get(&url).send().await
.map_err(|e| format!("Failed to get module info: {}", e))?;
if !response.status().is_success() {
return Err(format!("Module not found or HTTP {}", response.status()));
}
let detail: WasmModuleDetail = response
.json()
.await
let detail: WasmModuleDetail = response.json().await
.map_err(|e| format!("Failed to parse module info: {}", e))?;
Ok(detail)
@@ -228,19 +213,14 @@ pub async fn wasm_stats(node_ip: String) -> Result<WasmRuntimeStats, String> {
let url = format!("http://{}:{}/wasm/stats", node_ip, WASM_PORT);
let response = client
.get(&url)
.send()
.await
let response = client.get(&url).send().await
.map_err(|e| format!("Failed to get WASM stats: {}", e))?;
if !response.status().is_success() {
return Err(format!("HTTP {}", response.status()));
}
let stats: WasmRuntimeStats = response
.json()
.await
let stats: WasmRuntimeStats = response.json().await
.map_err(|e| format!("Failed to parse stats: {}", e))?;
Ok(stats)
@@ -266,16 +246,13 @@ pub async fn check_wasm_support(node_ip: String) -> Result<WasmSupportInfo, Stri
Ok(WasmSupportInfo {
supported: true,
max_modules: info
.as_ref()
max_modules: info.as_ref()
.and_then(|v| v.get("max_modules").and_then(|v| v.as_u64()))
.map(|v| v as u8),
memory_limit_kb: info
.as_ref()
memory_limit_kb: info.as_ref()
.and_then(|v| v.get("memory_limit_kb").and_then(|v| v.as_u64()))
.map(|v| v as u32),
verify_signatures: info
.as_ref()
verify_signatures: info.as_ref()
.and_then(|v| v.get("verify_signatures").and_then(|v| v.as_bool()))
.unwrap_or(false),
})
@@ -51,7 +51,10 @@ impl ProvisioningConfig {
}
if let Some(duty) = self.power_duty {
if !(10..=100).contains(&duty) {
return Err(format!("power_duty ({}) must be between 10 and 100", duty));
return Err(format!(
"power_duty ({}) must be between 10 and 100",
duty
));
}
}
Ok(())
+35 -3
View File
@@ -12,7 +12,6 @@ pub struct DiscoveryState {
}
/// Sub-state for the managed sensing server process.
#[derive(Default)]
pub struct ServerState {
pub running: bool,
pub pid: Option<u32>,
@@ -23,6 +22,20 @@ pub struct ServerState {
pub start_time: Option<Instant>,
}
impl Default for ServerState {
fn default() -> Self {
Self {
running: false,
pid: None,
http_port: None,
ws_port: None,
udp_port: None,
child: None,
start_time: None,
}
}
}
/// Sub-state for flash progress tracking.
#[derive(Default)]
pub struct FlashState {
@@ -60,14 +73,21 @@ impl Default for OtaUpdateTracker {
}
/// Sub-state for application settings cache.
#[derive(Default)]
pub struct SettingsState {
pub loaded: bool,
pub dirty: bool,
}
impl Default for SettingsState {
fn default() -> Self {
Self {
loaded: false,
dirty: false,
}
}
}
/// Top-level application state managed by Tauri.
#[derive(Default)]
pub struct AppState {
pub discovery: Mutex<DiscoveryState>,
pub server: Mutex<ServerState>,
@@ -76,6 +96,18 @@ pub struct AppState {
pub settings: Mutex<SettingsState>,
}
impl Default for AppState {
fn default() -> Self {
Self {
discovery: Mutex::new(DiscoveryState::default()),
server: Mutex::new(ServerState::default()),
flash: Mutex::new(FlashState::default()),
ota: Mutex::new(OtaState::default()),
settings: Mutex::new(SettingsState::default()),
}
}
}
impl AppState {
/// Create a new AppState instance.
pub fn new() -> Self {
@@ -10,44 +10,23 @@
fn test_serial_port_detection_logic() {
// Test ESP32 VID/PID detection
// CP210x (Silicon Labs)
assert!(
is_esp32_vid_pid(0x10C4, 0xEA60),
"CP2102 should be detected"
);
assert!(
is_esp32_vid_pid(0x10C4, 0xEA70),
"CP2104 should be detected"
);
assert!(is_esp32_vid_pid(0x10C4, 0xEA60), "CP2102 should be detected");
assert!(is_esp32_vid_pid(0x10C4, 0xEA70), "CP2104 should be detected");
// CH340/CH341 (QinHeng)
assert!(is_esp32_vid_pid(0x1A86, 0x7523), "CH340 should be detected");
assert!(is_esp32_vid_pid(0x1A86, 0x5523), "CH341 should be detected");
// FTDI
assert!(
is_esp32_vid_pid(0x0403, 0x6001),
"FTDI FT232 should be detected"
);
assert!(
is_esp32_vid_pid(0x0403, 0x6010),
"FTDI FT2232 should be detected"
);
assert!(is_esp32_vid_pid(0x0403, 0x6001), "FTDI FT232 should be detected");
assert!(is_esp32_vid_pid(0x0403, 0x6010), "FTDI FT2232 should be detected");
// ESP32 native USB
assert!(
is_esp32_vid_pid(0x303A, 0x1001),
"ESP32-S2/S3 native should be detected"
);
assert!(is_esp32_vid_pid(0x303A, 0x1001), "ESP32-S2/S3 native should be detected");
// Unknown device
assert!(
!is_esp32_vid_pid(0x0000, 0x0000),
"Unknown VID/PID should not be detected"
);
assert!(
!is_esp32_vid_pid(0x1234, 0x5678),
"Random VID/PID should not be detected"
);
assert!(!is_esp32_vid_pid(0x0000, 0x0000), "Unknown VID/PID should not be detected");
assert!(!is_esp32_vid_pid(0x1234, 0x5678), "Random VID/PID should not be detected");
}
fn is_esp32_vid_pid(vid: u16, pid: u16) -> bool {
@@ -60,9 +39,7 @@ fn is_esp32_vid_pid(vid: u16, pid: u16) -> bool {
return true;
}
// FTDI
if vid == 0x0403
&& (pid == 0x6001 || pid == 0x6010 || pid == 0x6011 || pid == 0x6014 || pid == 0x6015)
{
if vid == 0x0403 && (pid == 0x6001 || pid == 0x6010 || pid == 0x6011 || pid == 0x6014 || pid == 0x6015) {
return true;
}
// ESP32-S2/S3 native USB
@@ -101,14 +78,8 @@ fn test_settings_structure() {
// Check default values
assert!(!settings.theme.is_empty(), "Theme should have a default");
assert!(
settings.discover_interval_ms > 0,
"Discovery interval should be positive"
);
assert!(
settings.auto_discover,
"Auto-discover should default to true"
);
assert!(settings.discover_interval_ms > 0, "Discovery interval should be positive");
assert!(settings.auto_discover, "Auto-discover should default to true");
assert_eq!(settings.server_http_port, 8080);
}
@@ -157,10 +128,7 @@ fn test_chip_variants() {
for chip in chips {
let name = format!("{:?}", chip).to_lowercase();
assert!(
name.starts_with("esp32"),
"All chips should be ESP32 variants"
);
assert!(name.starts_with("esp32"), "All chips should be ESP32 variants");
}
}
@@ -184,7 +152,7 @@ fn test_progress_parsing() {
#[test]
fn test_sha256_hash() {
use sha2::{Digest, Sha256};
use sha2::{Sha256, Digest};
let data = b"test firmware data";
let mut hasher = Sha256::new();
@@ -210,11 +178,7 @@ fn test_hmac_signature() {
let result = mac.finalize();
let signature = hex::encode(result.into_bytes());
assert_eq!(
signature.len(),
64,
"HMAC-SHA256 should produce 64 hex characters"
);
assert_eq!(signature.len(), 64, "HMAC-SHA256 should produce 64 hex characters");
}
// ============================================================================
@@ -341,7 +305,11 @@ fn test_discovery_method_variants() {
fn test_mesh_role_variants() {
use wifi_densepose_desktop::domain::node::MeshRole;
let roles = vec![MeshRole::Coordinator, MeshRole::Aggregator, MeshRole::Node];
let roles = vec![
MeshRole::Coordinator,
MeshRole::Aggregator,
MeshRole::Node,
];
for role in roles {
let json = serde_json::to_string(&role).expect("Should serialize");
@@ -375,18 +343,14 @@ fn test_wifi_config_command_format() {
}
#[test]
#[allow(clippy::const_is_empty)]
fn test_wifi_credentials_validation() {
// SSID: 1-32 characters
let valid_ssid = "MyNetwork";
let empty_ssid = "";
let long_ssid = "A".repeat(33);
assert!(
!valid_ssid.is_empty() && valid_ssid.len() <= 32,
"SSID length must be 1-32"
);
assert!(empty_ssid.is_empty(), "empty_ssid must be empty");
assert!(!valid_ssid.is_empty() && valid_ssid.len() <= 32);
assert!(empty_ssid.is_empty());
assert!(long_ssid.len() > 32);
// Password: 8-63 characters for WPA2
@@ -406,7 +370,7 @@ fn test_wifi_credentials_validation() {
#[test]
fn test_node_registry() {
use wifi_densepose_desktop::domain::node::{
Chip, DiscoveredNode, DiscoveryMethod, HealthStatus, MacAddress, MeshRole, NodeRegistry,
DiscoveredNode, MacAddress, NodeRegistry, HealthStatus, Chip, MeshRole, DiscoveryMethod
};
let mut registry = NodeRegistry::new();
@@ -13,43 +13,24 @@ async fn main() -> anyhow::Result<()> {
println!(" Location: {:.4}N, {:.4}W", loc.lat, loc.lon);
let bbox = GeoBBox::from_center(&loc, 300.0);
let tiles_list =
tiles::fetch_area(&tiles::TileProvider::Sentinel2Cloudless, &bbox, 16, &cache).await?;
println!(
" Tiles: {} ({:.0}KB)",
tiles_list.len(),
tiles_list.iter().map(|t| t.data.len()).sum::<usize>() as f64 / 1024.0
);
let tiles_list = tiles::fetch_area(&tiles::TileProvider::Sentinel2Cloudless, &bbox, 16, &cache).await?;
println!(" Tiles: {} ({:.0}KB)", tiles_list.len(),
tiles_list.iter().map(|t| t.data.len()).sum::<usize>() as f64 / 1024.0);
let dem = terrain::fetch_elevation(&loc, &cache).await?;
println!(
" Elevation: {:.0}m (grid {}x{})",
terrain::elevation_at(&dem, &loc),
dem.cols,
dem.rows
);
println!(" Elevation: {:.0}m (grid {}x{})", terrain::elevation_at(&dem, &loc), dem.cols, dem.rows);
let buildings = osm::fetch_buildings(&loc, 300.0).await.unwrap_or_default();
let roads = osm::fetch_roads(&loc, 300.0).await.unwrap_or_default();
println!(
" OSM: {} buildings, {} roads",
buildings.len(),
roads.len()
);
println!(" OSM: {} buildings, {} roads", buildings.len(), roads.len());
let weather = temporal::fetch_weather(&loc).await?;
println!(
" Weather: {:.0}°C humidity={:.0}% wind={:.1}m/s",
weather.temperature_c, weather.humidity_pct, weather.wind_speed_ms
);
println!(" Weather: {:.0}°C humidity={:.0}% wind={:.1}m/s",
weather.temperature_c, weather.humidity_pct, weather.wind_speed_ms);
let scene = GeoScene {
location: loc.clone(),
bbox,
elevation_m: terrain::elevation_at(&dem, &loc),
buildings,
roads,
tile_count: tiles_list.len(),
location: loc.clone(), bbox, elevation_m: terrain::elevation_at(&dem, &loc),
buildings, roads, tile_count: tiles_list.len(),
registration: register::auto_register(&loc),
last_updated: chrono::Utc::now().to_rfc3339(),
};
@@ -60,10 +41,7 @@ async fn main() -> anyhow::Result<()> {
Err(e) => println!(" Brain: {e}"),
}
println!(
"\n Total: {}ms | Cache: {:.0}KB",
t0.elapsed().as_millis(),
cache.size_bytes() as f64 / 1024.0
);
println!("\n Total: {}ms | Cache: {:.0}KB",
t0.elapsed().as_millis(), cache.size_bytes() as f64 / 1024.0);
Ok(())
}
+3 -9
View File
@@ -13,8 +13,8 @@ const DEFAULT_BRAIN_URL: &str = "http://127.0.0.1:9876";
pub(crate) fn brain_url() -> &'static str {
static BRAIN_URL: OnceLock<String> = OnceLock::new();
BRAIN_URL.get_or_init(|| {
let url =
std::env::var("RUVIEW_BRAIN_URL").unwrap_or_else(|_| DEFAULT_BRAIN_URL.to_string());
let url = std::env::var("RUVIEW_BRAIN_URL")
.unwrap_or_else(|_| DEFAULT_BRAIN_URL.to_string());
eprintln!(" wifi-densepose-geo: using brain URL {url}");
url
})
@@ -34,13 +34,7 @@ pub async fn store_geo_context(scene: &GeoScene) -> Result<u32> {
"category": "spatial-geo",
"content": summary,
});
if client
.post(format!("{}/memories", brain_url()))
.json(&body)
.send()
.await
.is_ok()
{
if client.post(format!("{}/memories", brain_url())).json(&body).send().await.is_ok() {
stored += 1;
}
+2 -5
View File
@@ -54,11 +54,8 @@ fn walkdir(path: &Path) -> u64 {
.flatten()
.filter_map(|e| e.ok())
.map(|e| {
if e.path().is_dir() {
walkdir(&e.path())
} else {
e.metadata().map(|m| m.len()).unwrap_or(0)
}
if e.path().is_dir() { walkdir(&e.path()) }
else { e.metadata().map(|m| m.len()).unwrap_or(0) }
})
.sum()
}
+4 -15
View File
@@ -1,6 +1,6 @@
//! Coordinate transforms — WGS84, UTM, ENU, tile math.
use crate::types::{GeoBBox, GeoPoint, TileCoord};
use crate::types::{GeoPoint, GeoBBox, TileCoord};
const WGS84_A: f64 = 6_378_137.0;
#[allow(dead_code)]
@@ -55,20 +55,9 @@ pub fn tile_bounds(coord: &TileCoord) -> GeoBBox {
let n = 2f64.powi(coord.z as i32);
let west = coord.x as f64 / n * 360.0 - 180.0;
let east = (coord.x + 1) as f64 / n * 360.0 - 180.0;
let north = (std::f64::consts::PI * (1.0 - 2.0 * coord.y as f64 / n))
.sinh()
.atan()
.to_degrees();
let south = (std::f64::consts::PI * (1.0 - 2.0 * (coord.y + 1) as f64 / n))
.sinh()
.atan()
.to_degrees();
GeoBBox {
south,
west,
north,
east,
}
let north = (std::f64::consts::PI * (1.0 - 2.0 * coord.y as f64 / n)).sinh().atan().to_degrees();
let south = (std::f64::consts::PI * (1.0 - 2.0 * (coord.y + 1) as f64 / n)).sinh().atan().to_degrees();
GeoBBox { south, west, north, east }
}
/// Get all tile coordinates covering a bounding box at a zoom level.
+10 -30
View File
@@ -12,15 +12,11 @@ pub async fn build_scene(radius_m: f64) -> Result<GeoScene> {
// 1. Locate
let cache_path = cache.base_dir.join("location.json");
let location = locate::get_location(cache_path.to_str().unwrap_or("")).await?;
eprintln!(
" Geo: located at {:.4}N, {:.4}W",
location.lat, location.lon
);
eprintln!(" Geo: located at {:.4}N, {:.4}W", location.lat, location.lon);
// 2. Fetch satellite tiles
let bbox = GeoBBox::from_center(&location, radius_m);
let tile_list =
tiles::fetch_area(&tiles::TileProvider::Sentinel2Cloudless, &bbox, 16, &cache).await?;
let tile_list = tiles::fetch_area(&tiles::TileProvider::Sentinel2Cloudless, &bbox, 16, &cache).await?;
eprintln!(" Geo: fetched {} satellite tiles", tile_list.len());
// 3. Fetch elevation
@@ -29,17 +25,9 @@ pub async fn build_scene(radius_m: f64) -> Result<GeoScene> {
eprintln!(" Geo: elevation {:.0}m ASL", elevation);
// 4. Fetch OSM buildings + roads
let buildings = osm::fetch_buildings(&location, radius_m)
.await
.unwrap_or_default();
let roads = osm::fetch_roads(&location, radius_m)
.await
.unwrap_or_default();
eprintln!(
" Geo: {} buildings, {} roads",
buildings.len(),
roads.len()
);
let buildings = osm::fetch_buildings(&location, radius_m).await.unwrap_or_default();
let roads = osm::fetch_roads(&location, radius_m).await.unwrap_or_default();
eprintln!(" Geo: {} buildings, {} roads", buildings.len(), roads.len());
// 5. Build registration
let mut reg_origin = location.clone();
@@ -62,9 +50,7 @@ pub async fn build_scene(radius_m: f64) -> Result<GeoScene> {
pub fn summarize(scene: &GeoScene) -> String {
let building_count = scene.buildings.len();
let road_count = scene.roads.len();
let road_names: Vec<&str> = scene
.roads
.iter()
let road_names: Vec<&str> = scene.roads.iter()
.filter_map(|r| match r {
OsmFeature::Road { name, .. } => name.as_deref(),
_ => None,
@@ -76,16 +62,10 @@ pub fn summarize(scene: &GeoScene) -> String {
"Location: {:.4}N, {:.4}W, elevation {:.0}m ASL. \
{} buildings within view. {} roads nearby{}. \
{} satellite tiles at zoom 16. Updated: {}.",
scene.location.lat,
scene.location.lon,
scene.elevation_m,
building_count,
road_count,
if road_names.is_empty() {
String::new()
} else {
format!(" ({})", road_names.join(", "))
},
scene.location.lat, scene.location.lon, scene.elevation_m,
building_count, road_count,
if road_names.is_empty() { String::new() }
else { format!(" ({})", road_names.join(", ")) },
scene.tile_count,
&scene.last_updated[..10],
)
+6 -6
View File
@@ -4,16 +4,16 @@
//! SRTM elevation, OSM buildings/roads, coordinate transforms,
//! temporal change tracking, and brain memory integration.
pub mod brain;
pub mod cache;
pub mod types;
pub mod coord;
pub mod fuse;
pub mod locate;
pub mod cache;
pub mod tiles;
pub mod terrain;
pub mod osm;
pub mod register;
pub mod fuse;
pub mod brain;
pub mod temporal;
pub mod terrain;
pub mod tiles;
pub mod types;
pub use types::*;
+2 -4
View File
@@ -12,10 +12,8 @@ pub async fn locate_by_ip() -> Result<GeoPoint> {
// Primary: ip-api.com (free, 45 req/min)
let resp: serde_json::Value = client
.get("http://ip-api.com/json/?fields=lat,lon,city,regionName,country")
.send()
.await?
.json()
.await?;
.send().await?
.json().await?;
let lat = resp.get("lat").and_then(|v| v.as_f64()).unwrap_or(0.0);
let lon = resp.get("lon").and_then(|v| v.as_f64()).unwrap_or(0.0);
+23 -74
View File
@@ -13,9 +13,7 @@ pub const MAX_RADIUS_M: f64 = 5000.0;
fn check_radius(radius_m: f64) -> Result<()> {
if !radius_m.is_finite() || radius_m <= 0.0 {
return Err(anyhow!(
"radius_m must be positive and finite (got {radius_m})"
));
return Err(anyhow!("radius_m must be positive and finite (got {radius_m})"));
}
if radius_m > MAX_RADIUS_M {
return Err(anyhow!(
@@ -36,7 +34,8 @@ pub async fn fetch_buildings(center: &GeoPoint, radius_m: f64) -> Result<Vec<Osm
let bbox = GeoBBox::from_center(center, radius_m);
let query = format!(
r#"[out:json][timeout:25];(way["building"]({},{},{},{});relation["building"]({},{},{},{}););out body;>;out skel qt;"#,
bbox.south, bbox.west, bbox.north, bbox.east, bbox.south, bbox.west, bbox.north, bbox.east,
bbox.south, bbox.west, bbox.north, bbox.east,
bbox.south, bbox.west, bbox.north, bbox.east,
);
let resp = overpass_query(&query).await?;
parse_buildings(&resp)
@@ -60,11 +59,9 @@ async fn overpass_query(query: &str) -> Result<serde_json::Value> {
.user_agent("RuView/0.1")
.build()?;
let resp = client
.post(OVERPASS_URL)
let resp = client.post(OVERPASS_URL)
.form(&[("data", query)])
.send()
.await?;
.send().await?;
if !resp.status().is_success() {
anyhow::bail!("Overpass API error: {}", resp.status());
@@ -78,9 +75,7 @@ async fn overpass_query(query: &str) -> Result<serde_json::Value> {
/// top-level `elements` array (indicative of a malformed/non-Overpass payload).
pub fn parse_overpass_json(data: &serde_json::Value) -> Result<Vec<OsmFeature>> {
if !data.is_object() || data.get("elements").and_then(|e| e.as_array()).is_none() {
return Err(anyhow!(
"malformed Overpass response: missing `elements` array"
));
return Err(anyhow!("malformed Overpass response: missing `elements` array"));
}
parse_buildings(data)
}
@@ -89,11 +84,7 @@ pub(crate) fn parse_buildings(data: &serde_json::Value) -> Result<Vec<OsmFeature
let mut buildings = Vec::new();
let mut nodes: std::collections::HashMap<u64, [f64; 2]> = std::collections::HashMap::new();
let elements = data
.get("elements")
.and_then(|e| e.as_array())
.cloned()
.unwrap_or_default();
let elements = data.get("elements").and_then(|e| e.as_array()).cloned().unwrap_or_default();
// First pass: collect nodes
for el in &elements {
@@ -110,44 +101,24 @@ pub(crate) fn parse_buildings(data: &serde_json::Value) -> Result<Vec<OsmFeature
// Second pass: build ways
for el in &elements {
if el.get("type").and_then(|t| t.as_str()) != Some("way") {
continue;
}
if el.get("type").and_then(|t| t.as_str()) != Some("way") { continue; }
let tags = el.get("tags").cloned().unwrap_or(serde_json::json!({}));
if tags.get("building").is_none() {
continue;
}
if tags.get("building").is_none() { continue; }
let node_ids = el
.get("nodes")
.and_then(|n| n.as_array())
.cloned()
.unwrap_or_default();
let outline: Vec<[f64; 2]> = node_ids
.iter()
let node_ids = el.get("nodes").and_then(|n| n.as_array()).cloned().unwrap_or_default();
let outline: Vec<[f64; 2]> = node_ids.iter()
.filter_map(|id| id.as_u64().and_then(|id| nodes.get(&id).copied()))
.collect();
if outline.len() < 3 {
continue;
}
if outline.len() < 3 { continue; }
let height = tags
.get("height")
.and_then(|h| h.as_str())
let height = tags.get("height").and_then(|h| h.as_str())
.and_then(|s| s.trim_end_matches('m').trim().parse::<f32>().ok())
.or(Some(8.0)); // default building height
let name = tags
.get("name")
.and_then(|n| n.as_str())
.map(|s| s.to_string());
let name = tags.get("name").and_then(|n| n.as_str()).map(|s| s.to_string());
buildings.push(OsmFeature::Building {
outline,
height,
name,
});
buildings.push(OsmFeature::Building { outline, height, name });
}
Ok(buildings)
@@ -157,11 +128,7 @@ fn parse_roads(data: &serde_json::Value) -> Result<Vec<OsmFeature>> {
let mut roads = Vec::new();
let mut nodes: std::collections::HashMap<u64, [f64; 2]> = std::collections::HashMap::new();
let elements = data
.get("elements")
.and_then(|e| e.as_array())
.cloned()
.unwrap_or_default();
let elements = data.get("elements").and_then(|e| e.as_array()).cloned().unwrap_or_default();
for el in &elements {
if el.get("type").and_then(|t| t.as_str()) == Some("node") {
@@ -176,33 +143,19 @@ fn parse_roads(data: &serde_json::Value) -> Result<Vec<OsmFeature>> {
}
for el in &elements {
if el.get("type").and_then(|t| t.as_str()) != Some("way") {
continue;
}
if el.get("type").and_then(|t| t.as_str()) != Some("way") { continue; }
let tags = el.get("tags").cloned().unwrap_or(serde_json::json!({}));
let highway = tags.get("highway").and_then(|h| h.as_str());
if highway.is_none() {
continue;
}
if highway.is_none() { continue; }
let node_ids = el
.get("nodes")
.and_then(|n| n.as_array())
.cloned()
.unwrap_or_default();
let path: Vec<[f64; 2]> = node_ids
.iter()
let node_ids = el.get("nodes").and_then(|n| n.as_array()).cloned().unwrap_or_default();
let path: Vec<[f64; 2]> = node_ids.iter()
.filter_map(|id| id.as_u64().and_then(|id| nodes.get(&id).copied()))
.collect();
if path.len() < 2 {
continue;
}
if path.len() < 2 { continue; }
let name = tags
.get("name")
.and_then(|n| n.as_str())
.map(|s| s.to_string());
let name = tags.get("name").and_then(|n| n.as_str()).map(|s| s.to_string());
roads.push(OsmFeature::Road {
path,
@@ -256,11 +209,7 @@ mod tests {
#[tokio::test]
async fn fetch_buildings_rejects_oversized_radius() {
let center = GeoPoint {
lat: 43.0,
lon: -79.0,
alt: 0.0,
};
let center = GeoPoint { lat: 43.0, lon: -79.0, alt: 0.0 };
let err = fetch_buildings(&center, MAX_RADIUS_M + 1.0).await.err();
assert!(err.is_some(), "should reject radius > MAX_RADIUS_M");
}
+12 -33
View File
@@ -18,28 +18,13 @@ pub async fn fetch_weather(point: &GeoPoint) -> Result<WeatherData> {
.build()?;
let resp: serde_json::Value = client.get(&url).send().await?.json().await?;
let current = resp
.get("current")
.cloned()
.unwrap_or(serde_json::json!({}));
let current = resp.get("current").cloned().unwrap_or(serde_json::json!({}));
Ok(WeatherData {
temperature_c: current
.get("temperature_2m")
.and_then(|v| v.as_f64())
.unwrap_or(0.0) as f32,
humidity_pct: current
.get("relative_humidity_2m")
.and_then(|v| v.as_f64())
.unwrap_or(0.0) as f32,
wind_speed_ms: current
.get("wind_speed_10m")
.and_then(|v| v.as_f64())
.unwrap_or(0.0) as f32,
weather_code: current
.get("weather_code")
.and_then(|v| v.as_u64())
.unwrap_or(0) as u16,
temperature_c: current.get("temperature_2m").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32,
humidity_pct: current.get("relative_humidity_2m").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32,
wind_speed_ms: current.get("wind_speed_10m").and_then(|v| v.as_f64()).unwrap_or(0.0) as f32,
weather_code: current.get("weather_code").and_then(|v| v.as_u64()).unwrap_or(0) as u16,
})
}
@@ -48,8 +33,7 @@ pub async fn check_osm_changes(scene: &GeoScene, cache: &TileCache) -> Result<Ve
let mut changes = Vec::new();
let cache_key = "osm_building_count";
let prev_count: usize = cache
.get(cache_key)
let prev_count: usize = cache.get(cache_key)
.and_then(|d| String::from_utf8(d).ok())
.and_then(|s| s.trim().parse().ok())
.unwrap_or(0);
@@ -57,10 +41,7 @@ pub async fn check_osm_changes(scene: &GeoScene, cache: &TileCache) -> Result<Ve
let current_count = scene.buildings.len();
if prev_count > 0 && current_count != prev_count {
let diff = current_count as i64 - prev_count as i64;
changes.push(format!(
"Building count changed: {} → {} ({:+})",
prev_count, current_count, diff
));
changes.push(format!("Building count changed: {}{} ({:+})", prev_count, current_count, diff));
}
cache.put(cache_key, current_count.to_string().as_bytes())?;
@@ -218,7 +199,9 @@ pub fn is_night_at(lat_deg: f64, utc: chrono::DateTime<chrono::Utc>) -> bool {
// Solar declination (Spencer, 1971 — simplified)
let gamma = 2.0 * PI * (day_of_year - 1.0) / 365.0;
let decl = 0.006918 - 0.399912 * gamma.cos() + 0.070257 * gamma.sin()
let decl = 0.006918
- 0.399912 * gamma.cos()
+ 0.070257 * gamma.sin()
- 0.006758 * (2.0 * gamma).cos()
+ 0.000907 * (2.0 * gamma).sin();
@@ -307,9 +290,7 @@ mod tests {
.enable_all()
.build()
.unwrap();
let result = rt
.block_on(detect_tile_changes("test_tile_ident", &data, &cache))
.unwrap();
let result = rt.block_on(detect_tile_changes("test_tile_ident", &data, &cache)).unwrap();
assert!((result.diff_score - 0.0).abs() < 1e-9);
assert_eq!(result.changed_pixels, 0);
}
@@ -325,9 +306,7 @@ mod tests {
.enable_all()
.build()
.unwrap();
let result = rt
.block_on(detect_tile_changes("test_tile_diff", &new, &cache))
.unwrap();
let result = rt.block_on(detect_tile_changes("test_tile_diff", &new, &cache)).unwrap();
assert!((result.diff_score - 1.0).abs() < 1e-9);
}
}
+17 -38
View File
@@ -10,13 +10,7 @@ pub async fn fetch_elevation(point: &GeoPoint, cache: &TileCache) -> Result<Elev
let lon_int = point.lon.floor() as i32;
let ns = if lat_int >= 0 { 'N' } else { 'S' };
let ew = if lon_int >= 0 { 'E' } else { 'W' };
let filename = format!(
"{}{:02}{}{:03}.hgt",
ns,
lat_int.unsigned_abs(),
ew,
lon_int.unsigned_abs()
);
let filename = format!("{}{:02}{}{:03}.hgt", ns, lat_int.unsigned_abs(), ew, lon_int.unsigned_abs());
let cache_key = format!("srtm_{filename}");
if let Some(data) = cache.get(&cache_key) {
@@ -28,8 +22,9 @@ pub async fn fetch_elevation(point: &GeoPoint, cache: &TileCache) -> Result<Elev
.build()?;
// Primary: NASA SRTM public mirror (no auth required for .hgt)
let nasa_url =
format!("https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/{filename}");
let nasa_url = format!(
"https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/{filename}"
);
if let Ok(resp) = client.get(&nasa_url).send().await {
if resp.status().is_success() {
@@ -42,7 +37,9 @@ pub async fn fetch_elevation(point: &GeoPoint, cache: &TileCache) -> Result<Elev
// Fallback: viewfinderpanoramas.org
// Files are grouped by continent zip, but individual .hgt files can be
// fetched directly when the server exposes them.
let vfp_url = format!("http://viewfinderpanoramas.org/dem1/{filename}");
let vfp_url = format!(
"http://viewfinderpanoramas.org/dem1/{filename}"
);
if let Ok(resp) = client.get(&vfp_url).send().await {
if resp.status().is_success() {
@@ -57,8 +54,7 @@ pub async fn fetch_elevation(point: &GeoPoint, cache: &TileCache) -> Result<Elev
origin_lat: lat_int as f64,
origin_lon: lon_int as f64,
cell_size_deg: 1.0 / 3600.0,
cols: 100,
rows: 100,
cols: 100, rows: 100,
heights: vec![0.0; 10000],
})
}
@@ -68,24 +64,17 @@ pub fn parse_hgt(data: &[u8], origin_lat: f64, origin_lon: f64) -> Result<Elevat
let n_samples = data.len() / 2;
let side = (n_samples as f64).sqrt() as usize;
let heights: Vec<f32> = data
.chunks_exact(2)
let heights: Vec<f32> = data.chunks_exact(2)
.map(|c| {
let v = i16::from_be_bytes([c[0], c[1]]);
if v == -32768 {
0.0
} else {
v as f32
} // -32768 = void
if v == -32768 { 0.0 } else { v as f32 } // -32768 = void
})
.collect();
Ok(ElevationGrid {
origin_lat,
origin_lon,
origin_lat, origin_lon,
cell_size_deg: 1.0 / (side - 1) as f64,
cols: side,
rows: side,
cols: side, rows: side,
heights,
})
}
@@ -98,18 +87,10 @@ pub fn elevation_at(grid: &ElevationGrid, point: &GeoPoint) -> f32 {
/// Extract a small subgrid around a point.
pub fn extract_subgrid(grid: &ElevationGrid, center: &GeoPoint, radius_m: f64) -> ElevationGrid {
let radius_deg = radius_m / 111_320.0;
let min_row =
((grid.origin_lat + (grid.rows as f64 * grid.cell_size_deg) - center.lat - radius_deg)
/ grid.cell_size_deg)
.max(0.0) as usize;
let max_row = ((grid.origin_lat + (grid.rows as f64 * grid.cell_size_deg) - center.lat
+ radius_deg)
/ grid.cell_size_deg)
.min(grid.rows as f64) as usize;
let min_col =
((center.lon - radius_deg - grid.origin_lon) / grid.cell_size_deg).max(0.0) as usize;
let max_col = ((center.lon + radius_deg - grid.origin_lon) / grid.cell_size_deg)
.min(grid.cols as f64) as usize;
let min_row = ((grid.origin_lat + (grid.rows as f64 * grid.cell_size_deg) - center.lat - radius_deg) / grid.cell_size_deg).max(0.0) as usize;
let max_row = ((grid.origin_lat + (grid.rows as f64 * grid.cell_size_deg) - center.lat + radius_deg) / grid.cell_size_deg).min(grid.rows as f64) as usize;
let min_col = ((center.lon - radius_deg - grid.origin_lon) / grid.cell_size_deg).max(0.0) as usize;
let max_col = ((center.lon + radius_deg - grid.origin_lon) / grid.cell_size_deg).min(grid.cols as f64) as usize;
let rows = max_row.saturating_sub(min_row);
let cols = max_col.saturating_sub(min_col);
@@ -124,8 +105,6 @@ pub fn extract_subgrid(grid: &ElevationGrid, center: &GeoPoint, radius_m: f64) -
origin_lat: grid.origin_lat + (grid.rows - max_row) as f64 * grid.cell_size_deg,
origin_lon: grid.origin_lon + min_col as f64 * grid.cell_size_deg,
cell_size_deg: grid.cell_size_deg,
cols,
rows,
heights,
cols, rows, heights,
}
}
+4 -21
View File
@@ -43,19 +43,11 @@ impl TileProvider {
}
/// Fetch a single tile with caching.
pub async fn fetch_tile(
provider: &TileProvider,
coord: &TileCoord,
cache: &TileCache,
) -> Result<RasterTile> {
pub async fn fetch_tile(provider: &TileProvider, coord: &TileCoord, cache: &TileCache) -> Result<RasterTile> {
let cache_key = format!("tiles_{}_{}_{}.dat", coord.z, coord.x, coord.y);
if let Some(data) = cache.get(&cache_key) {
return Ok(RasterTile {
coord: coord.clone(),
data,
bounds: coord::tile_bounds(coord),
});
return Ok(RasterTile { coord: coord.clone(), data, bounds: coord::tile_bounds(coord) });
}
let url = provider.url(coord);
@@ -71,20 +63,11 @@ pub async fn fetch_tile(
let data = resp.bytes().await?.to_vec();
cache.put(&cache_key, &data)?;
Ok(RasterTile {
coord: coord.clone(),
data,
bounds: coord::tile_bounds(coord),
})
Ok(RasterTile { coord: coord.clone(), data, bounds: coord::tile_bounds(coord) })
}
/// Fetch all tiles covering a bounding box.
pub async fn fetch_area(
provider: &TileProvider,
bbox: &GeoBBox,
zoom: u8,
cache: &TileCache,
) -> Result<Vec<RasterTile>> {
pub async fn fetch_area(provider: &TileProvider, bbox: &GeoBBox, zoom: u8, cache: &TileCache) -> Result<Vec<RasterTile>> {
let coords = coord::tiles_for_bbox(bbox, zoom);
let mut tiles = Vec::with_capacity(coords.len());
for c in &coords {
+2 -7
View File
@@ -61,8 +61,7 @@ pub struct ElevationGrid {
impl ElevationGrid {
pub fn get(&self, lat: f64, lon: f64) -> Option<f32> {
let row = ((self.origin_lat + (self.rows as f64 * self.cell_size_deg) - lat)
/ self.cell_size_deg) as usize;
let row = ((self.origin_lat + (self.rows as f64 * self.cell_size_deg) - lat) / self.cell_size_deg) as usize;
let col = ((lon - self.origin_lon) / self.cell_size_deg) as usize;
if row < self.rows && col < self.cols {
Some(self.heights[row * self.cols + col])
@@ -98,11 +97,7 @@ pub struct GeoRegistration {
impl Default for GeoRegistration {
fn default() -> Self {
Self {
origin: GeoPoint {
lat: 0.0,
lon: 0.0,
alt: 0.0,
},
origin: GeoPoint { lat: 0.0, lon: 0.0, alt: 0.0 },
heading_deg: 0.0,
scale: 1.0,
}
+15 -63
View File
@@ -1,58 +1,26 @@
use wifi_densepose_geo::coord;
use wifi_densepose_geo::*;
use wifi_densepose_geo::coord;
#[test]
fn test_haversine() {
let toronto = GeoPoint {
lat: 43.6532,
lon: -79.3832,
alt: 0.0,
};
let ottawa = GeoPoint {
lat: 45.4215,
lon: -75.6972,
alt: 0.0,
};
let toronto = GeoPoint { lat: 43.6532, lon: -79.3832, alt: 0.0 };
let ottawa = GeoPoint { lat: 45.4215, lon: -75.6972, alt: 0.0 };
let dist = coord::haversine(&toronto, &ottawa);
assert!(
(dist - 353_000.0).abs() < 5_000.0,
"Toronto-Ottawa ~353km, got {:.0}m",
dist
);
assert!((dist - 353_000.0).abs() < 5_000.0, "Toronto-Ottawa ~353km, got {:.0}m", dist);
}
#[test]
fn test_wgs84_to_enu() {
let origin = GeoPoint {
lat: 43.0,
lon: -79.0,
alt: 100.0,
};
let point = GeoPoint {
lat: 43.001,
lon: -79.0,
alt: 100.0,
};
let origin = GeoPoint { lat: 43.0, lon: -79.0, alt: 100.0 };
let point = GeoPoint { lat: 43.001, lon: -79.0, alt: 100.0 };
let enu = coord::wgs84_to_enu(&point, &origin);
assert!(
(enu[1] - 111.0).abs() < 5.0,
"0.001 deg lat ~111m north, got {:.1}m",
enu[1]
);
assert!(
enu[0].abs() < 1.0,
"same longitude should have ~0 east, got {:.1}m",
enu[0]
);
assert!((enu[1] - 111.0).abs() < 5.0, "0.001 deg lat ~111m north, got {:.1}m", enu[1]);
assert!(enu[0].abs() < 1.0, "same longitude should have ~0 east, got {:.1}m", enu[0]);
}
#[test]
fn test_enu_roundtrip() {
let origin = GeoPoint {
lat: 43.6532,
lon: -79.3832,
alt: 76.0,
};
let origin = GeoPoint { lat: 43.6532, lon: -79.3832, alt: 76.0 };
let local = [100.0, 200.0, 5.0]; // 100m east, 200m north, 5m up
let geo = coord::enu_to_wgs84(&local, &origin);
let back = coord::wgs84_to_enu(&geo, &origin);
@@ -73,28 +41,16 @@ fn test_tile_coords() {
#[test]
fn test_tiles_for_bbox() {
let bbox = GeoBBox::from_center(
&GeoPoint {
lat: 43.6532,
lon: -79.3832,
alt: 0.0,
},
&GeoPoint { lat: 43.6532, lon: -79.3832, alt: 0.0 },
500.0,
);
let tiles = coord::tiles_for_bbox(&bbox, 16);
assert!(
tiles.len() >= 4 && tiles.len() <= 25,
"500m radius should need 4-25 tiles, got {}",
tiles.len()
);
assert!(tiles.len() >= 4 && tiles.len() <= 25, "500m radius should need 4-25 tiles, got {}", tiles.len());
}
#[test]
fn test_geo_bbox_from_center() {
let center = GeoPoint {
lat: 43.0,
lon: -79.0,
alt: 0.0,
};
let center = GeoPoint { lat: 43.0, lon: -79.0, alt: 0.0 };
let bbox = GeoBBox::from_center(&center, 1000.0);
assert!(bbox.south < 43.0 && bbox.north > 43.0);
assert!(bbox.west < -79.0 && bbox.east > -79.0);
@@ -114,18 +70,14 @@ fn test_hgt_parse() {
#[test]
fn test_registration() {
let origin = GeoPoint {
lat: 43.6532,
lon: -79.3832,
alt: 76.0,
};
let origin = GeoPoint { lat: 43.6532, lon: -79.3832, alt: 76.0 };
let reg = wifi_densepose_geo::register::auto_register(&origin);
let local = [10.0f32, 0.0, 20.0]; // 10m east, 20m forward
let geo = wifi_densepose_geo::register::local_to_wgs84(&reg, &local);
assert!((geo.lat - origin.lat).abs() < 0.001);
assert!((geo.lon - origin.lon).abs() < 0.001);
let back = wifi_densepose_geo::register::wgs84_to_local(&reg, &geo);
assert!((back[0] - local[0]).abs() < 0.1);
assert!((back[2] - local[2]).abs() < 0.1);
@@ -6,11 +6,12 @@
//! - Replay window check performance
//! - FramedMessage encode/decode throughput
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
use std::time::Duration;
use wifi_densepose_hardware::esp32::{
AuthenticatedBeacon, FramedMessage, MessageType, QuicTransportConfig, ReplayWindow, SecLevel,
SecureTdmConfig, SecureTdmCoordinator, SecurityMode, SyncBeacon, TdmSchedule,
TdmSchedule, SyncBeacon, SecurityMode, QuicTransportConfig,
SecureTdmCoordinator, SecureTdmConfig, SecLevel,
AuthenticatedBeacon, ReplayWindow, FramedMessage, MessageType,
};
fn make_beacon() -> SyncBeacon {
@@ -42,14 +43,12 @@ fn bench_beacon_serialize_authenticated(c: &mut Criterion) {
c.bench_function("beacon_serialize_28byte_auth", |b| {
b.iter(|| {
let tag = AuthenticatedBeacon::compute_tag(black_box(&msg), &key);
black_box(
AuthenticatedBeacon {
beacon: beacon.clone(),
nonce,
hmac_tag: tag,
}
.to_bytes(),
);
black_box(AuthenticatedBeacon {
beacon: beacon.clone(),
nonce,
hmac_tag: tag,
}
.to_bytes());
});
});
}
@@ -115,11 +114,15 @@ fn bench_framed_message_roundtrip(c: &mut Criterion) {
let msg = FramedMessage::new(MessageType::CsiFrame, payload);
let bytes = msg.to_bytes();
group.bench_with_input(BenchmarkId::new("encode", payload_size), &msg, |b, msg| {
b.iter(|| {
black_box(msg.to_bytes());
});
});
group.bench_with_input(
BenchmarkId::new("encode", payload_size),
&msg,
|b, msg| {
b.iter(|| {
black_box(msg.to_bytes());
});
},
);
group.bench_with_input(
BenchmarkId::new("decode", payload_size),
@@ -8,7 +8,7 @@
use std::collections::HashMap;
use std::io;
use std::net::{SocketAddr, UdpSocket};
use std::sync::mpsc::{self, Receiver, SyncSender};
use std::sync::mpsc::{self, SyncSender, Receiver};
use crate::csi_frame::CsiFrame;
use crate::esp32_parser::Esp32CsiParser;
@@ -58,7 +58,11 @@ impl NodeState {
fn update(&mut self, sequence: u32) -> u32 {
self.frames_received += 1;
let expected = self.last_sequence.wrapping_add(1);
let gap = sequence.saturating_sub(expected);
let gap = if sequence > expected {
sequence - expected
} else {
0
};
self.frames_dropped += gap as u64;
self.last_sequence = sequence;
gap
@@ -14,10 +14,7 @@ use wifi_densepose_hardware::{Esp32CsiParser, ParseError};
/// UDP aggregator for ESP32 CSI nodes (ADR-018).
#[derive(Parser)]
#[command(
name = "aggregator",
about = "Receive and display live CSI frames from ESP32 nodes"
)]
#[command(name = "aggregator", about = "Receive and display live CSI frames from ESP32 nodes")]
struct Cli {
/// Address:port to bind the UDP listener to.
#[arg(long, default_value = "0.0.0.0:5005")]
+15 -51
View File
@@ -79,7 +79,11 @@ mod tests {
use crate::csi_frame::{AntennaConfig, Bandwidth, CsiMetadata, SubcarrierData};
use chrono::Utc;
fn make_frame(node_id: u8, n_antennas: u8, subcarriers: Vec<SubcarrierData>) -> CsiFrame {
fn make_frame(
node_id: u8,
n_antennas: u8,
subcarriers: Vec<SubcarrierData>,
) -> CsiFrame {
let n_subcarriers = if n_antennas == 0 {
subcarriers.len()
} else {
@@ -111,16 +115,8 @@ mod tests {
#[test]
fn test_bridge_from_known_iq() {
let subs = vec![
SubcarrierData {
i: 3,
q: 4,
index: -1,
}, // amp = 5.0
SubcarrierData {
i: 0,
q: 10,
index: 1,
}, // amp = 10.0
SubcarrierData { i: 3, q: 4, index: -1 }, // amp = 5.0
SubcarrierData { i: 0, q: 10, index: 1 }, // amp = 10.0
];
let frame = make_frame(1, 1, subs);
let data: CsiData = frame.into();
@@ -134,36 +130,12 @@ mod tests {
fn test_bridge_multi_antenna() {
// 2 antennas, 3 subcarriers each = 6 total
let subs = vec![
SubcarrierData {
i: 1,
q: 0,
index: -1,
},
SubcarrierData {
i: 2,
q: 0,
index: 0,
},
SubcarrierData {
i: 3,
q: 0,
index: 1,
},
SubcarrierData {
i: 4,
q: 0,
index: -1,
},
SubcarrierData {
i: 5,
q: 0,
index: 0,
},
SubcarrierData {
i: 6,
q: 0,
index: 1,
},
SubcarrierData { i: 1, q: 0, index: -1 },
SubcarrierData { i: 2, q: 0, index: 0 },
SubcarrierData { i: 3, q: 0, index: 1 },
SubcarrierData { i: 4, q: 0, index: -1 },
SubcarrierData { i: 5, q: 0, index: 0 },
SubcarrierData { i: 6, q: 0, index: 1 },
];
let frame = make_frame(1, 2, subs);
let data: CsiData = frame.into();
@@ -176,11 +148,7 @@ mod tests {
#[test]
fn test_bridge_snr_computation() {
let subs = vec![SubcarrierData {
i: 1,
q: 0,
index: 0,
}];
let subs = vec![SubcarrierData { i: 1, q: 0, index: 0 }];
let frame = make_frame(1, 1, subs);
let data: CsiData = frame.into();
@@ -190,11 +158,7 @@ mod tests {
#[test]
fn test_bridge_preserves_metadata() {
let subs = vec![SubcarrierData {
i: 10,
q: 20,
index: 0,
}];
let subs = vec![SubcarrierData { i: 10, q: 20, index: 0 }];
let frame = make_frame(7, 1, subs);
let data: CsiData = frame.into();
@@ -28,15 +28,11 @@ impl CsiFrame {
/// - amplitude = sqrt(I^2 + Q^2)
/// - phase = atan2(Q, I)
pub fn to_amplitude_phase(&self) -> (Vec<f64>, Vec<f64>) {
let amplitudes: Vec<f64> = self
.subcarriers
.iter()
let amplitudes: Vec<f64> = self.subcarriers.iter()
.map(|sc| (sc.i as f64 * sc.i as f64 + sc.q as f64 * sc.q as f64).sqrt())
.collect();
let phases: Vec<f64> = self
.subcarriers
.iter()
let phases: Vec<f64> = self.subcarriers.iter()
.map(|sc| (sc.q as f64).atan2(sc.i as f64))
.collect();
@@ -48,9 +44,7 @@ impl CsiFrame {
if self.subcarriers.is_empty() {
return 0.0;
}
let sum: f64 = self
.subcarriers
.iter()
let sum: f64 = self.subcarriers.iter()
.map(|sc| (sc.i as f64 * sc.i as f64 + sc.q as f64 * sc.q as f64).sqrt())
.sum();
sum / self.subcarriers.len() as f64
@@ -58,7 +52,8 @@ impl CsiFrame {
/// Check if this frame has valid data (non-zero subcarriers with non-zero I/Q).
pub fn is_valid(&self) -> bool {
!self.subcarriers.is_empty() && self.subcarriers.iter().any(|sc| sc.i != 0 || sc.q != 0)
!self.subcarriers.is_empty()
&& self.subcarriers.iter().any(|sc| sc.i != 0 || sc.q != 0)
}
}
@@ -255,21 +250,9 @@ mod tests {
adr018_flags: Adr018Flags::default(),
},
subcarriers: vec![
SubcarrierData {
i: 100,
q: 0,
index: -28,
},
SubcarrierData {
i: 0,
q: 50,
index: -27,
},
SubcarrierData {
i: 30,
q: 40,
index: -26,
},
SubcarrierData { i: 100, q: 0, index: -28 },
SubcarrierData { i: 0, q: 50, index: -27 },
SubcarrierData { i: 30, q: 40, index: -26 },
],
}
}
+30 -8
View File
@@ -7,11 +7,17 @@ use thiserror::Error;
pub enum ParseError {
/// Not enough bytes in the buffer to parse a complete frame.
#[error("Insufficient data: need {needed} bytes, got {got}")]
InsufficientData { needed: usize, got: usize },
InsufficientData {
needed: usize,
got: usize,
},
/// The frame header magic bytes don't match expected values.
#[error("Invalid magic: expected {expected:#06x}, got {got:#06x}")]
InvalidMagic { expected: u32, got: u32 },
InvalidMagic {
expected: u32,
got: u32,
},
/// A recognized RuView wire packet was received that is *not* an
/// ADR-018 raw CSI frame (e.g. ADR-039 vitals, ADR-081 feature state,
@@ -20,25 +26,41 @@ pub enum ParseError {
/// interleaved with CSI frames — that is expected, not a corruption.
/// Consumers should route the packet to the matching decoder or skip it.
#[error("Non-CSI RuView packet on CSI socket: {kind} (magic {magic:#010x})")]
NonCsiPacket { magic: u32, kind: &'static str },
NonCsiPacket {
magic: u32,
kind: &'static str,
},
/// The frame indicates more subcarriers than physically possible.
#[error("Invalid subcarrier count: {count} (max {max})")]
InvalidSubcarrierCount { count: usize, max: usize },
InvalidSubcarrierCount {
count: usize,
max: usize,
},
/// The I/Q data buffer length doesn't match expected size.
#[error("I/Q data length mismatch: expected {expected}, got {got}")]
IqLengthMismatch { expected: usize, got: usize },
IqLengthMismatch {
expected: usize,
got: usize,
},
/// RSSI value is outside the valid range.
#[error("Invalid RSSI value: {value} dBm (expected -100..0)")]
InvalidRssi { value: i32 },
InvalidRssi {
value: i32,
},
/// Invalid antenna count (must be 1-4 for ESP32).
#[error("Invalid antenna count: {count} (expected 1-4)")]
InvalidAntennaCount { count: u8 },
InvalidAntennaCount {
count: u8,
},
/// Generic byte-level parse error.
#[error("Parse error at offset {offset}: {message}")]
ByteError { offset: usize, message: String },
ByteError {
offset: usize,
message: String,
},
}
@@ -9,18 +9,23 @@
//! - `quic_transport` -- QUIC-based authenticated transport for aggregator nodes
//! - `secure_tdm` -- Secured TDM protocol with dual-mode (QUIC / manual crypto)
pub mod tdm;
pub mod quic_transport;
pub mod secure_tdm;
pub mod tdm;
pub use tdm::{SyncBeacon, TdmCoordinator, TdmError, TdmSchedule, TdmSlot, TdmSlotCompleted};
pub use tdm::{
TdmSchedule, TdmCoordinator, TdmSlot, TdmSlotCompleted,
SyncBeacon, TdmError,
};
pub use quic_transport::{
ConnectionState, FramedMessage, MessageType, QuicTransportConfig, QuicTransportError,
QuicTransportHandle, SecurityMode, TransportStats, STREAM_BEACON, STREAM_CONTROL, STREAM_CSI,
SecurityMode, QuicTransportConfig, QuicTransportHandle, QuicTransportError,
TransportStats, ConnectionState, MessageType, FramedMessage,
STREAM_BEACON, STREAM_CSI, STREAM_CONTROL,
};
pub use secure_tdm::{
AuthenticatedBeacon, ReplayWindow, SecLevel, SecureCycleOutput, SecureTdmConfig,
SecureTdmCoordinator, SecureTdmError, AUTHENTICATED_BEACON_SIZE,
SecureTdmCoordinator, SecureTdmConfig, SecureTdmError,
SecLevel, AuthenticatedBeacon, SecureCycleOutput,
ReplayWindow, AUTHENTICATED_BEACON_SIZE,
};
@@ -41,17 +41,22 @@ pub const STREAM_CONTROL: u64 = 2;
/// Determines whether communication uses manual HMAC/SipHash over
/// plain UDP (for constrained ESP32-S3 devices) or QUIC with TLS 1.3
/// (for aggregator-class nodes).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecurityMode {
/// Manual HMAC-SHA256 beacon auth + SipHash-2-4 frame integrity
/// over plain UDP. Suitable for ESP32-S3 with limited memory.
ManualCrypto,
/// QUIC transport with TLS 1.3 AEAD encryption, built-in replay
/// protection, congestion control, and connection migration.
#[default]
QuicTransport,
}
impl Default for SecurityMode {
fn default() -> Self {
SecurityMode::QuicTransport
}
}
impl fmt::Display for SecurityMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -331,7 +336,8 @@ impl FramedMessage {
return None;
}
let msg_type = MessageType::from_byte(buf[0])?;
let payload_len = u32::from_le_bytes([buf[1], buf[2], buf[3], buf[4]]) as usize;
let payload_len =
u32::from_le_bytes([buf[1], buf[2], buf[3], buf[4]]) as usize;
let total = FRAMED_HEADER_SIZE + payload_len;
if buf.len() < total {
return None;
@@ -29,8 +29,8 @@
//! 4. Sent over plain UDP
use super::quic_transport::{
FramedMessage, MessageType, QuicTransportConfig, QuicTransportError, QuicTransportHandle,
SecurityMode,
FramedMessage, MessageType, QuicTransportConfig,
QuicTransportHandle, QuicTransportError, SecurityMode,
};
use super::tdm::{SyncBeacon, TdmCoordinator, TdmSchedule, TdmSlotCompleted};
use hmac::{Hmac, Mac};
@@ -59,7 +59,8 @@ pub const AUTHENTICATED_BEACON_SIZE: usize = 16 + NONCE_SIZE + HMAC_TAG_SIZE;
/// Default pre-shared key for testing (16 bytes). In production, this
/// would be loaded from NVS or a secure key store.
const DEFAULT_TEST_KEY: [u8; 16] = [
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
];
// ---------------------------------------------------------------------------
@@ -78,10 +79,7 @@ pub enum SecureTdmError {
/// QUIC transport error.
Transport(QuicTransportError),
/// The security mode does not match the incoming packet format.
ModeMismatch {
expected: SecurityMode,
got: SecurityMode,
},
ModeMismatch { expected: SecurityMode, got: SecurityMode },
/// The mesh key has not been provisioned.
NoMeshKey,
}
@@ -90,10 +88,7 @@ impl fmt::Display for SecureTdmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SecureTdmError::BeaconAuthFailed => write!(f, "Beacon HMAC verification failed"),
SecureTdmError::BeaconReplay {
nonce,
last_accepted,
} => {
SecureTdmError::BeaconReplay { nonce, last_accepted } => {
write!(
f,
"Beacon replay: nonce {} <= last_accepted {} - REPLAY_WINDOW",
@@ -101,19 +96,11 @@ impl fmt::Display for SecureTdmError {
)
}
SecureTdmError::BeaconTooShort { expected, got } => {
write!(
f,
"Beacon too short: expected {} bytes, got {}",
expected, got
)
write!(f, "Beacon too short: expected {} bytes, got {}", expected, got)
}
SecureTdmError::Transport(e) => write!(f, "Transport error: {}", e),
SecureTdmError::ModeMismatch { expected, got } => {
write!(
f,
"Security mode mismatch: expected {}, got {}",
expected, got
)
write!(f, "Security mode mismatch: expected {}, got {}", expected, got)
}
SecureTdmError::NoMeshKey => write!(f, "Mesh key not provisioned"),
}
@@ -267,7 +254,8 @@ impl AuthenticatedBeacon {
/// Uses the `hmac` + `sha2` crates for cryptographically secure
/// message authentication (ADR-050, Sprint 1).
pub fn compute_tag(payload_and_nonce: &[u8], key: &[u8; 16]) -> [u8; HMAC_TAG_SIZE] {
let mut mac = HmacSha256::new_from_slice(key).expect("HMAC-SHA256 accepts any key length");
let mut mac = HmacSha256::new_from_slice(key)
.expect("HMAC-SHA256 accepts any key length");
mac.update(payload_and_nonce);
let result = mac.finalize().into_bytes();
let mut tag = [0u8; HMAC_TAG_SIZE];
@@ -358,7 +346,10 @@ pub struct SecureTdmCoordinator {
impl SecureTdmCoordinator {
/// Create a new secure TDM coordinator.
pub fn new(schedule: TdmSchedule, config: SecureTdmConfig) -> Result<Self, SecureTdmError> {
pub fn new(
schedule: TdmSchedule,
config: SecureTdmConfig,
) -> Result<Self, SecureTdmError> {
let transport = if config.security_mode == SecurityMode::QuicTransport {
Some(QuicTransportHandle::new(config.quic_config.clone())?)
} else {
@@ -409,7 +400,10 @@ impl SecureTdmCoordinator {
}
SecurityMode::QuicTransport => {
let beacon_bytes = beacon.to_bytes();
let framed = FramedMessage::new(MessageType::Beacon, beacon_bytes.to_vec());
let framed = FramedMessage::new(
MessageType::Beacon,
beacon_bytes.to_vec(),
);
let wire = framed.to_bytes();
if let Some(ref mut transport) = self.transport {
@@ -455,11 +449,12 @@ impl SecureTdmCoordinator {
}
} else if buf.len() >= 16 && self.config.sec_level != SecLevel::Enforcing {
// Accept unauthenticated 16-byte beacon in permissive/transitional
let beacon =
SyncBeacon::from_bytes(buf).ok_or(SecureTdmError::BeaconTooShort {
let beacon = SyncBeacon::from_bytes(buf).ok_or(
SecureTdmError::BeaconTooShort {
expected: 16,
got: buf.len(),
})?;
},
)?;
self.beacons_verified += 1;
Ok(beacon)
} else {
@@ -471,11 +466,12 @@ impl SecureTdmCoordinator {
}
SecurityMode::QuicTransport => {
// In QUIC mode, extract beacon from framed message
let (framed, _) =
FramedMessage::from_bytes(buf).ok_or(SecureTdmError::BeaconTooShort {
let (framed, _) = FramedMessage::from_bytes(buf).ok_or(
SecureTdmError::BeaconTooShort {
expected: 5 + 16,
got: buf.len(),
})?;
},
)?;
if framed.message_type != MessageType::Beacon {
return Err(SecureTdmError::ModeMismatch {
expected: SecurityMode::QuicTransport,
@@ -500,7 +496,11 @@ impl SecureTdmCoordinator {
}
/// Complete a slot in the current cycle (delegates to inner coordinator).
pub fn complete_slot(&mut self, slot_index: usize, capture_quality: f32) -> TdmSlotCompleted {
pub fn complete_slot(
&mut self,
slot_index: usize,
capture_quality: f32,
) -> TdmSlotCompleted {
self.inner.complete_slot(slot_index, capture_quality)
}
@@ -755,7 +755,10 @@ mod tests {
#[test]
fn test_auth_beacon_too_short() {
let result = AuthenticatedBeacon::from_bytes(&[0u8; 10]);
assert!(matches!(result, Err(SecureTdmError::BeaconTooShort { .. })));
assert!(matches!(
result,
Err(SecureTdmError::BeaconTooShort { .. })
));
}
#[test]
@@ -767,7 +770,8 @@ mod tests {
#[test]
fn test_secure_coordinator_manual_create() {
let coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
assert_eq!(coord.security_mode(), SecurityMode::ManualCrypto);
assert_eq!(coord.beacons_produced(), 0);
assert!(coord.transport().is_none());
@@ -775,7 +779,8 @@ mod tests {
#[test]
fn test_secure_coordinator_manual_begin_cycle() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
assert_eq!(output.mode, SecurityMode::ManualCrypto);
@@ -787,7 +792,8 @@ mod tests {
#[test]
fn test_secure_coordinator_manual_nonce_increments() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
for expected_nonce in 1..=5u32 {
let _output = coord.begin_secure_cycle().unwrap();
@@ -801,37 +807,47 @@ mod tests {
#[test]
fn test_secure_coordinator_manual_verify_own_beacon() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
// Create a second coordinator to verify
let mut verifier = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let beacon = verifier.verify_beacon(&output.authenticated_bytes).unwrap();
let mut verifier =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let beacon = verifier
.verify_beacon(&output.authenticated_bytes)
.unwrap();
assert_eq!(beacon.cycle_id, 0);
}
#[test]
fn test_secure_coordinator_manual_reject_tampered() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
let mut tampered = output.authenticated_bytes.clone();
tampered[25] ^= 0xFF; // Tamper with HMAC tag
let mut verifier = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut verifier =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
assert!(verifier.verify_beacon(&tampered).is_err());
assert_eq!(verifier.verification_failures(), 1);
}
#[test]
fn test_secure_coordinator_manual_reject_replay() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
let mut verifier = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut verifier =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
// First acceptance succeeds
verifier.verify_beacon(&output.authenticated_bytes).unwrap();
verifier
.verify_beacon(&output.authenticated_bytes)
.unwrap();
// Replay of same beacon fails
let result = verifier.verify_beacon(&output.authenticated_bytes);
@@ -892,14 +908,16 @@ mod tests {
#[test]
fn test_secure_coordinator_quic_create() {
let coord = SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let coord =
SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
assert_eq!(coord.security_mode(), SecurityMode::QuicTransport);
assert!(coord.transport().is_some());
}
#[test]
fn test_secure_coordinator_quic_begin_cycle() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
assert_eq!(output.mode, SecurityMode::QuicTransport);
@@ -910,17 +928,22 @@ mod tests {
#[test]
fn test_secure_coordinator_quic_verify_own_beacon() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let output = coord.begin_secure_cycle().unwrap();
let mut verifier = SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let beacon = verifier.verify_beacon(&output.authenticated_bytes).unwrap();
let mut verifier =
SecureTdmCoordinator::new(test_schedule(), quic_config()).unwrap();
let beacon = verifier
.verify_beacon(&output.authenticated_bytes)
.unwrap();
assert_eq!(beacon.cycle_id, 0);
}
#[test]
fn test_secure_coordinator_complete_cycle() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
coord.begin_secure_cycle().unwrap();
for i in 0..4 {
@@ -932,7 +955,8 @@ mod tests {
#[test]
fn test_secure_coordinator_cycle_id_increments() {
let mut coord = SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let mut coord =
SecureTdmCoordinator::new(test_schedule(), manual_config()).unwrap();
let out0 = coord.begin_secure_cycle().unwrap();
assert_eq!(out0.beacon.cycle_id, 0);
@@ -962,10 +986,7 @@ mod tests {
let key2: [u8; 16] = [0x02; 16];
let tag1 = AuthenticatedBeacon::compute_tag(msg, &key1);
let tag2 = AuthenticatedBeacon::compute_tag(msg, &key2);
assert_ne!(
tag1, tag2,
"Different keys must produce different HMAC tags"
);
assert_ne!(tag1, tag2, "Different keys must produce different HMAC tags");
}
#[test]
@@ -973,10 +994,7 @@ mod tests {
let key: [u8; 16] = DEFAULT_TEST_KEY;
let tag1 = AuthenticatedBeacon::compute_tag(b"message one", &key);
let tag2 = AuthenticatedBeacon::compute_tag(b"message two", &key);
assert_ne!(
tag1, tag2,
"Different messages must produce different HMAC tags"
);
assert_ne!(tag1, tag2, "Different messages must produce different HMAC tags");
}
#[test]
@@ -1005,15 +1023,8 @@ mod tests {
msg[16..20].copy_from_slice(&nonce.to_le_bytes());
let tag = AuthenticatedBeacon::compute_tag(&msg, &correct_key);
let auth = AuthenticatedBeacon {
beacon,
nonce,
hmac_tag: tag,
};
assert!(
auth.verify(&wrong_key).is_err(),
"Wrong key must fail verification"
);
let auth = AuthenticatedBeacon { beacon, nonce, hmac_tag: tag };
assert!(auth.verify(&wrong_key).is_err(), "Wrong key must fail verification");
}
#[test]
@@ -1032,19 +1043,12 @@ mod tests {
msg[16..20].copy_from_slice(&nonce.to_le_bytes());
let tag = AuthenticatedBeacon::compute_tag(&msg, &key);
let auth = AuthenticatedBeacon {
beacon,
nonce,
hmac_tag: tag,
};
let auth = AuthenticatedBeacon { beacon, nonce, hmac_tag: tag };
let mut wire = auth.to_bytes();
// Flip one bit in the beacon payload
wire[0] ^= 0x01;
let tampered = AuthenticatedBeacon::from_bytes(&wire).unwrap();
assert!(
tampered.verify(&key).is_err(),
"Single bit flip must fail verification"
);
assert!(tampered.verify(&key).is_err(), "Single bit flip must fail verification");
}
#[test]
@@ -1059,8 +1063,7 @@ mod tests {
cycle_period: Duration::from_millis(50),
drift_correction_us: 0,
generated_at: std::time::Instant::now(),
}
.to_bytes();
}.to_bytes();
assert!(coord.verify_beacon(&raw).is_err());
}
@@ -67,38 +67,19 @@ impl fmt::Display for TdmError {
write!(f, "Invalid node count: {} (max {})", count, max)
}
TdmError::SlotIndexOutOfBounds { index, num_slots } => {
write!(
f,
"Slot index {} out of bounds (schedule has {} slots)",
index, num_slots
)
write!(f, "Slot index {} out of bounds (schedule has {} slots)", index, num_slots)
}
TdmError::UnknownNode { node_id } => {
write!(f, "Unknown node ID: {}", node_id)
}
TdmError::GuardIntervalTooLarge { guard_us, slot_us } => {
write!(
f,
"Guard interval {} us exceeds slot duration {} us",
guard_us, slot_us
)
write!(f, "Guard interval {} us exceeds slot duration {} us", guard_us, slot_us)
}
TdmError::CycleTooShort {
needed_us,
available_us,
} => {
write!(
f,
"Cycle too short: need {} us, have {} us",
needed_us, available_us
)
TdmError::CycleTooShort { needed_us, available_us } => {
write!(f, "Cycle too short: need {} us, have {} us", needed_us, available_us)
}
TdmError::DriftExceedsGuard { drift_us, guard_us } => {
write!(
f,
"Drift {:.1} us exceeds guard interval {} us",
drift_us, guard_us
)
write!(f, "Drift {:.1} us exceeds guard interval {} us", drift_us, guard_us)
}
}
}
@@ -293,10 +274,7 @@ impl TdmSchedule {
/// Check whether clock drift stays within the guard interval.
pub fn drift_within_guard(&self) -> bool {
let drift = self.max_drift_us();
let guard = self
.slots
.first()
.map_or(0, |s| s.guard_interval.as_micros() as u64);
let guard = self.slots.first().map_or(0, |s| s.guard_interval.as_micros() as u64);
drift < guard as f64
}
}
@@ -666,10 +644,7 @@ mod tests {
);
assert_eq!(
result.unwrap_err(),
TdmError::InvalidNodeCount {
count: 0,
max: MAX_NODES
}
TdmError::InvalidNodeCount { count: 0, max: MAX_NODES }
);
}
@@ -689,14 +664,11 @@ mod tests {
fn test_guard_interval_too_large() {
let result = TdmSchedule::uniform(
&[0, 1],
Duration::from_millis(1), // 1 ms slot
Duration::from_millis(2), // 2 ms guard > slot
Duration::from_millis(1), // 1 ms slot
Duration::from_millis(2), // 2 ms guard > slot
Duration::from_millis(30),
);
assert!(matches!(
result,
Err(TdmError::GuardIntervalTooLarge { .. })
));
assert!(matches!(result, Err(TdmError::GuardIntervalTooLarge { .. })));
}
#[test]
@@ -115,9 +115,10 @@ impl Esp32CsiParser {
let mut cursor = Cursor::new(data);
// Magic (offset 0, 4 bytes)
let magic = cursor
.read_u32::<LittleEndian>()
.map_err(|_| ParseError::InsufficientData { needed: 4, got: 0 })?;
let magic = cursor.read_u32::<LittleEndian>().map_err(|_| ParseError::InsufficientData {
needed: 4,
got: 0,
})?;
if magic != ESP32_CSI_MAGIC {
return Err(ParseError::InvalidMagic {
@@ -143,13 +144,10 @@ impl Esp32CsiParser {
}
// Number of subcarriers (offset 6, 2 bytes LE)
let n_subcarriers =
cursor
.read_u16::<LittleEndian>()
.map_err(|_| ParseError::ByteError {
offset: 6,
message: "Failed to read subcarrier count".into(),
})? as usize;
let n_subcarriers = cursor.read_u16::<LittleEndian>().map_err(|_| ParseError::ByteError {
offset: 6,
message: "Failed to read subcarrier count".into(),
})? as usize;
if n_subcarriers > MAX_SUBCARRIERS {
return Err(ParseError::InvalidSubcarrierCount {
@@ -159,21 +157,16 @@ impl Esp32CsiParser {
}
// Frequency MHz (offset 8, 4 bytes LE)
let channel_freq_mhz =
cursor
.read_u32::<LittleEndian>()
.map_err(|_| ParseError::ByteError {
offset: 8,
message: "Failed to read frequency".into(),
})?;
let channel_freq_mhz = cursor.read_u32::<LittleEndian>().map_err(|_| ParseError::ByteError {
offset: 8,
message: "Failed to read frequency".into(),
})?;
// Sequence number (offset 12, 4 bytes LE)
let sequence = cursor
.read_u32::<LittleEndian>()
.map_err(|_| ParseError::ByteError {
offset: 12,
message: "Failed to read sequence number".into(),
})?;
let sequence = cursor.read_u32::<LittleEndian>().map_err(|_| ParseError::ByteError {
offset: 12,
message: "Failed to read sequence number".into(),
})?;
// RSSI (offset 16, 1 byte signed)
let rssi_dbm = cursor.read_i8().map_err(|_| ParseError::ByteError {
@@ -472,17 +465,11 @@ mod tests {
RUVIEW_FEATURE_STATE_MAGIC,
RUVIEW_TEMPORAL_MAGIC,
] {
assert!(
ruview_sibling_packet_name(m).is_some(),
"{m:#010x} unclassified"
);
assert!(ruview_sibling_packet_name(m).is_some(), "{m:#010x} unclassified");
let mut data = vec![0u8; 24];
data[0..4].copy_from_slice(&m.to_le_bytes());
assert!(
matches!(
Esp32CsiParser::parse_frame(&data),
Err(ParseError::NonCsiPacket { .. })
),
matches!(Esp32CsiParser::parse_frame(&data), Err(ParseError::NonCsiPacket { .. })),
"{m:#010x} should parse as NonCsiPacket"
);
}
+13 -16
View File
@@ -34,13 +34,12 @@
//! }
//! ```
pub mod aggregator;
mod bridge;
mod csi_frame;
mod error;
pub mod esp32;
mod esp32_parser;
pub mod sync_packet;
pub mod aggregator;
mod bridge;
pub mod esp32;
// ADR-081: Rust mirror of the firmware radio abstraction layer (L1) and
// mesh sensing plane (L3). Lets host tests, simulators, and future
@@ -48,20 +47,18 @@ pub mod sync_packet;
// touching any downstream signal/ruvector/train/mat crate.
pub mod radio_ops;
pub use bridge::CsiData;
pub use csi_frame::{AntennaConfig, Bandwidth, CsiFrame, CsiMetadata, SubcarrierData};
pub use csi_frame::{CsiFrame, CsiMetadata, SubcarrierData, Bandwidth, AntennaConfig};
pub use error::ParseError;
pub use esp32_parser::{
ruview_sibling_packet_name, Esp32CsiParser, ESP32_CSI_MAGIC, RUVIEW_COMPRESSED_CSI_MAGIC,
RUVIEW_FEATURE_MAGIC, RUVIEW_FEATURE_STATE_MAGIC, RUVIEW_FUSED_VITALS_MAGIC,
RUVIEW_TEMPORAL_MAGIC, RUVIEW_VITALS_MAGIC,
};
pub use sync_packet::{
SyncPacket, SyncPacketFlags, SYNC_PACKET_MAGIC, SYNC_PACKET_SIZE, SYNC_PACKET_PROTO_VER,
Esp32CsiParser, ruview_sibling_packet_name, ESP32_CSI_MAGIC, RUVIEW_VITALS_MAGIC,
RUVIEW_FEATURE_MAGIC, RUVIEW_FUSED_VITALS_MAGIC, RUVIEW_COMPRESSED_CSI_MAGIC,
RUVIEW_FEATURE_STATE_MAGIC, RUVIEW_TEMPORAL_MAGIC,
};
pub use bridge::CsiData;
pub use radio_ops::{
crc32_ieee, decode_anomaly_alert, decode_mesh, decode_node_status, encode_health, AnomalyAlert,
AuthClass, CaptureProfile, MeshError, MeshHeader, MeshMsgType, MeshRole, MockRadio, NodeStatus,
RadioError, RadioHealth, RadioMode, RadioOps, MESH_HEADER_SIZE, MESH_MAGIC, MESH_MAX_PAYLOAD,
MESH_VERSION,
RadioOps, RadioMode, CaptureProfile, RadioHealth, RadioError, MockRadio,
MeshRole, MeshMsgType, AuthClass, MeshHeader, NodeStatus, AnomalyAlert,
MeshError, MESH_MAGIC, MESH_VERSION, MESH_HEADER_SIZE, MESH_MAX_PAYLOAD,
crc32_ieee, decode_mesh, decode_node_status, decode_anomaly_alert,
encode_health,
};
@@ -24,10 +24,10 @@ use std::convert::TryFrom;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum RadioMode {
Disabled = 0,
PassiveRx = 1,
ActiveProbe = 2,
Calibration = 3,
Disabled = 0,
PassiveRx = 1,
ActiveProbe = 2,
Calibration = 3,
}
/// Named capture profiles, mirror of `rv_capture_profile_t`.
@@ -35,10 +35,10 @@ pub enum RadioMode {
#[repr(u8)]
pub enum CaptureProfile {
PassiveLowRate = 0,
ActiveProbe = 1,
RespHighSens = 2,
FastMotion = 3,
Calibration = 4,
ActiveProbe = 1,
RespHighSens = 2,
FastMotion = 3,
Calibration = 4,
}
impl TryFrom<u8> for CaptureProfile {
@@ -59,12 +59,12 @@ impl TryFrom<u8> for CaptureProfile {
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct RadioHealth {
pub pkt_yield_per_sec: u16,
pub send_fail_count: u16,
pub rssi_median_dbm: i8,
pub noise_floor_dbm: i8,
pub current_channel: u8,
pub current_bw_mhz: u8,
pub current_profile: u8,
pub send_fail_count: u16,
pub rssi_median_dbm: i8,
pub noise_floor_dbm: i8,
pub current_channel: u8,
pub current_bw_mhz: u8,
pub current_profile: u8,
}
#[derive(Debug, thiserror::Error)]
@@ -95,12 +95,12 @@ pub trait RadioOps: Send + Sync {
/// A zero-hardware radio backend for host tests and CI.
#[derive(Debug, Clone, Default)]
pub struct MockRadio {
pub health: RadioHealth,
pub init_count: u32,
pub health: RadioHealth,
pub init_count: u32,
pub channel_calls: Vec<(u8, u8)>,
pub profile_calls: Vec<CaptureProfile>,
pub mode_calls: Vec<RadioMode>,
pub csi_enabled: bool,
pub mode_calls: Vec<RadioMode>,
pub csi_enabled: bool,
}
impl RadioOps for MockRadio {
@@ -111,7 +111,7 @@ impl RadioOps for MockRadio {
fn set_channel(&mut self, ch: u8, bw: u8) -> Result<(), RadioError> {
self.channel_calls.push((ch, bw));
self.health.current_channel = ch;
self.health.current_bw_mhz = bw;
self.health.current_bw_mhz = bw;
Ok(())
}
fn set_mode(&mut self, mode: RadioMode) -> Result<(), RadioError> {
@@ -137,9 +137,9 @@ impl RadioOps for MockRadio {
// ---------------------------------------------------------------------------
/// `RV_MESH_MAGIC` from rv_mesh.h.
pub const MESH_MAGIC: u32 = 0xC511_8100;
pub const MESH_MAGIC: u32 = 0xC511_8100;
/// `RV_MESH_VERSION` from rv_mesh.h.
pub const MESH_VERSION: u8 = 1;
pub const MESH_VERSION: u8 = 1;
/// `RV_MESH_MAX_PAYLOAD` from rv_mesh.h.
pub const MESH_MAX_PAYLOAD: usize = 256;
/// `sizeof(rv_mesh_header_t)`.
@@ -149,9 +149,9 @@ pub const MESH_HEADER_SIZE: usize = 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MeshRole {
Unassigned = 0,
Anchor = 1,
Observer = 2,
Unassigned = 0,
Anchor = 1,
Observer = 2,
FusionRelay = 3,
Coordinator = 4,
}
@@ -174,13 +174,13 @@ impl TryFrom<u8> for MeshRole {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MeshMsgType {
TimeSync = 0x01,
RoleAssign = 0x02,
ChannelPlan = 0x03,
TimeSync = 0x01,
RoleAssign = 0x02,
ChannelPlan = 0x03,
CalibrationStart = 0x04,
FeatureDelta = 0x05,
Health = 0x06,
AnomalyAlert = 0x07,
FeatureDelta = 0x05,
Health = 0x06,
AnomalyAlert = 0x07,
}
impl TryFrom<u8> for MeshMsgType {
@@ -194,7 +194,7 @@ impl TryFrom<u8> for MeshMsgType {
0x05 => Ok(MeshMsgType::FeatureDelta),
0x06 => Ok(MeshMsgType::Health),
0x07 => Ok(MeshMsgType::AnomalyAlert),
_ => Err(MeshError::UnknownMsgType(v)),
_ => Err(MeshError::UnknownMsgType(v)),
}
}
}
@@ -203,44 +203,44 @@ impl TryFrom<u8> for MeshMsgType {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AuthClass {
None = 0,
HmacSession = 1,
None = 0,
HmacSession = 1,
Ed25519Batch = 2,
}
/// `rv_mesh_header_t`, 16 bytes.
#[derive(Debug, Clone, Copy)]
pub struct MeshHeader {
pub msg_type: MeshMsgType,
pub msg_type: MeshMsgType,
pub sender_role: MeshRole,
pub auth_class: AuthClass,
pub epoch: u32,
pub auth_class: AuthClass,
pub epoch: u32,
pub payload_len: u16,
}
/// `rv_node_status_t`, 28 bytes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeStatus {
pub node_id: [u8; 8],
pub local_time_us: u64,
pub role: MeshRole,
pub node_id: [u8; 8],
pub local_time_us: u64,
pub role: MeshRole,
pub current_channel: u8,
pub current_bw: u8,
pub current_bw: u8,
pub noise_floor_dbm: i8,
pub pkt_yield: u16,
pub sync_error_us: u16,
pub health_flags: u16,
pub pkt_yield: u16,
pub sync_error_us: u16,
pub health_flags: u16,
}
/// `rv_anomaly_alert_t`, 28 bytes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AnomalyAlert {
pub node_id: [u8; 8],
pub ts_us: u64,
pub severity: u8,
pub reason: u8,
pub node_id: [u8; 8],
pub ts_us: u64,
pub severity: u8,
pub reason: u8,
pub anomaly_score: f32,
pub motion_score: f32,
pub motion_score: f32,
}
#[derive(Debug, thiserror::Error)]
@@ -262,11 +262,7 @@ pub enum MeshError {
#[error("unknown auth class: {0}")]
UnknownAuth(u8),
#[error("payload size mismatch for {which}: got {got}, want {want}")]
PayloadSizeMismatch {
which: &'static str,
got: usize,
want: usize,
},
PayloadSizeMismatch { which: &'static str, got: usize, want: usize },
}
/// IEEE CRC32 — matches the bit-by-bit implementation in
@@ -291,19 +287,15 @@ pub fn decode_mesh(buf: &[u8]) -> Result<(MeshHeader, &[u8]), MeshError> {
}
let magic = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]);
if magic != MESH_MAGIC {
return Err(MeshError::BadMagic(magic));
}
if magic != MESH_MAGIC { return Err(MeshError::BadMagic(magic)); }
let version = buf[4];
if version != MESH_VERSION {
return Err(MeshError::BadVersion(version));
}
if version != MESH_VERSION { return Err(MeshError::BadVersion(version)); }
let ty = buf[5];
let ty = buf[5];
let sender_role = buf[6];
let auth_class = buf[7];
let epoch = u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]);
let auth_class = buf[7];
let epoch = u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]);
let payload_len = u16::from_le_bytes([buf[12], buf[13]]);
if payload_len as usize > MESH_MAX_PAYLOAD {
@@ -311,28 +303,20 @@ pub fn decode_mesh(buf: &[u8]) -> Result<(MeshHeader, &[u8]), MeshError> {
}
let total = MESH_HEADER_SIZE + payload_len as usize + 4;
if buf.len() < total {
return Err(MeshError::TooShort(buf.len()));
}
if buf.len() < total { return Err(MeshError::TooShort(buf.len())); }
let want_crc = crc32_ieee(&buf[..MESH_HEADER_SIZE + payload_len as usize]);
let crc_off = MESH_HEADER_SIZE + payload_len as usize;
let got_crc = u32::from_le_bytes([
buf[crc_off],
buf[crc_off + 1],
buf[crc_off + 2],
buf[crc_off + 3],
let crc_off = MESH_HEADER_SIZE + payload_len as usize;
let got_crc = u32::from_le_bytes([
buf[crc_off], buf[crc_off + 1], buf[crc_off + 2], buf[crc_off + 3],
]);
if got_crc != want_crc {
return Err(MeshError::CrcMismatch {
got: got_crc,
want: want_crc,
});
return Err(MeshError::CrcMismatch { got: got_crc, want: want_crc });
}
let msg_type = MeshMsgType::try_from(ty)?;
let msg_type = MeshMsgType::try_from(ty)?;
let sender_role = MeshRole::try_from(sender_role)?;
let auth_class = match auth_class {
let auth_class = match auth_class {
0 => AuthClass::None,
1 => AuthClass::HmacSession,
2 => AuthClass::Ed25519Batch,
@@ -340,14 +324,8 @@ pub fn decode_mesh(buf: &[u8]) -> Result<(MeshHeader, &[u8]), MeshError> {
};
Ok((
MeshHeader {
msg_type,
sender_role,
auth_class,
epoch,
payload_len,
},
&buf[MESH_HEADER_SIZE..MESH_HEADER_SIZE + payload_len as usize],
MeshHeader { msg_type, sender_role, auth_class, epoch, payload_len },
&buf[MESH_HEADER_SIZE .. MESH_HEADER_SIZE + payload_len as usize],
))
}
@@ -355,24 +333,24 @@ pub fn decode_mesh(buf: &[u8]) -> Result<(MeshHeader, &[u8]), MeshError> {
pub fn decode_node_status(p: &[u8]) -> Result<NodeStatus, MeshError> {
if p.len() != 28 {
return Err(MeshError::PayloadSizeMismatch {
which: "HEALTH",
got: p.len(),
want: 28,
which: "HEALTH", got: p.len(), want: 28,
});
}
let mut node_id = [0u8; 8];
node_id.copy_from_slice(&p[0..8]);
let local_time_us = u64::from_le_bytes([p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]]);
let local_time_us = u64::from_le_bytes([
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15],
]);
Ok(NodeStatus {
node_id,
local_time_us,
role: MeshRole::try_from(p[16])?,
current_channel: p[17],
current_bw: p[18],
current_bw: p[18],
noise_floor_dbm: p[19] as i8,
pkt_yield: u16::from_le_bytes([p[20], p[21]]),
sync_error_us: u16::from_le_bytes([p[22], p[23]]),
health_flags: u16::from_le_bytes([p[24], p[25]]),
pkt_yield: u16::from_le_bytes([p[20], p[21]]),
sync_error_us: u16::from_le_bytes([p[22], p[23]]),
health_flags: u16::from_le_bytes([p[24], p[25]]),
})
}
@@ -380,29 +358,31 @@ pub fn decode_node_status(p: &[u8]) -> Result<NodeStatus, MeshError> {
pub fn decode_anomaly_alert(p: &[u8]) -> Result<AnomalyAlert, MeshError> {
if p.len() != 28 {
return Err(MeshError::PayloadSizeMismatch {
which: "ANOMALY_ALERT",
got: p.len(),
want: 28,
which: "ANOMALY_ALERT", got: p.len(), want: 28,
});
}
let mut node_id = [0u8; 8];
node_id.copy_from_slice(&p[0..8]);
let ts_us = u64::from_le_bytes([p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]]);
let ts_us = u64::from_le_bytes([
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15],
]);
let anomaly_score = f32::from_le_bytes([p[20], p[21], p[22], p[23]]);
let motion_score = f32::from_le_bytes([p[24], p[25], p[26], p[27]]);
let motion_score = f32::from_le_bytes([p[24], p[25], p[26], p[27]]);
Ok(AnomalyAlert {
node_id,
ts_us,
node_id, ts_us,
severity: p[16],
reason: p[17],
anomaly_score,
motion_score,
reason: p[17],
anomaly_score, motion_score,
})
}
/// Encode a `HEALTH` payload. Produces the 16-byte header, 28-byte
/// payload, and 4-byte CRC — bit-identical to what the firmware emits.
pub fn encode_health(sender_role: MeshRole, epoch: u32, status: &NodeStatus) -> Vec<u8> {
pub fn encode_health(
sender_role: MeshRole,
epoch: u32,
status: &NodeStatus,
) -> Vec<u8> {
let payload_len: u16 = 28;
let mut buf = Vec::with_capacity(MESH_HEADER_SIZE + payload_len as usize + 4);
@@ -414,7 +394,7 @@ pub fn encode_health(sender_role: MeshRole, epoch: u32, status: &NodeStatus) ->
buf.push(AuthClass::None as u8);
buf.extend_from_slice(&epoch.to_le_bytes());
buf.extend_from_slice(&payload_len.to_le_bytes());
buf.extend_from_slice(&0u16.to_le_bytes()); // reserved
buf.extend_from_slice(&0u16.to_le_bytes()); // reserved
// payload
buf.extend_from_slice(&status.node_id);
@@ -426,7 +406,7 @@ pub fn encode_health(sender_role: MeshRole, epoch: u32, status: &NodeStatus) ->
buf.extend_from_slice(&status.pkt_yield.to_le_bytes());
buf.extend_from_slice(&status.sync_error_us.to_le_bytes());
buf.extend_from_slice(&status.health_flags.to_le_bytes());
buf.extend_from_slice(&0u16.to_le_bytes()); // reserved
buf.extend_from_slice(&0u16.to_le_bytes()); // reserved
let crc = crc32_ieee(&buf);
buf.extend_from_slice(&crc.to_le_bytes());
@@ -464,8 +444,8 @@ mod tests {
fn crc32_matches_firmware_vectors() {
// Same vectors as test_rv_feature_state.c
assert_eq!(crc32_ieee(b"123456789"), 0xCBF43926);
assert_eq!(crc32_ieee(&[]), 0x00000000);
assert_eq!(crc32_ieee(&[0u8]), 0xD202EF8D);
assert_eq!(crc32_ieee(&[]), 0x00000000);
assert_eq!(crc32_ieee(&[0u8]), 0xD202EF8D);
}
#[test]
@@ -510,7 +490,7 @@ mod tests {
health_flags: 0,
};
let mut wire = encode_health(MeshRole::Observer, 0, &st);
let p0 = MESH_HEADER_SIZE; // first payload byte
let p0 = MESH_HEADER_SIZE; // first payload byte
wire[p0] ^= 0xFF;
let err = decode_mesh(&wire).unwrap_err();
assert!(matches!(err, MeshError::CrcMismatch { .. }));
@@ -1,471 +0,0 @@
//! ADR-110 §A0.12 sync packet decoder (firmware v0.6.9+).
//!
//! Emitted by the firmware on the same UDP socket as ADR-018 CSI frames,
//! distinguished by leading magic `0xC511A110`. Pairs `(node_id, sequence)`
//! across the two UDP streams so a host aggregator can recover mesh-aligned
//! timestamps for every CSI frame — see `WITNESS-LOG-110 §A0.12` for live
//! verification, `archive/v1/src/hardware/csi_extractor.py:SyncPacketParser`
//! for the matching Python decoder.
//!
//! Wire format (32 bytes, little-endian):
//! ```text
//! [0..3] magic 0xC511A110 (LE u32)
//! [4] node_id
//! [5] proto_ver (currently 0x01)
//! [6] flags: bit 0 = is_leader
//! bit 1 = is_valid (fresh sync within VALID_WINDOW_MS)
//! bit 2 = smoothed_used (EMA filter active)
//! [7] reserved
//! [8..15] local esp_timer_get_time() (u64)
//! [16..23] mesh-aligned epoch = local + smoothed offset (u64)
//! [24..27] high-water CSI sequence (u32) — pairing key against ADR-018 frames
//! [28..31] reserved
//! ```
//!
//! Recover the per-board offset for a given sync packet as
//! `local_us - epoch_us` (signed). Follower nodes report the EMA-smoothed
//! offset measured in §A0.10; leader nodes report `~0` modulo call-stack
//! elapsed time (`leader_epoch_us = now_us` by definition).
use serde::{Deserialize, Serialize};
use crate::error::ParseError;
/// Magic constant in the first 4 little-endian bytes of every sync packet.
pub const SYNC_PACKET_MAGIC: u32 = 0xC511_A110;
/// Total wire size of a v0.6.9+ sync packet.
pub const SYNC_PACKET_SIZE: usize = 32;
/// Wire protocol version currently emitted by firmware.
pub const SYNC_PACKET_PROTO_VER: u8 = 0x01;
/// Decoded ADR-110 §A0.12 sync packet.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyncPacket {
pub node_id: u8,
pub proto_ver: u8,
pub flags: SyncPacketFlags,
/// Node-local `esp_timer_get_time()` snapshot at emission time.
pub local_us: u64,
/// Mesh-aligned epoch — `local_us + smoothed_offset`.
pub epoch_us: u64,
/// High-water ADR-018 CSI sequence number at emission time. Host
/// aggregator pairs (`node_id`, `sequence`) across the two UDP streams
/// to apply the recovered offset back to in-flight CSI frames.
pub sequence: u32,
}
/// Flag bits packed into byte 6 of the sync packet.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct SyncPacketFlags {
pub is_leader: bool,
pub is_valid: bool,
pub smoothed_used: bool,
}
impl SyncPacketFlags {
pub fn from_byte(b: u8) -> Self {
Self {
is_leader: (b & 0x01) != 0,
is_valid: (b & 0x02) != 0,
smoothed_used: (b & 0x04) != 0,
}
}
pub fn to_byte(self) -> u8 {
let mut b = 0u8;
if self.is_leader { b |= 0x01; }
if self.is_valid { b |= 0x02; }
if self.smoothed_used { b |= 0x04; }
b
}
}
impl SyncPacket {
/// Decode a 32-byte sync packet. Returns `ParseError::InvalidMagic` if
/// the leading u32 doesn't match `SYNC_PACKET_MAGIC` (host should
/// dispatch on the magic before calling this — see crate-level docs).
pub fn from_bytes(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < SYNC_PACKET_SIZE {
return Err(ParseError::InsufficientData {
needed: SYNC_PACKET_SIZE,
got: buf.len(),
});
}
let magic = u32::from_le_bytes(buf[0..4].try_into().unwrap());
if magic != SYNC_PACKET_MAGIC {
return Err(ParseError::InvalidMagic { expected: SYNC_PACKET_MAGIC, got: magic });
}
let node_id = buf[4];
let proto_ver = buf[5];
let flags = SyncPacketFlags::from_byte(buf[6]);
// buf[7] reserved
let local_us = u64::from_le_bytes(buf[8..16].try_into().unwrap());
let epoch_us = u64::from_le_bytes(buf[16..24].try_into().unwrap());
let sequence = u32::from_le_bytes(buf[24..28].try_into().unwrap());
// buf[28..32] reserved
Ok(Self {
node_id,
proto_ver,
flags,
local_us,
epoch_us,
sequence,
})
}
/// Recover the signed offset between this node's local monotonic clock
/// and the mesh epoch (`local_us - epoch_us`). For followers this is
/// the EMA-smoothed offset; for leaders this is approximately 0 (a few
/// µs of call-stack elapsed only).
pub fn local_minus_epoch_us(&self) -> i64 {
(self.local_us as i64) - (self.epoch_us as i64)
}
/// Given a CSI frame's node-local `esp_timer_get_time()` snapshot,
/// recover the mesh-aligned timestamp using this sync packet as the
/// reference point.
///
/// Math (all in node-local µs, see ADR-110 §A0.12):
///
/// ```text
/// offset = epoch_us - local_us (signed; this packet)
/// mesh_epoch(frame) = local_at_frame_us + offset
/// = local_at_frame_us + (epoch_us - local_us)
/// ```
///
/// On the leader this gives `≈ local_at_frame_us`. On a follower this
/// gives the mesh-aligned time aligned to the leader's clock within
/// the §A0.10 measured 104 µs stdev (the same EMA-smoothed offset
/// the firmware applied when it built this sync packet's `epoch_us`).
///
/// Use this on the host side whenever a CSI frame arrives with
/// ADR-018 byte 19 bit 4 set: look up the matching node's most-recent
/// `SyncPacket`, call `apply_to_local(frame.local_us)`, stamp the
/// result on the frame for downstream multistatic fusion.
pub fn apply_to_local(&self, local_at_frame_us: u64) -> u64 {
// Compute the offset as a signed delta in the µs domain. Adding it
// back to the frame's local snapshot recovers the mesh epoch.
let offset = (self.epoch_us as i64).wrapping_sub(self.local_us as i64);
(local_at_frame_us as i64).wrapping_add(offset) as u64
}
/// Recover the mesh-aligned timestamp for an in-flight CSI frame
/// **using its ADR-018 sequence number** as the timeline anchor.
///
/// CSI frames carry no per-frame `local_us` field (ADR-018 v1 wire
/// format reserves no slot for it — see WITNESS-LOG-110 §A0.11),
/// but they do carry a 32-bit sequence number. The firmware emits
/// a sync packet alongside CSI frames, stamping the sequence
/// high-water observed at emit time into [`SyncPacket::sequence`].
///
/// Given a frame's sequence and the node's observed CSI frame rate,
/// estimate the node-local time at the frame and apply the mesh
/// offset:
///
/// ```text
/// Δframes = frame_seq - sync.sequence (wrapping)
/// Δus = Δframes × 1_000_000 / fps_hz (node-local)
/// local_at = sync.local_us + Δus
/// mesh = local_at + (sync.epoch_us - sync.local_us)
/// ```
///
/// `fps_hz` must be > 0; pass the firmware's `CSI_MIN_SEND_INTERVAL_US`
/// inverse (≈ 20 fps) or a measured rate from the broadcast-tick task.
/// The estimate is exact when the frame rate is stable (a node holding
/// 20 fps within ±1 frame for the sync→frame interval gives
/// |error| < 1/fps_hz ≈ 50 ms × the per-frame jitter ratio).
pub fn mesh_aligned_us_for_sequence(&self, frame_seq: u32, fps_hz: f64) -> u64 {
debug_assert!(fps_hz > 0.0, "fps_hz must be positive");
let dframes = (frame_seq.wrapping_sub(self.sequence)) as i64;
let dus = (dframes as f64 * 1_000_000.0 / fps_hz) as i64;
let local_at = (self.local_us as i64).wrapping_add(dus) as u64;
self.apply_to_local(local_at)
}
/// Serialize back to wire bytes (32 bytes, little-endian).
pub fn to_bytes(&self) -> [u8; SYNC_PACKET_SIZE] {
let mut out = [0u8; SYNC_PACKET_SIZE];
out[0..4].copy_from_slice(&SYNC_PACKET_MAGIC.to_le_bytes());
out[4] = self.node_id;
out[5] = self.proto_ver;
out[6] = self.flags.to_byte();
// out[7] reserved zero
out[8..16].copy_from_slice(&self.local_us.to_le_bytes());
out[16..24].copy_from_slice(&self.epoch_us.to_le_bytes());
out[24..28].copy_from_slice(&self.sequence.to_le_bytes());
// out[28..32] reserved zero
out
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Reproduces the COM9 follower sync-pkt #1 captured in WITNESS-LOG-110 §A0.12.
#[test]
fn follower_typical_packet_roundtrips() {
let pkt = SyncPacket {
node_id: 9,
proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450,
epoch_us: 27_634_885,
sequence: 20,
};
let wire = pkt.to_bytes();
let decoded = SyncPacket::from_bytes(&wire).unwrap();
assert_eq!(decoded, pkt);
// The 1.16-second boot delta §A0.10 measured between COM9 and COM12.
assert_eq!(decoded.local_minus_epoch_us(), 1_163_565);
assert_eq!(decoded.flags.to_byte(), 0x06);
}
/// COM12 leader case from WITNESS-LOG-110 §A0.12: flags=0x03, epoch ≈ local.
#[test]
fn leader_packet_has_local_close_to_epoch() {
let pkt = SyncPacket {
node_id: 12,
proto_ver: 1,
flags: SyncPacketFlags { is_leader: true, is_valid: true, smoothed_used: false },
local_us: 28_864_932,
epoch_us: 28_864_939,
sequence: 20,
};
let wire = pkt.to_bytes();
let decoded = SyncPacket::from_bytes(&wire).unwrap();
assert_eq!(decoded.flags.to_byte(), 0x03);
assert_eq!(decoded.local_minus_epoch_us(), -7); // leader has zero offset modulo call-stack
assert!(decoded.flags.is_leader);
assert!(decoded.flags.is_valid);
assert!(!decoded.flags.smoothed_used);
}
#[test]
fn magic_mismatch_is_typed_error() {
let mut wire = SyncPacket {
node_id: 1, proto_ver: 1, flags: SyncPacketFlags::default(),
local_us: 0, epoch_us: 0, sequence: 0,
}.to_bytes();
wire[0] = 0x01; // corrupt magic low byte
let err = SyncPacket::from_bytes(&wire).unwrap_err();
match err {
ParseError::InvalidMagic { got, .. } => assert_ne!(got, SYNC_PACKET_MAGIC),
other => panic!("expected InvalidMagic, got {other:?}"),
}
}
#[test]
fn short_packet_is_typed_error() {
let wire = [0u8; 16]; // half a packet
let err = SyncPacket::from_bytes(&wire).unwrap_err();
match err {
ParseError::InsufficientData { needed, got } => {
assert_eq!(needed, SYNC_PACKET_SIZE);
assert_eq!(got, 16);
}
other => panic!("expected InsufficientData, got {other:?}"),
}
}
/// Every (leader, valid, smoothed_used) triple round-trips independently.
#[test]
fn all_flag_combinations_roundtrip() {
for &is_leader in &[false, true] {
for &is_valid in &[false, true] {
for &smoothed_used in &[false, true] {
let flags = SyncPacketFlags { is_leader, is_valid, smoothed_used };
let pkt = SyncPacket {
node_id: 1, proto_ver: 1, flags,
local_us: 1234, epoch_us: 5678, sequence: 99,
};
let wire = pkt.to_bytes();
let decoded = SyncPacket::from_bytes(&wire).unwrap();
assert_eq!(decoded.flags, flags);
assert_eq!(decoded.flags.to_byte(), flags.to_byte());
}
}
}
}
/// A host dispatches CSI vs sync purely on the leading u32. The two
/// magics must therefore never collide.
#[test]
fn sync_and_csi_magics_differ() {
assert_ne!(SYNC_PACKET_MAGIC, crate::esp32_parser::ESP32_CSI_MAGIC);
}
/// Applying a sync packet to its own local_us must recover its own
/// epoch_us. Foundational identity for the math.
#[test]
fn apply_to_local_recovers_packet_epoch() {
let pkt = SyncPacket {
node_id: 9, proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450, epoch_us: 27_634_885, sequence: 20,
};
assert_eq!(pkt.apply_to_local(pkt.local_us), pkt.epoch_us);
}
/// A CSI frame's local timestamp arriving after the sync packet
/// gets the same offset applied — the µs delta between sync and frame
/// is preserved on both clocks.
#[test]
fn apply_to_local_preserves_inter_frame_delta() {
let pkt = SyncPacket {
node_id: 9, proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450, epoch_us: 27_634_885, sequence: 20,
};
// Frame arrives 100 ms after the sync packet on the follower's local clock.
let local_at_frame = pkt.local_us + 100_000;
let mesh_epoch = pkt.apply_to_local(local_at_frame);
// Mesh epoch should also be 100 ms after the sync packet's epoch.
assert_eq!(mesh_epoch, pkt.epoch_us + 100_000);
// Offset must equal local - epoch on both clocks.
assert_eq!(local_at_frame - mesh_epoch, pkt.local_us - pkt.epoch_us);
}
/// Leader sync packet has near-zero offset, so apply_to_local is
/// approximately identity (modulo the few µs call-stack delta).
#[test]
fn apply_to_local_on_leader_is_near_identity() {
let pkt = SyncPacket {
node_id: 12, proto_ver: 1,
flags: SyncPacketFlags { is_leader: true, is_valid: true, smoothed_used: false },
local_us: 28_864_932, epoch_us: 28_864_939, sequence: 20,
};
let frame_local = 30_000_000u64;
let mesh = pkt.apply_to_local(frame_local);
assert!((mesh as i64 - frame_local as i64).abs() <= 100,
"leader apply should be within 100 µs of identity, got {} delta",
mesh as i64 - frame_local as i64);
}
/// At the sync packet's own sequence number, the interpolated mesh
/// time must equal `epoch_us` exactly.
#[test]
fn mesh_aligned_for_sequence_identity_at_sync_point() {
let pkt = SyncPacket {
node_id: 9, proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450, epoch_us: 27_634_885, sequence: 20,
};
assert_eq!(pkt.mesh_aligned_us_for_sequence(20, 20.0), pkt.epoch_us);
}
/// 20 frames after the sync packet at 20 Hz → mesh time advances by 1 s,
/// preserving the leader/follower clock offset.
#[test]
fn mesh_aligned_for_sequence_extrapolates_forward() {
let pkt = SyncPacket {
node_id: 9, proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450, epoch_us: 27_634_885, sequence: 20,
};
// 20 frames at 20 fps = 1 000 000 µs
let mesh = pkt.mesh_aligned_us_for_sequence(40, 20.0);
assert_eq!(mesh, pkt.epoch_us + 1_000_000);
}
/// Sequence wraparound (u32 overflow) must extrapolate forward by one
/// frame, not jump backward by 2^32. The wrapping_sub semantics in
/// the implementation guard this.
#[test]
fn mesh_aligned_for_sequence_handles_seq_wraparound() {
let pkt = SyncPacket {
node_id: 9, proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 10_000, epoch_us: 10_000, sequence: u32::MAX,
};
// Next sequence after u32::MAX is 0 (wrap). Δframes = 1, not -2^32.
let mesh = pkt.mesh_aligned_us_for_sequence(0, 20.0);
assert_eq!(mesh, pkt.epoch_us + 50_000); // 1 frame at 20 fps = 50 ms
}
/// End-to-end ADR-110 pipeline sanity:
/// (1) firmware emits sync packet (bytes built here as a stand-in)
/// (2) host wire-decodes via from_bytes
/// (3) a CSI frame arrives 100 sequences later (≈ 5 s @ 20 fps)
/// (4) mesh_aligned_us_for_sequence recovers its mesh timestamp
/// Asserts that the recovered mesh time matches sync.epoch_us + Δus exactly,
/// and cross-checks against apply_to_local. This is the contract every
/// downstream multistatic-fusion consumer relies on.
#[test]
fn end_to_end_sync_decode_then_frame_mesh_recovery() {
let pkt = SyncPacket {
node_id: 9,
proto_ver: 1,
flags: SyncPacketFlags { is_leader: false, is_valid: true, smoothed_used: true },
local_us: 28_798_450,
epoch_us: 27_634_885,
sequence: 20,
};
let wire = pkt.to_bytes();
assert_eq!(wire.len(), SYNC_PACKET_SIZE);
let decoded = SyncPacket::from_bytes(&wire).unwrap();
assert_eq!(decoded, pkt);
// 5 s after sync at 20 fps = 100 frames later
let frame_seq = pkt.sequence + 100;
let mesh_us = decoded.mesh_aligned_us_for_sequence(frame_seq, 20.0);
assert_eq!(mesh_us, pkt.epoch_us + 5_000_000);
// Same mesh time via direct apply_to_local — both paths must agree
let local_at_frame = pkt.local_us + 5_000_000;
assert_eq!(decoded.apply_to_local(local_at_frame), mesh_us);
}
#[test]
fn wire_size_constant_is_correct() {
let pkt = SyncPacket {
node_id: 0, proto_ver: 1, flags: SyncPacketFlags::default(),
local_us: 0, epoch_us: 0, sequence: 0,
};
assert_eq!(pkt.to_bytes().len(), SYNC_PACKET_SIZE);
assert_eq!(SYNC_PACKET_SIZE, 32);
}
/// ADR-110 iter 21 — cross-language wire-format conformance gate.
///
/// These exact bytes are ALSO pinned in the Python test
/// `test_canonical_wire_bytes_match_rust_decoder` in
/// `archive/v1/tests/unit/test_esp32_binary_parser.py`. If this
/// canonical hex stops matching what Python emits for the same
/// SyncPacket fields, ONE of the decoders has drifted from the wire.
///
/// Canonical packet: COM9 sync-pkt #1 from §A0.12 live capture.
#[test]
fn canonical_wire_bytes_match_python_decoder() {
// Exact bytes matching the Python pin (hex-decoded by hand to bytes).
let canonical: [u8; 32] = [
0x10, 0xa1, 0x11, 0xc5, // magic 0xC511A110 (LE u32)
0x09, // node_id = 9
0x01, // proto_ver = 1
0x06, // flags: bit1=is_valid, bit2=smoothed_used
0x00, // reserved
0xf2, 0x6d, 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, // local_us = 28_798_450
0xc5, 0xac, 0xa5, 0x01, 0x00, 0x00, 0x00, 0x00, // epoch_us = 27_634_885
0x14, 0x00, 0x00, 0x00, // sequence = 20
0x00, 0x00, 0x00, 0x00, // reserved
];
let decoded = SyncPacket::from_bytes(&canonical).unwrap();
assert_eq!(decoded.node_id, 9);
assert_eq!(decoded.proto_ver, 1);
assert_eq!(decoded.flags.to_byte(), 0x06);
assert!(!decoded.flags.is_leader);
assert!(decoded.flags.is_valid);
assert!(decoded.flags.smoothed_used);
assert_eq!(decoded.local_us, 28_798_450);
assert_eq!(decoded.epoch_us, 27_634_885);
assert_eq!(decoded.sequence, 20);
// §A0.10's measured 1.16-second boot delta.
assert_eq!(decoded.local_minus_epoch_us(), 1_163_565);
// Round-trip: re-encoding the decoded struct must produce the same
// canonical bytes — this is what catches any drift in to_bytes.
let re_encoded = decoded.to_bytes();
assert_eq!(re_encoded, canonical,
"Rust to_bytes drifted from the canonical pin — Python decoder will break");
}
}
@@ -10,39 +10,31 @@
//! - Localization algorithms (triangulation, depth estimation)
//! - Alert generation
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use criterion::{
black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput,
};
use std::f64::consts::PI;
use wifi_densepose_mat::{
// Detection types
BreathingDetector, BreathingDetectorConfig,
HeartbeatDetector, HeartbeatDetectorConfig,
MovementClassifier, MovementClassifierConfig,
DetectionConfig, DetectionPipeline, VitalSignsDetector,
// Localization types
Triangulator, DepthEstimator,
// Alerting types
AlertGenerator,
// Detection types
BreathingDetector,
BreathingDetectorConfig,
// Domain types exported at crate root
BreathingPattern,
BreathingType,
DepthEstimator,
DetectionConfig,
DetectionPipeline,
HeartbeatDetector,
HeartbeatDetectorConfig,
MovementClassifier,
MovementClassifierConfig,
MovementProfile,
ScanZoneId,
Survivor,
// Localization types
Triangulator,
VitalSignsDetector,
VitalSignsReading,
BreathingPattern, BreathingType, VitalSignsReading,
MovementProfile, ScanZoneId, Survivor,
};
// Types that need to be accessed from submodules
use wifi_densepose_mat::detection::CsiDataBuffer;
use wifi_densepose_mat::domain::{
ConfidenceScore, DebrisMaterial, DebrisProfile, MetalContent, MoistureLevel, SensorPosition,
SensorType,
ConfidenceScore, SensorPosition, SensorType,
DebrisProfile, DebrisMaterial, MoistureLevel, MetalContent,
};
use chrono::Utc;
@@ -148,8 +140,7 @@ fn generate_multi_person_signal(
(0..num_samples)
.map(|i| {
let t = i as f64 / sample_rate;
base_rates
.iter()
base_rates.iter()
.enumerate()
.map(|(idx, &rate)| {
let freq = rate / 60.0;
@@ -163,26 +154,22 @@ fn generate_multi_person_signal(
}
/// Generate movement signal with specified characteristics
fn generate_movement_signal(movement_type: &str, sample_rate: f64, duration_secs: f64) -> Vec<f64> {
fn generate_movement_signal(
movement_type: &str,
sample_rate: f64,
duration_secs: f64,
) -> Vec<f64> {
let num_samples = (sample_rate * duration_secs) as usize;
match movement_type {
"gross" => {
// Large, irregular movements
let mut signal = vec![0.0; num_samples];
for s in signal
.iter_mut()
.take(num_samples / 2)
.skip(num_samples / 4)
{
*s = 2.0;
for i in (num_samples / 4)..(num_samples / 2) {
signal[i] = 2.0;
}
for s in signal
.iter_mut()
.take(4 * num_samples / 5)
.skip(3 * num_samples / 4)
{
*s = -1.5;
for i in (3 * num_samples / 4)..(4 * num_samples / 5) {
signal[i] = -1.5;
}
signal
}
@@ -272,7 +259,9 @@ fn bench_breathing_detection(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("clean_signal", format!("{}s", duration as u32)),
&signal,
|b, signal| b.iter(|| detector.detect(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| detector.detect(black_box(signal), black_box(sample_rate)))
},
);
}
@@ -281,12 +270,11 @@ fn bench_breathing_detection(c: &mut Criterion) {
let signal = generate_noisy_breathing_signal(16.0, sample_rate, 30.0, noise_level);
group.bench_with_input(
BenchmarkId::new(
"noisy_signal",
format!("noise_{}", (noise_level * 10.0) as u32),
),
BenchmarkId::new("noisy_signal", format!("noise_{}", (noise_level * 10.0) as u32)),
&signal,
|b, signal| b.iter(|| detector.detect(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| detector.detect(black_box(signal), black_box(sample_rate)))
},
);
}
@@ -297,7 +285,9 @@ fn bench_breathing_detection(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("rate_variation", format!("{}bpm", rate as u32)),
&signal,
|b, signal| b.iter(|| detector.detect(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| detector.detect(black_box(signal), black_box(sample_rate)))
},
);
}
@@ -316,7 +306,9 @@ fn bench_breathing_detection(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("high_sensitivity", "30s_noisy"),
&signal,
|b, signal| b.iter(|| sensitive_detector.detect(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| sensitive_detector.detect(black_box(signal), black_box(sample_rate)))
},
);
group.finish();
@@ -341,7 +333,9 @@ fn bench_heartbeat_detection(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("clean_signal", format!("{}s", duration as u32)),
&signal,
|b, signal| b.iter(|| detector.detect(black_box(signal), black_box(sample_rate), None)),
|b, signal| {
b.iter(|| detector.detect(black_box(signal), black_box(sample_rate), None))
},
);
}
@@ -368,7 +362,9 @@ fn bench_heartbeat_detection(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("rate_variation", format!("{}bpm", rate as u32)),
&signal,
|b, signal| b.iter(|| detector.detect(black_box(signal), black_box(sample_rate), None)),
|b, signal| {
b.iter(|| detector.detect(black_box(signal), black_box(sample_rate), None))
},
);
}
@@ -414,7 +410,9 @@ fn bench_movement_classification(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("movement_type", movement_type),
&signal,
|b, signal| b.iter(|| classifier.classify(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| classifier.classify(black_box(signal), black_box(sample_rate)))
},
);
}
@@ -425,7 +423,9 @@ fn bench_movement_classification(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("signal_length", format!("{}s", duration as u32)),
&signal,
|b, signal| b.iter(|| classifier.classify(black_box(signal), black_box(sample_rate))),
|b, signal| {
b.iter(|| classifier.classify(black_box(signal), black_box(sample_rate)))
},
);
}
@@ -480,8 +480,7 @@ fn bench_detection_pipeline(c: &mut Criterion) {
// Benchmark standard pipeline at different data sizes
for duration in [5.0, 10.0, 30.0] {
let (amplitudes, phases) =
generate_combined_vital_signal(16.0, 72.0, sample_rate, duration);
let (amplitudes, phases) = generate_combined_vital_signal(16.0, 72.0, sample_rate, duration);
let mut buffer = CsiDataBuffer::new(sample_rate);
buffer.add_samples(&amplitudes, &phases);
@@ -489,7 +488,9 @@ fn bench_detection_pipeline(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("standard_pipeline", format!("{}s", duration as u32)),
&buffer,
|b, buffer| b.iter(|| standard_pipeline.detect(black_box(buffer))),
|b, buffer| {
b.iter(|| standard_pipeline.detect(black_box(buffer)))
},
);
}
@@ -502,7 +503,9 @@ fn bench_detection_pipeline(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("full_pipeline", format!("{}s", duration as u32)),
&buffer,
|b, buffer| b.iter(|| full_pipeline.detect(black_box(buffer))),
|b, buffer| {
b.iter(|| full_pipeline.detect(black_box(buffer)))
},
);
}
@@ -515,7 +518,9 @@ fn bench_detection_pipeline(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::new("multi_person", format!("{}_people", person_count)),
&buffer,
|b, buffer| b.iter(|| standard_pipeline.detect(black_box(buffer))),
|b, buffer| {
b.iter(|| standard_pipeline.detect(black_box(buffer)))
},
);
}
@@ -536,8 +541,7 @@ fn bench_triangulation(c: &mut Criterion) {
let sensors = create_test_sensors(sensor_count);
// Generate RSSI values (simulate target at center)
let rssi_values: Vec<(String, f64)> = sensors
.iter()
let rssi_values: Vec<(String, f64)> = sensors.iter()
.map(|s| {
let distance = (s.x * s.x + s.y * s.y).sqrt();
let rssi = -30.0 - 20.0 * distance.log10(); // Path loss model
@@ -549,7 +553,9 @@ fn bench_triangulation(c: &mut Criterion) {
BenchmarkId::new("rssi_position", format!("{}_sensors", sensor_count)),
&(sensors.clone(), rssi_values.clone()),
|b, (sensors, rssi)| {
b.iter(|| triangulator.estimate_position(black_box(sensors), black_box(rssi)))
b.iter(|| {
triangulator.estimate_position(black_box(sensors), black_box(rssi))
})
},
);
}
@@ -559,8 +565,7 @@ fn bench_triangulation(c: &mut Criterion) {
let sensors = create_test_sensors(sensor_count);
// Generate ToA values (time in nanoseconds)
let toa_values: Vec<(String, f64)> = sensors
.iter()
let toa_values: Vec<(String, f64)> = sensors.iter()
.map(|s| {
let distance = (s.x * s.x + s.y * s.y).sqrt();
// Round trip time: 2 * distance / speed_of_light
@@ -573,7 +578,9 @@ fn bench_triangulation(c: &mut Criterion) {
BenchmarkId::new("toa_position", format!("{}_sensors", sensor_count)),
&(sensors.clone(), toa_values.clone()),
|b, (sensors, toa)| {
b.iter(|| triangulator.estimate_from_toa(black_box(sensors), black_box(toa)))
b.iter(|| {
triangulator.estimate_from_toa(black_box(sensors), black_box(toa))
})
},
);
}
@@ -581,8 +588,7 @@ fn bench_triangulation(c: &mut Criterion) {
// Benchmark with noisy measurements
let sensors = create_test_sensors(5);
for noise_pct in [0, 5, 10, 20] {
let rssi_values: Vec<(String, f64)> = sensors
.iter()
let rssi_values: Vec<(String, f64)> = sensors.iter()
.enumerate()
.map(|(i, s)| {
let distance = (s.x * s.x + s.y * s.y).sqrt();
@@ -597,7 +603,9 @@ fn bench_triangulation(c: &mut Criterion) {
BenchmarkId::new("noisy_rssi", format!("{}pct_noise", noise_pct)),
&(sensors.clone(), rssi_values.clone()),
|b, (sensors, rssi)| {
b.iter(|| triangulator.estimate_position(black_box(sensors), black_box(rssi)))
b.iter(|| {
triangulator.estimate_position(black_box(sensors), black_box(rssi))
})
},
);
}
@@ -654,7 +662,11 @@ fn bench_depth_estimation(c: &mut Criterion) {
&debris,
|b, debris| {
b.iter(|| {
estimator.estimate_depth(black_box(30.0), black_box(5.0), black_box(debris))
estimator.estimate_depth(
black_box(30.0),
black_box(5.0),
black_box(debris),
)
})
},
);
@@ -687,20 +699,21 @@ fn bench_depth_estimation(c: &mut Criterion) {
}
// Benchmark debris profile estimation
for (variance, multipath, moisture) in [(0.2, 0.3, 0.2), (0.5, 0.5, 0.5), (0.7, 0.8, 0.8)] {
for (variance, multipath, moisture) in [
(0.2, 0.3, 0.2),
(0.5, 0.5, 0.5),
(0.7, 0.8, 0.8),
] {
group.bench_with_input(
BenchmarkId::new(
"profile_estimation",
format!(
"v{}_m{}",
(variance * 10.0) as u32,
(multipath * 10.0) as u32
),
),
BenchmarkId::new("profile_estimation", format!("v{}_m{}", (variance * 10.0) as u32, (multipath * 10.0) as u32)),
&(variance, multipath, moisture),
|b, &(v, m, mo)| {
b.iter(|| {
estimator.estimate_debris_profile(black_box(v), black_box(m), black_box(mo))
estimator.estimate_debris_profile(
black_box(v),
black_box(m),
black_box(mo),
)
})
},
);
@@ -727,8 +740,10 @@ fn bench_alert_generation(c: &mut Criterion) {
// Benchmark escalation alert
group.bench_function("generate_escalation_alert", |b| {
b.iter(|| {
generator
.generate_escalation(black_box(&survivor), black_box("Vital signs deteriorating"))
generator.generate_escalation(
black_box(&survivor),
black_box("Vital signs deteriorating"),
)
})
});
@@ -736,7 +751,10 @@ fn bench_alert_generation(c: &mut Criterion) {
use wifi_densepose_mat::domain::TriageStatus;
group.bench_function("generate_status_change_alert", |b| {
b.iter(|| {
generator.generate_status_change(black_box(&survivor), black_box(&TriageStatus::Minor))
generator.generate_status_change(
black_box(&survivor),
black_box(&TriageStatus::Minor),
)
})
});
@@ -755,8 +773,7 @@ fn bench_alert_generation(c: &mut Criterion) {
group.bench_function("batch_generate_10_alerts", |b| {
b.iter(|| {
survivors
.iter()
survivors.iter()
.map(|s| generator.generate(black_box(s)))
.collect::<Vec<_>>()
})
@@ -779,7 +796,9 @@ fn bench_csi_buffer(c: &mut Criterion) {
let amplitudes: Vec<f64> = (0..sample_count)
.map(|i| (i as f64 / 100.0).sin())
.collect();
let phases: Vec<f64> = (0..sample_count).map(|i| (i as f64 / 50.0).cos()).collect();
let phases: Vec<f64> = (0..sample_count)
.map(|i| (i as f64 / 50.0).cos())
.collect();
group.throughput(Throughput::Elements(sample_count as u64));
group.bench_with_input(
@@ -1,8 +1,8 @@
//! Alert dispatching and delivery.
use super::AlertGenerator;
use crate::domain::{Alert, AlertId, Priority, Survivor};
use crate::MatError;
use super::AlertGenerator;
use std::collections::HashMap;
/// Configuration for alert dispatch
@@ -67,9 +67,7 @@ impl AlertDispatcher {
let priority = alert.priority();
// Store in pending alerts
self.pending_alerts
.write()
.insert(alert_id.clone(), alert.clone());
self.pending_alerts.write().insert(alert_id.clone(), alert.clone());
// Log the alert
tracing::info!(
@@ -123,11 +121,7 @@ impl AlertDispatcher {
}
/// Resolve an alert
pub fn resolve(
&self,
alert_id: &AlertId,
resolution: crate::domain::AlertResolution,
) -> Result<(), MatError> {
pub fn resolve(&self, alert_id: &AlertId, resolution: crate::domain::AlertResolution) -> Result<(), MatError> {
let mut alerts = self.pending_alerts.write();
if let Some(alert) = alerts.remove(alert_id) {
@@ -197,9 +191,7 @@ impl AlertDispatcher {
/// Escalate oldest pending alerts
async fn escalate_oldest(&self) -> Result<(), MatError> {
let mut alerts: Vec<_> = self
.pending_alerts
.read()
let mut alerts: Vec<_> = self.pending_alerts.read()
.iter()
.map(|(id, alert)| (id.clone(), *alert.created_at()))
.collect();
@@ -237,7 +229,6 @@ pub trait AlertHandler: Send + Sync {
}
/// Console/logging alert handler
#[allow(dead_code)]
pub struct ConsoleAlertHandler;
#[async_trait::async_trait]
@@ -273,7 +264,6 @@ impl AlertHandler for ConsoleAlertHandler {
/// Requires platform audio support. On systems without audio hardware
/// (headless servers, embedded), this logs the alert pattern. On systems
/// with audio, integrate with the platform's audio API.
#[allow(dead_code)]
pub struct AudioAlertHandler {
/// Whether audio hardware is available
audio_available: bool,
@@ -281,19 +271,15 @@ pub struct AudioAlertHandler {
impl AudioAlertHandler {
/// Create a new audio handler, auto-detecting audio support.
#[allow(dead_code)]
pub fn new() -> Self {
let audio_available =
std::env::var("DISPLAY").is_ok() || std::env::var("PULSE_SERVER").is_ok();
let audio_available = std::env::var("DISPLAY").is_ok()
|| std::env::var("PULSE_SERVER").is_ok();
Self { audio_available }
}
/// Create with explicit audio availability flag.
#[allow(dead_code)]
pub fn with_availability(available: bool) -> Self {
Self {
audio_available: available,
}
Self { audio_available: available }
}
}
@@ -334,7 +320,7 @@ impl AlertHandler for AudioAlertHandler {
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{AlertPayload, SurvivorId, TriageStatus};
use crate::domain::{SurvivorId, TriageStatus, AlertPayload};
fn create_test_alert() -> Alert {
Alert::new(
@@ -366,9 +352,7 @@ mod tests {
assert!(result.is_ok());
let pending = dispatcher.pending();
assert!(pending
.iter()
.any(|a| a.id() == &alert_id && a.acknowledged_by() == Some("Team Alpha")));
assert!(pending.iter().any(|a| a.id() == &alert_id && a.acknowledged_by() == Some("Team Alpha")));
}
#[tokio::test]
@@ -1,6 +1,8 @@
//! Alert generation from survivor detections.
use crate::domain::{Alert, AlertPayload, Priority, ScanZoneId, Survivor, TriageStatus};
use crate::domain::{
Alert, AlertPayload, Priority, Survivor, TriageStatus, ScanZoneId,
};
use crate::MatError;
/// Generator for alerts based on survivor status
@@ -38,7 +40,10 @@ impl AlertGenerator {
) -> Result<Alert, MatError> {
let mut payload = self.create_payload(survivor);
payload.title = format!("ESCALATED: {}", payload.title);
payload.message = format!("{}\n\nReason for escalation: {}", payload.message, reason);
payload.message = format!(
"{}\n\nReason for escalation: {}",
payload.message, reason
);
// Escalated alerts are always at least high priority
let priority = match survivor.triage_status() {
@@ -59,8 +64,7 @@ impl AlertGenerator {
payload.title = format!(
"Status Change: {} → {}",
previous_status,
survivor.triage_status()
previous_status, survivor.triage_status()
);
// Determine if this is an upgrade (worse) or downgrade (better)
@@ -93,8 +97,7 @@ impl AlertGenerator {
/// Create alert payload from survivor data
fn create_payload(&self, survivor: &Survivor) -> AlertPayload {
let zone_name = self
.zone_names
let zone_name = self.zone_names
.get(survivor.zone_id())
.map(String::as_str)
.unwrap_or("Unknown Zone");
@@ -156,7 +159,8 @@ impl AlertGenerator {
lines.push(format!(
" Movement: {:?} (intensity: {:.1})",
reading.movement.movement_type, reading.movement.intensity
reading.movement.movement_type,
reading.movement.intensity
));
} else {
lines.push(" No recent readings".to_string());
@@ -179,7 +183,9 @@ impl AlertGenerator {
" Position: ({:.1}, {:.1})\n\
Depth: {}\n\
Uncertainty: ±{:.1}m",
loc.x, loc.y, depth_str, loc.uncertainty.horizontal_error
loc.x, loc.y,
depth_str,
loc.uncertainty.horizontal_error
)
}
None => " Position not yet determined".to_string(),
@@ -260,15 +266,11 @@ mod tests {
let generator = AlertGenerator::new();
let survivor = create_test_survivor();
let alert = generator
.generate_escalation(&survivor, "Vital signs deteriorating")
let alert = generator.generate_escalation(&survivor, "Vital signs deteriorating")
.unwrap();
assert!(alert.payload().title.contains("ESCALATED"));
assert!(matches!(
alert.priority(),
Priority::Critical | Priority::High
));
assert!(matches!(alert.priority(), Priority::Critical | Priority::High));
}
#[test]
@@ -276,9 +278,10 @@ mod tests {
let generator = AlertGenerator::new();
let survivor = create_test_survivor();
let alert = generator
.generate_status_change(&survivor, &TriageStatus::Minor)
.unwrap();
let alert = generator.generate_status_change(
&survivor,
&TriageStatus::Minor,
).unwrap();
assert!(alert.payload().title.contains("Status Change"));
}
@@ -1,9 +1,9 @@
//! Alerting module for emergency notifications.
mod dispatcher;
mod generator;
mod dispatcher;
mod triage_service;
pub use dispatcher::{AlertConfig, AlertDispatcher};
pub use generator::AlertGenerator;
pub use triage_service::{PriorityCalculator, TriageService};
pub use dispatcher::{AlertDispatcher, AlertConfig};
pub use triage_service::{TriageService, PriorityCalculator};
@@ -1,7 +1,8 @@
//! Triage service for calculating and updating survivor priority.
use crate::domain::{
triage::TriageCalculator, Priority, Survivor, TriageStatus, VitalSignsReading,
Priority, Survivor, TriageStatus, VitalSignsReading,
triage::TriageCalculator,
};
/// Service for triage operations
@@ -15,7 +16,10 @@ impl TriageService {
/// Check if survivor should be upgraded
pub fn should_upgrade(survivor: &Survivor) -> bool {
TriageCalculator::should_upgrade(survivor.triage_status(), survivor.is_deteriorating())
TriageCalculator::should_upgrade(
survivor.triage_status(),
survivor.is_deteriorating(),
)
}
/// Get upgraded status
@@ -185,14 +189,9 @@ impl MassCasualtyAssessment {
Total: {} (Living: {}, Deceased: {})\n\
Immediate: {}, Delayed: {}, Minor: {}\n\
Severity: {:?}, Resources: {:?}",
self.total,
self.living(),
self.deceased,
self.immediate,
self.delayed,
self.minor,
self.severity,
self.resource_level
self.total, self.living(), self.deceased,
self.immediate, self.delayed, self.minor,
self.severity, self.resource_level
)
}
}
@@ -228,7 +227,9 @@ pub enum ResourceLevel {
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{BreathingPattern, BreathingType, ConfidenceScore, ScanZoneId};
use crate::domain::{
BreathingPattern, BreathingType, ConfidenceScore, ScanZoneId,
};
use chrono::Utc;
fn create_test_vitals(rate_bpm: f32) -> VitalSignsReading {
@@ -277,14 +278,12 @@ mod tests {
fn test_mass_casualty_assessment() {
let survivors: Vec<Survivor> = (0..10)
.map(|i| {
let rate = if i < 3 {
35.0
} else if i < 6 {
16.0
} else {
18.0
};
Survivor::new(ScanZoneId::new(), create_test_vitals(rate), None)
let rate = if i < 3 { 35.0 } else if i < 6 { 16.0 } else { 18.0 };
Survivor::new(
ScanZoneId::new(),
create_test_vitals(rate),
None,
)
})
.collect();
@@ -298,13 +297,21 @@ mod tests {
#[test]
fn test_priority_with_factors() {
// Deteriorating patient should be upgraded
let priority =
PriorityCalculator::calculate_with_factors(&TriageStatus::Delayed, true, 0, None);
let priority = PriorityCalculator::calculate_with_factors(
&TriageStatus::Delayed,
true,
0,
None,
);
assert_eq!(priority, Priority::Critical);
// Deep burial should upgrade
let priority =
PriorityCalculator::calculate_with_factors(&TriageStatus::Delayed, false, 0, Some(4.0));
let priority = PriorityCalculator::calculate_with_factors(
&TriageStatus::Delayed,
false,
0,
Some(4.0),
);
assert_eq!(priority, Priority::Critical);
}
}
+29 -21
View File
@@ -2,14 +2,14 @@
//!
//! These types are used for serializing/deserializing API requests and responses.
//! They provide a clean separation between domain models and API contracts.
#![allow(missing_docs)]
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::domain::{
AlertStatus, DisasterType, EventStatus, Priority, SurvivorStatus, TriageStatus, ZoneStatus,
DisasterType, EventStatus, ZoneStatus, TriageStatus, Priority,
AlertStatus, SurvivorStatus,
};
// ============================================================================
@@ -206,7 +206,9 @@ pub enum ZoneBoundsDto {
radius: f64,
},
/// Polygon boundary (list of vertices)
Polygon { vertices: Vec<(f64, f64)> },
Polygon {
vertices: Vec<(f64, f64)>,
},
}
/// Scan parameters for a zone.
@@ -230,15 +232,9 @@ pub struct ScanParametersDto {
pub heartbeat_detection: bool,
}
fn default_sensitivity() -> f64 {
0.8
}
fn default_max_depth() -> f64 {
5.0
}
fn default_true() -> bool {
true
}
fn default_sensitivity() -> f64 { 0.8 }
fn default_max_depth() -> f64 { 5.0 }
fn default_true() -> bool { true }
impl Default for ScanParametersDto {
fn default() -> Self {
@@ -554,7 +550,10 @@ pub enum WebSocketMessage {
survivor: SurvivorResponse,
},
/// Survivor lost (signal lost)
SurvivorLost { event_id: Uuid, survivor_id: Uuid },
SurvivorLost {
event_id: Uuid,
survivor_id: Uuid,
},
/// New alert generated
AlertCreated {
event_id: Uuid,
@@ -578,9 +577,14 @@ pub enum WebSocketMessage {
new_status: EventStatusDto,
},
/// Heartbeat/keep-alive
Heartbeat { timestamp: DateTime<Utc> },
Heartbeat {
timestamp: DateTime<Utc>,
},
/// Error message
Error { code: String, message: String },
Error {
code: String,
message: String,
},
}
/// WebSocket subscription request.
@@ -588,13 +592,19 @@ pub enum WebSocketMessage {
#[serde(tag = "action", rename_all = "snake_case")]
pub enum WebSocketRequest {
/// Subscribe to events for a disaster event
Subscribe { event_id: Uuid },
Subscribe {
event_id: Uuid,
},
/// Unsubscribe from events
Unsubscribe { event_id: Uuid },
Unsubscribe {
event_id: Uuid,
},
/// Subscribe to all events
SubscribeAll,
/// Request current state
GetState { event_id: Uuid },
GetState {
event_id: Uuid,
},
}
// ============================================================================
@@ -806,9 +816,7 @@ pub struct ListEventsQuery {
pub page_size: usize,
}
fn default_page_size() -> usize {
20
}
fn default_page_size() -> usize { 20 }
/// Query parameters for listing survivors.
#[derive(Debug, Clone, Deserialize, Default)]
+10 -4
View File
@@ -2,7 +2,6 @@
//!
//! This module provides a unified error type that maps to appropriate HTTP status codes
//! and JSON error responses for the API.
#![allow(missing_docs)]
use axum::{
http::StatusCode,
@@ -24,7 +23,10 @@ use uuid::Uuid;
pub enum ApiError {
/// Resource not found (404)
#[error("Resource not found: {resource_type} with id {id}")]
NotFound { resource_type: String, id: String },
NotFound {
resource_type: String,
id: String,
},
/// Invalid request data (400)
#[error("Bad request: {message}")]
@@ -43,7 +45,9 @@ pub enum ApiError {
/// Conflict with existing resource (409)
#[error("Conflict: {message}")]
Conflict { message: String },
Conflict {
message: String,
},
/// Resource is in invalid state for operation (409)
#[error("Invalid state: {message}")]
@@ -62,7 +66,9 @@ pub enum ApiError {
/// Service unavailable (503)
#[error("Service unavailable: {message}")]
ServiceUnavailable { message: String },
ServiceUnavailable {
message: String,
},
/// Domain error from business logic
#[error("Domain error: {0}")]
@@ -15,7 +15,8 @@ use super::dto::*;
use super::error::{ApiError, ApiResult};
use super::state::AppState;
use crate::domain::{
DisasterEvent, DisasterType, MovementType, ScanParameters, ScanResolution, ScanZone, ZoneBounds,
DisasterEvent, DisasterType, ScanZone, ZoneBounds,
ScanParameters, ScanResolution, MovementType,
};
// ============================================================================
@@ -94,7 +95,7 @@ pub async fn list_events(
let total = filtered.len();
// Apply pagination
let page_size = query.page_size.clamp(1, 100);
let page_size = query.page_size.min(100).max(1);
let start = query.page * page_size;
let events: Vec<_> = filtered
.into_iter()
@@ -317,12 +318,7 @@ pub async fn add_zone(
) -> ApiResult<(StatusCode, Json<ZoneResponse>)> {
// Convert DTO to domain
let bounds = match request.bounds {
ZoneBoundsDto::Rectangle {
min_x,
min_y,
max_x,
max_y,
} => {
ZoneBoundsDto::Rectangle { min_x, min_y, max_x, max_y } => {
if max_x <= min_x || max_y <= min_y {
return Err(ApiError::validation(
"max coordinates must be greater than min coordinates",
@@ -331,11 +327,7 @@ pub async fn add_zone(
}
ZoneBounds::rectangle(min_x, min_y, max_x, max_y)
}
ZoneBoundsDto::Circle {
center_x,
center_y,
radius,
} => {
ZoneBoundsDto::Circle { center_x, center_y, radius } => {
if radius <= 0.0 {
return Err(ApiError::validation(
"radius must be positive",
@@ -721,29 +713,26 @@ fn event_to_response(event: DisasterEvent) -> EventResponse {
fn zone_to_response(zone: &ScanZone) -> ZoneResponse {
let bounds = match zone.bounds() {
ZoneBounds::Rectangle {
min_x,
min_y,
max_x,
max_y,
} => ZoneBoundsDto::Rectangle {
min_x: *min_x,
min_y: *min_y,
max_x: *max_x,
max_y: *max_y,
},
ZoneBounds::Circle {
center_x,
center_y,
radius,
} => ZoneBoundsDto::Circle {
center_x: *center_x,
center_y: *center_y,
radius: *radius,
},
ZoneBounds::Polygon { vertices } => ZoneBoundsDto::Polygon {
vertices: vertices.clone(),
},
ZoneBounds::Rectangle { min_x, min_y, max_x, max_y } => {
ZoneBoundsDto::Rectangle {
min_x: *min_x,
min_y: *min_y,
max_x: *max_x,
max_y: *max_y,
}
}
ZoneBounds::Circle { center_x, center_y, radius } => {
ZoneBoundsDto::Circle {
center_x: *center_x,
center_y: *center_y,
radius: *radius,
}
}
ZoneBounds::Polygon { vertices } => {
ZoneBoundsDto::Polygon {
vertices: vertices.clone(),
}
}
};
let params = zone.parameters();
@@ -786,11 +775,7 @@ fn survivor_to_response(survivor: &crate::Survivor) -> SurvivorResponse {
let latest_vitals = survivor.vital_signs().latest();
let vital_signs = VitalSignsSummaryDto {
breathing_rate: latest_vitals.and_then(|v| v.breathing.as_ref().map(|b| b.rate_bpm)),
breathing_type: latest_vitals.and_then(|v| {
v.breathing
.as_ref()
.map(|b| format!("{:?}", b.pattern_type))
}),
breathing_type: latest_vitals.and_then(|v| v.breathing.as_ref().map(|b| format!("{:?}", b.pattern_type))),
heart_rate: latest_vitals.and_then(|v| v.heartbeat.as_ref().map(|h| h.rate_bpm)),
has_heartbeat: latest_vitals.map(|v| v.has_heartbeat()).unwrap_or(false),
has_movement: latest_vitals.map(|v| v.has_movement()).unwrap_or(false),
@@ -801,9 +786,7 @@ fn survivor_to_response(survivor: &crate::Survivor) -> SurvivorResponse {
None
}
}),
timestamp: latest_vitals
.map(|v| v.timestamp)
.unwrap_or_else(chrono::Utc::now),
timestamp: latest_vitals.map(|v| v.timestamp).unwrap_or_else(chrono::Utc::now),
};
let metadata = {
@@ -812,10 +795,7 @@ fn survivor_to_response(survivor: &crate::Survivor) -> SurvivorResponse {
None
} else {
Some(SurvivorMetadataDto {
estimated_age_category: m
.estimated_age_category
.as_ref()
.map(|a| format!("{:?}", a)),
estimated_age_category: m.estimated_age_category.as_ref().map(|a| format!("{:?}", a)),
assigned_team: m.assigned_team.clone(),
notes: m.notes.clone(),
tags: m.tags.clone(),
@@ -1075,9 +1055,9 @@ pub async fn list_domain_events(
State(state): State<AppState>,
) -> ApiResult<Json<DomainEventsResponse>> {
let store = state.event_store();
let events = store
.all()
.map_err(|e| ApiError::internal(format!("Failed to read event store: {}", e)))?;
let events = store.all().map_err(|e| ApiError::internal(
format!("Failed to read event store: {}", e),
))?;
let event_dtos: Vec<DomainEventDto> = events
.iter()
+8 -26
View File
@@ -33,14 +33,14 @@
//! - `WS /ws/mat/stream` - Real-time survivor and alert stream
pub mod dto;
pub mod error;
pub mod handlers;
pub mod error;
pub mod state;
pub mod websocket;
use axum::{
routing::{get, post},
Router,
routing::{get, post},
};
pub use dto::*;
@@ -64,39 +64,21 @@ pub use state::AppState;
pub fn create_router(state: AppState) -> Router {
Router::new()
// Event endpoints
.route(
"/api/v1/mat/events",
get(handlers::list_events).post(handlers::create_event),
)
.route("/api/v1/mat/events", get(handlers::list_events).post(handlers::create_event))
.route("/api/v1/mat/events/:event_id", get(handlers::get_event))
// Zone endpoints
.route(
"/api/v1/mat/events/:event_id/zones",
get(handlers::list_zones).post(handlers::add_zone),
)
.route("/api/v1/mat/events/:event_id/zones", get(handlers::list_zones).post(handlers::add_zone))
// Survivor endpoints
.route(
"/api/v1/mat/events/:event_id/survivors",
get(handlers::list_survivors),
)
.route("/api/v1/mat/events/:event_id/survivors", get(handlers::list_survivors))
// Alert endpoints
.route(
"/api/v1/mat/events/:event_id/alerts",
get(handlers::list_alerts),
)
.route(
"/api/v1/mat/alerts/:alert_id/acknowledge",
post(handlers::acknowledge_alert),
)
.route("/api/v1/mat/events/:event_id/alerts", get(handlers::list_alerts))
.route("/api/v1/mat/alerts/:alert_id/acknowledge", post(handlers::acknowledge_alert))
// Scan control endpoints (ADR-001: CSI data ingestion + pipeline control)
.route("/api/v1/mat/scan/csi", post(handlers::push_csi_data))
.route("/api/v1/mat/scan/control", post(handlers::scan_control))
.route("/api/v1/mat/scan/status", get(handlers::pipeline_status))
// Domain event store endpoint
.route(
"/api/v1/mat/events/domain",
get(handlers::list_domain_events),
)
.route("/api/v1/mat/events/domain", get(handlers::list_domain_events))
// WebSocket endpoint
.route("/ws/mat/stream", get(websocket::ws_handler))
.with_state(state)
+12 -13
View File
@@ -2,7 +2,6 @@
//!
//! This module provides the shared state that is passed to all API handlers.
//! It contains repositories, services, and real-time event broadcasting.
#![allow(missing_docs)]
use std::collections::HashMap;
use std::sync::Arc;
@@ -11,12 +10,12 @@ use parking_lot::RwLock;
use tokio::sync::broadcast;
use uuid::Uuid;
use super::dto::WebSocketMessage;
use crate::detection::{DetectionConfig, DetectionPipeline};
use crate::domain::{
DisasterEvent, Alert,
events::{EventStore, InMemoryEventStore},
Alert, DisasterEvent,
};
use crate::detection::{DetectionPipeline, DetectionConfig};
use super::dto::WebSocketMessage;
/// Shared application state for the API.
///
@@ -110,16 +109,12 @@ impl AppState {
/// Get scanning state.
pub fn is_scanning(&self) -> bool {
self.inner
.scanning
.load(std::sync::atomic::Ordering::SeqCst)
self.inner.scanning.load(std::sync::atomic::Ordering::SeqCst)
}
/// Set scanning state.
pub fn set_scanning(&self, state: bool) {
self.inner
.scanning
.store(state, std::sync::atomic::Ordering::SeqCst);
self.inner.scanning.store(state, std::sync::atomic::Ordering::SeqCst);
}
// ========================================================================
@@ -240,7 +235,7 @@ impl Default for AppState {
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{DisasterEvent, DisasterType};
use crate::domain::{DisasterType, DisasterEvent};
use geo::Point;
#[test]
@@ -263,7 +258,11 @@ mod tests {
#[test]
fn test_update_event() {
let state = AppState::new();
let event = DisasterEvent::new(DisasterType::Earthquake, Point::new(0.0, 0.0), "Test");
let event = DisasterEvent::new(
DisasterType::Earthquake,
Point::new(0.0, 0.0),
"Test",
);
let id = *event.id().as_uuid();
state.store_event(event);
@@ -280,7 +279,7 @@ mod tests {
#[test]
fn test_broadcast_subscribe() {
let state = AppState::new();
let _rx = state.subscribe();
let mut rx = state.subscribe();
state.broadcast(WebSocketMessage::Heartbeat {
timestamp: chrono::Utc::now(),
@@ -76,7 +76,10 @@ use super::state::AppState;
/// description: WebSocket connection established
/// ```
#[tracing::instrument(skip(state, ws))]
pub async fn ws_handler(State(state): State<AppState>, ws: WebSocketUpgrade) -> Response {
pub async fn ws_handler(
State(state): State<AppState>,
ws: WebSocketUpgrade,
) -> Response {
ws.on_upgrade(move |socket| handle_socket(socket, state))
}
@@ -85,8 +88,7 @@ async fn handle_socket(socket: WebSocket, state: AppState) {
let (mut sender, mut receiver) = socket.split();
// Subscription state for this connection
let subscriptions: Arc<Mutex<SubscriptionState>> =
Arc::new(Mutex::new(SubscriptionState::new()));
let subscriptions: Arc<Mutex<SubscriptionState>> = Arc::new(Mutex::new(SubscriptionState::new()));
// Subscribe to broadcast channel
let mut broadcast_rx = state.subscribe();
@@ -258,7 +260,7 @@ impl SubscriptionState {
WebSocketMessage::ZoneScanComplete { event_id, .. } => Some(*event_id),
WebSocketMessage::EventStatusChanged { event_id, .. } => Some(*event_id),
WebSocketMessage::Heartbeat { .. } => None, // Always receive
WebSocketMessage::Error { .. } => None, // Always receive
WebSocketMessage::Error { .. } => None, // Always receive
};
match event_id {
@@ -1,5 +1,4 @@
//! Breathing pattern detection from CSI signals.
#![allow(missing_docs)]
use crate::domain::{BreathingPattern, BreathingType};
@@ -52,8 +51,7 @@ impl CompressedBreathingBuffer {
// policy's age computation (now_ts - last_access_ts + 1) never wraps to
// zero (which would cause a divide-by-zero in wrapping_div).
self.compressor.set_access(ts, ts);
self.compressor
.push_frame(amplitudes, ts, &mut self.encoded);
self.compressor.push_frame(amplitudes, ts, &mut self.encoded);
self.frame_count += 1;
}
@@ -106,8 +104,8 @@ pub struct BreathingDetectorConfig {
impl Default for BreathingDetectorConfig {
fn default() -> Self {
Self {
min_rate_bpm: 4.0, // Very slow breathing
max_rate_bpm: 40.0, // Fast breathing (distressed)
min_rate_bpm: 4.0, // Very slow breathing
max_rate_bpm: 40.0, // Fast breathing (distressed)
min_amplitude: 0.1,
window_size: 512,
window_overlap: 0.5,
@@ -149,8 +147,12 @@ impl BreathingDetector {
let min_freq = self.config.min_rate_bpm as f64 / 60.0;
let max_freq = self.config.max_rate_bpm as f64 / 60.0;
let (dominant_freq, amplitude) =
self.find_dominant_frequency(&spectrum, sample_rate, min_freq, max_freq)?;
let (dominant_freq, amplitude) = self.find_dominant_frequency(
&spectrum,
sample_rate,
min_freq,
max_freq,
)?;
// Convert to BPM
let rate_bpm = (dominant_freq * 60.0) as f32;
@@ -183,27 +185,32 @@ impl BreathingDetector {
/// Compute frequency spectrum using FFT
fn compute_spectrum(&self, signal: &[f64]) -> Vec<f64> {
use rustfft::{num_complex::Complex, FftPlanner};
use rustfft::{FftPlanner, num_complex::Complex};
let n = signal.len().next_power_of_two();
let mut planner = FftPlanner::new();
let fft = planner.plan_fft_forward(n);
// Prepare input with zero padding
let mut buffer: Vec<Complex<f64>> = signal.iter().map(|&x| Complex::new(x, 0.0)).collect();
let mut buffer: Vec<Complex<f64>> = signal
.iter()
.map(|&x| Complex::new(x, 0.0))
.collect();
buffer.resize(n, Complex::new(0.0, 0.0));
// Apply Hanning window
for (i, sample) in buffer.iter_mut().enumerate().take(signal.len()) {
let window =
0.5 * (1.0 - (2.0 * std::f64::consts::PI * i as f64 / signal.len() as f64).cos());
let window = 0.5 * (1.0 - (2.0 * std::f64::consts::PI * i as f64 / signal.len() as f64).cos());
*sample = Complex::new(sample.re * window, 0.0);
}
fft.process(&mut buffer);
// Return magnitude spectrum (only positive frequencies)
buffer.iter().take(n / 2).map(|c| c.norm()).collect()
buffer.iter()
.take(n / 2)
.map(|c| c.norm())
.collect()
}
/// Find dominant frequency in a given range
@@ -228,11 +235,10 @@ impl BreathingDetector {
let mut max_amplitude = 0.0;
let mut max_bin_idx = min_bin;
for (i, &amp_val) in spectrum[min_bin..=max_bin].iter().enumerate() {
let bin = min_bin + i;
if amp_val > max_amplitude {
max_amplitude = amp_val;
max_bin_idx = bin;
for i in min_bin..=max_bin {
if spectrum[i] > max_amplitude {
max_amplitude = spectrum[i];
max_bin_idx = i;
}
}
@@ -265,8 +271,7 @@ impl BreathingDetector {
}
// Also check harmonics (2x, 3x frequency)
let harmonic_power: f64 = [2, 3]
.iter()
let harmonic_power: f64 = [2, 3].iter()
.filter_map(|&mult| {
let harmonic_bin = peak_bin * mult;
if harmonic_bin < spectrum.len() {
@@ -389,7 +394,9 @@ mod tests {
let detector = BreathingDetector::with_defaults();
// Random noise with low amplitude
let signal: Vec<f64> = (0..1000).map(|i| (i as f64 * 0.1).sin() * 0.01).collect();
let signal: Vec<f64> = (0..1000)
.map(|i| (i as f64 * 0.1).sin() * 0.01)
.collect();
let result = detector.detect(&signal, 100.0);
// Should either be None or have very low confidence
@@ -9,7 +9,9 @@
//! The classifier produces a single confidence score and a recommended
//! triage status based on the combined signals.
use crate::domain::{BreathingType, MovementType, TriageStatus, VitalSignsReading};
use crate::domain::{
BreathingType, MovementType, TriageStatus, VitalSignsReading,
};
/// Configuration for the ensemble classifier
#[derive(Debug, Clone)]
@@ -99,9 +101,8 @@ impl EnsembleClassifier {
};
// Weighted ensemble confidence
let total_weight = self.config.breathing_weight
+ self.config.heartbeat_weight
+ self.config.movement_weight;
let total_weight =
self.config.breathing_weight + self.config.heartbeat_weight + self.config.movement_weight;
let ensemble_confidence = if total_weight > 0.0 {
(breathing_conf * self.config.breathing_weight
@@ -146,7 +147,11 @@ impl EnsembleClassifier {
/// as Immediate regardless of confidence level, because in disaster response
/// a false negative (missing a survivor in distress) is far more costly
/// than a false positive.
fn determine_triage(&self, reading: &VitalSignsReading, confidence: f64) -> TriageStatus {
fn determine_triage(
&self,
reading: &VitalSignsReading,
confidence: f64,
) -> TriageStatus {
// CRITICAL PATTERNS: always classify regardless of confidence.
// In disaster response, any sign of distress must be escalated.
if let Some(ref breathing) = reading.breathing {
@@ -158,7 +163,7 @@ impl EnsembleClassifier {
}
let rate = breathing.rate_bpm;
if !(10.0..=30.0).contains(&rate) {
if rate < 10.0 || rate > 30.0 {
return TriageStatus::Immediate;
}
}
@@ -183,7 +188,7 @@ impl EnsembleClassifier {
if let Some(ref breathing) = reading.breathing {
let rate = breathing.rate_bpm;
if !(12.0..=24.0).contains(&rate) {
if rate < 12.0 || rate > 24.0 {
if has_movement {
return TriageStatus::Delayed;
}
@@ -210,7 +215,8 @@ impl EnsembleClassifier {
mod tests {
use super::*;
use crate::domain::{
BreathingPattern, ConfidenceScore, HeartbeatSignature, MovementProfile, SignalStrength,
BreathingPattern, HeartbeatSignature, MovementProfile,
SignalStrength, ConfidenceScore,
};
fn make_reading(
@@ -260,7 +266,11 @@ mod tests {
#[test]
fn test_agonal_breathing_is_immediate() {
let classifier = EnsembleClassifier::new(EnsembleConfig::default());
let reading = make_reading(Some((8.0, BreathingType::Agonal)), None, MovementType::None);
let reading = make_reading(
Some((8.0, BreathingType::Agonal)),
None,
MovementType::None,
);
let result = classifier.classify(&reading);
assert_eq!(result.recommended_triage, TriageStatus::Immediate);
@@ -285,10 +295,8 @@ mod tests {
let mut reading = VitalSignsReading::new(None, None, mv);
reading.confidence = ConfidenceScore::new(0.5);
let config = EnsembleConfig {
min_ensemble_confidence: 0.0,
..EnsembleConfig::default()
};
let mut config = EnsembleConfig::default();
config.min_ensemble_confidence = 0.0;
let classifier = EnsembleClassifier::new(config);
let result = classifier.classify(&reading);
@@ -1,5 +1,4 @@
//! Heartbeat detection from micro-Doppler signatures in CSI.
#![allow(missing_docs)]
use crate::domain::{HeartbeatSignature, SignalStrength};
@@ -32,12 +31,7 @@ impl CompressedHeartbeatSpectrogram {
.map(|i| TemporalTensorCompressor::new(TierPolicy::default(), 1, i as u32))
.collect();
let encoded = vec![Vec::new(); n_freq_bins];
Self {
bin_buffers,
encoded,
n_freq_bins,
frame_count: 0,
}
Self { bin_buffers, encoded, n_freq_bins, frame_count: 0 }
}
/// Push one column of the spectrogram (one time step, all frequency bins).
@@ -77,19 +71,11 @@ impl CompressedHeartbeatSpectrogram {
total += recent;
count += 1;
}
if count == 0 {
0.0
} else {
total / count as f32
}
if count == 0 { 0.0 } else { total / count as f32 }
}
pub fn frame_count(&self) -> u64 {
self.frame_count
}
pub fn n_freq_bins(&self) -> usize {
self.n_freq_bins
}
pub fn frame_count(&self) -> u64 { self.frame_count }
pub fn n_freq_bins(&self) -> usize { self.n_freq_bins }
}
/// Configuration for heartbeat detection
@@ -112,8 +98,8 @@ pub struct HeartbeatDetectorConfig {
impl Default for HeartbeatDetectorConfig {
fn default() -> Self {
Self {
min_rate_bpm: 30.0, // Very slow (bradycardia)
max_rate_bpm: 200.0, // Very fast (extreme tachycardia)
min_rate_bpm: 30.0, // Very slow (bradycardia)
max_rate_bpm: 200.0, // Very fast (extreme tachycardia)
min_signal_strength: 0.05,
window_size: 1024,
enhanced_processing: true,
@@ -175,8 +161,12 @@ impl HeartbeatDetector {
let min_freq = self.config.min_rate_bpm as f64 / 60.0;
let max_freq = self.config.max_rate_bpm as f64 / 60.0;
let (heart_freq, strength) =
self.find_heartbeat_frequency(&spectrum, sample_rate, min_freq, max_freq)?;
let (heart_freq, strength) = self.find_heartbeat_frequency(
&spectrum,
sample_rate,
min_freq,
max_freq,
)?;
if strength < self.config.min_signal_strength {
return None;
@@ -286,7 +276,7 @@ impl HeartbeatDetector {
/// Compute micro-Doppler spectrum optimized for heartbeat detection
fn compute_micro_doppler_spectrum(&self, signal: &[f64], _sample_rate: f64) -> Vec<f64> {
use rustfft::{num_complex::Complex, FftPlanner};
use rustfft::{FftPlanner, num_complex::Complex};
let n = signal.len().next_power_of_two();
let mut planner = FftPlanner::new();
@@ -298,7 +288,8 @@ impl HeartbeatDetector {
.enumerate()
.map(|(i, &x)| {
let n_f = signal.len() as f64;
let window = 0.42 - 0.5 * (2.0 * std::f64::consts::PI * i as f64 / n_f).cos()
let window = 0.42
- 0.5 * (2.0 * std::f64::consts::PI * i as f64 / n_f).cos()
+ 0.08 * (4.0 * std::f64::consts::PI * i as f64 / n_f).cos();
Complex::new(x * window, 0.0)
})
@@ -308,7 +299,10 @@ impl HeartbeatDetector {
fft.process(&mut buffer);
// Return power spectrum
buffer.iter().take(n / 2).map(|c| c.norm_sqr()).collect()
buffer.iter()
.take(n / 2)
.map(|c| c.norm_sqr())
.collect()
}
/// Find heartbeat frequency in spectrum
@@ -332,24 +326,22 @@ impl HeartbeatDetector {
// Find the strongest peak
let mut max_power = 0.0;
let mut max_bin_idx = min_bin;
let upper = max_bin.min(spectrum.len() - 1);
for (i, &pwr) in spectrum[min_bin..=upper].iter().enumerate() {
let bin = min_bin + i;
if pwr > max_power {
max_power = pwr;
max_bin_idx = bin;
for i in min_bin..=max_bin.min(spectrum.len() - 1) {
if spectrum[i] > max_power {
max_power = spectrum[i];
max_bin_idx = i;
}
}
// Check if it's a real peak (local maximum)
if max_bin_idx > 0
&& max_bin_idx < spectrum.len() - 1
&& (spectrum[max_bin_idx] <= spectrum[max_bin_idx - 1]
|| spectrum[max_bin_idx] <= spectrum[max_bin_idx + 1])
{
// Not a real peak
return None;
if max_bin_idx > 0 && max_bin_idx < spectrum.len() - 1 {
if spectrum[max_bin_idx] <= spectrum[max_bin_idx - 1]
|| spectrum[max_bin_idx] <= spectrum[max_bin_idx + 1]
{
// Not a real peak
return None;
}
}
let freq = max_bin_idx as f64 * freq_resolution;
@@ -412,7 +404,11 @@ impl HeartbeatDetector {
let strength_score = (strength / 0.5).min(1.0) as f32;
// Very low or very high HRV might indicate noise
let hrv_score = if hrv > 0.05 && hrv < 0.5 { 1.0 } else { 0.5 };
let hrv_score = if hrv > 0.05 && hrv < 0.5 {
1.0
} else {
0.5
};
strength_score * 0.7 + hrv_score * 0.3
}
@@ -438,10 +434,8 @@ mod heartbeat_buffer_tests {
// Low bins (0..15) should have higher power than high bins (16..31)
let low_power = spec.band_power(0, 15, 20);
let high_power = spec.band_power(16, 31, 20);
assert!(
low_power >= high_power,
"low_power={low_power} should >= high_power={high_power}"
);
assert!(low_power >= high_power,
"low_power={low_power} should >= high_power={high_power}");
}
}
@@ -12,12 +12,12 @@ mod heartbeat;
mod movement;
mod pipeline;
pub use breathing::{BreathingDetector, BreathingDetectorConfig};
#[cfg(feature = "ruvector")]
pub use breathing::CompressedBreathingBuffer;
pub use breathing::{BreathingDetector, BreathingDetectorConfig};
pub use ensemble::{EnsembleClassifier, EnsembleConfig, EnsembleResult, SignalConfidences};
pub use heartbeat::{HeartbeatDetector, HeartbeatDetectorConfig};
#[cfg(feature = "ruvector")]
pub use heartbeat::CompressedHeartbeatSpectrogram;
pub use heartbeat::{HeartbeatDetector, HeartbeatDetectorConfig};
pub use movement::{MovementClassifier, MovementClassifierConfig};
pub use pipeline::{CsiDataBuffer, DetectionConfig, DetectionPipeline, VitalSignsDetector};
pub use pipeline::{DetectionPipeline, DetectionConfig, VitalSignsDetector, CsiDataBuffer};
@@ -54,8 +54,11 @@ impl MovementClassifier {
let periodicity = self.calculate_periodicity(csi_signal, sample_rate);
// Determine movement type
let (movement_type, is_voluntary) =
self.determine_movement_type(variance, max_change, periodicity);
let (movement_type, is_voluntary) = self.determine_movement_type(
variance,
max_change,
periodicity,
);
// Calculate intensity
let intensity = self.calculate_intensity(variance, max_change);
@@ -78,7 +81,9 @@ impl MovementClassifier {
}
let mean = signal.iter().sum::<f64>() / signal.len() as f64;
let variance = signal.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / signal.len() as f64;
let variance = signal.iter()
.map(|x| (x - mean).powi(2))
.sum::<f64>() / signal.len() as f64;
variance
}
@@ -89,8 +94,7 @@ impl MovementClassifier {
return 0.0;
}
signal
.windows(2)
signal.windows(2)
.map(|w| (w[1] - w[0]).abs())
.fold(0.0, f64::max)
}
@@ -116,8 +120,7 @@ impl MovementClassifier {
let mut max_corr = 0.0;
for lag in 1..max_lag {
let corr: f64 = centered
.iter()
let corr: f64 = centered.iter()
.take(n - lag)
.zip(centered.iter().skip(lag))
.map(|(a, b)| a * b)
@@ -194,8 +197,7 @@ impl MovementClassifier {
let mean = signal.iter().sum::<f64>() / signal.len() as f64;
let centered: Vec<f64> = signal.iter().map(|x| x - mean).collect();
let zero_crossings: usize = centered
.windows(2)
let zero_crossings: usize = centered.windows(2)
.filter(|w| (w[0] >= 0.0) != (w[1] >= 0.0))
.count();
@@ -225,17 +227,13 @@ mod tests {
let classifier = MovementClassifier::with_defaults();
// Simulate large movement
let signal: Vec<f64> = (0..200)
.map(|i| {
if (50..100).contains(&i) {
2.0
} else if (150..180).contains(&i) {
-1.5
} else {
0.0
}
})
.collect();
let mut signal: Vec<f64> = vec![0.0; 200];
for i in 50..100 {
signal[i] = 2.0;
}
for i in 150..180 {
signal[i] = -1.5;
}
let profile = classifier.classify(&signal, 100.0);
assert!(matches!(profile.movement_type, MovementType::Gross));
@@ -261,11 +259,15 @@ mod tests {
let classifier = MovementClassifier::with_defaults();
// Low intensity
let low_signal: Vec<f64> = (0..200).map(|i| (i as f64 * 0.1).sin() * 0.05).collect();
let low_signal: Vec<f64> = (0..200)
.map(|i| (i as f64 * 0.1).sin() * 0.05)
.collect();
let low_profile = classifier.classify(&low_signal, 100.0);
// High intensity
let high_signal: Vec<f64> = (0..200).map(|i| (i as f64 * 0.1).sin() * 2.0).collect();
let high_signal: Vec<f64> = (0..200)
.map(|i| (i as f64 * 0.1).sin() * 2.0)
.collect();
let high_profile = classifier.classify(&high_signal, 100.0);
assert!(high_profile.intensity > low_profile.intensity);
@@ -3,13 +3,14 @@
//! This module provides both traditional signal-processing-based detection
//! and optional ML-enhanced detection for improved accuracy.
use super::{
BreathingDetector, BreathingDetectorConfig, HeartbeatDetector, HeartbeatDetectorConfig,
MovementClassifier, MovementClassifierConfig,
};
use crate::domain::{ScanZone, VitalSignsReading};
use crate::ml::{MlDetectionConfig, MlDetectionPipeline, MlDetectionResult};
use crate::{DisasterConfig, MatError};
use super::{
BreathingDetector, BreathingDetectorConfig,
HeartbeatDetector, HeartbeatDetectorConfig,
MovementClassifier, MovementClassifierConfig,
};
/// Configuration for the detection pipeline
#[derive(Debug, Clone)]
@@ -85,7 +86,7 @@ pub trait VitalSignsDetector: Send + Sync {
}
/// Buffer for CSI data samples
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default)]
pub struct CsiDataBuffer {
/// Amplitude samples
pub amplitudes: Vec<f64>,
@@ -179,7 +180,7 @@ impl DetectionPipeline {
/// Check if ML pipeline is ready
pub fn ml_ready(&self) -> bool {
self.ml_pipeline.as_ref().is_none_or(|ml| ml.is_ready())
self.ml_pipeline.as_ref().map_or(true, |ml| ml.is_ready())
}
/// Process a scan zone and return detected vital signs.
@@ -191,30 +192,23 @@ impl DetectionPipeline {
///
/// Returns `None` if insufficient data is buffered (< 5 seconds) or if
/// detection confidence is below the configured threshold.
pub async fn process_zone(
&self,
zone: &ScanZone,
) -> Result<Option<VitalSignsReading>, MatError> {
pub async fn process_zone(&self, zone: &ScanZone) -> Result<Option<VitalSignsReading>, MatError> {
// Process buffered CSI data through the signal processing pipeline.
// Data arrives via add_data() from hardware adapters (ESP32, Intel 5300, etc.)
// or from the CSI push API endpoint.
// Drop the MutexGuard before hitting any await point.
let reading = {
let buffer = self.data_buffer.read();
if !buffer.has_sufficient_data(5.0) {
// Need at least 5 seconds of data
return Ok(None);
}
// Detect vital signs using traditional pipeline
self.detect_from_buffer(&buffer, zone)?
// `buffer` guard dropped here
};
let buffer = self.data_buffer.read();
if !buffer.has_sufficient_data(5.0) {
// Need at least 5 seconds of data
return Ok(None);
}
// Detect vital signs using traditional pipeline
let reading = self.detect_from_buffer(&buffer, zone)?;
// If ML is enabled and ready, enhance with ML predictions
let enhanced_reading = if self.config.enable_ml && self.ml_ready() {
// Snapshot the buffer under the lock, then drop the guard before await.
let buffer_snapshot = { self.data_buffer.read().clone() };
self.enhance_with_ml(reading, &buffer_snapshot).await?
self.enhance_with_ml(reading, &buffer).await?
} else {
reading
};
@@ -263,16 +257,12 @@ impl DetectionPipeline {
/// Get the latest ML detection results (if ML is enabled)
pub async fn get_ml_results(&self) -> Option<MlDetectionResult> {
let ml = match &self.ml_pipeline {
Some(ml) => ml,
None => return None,
};
// Acquire lock, clone the relevant buffer data, then drop the guard before awaiting.
let buffer = {
let guard = self.data_buffer.read();
guard.clone()
};
ml.process(&buffer).await.ok()
let buffer = self.data_buffer.read();
if let Some(ref ml) = self.ml_pipeline {
ml.process(&buffer).await.ok()
} else {
None
}
}
/// Add CSI data to the processing buffer
@@ -302,29 +292,31 @@ impl DetectionPipeline {
_zone: &ScanZone,
) -> Result<Option<VitalSignsReading>, MatError> {
// Detect breathing
let breathing = self
.breathing_detector
.detect(&buffer.amplitudes, buffer.sample_rate);
let breathing = self.breathing_detector.detect(
&buffer.amplitudes,
buffer.sample_rate,
);
// Detect heartbeat (if enabled)
let heartbeat = if self.config.enable_heartbeat {
let breathing_rate = breathing.as_ref().map(|b| b.rate_bpm as f64);
self.heartbeat_detector
.detect(&buffer.phases, buffer.sample_rate, breathing_rate)
self.heartbeat_detector.detect(
&buffer.phases,
buffer.sample_rate,
breathing_rate,
)
} else {
None
};
// Classify movement
let movement = self
.movement_classifier
.classify(&buffer.amplitudes, buffer.sample_rate);
let movement = self.movement_classifier.classify(
&buffer.amplitudes,
buffer.sample_rate,
);
// Check if we detected anything
if breathing.is_none()
&& heartbeat.is_none()
&& movement.movement_type == crate::domain::MovementType::None
{
if breathing.is_none() && heartbeat.is_none() && movement.movement_type == crate::domain::MovementType::None {
return Ok(None);
}
@@ -366,27 +358,31 @@ impl DetectionPipeline {
impl VitalSignsDetector for DetectionPipeline {
fn detect(&self, csi_data: &CsiDataBuffer) -> Option<VitalSignsReading> {
// Detect breathing from amplitude variations
let breathing = self
.breathing_detector
.detect(&csi_data.amplitudes, csi_data.sample_rate);
let breathing = self.breathing_detector.detect(
&csi_data.amplitudes,
csi_data.sample_rate,
);
// Detect heartbeat from phase variations
let heartbeat = if self.config.enable_heartbeat {
let breathing_rate = breathing.as_ref().map(|b| b.rate_bpm as f64);
self.heartbeat_detector
.detect(&csi_data.phases, csi_data.sample_rate, breathing_rate)
self.heartbeat_detector.detect(
&csi_data.phases,
csi_data.sample_rate,
breathing_rate,
)
} else {
None
};
// Classify movement
let movement = self
.movement_classifier
.classify(&csi_data.amplitudes, csi_data.sample_rate);
let movement = self.movement_classifier.classify(
&csi_data.amplitudes,
csi_data.sample_rate,
);
// Create reading if we detected anything
if breathing.is_some()
|| heartbeat.is_some()
if breathing.is_some() || heartbeat.is_some()
|| movement.movement_type != crate::domain::MovementType::None
{
Some(VitalSignsReading::new(breathing, heartbeat, movement))
@@ -461,7 +457,9 @@ mod tests {
#[test]
fn test_config_from_disaster_config() {
let disaster_config = DisasterConfig::builder().sensitivity(0.9).build();
let disaster_config = DisasterConfig::builder()
.sensitivity(0.9)
.build();
let detection_config = DetectionConfig::from_disaster_config(&disaster_config);
@@ -3,7 +3,7 @@
use chrono::{DateTime, Utc};
use uuid::Uuid;
use super::{Coordinates3D, SurvivorId, TriageStatus};
use super::{SurvivorId, TriageStatus, Coordinates3D};
/// Unique identifier for an alert
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -398,7 +398,11 @@ mod tests {
#[test]
fn test_alert_lifecycle() {
let mut alert = Alert::new(SurvivorId::new(), Priority::High, create_test_payload());
let mut alert = Alert::new(
SurvivorId::new(),
Priority::High,
create_test_payload(),
);
// Initial state
assert!(alert.is_pending());
@@ -425,7 +429,11 @@ mod tests {
#[test]
fn test_alert_escalation() {
let mut alert = Alert::new(SurvivorId::new(), Priority::Low, create_test_payload());
let mut alert = Alert::new(
SurvivorId::new(),
Priority::Low,
create_test_payload(),
);
alert.escalate();
assert_eq!(alert.priority(), Priority::Medium);
@@ -444,17 +452,8 @@ mod tests {
#[test]
fn test_priority_from_triage() {
assert_eq!(
Priority::from_triage(&TriageStatus::Immediate),
Priority::Critical
);
assert_eq!(
Priority::from_triage(&TriageStatus::Delayed),
Priority::High
);
assert_eq!(
Priority::from_triage(&TriageStatus::Minor),
Priority::Medium
);
assert_eq!(Priority::from_triage(&TriageStatus::Immediate), Priority::Critical);
assert_eq!(Priority::from_triage(&TriageStatus::Delayed), Priority::High);
assert_eq!(Priority::from_triage(&TriageStatus::Minor), Priority::Medium);
}
}
@@ -17,12 +17,7 @@ pub struct Coordinates3D {
impl Coordinates3D {
/// Create new coordinates with uncertainty
pub fn new(x: f64, y: f64, z: f64, uncertainty: LocationUncertainty) -> Self {
Self {
x,
y,
z,
uncertainty,
}
Self { x, y, z, uncertainty }
}
/// Create coordinates with default uncertainty
@@ -81,9 +76,9 @@ pub struct LocationUncertainty {
impl Default for LocationUncertainty {
fn default() -> Self {
Self {
horizontal_error: 2.0, // 2 meter default uncertainty
vertical_error: 1.0, // 1 meter vertical uncertainty
confidence: 0.95, // 95% confidence
horizontal_error: 2.0, // 2 meter default uncertainty
vertical_error: 1.0, // 1 meter vertical uncertainty
confidence: 0.95, // 95% confidence
}
}
}
@@ -123,11 +118,11 @@ impl LocationUncertainty {
// Combined uncertainty is reduced when multiple estimates agree
let h_var1 = self.horizontal_error * self.horizontal_error;
let h_var2 = other.horizontal_error * other.horizontal_error;
let combined_h_var = 1.0 / (1.0 / h_var1 + 1.0 / h_var2);
let combined_h_var = 1.0 / (1.0/h_var1 + 1.0/h_var2);
let v_var1 = self.vertical_error * self.vertical_error;
let v_var2 = other.vertical_error * other.vertical_error;
let combined_v_var = 1.0 / (1.0 / v_var1 + 1.0 / v_var2);
let combined_v_var = 1.0 / (1.0/v_var1 + 1.0/v_var2);
LocationUncertainty {
horizontal_error: combined_h_var.sqrt(),
@@ -230,10 +225,8 @@ impl DebrisProfile {
/// Check if debris allows good signal penetration
pub fn is_penetrable(&self) -> bool {
!matches!(
self.metal_content,
MetalContent::High | MetalContent::Blocking
) && self.primary_material.attenuation_coefficient() < 5.0
!matches!(self.metal_content, MetalContent::High | MetalContent::Blocking)
&& self.primary_material.attenuation_coefficient() < 5.0
}
}
@@ -1,10 +1,13 @@
//! Disaster event aggregate root.
use chrono::{DateTime, Utc};
use geo::Point;
use uuid::Uuid;
use geo::Point;
use super::{Coordinates3D, ScanZone, ScanZoneId, Survivor, SurvivorId, VitalSignsReading};
use super::{
Survivor, SurvivorId, ScanZone, ScanZoneId,
VitalSignsReading, Coordinates3D,
};
use crate::MatError;
/// Unique identifier for a disaster event
@@ -63,7 +66,7 @@ pub enum DisasterType {
impl DisasterType {
/// Get typical debris profile for this disaster type
pub fn typical_debris_profile(&self) -> super::DebrisProfile {
use super::{DebrisMaterial, DebrisProfile, MetalContent, MoistureLevel};
use super::{DebrisProfile, DebrisMaterial, MoistureLevel, MetalContent};
match self {
DisasterType::BuildingCollapse => DebrisProfile {
@@ -115,9 +118,9 @@ impl DisasterType {
/// Get expected maximum survival time (hours)
pub fn expected_survival_hours(&self) -> u32 {
match self {
DisasterType::Avalanche => 2, // Limited air, hypothermia
DisasterType::Flood => 6, // Drowning risk
DisasterType::MineCollapse => 72, // Air supply critical
DisasterType::Avalanche => 2, // Limited air, hypothermia
DisasterType::Flood => 6, // Drowning risk
DisasterType::MineCollapse => 72, // Air supply critical
DisasterType::BuildingCollapse => 96,
DisasterType::Earthquake => 120,
DisasterType::Landslide => 48,
@@ -185,7 +188,11 @@ pub struct EventMetadata {
impl DisasterEvent {
/// Create a new disaster event
pub fn new(event_type: DisasterType, location: Point<f64>, description: &str) -> Self {
pub fn new(
event_type: DisasterType,
location: Point<f64>,
description: &str,
) -> Self {
Self {
id: DisasterEventId::new(),
event_type,
@@ -290,9 +297,7 @@ impl DisasterEvent {
if let Some(existing) = existing_id {
// Update existing survivor
let survivor = self
.survivors
.iter_mut()
let survivor = self.survivors.iter_mut()
.find(|s| s.id() == &existing)
.ok_or_else(|| MatError::Domain("Survivor not found".into()))?;
survivor.update_vitals(vitals);
@@ -306,10 +311,7 @@ impl DisasterEvent {
let survivor = Survivor::new(zone_id, vitals, location);
self.survivors.push(survivor);
// Safe: we just pushed, so last() is always Some
Ok(self
.survivors
.last()
.expect("survivors is non-empty after push"))
Ok(self.survivors.last().expect("survivors is non-empty after push"))
}
/// Find a survivor near a location
@@ -423,7 +425,7 @@ impl TriageCounts {
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{BreathingPattern, BreathingType, ConfidenceScore, ZoneBounds};
use crate::domain::{ZoneBounds, BreathingPattern, BreathingType, ConfidenceScore};
fn create_test_vitals() -> VitalSignsReading {
VitalSignsReading {
@@ -454,8 +456,11 @@ mod tests {
#[test]
fn test_add_zone_activates_event() {
let mut event =
DisasterEvent::new(DisasterType::BuildingCollapse, Point::new(0.0, 0.0), "Test");
let mut event = DisasterEvent::new(
DisasterType::BuildingCollapse,
Point::new(0.0, 0.0),
"Test",
);
assert_eq!(event.status(), &EventStatus::Initializing);
@@ -467,7 +472,11 @@ mod tests {
#[test]
fn test_record_detection() {
let mut event = DisasterEvent::new(DisasterType::Earthquake, Point::new(0.0, 0.0), "Test");
let mut event = DisasterEvent::new(
DisasterType::Earthquake,
Point::new(0.0, 0.0),
"Test",
);
let zone = ScanZone::new("Zone A", ZoneBounds::rectangle(0.0, 0.0, 10.0, 10.0));
let zone_id = zone.id().clone();
@@ -481,9 +490,6 @@ mod tests {
#[test]
fn test_disaster_type_survival_hours() {
assert!(
DisasterType::Avalanche.expected_survival_hours()
< DisasterType::Earthquake.expected_survival_hours()
);
assert!(DisasterType::Avalanche.expected_survival_hours() < DisasterType::Earthquake.expected_survival_hours());
}
}

Some files were not shown because too many files have changed in this diff Show More