fix(minimateplus): read a MiniMate's mode, sample rate, interval and record time after removing escapes (#1)

The unit escapes 02/03/04/10 in every reply and S3FrameParser keeps the 10 of
10 02/03/04, so on the raw SUB 1A bytes an escaped mode, rate or interval
shifted every field after it: BE9441 at Histogram / 4096 sps / 2 s read back as
0 sps and a 4098 s interval (bench 2026-10-29), and a Continuous unit read as
Single Shot.  The four anchor fields are now read from unescape_reply(data):
mode anchor-7, sample rate anchor-6, interval anchor-4, record time anchor+6 --
measured on all four distinct recorded replies, two of them Blastware's own
writes (15 min, then 2 s).  The old 'anchor-7 = constant 0x10' was the escape
before 04 at 1024 sps.

The channel block and strings still read the raw bytes: their label offsets were
measured there and are wrong unescaped (pinned by a test).  A reply with no
anchor -- BE9441's monitoring-time 1A answer was its device-info block -- leaves
all four None.

Store consequence: none; this is the live settings read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-29 23:19:11 -04:00
1 parent 0dbd0b506d
commit b320a534bc
2 files changed
+134 -77

No files matched your search

+38 -77
View File
@@ -2365,92 +2365,53 @@ def _decode_compliance_config_into(data: bytes, info: DeviceInfo) -> None:
# ── recording_mode / sample_rate / histogram_interval / record_time ─────────
# 6-byte stable anchor: b'\xbe\x80\x00\x00\x00\x00' — confirmed across BE11529
# and BE18189. The 4 bytes immediately before the anchor are NOT constant:
# bytes -4:-2 are the histogram_interval (uint16 BE, seconds), and bytes -2:0
# are zero padding. The old "10-byte anchor" (\x01\x2c\x00\x00 prefix) was
# only constant when the histogram interval happened to be 5 min (0x012C = 300).
# and BE18189. These four fields are read from the reply with its escapes
# REMOVED (unescape_reply): the unit escapes 02/03/04/10 in every reply and
# S3FrameParser keeps the `10` of `10 02/03/04`, so on the raw bytes an escaped
# mode, sample rate or interval moves every field after it by one. BE9441 at
# Histogram / 4096 sps / 2 s read 0 sps and a 4098 s interval (2026-10-29).
#
# E5 read format layout relative to 6-byte anchor:
# _anchor - 8 : recording_mode (uint8)
# _anchor - 7 : 0x10 (extra byte E5 read only; absent in SUB 71 write)
# _anchor - 6 : sample_rate_hi (uint16 BE, MSB)
# _anchor - 5 : sample_rate_lo (uint16 BE, LSB)
# _anchor - 4 : histogram_interval_hi (uint16 BE, seconds, MSB)
# _anchor - 3 : histogram_interval_lo (uint16 BE, seconds, LSB)
# _anchor - 2 : 0x00 (padding)
# _anchor - 1 : 0x00 (padding)
# _anchor : \xbe\x80\x00\x00\x00\x00 (6-byte anchor)
# _anchor + 6 : record_time (float32 BE)
# Layout after unescaping, measured on every recorded reply (four settings,
# two of them Blastware's own writes -- tests/test_s3_compliance_anchor_fields.py):
# _anchor - 7 : recording_mode (uint8)
# _anchor - 6 : sample_rate (uint16 BE)
# _anchor - 4 : histogram_interval_sec (uint16 BE, seconds)
# _anchor - 2 : 0x00 0x00 (padding)
# _anchor : \xbe\x80\x00\x00\x00\x00
# _anchor + 6 : record_time (float32 BE)
# ⚠ The old raw layout's "anchor - 7 = constant 0x10" was the escape in front
# of `04` at 1024 sps (0x0400); it read right only at that rate.
# Everything below this block (channel block, strings) still reads the RAW
# bytes: its label-relative offsets were measured on them and are wrong
# unescaped. A reply with no anchor (BE9441 answered 1A with its device-info
# block while monitoring, 2026-10-29) leaves all four fields None.
_ANCHOR = b'\xbe\x80\x00\x00\x00\x00'
_anchor = data.find(_ANCHOR, 0, 150)
logical = unescape_reply(data)
_anchor = logical.find(_ANCHOR, 0, 150)
# Log anchor position on every decode so we can compare read vs write and
# catch unexpected shifts from DLE jitter or firmware differences.
# Expected position is ~15 for the E5 read payload (anchor - 8 = recording_mode).
if _anchor < 0:
log.warning(
"_decode_compliance_config_into: anchor NOT FOUND in data[0:150] (len=%d)",
len(data),
)
else:
log.info(
"_decode_compliance_config_into: anchor at data[%d] data_len=%d "
"(expected ~15; recording_mode@%d sample_rate@%d:%d "
"histogram_interval@%d:%d record_time@%d:%d)",
_anchor, len(data),
_anchor - 8,
_anchor - 6, _anchor - 4,
_anchor - 4, _anchor - 2,
_anchor + 6, _anchor + 10,
)
if _anchor >= 8 and _anchor + 10 <= len(data):
try:
config.recording_mode = data[_anchor - 8]
log.debug(
"compliance_config: recording_mode = 0x%02X (anchor@%d)", config.recording_mode, _anchor
)
except Exception as exc:
log.warning("compliance_config: recording_mode extraction failed: %s", exc)
try:
config.sample_rate = struct.unpack_from(">H", data, _anchor - 6)[0]
log.debug(
"compliance_config: sample_rate = %d Sa/s (anchor@%d)", config.sample_rate, _anchor
)
except Exception as exc:
log.warning("compliance_config: sample_rate extraction failed: %s", exc)
try:
config.histogram_interval_sec = struct.unpack_from(">H", data, _anchor - 4)[0]
log.debug(
"compliance_config: histogram_interval = %d s (anchor@%d)",
config.histogram_interval_sec, _anchor
)
except Exception as exc:
log.warning("compliance_config: histogram_interval extraction failed: %s", exc)
try:
config.record_time = struct.unpack_from(">f", data, _anchor + 6)[0]
log.debug(
"compliance_config: record_time = %.3f s (anchor@%d)", config.record_time, _anchor
)
except Exception as exc:
log.warning("compliance_config: record_time extraction failed: %s", exc)
elif _anchor >= 6 and _anchor + 10 <= len(data):
# Fallback: anchor found but not enough bytes before it for recording_mode
log.warning("compliance_config: anchor too close to start (anchor@%d) — skipping recording_mode", _anchor)
try:
config.sample_rate = struct.unpack_from(">H", data, _anchor - 6)[0]
except Exception:
pass
try:
config.record_time = struct.unpack_from(">f", data, _anchor + 6)[0]
except Exception:
pass
else:
log.warning(
"compliance_config: anchor %s not found in cfg[0:150] (len=%d) "
"— sample_rate, record_time and recording_mode will be None",
_ANCHOR.hex(), len(data),
)
elif _anchor >= 7 and _anchor + 10 <= len(logical):
log.info("_decode_compliance_config_into: anchor at unescaped[%d] data_len=%d",
_anchor, len(data))
config.recording_mode = logical[_anchor - 7]
config.sample_rate = struct.unpack_from(">H", logical, _anchor - 6)[0]
config.histogram_interval_sec = struct.unpack_from(">H", logical, _anchor - 4)[0]
config.record_time = struct.unpack_from(">f", logical, _anchor + 6)[0]
log.debug(
"compliance_config: recording_mode=0x%02X sample_rate=%d histogram_interval=%d s "
"record_time=%.3f s (anchor@%d unescaped)",
config.recording_mode, config.sample_rate, config.histogram_interval_sec,
config.record_time, _anchor,
)
else:
log.warning("compliance_config: anchor at unescaped[%d] is too close to an end "
"(len=%d) — mode, rate, interval and record time left None",
_anchor, len(logical))
# ── Project strings ───────────────────────────────────────────────────────
try:
+96
View File
@@ -0,0 +1,96 @@
"""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)