Skip to content

add the RS485 bench tooling: a UDP probe and an HMTL_Command_CLI driver - #26

Merged
aphelps-claude-bot merged 2 commits into
mainfrom
rs485-bench-tooling
Jul 30, 2026
Merged

aphelps-claude-bot merged 2 commits into
mainfrom
rs485-bench-tooling

Conversation

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator

Preparation for the hardware bring-up session — the Claude-runnable half of that task, which needs no bench access. The bridge has never run on silicon; the point of these two scripts is that a failure comes out as "the bridge relayed a POLL response with MSG_FLAG_ERROR and address 0" rather than "it didn't work", and that the observation lands in a transcript instead of someone's memory.

Both blockers in front of that session are now merged (socket-header packing #24, receive-path fixes #25), so this is the remaining prep.

tools/rs485_bridge_probe.py

Builds correctly-framed HMTL messages (VALUE, RGB, POLL, SET_ADDRESS, TIMESYNC), sends them to the bridge's UDP port, pretty-prints anything relayed back, and decodes a hex dump from a serial log.

Two constraints are baked in because both are easy to get wrong and neither fails loudly:

  • One socket, never rebound. The bridge relays to the source address and ephemeral source port of the last datagram it received. A separate listener — or one bound to 21331, which is the intuitive choice — never sees a reply. It also no-ops until it has heard from someone, so relays cannot be observed passively.
  • --await-reply explains itself when a POLL times out, because that is expected rather than broken: a module's poll response comes back addressed to the bridge with MSG_FLAG_ACK set, and rs485b_decide() has no ACK guard, so the bridge answers it instead of relaying it. Without that message the runbook would send the tester after a phantom framing bug.

--self-test is a real drift guard

The wire layout is stated explicitly in Python rather than parsed out of the C headers — HMTLWireFormat.h includes Socket.h from a different submodule, config_hdr_t is chosen by an #if, and msg_value_t is a bitfield no offset table can express. So the table is cross-checked against assertions the firmware already carries:

source what is checked
rs485_bridge_protocol.h static_asserts 10 struct sizes + HMTL_MSG_POLL_MIN_LEN
host test offsetof checks 17 field offsets
host test pointer-arithmetic checks msg_hdr_t's offsets — a scraper looking only for offsetof finds nothing there and would wrongly report the header unguarded
HMTLWireFormat.h 4 constants

Every scrape raises if its regex matches nothing, rather than passing vacuously. A renamed field would otherwise switch the guard off silently — which is exactly the failure it exists to catch. Verified with three negative controls:

  1. wrong value in the Python table → caught
  2. mutated firmware assertion → caught
  3. regex matching nothing → raises instead of reporting OK

The decoder also knows the two values that look like bugs and are not: baud 23 decodes to 27600 rather than 28000 (the field is in units of 1200), and object_type 0x0057 is 'W', marking a WLED bridge rather than a real HMTL module.

tools/hmtl_cli.py

Drives HMTL_Command_CLI over USB serial at 57600 — not the 115200 a WLED board uses, and getting that wrong yields plausible garbage rather than an obvious failure. Sends one command (p/s/t <addr>) and captures output for N seconds, holding the port only for the invocation so pio device monitor still works between steps. pyserial is imported lazily so --help and --list work without it.

That sketch is the recommended bus partner for a reason worth recording: it prints every frame it receives with source, destination, flags and a hex dump, and decodes poll responses without filtering on address. A stock HMTL_Module drops anything not addressed to it, so you cannot distinguish "the bridge sent nothing" from "my module discarded it" — which matters right now, because the bridge stamps its own address in a poll response rather than the requester's.

Verification

python3 -m py_compile on both; --self-test passes and fails correctly under all three negative controls; encode→decode round-trips for all five message types; and a synthetic 23-byte poll response decodes to exactly the field values step 9 of the runbook tells the tester to expect.

No firmware change, no submodule change — tools/ only.

🤖 Generated with Claude Code

https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ

aphelps-claude-bot and others added 2 commits July 29, 2026 20:01
Preparation for the hardware bring-up session. The bridge has never run on silicon,
and the point of these two scripts is that a bench failure comes out as "the bridge
relayed a POLL response with MSG_FLAG_ERROR and address 0" rather than "it didn't
work" -- and that the observation lands in a transcript instead of someone's memory.

tools/rs485_bridge_probe.py builds correctly-framed HMTL messages (VALUE, RGB, POLL,
SET_ADDRESS, TIMESYNC), sends them to the bridge's UDP port, and pretty-prints
anything relayed back.

Two constraints are baked in because both are easy to get wrong and neither fails
loudly:

  * It sends and receives on ONE socket and never rebinds. The bridge relays to the
    source address AND EPHEMERAL SOURCE PORT of the last datagram it received, so a
    separate listener -- or one bound to 21331, which is the intuitive choice -- never
    sees a reply. It also no-ops until it has heard from someone, so relays cannot be
    observed passively.

  * --await-reply explains itself when a POLL times out, because that is expected
    rather than broken: a module's poll response comes back addressed to the bridge
    with MSG_FLAG_ACK set, and rs485b_decide() has no ACK guard, so the bridge answers
    it instead of relaying it. Without that message the runbook would send the tester
    after a phantom framing bug.

The wire layout is stated explicitly in Python rather than parsed out of the C
headers -- HMTLWireFormat.h includes Socket.h from a different submodule, config_hdr_t
is chosen by an #if, and msg_value_t is a bitfield no offset table can express. So
--self-test cross-checks the table against the assertions the firmware already
carries: 10 struct sizes and HMTL_MSG_POLL_MIN_LEN from rs485_bridge_protocol.h's
static_asserts, 17 field offsets from the host test's offsetof checks, msg_hdr_t's
offsets from the same file's POINTER-ARITHMETIC checks (a scraper looking only for
offsetof finds nothing there and would wrongly call the header unguarded), and four
constants from HMTLWireFormat.h.

