From d73a27d2e37dc390e60c5851e0d7250528e65863 Mon Sep 17 00:00:00 2001 From: serversdown Date: Sun, 25 Oct 2026 18:17:09 -0400 Subject: [PATCH] fix(minimateplus): a MiniMate that boots on connect is waited for, not given up on BE9441 on a serial cable (bench, 2026-10-25) twice sent "Operating System" ~3 s after the port opened and ignored the POLL sent before it; the request then failed at the full receive timeout (502 after 15 s on /device/info, 504 after 120 s on a download). After a mid-download reboot it answered 8.8 s after its banner -- the one measured readiness point. startup()'s first POLL now stops waiting the moment the banner arrives (that POLL will never be answered) and polls again every 2 s for up to 20 s. Without a banner nothing changes: a silent unit fails at the normal timeout, with no re-polling. Only SESSION_RESET and POLL are sent, so this is safe in front of the change path too. No codec, store or DB change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44 --- minimateplus/protocol.py | 56 +++++++++++++++++-- tests/test_minimate_boot_on_connect.py | 77 ++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 tests/test_minimate_boot_on_connect.py diff --git a/minimateplus/protocol.py b/minimateplus/protocol.py index 509f235..9b7aadb 100644 --- a/minimateplus/protocol.py +++ b/minimateplus/protocol.py @@ -223,10 +223,9 @@ class MiniMateProtocol: log.debug("startup: POLL probe") self._send(POLL_PROBE) - probe_rsp = self._recv_one( - expected_sub=_expected_rsp_sub(SUB_POLL), - timeout=self._recv_timeout, - ) + probe_rsp = self._await_poll_or_boot(self._recv_timeout) + if probe_rsp is None: + probe_rsp = self._poll_until_booted() log.debug( "startup: POLL probe response page_key=0x%04X", probe_rsp.page_key ) @@ -1570,6 +1569,55 @@ class MiniMateProtocol: f"Expected SUB=0x{expected_sub:02X}, got 0x{frame.sub:02X}" ) + # A MiniMate whose port has just opened may boot: it sends this banner a few + # seconds later and ignores a POLL sent before it. Measured on BE9441 over a + # serial cable (2026-10-25): banner ~3 s after the port opened, twice; after a + # mid-download reboot it answered 8.8 s after the banner. + _BOOT_BANNER = b"Operating System" + _BOOT_GRACE_S = 20.0 + _BOOT_POLL_S = 2.0 + + def _await_poll_or_boot(self, timeout: float) -> Optional[S3Frame]: + """The first POLL probe's reply -- or None the moment the unit sends its + boot banner, because a POLL sent before the banner is never answered.""" + expected = _expected_rsp_sub(SUB_POLL) + deadline = time.monotonic() + timeout + tail = b"" + while time.monotonic() < deadline: + chunk = self._transport.read(256) + if not chunk: + time.sleep(0.005) + continue + log.debug("RX %d bytes: %s", len(chunk), chunk.hex()) + frames = self._parser.feed(chunk) + if frames: + self._pending_frames.extend(frames[1:]) + self._validate_frame(frames[0], expected) + return frames[0] + tail = (tail + chunk)[-64:] + if self._BOOT_BANNER in tail: + return None + raise TimeoutError( + f"No S3 frame received within {timeout:.1f}s (expected SUB 0x{expected:02X})") + + def _poll_until_booted(self) -> S3Frame: + """The unit said it is booting: POLL again until it answers. A read -- + nothing is ever sent but SESSION_RESET and POLL.""" + log.info("startup: the unit is booting (it sent %r) -- polling again until it answers", + self._BOOT_BANNER.decode()) + deadline = time.monotonic() + self._BOOT_GRACE_S + while time.monotonic() < deadline: + self._send(SESSION_RESET) + self._send(POLL_PROBE) + try: + return self._recv_one(expected_sub=_expected_rsp_sub(SUB_POLL), + timeout=self._BOOT_POLL_S) + except TimeoutError: + continue + raise TimeoutError( + f"the unit sent its boot message but did not answer a POLL within " + f"{self._BOOT_GRACE_S:.0f} s") + def _drain_boot_string(self, drain_ms: int = 200) -> None: """ Read and discard any boot-string bytes ("Operating System") the device diff --git a/tests/test_minimate_boot_on_connect.py b/tests/test_minimate_boot_on_connect.py new file mode 100644 index 0000000..010b4a9 --- /dev/null +++ b/tests/test_minimate_boot_on_connect.py @@ -0,0 +1,77 @@ +"""A MiniMate that boots when its port opens is waited for, not given up on. + +BE9441 on a serial cable (bench, 2026-10-25) twice sent its boot banner +("Operating System") ~3 s after the port opened and ignored the POLL sent +before it; the request then failed at the full receive timeout (15 s, or 120 s +on the download path). After a mid-download reboot it answered 8.8 s after its +banner. startup() now stops waiting on the moment it sees the banner and polls +again until the unit answers (up to 20 s); with no banner, nothing changes. +""" +from __future__ import annotations + +import os +import sys + +import pytest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +import minimateplus.client as mc # noqa: E402 +import minimateplus.protocol as mp # noqa: E402 +from minimate_replay import SessionReplay, fast_clock, recorded_replies # noqa: E402 + +BANNER = b"Operating System" + + +@pytest.fixture(autouse=True) +def _fast(monkeypatch): + fast_clock(monkeypatch) + + +class Booting(SessionReplay): + """Banner instead of an answer to the first POLL; then `deaf` more POLL probes + go unanswered (still booting); after that, the recorded session as normal.""" + + def __init__(self, replies, deaf=2, banner=True): + super().__init__(replies) + self.deaf, self.banner, self.polls = deaf, banner, 0 + + def write(self, data): + data = bytes(data) + if data[:2] == b"\x41\x02" and data[5] == 0x5B and data[8] == 0x00: # the POLL probe + self.polls += 1 + if self.polls == 1: + if self.banner: + self._buf += BANNER + return + if self.polls <= 1 + self.deaf: + return + super().write(data) + + +def connect(transport): + client = mc.MiniMateClient(transport=transport) + client.open() + return client, client.connect() + + +def test_a_unit_booting_on_connect_is_waited_for(): + t = Booting(recorded_replies(), deaf=2) + _client, info = connect(t) + assert info.serial == "BE9441" + assert t.polls == 4 # the lost one, two while booting, the answered one + + +def test_a_unit_that_boots_but_never_answers_says_so(): + t = Booting(recorded_replies(), deaf=10_000) + with pytest.raises(mp.TimeoutError, match="boot"): + connect(t) + + +def test_a_silent_unit_without_a_banner_fails_as_before(): + t = Booting(recorded_replies(), deaf=10_000, banner=False) + with pytest.raises(mp.TimeoutError) as e: + connect(t) + assert "boot" not in str(e.value) + assert t.polls == 1 # no re-polling without the banner