From 71bd14156d254141834aa905980383e1292b2976 Mon Sep 17 00:00:00 2001 From: serversdown Date: Tue, 1 Dec 2026 01:08:36 -0500 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1 --- CLAUDE.md | 11 +++- docs/instantel_protocol_reference.md | 1 + minimateplus/client.py | 77 +++++++++++++---------- tests/test_s3_compliance_anchor_fields.py | 36 +++++++++++ 4 files changed, 90 insertions(+), 35 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index d130e45..c62c01a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -225,6 +225,9 @@ Read this first when picking the project back up. are re-rendered by keypad saves and are not identifiers. โš  **`0x1A` reads the keypad's edit buffer live** โ€” unsaved values show while someone is in the menu; a discard reverts them. + โœ… **Fixed 2026-12-01 (#48):** the legacy decode now finds the block by its + length field N = 2090 and reads by position; no DL2 event was ever recorded in + Auto stop mode (70,041 / 70,041 Fixed). Kept below as history: ๐Ÿ”ถ **Possible Series III bug, inferred only:** S3's 6-byte "stable anchor" `be 80 00 00 00 00` sits exactly where Series IV keeps pre-trigger (โˆ’0.25 s) + stop mode (Fixed). An S3 unit in Auto stop mode would then lose the @@ -1660,6 +1663,12 @@ frame D's channel block (trigger_level_geo, alarm_level_geo, max_range_geo) unre producing only ~1071 bytes instead of ~2126. ### SUB 1A โ€” anchor search range +> โš  **RETRACTED 2026-12-01 (#48):** there is no stable anchor. `be 80 00 00 00 00` +> is the block's `[12:18]`: the pre-trigger (โˆ’0.25 s) and the stop mode (Fixed). +> `_decode_compliance_config_into()` now unescapes the reply, finds the block by +> its length field N = 2090 (`08 2a`, at unescaped[12]) and reads mode `[5]`, +> rate `[6:8]`, interval `[8:10]` and record time `[18:22]` by position. The text +> below is history. `_decode_compliance_config_into()` locates fields via the **6-byte stable anchor** `b'\xbe\x80\x00\x00\x00\x00'`. Search range is `cfg[0:150]`. @@ -1731,7 +1740,7 @@ Do NOT use fixed absolute offsets for sample_rate or record_time. | setup_name | ASCII, null-padded, in cfg body | | project / client / operator / sensor_location | ASCII, label-value pairs | -**True stable anchor: `b'\xbe\x80\x00\x00\x00\x00'` (6-byte suffix), search `cfg[0:150]`.** +**True stable anchor: `b'\xbe\x80\x00\x00\x00\x00'` (6-byte suffix), search `cfg[0:150]`.** (โš  Retracted 2026-12-01, #48: not a constant; see "SUB 1A โ€” anchor search range".) The old "10-byte anchor" `b'\x01\x2c\x00\x00\xbe\x80\x00\x00\x00\x00'` is partially variable: bytes `\x01\x2c` = 300 (5-minute default histogram interval); changes when interval changes. diff --git a/docs/instantel_protocol_reference.md b/docs/instantel_protocol_reference.md index 7f38019..89f74f5 100644 --- a/docs/instantel_protocol_reference.md +++ b/docs/instantel_protocol_reference.md @@ -11,6 +11,7 @@ | Date | Section | Change | |---|---|---| +| 2026-12-01 | ยง7.6.1, SUB 1A (legacy decode) | โœ… **#48 fixed: the settings are read by position, not by the "stable anchor".** `_decode_compliance_config_into` unescapes the 1A reply, finds the block by its own length field N = 2090 (`08 2a`, at unescaped[12] on all 4 recorded replies), and reads mode `[5]`, sample rate `[6:8]`, histogram interval `[8:10]` and record time `[18:22]` by position: Series IV's `0x1A` layout, as the 2026-11-03 (5) row measured. The anchor `be 80 00 00 00 00` was `[12:18]`, the pre-trigger and the stop mode, so a unit in Auto stop mode would have read all four as None. **How often it could strike (measured 2026-12-01):** every DL2 event file's setup copy has stop mode Fixed, 70,041 / 70,041 (63,500 histograms, 6,541 waveforms), and Blastware's reports agree (7,631 / 7,631 `RecordStopMode` Fixed). Tested on recorded replies patched to Auto stop and to a โˆ’0.5 s pre-trigger (`tests/test_s3_compliance_anchor_fields.py`); not yet on a unit in Auto stop. Resolves the 2026-10-17 row below. | | 2026-11-13 | Appendix D.4 (rewritten), D.4.1โ€“D.4.4 | โœ… **The `.MLG` format, measured.** An 18-byte prefix, then 292-byte records `[length 290 LE][id 0e a0 header / 0e 80 log][โ€ฆ][00 00][CRC]`. The CRC is **CRC-16/AUG-CCITT over `[0:288]`, little-endian** (3,329 / 3,329 records, 1,282 files). Fields: times with the year LE, the monitor-log flag word and trigger source LE, the serial, Blastware's text. Also: one file per call-in, how sessions appear, and a Modem Exit that is not a file's last record was a call that left no file. Cross-checked against DL2 event footers: 785 / 785 for BE8081. The old D.4.1โ€“D.4.3 (308-byte header, CRC at the start) are kept, marked retracted. Reader/writer `minimateplus/mlg_file.py`. | | 2026-11-11 | Appendix D.3.3 | โœ… **The footer's last two bytes are a CRC-16/AUG-CCITT** (init `0x1D0F`), big-endian, over the footer record from its length prefix `00 1a` through footer `[0:22]`: 70,041 / 70,041 DL2 files. Found by the `.MLG` session, verified here. Retracts 2026-11-08's "no common CRC-16 fits": every attempt had started at the footer itself. Same CRC family as the Series IV `0x1A` block's and the `.MLG` record's. | | 2026-11-09 | Appendix D.6.15 (NEW), D.6.6, D.6.11 | โœ… **Over range and the mic.** **D.6.15:** a peak past full scale (2,000 units = 32,000 counts; the mic in counts) is Blastware's `OORANGE`, per channel and per histogram interval, from the unit's own summary peaks. The vector sum is `OORANGE` exactly when a geophone is (43 / 43); never on its own (38 / 38 numeric), which retracts D.6.11's open "vector sum alone" case. The `` line marks the mic over (18 / 18). **D.6.6:** the mic has its own range byte (label + 29): at 1 a count is an eighth, 7 DL2 events SFM read 8ร— high. The dB offset is **81.938216**: [81.9383, 81.9385] was too high, and every count Blastware printed leaves [81.9382058, 81.9382266]. Blastware's mic unit labels do not hold on every report; some mix dB and psi in one column. **80 of 88 report fields now match on every DL2 event.** | diff --git a/minimateplus/client.py b/minimateplus/client.py index e8b36a9..4b3afa4 100644 --- a/minimateplus/client.py +++ b/minimateplus/client.py @@ -55,6 +55,7 @@ from .protocol import ( SUB_WRITE_CONFIRM_C, SUB_TRIGGER_CONFIRM, SUB_MONITOR_STATUS, + COMPLIANCE_CONTENT_LEN, ) from .transport import SerialTransport, BaseTransport @@ -2012,6 +2013,9 @@ def _extract_project_strings(data: bytes) -> Optional[ProjectInfo]: ) +# The settings block starts with its own length N = 2090, uint16 BE (#48). +_SETTINGS_BLOCK_HEAD = COMPLIANCE_CONTENT_LEN.to_bytes(2, "big") # 08 2a +_SETTINGS_BLOCK_SEARCH = 40 # found at unescaped[12] on every recorded reply def _decode_compliance_config_into(data: bytes, info: DeviceInfo) -> None: @@ -2023,20 +2027,20 @@ def _decode_compliance_config_into(data: bytes, info: DeviceInfo) -> None: Confirmed field locations (BE11529 with 3-step read, duplicate detection): - cfg[89] = setup_name: first long ASCII string in cfg[40:250] โœ… - - ANCHOR = b'\\xbe\\x80\\x00\\x00\\x00\\x00' in cfg[0:150] โœ… (revised 2026-04-07) - - anchor - 6 = sample_rate uint16_BE (1024 normal / 2048 fast / 4096 faster) - - anchor + 6 = record_time float32_BE + - the settings block, found by its length field N = 2090 (`08 2a`) after + unescaping, read by position (#48, 2026-12-01; see the comment in the body): + mode [5], sample rate [6:8], histogram interval [8:10], record time [18:22] - "Project:" needle โ†’ project string - "Client:" needle โ†’ client string - "User Name:" needle โ†’ operator string - "Seis Loc:" needle โ†’ sensor_location string - "Extended Notes" needle โ†’ notes string - Anchor approach is required because a DLE byte in the sample_rate field - (4096 = 0x1000 โ†’ stored as 10 10 00 in raw S3 frame โ†’ unstuffed to 10 00, - 1 byte shorter than 04 00 or 08 00) causes frame C to be 1 byte shorter - for "faster" mode, shifting all subsequent offsets by 1. The 10-byte - anchor is stable across all modes. + โš  Retracted 2026-12-01 (#48): the mode, rate, interval and record time were + found by the "anchor" `be 80 00 00 00 00`, thought stable across all modes. + It is the pre-trigger and the stop mode, so it is absent in Auto stop mode. + Escapes are removed first (unescape_reply), so an escaped byte no longer + shifts the positions. Channel block layout (โœ… confirmed 2026-04-02 from 3-11-26 E5 frame 78 and 1-2-26 A5 frame 77): @@ -2076,47 +2080,52 @@ def _decode_compliance_config_into(data: bytes, info: DeviceInfo) -> None: # 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). # - # 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 block is the settings block Series 4's 0x1A shares (reference D.6.2, + # micromate/setup_block.py), found by its own length field N = 2090 (`08 2a`) + # and read by position -- at unescaped[12] on every recorded reply: + # [5] recording_mode (uint8) + # [6:8] sample_rate (uint16 BE) + # [8:10] histogram_interval_sec (uint16 BE, seconds) + # [12:16] pre-trigger (float32 BE, negative seconds) + # [17] stop mode (0 Fixed) + # [18:22] record_time (float32 BE) + # โš  RETRACTED 2026-12-01 (#48): these were found by the "stable anchor" + # `be 80 00 00 00 00`, which is [12:18] -- the pre-trigger (-0.25 s) and the + # stop mode (Fixed), not a marker. A unit in Auto stop mode, or with another + # pre-trigger, has no such bytes, and all four fields read None. No DL2 event + # was recorded in Auto (70,041 / 70,041 Fixed), so it never struck. # โš  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' + # unescaped. A reply with no settings block (BE9441 answered 1A with its + # device-info block while monitoring, 2026-10-29) leaves all four fields None. logical = unescape_reply(data) - _anchor = logical.find(_ANCHOR, 0, 150) + _block = logical.find(_SETTINGS_BLOCK_HEAD, 0, _SETTINGS_BLOCK_SEARCH) - if _anchor < 0: + if _block < 0: log.warning( - "compliance_config: anchor %s not found in cfg[0:150] (len=%d) " + "compliance_config: no settings block (N = 2090) in cfg[0:%d] (len=%d) " "โ€” sample_rate, record_time and recording_mode will be None", - _ANCHOR.hex(), len(data), + _SETTINGS_BLOCK_SEARCH, 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] + elif _block + 22 <= len(logical): + log.info("_decode_compliance_config_into: settings block at unescaped[%d] data_len=%d", + _block, len(data)) + config.recording_mode = logical[_block + 5] + config.sample_rate = struct.unpack_from(">H", logical, _block + 6)[0] + config.histogram_interval_sec = struct.unpack_from(">H", logical, _block + 8)[0] + config.record_time = struct.unpack_from(">f", logical, _block + 18)[0] log.debug( "compliance_config: recording_mode=0x%02X sample_rate=%d histogram_interval=%d s " - "record_time=%.3f s (anchor@%d unescaped)", + "record_time=%.3f s (block@%d unescaped)", config.recording_mode, config.sample_rate, config.histogram_interval_sec, - config.record_time, _anchor, + config.record_time, _block, ) else: - log.warning("compliance_config: anchor at unescaped[%d] is too close to an end " + log.warning("compliance_config: settings block at unescaped[%d] is too close to the end " "(len=%d) โ€” mode, rate, interval and record time left None", - _anchor, len(logical)) + _block, len(logical)) # โ”€โ”€ Project strings โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ try: diff --git a/tests/test_s3_compliance_anchor_fields.py b/tests/test_s3_compliance_anchor_fields.py index a7b0389..d628d73 100644 --- a/tests/test_s3_compliance_anchor_fields.py +++ b/tests/test_s3_compliance_anchor_fields.py @@ -98,3 +98,39 @@ def test_a_reply_without_the_settings_block_reads_as_unknown(): # 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]