Every scrape raises if its regex matches nothing, rather than passing vacuously -- a
renamed field would otherwise switch the drift guard off silently, which is exactly
the failure it exists to catch. Verified with three negative controls: a wrong value
in the Python table is caught, a mutated firmware assertion is caught, and a regex
that matches nothing raises.

The decoder also knows the two values that look like bugs and are not: baud 23 decodes
to 27600 rather than 28000 (the field is in units of 1200), and object_type 0x0057 is
'W', marking a WLED bridge rather than a real HMTL module.

tools/hmtl_cli.py drives HMTL_Command_CLI over USB serial at 57600 -- not the 115200 a
WLED board uses, and getting that wrong yields plausible garbage rather than an
obvious failure. It sends one command (p/s/t <addr>) and captures output for N
seconds, holding the port only for the invocation so `pio device monitor` still works
between steps. pyserial is imported lazily so --help and --list work without it.

That sketch is the recommended bus partner for a reason worth recording: it prints
every frame it receives with source, destination, flags and a hex dump, and decodes
poll responses WITHOUT filtering on address. A stock HMTL_Module drops anything not
addressed to it, so you cannot distinguish "the bridge sent nothing" from "my module
discarded it" -- which matters right now, because the bridge stamps its own address in
a poll response rather than the requester's.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ
…d had holes

Seven findings. The first invalidates a claim the runbook was built on.

HMTL_Command_CLI IS NOT PROMISCUOUS. I documented it as printing "every frame it
receives", and steps 5 and 6 of the runbook told the tester to watch it while the
probe drove a *third* node. It does not work that way: the sketch calls
hmtl_socket_getmsg(&rs485, &msglen, config.address), which filters on the SOCKET-layer
destination (RS485Socket::getMsg -> SOCKET_ADDRESS_MATCH, RS485Utils.cpp:365), so a
frame addressed to another node never reaches process_message() and nothing prints.

Concretely: probe an RGB command at module 3 while listening on a CLI at address 5,
and the bridge's tx counter climbs while the CLI stays silent -- which reads as "the
bridge never transmitted" off a perfectly working bridge. That is a wasted bench
session chasing a bug that isn't there.

What the sketch actually buys is narrower and still worth having: it does not filter
on the HMTL HEADER address, so it will show the bridge's poll response, which a stock
module discards (MessageHandler.cpp:78-79, no else-branch). Docstring, failure advice
and runbook steps 5/6/rig now say so, and every step that expects the CLI to observe
traffic addresses it to the CLI's own address or to broadcast 0xFFFF.

THE DRIFT GUARD HAD HOLES. --self-test raised only when a whole regex matched nothing;
per-entry lookups then used `if got is not None`, so a single deleted or renamed
assertion was skipped in silence while the summary still claimed full coverage.
Verified: removing just the offsetof(output_hdr_t, output) check used to leave it
green at "17 field offsets". Now every table entry must be confirmed, and the summary
reports what was actually confirmed -- that case fails with "16/17" and a named
problem.

Also added the one check that was conspicuously missing: msg_value_t's 13/3 bitfield,
the single field the module docstring calls out as unexpressible in an offset table
and hand-encodes. Item 6 only round-tripped Python against Python, so if the
declaration order ever flipped to `flags : 3; value : 13` the probe would put
brightness in the top three bits, every VALUE command would land as ~0, and the
self-test would stay green. It now scrapes the byte layout the host test pins and
compares the emitted bytes against it.

