diff --git a/minimateplus/protocol.py b/minimateplus/protocol.py index 47722a4..50c7969 100644 --- a/minimateplus/protocol.py +++ b/minimateplus/protocol.py @@ -185,14 +185,26 @@ class ImageRefused(ValueError): # tests/test_s3_settings_read.py. COMPLIANCE_CONTENT_LEN = 2090 +# SFM's 1A read: (offset = the content length asked for, page) per data frame. +_COMPLIANCE_READ_STEPS = ((0x0400, 0x00), (0x0400, 0x04), (0x002A, 0x08)) -def assemble_compliance_content(frames: "list[bytes]") -> bytes: +def assemble_compliance_content(frames: "list[bytes]", lengths: "Optional[tuple]" = None) -> bytes: """Join a 1A read's data frames (each S3Frame.data, kept escapes and all, in order) into the logical settings block. Judges nothing: a monitoring unit's - device-info answer assembles too -- `is_compliance_content` tells them apart.""" + device-info answer assembles too -- `is_compliance_content` tells them apart. + + `lengths`: the content each frame was asked for (SFM's read: 1024, 1024, 42). + Each frame then gives exactly that. Measured at the bench 2026-11-04 (BE9441): + when a frame's checksum byte is itself escaped (02 / 03 / 04), the parser keeps + the escape's 0x10 and strips only the byte after it, so the frame arrives one + byte long, ending in a lone 0x10. That one byte is dropped; any other leftover + is kept, and the block then fails is_compliance_content.""" parts = [unescape_reply(bytes(f))[11:] for f in frames] - return b"".join(p[1:] for p in parts[:-1]) + (parts[-1] if parts else b"") + parts = [p[1:] for p in parts[:-1]] + parts[-1:] + if lengths is not None and len(lengths) == len(parts): + parts = [p[:n] if p[n:] == b"\x10" else p for p, n in zip(parts, lengths)] + return b"".join(parts) def is_compliance_content(block: bytes) -> bool: @@ -1074,11 +1086,11 @@ class MiniMateProtocol: self._send(build_bw_frame(SUB_COMPLIANCE, 0, bytes([0, 0, 0, 0, 0, 0, 0, 0x64, 0, 0]))) self._recv_one(expected_sub=rsp_sub) frames = [] - for offset, page in ((0x0400, 0x00), (0x0400, 0x04), (0x002A, 0x08)): + for offset, page in _COMPLIANCE_READ_STEPS: self._send(build_bw_frame(SUB_COMPLIANCE, offset, bytes([0, 0, page, 0, 0, 0, 0, 0x64, 0, 0]))) frames.append(self._recv_one(expected_sub=rsp_sub).data) - return assemble_compliance_content(frames) + return assemble_compliance_content(frames, lengths=tuple(o for o, _ in _COMPLIANCE_READ_STEPS)) # ── Write commands (SUBs 68–83) ─────────────────────────────────────────── diff --git a/tests/test_s3_settings_read.py b/tests/test_s3_settings_read.py index 11116ac..1f364b9 100644 --- a/tests/test_s3_settings_read.py +++ b/tests/test_s3_settings_read.py @@ -99,3 +99,25 @@ def test_read_compliance_content_refuses_a_missing_frame(): with pytest.raises(mp.ProtocolError): proto.read_compliance_content() assert len(t.sent) == 3 # probe, B, C -- and no D after C went unanswered + + +# Bench 2026-11-04 (BE9441, live): frame B of a 1A read came back one byte long, +# ending in a lone 0x10 -- the kept escape of a checksum byte that was itself +# escaped (02 / 03 / 04: about one frame in 85); the parser strips only the byte +# after it. The 12 recorded reads never hit it. The read knows how much it asked +# for, so it takes exactly that; a leftover other than that one 0x10 is kept, and +# the block then fails is_compliance_content. + +def test_a_frame_with_an_escaped_checksums_stray_byte_is_trimmed_to_what_was_asked(): + sfm_read = READS[8] # SFM's own 1024 + 1024 + 42 pattern + good = assemble_compliance_content(sfm_read, lengths=(1024, 1024, 42)) + assert good == assemble_compliance_content(sfm_read) and is_compliance_content(good) + stray = [sfm_read[0] + b"\x10", sfm_read[1], sfm_read[2]] + assert len(assemble_compliance_content(stray)) == 2091 # what the bench saw + assert assemble_compliance_content(stray, lengths=(1024, 1024, 42)) == good + + +def test_a_leftover_that_is_not_the_stray_escape_is_not_trimmed(): + sfm_read = READS[8] + odd = [sfm_read[0] + b"\x55", sfm_read[1], sfm_read[2]] + assert not is_compliance_content(assemble_compliance_content(odd, lengths=(1024, 1024, 42)))