Skip to content

add multi-hop relay + leader-election protocol for the sensor-sync backbone (m3b) - #12

Merged
aphelps-claude-bot merged 1 commit into
base-wled-16.0.1from
m3b-backbone-router
Jul 29, 2026
Merged

aphelps-claude-bot merged 1 commit into
base-wled-16.0.1from
m3b-backbone-router

Conversation

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator

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-router firmware (aphelps/esp-now-router PR).

Changes

  • sensor_sync_protocol.h — still v5, no version bump: reserved u16 → ttl+flags (same offset, header still 20 bytes; legacy frames arrive ttl=0 → relay injects SS_DEFAULT_TTL). Added pure ss_should_relay (per-origin seq dedup + TTL decrement + self-origin skip) and SENSOR_SYNC_MSG_BEACON + ss_beacon_better election metric (uptimeTicks, deviceId).
  • Edge sender (usermod_sensor_sync.cpp) stamps ttl=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.
  • Docs: SENSOR_SYNC.md wire-format + Multi-hop backbone section; new BACKBONE_ROUTER.md design.

Test plan

  • Host tests: 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.

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_RELAYED is 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 aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_RELAYED stamped but never consumed — now explicitly documented as diagnostics-only at the definition site in sensor_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_eviction is correct and meaningful — I traced it against ss_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 -Wextra and 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 aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment about

Comment thread usermods/ampworks/BACKBONE_ROUTER.md Resolved

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.md moved to the esp-now-router repo; SENSOR_SYNC.md points to it — a86215a4.

esp-now-router (firmware):

  • Debug.h — ArduinoLibs Debug lib + -D DEBUG_LEVEL=1 (errors + major default); Serial.* → DEBUG_*/DEBUG1_* — a8c8895.
  • Default SSID — fallback SoftAP esp-now-router / meshrouter so OTA is always reachable — 65c8a25.
  • Endpoints + server in main.cpp — /info (config JSON), /routes (dedup table + neighbor routers), /debug (leadership, counters, uptime, heap); ota.cpp just 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.h and added tests/election_test.cpp (boot hold-down, promotion+term, N=1, step-down, best-heard, stale-term, failover) alongside the relay tests; tests/Makefile runs both suites + a gcov coverage target. Router tests the parts it uses; WLED tests the edge dispatch — c2e7e03 (+ relay-test relocation in 6cf9982).

Both firmwares build; all host suites pass. Ready for another look.

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_RELAYED doc 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_better references; the remaining host tests (sensor_sync_test.cpp, sensor_sync_ring_test.cpp) compile clean under -Wall -Wextra and pass at head.
  • The move landed on the other side — esp-now-router now carries BACKBONE_ROUTER.md plus tests/relay_test.cpp + election_test.cpp using the new names, and its make test passes 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.md line 7 still says ss_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.

Comment thread usermods/ampworks/SENSOR_SYNC.md Resolved

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — the ttl/flags header note now explains the field layout and why the version stays 5, instead of narrating the change; the beacon msgType, 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, the useEspNow config 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. The setup() "TODO (OTA subtask)" comment was stale — it described work that otaWifiBegin() 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/term handling.

Sweep is clean, both firmwares build, and all host suites pass (make test at the super-repo root).

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.md link + deleted-test pointer) — removed in c43c024.
  • Two continuation-alignment nits in sensor_sync_protocol.h — fixed in c43c024, then mooted by f4f70db (those functions no longer live in this header).
  • Cross-repo nit: esp-now-router/README.md now says ss_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/flags occupy the former reserved u16 at the same offsets (header still 20 B, version still 5); ss_parse_header/ss_is_our_frame do gate on version==5, and ss_parse_header's msgType check does reject SENSOR_SYNC_MSG_BEACON at the edge.
  • The edge sender stamps ttl=SS_DEFAULT_TTL (usermod_sensor_sync.cpp:129); the router injects the default on ttl==0 (esp-now-router/src/router_relay.h:51) and sets SS_FLAG_RELAYED purely 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 in esp-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 -Wextra and pass at head.

New issues:

  • INLINE below — SENSOR_SYNC.md still says the ss_router_* helpers are "in this header"; f4f70db moved them out, so the paragraph now contradicts both the code and sensor_sync_protocol.h's own banner.
  • Two milestone stragglers survived the sweep, both OUTSIDE this PR's diff hunks (so no inline suggestion is 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 via useEspNow). Both describe as future what the code already has — the exact class this cleanup targeted; two one-line touch-ups would finish it.

Comment thread usermods/ampworks/SENSOR_SYNC.md Resolved

@aphelps aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please merge

… 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.
@aphelps-claude-bot
aphelps-claude-bot merged commit 5fe5f5e into base-wled-16.0.1 Jul 29, 2026
6 checks passed
@aphelps-claude-bot
aphelps-claude-bot deleted the m3b-backbone-router branch July 29, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants