fix(minimateplus): every A5 header is 12 bytes once inner DLE pairs count as one -- the general rule
Review of 352f694: the metadata-page detection fixed every observed stream but
was one case of a general rule. An A5 header is 12 bytes in the stripped sense
(TERM's 11); its raw length is 13 whenever the echoed address carries an inner
DLE pair -- the metadata pages at 0x1002 / 0x1004, but equally a sample chunk
or TERM boundary that lands there (inferred: ~1 in 128 events spanning
0x1000-0x1200, not in any capture). _raw_skip() counts it per frame, the way
probe_skip already did; _has_metadata_pages is gone. The reviewer measured
the rule reproducing the shipped bodies byte for byte on all 26 current-walk
streams on this box. Test: a chunk moved to 0x1002 yields the same body.
No DB or schema change; TOOL_VERSION not bumped (the file builder changed, the
.h5 decode did not).
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
f6166b4b04
commit
ac34f80daa
3 files changed
+54
-35
No files matched your search
@@ -552,19 +552,29 @@ def classify_frame(frame: S3Frame) -> str:
|
||||
|
||||
# ── Waveform file writer ───────────────────────────────────────────────────────────
|
||||
|
||||
def _has_metadata_pages(a5_frames) -> bool:
|
||||
"""Whether A5 frames 1 and 2 are the global metadata pages.
|
||||
_A5_HEADER = 12 # every A5 reply's header after the probe, in stripped bytes
|
||||
_A5_TERM_HEADER = 11 # the TERM reply's header
|
||||
|
||||
Only the event-1 walk (start_key[2:4] == 0) fetches them, at 0x1002 and
|
||||
0x1004; a non-first event's stream goes straight from the probe to sample
|
||||
chunks. Each A5 reply echoes its address at data[2:6], so the frames say
|
||||
which they are. Both must match: a sample chunk can sit at 0x1002, but then
|
||||
the next one is at 0x1202, never 0x1004. (Assuming metadata pages in every
|
||||
stream lost two bytes from every non-first event -- BE9441, 2026-10-25.)
|
||||
|
||||
def _raw_skip(data, stripped_len: int) -> int:
|
||||
"""Raw bytes of `data` covering its first `stripped_len` bytes, an inner DLE
|
||||
pair (10 02 / 10 03 / 10 04) counting as one -- the rule `probe_skip` and
|
||||
`_strip_inner_frame_dles` already use.
|
||||
|
||||
Every A5 header is 12 bytes in that sense (the TERM's 11), but its raw length
|
||||
varies with the address it echoes: 13 for the metadata pages at 0x1002 and
|
||||
0x1004, and for a sample chunk that lands there; 12 otherwise. A fixed
|
||||
"frames 1 and 2 are metadata pages, skip 13" cut two bytes from every
|
||||
non-first event, which has no metadata pages (BE9441, 2026-10-25).
|
||||
"""
|
||||
return (len(a5_frames) > 2
|
||||
and bytes(a5_frames[1].data[4:6]) == b"\x10\x02"
|
||||
and bytes(a5_frames[2].data[4:6]) == b"\x10\x04")
|
||||
raw_i = stripped = 0
|
||||
while stripped < stripped_len and raw_i < len(data):
|
||||
if data[raw_i] == 0x10 and raw_i + 1 < len(data) and data[raw_i + 1] in (0x02, 0x03, 0x04):
|
||||
raw_i += 2
|
||||
else:
|
||||
raw_i += 1
|
||||
stripped += 1
|
||||
return raw_i
|
||||
|
||||
|
||||
def extract_body_bytes(a5_frames):
|
||||
@@ -627,19 +637,13 @@ def extract_body_bytes(a5_frames):
|
||||
body_frames = a5_frames
|
||||
term_frame = None
|
||||
|
||||
meta = _has_metadata_pages(body_frames)
|
||||
all_bytes = bytearray()
|
||||
for fi, frame in enumerate(body_frames):
|
||||
if fi == 0:
|
||||
skip = probe_skip
|
||||
elif fi in (1, 2) and meta:
|
||||
skip = 13 # metadata pages
|
||||
else:
|
||||
skip = 12 # sample chunks
|
||||
skip = probe_skip if fi == 0 else _raw_skip(frame.data, _A5_HEADER)
|
||||
all_bytes.extend(_frame_body_bytes(frame, skip))
|
||||
|
||||
if term_frame is not None:
|
||||
all_bytes.extend(_frame_body_bytes(term_frame, 11))
|
||||
all_bytes.extend(_frame_body_bytes(term_frame, _raw_skip(term_frame.data, _A5_TERM_HEADER)))
|
||||
|
||||
# Find the first valid `0e 08` footer marker.
|
||||
footer_pos = -1
|
||||
@@ -802,9 +806,9 @@ def write_blastware_file(
|
||||
# Frame contribution loop (v0.14.0 BW-exact walk).
|
||||
# Skip values:
|
||||
# probe (fi=0): probe_skip
|
||||
# meta@0x1002 (fi=1): 13 (6-byte inner header) -- event-1 walk only
|
||||
# meta@0x1004 (fi=2): 13 (6-byte inner header) -- event-1 walk only
|
||||
# sample chunks (fi=3+): 12 (5-byte inner header)
|
||||
# every later frame: 12 stripped bytes (_raw_skip): 13 raw for an
|
||||
# address with an inner DLE pair (the metadata
|
||||
# pages 0x1002 / 0x1004, or a chunk there), else 12
|
||||
last_fi = len(body_frames) - 1
|
||||
|
||||
log.debug(
|
||||
@@ -812,16 +816,10 @@ def write_blastware_file(
|
||||
len(body_frames), last_fi,
|
||||
)
|
||||
|
||||
meta = _has_metadata_pages(body_frames)
|
||||
all_bytes = bytearray()
|
||||
|
||||
for fi, frame in enumerate(body_frames):
|
||||
if fi == 0:
|
||||
skip = probe_skip
|
||||
elif fi in (1, 2) and meta:
|
||||
skip = 13 # metadata pages (event-1 walk only)
|
||||
else:
|
||||
skip = 12 # sample chunks
|
||||
skip = probe_skip if fi == 0 else _raw_skip(frame.data, _A5_HEADER)
|
||||
|
||||
contribution = _frame_body_bytes(frame, skip)
|
||||
log.debug("write_blastware_file: fi=%d skip=%d raw_data=%d contribution=%d",
|
||||
@@ -829,14 +827,15 @@ def write_blastware_file(
|
||||
all_bytes.extend(contribution)
|
||||
|
||||
# Terminator contributes its content, which ends with the 26-byte footer.
|
||||
# skip=11 (not 12) because the terminator's inner frame header is 4 bytes,
|
||||
# one shorter than chunk frames' 5-byte inner header. Confirmed 2026-04-21.
|
||||
# 11 stripped bytes (not 12): the terminator's inner frame header is one
|
||||
# shorter than a chunk's. Confirmed 2026-04-21.
|
||||
if term_frame is not None:
|
||||
term_contribution = _frame_body_bytes(term_frame, 11)
|
||||
term_skip = _raw_skip(term_frame.data, _A5_TERM_HEADER)
|
||||
term_contribution = _frame_body_bytes(term_frame, term_skip)
|
||||
log.debug(
|
||||
"write_blastware_file: term_frame data_len=%d skip=11 "
|
||||
"write_blastware_file: term_frame data_len=%d skip=%d "
|
||||
"contribution_len=%d first8=%s",
|
||||
len(term_frame.data),
|
||||
len(term_frame.data), term_skip,
|
||||
len(term_contribution),
|
||||
term_contribution[:8].hex() if len(term_contribution) >= 8 else term_contribution.hex(),
|
||||
)
|
||||
|
||||
@@ -90,6 +90,12 @@ def test_be9441_event_1_downloads_verified():
|
||||
|
||||
|
||||
def test_the_check_refuses_the_file_the_metadata_page_bug_produced(monkeypatch):
|
||||
monkeypatch.setattr(bw, "_has_metadata_pages", lambda frames: True) # the bug, back
|
||||
real = bw._frame_body_bytes
|
||||
calls = []
|
||||
|
||||
def old_rule(frame, skip): # the bug, back: frames 1 and 2 cut as metadata pages
|
||||
calls.append(1)
|
||||
return real(frame, skip + 1 if len(calls) in (2, 3) and skip == 12 else skip)
|
||||
monkeypatch.setattr(bw, "_frame_body_bytes", old_rule)
|
||||
with pytest.raises(MalformedReply, match="not saved"):
|
||||
adapter().download_event(CABLE, "BE9441", KEY, "20261025141049")
|
||||
@@ -60,3 +60,17 @@ def test_the_written_file_reads_back_whole(which, tmp_path):
|
||||
pv = read_blastware_file(path).peak_values
|
||||
got = {"Tran": pv.tran, "Vert": pv.vert, "Long": pv.long}
|
||||
assert got == pytest.approx(UNIT_PEAKS[which], abs=1e-6)
|
||||
|
||||
|
||||
def test_a_sample_chunk_at_0x1002_is_not_mistaken_for_a_header_byte():
|
||||
"""Every A5 header is 12 bytes once inner DLE pairs (10 02/03/04) count as one;
|
||||
the TERM's is 11. A sample chunk echoing address 0x1002 carries 13 raw header
|
||||
bytes, like a metadata page -- the general rule handles both."""
|
||||
frames = a5_frames("event1")
|
||||
_s, want, _f = extract_body_bytes(frames)
|
||||
one = frames[1]
|
||||
hdr, payload = bytes(one.data[:12]), bytes(one.data[12:])
|
||||
assert hdr[4:6] != b"\x10\x02"
|
||||
moved = type(one)(**{**vars(one), "data": hdr[:4] + b"\x10\x02" + b"\x00" + hdr[6:] + payload})
|
||||
_s, got, _f = extract_body_bytes([frames[0], moved] + frames[2:])
|
||||
assert got == want
|
||||
Reference in new issue
Block a user