diff --git a/minimateplus/blastware_file.py b/minimateplus/blastware_file.py index 2784040..d10eab6 100644 --- a/minimateplus/blastware_file.py +++ b/minimateplus/blastware_file.py @@ -552,6 +552,21 @@ 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. + + 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.) + """ + 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") + + def extract_body_bytes(a5_frames): """Reconstruct the Blastware-file body bytes from a list of A5 frames. @@ -612,11 +627,12 @@ 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): + elif fi in (1, 2) and meta: skip = 13 # metadata pages else: skip = 12 # sample chunks @@ -786,8 +802,8 @@ 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) - # meta@0x1004 (fi=2): 13 (6-byte inner header) + # 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) last_fi = len(body_frames) - 1 @@ -796,13 +812,14 @@ 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): - skip = 13 # metadata pages + elif fi in (1, 2) and meta: + skip = 13 # metadata pages (event-1 walk only) else: skip = 12 # sample chunks diff --git a/tests/fixtures/minimate-bench-10-25-26/BE9441_event0_01110000_rx.bin b/tests/fixtures/minimate-bench-10-25-26/BE9441_event0_01110000_rx.bin new file mode 100644 index 0000000..03e78a0 Binary files /dev/null and b/tests/fixtures/minimate-bench-10-25-26/BE9441_event0_01110000_rx.bin differ diff --git a/tests/fixtures/minimate-bench-10-25-26/BE9441_event1_0111646a_rx.bin b/tests/fixtures/minimate-bench-10-25-26/BE9441_event1_0111646a_rx.bin new file mode 100644 index 0000000..b042d70 Binary files /dev/null and b/tests/fixtures/minimate-bench-10-25-26/BE9441_event1_0111646a_rx.bin differ diff --git a/tests/fixtures/minimate-bench-10-25-26/README.md b/tests/fixtures/minimate-bench-10-25-26/README.md new file mode 100644 index 0000000..7983416 --- /dev/null +++ b/tests/fixtures/minimate-bench-10-25-26/README.md @@ -0,0 +1,15 @@ +# MiniMate bench, 2026-10-25 — BE9441 (S338.17, mic disabled), serial cable + +Raw unit-to-SFM bytes of two one-event downloads by serial (`POST /device/events/download`), +taken from the bench frame log in the order received: the whole session (POLL, serial, +config, the event walk, then the `0x5A` bulk stream). Parse with `S3FrameParser` and keep +the `0xA5` frames. + +| file | event | key | 0x5A pattern | unit's own 0C peaks (in/s) T / V / L | +|---|---|---|---|---| +| `BE9441_event0_01110000_rx.bin` | 0 (first) | `01110000` | probe, metadata pages `0x1002` `0x1004`, samples, TERM | 0.020 / 0.060 / 0.015 | +| `BE9441_event1_0111646a_rx.bin` | 1 | `0111646a` | probe, samples, TERM — no metadata pages | 0.030 / 0.095 / 0.020 | + +Both events: single shot, 4096 sps, 5 s record + 1 s pre-trigger = 21,504 samples per channel. +Event 1 is the case that showed the body builder skipping 13 bytes on its second and third +frames as if they were metadata pages (2 bytes lost, decode truncated at 1,024 / 2,782 / 512). diff --git a/tests/test_minimate_body_no_metadata.py b/tests/test_minimate_body_no_metadata.py new file mode 100644 index 0000000..befb939 --- /dev/null +++ b/tests/test_minimate_body_no_metadata.py @@ -0,0 +1,62 @@ +"""A non-first MiniMate event's 5A stream has no metadata pages. + +The event-1 walk fetches the two global metadata pages (0x1002, 0x1004) as A5 +frames 1 and 2, which carry a 6-byte inner header (skip 13). A non-first event's +walk fetches none, so its frames 1 and 2 are ordinary sample chunks (skip 12). +The body builder skipped 13 on frames 1 and 2 unconditionally: every non-first +event lost two bytes and decoded truncated. Found on BE9441 at the bench +(2026-10-25); fixtures are its raw replies (see the fixture README). +""" +from __future__ import annotations + +import os +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from minimateplus.blastware_file import extract_body_bytes, write_blastware_file # noqa: E402 +from minimateplus.event_file_io import read_blastware_file # noqa: E402 +from minimateplus.framing import S3FrameParser # noqa: E402 +from minimateplus.models import Event # noqa: E402 +from minimateplus.waveform_codec import decode_waveform_v2 # noqa: E402 + +FIX = Path(__file__).parent / "fixtures" / "minimate-bench-10-25-26" +SAMPLES = 21_504 # 5 s record + 1 s pre-trigger at 4096 sps +UNIT_PEAKS = { # the unit's own 0C peaks, in/s + "event0": {"Tran": 0.020, "Vert": 0.060, "Long": 0.015}, + "event1": {"Tran": 0.030, "Vert": 0.095, "Long": 0.020}, +} +FILES = {"event0": "BE9441_event0_01110000_rx.bin", "event1": "BE9441_event1_0111646a_rx.bin"} + + +def a5_frames(which): + return [f for f in S3FrameParser().feed((FIX / FILES[which]).read_bytes()) if f.sub == 0xA5] + + +@pytest.mark.parametrize("which", ["event0", "event1"]) +def test_the_body_decodes_whole_and_matches_the_units_own_peaks(which): + _strt, body, _footer = extract_body_bytes(a5_frames(which)) + dec = decode_waveform_v2(body) + for ch, peak in UNIT_PEAKS[which].items(): + assert len(dec[ch]) == SAMPLES, ch + assert max(abs(x) for x in dec[ch]) * 0.005 == pytest.approx(peak, abs=1e-9), ch + + +def test_metadata_pages_are_recognised_by_their_echoed_addresses(): + first, other = a5_frames("event0"), a5_frames("event1") + assert (bytes(first[1].data[4:6]), bytes(first[2].data[4:6])) == (b"\x10\x02", b"\x10\x04") + assert bytes(other[1].data[4:6]) not in (b"\x10\x02", b"\x10\x04") + + +@pytest.mark.parametrize("which", ["event0", "event1"]) +def test_the_written_file_reads_back_whole(which, tmp_path): + ev = Event(index=0) + ev.timestamp = None # the builder takes the footer from the TERM frame + path = tmp_path / f"{which}.P00" + write_blastware_file(ev, a5_frames(which), 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)