Files
seismo-relay/tests/test_s3_compliance_anchor_fields.py
T
serversdownandClaude Opus 5.5 71bd14156d fix(minimateplus): read a MiniMate's settings by position, not the 'anchor' (#48)
The legacy SUB 1A decode found the record mode, sample rate, histogram
interval and record time by searching for be 80 00 00 00 00, believed a
stable anchor.  It is the block's [12:18]: the pre-trigger (-0.25 s) and
the stop mode (Fixed).  A unit in Auto stop mode, or with another
pre-trigger, has no such bytes, and all four read None -- in connect()'s
legacy routes (/device/info without a serial, the legacy event download's
sample rate).  The serial-routed settings path already read by position.

The block is now found by its own length field N = 2090 (08 2a, at
unescaped[12] on every recorded reply) and read by position, Series IV's
0x1A layout.

It never struck: every DL2 event file's setup copy has stop mode Fixed,
70,041 / 70,041, and Blastware's reports agree (7,631 / 7,631).
Tested on recorded replies patched to Auto stop and to a -0.5 s
pre-trigger (8 new tests; the 7 existing ones unchanged); not yet on a
unit in Auto stop mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
2026-12-01 01:08:36 -05:00

137 lines
5.4 KiB
Python

"""A MiniMate's mode, sample rate, histogram interval and record time are read
from the SUB 1A reply after its escapes are removed (bench finding #1, 2026-10-29).
The unit escapes 02/03/04/10 in every reply, and S3FrameParser keeps the `10` of
`10 02/03/04`, so the bytes between the mode and the `be 80 00 00 00 00` anchor
move by one for each escaped value. BE9441 at Histogram / 4096 sps / 2 s read
back as 0 sps and a 4098 s interval. Once unescaped, every recorded reply has the
same layout: mode at anchor-7, sample rate anchor-6, interval anchor-4, record
time anchor+6. The channel block and the strings are read on the raw bytes and
must not move.
"""
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.client import _decode_compliance_config_into # noqa: E402
from minimateplus.framing import S3FrameParser, unescape_reply # noqa: E402
from minimateplus.models import DeviceInfo # noqa: E402
FIX = Path(__file__).parent / "fixtures"
ANCHOR = b"\xbe\x80\x00\x00\x00\x00"
SOURCES = sorted((FIX / "minimate-bench-10-25-26").glob("*_rx.bin")) + \
[FIX / "minimate-bench-10-26-26-histogram" / "unit_replies.bin"]
def _frames(sub):
out = []
for path in SOURCES:
out += [f for f in S3FrameParser().feed(path.read_bytes()) if f.sub == sub]
return out
def _settings_replies():
"""Each distinct recorded 1A data frame carrying the anchor, in file order."""
seen, out = set(), []
for f in _frames(0xE5):
data = bytes(f.data)
logical = unescape_reply(data)
a = logical.find(ANCHOR, 0, 200)
if a < 0:
continue
key = logical[a - 8:a + 10]
if key not in seen:
seen.add(key)
out.append(data)
return out
def _decode(data):
info = DeviceInfo(serial="BE9441", firmware_minor=17)
_decode_compliance_config_into(data, info)
return info.compliance_config
EXPECT = [ # (mode, sample_rate, interval_s, record_time_s)
(1, 4096, 5, 5.0), # 10-25: Continuous
(4, 2048, 2, 20.0), # 10-26: Histogram + Continuous
(3, 4096, 900, 20.0), # Blastware's 16:26 write: Histogram, 15 min
(3, 4096, 2, 20.0), # Blastware's 16:27 write: back to 2 s
]
def test_there_are_four_distinct_recorded_settings_replies():
assert len(_settings_replies()) == 4
@pytest.mark.parametrize("i", range(4))
def test_anchor_fields_read_from_unescaped_bytes(i):
cc = _decode(_settings_replies()[i])
got = (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, round(cc.record_time, 2))
assert got == EXPECT[i]
def test_channel_block_still_reads_from_raw_bytes():
frames = _frames(0xE5)
start = next(i for i, f in enumerate(frames) if bytes(f.data) == _settings_replies()[3])
chan = next(bytes(f.data) for f in frames[start + 1:] if b"Tran" in bytes(f.data))
cc = _decode(_settings_replies()[3] + chan)
assert cc.trigger_level_geo == pytest.approx(0.04, abs=1e-4)
assert cc.geo_adc_scale == pytest.approx(6.2061, abs=1e-3)
assert cc.geo_range == 0
def test_a_reply_without_the_settings_block_reads_as_unknown():
# While monitoring, BE9441 answered its 1A reads with this same device-info
# block (2026-10-29): no anchor, so nothing is read from it.
fe = next(bytes(f.data) for f in _frames(0xFE) if b"Instantel" in bytes(f.data))
cc = _decode(fe)
assert (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, cc.record_time) == \
(None, None, None, None)
# The write side moved to the shared settings codec in phase 5 (2026-11-03):
# tests/test_setup_fields.py reproduces every Blastware write byte for byte.
# ── #48: the "anchor" is two settings, not a marker ───────────────────────────
# `be 80 00 00 00 00` is the block's [12:18]: the pre-trigger (-0.25 s, f32 BE)
# then [16] and the stop mode [17] (0 = Fixed). A unit in Auto stop mode, or
# with another pre-trigger, has no such bytes. The block is found instead by its
# own length field N = 2090 (`08 2a`), and its fields read by position: the layout
# Series 4's 0x1A shares (reference D.6.2; setup_block.py). No DL2 event was
# recorded in Auto stop mode (70,041 / 70,041 Fixed, measured 2026-12-01), so this
# tests a reply patched from a real one.
import struct # noqa: E402
def _patched(i, at, new):
"""Reply i with the anchor's bytes [at:at+len(new)] replaced. The anchor holds
no 02/03/04, so the raw bytes can be patched in place."""
d = bytearray(_settings_replies()[i])
a = bytes(d).find(ANCHOR)
assert a >= 0
d[a + at:a + at + len(new)] = new
return bytes(d)
@pytest.mark.parametrize("i", range(4))
def test_auto_stop_mode_still_reads_the_settings(i):
cc = _decode(_patched(i, 5, b"\x01")) # [17] stop mode = Auto
got = (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, round(cc.record_time, 2))
assert got == EXPECT[i]
@pytest.mark.parametrize("i", range(4))
def test_another_pretrigger_still_reads_the_settings(i):
cc = _decode(_patched(i, 0, struct.pack(">f", -0.5))) # [12:16] pre-trigger
got = (cc.recording_mode, cc.sample_rate, cc.histogram_interval_sec, round(cc.record_time, 2))
assert got == EXPECT[i]