fix(minimateplus): MiniMate config writes are ordinary escaped requests carrying whole blocks; apply_config reads the unit's own trigger block (#46)
Measured against every Blastware request on record: the uploaded Feb-Apr 2026
captures (3,869 frames, 3,869 re-encode exactly) and BE9441's two sends on
2026-10-26 (26 write frames). A write is the standard 16-byte request plus its
data, every 02/03/04/10 escaped, SUM8 checksum; the length field is the data's
length. A config block starts with its own length N and is written as N + 2
bytes: event index (68) and trigger (82) as read plus 02 00 / 00 00 (constant on
15 sends), waveform data (69) and the 2,092-byte compliance image (71) whole.
The compliance image goes in addressed chunks (params[2:4] = address): 512 bytes
tonight (Blastware 10.74), 1,024 in March.
- framing.build_bw_write_frame: uniform escaping, SUM8, length = len(data).
RETRACTED: 'only BW_CMD doubled', the 'DLE-aware checksum', 'offset = data[1]
+ 2' -- fitted to frames still holding their escape bytes ('data 91, offset 90'
was 90 bytes and one escape). It got 7 of every 13 frames in a write wrong.
- protocol.write_image(): unescapes (callers may pass the parser's form), adds
the trailing two bytes to an N-byte block, and REFUSES anything else before a
byte is sent -- an edit beside a kept escape byte (the old encoder's anc-9
hazard) leaves an image one byte long.
- write_compliance_config_raw: 512-byte addressed chunks, then confirm 72.
- read_event_index / read_waveform_data_raw unescape before slicing (a block
holding 02/03/04/10 came out short); new read_trigger_config (SUB 0x2E).
- apply_config reads the unit's trigger block. RETRACTED: the block
'hardcoded from 3-11-26 -- no read exists'. Blastware reads it; byte 5 has
been 01 / 03 / 04 by unit and session, and the March copy (03) would have
changed BE9441's (01).
- The call-home write (7E) goes through write_image: its 127-byte 'read + 00 00'
is the 126-byte image (N = 0x7C). No Blastware 7E is on record here -- that
one is inferred from the other 77 write frames.
Tests: tonight's 26 and every Feb-Apr write frame rebuilt byte for byte; each
block as read written exactly as Blastware wrote it; the parser's form; the
stray-byte refusal; the reads against the unit's recorded replies; apply_config
writes BE9441's own trigger block. Six mutants, each caught. The old
test_write_frames.py (25 tests that had been skipping) runs now that the
captures are here, rewritten to the measured model.
Not yet sent to a unit: a config push changes settings, so its first live use
is a bench session with Brian. No codec, store or DB change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
a7f581f66a
commit
b5037b0b1e
5 files changed
+436
-702
No files matched your search
+93
-205
@@ -33,6 +33,7 @@ from .framing import (
|
||||
waveform_key_params,
|
||||
token_params,
|
||||
parse_strt_end_key,
|
||||
unescape_reply,
|
||||
build_5a_request,
|
||||
POLL_PROBE,
|
||||
POLL_DATA,
|
||||
@@ -130,6 +131,40 @@ _BULK_COUNTER_STEP = 0x0200 # bytes per chunk read, and the step between addre
|
||||
# claim past this is a misread STRT, refused before walking megabytes of garbage.
|
||||
_BULK_MAX_EVENT_BYTES = 0x400000 # 4 MiB
|
||||
|
||||
# Config writes (issue #46, measured on every Blastware write since February 2026).
|
||||
_COMPLIANCE_CHUNK = 0x0200
|
||||
# The two bytes after a block's N header bytes, when the read returned only N:
|
||||
# Blastware's own, constant on record -- event index 02 00, trigger 00 00. The
|
||||
# unit evidently does not check them (they are no CRC, and the compliance
|
||||
# image's went out stale, unchanged across two different sends).
|
||||
_WRITE_TAIL = {SUB_EVENT_INDEX_WRITE: b"\x02\x00", SUB_TRIGGER_CONFIG_WRITE: b"\x00\x00",
|
||||
SUB_WAVEFORM_DATA_WRITE: b"\x00\x00", SUB_COMPLIANCE_WRITE: b"\x00\x00",
|
||||
SUB_CALL_HOME_WRITE: b"\x00\x00"}
|
||||
|
||||
|
||||
def write_image(sub: int, data: bytes) -> bytes:
|
||||
"""The whole block a write sends: `data` unescaped, N + 2 bytes.
|
||||
|
||||
A config block starts with its own length N (uint16 BE) and is written as N
|
||||
bytes plus two -- on every Blastware write on record. `data` may be the
|
||||
block as a read returned it (N bytes, or N + 2) and may still hold the
|
||||
parser's kept escape bytes (10 02 / 10 03 / 10 04), which come out here.
|
||||
Anything else is refused before a byte is sent: a block one byte longer than
|
||||
its header says is what an edit beside a kept escape byte leaves behind.
|
||||
"""
|
||||
image = unescape_reply(bytes(data))
|
||||
if len(image) < 2:
|
||||
raise ValueError(f"write SUB 0x{sub:02X}: {len(image)} bytes is not a whole block")
|
||||
n = int.from_bytes(image[:2], "big")
|
||||
if len(image) == n:
|
||||
image += _WRITE_TAIL[sub]
|
||||
elif len(image) != n + 2:
|
||||
raise ValueError(
|
||||
f"write SUB 0x{sub:02X}: {len(image)} bytes, but its header says {n} (+ 2) -- "
|
||||
"not a whole block (a stray byte?); nothing sent")
|
||||
return image
|
||||
|
||||
|
||||
# Default timeout values (seconds).
|
||||
# MiniMate Plus is a slow device — keep these generous.
|
||||
DEFAULT_RECV_TIMEOUT = 10.0
|
||||
@@ -383,7 +418,9 @@ class MiniMateProtocol:
|
||||
self._send(build_bw_frame(SUB_EVENT_INDEX, length))
|
||||
data_rsp = self._recv_one(expected_sub=rsp_sub)
|
||||
|
||||
raw = data_rsp.data[11 : 11 + length]
|
||||
# Unescaped first (2026-10-27): a 02/03/04 inside the block arrives as
|
||||
# 10 0X and the parser keeps both, so a slice by length came up short.
|
||||
raw = unescape_reply(data_rsp.data)[11 : 11 + length]
|
||||
log.debug("read_event_index: got %d bytes", len(raw))
|
||||
return raw
|
||||
|
||||
@@ -515,7 +552,7 @@ class MiniMateProtocol:
|
||||
self._send(build_bw_frame(SUB_WAVEFORM_DATA, length))
|
||||
data_rsp = self._recv_one(expected_sub=rsp_sub)
|
||||
|
||||
raw = data_rsp.data[11:]
|
||||
raw = unescape_reply(data_rsp.data)[11:] # unescaped, as read_event_index
|
||||
log.debug("read_waveform_data_raw: got %d bytes", len(raw))
|
||||
return raw
|
||||
|
||||
@@ -1013,208 +1050,66 @@ class MiniMateProtocol:
|
||||
self._send(frame)
|
||||
return self.recv_write_ack(expected_sub=rsp_sub)
|
||||
|
||||
def write_event_index(self, data: bytes) -> S3Frame:
|
||||
"""
|
||||
Send a SUB 68 (EVENT_INDEX_WRITE) frame and await the confirm ack (SUB 97).
|
||||
|
||||
Offset formula: data[1] + 2 — confirmed from 3-11-26 BW TX capture frame 102.
|
||||
The write payload has a 2-byte header [0x00][length] where data[1] encodes
|
||||
the length of the meaningful payload; offset = data[1] + 2.
|
||||
|
||||
Example from capture:
|
||||
data[0:4] = 00 58 09 00 (data[1]=0x58=88 → offset=0x5A=90)
|
||||
data length = 91, offset = 90
|
||||
|
||||
Write sequence fragment:
|
||||
68 (data) → device acks with SUB 0x97
|
||||
73 (confirm) → device acks with SUB 0x8C
|
||||
|
||||
Callers should call write_confirm(SUB_WRITE_CONFIRM_B) after this.
|
||||
|
||||
Args:
|
||||
data: Raw event-index payload bytes to write to the device.
|
||||
Must be at least 2 bytes. data[1] must contain the length field.
|
||||
|
||||
Returns:
|
||||
The SUB 0x97 ack frame.
|
||||
|
||||
Raises:
|
||||
ProtocolError: on timeout or wrong response SUB.
|
||||
ValueError: if data is shorter than 2 bytes.
|
||||
"""
|
||||
if len(data) < 2:
|
||||
raise ValueError(f"event index write data must be at least 2 bytes, got {len(data)}")
|
||||
rsp_sub = _expected_rsp_sub(SUB_EVENT_INDEX_WRITE) # 0xFF - 0x68 = 0x97
|
||||
offset = data[1] + 2
|
||||
frame = build_bw_write_frame(SUB_EVENT_INDEX_WRITE, data, offset=offset)
|
||||
log.debug(
|
||||
"write_event_index: %d bytes data[1]=0x%02X offset=0x%04X rsp_sub=0x%02X",
|
||||
len(data), data[1], offset, rsp_sub,
|
||||
)
|
||||
def _write_block(self, sub: int, data: bytes, params: bytes = bytes(10)) -> S3Frame:
|
||||
"""Send one config block as a write and return its ack (0xFF - sub)."""
|
||||
image = write_image(sub, data)
|
||||
frame = build_bw_write_frame(sub, image, params=params)
|
||||
log.debug("write SUB=0x%02X: %d-byte image", sub, len(image))
|
||||
self._send(frame)
|
||||
return self.recv_write_ack(expected_sub=rsp_sub)
|
||||
return self.recv_write_ack(expected_sub=_expected_rsp_sub(sub))
|
||||
|
||||
def write_event_index(self, data: bytes) -> S3Frame:
|
||||
"""SUB 68: write the event-index block (as SUB 08 read it). Ack 0x97;
|
||||
callers follow with write_confirm(SUB_WRITE_CONFIRM_B). See write_image."""
|
||||
return self._write_block(SUB_EVENT_INDEX_WRITE, data)
|
||||
|
||||
def write_waveform_data(self, data: bytes) -> S3Frame:
|
||||
"""
|
||||
Send a SUB 69 (WAVEFORM_DATA_WRITE) frame and await the confirm ack (SUB 96).
|
||||
|
||||
Offset formula: data[1] + 2 — same pattern as write_event_index().
|
||||
Confirmed from 3-11-26 BW TX capture frame 110:
|
||||
data[0:4] = 00 c8 08 00 (data[1]=0xC8=200 → offset=0xCA=202)
|
||||
data length = 204, offset = 202
|
||||
|
||||
Write sequence fragment:
|
||||
69 (data) → device acks with SUB 0x96
|
||||
74 (confirm) → device acks with SUB 0x8B
|
||||
72 (confirm) → device acks with SUB 0x8D
|
||||
|
||||
Callers should call write_confirm(SUB_WRITE_CONFIRM_C) then
|
||||
write_confirm(SUB_WRITE_CONFIRM_A) after this.
|
||||
|
||||
Args:
|
||||
data: Raw waveform-data payload bytes to write.
|
||||
Must be at least 2 bytes. data[1] must contain the length field.
|
||||
|
||||
Returns:
|
||||
The SUB 0x96 ack frame.
|
||||
|
||||
Raises:
|
||||
ProtocolError: on timeout or wrong response SUB.
|
||||
ValueError: if data is shorter than 2 bytes.
|
||||
"""
|
||||
if len(data) < 2:
|
||||
raise ValueError(f"waveform data write payload must be at least 2 bytes, got {len(data)}")
|
||||
rsp_sub = _expected_rsp_sub(SUB_WAVEFORM_DATA_WRITE) # 0xFF - 0x69 = 0x96
|
||||
offset = data[1] + 2
|
||||
frame = build_bw_write_frame(SUB_WAVEFORM_DATA_WRITE, data, offset=offset)
|
||||
log.debug(
|
||||
"write_waveform_data: %d bytes data[1]=0x%02X offset=0x%04X rsp_sub=0x%02X",
|
||||
len(data), data[1], offset, rsp_sub,
|
||||
)
|
||||
self._send(frame)
|
||||
return self.recv_write_ack(expected_sub=rsp_sub)
|
||||
"""SUB 69: write the waveform-data block (as SUB 09 read it). Ack 0x96;
|
||||
callers follow with write_confirm C then A. See write_image."""
|
||||
return self._write_block(SUB_WAVEFORM_DATA_WRITE, data)
|
||||
|
||||
def write_compliance_config_raw(self, data: bytes) -> None:
|
||||
"""SUB 71: write the compliance image in addressed chunks, then confirm (72).
|
||||
|
||||
Measured: the image is 2,092 bytes (its header N = 2,090, plus 2), sent as
|
||||
512-byte chunks with the chunk's address at params[2:4] -- 0x0000, 0x0200,
|
||||
0x0400, 0x0600, then the 44-byte rest at 0x0800 -- each acked 0x8E, then
|
||||
SUB 72 (ack 0x8D). Blastware 10.74 on BE9441 (2026-10-26), byte for byte.
|
||||
March 2026's Blastware sent 1,024-byte chunks the same way; the units took
|
||||
both. (Retracted: "exactly 3 chunks, offset 0x1004, params 00 00 00 10 04"
|
||||
-- the length 0x0400 and address 0x0400, read with their escape bytes.)
|
||||
"""
|
||||
Send the SUB 71 (COMPLIANCE_WRITE) 3-chunk sequence and final confirm.
|
||||
|
||||
The full compliance config payload (~2128 bytes) is split into exactly 3
|
||||
chunks with hardcoded boundaries and params confirmed from the 3-11-26 BW
|
||||
TX capture (frames 104–108):
|
||||
|
||||
Chunk 1 — first 1027 bytes:
|
||||
offset=0x1004 params=bytes(10)
|
||||
device acks SUB 0x8E
|
||||
|
||||
Chunk 2 — next 1055 bytes:
|
||||
offset=0x1004 params=b'\\x00\\x00\\x00\\x00\\x10\\x04' + b'\\x00'*4
|
||||
device acks SUB 0x8E
|
||||
|
||||
Chunk 3 — remaining bytes:
|
||||
offset=0x002C params=b'\\x00\\x00\\x08' + b'\\x00'*7
|
||||
device acks SUB 0x8E
|
||||
|
||||
Confirm — SUB 72 (zero data):
|
||||
device acks SUB 0x8D
|
||||
|
||||
The total write payload should be at least 1027+1055=2082 bytes; chunk 3
|
||||
carries everything after offset 2082 (typically ~46 bytes for a 2128-byte
|
||||
config).
|
||||
|
||||
Args:
|
||||
data: Raw compliance config bytes to write. Must be at least 2082 bytes.
|
||||
|
||||
Raises:
|
||||
ValueError: if data is too short to fill chunks 1 and 2.
|
||||
ProtocolError: on timeout or wrong response SUB from any chunk.
|
||||
"""
|
||||
_CHUNK1_SIZE = 1027
|
||||
_CHUNK2_SIZE = 1055
|
||||
_CHUNK1_OFFSET = 0x1004
|
||||
_CHUNK2_OFFSET = 0x1004
|
||||
_CHUNK3_OFFSET = 0x002C
|
||||
|
||||
_CHUNK1_PARAMS = bytes(10)
|
||||
_CHUNK2_PARAMS = bytes([0x00, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00])
|
||||
_CHUNK3_PARAMS = bytes([0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
|
||||
|
||||
min_size = _CHUNK1_SIZE + _CHUNK2_SIZE
|
||||
if len(data) < min_size:
|
||||
raise ValueError(
|
||||
f"Compliance write data too short: {len(data)} bytes, "
|
||||
f"need at least {min_size} (chunk1={_CHUNK1_SIZE} + chunk2={_CHUNK2_SIZE})"
|
||||
)
|
||||
|
||||
rsp_sub = _expected_rsp_sub(SUB_COMPLIANCE_WRITE) # 0xFF - 0x71 = 0x8E
|
||||
|
||||
chunk1 = data[:_CHUNK1_SIZE]
|
||||
chunk2 = data[_CHUNK1_SIZE : _CHUNK1_SIZE + _CHUNK2_SIZE]
|
||||
chunk3 = data[_CHUNK1_SIZE + _CHUNK2_SIZE :]
|
||||
|
||||
chunks = [
|
||||
(1, chunk1, _CHUNK1_OFFSET, _CHUNK1_PARAMS),
|
||||
(2, chunk2, _CHUNK2_OFFSET, _CHUNK2_PARAMS),
|
||||
(3, chunk3, _CHUNK3_OFFSET, _CHUNK3_PARAMS),
|
||||
]
|
||||
|
||||
for chunk_num, chunk_data, chunk_offset, chunk_params in chunks:
|
||||
frame = build_bw_write_frame(
|
||||
SUB_COMPLIANCE_WRITE,
|
||||
chunk_data,
|
||||
offset=chunk_offset,
|
||||
params=chunk_params,
|
||||
)
|
||||
log.debug(
|
||||
"write_compliance_config_raw: chunk %d %d bytes "
|
||||
"offset=0x%04X params=%s",
|
||||
chunk_num, len(chunk_data), chunk_offset, chunk_params.hex(),
|
||||
)
|
||||
image = write_image(SUB_COMPLIANCE_WRITE, data)
|
||||
rsp_sub = _expected_rsp_sub(SUB_COMPLIANCE_WRITE)
|
||||
for addr in range(0, len(image), _COMPLIANCE_CHUNK):
|
||||
params = bytes([0x00, 0x00, (addr >> 8) & 0xFF, addr & 0xFF]) + bytes(6)
|
||||
frame = build_bw_write_frame(SUB_COMPLIANCE_WRITE,
|
||||
image[addr:addr + _COMPLIANCE_CHUNK], params=params)
|
||||
log.debug("write_compliance_config_raw: chunk at 0x%04X", addr)
|
||||
self._send(frame)
|
||||
self.recv_write_ack(expected_sub=rsp_sub)
|
||||
log.debug("write_compliance_config_raw: chunk %d acked", chunk_num)
|
||||
|
||||
# Final confirm (SUB 72)
|
||||
log.debug("write_compliance_config_raw: sending confirm (SUB 0x72)")
|
||||
self.write_confirm(SUB_WRITE_CONFIRM_A)
|
||||
log.debug("write_compliance_config_raw: done")
|
||||
|
||||
def write_trigger_config(self, data: bytes) -> S3Frame:
|
||||
"""SUB 82: write the trigger block (as SUB 0x2E read it). Ack 0x7D;
|
||||
callers follow with write_confirm(SUB_TRIGGER_CONFIRM). See write_image."""
|
||||
return self._write_block(SUB_TRIGGER_CONFIG_WRITE, data)
|
||||
|
||||
def read_trigger_config(self) -> bytes:
|
||||
"""SUB 0x2E two-step read: the 26-byte trigger block that SUB 82 writes.
|
||||
|
||||
Blastware reads it before writing it (BE9441, 2026-10-26). Its byte 5
|
||||
differs by unit and session (01 / 03 / 04 on record) -- so a block copied
|
||||
from another unit, as apply_config once wrote, changes a setting.
|
||||
Returns the block unescaped (data[11:] after unescape_reply).
|
||||
"""
|
||||
Send a SUB 82 (TRIGGER_CONFIG_WRITE) frame and await the confirm ack (SUB 7D).
|
||||
|
||||
Offset formula: data[1] + 2 — same pattern as write_event_index().
|
||||
Confirmed from 3-11-26 BW TX capture frame 108:
|
||||
data[0:4] = 00 1a d5 00 (data[1]=0x1A=26 → offset=0x1C=28)
|
||||
data length = 29, offset = 28
|
||||
|
||||
Write sequence fragment:
|
||||
82 (data) → device acks with SUB 0x7D
|
||||
83 (confirm) → device acks with SUB 0x7C
|
||||
|
||||
Callers should call write_confirm(SUB_TRIGGER_CONFIRM) after this.
|
||||
|
||||
Args:
|
||||
data: Raw trigger-config payload bytes to write.
|
||||
Must be at least 2 bytes. data[1] must contain the length field.
|
||||
|
||||
Returns:
|
||||
The SUB 0x7D ack frame.
|
||||
|
||||
Raises:
|
||||
ProtocolError: on timeout or wrong response SUB.
|
||||
ValueError: if data is shorter than 2 bytes.
|
||||
"""
|
||||
if len(data) < 2:
|
||||
raise ValueError(f"trigger config write payload must be at least 2 bytes, got {len(data)}")
|
||||
rsp_sub = _expected_rsp_sub(SUB_TRIGGER_CONFIG_WRITE) # 0xFF - 0x82 = 0x7D
|
||||
offset = data[1] + 2
|
||||
frame = build_bw_write_frame(SUB_TRIGGER_CONFIG_WRITE, data, offset=offset)
|
||||
log.debug(
|
||||
"write_trigger_config: %d bytes data[1]=0x%02X offset=0x%04X rsp_sub=0x%02X",
|
||||
len(data), data[1], offset, rsp_sub,
|
||||
)
|
||||
self._send(frame)
|
||||
return self.recv_write_ack(expected_sub=rsp_sub)
|
||||
rsp_sub = _expected_rsp_sub(SUB_UNKNOWN_2E)
|
||||
length = DATA_LENGTHS[SUB_UNKNOWN_2E]
|
||||
self._send(build_bw_frame(SUB_UNKNOWN_2E, 0))
|
||||
self._recv_one(expected_sub=rsp_sub)
|
||||
self._send(build_bw_frame(SUB_UNKNOWN_2E, length))
|
||||
data_rsp = self._recv_one(expected_sub=rsp_sub)
|
||||
return unescape_reply(data_rsp.data)[11:11 + length]
|
||||
|
||||
# ── Call home config (SUBs 0x2C / 0x7E / 0x7F) ──────────────────────────
|
||||
|
||||
@@ -1278,20 +1173,13 @@ class MiniMateProtocol:
|
||||
ValueError: if data is not exactly 127 bytes or lacks expected header.
|
||||
ProtocolError: on timeout or wrong response SUB.
|
||||
"""
|
||||
if len(data) < 2:
|
||||
raise ValueError(f"call home write payload must be at least 2 bytes, got {len(data)}")
|
||||
rsp_sub_write = _expected_rsp_sub(SUB_CALL_HOME_WRITE) # 0xFF - 0x7E = 0x81
|
||||
rsp_sub_confirm = _expected_rsp_sub(SUB_CALL_HOME_CONFIRM) # 0xFF - 0x7F = 0x80
|
||||
|
||||
# Offset formula: data[1] + 2 (same pattern as other single-chunk writes)
|
||||
offset = data[1] + 2 # 0x7C + 2 = 0x7E = 126
|
||||
frame = build_bw_write_frame(SUB_CALL_HOME_WRITE, data, offset=offset)
|
||||
log.debug(
|
||||
"write_call_home_config: %d bytes data[1]=0x%02X offset=0x%04X",
|
||||
len(data), data[1], offset,
|
||||
)
|
||||
self._send(frame)
|
||||
self.recv_write_ack(expected_sub=rsp_sub_write)
|
||||
# 2026-10-27 (#46): the read payload holds the parser's kept escape bytes
|
||||
# (num_retries 3 arrives as 10 03); write_image removes them, so the
|
||||
# 127-byte "read + 00 00" goes out as its 126-byte image (N = 0x7C, + 2),
|
||||
# escaped like every request. No Blastware call-home write is on record
|
||||
# here to compare against -- inferred from the 77 other write frames.
|
||||
self._write_block(SUB_CALL_HOME_WRITE, data)
|
||||
log.debug("write_call_home_config: write acked; sending confirm 0x7F")
|
||||
|
||||
confirm_frame = build_bw_write_frame(SUB_CALL_HOME_CONFIRM, b"")
|
||||
|
||||
Reference in new issue
Block a user