Repository navigation
add multi-hop relay + leader-election protocol for the sensor-sync backbone (m3b) - #12
Conversation
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Self-review (bot) — M3b base PR: reserved→ttl/flags split, relay + election helpers, host tests, docs. Wire-compat of the repurpose checks out (header still 20 bytes, offsets pinned by test_wire_compat, and legacy receivers are safe: ss_is_our_frame/ss_parse_header never inspect the former reserved bytes, so a router-stamped flags is ignored by pre-M3b edges).
Top-level findings:
- [low]
SS_FLAG_RELAYEDis stamped but never consumed — routers set it on re-broadcast frames, but no receiver reads it and it plays no role in dedup/loop suppression. Fine as a diagnostics marker; worth one line in SENSOR_SYNC.md saying it is diagnostics-only, so nobody later assumes it participates in relay decisions.
Router-firmware findings (leader-state tracking + unused term, millis-wrap in the uptime metric, TX backpressure not actually honored, OTA error status) are posted on the companion PR aphelps/esp-now-router#1.
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Re-review (bot) — incremental, covering the two commits since the last review (reserved->ttl/flags doc + relay-table eviction test).
Prior issues:
- [addressed]
SS_FLAG_RELAYEDstamped but never consumed — now explicitly documented as diagnostics-only at the definition site insensor_sync_protocol.h("NOT consumed by relay/dispatch... purely so a sniffer can tell origin frames from relays"). That is where a future reader would look before wiring it into relay logic, so this closes the finding. (SENSOR_SYNC.md's wire-format table still doesn't name the flag bit itself — fine, the header is the source of truth.)
New commits:
test_relay_evictionis correct and meaningful — I traced it againstss_relay_slot's evict-slot-0 policy: origin 3 evicts origin 1, origin 1's return then evicts origin 3 and re-floods exactly once, and is immediately deduped again. Matches the implementation's documented bounded-re-flood behavior; suite compiles clean under-Wall -Wextraand passes locally.- One behavior the test surfaces (pre-existing, not introduced here, no action needed): fixed slot-0 eviction means two hot origins beyond table capacity can ping-pong slot 0, each re-flooding once per alternation — bounded per frame (never a loop), just more duplicate waves than LRU would give. Worth remembering when sizing the router's table for the multi-hop bench test.
No new issues — clean.
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Thanks for the thorough review — all points are addressed across the follow-up commits (both repos on m3b-backbone-router):
WLED (protocol):
- Router-only helpers renamed
ss_router_*(ss_router_should_relay,ss_router_relay_slot,ss_router_beacon_better,SensorRouterPeer) —a86215a4. BACKBONE_ROUTER.mdmoved to the esp-now-router repo; SENSOR_SYNC.md points to it —a86215a4.
esp-now-router (firmware):
- Debug.h — ArduinoLibs
Debuglib +-D DEBUG_LEVEL=1(errors + major default);Serial.*→DEBUG_*/DEBUG1_*—a8c8895. - Default SSID — fallback SoftAP
esp-now-router/meshrouterso OTA is always reachable —65c8a25. - Endpoints + server in main.cpp —
/info(config JSON),/routes(dedup table + neighbor routers),/debug(leadership, counters, uptime, heap);ota.cppjust does WiFi +POST /update—064a08c. - README milestones removed; function header comments + section whitespace —
6cf9982,064a08c. - Testing/coverage — extracted the leader-election state machine into a pure
router_election.hand addedtests/election_test.cpp(boot hold-down, promotion+term, N=1, step-down, best-heard, stale-term, failover) alongside the relay tests;tests/Makefileruns both suites + a gcovcoveragetarget. Router tests the parts it uses; WLED tests the edge dispatch —c2e7e03(+ relay-test relocation in6cf9982).
Both firmwares build; all host suites pass. Ready for another look.
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Re-review (bot) — incremental, covering the one commit since the last review (a86215a: rename the router-only helpers to ss_router_* and move BACKBONE_ROUTER.md + the relay/election host test out to the esp-now-router repo).
Prior issues:
- None outstanding — the last review was clean (the
SS_FLAG_RELAYEDdoc finding was already closed).
New commit:
- Rename is complete in this repo — grep at PR head finds no remaining
ss_should_relay/SensorRelayPeer/ss_relay_slot/ss_beacon_betterreferences; the remaining host tests (sensor_sync_test.cpp,sensor_sync_ring_test.cpp) compile clean under-Wall -Wextraand pass at head. - The move landed on the other side —
esp-now-routernow carriesBACKBONE_ROUTER.mdplustests/relay_test.cpp+election_test.cppusing the new names, and itsmake testpasses against this PR's renamed header, so SENSOR_SYNC.md's claim that the router helpers are tested from the router repo holds. The coverage coupling is now cross-repo — a header edit made from the WLED side is only exercised by running the router repo's tests — but SENSOR_SYNC.md documents exactly that split, so no action. - Cross-repo nit for the companion PR (aphelps/esp-now-router#1, not this one):
esp-now-router/README.mdline 7 still saysss_should_relay()— the one stale old name the rename missed.
One INLINE doc leftover below (stale paragraph in SENSOR_SYNC.md), plus two whitespace suggestions.
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Per the review question ("why are the router-only functions in WLED at all?") — moved them out. The relay + election logic (ss_router_should_relay, ss_router_relay_slot, SensorRouterPeer, ss_router_beacon_better, RouterBeacon) now lives in esp-now-router/src/router_relay.h; the WLED shared header keeps only the wire format the edge actually needs — SensorSyncHeader (ttl/flags), SS_DEFAULT_TTL, and the reserved msgType/flag numbers (MSG_BEACON, SS_FLAG_RELAYED) so the format stays authoritative. The router repo is now self-contained for its own logic + tests. WLED f4f70db9, esp-now-router 1126ec6. Edge firmware + all host suites still build/pass.
aphelps
left a comment
There was a problem hiding this comment.
Overall good, but please cleanup the comments. They shouldn't be discussing phases of development, they should describe the code that is present.
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Addressed — comments now describe the code rather than the development history.
Swept both repos for milestone names (M3a/M3b/M3c), plan-subtask references, and "v1 vs later" framing, and rewrote each one to say what the code actually does:
sensor_sync_protocol.h— thettl/flagsheader note now explains the field layout and why the version stays 5, instead of narrating the change; the beaconmsgType, the two header fields, and the "routing logic lives in the router repo" banner lost their milestone tags.usermod_sensor_sync.{h,cpp}— the ESP-NOW transport header, theuseEspNowconfig comment, and the transport-selection note now describe the default (UDP) behavior directly.SENSOR_SYNC.md— "Multi-hop backbone" section and the design-goals line describe the transports that exist.- esp-now-router — file headers,
platformio.ini,README.md, the TTL rule, and the OTA notes. Thesetup()"TODO (OTA subtask)" comment was stale — it described work thatotaWifiBegin()already does, so it now points at that. BACKBONE_ROUTER.md— rewritten to document the architecture as built. The leader-election section had been written as a proposed design ("feeds the subtask", "recovery measured in M3c") while the code implements it; it now describes the shipped behavior, including the boot hold-down and best-heard/termhandling.
Sweep is clean, both firmwares build, and all host suites pass (make test at the super-repo root).
aphelps-claude-bot
left a comment
There was a problem hiding this comment.
Re-review (bot) — incremental, covering the three commits since the last review (c43c024 stale-doc/alignment fixes, f4f70db move router relay/election logic out of the shared header, 983578d comment cleanup per Adam's CHANGES_REQUESTED: describe the code, not the development phases).
Prior issues — all addressed:
- Stale SENSOR_SYNC.md paragraph (broken
BACKBONE_ROUTER.mdlink + deleted-test pointer) — removed inc43c024. - Two continuation-alignment nits in
sensor_sync_protocol.h— fixed inc43c024, then mooted byf4f70db(those functions no longer live in this header). - Cross-repo nit:
esp-now-router/README.mdnow saysss_router_should_relay()— fixed on the companion-PR side.
New commits — the rewritten comments were checked against the code at head, and they hold:
ttl/flagsoccupy the formerreservedu16 at the same offsets (header still 20 B, version still 5);ss_parse_header/ss_is_our_framedo gate on version==5, andss_parse_header's msgType check does rejectSENSOR_SYNC_MSG_BEACONat the edge.- The edge sender stamps
ttl=SS_DEFAULT_TTL(usermod_sensor_sync.cpp:129); the router injects the default onttl==0(esp-now-router/src/router_relay.h:51) and setsSS_FLAG_RELAYEDpurely as diagnostics (src/main.cpp:233) — matching the new header comments. - The "routing logic lives in the router repo" banner is accurate: all five moved names (
ss_router_should_relay/ss_router_relay_slot/SensorRouterPeer/ss_router_beacon_better/RouterBeacon) now live inesp-now-router/src/router_relay.h. - Test comment "
h{}zero-inits ttl/flags" is correct (aggregate value-initialization); both host tests compile clean under-Wall -Wextraand pass at head.
New issues:
- INLINE below — SENSOR_SYNC.md still says the
ss_router_*helpers are "in this header";f4f70dbmoved them out, so the paragraph now contradicts both the code andsensor_sync_protocol.h's own banner. - Two milestone stragglers survived the sweep, both OUTSIDE this PR's diff hunks (so no inline
suggestionis possible):usermod_sensor_sync.h:38("…M3 adds an ESP-NOW impl" — the ESP-NOW impl exists ~30 lines below in the same file) and SENSOR_SYNC.md's "Transport seam" section ("Milestone M3 adds an ESP-NOW / multi-hop implementation…" — both transports exist now, selected viauseEspNow). Both describe as future what the code already has — the exact class this cleanup targeted; two one-line touch-ups would finish it.
… bus Carry routing state in the SensorSyncHeader so a dedicated router tier can relay sensor frames beyond direct radio range. The `ttl` and `flags` bytes occupy what was a 16-bit `reserved` padding word, so the header stays 20 bytes and the version stays 5 — senders predating the split send ttl=0, which a relay reads as "unset" and fills in with SS_DEFAULT_TTL. Raising the version would have broken interoperation, since ss_parse_header/ss_is_our_frame gate on version==5. Reserve SENSOR_SYNC_MSG_BEACON for the routers' leader-election beacon (edges reject it via the msgType check) and SS_FLAG_RELAYED as a diagnostics-only marker. The edge sender stamps ttl=SS_DEFAULT_TTL. The relay and election *logic* lives in the esp-now-router repo, not here — the edge never calls it; only the wire format is shared. Document the split and the multi-hop backbone in SENSOR_SYNC.md.
32de175 to
ddea762
Compare
Part of Mesh M3b (multi-hop backbone router). Adds the relay + leader-election wire protocol to the shared SensorSync header, consumed by the new
esp-now-routerfirmware (aphelps/esp-now-router PR).Changes
sensor_sync_protocol.h— still v5, no version bump:reservedu16 →ttl+flags(same offset, header still 20 bytes; legacy frames arrivettl=0→ relay injectsSS_DEFAULT_TTL). Added puress_should_relay(per-originseqdedup + TTL decrement + self-origin skip) andSENSOR_SYNC_MSG_BEACON+ss_beacon_betterelection metric(uptimeTicks, deviceId).usermod_sensor_sync.cpp) stampsttl=SS_DEFAULT_TTL.tests/sensor_sync_relay_test.cpp— dedup / 16-bit wraparound / TTL cutoff / loop-free flood (triangle, line, bridged out-of-range) / beacon metric.SENSOR_SYNC.mdwire-format + Multi-hop backbone section; newBACKBONE_ROUTER.mddesign.Test plan
sensor_sync_relay_test.cpp+sensor_sync_test.cpp+sensor_sync_ring_test.cpp— ALL PASS (c++ -std=c++11 -Wall -Wextra).pio run -e ampworks(edge firmware) — SUCCESS (additive change safe).Manual (hardware, tracked in the M3b plan): fixed-channel spike, multi-hop relay, OTA one-and-done.