"""Micromate (Series IV) opcode table: which SUBs write, and which collide. One place, as the parity spec requires. `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 typing import Dict, FrozenSet 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 bytes, opposite consequence on the other series. 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", } class OpcodeRefused(RuntimeError): """A frame was stopped before it reached the wire.""" def check_transmit(sub: int, allowed_writes: FrozenSet[int] = frozenset()) -> None: if sub in CROSS_SERIES_COLLISIONS: raise OpcodeRefused( f"refused to transmit SUB 0x{sub:02X}: {CROSS_SERIES_COLLISIONS[sub]}") 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")