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
+78
-185
@@ -37,6 +37,8 @@ from .framing import (
|
||||
bulk_waveform_term_params,
|
||||
bulk_waveform_term_v2,
|
||||
parse_strt_end_offset,
|
||||
parse_strt_end_key,
|
||||
build_5a_request,
|
||||
POLL_PROBE,
|
||||
POLL_DATA,
|
||||
SESSION_RESET,
|
||||
@@ -558,48 +560,34 @@ class MiniMateProtocol:
|
||||
2026-10-25 and its partial event was kept). Without strict the legacy
|
||||
tolerance stands: an early end proceeds to TERM and returns what arrived.
|
||||
|
||||
Algorithm (matches BW captures across 2-sec / 3-sec / event-2):
|
||||
The walk (measured 2026-10-26: Blastware downloading all 8 of BE9441's
|
||||
events, three crossing a 64 KB page -- tests/test_5a_blastware_walk.py
|
||||
rebuilds its requests byte for byte):
|
||||
|
||||
1. Probe
|
||||
- For events at start_key[2:4] = 0x0000 (first event after erase
|
||||
/ wrap): probe at counter=0x0000 with full key in params.
|
||||
- For continuation events (start_key[2:4] != 0): first chunk at
|
||||
counter = start_key[2:4] + 0x0046; acts as both probe and
|
||||
first sample chunk; response carries STRT.
|
||||
1. Read 0x200 bytes at the event's own 32-bit address (the key). The
|
||||
reply carries STRT, whose end key is the full 32-bit address where
|
||||
the event ends.
|
||||
2. Read 0x200 bytes at each next address, +0x200 at a time, as plain
|
||||
32-bit numbers -- crossing into the next 64 KB page needs nothing
|
||||
special -- while the whole chunk fits before the end.
|
||||
3. TERM: read the remainder (end - next boundary) at the next boundary.
|
||||
Its reply carries the event's tail and the file footer.
|
||||
|
||||
2. Parse end_offset from STRT record at data[23:27] of the probe response.
|
||||
|
||||
3. Read two fixed metadata pages at counter=0x1002 and counter=0x1004
|
||||
— global session metadata (Project / Client / User Name / Seis Loc
|
||||
/ Extended Notes ASCII strings). Event 1 only; continuation
|
||||
events skip these (BW caches them across the session).
|
||||
|
||||
4. Walk sample chunks at 0x0200 increments, starting from 0x0600 for
|
||||
event 1 or `start + 0x0046 + 0x0200` for continuation events.
|
||||
Stop when `next_chunk + 0x0200 > end_offset`.
|
||||
|
||||
5. Send TERM frame with offset_word and params computed by
|
||||
`bulk_waveform_term_v2(key4, end_offset, last_chunk_counter)`.
|
||||
The TERM response contains the partial last chunk (residual =
|
||||
end_offset - next_boundary) including the 26-byte 0e 08 file
|
||||
footer.
|
||||
The first event on a unit reads 0x0000, 0x0200, 0x0400, 0x0600, ...: the
|
||||
"metadata pages at 0x1002 / 0x1004" of the earlier model were its chunks
|
||||
at 0x0200 and 0x0400, escaped (`10 02 00`, `10 04 00`). That model's
|
||||
16-bit counter could not leave the key's page -- the 64 KB page bug.
|
||||
|
||||
Returns:
|
||||
List of S3Frame objects from each A5 response (probe, metadata
|
||||
pages, sample chunks, optional TERM response). Caller passes
|
||||
`include_terminator=True` (e.g. write_blastware_file) to keep the
|
||||
TERM response in the list — it's required to reconstruct the
|
||||
file footer.
|
||||
The A5 frames in order (first chunk, chunks, and with
|
||||
`include_terminator=True` the TERM reply, which write_blastware_file
|
||||
needs for the footer).
|
||||
|
||||
Deprecated kwargs:
|
||||
stop_after_metadata: legacy "Project:"-string-based stop condition.
|
||||
No-op under the BW-exact walk; the loop is
|
||||
deterministically bounded by end_offset from
|
||||
STRT. Accepted for backward compat.
|
||||
extra_chunks_after_metadata: same.
|
||||
Deprecated kwargs (no-ops): stop_after_metadata, extra_chunks_after_metadata.
|
||||
|
||||
Raises:
|
||||
ProtocolError: on timeout / bad checksum / unexpected SUB.
|
||||
ProtocolError: on timeout / bad checksum / unexpected SUB, and in
|
||||
strict mode on any early end.
|
||||
"""
|
||||
if len(key4) != 4:
|
||||
raise ValueError(f"waveform key must be 4 bytes, got {len(key4)}")
|
||||
@@ -613,38 +601,15 @@ class MiniMateProtocol:
|
||||
|
||||
rsp_sub = _expected_rsp_sub(SUB_BULK_WAVEFORM) # 0xA5
|
||||
frames_data: list[S3Frame] = []
|
||||
start = int.from_bytes(key4, "big")
|
||||
|
||||
start_offset = (key4[2] << 8) | key4[3]
|
||||
is_event_1 = (start_offset == 0)
|
||||
def request(address: int, length: int) -> None:
|
||||
self._send(build_5a_request(address, length))
|
||||
self._parser.reset()
|
||||
|
||||
# ── Step 1: probe / first chunk ──────────────────────────────────────
|
||||
if is_event_1:
|
||||
probe_counter = 0
|
||||
probe_params = bulk_waveform_params(key4, 0, is_probe=True)
|
||||
log.debug("5A probe (event-1) key=%s counter=0x0000", key4.hex())
|
||||
else:
|
||||
# Continuation events: first 5A request lands at counter = key[2:4]
|
||||
# (i.e. the address of the off=0x46 WAVEHDR record returned by 1F).
|
||||
# The probe response carries STRT at byte 17 with end_offset.
|
||||
#
|
||||
# Confirmed 2026-05-04 from 5-1-26 "copy 2nd address" capture
|
||||
# (BW probes counter=0x2238 with key=01112238, STRT@17 end=0x417E)
|
||||
# and 5-4-26 BW captures (2-sec event probes counter=0x2238).
|
||||
#
|
||||
# The earlier "+0x46" formula in the doc came from calling
|
||||
# start_key the BOUNDARY (off=0x2C) key, but the iteration walk
|
||||
# uses 1F's off=0x46 key as cur_key, which already incorporates
|
||||
# the +0x46 offset relative to the boundary. Adding it again
|
||||
# caused the probe to overshoot, miss STRT, and run uncapped.
|
||||
probe_counter = start_offset
|
||||
probe_params = bulk_waveform_params(key4, probe_counter)
|
||||
log.debug(
|
||||
"5A probe (event-N) key=%s counter=0x%04X",
|
||||
key4.hex(), probe_counter,
|
||||
)
|
||||
|
||||
self._send(build_5a_frame(_BULK_CHUNK_OFFSET, probe_params))
|
||||
self._parser.reset()
|
||||
# ── Step 1: the event's first chunk; its reply carries STRT ──────────
|
||||
log.debug("5A first chunk key=%s", key4.hex())
|
||||
request(start, _BULK_COUNTER_STEP)
|
||||
try:
|
||||
rsp = self._recv_one(expected_sub=rsp_sub, reset_parser=False)
|
||||
except TimeoutError:
|
||||
@@ -653,110 +618,49 @@ class MiniMateProtocol:
|
||||
key4.hex(), self._parser.bytes_fed,
|
||||
)
|
||||
raise
|
||||
|
||||
frames_data.append(rsp)
|
||||
log.debug("5A A5[0] (probe) page_key=0x%04X %d bytes",
|
||||
rsp.page_key, len(rsp.data))
|
||||
log.debug("5A A5[0] (probe) page_key=0x%04X %d bytes", rsp.page_key, len(rsp.data))
|
||||
|
||||
# ── Step 2: parse STRT end_offset from probe response ────────────────
|
||||
end_offset = parse_strt_end_offset(rsp.data)
|
||||
if end_offset is None and strict:
|
||||
# ── Step 2: where the event ends -- STRT's full 32-bit end key ───────
|
||||
end = parse_strt_end_key(rsp.data)
|
||||
if end is None and strict:
|
||||
raise ProtocolError(
|
||||
f"5A probe for {key4.hex()} carried no STRT record, so the event's end "
|
||||
"is unknown -- not downloaded")
|
||||
if strict and end_offset is not None and end_offset < start_offset:
|
||||
# The known 5A page bug: parse_strt_end_offset keeps only the low 16
|
||||
# bits, so an event that ends in the next 64 KB page reads as ending
|
||||
# before it starts, and the walk cannot follow it yet. BE9441's
|
||||
# events 2 and 19 (bench 2026-10-26) -- named here, before any chunk.
|
||||
raise ProtocolError(
|
||||
f"event {key4.hex()} crosses the unit's 64 KB page boundary (starts at "
|
||||
f"0x{start_offset:04X} in page 0x{key4[1]:02X}, ends at 0x{end_offset:04X} in "
|
||||
"the next) -- SFM cannot download such an event yet (the known 5A page bug); "
|
||||
"not saved")
|
||||
if end_offset is None:
|
||||
if end is not None and end <= start:
|
||||
if strict:
|
||||
raise ProtocolError(
|
||||
f"5A STRT for {key4.hex()} says the event ends at 0x{end:08X}, at or "
|
||||
"before its start -- not downloaded")
|
||||
log.warning("5A STRT end 0x%08X is not after the start 0x%08X -- ignored", end, start)
|
||||
end = None
|
||||
have_end = end is not None
|
||||
if not have_end:
|
||||
log.warning(
|
||||
"5A probe response did not contain a STRT record; "
|
||||
"5A probe response did not contain a usable STRT record; "
|
||||
"cannot bound chunk loop — falling back to max_chunks=%d cap",
|
||||
max_chunks,
|
||||
)
|
||||
end_offset = 0xFFFF # impossible value → loop runs to max_chunks
|
||||
end = start + (max_chunks + 2) * _BULK_COUNTER_STEP # the cap bounds the loop
|
||||
else:
|
||||
log.info(
|
||||
"5A STRT start_offset=0x%04X end_offset=0x%04X size=0x%04X",
|
||||
start_offset, end_offset, end_offset - start_offset,
|
||||
)
|
||||
log.info("5A STRT start=0x%08X end=0x%08X size=0x%X", start, end, end - start)
|
||||
|
||||
# ── Step 3: metadata pages 0x1002 + 0x1004 (event 1 only) ────────────
|
||||
# Confirmed from BW captures: BW reads these two fixed device-buffer
|
||||
# pages immediately after the probe for events at start_key[2:4]=0.
|
||||
# Continuation events skip them (BW caches across the session).
|
||||
# Their content is global compliance-setup metadata: Project, Client,
|
||||
# User Name, Seis Loc, Extended Notes.
|
||||
if is_event_1:
|
||||
for meta_counter in (0x1002, 0x1004):
|
||||
# Metadata page params have an extra trailing 0x00 byte
|
||||
# (12-byte params instead of 11) — empirical from BW captures.
|
||||
# Checksum-neutral but matches BW byte-for-byte.
|
||||
meta_params = bytes([
|
||||
0x00,
|
||||
key4[0], key4[1],
|
||||
(meta_counter >> 8) & 0xFF,
|
||||
meta_counter & 0xFF,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
])
|
||||
log.debug("5A metadata page counter=0x%04X", meta_counter)
|
||||
self._send(build_5a_frame(_BULK_CHUNK_OFFSET, meta_params))
|
||||
self._parser.reset()
|
||||
try:
|
||||
meta_rsp = self._recv_one(
|
||||
expected_sub=rsp_sub, reset_parser=False, timeout=10.0,
|
||||
)
|
||||
except TimeoutError as exc:
|
||||
if strict:
|
||||
raise TimeoutError(
|
||||
f"5A stream ended early: metadata page 0x{meta_counter:04X} got "
|
||||
"no reply -- the event was not kept") from exc
|
||||
log.warning(
|
||||
"5A metadata page 0x%04X TIMED OUT — continuing",
|
||||
meta_counter,
|
||||
)
|
||||
continue
|
||||
frames_data.append(meta_rsp)
|
||||
log.debug(
|
||||
"5A meta@0x%04X page_key=0x%04X %d bytes",
|
||||
meta_counter, meta_rsp.page_key, len(meta_rsp.data),
|
||||
)
|
||||
|
||||
# ── Step 4: sample chunk loop, bounded by end_offset ─────────────────
|
||||
# Sample chunks start at:
|
||||
# event 1: counter = 0x0600
|
||||
# event N (>0): counter = probe_counter + 0x0200
|
||||
# (probe was the first sample chunk)
|
||||
if is_event_1:
|
||||
counter = 0x0600
|
||||
else:
|
||||
counter = probe_counter + _BULK_COUNTER_STEP
|
||||
|
||||
last_chunk_counter: Optional[int] = (
|
||||
probe_counter if not is_event_1 else None
|
||||
)
|
||||
# ── Step 3: chunks at +0x200, plain 32-bit addresses ─────────────────
|
||||
address = start + _BULK_COUNTER_STEP
|
||||
last = start
|
||||
chunks_fetched = 0
|
||||
|
||||
while chunks_fetched < max_chunks:
|
||||
# Stop when next chunk would straddle the event end.
|
||||
if counter + _BULK_COUNTER_STEP > end_offset:
|
||||
# Stop when the next chunk would straddle the event end.
|
||||
if address + _BULK_COUNTER_STEP > end:
|
||||
log.debug(
|
||||
"5A chunk loop done at counter=0x%04X (end=0x%04X); "
|
||||
"%d chunks fetched",
|
||||
counter, end_offset, chunks_fetched,
|
||||
"5A chunk loop done at 0x%08X (end=0x%08X); %d chunks fetched",
|
||||
address, end, chunks_fetched,
|
||||
)
|
||||
break
|
||||
|
||||
params = bulk_waveform_params(key4, counter)
|
||||
log.debug("5A chunk #%d counter=0x%04X", chunks_fetched + 1, counter)
|
||||
self._send(build_5a_frame(_BULK_CHUNK_OFFSET, params))
|
||||
self._parser.reset()
|
||||
log.debug("5A chunk #%d address=0x%08X", chunks_fetched + 1, address)
|
||||
request(address, _BULK_COUNTER_STEP)
|
||||
try:
|
||||
rsp = self._recv_one(
|
||||
expected_sub=rsp_sub, reset_parser=False, timeout=10.0,
|
||||
@@ -764,13 +668,13 @@ class MiniMateProtocol:
|
||||
except TimeoutError as exc:
|
||||
raw = self._parser.bytes_fed
|
||||
log.warning(
|
||||
"5A TIMEOUT chunk=%d counter=0x%04X raw_bytes=%d",
|
||||
chunks_fetched + 1, counter, raw,
|
||||
"5A TIMEOUT chunk=%d address=0x%08X raw_bytes=%d",
|
||||
chunks_fetched + 1, address, raw,
|
||||
)
|
||||
if strict:
|
||||
raise TimeoutError(
|
||||
f"5A stream ended early: no reply for the chunk at 0x{counter:04X}; the "
|
||||
f"event ends at 0x{end_offset:04X} ({chunks_fetched} chunk(s) received) "
|
||||
f"5A stream ended early: no reply for the chunk at 0x{address:08X}; the "
|
||||
f"event ends at 0x{end:08X} ({chunks_fetched} chunk(s) received) "
|
||||
"-- the event was not kept") from exc
|
||||
if raw > 0 and frames_data:
|
||||
log.warning(
|
||||
@@ -788,52 +692,41 @@ class MiniMateProtocol:
|
||||
# Device terminated mid-stream unexpectedly.
|
||||
if strict:
|
||||
raise ProtocolError(
|
||||
f"5A stream ended early: the unit sent its terminator at 0x{counter:04X}, "
|
||||
f"before the event's end 0x{end_offset:04X} -- the event was not kept")
|
||||
f"5A stream ended early: the unit sent its terminator at 0x{address:08X}, "
|
||||
f"before the event's end 0x{end:08X} -- the event was not kept")
|
||||
log.warning(
|
||||
"5A unexpected page_key=0x0000 mid-stream at counter=0x%04X",
|
||||
counter,
|
||||
"5A unexpected page_key=0x0000 mid-stream at address=0x%08X",
|
||||
address,
|
||||
)
|
||||
if include_terminator:
|
||||
frames_data.append(rsp)
|
||||
return frames_data
|
||||
|
||||
frames_data.append(rsp)
|
||||
last_chunk_counter = counter
|
||||
counter += _BULK_COUNTER_STEP
|
||||
last = address
|
||||
address += _BULK_COUNTER_STEP
|
||||
chunks_fetched += 1
|
||||
else:
|
||||
if strict:
|
||||
raise ProtocolError(
|
||||
f"5A reached the {max_chunks}-chunk cap at 0x{counter:04X} before the "
|
||||
f"event's end 0x{end_offset:04X} -- the event was not kept")
|
||||
f"5A reached the {max_chunks}-chunk cap at 0x{address:08X} before the "
|
||||
f"event's end 0x{end:08X} -- the event was not kept")
|
||||
log.warning(
|
||||
"5A reached max_chunks=%d at counter=0x%04X (end=0x%04X)",
|
||||
max_chunks, counter, end_offset,
|
||||
"5A reached max_chunks=%d at address=0x%08X (end=0x%08X)",
|
||||
max_chunks, address, end,
|
||||
)
|
||||
|
||||
# ── Step 5: TERM with proper end_offset-derived formula ──────────────
|
||||
if last_chunk_counter is None or end_offset == 0xFFFF:
|
||||
# No STRT or no chunks fetched — fall back to legacy TERM.
|
||||
log.warning(
|
||||
"5A using legacy TERM (offset_word=0x005A); "
|
||||
"end_offset unavailable or no chunks fetched",
|
||||
)
|
||||
legacy_counter = (last_chunk_counter or probe_counter) + _BULK_COUNTER_STEP
|
||||
term_offset_word = _BULK_TERM_OFFSET # 0x005A
|
||||
term_params = bulk_waveform_term_params(key4, legacy_counter)
|
||||
# ── Step 4: TERM -- the remainder, from the next boundary ───────────
|
||||
boundary = last + _BULK_COUNTER_STEP
|
||||
if have_end:
|
||||
term_length = end - boundary
|
||||
log.debug("5A TERM address=0x%08X length=0x%04X end=0x%08X",
|
||||
boundary, term_length, end)
|
||||
else:
|
||||
term_offset_word, term_params = bulk_waveform_term_v2(
|
||||
key4, end_offset, last_chunk_counter,
|
||||
)
|
||||
log.debug(
|
||||
"5A TERM offset_word=0x%04X params[2:4]=%s end=0x%04X "
|
||||
"last_chunk=0x%04X",
|
||||
term_offset_word, term_params[2:4].hex(),
|
||||
end_offset, last_chunk_counter,
|
||||
)
|
||||
term_length = _BULK_TERM_OFFSET # legacy: no STRT, so no remainder known
|
||||
log.warning("5A using legacy TERM (length 0x%04X); end unknown", term_length)
|
||||
|
||||
self._send(build_5a_frame(term_offset_word, term_params))
|
||||
self._send(build_5a_request(boundary, term_length))
|
||||
try:
|
||||
term_rsp = self._recv_one(expected_sub=rsp_sub, timeout=10.0)
|
||||
log.info(
|
||||
|
||||
Reference in new issue
Block a user