feat(micromate): build_request carries a data section -- reproduces all 251 THOR request frames
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
2a6acae259
commit
151ba1546f
2 files changed
+111
-2
No files matched your search
+20
-2
@@ -126,7 +126,7 @@ def checksum(payload: bytes) -> int:
|
||||
|
||||
# ── Request builder ───────────────────────────────────────────────────────────
|
||||
|
||||
def build_request(sub: int, offset: int = 0, params: bytes = bytes(10)) -> bytes:
|
||||
def build_request(sub: int, offset: int = 0, params: bytes = bytes(10), data: bytes = b"") -> bytes:
|
||||
"""Build a host→unit command frame.
|
||||
|
||||
⚠ Do **not** substitute `minimateplus.framing.build_bw_frame()` here, even
|
||||
@@ -150,6 +150,9 @@ def build_request(sub: int, offset: int = 0, params: bytes = bytes(10)) -> bytes
|
||||
Thor asks for `0xFFFF` and gets the whole block — so this is
|
||||
usually `0xFFFF`, not Series III's probe-then-data pair.
|
||||
params: exactly 10 bytes at payload[6:16].
|
||||
data: the write section after params (empty for every read and for
|
||||
write confirms). Escaped and checksummed exactly like the rest
|
||||
of the payload -- reproduces all 33 data-carrying THOR frames.
|
||||
|
||||
A `0x10` inside `params` is fine and needs no special handling: Thor sends
|
||||
`SUB 0x5A` with `params = 00 00 10 00 …` and the wire carries `10 10`.
|
||||
@@ -162,11 +165,26 @@ def build_request(sub: int, offset: int = 0, params: bytes = bytes(10)) -> bytes
|
||||
if not 0 <= sub <= 0xFF:
|
||||
raise ValueError(f"sub must be a single byte, got {sub:#x}")
|
||||
|
||||
payload = bytes([MM_CMD, 0x00, sub, 0x00, (offset >> 8) & 0xFF, offset & 0xFF]) + params
|
||||
payload = (bytes([MM_CMD, 0x00, sub, 0x00, (offset >> 8) & 0xFF, offset & 0xFF])
|
||||
+ params + bytes(data))
|
||||
body = payload + bytes([checksum(payload)])
|
||||
return bytes([ACK, STX]) + stuff(body) + bytes([ETX])
|
||||
|
||||
|
||||
def request_sub(frame: bytes) -> int:
|
||||
"""The SUB of a built host->unit frame (`41 02 <stuffed body> 03`).
|
||||
|
||||
Used by the protocol's send-side opcode gate, which must see exactly what is
|
||||
about to go on the wire rather than trust the caller's word for it.
|
||||
"""
|
||||
if len(frame) < 5 or frame[0] != ACK or frame[1] != STX or frame[-1] != ETX:
|
||||
raise ValueError(f"not a host->unit request frame: {bytes(frame[:8]).hex()}")
|
||||
body = unstuff(frame[2:-1])
|
||||
if len(body) < 17 or body[0] != MM_CMD:
|
||||
raise ValueError(f"not a host->unit request frame: {bytes(frame[:8]).hex()}")
|
||||
return body[2]
|
||||
|
||||
|
||||
# ── Response frame ────────────────────────────────────────────────────────────
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -411,3 +411,94 @@ def test_builder_reproduces_every_captured_read_frame():
|
||||
checked += 1
|
||||
i = j + 1
|
||||
assert checked == 218, f"expected 218 read frames, checked {checked}"
|
||||
|
||||
# ── the data section (writes) ─────────────────────────────────────────────────
|
||||
|
||||
from micromate.framing import request_sub, unstuff # noqa: E402
|
||||
|
||||
|
||||
def _destuffed_payload(frame: bytes) -> bytes:
|
||||
body = unstuff(frame[2:-1])
|
||||
return body[:-1] # drop the checksum byte
|
||||
|
||||
|
||||
def test_data_goes_after_the_params_inside_the_checksum():
|
||||
data = bytes(range(0x20, 0x40))
|
||||
f = build_request(0x7E, 0x007E, bytes(10), data)
|
||||
payload = _destuffed_payload(f)
|
||||
assert payload[:6] == bytes([0x10, 0x00, 0x7E, 0x00, 0x00, 0x7E])
|
||||
assert payload[6:16] == bytes(10) and payload[16:] == data
|
||||
assert unstuff(f[2:-1])[-1] == sum(payload) & 0xFF
|
||||
|
||||
|
||||
@pytest.mark.parametrize("b", [0x02, 0x03, 0x04, 0x10])
|
||||
def test_every_escape_byte_in_data_is_escaped_on_the_wire(b):
|
||||
f = build_request(0x71, 0x082C, bytes(10), bytes([0x55, b, 0x55]))
|
||||
assert bytes([0x55, DLE, b, 0x55]) in f
|
||||
assert _destuffed_payload(f)[16:] == bytes([0x55, b, 0x55])
|
||||
|
||||
|
||||
def test_a_literal_0x10_in_data_round_trips():
|
||||
# Never captured in write data; params already prove `10 10` (SUB 0x5A).
|
||||
data = b"\x10\x10\x00\x10"
|
||||
assert _destuffed_payload(build_request(0x71, 0x082C, bytes(10), data))[16:] == data
|
||||
|
||||
|
||||
def test_a_checksum_that_lands_on_an_escape_byte_is_escaped():
|
||||
# Pick the last data byte so SUM8 of the payload is exactly 0x03.
|
||||
head = bytes([0x10, 0x00, 0x7E, 0x00, 0x00, 0x7E]) + bytes(10) + b"\x01\x02"
|
||||
last = (0x03 - sum(head)) & 0xFF
|
||||
f = build_request(0x7E, 0x007E, bytes(10), b"\x01\x02" + bytes([last]))
|
||||
assert f[-3:] == bytes([DLE, 0x03, ETX])
|
||||
|
||||
|
||||
def test_without_data_the_frame_is_unchanged():
|
||||
assert build_request(0x5B, 0x0030) == build_request(0x5B, 0x0030, bytes(10), b"")
|
||||
|
||||
|
||||
def test_request_sub_reads_the_sub_back_even_when_stuffed():
|
||||
assert request_sub(build_request(0x96, 0xFFFF)) == 0x96
|
||||
assert request_sub(build_request(0x10, 0x0000)) == 0x10 # SUB itself escaped
|
||||
assert request_sub(build_request(0x71, 0x082C, bytes(10), b"\x03" * 5)) == 0x71
|
||||
|
||||
|
||||
@pytest.mark.parametrize("junk", [b"", b"\x41\x02\x03", b"\x02\x10\x10\x00\x96\x03", b"hello"])
|
||||
def test_request_sub_refuses_anything_that_is_not_a_request_frame(junk):
|
||||
with pytest.raises(ValueError):
|
||||
request_sub(junk)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not _CAPTURES.is_dir(),
|
||||
reason="capture directory is gitignored; present only on a dev box",
|
||||
)
|
||||
def test_builder_reproduces_every_captured_request_frame_including_writes():
|
||||
"""251/251: the 218 reads plus the 33 data-carrying writes."""
|
||||
checked = 0
|
||||
for path in sorted(_CAPTURES.rglob("raw_bw_*.bin")):
|
||||
blob = path.read_bytes()
|
||||
i = 0
|
||||
while i < len(blob):
|
||||
if not (blob[i] == ACK and i + 1 < len(blob) and blob[i + 1] == STX):
|
||||
i += 1
|
||||
continue
|
||||
j = i + 2
|
||||
body = bytearray()
|
||||
while j < len(blob):
|
||||
if blob[j] == DLE and j + 1 < len(blob):
|
||||
body.append(blob[j + 1])
|
||||
j += 2
|
||||
continue
|
||||
if blob[j] == ETX:
|
||||
break
|
||||
body.append(blob[j])
|
||||
j += 1
|
||||
payload = bytes(body[:-1])
|
||||
sub = payload[2]
|
||||
offset = (payload[4] << 8) | payload[5]
|
||||
assert build_request(sub, offset, payload[6:16], payload[16:]) == blob[i:j + 1], (
|
||||
f"{path.name} @0x{i:04x} SUB=0x{sub:02x}"
|
||||
)
|
||||
checked += 1
|
||||
i = j + 1
|
||||
assert checked == 251, f"expected 251 request frames, checked {checked}"
|
||||
Reference in new issue
Block a user