DROPPED THE TIMESYNC EMITTER. Two independent reasons. A bare TIMESYNC header is not a
legal frame -- TimeSync::synchronize() casts past the header to msg_time_sync_t and
switches on sync_phase with no length check, so an 8-byte frame makes the receiver
read a stale byte and, on an unlucky value, emit unsolicited traffic and mutate its
clock delta. And it could not have tested what I added it for: a frame sent over UDP
never reaches rs485b_decide(), so the relay path is only reachable from the RS485
side. Step 8 already used hmtl_cli for this; the runbook now says why.

Smaller ones: --value refuses out-of-range input instead of silently masking 9000 to
808 (which would look exactly like an applyValue mapping bug and misdirect step 7);
--rgb validates channels instead of dying in bytes(); hmtl_cli waits 2 s for the DTR
auto-reset rather than 0.2 s, since the bootloader was eating the command and the
resulting empty capture pointed at baud and wiring; and --list no longer hard-exits
without pyserial in a way that contradicted its own docstring.

Verified: self-test passes and fails correctly under five negative controls -- wrong
table value, mutated firmware assertion, blind regex, a single removed assertion, and
a flipped bitfield layout.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ
@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator Author

Self-review found the tooling would have wasted a bench session — fixed in 4e554a3

The first finding invalidates a claim the runbook was built on, so it's worth stating plainly.

HMTL_Command_CLI is not a bus sniffer

I documented it as printing "every frame it receives", and steps 5 and 6 of the runbook told the tester to watch it while the probe drove a third node. It does not work that way: the sketch calls hmtl_socket_getmsg(&rs485, &msglen, config.address), which filters on the socket-layer destination (RS485Socket::getMsg → SOCKET_ADDRESS_MATCH, RS485Utils.cpp:365), so a frame addressed elsewhere never reaches process_message() and nothing prints.

Concretely: probe an RGB command at module 3 while listening on a CLI at address 5, and the bridge's tx counter climbs while the CLI stays silent — which reads as "the bridge never transmitted" off a perfectly working bridge. A whole bench session chasing a bug that isn't there, which is the precise failure mode this tooling exists to prevent.

What the sketch actually buys is narrower and still worth having: it doesn't filter on the HMTL header address, so it shows the bridge's poll response, which a stock module discards (MessageHandler.cpp:78-79, no else-branch). Docstring, failure advice, and runbook rig/step 5/step 6 now say so — and every step that expects the CLI to observe traffic addresses it to the CLI's own address or to broadcast 0xFFFF.

The drift guard had holes

--self-test raised only when a whole regex matched nothing; per-entry lookups then used if got is not None, so a single deleted or renamed assertion was skipped in silence while the summary still claimed full coverage. Removing just the offsetof(output_hdr_t, output) check left it green at "17 field offsets". Now every table entry must be confirmed and the summary reports what actually was — that case now fails with 16/17 and a named problem.

Added the check that was conspicuously missing: msg_value_t's 13/3 bitfield — the one field the docstring calls unexpressible in an offset table and hand-encodes. The old round-trip only compared Python against Python, so if the declaration order ever flipped to flags : 3; value : 13, the probe would put brightness in the top three bits, every VALUE command would land as ~0, and the self-test would stay green.

Dropped the TIMESYNC emitter

Two independent reasons. A bare TIMESYNC header is not a legal frame — TimeSync::synchronize() casts past the header to msg_time_sync_t and switches on sync_phase with no length check, so an 8-byte frame makes the receiver read a stale byte and, on an unlucky value, emit unsolicited traffic and mutate its clock delta. A bench tool must not make the bus partner do something nondeterministic.

And it couldn't have tested what I added it for: a frame sent over UDP never reaches rs485b_decide() — serviceUdp() validates and puts it straight on the wire — so the relay path is only reachable from the RS485 side. Step 8 already used hmtl_cli for this; the runbook now says why.

Smaller

  • --value refuses out-of-range input instead of masking 9000 → 808, which would look exactly like an applyValue mapping bug and misdirect step 7
  • --rgb validates channels instead of dying in bytes()
  • hmtl_cli waits 2 s for the DTR auto-reset rather than 0.2 s — the bootloader was eating the command, and the resulting empty capture pointed the tester at baud and wiring
  • --list no longer hard-exits without pyserial in a way that contradicted its own docstring

Verification

Self-test passes, and fails correctly under five negative controls: wrong table value, mutated firmware assertion, blind regex, a single removed assertion, and a flipped bitfield layout.

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.

1 participant