Files
seismo-relay/micromate/opcodes.py
T
serversdownandClaude Opus 5.5 eeb55effe9 feat(sfm): Series-4 call-home state and audited enable/disable (phase 4)
- micromate: ENABLEABLE_WRITES gains 0x7E/0x7F (the call-home write and its
  confirm; spec row 9, enable/disable only); MicromateClient.get_call_home_content()
  returns the 126-byte 0x2C content.
- sfm/device_access: CallHomeState (serial, series, enabled, dial_string, detail
  with byte5 and the whole block); s4_call_home_state reads bit 0 of [5] and the
  dial string at [6:46]; s4_change_call_home runs the shared change core with
  micromate.writes.plan_call_home_enabled -- built from the read run_change made
  just before, and that same plan is the one sent.  A unit calling home (run
  state 0x03) is refused, nothing sent.  The read-back judges the edited bit
  only; the snapshot records both blocks and every changed offset -- [118:120]
  is unit-maintained and always moves.
- The write-ceiling tests take 0x71 (the setup write, phase 5) as their
  over-the-ceiling example now that 0x7E is enableable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-02 16:56:04 -05:00

115 lines
4.7 KiB
Python

"""Micromate (Series IV) opcode table: which SUBs write, and which collide.
One place, as the parity spec requires. The gate is an ALLOWLIST: a SUB is
transmitted only if it is a catalogued read (READ_SUBS) or a catalogued write
that arrives through `MicromateProtocol.execute` (one call's permission, capped at
ENABLEABLE_WRITES; the connection itself holds none). An unknown SUB is refused -- clock-set
and delete-all have Series-IV opcodes nobody has catalogued yet, and "unknown
means read" is the wrong default. `MicromateProtocol._send` consults it
on every outgoing frame, so a write cannot reach a unit by a path nobody
thought about -- a scratch tool calling `protocol._send`, a hand-built frame, a
method added later.
Every SUB here was seen as a host->unit request in THOR's captures
(`docs/micromate_protocol_reference.md`). The table classifies REQUESTS: a few
of these values are also ACK SUBs of other commands (0x71 acks 0x8E, 0x72 acks
0x8D), which is irrelevant to a send-side check.
"""
from __future__ import annotations
from types import MappingProxyType
from typing import Dict, FrozenSet, Mapping
_WRITE_SUBS: Dict[int, str] = {
0x47: "scheduler enable/disable",
0x68: "setup push: preferences block (0x08)",
0x71: "setup push: setup block (0x1A)",
0x72: "setup push: setup confirm",
0x73: "setup push: preferences confirm",
0x7E: "call-home config write (0x2C)",
0x7F: "call-home config confirm",
0x82: "setup push: trigger block (0x2E)",
0x83: "setup push: trigger confirm",
0x8D: "file open for write",
0x8E: "file body write",
0x96: "start monitoring",
0x97: "stop monitoring",
0xA2: "end call-home",
0xA8: "delete one event",
0xAA: "delete-event confirm",
0xDA: "setup push: file name",
}
# Same SUB, opposite consequence on the other series (not the same bytes:
# Series III's CONFIRM ERASE carries offset 0x0000 and token 0xFE, THOR's 0xA2
# carries offset 0xFFFF). Never transmitted on
# Series IV, allowed or not: if a Series-III unit is at the address (a moved
# modem, a stale IP), it would execute the other meaning. Lifting an entry is
# a spec change, not a parameter.
_CROSS_SERIES_COLLISIONS: Dict[int, str] = {
0xA2: "CONFIRM ERASE on Series III -- erases every stored event; "
"'end call-home' on Series IV",
}
_READ_SUBS: Dict[int, str] = {
0x01: "device info",
0x06: "storage range",
0x08: "event index / preferences block",
0x0A: "monitor log",
0x0C: "event record",
0x15: "serial number",
0x1A: "setup block",
0x1C: "monitor status",
0x1E: "event first",
0x1F: "event next",
0x2C: "call-home config",
0x2E: "trigger config",
0x3F: "setup list first",
0x40: "setup list next",
0x41: "active setup",
0x48: "file read next page",
0x49: "state",
0x5A: "bulk download",
0x5B: "poll",
0x93: "arm event",
0x94: "file open for read",
}
# Read-only views: a stray .pop() anywhere in the process cannot disable a gate.
WRITE_SUBS: Mapping[int, str] = MappingProxyType(_WRITE_SUBS)
CROSS_SERIES_COLLISIONS: Mapping[int, str] = MappingProxyType(_CROSS_SERIES_COLLISIONS)
READ_SUBS: Mapping[int, str] = MappingProxyType(_READ_SUBS)
# The write SUBs any code may transmit at all -- through MicromateProtocol.execute,
# one call at a time. Raising this is a spec change, reviewed like one: delete
# (0xA8/0xAA), raw file writes (0x8D/0x8E -- they could reach the modem
# settings) and 0xA2 never belong here without one. Phase 4 (2026-11-02) added
# the call-home write and its confirm (spec row 9: enable/disable only).
ENABLEABLE_WRITES = frozenset({0x96, 0x97, 0x7E, 0x7F})
class OpcodeRefused(RuntimeError):
"""A frame was stopped before it reached the wire."""
def check_transmit(sub: int, allowed_writes: FrozenSet[int] = frozenset()) -> None:
"""Raise OpcodeRefused unless `sub` may go on the wire.
Precedence: (1) a cross-series collision is refused always; (2) a
SUB in neither READ_SUBS nor WRITE_SUBS is unknown and refused whatever
`allowed_writes` says; (3) a catalogued write is refused unless in
`allowed_writes`.
"""
if sub in CROSS_SERIES_COLLISIONS:
raise OpcodeRefused(
f"refused to transmit SUB 0x{sub:02X}: {CROSS_SERIES_COLLISIONS[sub]}")
if sub not in READ_SUBS and sub not in WRITE_SUBS:
raise OpcodeRefused(
f"refused to transmit SUB 0x{sub:02X}: unknown opcode -- not in "
"micromate.opcodes; catalogue it first (allowing it does not help)")
if sub in WRITE_SUBS and sub not in allowed_writes:
raise OpcodeRefused(
f"refused to transmit SUB 0x{sub:02X} ({WRITE_SUBS[sub]}): writes are "
"not enabled on this connection")