diff --git a/minimateplus/client.py b/minimateplus/client.py index 5f74588..2cf70e6 100644 --- a/minimateplus/client.py +++ b/minimateplus/client.py @@ -34,7 +34,7 @@ import re import struct from typing import Optional -from .framing import S3Frame +from .framing import S3Frame, unescape_reply from .models import ( CallHomeConfig, ComplianceConfig, @@ -60,6 +60,11 @@ from .transport import SerialTransport, BaseTransport log = logging.getLogger(__name__) +# SUB 0x1C state-flag values measured while a unit was monitoring: 0x10 (BE11529, +# 2026-04-08, 98 frames) and 0x0B (BE9441 recording a histogram, 2026-10-26). +# Idle is 0x00 on both. +S3_MONITORING_FLAGS = frozenset({0x10, 0x0B}) + # ── Module-level constants ──────────────────────────────────────────────────── # Trigger config payload hardcoded from 3-11-26 BW TX capture (BW frame 108). @@ -2682,6 +2687,10 @@ def _decode_monitor_status(data: bytes) -> MonitorStatus: mem_total 0x000EFFF2 = 983026 bytes ≈ 960 KB mem_free 0x000E9E52 = 958034 bytes ≈ 935 KB """ + # Unescape first (2026-10-26): BE9441's free memory 0x000D0E04 arrived as + # `0d 0e 10 04`, and with the parser's kept 10 every field counted from the + # end read one byte off -- 419.84 V and 251,655,168 bytes for 6.76 V and 983,028. + data = unescape_reply(data) # The data section starts at offset 11 (after the S3 section header). section = data[11:] if len(data) > 11 else data @@ -2694,7 +2703,11 @@ def _decode_monitor_status(data: bytes) -> MonitorStatus: # Confirmed from byte diff of all 144 0xE3 data frames in 4-8-26/2ndtry capture: # section[1] = 0x00 in all IDLE frames, 0x10 in all MONITORING frames. # (section[6] also changes but has non-binary values 0xea/0x07 — device-specific.) - is_monitoring = len(section) > 1 and section[1] == 0x10 + # + # 2026-10-26: 0x0B on BE9441 recording a HISTOGRAM (confirmed at the unit); + # 0x00 on the same unit idle. Only measured values count as monitoring -- + # sfm.device_access.s3.S3_RUN_STATES refuses any other for a change. + is_monitoring = len(section) > 1 and section[1] in S3_MONITORING_FLAGS # The same byte, raw -- but only from a real section: for data of 11 bytes # or fewer, `section` above falls back to the header itself. state_flag = data[12] if len(data) > 12 else None diff --git a/minimateplus/framing.py b/minimateplus/framing.py index 8795a77..5a29e21 100644 --- a/minimateplus/framing.py +++ b/minimateplus/framing.py @@ -600,20 +600,31 @@ def bulk_waveform_term_v2( STRT_MARKER = b"STRT" +def unescape_reply(data: bytes) -> bytes: + """A reply's data with each kept escape removed: `10 02/03/04` → `02/03/04`. + + The unit escapes 02, 03, 04 and 10 in its replies exactly as requests do + (748/748 replies, 2026-10-26). S3FrameParser collapses `10 10` but keeps the + `10` of `10 02/03/04` (inner-frame markers to it), so its `.data` carries an + extra byte before each 02/03/04 value -- harmless for fields read before the + escape, one byte off for any read after it, or counted from the end.""" + out, i = bytearray(), 0 + while i < len(data): + if data[i] == DLE and i + 1 < len(data) and data[i + 1] in (STX, ETX, 0x04): + i += 1 + continue + out.append(data[i]) + i += 1 + return bytes(out) + + def parse_strt_end_key(a5_data: bytes) -> Optional[int]: """The event's FULL 32-bit end key from the STRT record in an A5 reply's data, or None. The page byte is part of it: an event that ends in the next 64 KB page reads e.g. 0x011216C4 here, where `parse_strt_end_offset` gave - 0x16C4. Read after removing the reply's inner DLE pairs (10 02 / 10 03 / - 10 04 → 02 / 03 / 04), which the parser keeps as two bytes.""" - out, i = bytearray(), 0 - while i < len(a5_data): - if a5_data[i] == DLE and i + 1 < len(a5_data) and a5_data[i + 1] in (STX, ETX, 0x04): - i += 1 - continue - out.append(a5_data[i]) - i += 1 - pos = bytes(out).find(STRT_MARKER) + 0x16C4. Read after `unescape_reply`.""" + out = unescape_reply(a5_data) + pos = out.find(STRT_MARKER) if pos < 0 or pos + 10 > len(out): return None return int.from_bytes(out[pos + 6:pos + 10], "big") diff --git a/sfm/device_access/s3.py b/sfm/device_access/s3.py index e84c49e..c9e13cc 100644 --- a/sfm/device_access/s3.py +++ b/sfm/device_access/s3.py @@ -27,8 +27,10 @@ S3_MODES = {0x00: "Single Shot", 0x01: "Continuous", 0x03: "Histogram", 0x04: "H S3_EVENT_TIMEOUT = 120.0 # what the legacy event-download routes use -# The 0x1C flag byte (section[1]): the only two values ever measured. -S3_RUN_STATES = {0x00: "idle", 0x10: "monitoring"} +# The 0x1C flag byte (section[1]): the only values ever measured. 0x10 is +# BE11529 monitoring (2026-04-08); 0x0B is BE9441 recording a histogram, +# confirmed at the unit (2026-10-26). Anything else is refused for a change. +S3_RUN_STATES = {0x00: "idle", 0x10: "monitoring", 0x0B: "monitoring"} def s3_run_state(status: Any) -> str: @@ -263,8 +265,10 @@ class Series3Adapter: def op(client: Any) -> UnitStatus: client.poll() ms = client.get_monitor_status() + # The change path's reading, not a guess: an unmeasured flag is None. + monitoring = {"monitoring": True, "idle": False}.get(s3_run_state(ms)) return UnitStatus( - serial=serial, series=3, monitoring=ms.is_monitoring, + serial=serial, series=3, monitoring=monitoring, battery_volts=round(ms.battery_v, 2) if ms.battery_v is not None else None, memory_total_bytes=ms.memory_total, memory_free_bytes=ms.memory_free, device_time=None, event_count=None, diff --git a/tests/test_device_access_change.py b/tests/test_device_access_change.py index 56b32d9..2d245b6 100644 --- a/tests/test_device_access_change.py +++ b/tests/test_device_access_change.py @@ -686,10 +686,17 @@ def test_series3_state_names_come_from_the_raw_flag(flag, state): assert r.state_before == state +@pytest.mark.parametrize("flag,monitoring", [(0x00, False), (0x10, True), (0x0B, True), (0x07, None)]) +def test_series3_status_shows_the_change_paths_reading(flag, monitoring): + # 0x0B: BE9441 recording a histogram (2026-10-26) once showed "not monitoring" + st = access3(FakeMiniMateClient(monitor_states=[flag])).status(TCP3, "BE18193") + assert st.monitoring is monitoring + + def test_series3_monitor_status_decode_keeps_the_raw_flag(): from minimateplus.client import _decode_monitor_status tail = bytes.fromhex("02a8000effc0000e9e52") # battery + memory: the last 10 bytes - for flag, monitoring in ((0x00, False), (0x10, True), (0x07, False)): + for flag, monitoring in ((0x00, False), (0x10, True), (0x0B, True), (0x07, False)): st = _decode_monitor_status(bytes(11) + bytes([0x00, flag]) + bytes(34) + tail) assert (st.state_flag, st.is_monitoring) == (flag, monitoring) assert _decode_monitor_status(bytes(12)).state_flag is None # no section[1] diff --git a/tests/test_minimate_monitor_status.py b/tests/test_minimate_monitor_status.py new file mode 100644 index 0000000..bd96677 --- /dev/null +++ b/tests/test_minimate_monitor_status.py @@ -0,0 +1,63 @@ +"""SUB 0x1C monitor status, decoded from BE9441's own replies (2026-10-26). + +Recorded by the bridge (bridges/captures/10-26-26 - BE9441 blastware histogram/): + +* 16:24:54, idle, before the histogram run: flag 0x00. +* 18:47:41, during a histogram run Brian confirmed at the unit: flag **0x0B**. Only + 0x10 had ever been measured as "monitoring" (BE11529, 2026-04-08), so the status + read "not monitoring" and the change path refused the state as unknown. +* The same 18:47 reply's free memory, 0x000D0E04, holds a 04. The unit escapes it + (10 04) and the parser keeps both bytes, so the fields counted from the end came + out one byte off: 419.84 V and 251,655,168 bytes, against 6.76 V and 983,028. +""" +from __future__ import annotations + +import os +import sys + +import pytest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from minimateplus.client import _decode_monitor_status # noqa: E402 +from minimateplus.framing import S3FrameParser # noqa: E402 +from sfm.device_access.s3 import s3_run_state # noqa: E402 + +IDLE_1624 = bytes.fromhex( + "1002001010e300002c0000000000000000000000001a0a07ea0010101836000000000000010107cb00070000010107cb0013000000001002a4000efff4000e0de4fa03") +MONITORING_1847 = bytes.fromhex( + "1002001010e300002c00000000000000000000000b1a0a07ea00122f29000000000000010107cb00070000010107cb0013000000001002a4000efff4000d0e10043103") + + +def _status(wire: bytes): + frames = S3FrameParser().feed(wire) + assert len(frames) == 1 and frames[0].sub == 0xE3 + return _decode_monitor_status(frames[0].data) + + +def test_idle_reply(): + st = _status(IDLE_1624) + assert (st.state_flag, st.is_monitoring, s3_run_state(st)) == (0x00, False, "idle") + assert (st.battery_v, st.memory_total, st.memory_free) == (6.76, 983028, 921060) + + +def test_a_histogram_run_reads_monitoring(): + st = _status(MONITORING_1847) + assert (st.state_flag, st.is_monitoring, s3_run_state(st)) == (0x0B, True, "monitoring") + + +def test_an_escaped_byte_in_the_memory_field_does_not_shift_the_fields(): + st = _status(MONITORING_1847) + assert (st.battery_v, st.memory_total, st.memory_free) == (6.76, 983028, 855556) + + +@pytest.mark.parametrize("value", [0x02, 0x03, 0x04, 0x10]) +def test_every_escaped_value_in_the_last_ten_bytes_decodes(value): + # free memory ending in each escaped value, encoded as the unit does + st0 = _status(IDLE_1624) + # the reply's last data byte (free memory's low byte, 0xE4) swapped for `value` + assert IDLE_1624[-3] == 0xE4 + frames = S3FrameParser().feed(IDLE_1624[:-3] + bytes([0x10, value]) + IDLE_1624[-2:]) + st = _decode_monitor_status(frames[0].data) + assert st.memory_free == (st0.memory_free & ~0xFF) | value + assert (st.battery_v, st.memory_total) == (6.76, 983028)