Files
seismo-relay/micromate/opcodes.py
T

107 lines
4.1 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
this connection was explicitly allowed. 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)
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")