Measured at the bench 2026-11-03 (UM20147): monitoring, a Micromate returns its real call-home block and ACKS a call-home write, but does not apply it -- three read-backs unchanged, and its own menu says 'Feature disabled while monitoring'. SFM now refuses up front (UnitStateUnknown, 409: '... is monitoring -- a Micromate ignores call-home changes while monitoring ...; stop monitoring first -- nothing sent'), dry run included, instead of sending a write the unit ignores. And the block must be the call-home block: micromate.writes.is_call_home_content checks its header is its own length (00 7C, N = 124 -- 00 7C DC on every read, UM12947 and UM20147). plan_call_home_enabled refuses any other 126 bytes, the change refuses before sending, and call_home_state says why (enabled = None). At the bench this was checked by eye before the monitoring write. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
151 lines
5.9 KiB
Python
151 lines
5.9 KiB
Python
"""Series 4 call-home: read the enable bit and dial string, and change it through
|
|
the audited path (phase 4). The frames are micromate.writes' (equal to THOR's
|
|
for UM12947); [118:120] is unit-maintained and moves after any config write."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from device_access_fakes import FakeMicromateClient, um12947_call_home # noqa: E402
|
|
from micromate.opcodes import ENABLEABLE_WRITES # noqa: E402
|
|
from micromate.writes import plan_call_home_enabled # noqa: E402
|
|
from sfm.device_access.errors import CallHomeInProgress # noqa: E402
|
|
from sfm.device_access.models import UnitAddress # noqa: E402
|
|
from sfm.device_access.s4 import s4_call_home_state, s4_change_call_home # noqa: E402
|
|
|
|
ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None)
|
|
|
|
|
|
class Audit:
|
|
def __init__(self):
|
|
self.rows = {}
|
|
|
|
def begin(self, **f):
|
|
cid = f"c{len(self.rows) + 1}"
|
|
self.rows[cid] = dict(f, outcome="pending")
|
|
return cid
|
|
|
|
def finish(self, cid, **f):
|
|
self.rows[cid].update(f)
|
|
|
|
|
|
def change(fake, *, enabled, dry_run=False, even_if_already=False):
|
|
return s4_change_call_home(fake, "UM20147", enabled=enabled, dry_run=dry_run,
|
|
even_if_already=even_if_already, audit=Audit(), address=ADDR,
|
|
caller="test", sleep=lambda s: None,
|
|
verify_timeout_s=3.0, verify_interval_s=1.0)
|
|
|
|
|
|
def test_call_home_writes_are_now_enableable():
|
|
assert {0x7E, 0x7F} <= ENABLEABLE_WRITES
|
|
|
|
|
|
def test_s4_call_home_state_reads_the_enable_bit_and_dial_string():
|
|
st = s4_call_home_state(FakeMicromateClient(), "UM20147")
|
|
assert (st.serial, st.series, st.enabled, st.dial_string) == ("UM20147", 4, True, "RADIO RING")
|
|
assert st.detail["byte5"] == "0x05"
|
|
assert st.detail["block"] == um12947_call_home().hex()
|
|
assert s4_call_home_state(FakeMicromateClient(call_home=um12947_call_home(False)), "UM20147").enabled is False
|
|
|
|
|
|
def test_s4_dry_run_shows_thors_frames():
|
|
fake = FakeMicromateClient()
|
|
r = change(fake, enabled=False, dry_run=True)
|
|
assert r.outcome == "dry_run"
|
|
assert r.plan["steps"] == plan_call_home_enabled(um12947_call_home(), False).describe()["steps"]
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_s4_disable_sends_7e_7f_and_verifies():
|
|
fake = FakeMicromateClient()
|
|
r = change(fake, enabled=False)
|
|
assert (r.outcome, r.verified, r.state_after, r.action) == ("sent", "ok", "disabled", "disable call-home")
|
|
assert [st.sub for st in fake.executed[0].steps] == [0x7E, 0x7F]
|
|
assert fake.call_home[5] == 0x04
|
|
|
|
|
|
def test_s4_unit_maintained_bytes_do_not_make_a_mismatch():
|
|
r = change(FakeMicromateClient(), enabled=False)
|
|
assert r.verified == "ok"
|
|
snap = r.detail["snapshot"]
|
|
assert snap["changed_offsets"] == [5, 118, 119]
|
|
assert snap["before"] == um12947_call_home().hex() and snap["after"] != snap["before"]
|
|
|
|
|
|
def test_s4_no_change_while_calling_home():
|
|
fake = FakeMicromateClient(run_states=[0x03])
|
|
with pytest.raises(CallHomeInProgress):
|
|
change(fake, enabled=False)
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_s4_already_enabled_is_skipped():
|
|
fake = FakeMicromateClient()
|
|
r = change(fake, enabled=True)
|
|
assert r.outcome == "skipped" and fake.executed == []
|
|
|
|
|
|
def test_s4_even_if_already_sends_the_no_op_write():
|
|
fake = FakeMicromateClient()
|
|
r = change(fake, enabled=True, even_if_already=True)
|
|
assert len(fake.executed) == 1
|
|
assert any("changes nothing" in w for w in r.plan["warnings"])
|
|
|
|
|
|
|
|
def test_s4_unknown_run_state_sends_nothing():
|
|
# Final review #3: a run state never measured is refused, as start/stop does.
|
|
from sfm.device_access.errors import UnitStateUnknown
|
|
fake = FakeMicromateClient(run_states=[0x07])
|
|
with pytest.raises(UnitStateUnknown):
|
|
change(fake, enabled=False)
|
|
assert fake.executed == []
|
|
|
|
|
|
def test_a_lost_ack_still_records_what_the_unit_holds():
|
|
# Final review #4: the lost-ack path is where the operator most needs it.
|
|
import micromate.protocol as mm_proto
|
|
fake = FakeMicromateClient(execute_error=mm_proto.TimeoutError("lost"))
|
|
r = change(fake, enabled=False)
|
|
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"]
|