diff --git a/micromate/writes.py b/micromate/writes.py index 61bd3c2..71f2713 100644 --- a/micromate/writes.py +++ b/micromate/writes.py @@ -101,6 +101,13 @@ def plan_stop_monitoring() -> WritePlan: return WritePlan("stop monitoring", (WriteStep(0x97, _CONFIRM, what="stop monitoring"),)) +def is_call_home_content(block: bytes) -> bool: + """Whether 126 bytes are the 0x2C call-home content: its header is its own + length (00 7C, N = 124 -- 00 7C DC on every read on record, UM12947 and + UM20147). A bit is flipped only in a block that carries it.""" + return len(block) == _CALL_HOME_LEN and int.from_bytes(block[0:2], "big") == _CALL_HOME_LEN - 2 + + def plan_call_home_enabled(call_home: bytes, enabled: bool) -> WritePlan: """Read `0x2C`, flip bit 0 of content[5], send the rest back byte for byte. @@ -113,6 +120,9 @@ def plan_call_home_enabled(call_home: bytes, enabled: bool) -> WritePlan: if len(call_home) != _CALL_HOME_LEN: raise ValueError(f"call-home content must be {_CALL_HOME_LEN} B " f"(the size THOR's offset was seen at), got {len(call_home)}") + if not is_call_home_content(call_home): + raise ValueError(f"call-home content's header is {call_home[0:2].hex()}, not its own length " + f"(00 {_CALL_HOME_LEN - 2:02x}) -- not the call-home block; nothing built") b5 = (call_home[5] | 0x01) if enabled else (call_home[5] & 0xFE) data = call_home[:5] + bytes([b5]) + call_home[6:] what = "call-home " + ("enable" if enabled else "disable") diff --git a/sfm/device_access/s4.py b/sfm/device_access/s4.py index b0a881a..57b8f95 100644 --- a/sfm/device_access/s4.py +++ b/sfm/device_access/s4.py @@ -19,12 +19,14 @@ from typing import Any, Callable, Optional, TypeVar from micromate.client import MicromateClient from micromate.opcodes import OpcodeRefused from micromate.protocol import THOR_CHUNK_SIZE -from micromate.writes import plan_call_home_enabled, plan_start_monitoring, plan_stop_monitoring +from micromate.writes import ( + is_call_home_content, plan_call_home_enabled, plan_start_monitoring, plan_stop_monitoring, +) from minimateplus.transport import BaseTransport, SerialTransport, TcpTransport from .errors import ( AuditIncomplete, CallHomeInProgress, ChangeUnconfirmed, DeviceError, EventNotFound, - MalformedReply, StaleEvent, WrongUnit, translate, + MalformedReply, StaleEvent, UnitStateUnknown, WrongUnit, translate, ) from .change import CALL_HOME_MESSAGE, call_home_change, monitoring_change, run_change from .events import iso_of, make_event_id, stamp_of @@ -383,12 +385,24 @@ def _changed_offsets(before: bytes, after: bytes) -> list: if (before[i] if i < len(before) else None) != (after[i] if i < len(after) else None)] +# Measured 2026-11-03 (UM20147): monitoring, a Micromate returns its real call-home +# block and ACKS a write, but does not apply it -- its own menu: "Feature disabled +# while monitoring". So SFM refuses before sending one it would ignore. +S4_CALL_HOME_WHILE_MONITORING = ("is monitoring -- a Micromate ignores call-home changes while " + "monitoring (its own menu: 'Feature disabled while monitoring'); " + "stop monitoring first, then change call-home") +_NOT_CALL_HOME = "returned a block that is not its call-home block (header {}, not 007c)" + + def s4_call_home_state(client: Any, serial: str) -> CallHomeState: """Bit 0 of content[5] is the enable (UM12947 reads 0x05 / 0x04, UM20147 0x01).""" block = client.get_call_home_content() + detail = {"byte5": f"0x{block[5]:02X}" if len(block) > 5 else None, "block": block.hex()} + if not is_call_home_content(block): + detail["reason"] = "the unit " + _NOT_CALL_HOME.format(block[0:2].hex()) + return CallHomeState(serial=serial, series=4, enabled=None, dial_string=None, detail=detail) return CallHomeState(serial=serial, series=4, enabled=bool(block[5] & 0x01), - dial_string=_dial_string(block), - detail={"byte5": f"0x{block[5]:02X}", "block": block.hex()}) + dial_string=_dial_string(block), detail=detail) def s4_change_call_home(client: Any, serial: str, *, enabled: bool, dry_run: bool, @@ -406,7 +420,12 @@ def s4_change_call_home(client: Any, serial: str, *, enabled: bool, dry_run: boo run = client.read_run_state().name if run == "calling_home" or run.startswith("unknown"): return run # the core refuses both; a write waits for a known state + if run == "monitoring" and not reads: + raise UnitStateUnknown(f"{serial} {S4_CALL_HOME_WHILE_MONITORING} -- nothing sent") block = client.get_call_home_content() + if not is_call_home_content(block): + raise UnitStateUnknown(f"{serial} " + _NOT_CALL_HOME.format(block[0:2].hex()) + + " -- nothing sent") reads.append(block) return "enabled" if block[5] & 0x01 else "disabled" diff --git a/tests/test_device_access_s4_call_home.py b/tests/test_device_access_s4_call_home.py index 6711810..af72742 100644 --- a/tests/test_device_access_s4_call_home.py +++ b/tests/test_device_access_s4_call_home.py @@ -114,3 +114,37 @@ def test_a_lost_ack_still_records_what_the_unit_holds(): assert (r.outcome, r.sent) == ("failed", True) snap = r.detail["snapshot"] assert snap["changed_offsets"] == [] and snap["before"] == snap["after"] # the write did not land + + + +# Measured at the bench 2026-11-03 (UM20147): a MONITORING Micromate returns its +# real call-home block and ACKS a write, but does not apply it -- its own menu says +# "Feature disabled while monitoring". SFM refuses up front and says why, rather +# than send a write the unit will ignore. + +@pytest.mark.parametrize("dry_run", [True, False]) +def test_s4_call_home_is_refused_while_monitoring(dry_run): + from sfm.device_access.errors import UnitStateUnknown + fake = FakeMicromateClient(run_states=[0x02]) + with pytest.raises(UnitStateUnknown) as e: + change(fake, enabled=False, dry_run=dry_run) + msg = str(e.value) + assert "monitoring" in msg and "stop" in msg and "nothing sent" in msg + assert fake.executed == [] + + +# The block's header is its own length (00 7C, N = 124); SFM flips a bit only in a +# block that carries it, never in whatever 126 bytes came back. + +def test_s4_a_block_that_is_not_the_call_home_block_sends_nothing(): + from sfm.device_access.errors import UnitStateUnknown + fake = FakeMicromateClient(call_home=bytes(126)) + with pytest.raises(UnitStateUnknown) as e: + change(fake, enabled=False) + assert "not its call-home block" in str(e.value) + assert fake.executed == [] + + +def test_s4_state_of_a_block_that_is_not_the_call_home_block_says_why(): + st = s4_call_home_state(FakeMicromateClient(call_home=bytes(126)), "UM20147") + assert st.enabled is None and "not its call-home block" in st.detail["reason"] diff --git a/tests/test_micromate_writes.py b/tests/test_micromate_writes.py index 5e475d4..f6cdbed 100644 --- a/tests/test_micromate_writes.py +++ b/tests/test_micromate_writes.py @@ -265,3 +265,10 @@ def test_setup_push_refuses_a_block_whose_own_length_header_is_wrong(): bad[0:2] = (int.from_bytes(bad[0:2], "big") + 1).to_bytes(2, "big") with pytest.raises(ValueError, match="header"): plan_setup_push(reads[0x08], reads[0x2E], bytes(bad), "x.mmb") + + + +def test_call_home_refuses_a_block_whose_header_is_not_its_length(): + # 126 bytes, but not the call-home block: its header must be its own length (00 7C). + with pytest.raises(ValueError, match="header"): + plan_call_home_enabled(bytes(126), True)