fix(minimateplus): MiniMate requests escape 02/03/04/10, and the 5A walk is 32-bit linear -- the 64 KB page bug
A recording of Blastware downloading all 8 of BE9441's events (three crossing a 64 KB page; tests/fixtures/minimate-bench-10-26-26-blastware/) settles it: every one of its 748 requests is the standard 16-byte payload with a SUM8 checksum and every 02 / 03 / 04 / 10 byte DLE-escaped -- 748/748 re-encode byte-identically. A 5A request is not a special frame: it is that payload with a 2-byte LENGTH (0x0200 per chunk, the remainder for TERM) and a 4-byte linear ADDRESS. The walk is the event's start, +0x200 at a time carrying into the page byte, then TERM at the next boundary for exactly the remainder. What the old model misread: the fixed 'offset 0x1002' is length 0x0200 escaped (10 02 00); the first event's 'metadata pages at 0x1002/0x1004' are its chunks at 0x0200 and 0x0400; the 16-bit counter could not leave the key's page. - build_bw_frame escapes the four values (was 0x10 only); new s3_stuff(). - build_5a_request(address, length); parse_strt_end_key() -> the full 32 bits. - read_bulk_waveform_stream: one linear walk, no event-1 special case; strict mode keeps every refusal; an end at or before the start is refused as a corrupt STRT (a page crossing is no longer refused -- it downloads). Tests: all 540 Blastware 5A requests and all 208 others rebuilt byte for byte; SFM's walks for events 0, 1, 3 and 7 send exactly Blastware's requests and, fed Blastware's recorded replies, build whole events. In-page requests are byte-identical to before. No codec, store or DB change. 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
465bc704dd
commit
887e75d1ba
7 files changed
+339
-196
No files matched your search
+67
-2
@@ -28,6 +28,10 @@ De-stuffed payload layout:
|
||||
|
||||
DLE stuffing rule: any 0x10 byte in the payload is doubled on the wire (0x10 → 0x10 0x10).
|
||||
This applies to the checksum byte too.
|
||||
⚠ AMENDED 2026-10-26 (measured): a request escapes FOUR values, not one -- every 02, 03,
|
||||
04 and 10 in payload and checksum gets a DLE in front. Blastware's 748 recorded requests
|
||||
re-encode byte-identically under that rule (tests/test_5a_blastware_walk.py); a bare 02 or
|
||||
03 left a request unanswered. See `s3_stuff()`.
|
||||
|
||||
Confirmed from live captures (s3_parser.py validation + raw_bw.bin / raw_s3.bin).
|
||||
"""
|
||||
@@ -83,6 +87,24 @@ def dle_unstuff(data: bytes) -> bytes:
|
||||
|
||||
# ── Checksum ─────────────────────────────────────────────────────────────────
|
||||
|
||||
# Every byte value a request DLE-escapes (2026-10-26, measured): Blastware's 748
|
||||
# requests from a whole download session re-encode byte-identically with this set,
|
||||
# payload and checksum alike. A bare 0x02 checksum or a 0x03 inside the params left
|
||||
# BE9441 silent (and the 0x02 made it reset ~11 s later). It is a Micromate's set
|
||||
# too (micromate.framing._ESCAPED).
|
||||
_ESCAPED = frozenset({STX, ETX, 0x04, DLE})
|
||||
|
||||
|
||||
def s3_stuff(data: bytes) -> bytes:
|
||||
"""Escape every byte a request escapes: `XX` → `10 XX` for 02, 03, 04 and 10."""
|
||||
out = bytearray()
|
||||
for b in data:
|
||||
if b in _ESCAPED:
|
||||
out.append(DLE)
|
||||
out.append(b)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def checksum(payload: bytes) -> int:
|
||||
"""SUM8: sum of all de-stuffed payload bytes, mod 256."""
|
||||
return sum(payload) & 0xFF
|
||||
@@ -95,6 +117,30 @@ def checksum(payload: bytes) -> int:
|
||||
SUB_5A = 0x5A
|
||||
|
||||
|
||||
_BULK_CHUNK = 0x0200 # bytes per 5A read; a TERM reads the remainder
|
||||
|
||||
|
||||
def build_5a_request(address: int, length: int) -> bytes:
|
||||
"""A SUB 5A read of `length` bytes at the unit's 32-bit `address`.
|
||||
|
||||
Measured 2026-10-26 from Blastware downloading all 8 of BE9441's events (three
|
||||
crossing a 64 KB page): a 5A request is an ORDINARY 16-byte request --
|
||||
`[10 00 5A 00][length BE][address BE 4][zeros 6]`, SUM8 checksum, every
|
||||
02/03/04/10 escaped -- and this rebuilds all 540 of Blastware's 5A requests
|
||||
byte for byte. The older model below (`build_5a_frame`: a raw "offset_hi
|
||||
0x10", an "11-byte params", "metadata pages at 0x1002 / 0x1004") was the
|
||||
same bytes misread: `10 02 00` is the length 0x0200 escaped, and the
|
||||
"metadata pages" are the first event's chunks at 0x0200 and 0x0400.
|
||||
"""
|
||||
if not 0 <= address <= 0xFFFFFFFF:
|
||||
raise ValueError(f"address must fit in 32 bits, got {address:#x}")
|
||||
if not 0 <= length <= 0xFFFF:
|
||||
raise ValueError(f"length must fit in 16 bits, got {length:#x}")
|
||||
payload = (bytes([BW_CMD, 0x00, SUB_5A, 0x00, (length >> 8) & 0xFF, length & 0xFF])
|
||||
+ address.to_bytes(4, "big") + bytes(6))
|
||||
return bytes([ACK, STX]) + s3_stuff(payload + bytes([checksum(payload)])) + bytes([ETX])
|
||||
|
||||
|
||||
def build_5a_frame(offset_word: int, raw_params: bytes) -> bytes:
|
||||
"""
|
||||
Build a BW→S3 frame for SUB 5A (BULK_WAVEFORM_STREAM) that exactly
|
||||
@@ -210,7 +256,7 @@ def build_bw_frame(sub: int, offset: int = 0, params: bytes = bytes(10)) -> byte
|
||||
byte lives at params[6] (= payload[12]). Use waveform_key_params() and
|
||||
token_params() helpers to build these safely.
|
||||
|
||||
Wire output: [ACK] [STX] dle_stuff(payload + checksum) [ETX]
|
||||
Wire output: [ACK] [STX] s3_stuff(payload + checksum) [ETX]
|
||||
|
||||
Args:
|
||||
sub: SUB command byte (e.g. 0x01 = FULL_CONFIG_READ)
|
||||
@@ -233,7 +279,7 @@ def build_bw_frame(sub: int, offset: int = 0, params: bytes = bytes(10)) -> byte
|
||||
offset_lo = offset & 0xFF
|
||||
payload = bytes([BW_CMD, 0x00, sub, 0x00, offset_hi, offset_lo]) + params
|
||||
chk = checksum(payload)
|
||||
wire = bytes([ACK, STX]) + dle_stuff(payload + bytes([chk])) + bytes([ETX])
|
||||
wire = bytes([ACK, STX]) + s3_stuff(payload + bytes([chk])) + bytes([ETX])
|
||||
return wire
|
||||
|
||||
|
||||
@@ -548,6 +594,25 @@ def bulk_waveform_term_v2(
|
||||
STRT_MARKER = b"STRT"
|
||||
|
||||
|
||||
def parse_strt_end_key(a5_data: bytes) -> Optional[int]:
|
||||
"""The event's FULL 32-bit end key from the STRT record in an A5 reply's
|
||||
data, or None. The page byte is part of it: an event that ends in the next
|
||||
64 KB page reads e.g. 0x011216C4 here, where `parse_strt_end_offset` gave
|
||||
0x16C4. Read after removing the reply's inner DLE pairs (10 02 / 10 03 /
|
||||
10 04 → 02 / 03 / 04), which the parser keeps as two bytes."""
|
||||
out, i = bytearray(), 0
|
||||
while i < len(a5_data):
|
||||
if a5_data[i] == DLE and i + 1 < len(a5_data) and a5_data[i + 1] in (STX, ETX, 0x04):
|
||||
i += 1
|
||||
continue
|
||||
out.append(a5_data[i])
|
||||
i += 1
|
||||
pos = bytes(out).find(STRT_MARKER)
|
||||
if pos < 0 or pos + 10 > len(out):
|
||||
return None
|
||||
return int.from_bytes(out[pos + 6:pos + 10], "big")
|
||||
|
||||
|
||||
def parse_strt_end_offset(a5_data: bytes) -> Optional[int]:
|
||||
"""
|
||||
Extract the event-end offset from the STRT record in an A5 response payload.
|
||||
|
||||
Reference in new issue
Block a user