feat(micromate): write plans for start/stop, call-home and setup push -- THOR's frames byte for byte
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
c46dd878ff
commit
49f8f49ac1
2 files changed
+301
No files matched your search
@@ -0,0 +1,139 @@
|
||||
"""Write plans for a Micromate: the exact frames a write would send, in order.
|
||||
|
||||
A plan is what a dry run returns. Building one sends nothing; transmitting it
|
||||
also needs a protocol constructed with those SUBs in `allowed_writes`, which no
|
||||
code does yet (see `micromate.opcodes`).
|
||||
|
||||
Every input is the unit's OWN read of the block being written (content, i.e.
|
||||
response data[11:]), and every plan sends back exactly what was read except
|
||||
the one thing the operation changes. THOR works the same way, and the
|
||||
reproduction tests hold the plans to THOR's frames byte for byte.
|
||||
|
||||
Offsets are THOR's observed constants. 🔶 Each was seen at one block size
|
||||
only, so a plan refuses a block of any other size rather than guess.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import FrozenSet, List, Tuple
|
||||
|
||||
from .framing import build_request
|
||||
from .opcodes import CROSS_SERIES_COLLISIONS, WRITE_SUBS, OpcodeRefused
|
||||
from .setup_block import crc_ok
|
||||
|
||||
_CONFIRM = 0xFFFF
|
||||
|
||||
# block content sizes THOR's offsets were observed at (all on UM12947 / 11.0CB)
|
||||
_PREFS_LEN, _TRIGGER_LEN, _SETUP_LEN, _CALL_HOME_LEN = 90, 28, 2092, 126
|
||||
_NAME_FIELD = 256
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WriteStep:
|
||||
sub: int
|
||||
offset: int
|
||||
data: bytes = b""
|
||||
params: bytes = field(default=bytes(10))
|
||||
what: str = ""
|
||||
|
||||
@property
|
||||
def ack_sub(self) -> int:
|
||||
return 0xFF - self.sub
|
||||
|
||||
@property
|
||||
def frame(self) -> bytes:
|
||||
return build_request(self.sub, self.offset, self.params, self.data)
|
||||
|
||||
def describe(self) -> dict:
|
||||
return {"sub": f"0x{self.sub:02X}", "what": self.what,
|
||||
"offset": f"0x{self.offset:04X}", "data_len": len(self.data),
|
||||
"expect_ack": f"0x{self.ack_sub:02X}", "frame": self.frame.hex()}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WritePlan:
|
||||
action: str
|
||||
steps: Tuple[WriteStep, ...]
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
for s in self.steps:
|
||||
if s.sub in CROSS_SERIES_COLLISIONS:
|
||||
raise OpcodeRefused(
|
||||
f"a plan cannot hold SUB 0x{s.sub:02X}: {CROSS_SERIES_COLLISIONS[s.sub]}")
|
||||
if s.sub not in WRITE_SUBS:
|
||||
raise ValueError(f"SUB 0x{s.sub:02X} is not a write opcode")
|
||||
|
||||
@property
|
||||
def subs(self) -> FrozenSet[int]:
|
||||
return frozenset(s.sub for s in self.steps)
|
||||
|
||||
def frames(self) -> List[bytes]:
|
||||
return [s.frame for s in self.steps]
|
||||
|
||||
def describe(self) -> dict:
|
||||
return {"action": self.action, "sends_nothing": True,
|
||||
"steps": [s.describe() for s in self.steps]}
|
||||
|
||||
|
||||
def plan_start_monitoring() -> WritePlan:
|
||||
return WritePlan("start monitoring", (WriteStep(0x96, _CONFIRM, what="start monitoring"),))
|
||||
|
||||
|
||||
def plan_stop_monitoring() -> WritePlan:
|
||||
return WritePlan("stop monitoring", (WriteStep(0x97, _CONFIRM, what="stop monitoring"),))
|
||||
|
||||
|
||||
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.
|
||||
|
||||
`[5]` reads 0x05 enabled / 0x04 disabled; bit 2 is set in both, so only
|
||||
bit 0 is the enable. `[118:120]` is unit-maintained and THOR echoes it.
|
||||
"""
|
||||
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)}")
|
||||
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")
|
||||
return WritePlan(what, (
|
||||
WriteStep(0x7E, 0x007E, data, what="call-home config write"),
|
||||
WriteStep(0x7F, _CONFIRM, what="call-home config confirm"),
|
||||
))
|
||||
|
||||
|
||||
def _checked(block: bytes, size: int, label: str) -> bytes:
|
||||
if len(block) != size:
|
||||
raise ValueError(f"{label} block size {len(block)} B is not the {size} B "
|
||||
"THOR's offset was observed at -- refusing to guess")
|
||||
if not crc_ok(block):
|
||||
raise ValueError(f"{label} block failed its checksum -- refusing to write a "
|
||||
"corrupt read back to the unit")
|
||||
return block
|
||||
|
||||
|
||||
def plan_setup_push(prefs: bytes, trigger: bytes, setup: bytes, name: str) -> WritePlan:
|
||||
"""Rewrite a setup as THOR does: read 0x08, 0x2E, 0x1A, send each back.
|
||||
|
||||
No field editing here (phase 5). Each block goes back with its length echo
|
||||
[0:3] zeroed; the unit recomputes the CRCs, so 0x68 and 0x71 drop the CRC
|
||||
slot and 0x82 sends it zeroed -- exactly THOR's frames.
|
||||
"""
|
||||
_checked(prefs, _PREFS_LEN, "preferences (0x08)")
|
||||
_checked(trigger, _TRIGGER_LEN, "trigger (0x2E)")
|
||||
_checked(setup, _SETUP_LEN, "setup (0x1A)")
|
||||
try:
|
||||
raw = name.encode("ascii")
|
||||
except UnicodeEncodeError:
|
||||
raise ValueError(f"setup name must be ASCII: {name!r}") from None
|
||||
if not raw or b"\x00" in raw or len(raw) >= _NAME_FIELD:
|
||||
raise ValueError(f"setup name must be 1-{_NAME_FIELD - 1} ASCII bytes "
|
||||
f"with no NUL: {name!r}")
|
||||
return WritePlan(f"setup push: {name}", (
|
||||
WriteStep(0xDA, 0x0100, raw.ljust(_NAME_FIELD, b"\x00"), what="setup file name"),
|
||||
WriteStep(0x68, 0x005A, bytes(3) + prefs[3:88], what="preferences block"),
|
||||
WriteStep(0x73, _CONFIRM, what="preferences confirm"),
|
||||
WriteStep(0x82, 0x001C, bytes(3) + trigger[3:26] + bytes(2), what="trigger block"),
|
||||
WriteStep(0x83, _CONFIRM, what="trigger confirm"),
|
||||
WriteStep(0x71, 0x082C, bytes(3) + setup[3:2090], what="setup block"),
|
||||
WriteStep(0x72, _CONFIRM, what="setup confirm"),
|
||||
))
|
||||
Reference in new issue
Block a user