Repository navigation
rs485_bridge: the payload ceiling is 57, not 64 — derive it instead of writing it down - #16
Conversation
…f writing it down
RS485_RECV_BUFFER (64) is the budget for the whole FRAME, socket header included.
RS485B_TX_SLOT_LEN was hardcoded to the same number but means a PAYLOAD, so a full slot
reached the wire as 64 + 7 = 71 bytes and overflowed every peer on the default buffer --
including this bridge's own receive path. RS485_non_blocking discards the frame and
bumps only an internal counter nobody read, so a WiFi client could submit 64 bytes,
watch rs485Tx increment, and see it vanish with nothing recorded anywhere.
The real ceiling is 57. Nothing in the v1 command set came close -- the largest frame
the bridge emits is a 23-byte poll response -- but the UDP ingress path accepted up to
the slot length, so it was reachable from a client, and the symptom ("tx increments,
module saw nothing") is identical to the socket-header bug fixed in #14. Left alone it
would have sent the next person at the bench after settled code.
Rather than change 64 to 57, the slot is now DERIVED:
RS485B_TX_SLOT_LEN = RS485B_RECV_BUFFER_LEN - RS485B_SOCKET_HDR_LEN
which makes this class of error structurally impossible rather than fixed once. That
matters here specifically: this is the third defect in this subsystem whose root cause
was a constant describing one thing compared against a constant describing another
(payload vs total frame here, AVR vs 32-bit struct size in the header, poll-request vs
poll-response in the ACK handling).
The old guard was `RS485B_TX_SLOT_LEN <= RS485_RECV_BUFFER`, which passed at 64 <= 64
while the property it claimed was false. It now includes the header. It is true by
construction and therefore cannot fail -- kept only to stop the slot being hardcoded
again, and the comment says so instead of pretending it is coverage. The ceiling is
pinned where it CAN fail: an absolute 57/58 pair in the ingress test here (every other
check in that group uses the constant symbolically, which follows the number wherever
it goes and pins nothing), and a 57-delivered / 58-dropped boundary group in
ArduinoLibs' receive-path suite.
UDP_BUF_LEN is now RS485B_RECV_BUFFER_LEN, deliberately one header LARGER than a slot.
Sized to the slot, a 58..64-byte payload would be truncated by udp.read() and miscounted
as RS485B_ERR_SHORT; read whole, it is refused as RS485B_ERR_OVERSIZE, which is the
countable rejection worth having. Costs 7 bytes of stack.
Also surfaces the framing counter from the paired ArduinoLibs change in the info row
(with buf3 grown 80 -> 128, since five counters plus the literal already ran close
enough that snprintf_P would have truncated silently), corrects the rate-limit
arithmetic in three places (a max frame is 64 B total -> ~132 wire bytes -> ~47 ms, not
"a 64-byte payload -> 135 -> 48 ms"), and fixes the readme's ingress-cap bullet, which
stated the cap as the buffer size.
Verified: host suite passes natively and under -fpack-struct=1; `pio run -e ampworks`
and `-e usermods` both SUCCESS with libdeps cleared; and hardcoding the slot back to 64
FAILS the build on the new assert -- confirmed, because a guard that cannot fail is not
a guard.
Needs the paired aphelps/ArduinoLibs change for getFramingErrorCount().
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ
Five findings, and the first is a false statement I put in a comment. The assert tying RS485B_RECV_BUFFER_LEN to the real RS485_RECV_BUFFER said that tools/rs485_bridge_probe.py --self-test cross-checks it too. It does not -- the probe pins the HMTL header and struct sizes and has never looked at either buffer constant. That check is queued for the parent pointer bump, so the claim was true of the plan and false of the code. The consequence is the same shape as the bug being fixed. That assert compiles only under [env:ampworks], which no CI workflow builds, so the mirror has NO automated guard at all right now: raise RS485_RECV_BUFFER in ArduinoLibs and the slot stays at 57 with CI green and the bus quietly under-used; lower it and the 71-vs-64 silent drop comes straight back. The comment now says that plainly instead of pointing at a check that isn't there. Added the guard that CAN fail, in-class because UDP_BUF_LEN is private: static_assert(UDP_BUF_LEN >= RS485B_TX_SLOT_LEN + RS485B_SOCKET_HDR_LEN) Reverting UDP_BUF_LEN to the slot length is an obvious-looking simplification that would silently make RS485B_ERR_OVERSIZE unreachable -- a 58..64-byte payload would be truncated by udp.read() and miscounted as short, which is precisely the behaviour this change exists to remove. Verified it fails the build when reverted. Two comments were left contradicting the change: the one directly above buf[UDP_BUF_LEN] still said truncation was intentional "because nothing bigger than a transmit slot can be forwarded", and the maxPayload doc said initBuffer() "handed back" the size. initBuffer() returns an offset POINTER and enforces no size -- its data_size argument only feeds a debug print -- so the bound actually lives in rawTxBuf's own dimension. A reader following that comment would have gone looking in the wrong place. Also: the poll response advertises recv_buffer_size 64, which is the whole-frame budget and not the sendable payload, so a master that treats it as a payload size and sends 64 gets ERR_OVERSIZE from the node that advertised it. That conflation is in the HMTL wire format, so the value stays -- but it is now labelled in both the code and the readme's message table rather than left as a trap. The readme's info-page section also still described a four-bucket dropped row; it has six, and the two new ones needed the caveats spelled out. Two host tests added, both in CI's reach where the ampworks assert is not: the slot/header/budget relationship checked against a real packed struct rather than the mirrored literal, and a 64-byte datagram carrying a valid 40-byte frame plus trailing junk -- confirming "read more than a slot" is not mistaken for "forward more than a slot", which is a path that only exists now that UDP_BUF_LEN is 64. Reconciled the frame-size figures while here: 23 bytes is the largest frame the bridge emits, 43 the largest it can relay (MSG_MAX_SZ), and the "35-byte PROGRAM" in earlier notes was sizeof(msg_program_t) without its header. Verified: host suite green on both ABIs, ampworks and usermods both build, and both new asserts fail the build when their invariant is broken. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ
Self-review: I claimed a guard that doesn't exist yet — fixed in b83747d / ArduinoLibs f9bb2cfFive findings. Leading with the one that was a false statement of mine, because it's the same shape as the bug this PR fixes. The mirror has no automated guard, and I said it didThe assert tying The consequence is concrete: that assert compiles only under Added the guard that can failstatic_assert(UDP_BUF_LEN >= RS485B_TX_SLOT_LEN + RS485B_SOCKET_HDR_LEN, ...)In-class, since The framing counter wrapped, presenting as "healthy"
Two doc corrections on the same function: "since boot" was wrong ( Two comments left contradicting the changeThe one directly above Also
Verified: host suite green on both ABIs, |
Paired with aphelps/ArduinoLibs#12 (for
getFramingErrorCount()). Merge that one first.The defect
RS485_RECV_BUFFER(64) is the budget for the whole frame, socket header included.RS485B_TX_SLOT_LENwas hardcoded to the same number but means a payload — so a full slot reached the wire as64 + 7 = 71bytes and overflowed every peer on the default buffer, including this bridge's own receive path.RS485_non_blockingdiscards the frame and bumps only an internal counter nobody read, so a WiFi client could submit 64 bytes, watchrs485Txincrement, and see it vanish with nothing recorded anywhere.The real ceiling is 57. Nothing in the v1 command set came close — the largest frame the bridge emits is a 23-byte poll response — but the UDP ingress path accepted up to the slot length, so it was reachable from a client. And the symptom, "tx increments, module saw nothing", is identical to the socket-header bug fixed in #14. Left alone it would have sent the next person at the bench chasing settled code.
Derived, not corrected
Rather than change 64 to 57:
This makes the class of error structurally impossible rather than fixed once — which matters here specifically, because it's the third defect in this subsystem whose root cause was a constant describing one thing compared against a constant describing another:
On the guard that can't fail
The old assert was
RS485B_TX_SLOT_LEN <= RS485_RECV_BUFFER— it passed at64 <= 64while the property it claimed was false. It now includes the header, is true by construction, and therefore cannot fail. It's kept only to stop the slot being hardcoded again, and the comment says that rather than pretending it's coverage.The ceiling is pinned where it can fail: an absolute 57/58 pair in the ingress test here — every other check in that group uses the constant symbolically, which follows the number wherever it goes and pins nothing — plus a 57-delivered / 58-dropped boundary group in the paired ArduinoLibs suite.
UDP_BUF_LENis now the buffer size, not the slotDeliberately one header larger than a slot. Sized to the slot, a 58..64-byte payload would be truncated by
udp.read()and miscounted asRS485B_ERR_SHORT; read whole, it's refused asRS485B_ERR_OVERSIZE— the countable rejection worth having. Costs 7 bytes of stack.Also
RS485 droppedinfo row, withbuf3grown 80 → 128 (five counters plus the literal already ran close enough thatsnprintf_Pwould have truncated silently)Verification
-fpack-struct=1pio run -e ampworksand-e usermodsboth SUCCESS,libdepscleared first🤖 Generated with Claude Code
https://claude.ai/code/session_013HqzdRdTRnreSvevaQGQSJ