121 lines
5.3 KiB
Python
121 lines
5.3 KiB
Python
"""B2 manual §10 + INTERNAL SERVER_COM token envelope; no arbitrary commands."""
|
|
|
|
import re
|
|
import secrets
|
|
|
|
KINDS = {
|
|
"request-position",
|
|
"restart",
|
|
"pulse-output",
|
|
"set-output",
|
|
"upload-configuration",
|
|
"download-configuration",
|
|
"probe-diagnostic-mode",
|
|
}
|
|
|
|
|
|
def prepare(body):
|
|
if body.get("model") != 231 or body.get("firmware") != "0.01":
|
|
raise ValueError("unqualified action firmware")
|
|
allowed = {"id", "imei", "kind", "model", "firmware"}
|
|
if body["kind"] == "probe-diagnostic-mode":
|
|
# INTERNAL SERVER_COM 01 00. Engineering qualification only: neither
|
|
# a SIM read nor a promise about this firmware's mode/termination.
|
|
# Admission additionally requires an explicit live observation lease.
|
|
allowed.add("observation_id")
|
|
if not isinstance(body.get("observation_id"), str) or not re.fullmatch(
|
|
r"[a-f0-9]{32}", body["observation_id"]
|
|
):
|
|
raise ValueError("diagnostic probe requires observation")
|
|
command = b"\x01\x00"
|
|
elif body["kind"] == "request-position":
|
|
command = b"\x01\x01"
|
|
elif body["kind"] == "restart":
|
|
# B2 manual §10. A token receipt is not proof of a completed restart.
|
|
command = b"\x01\x07"
|
|
elif body["kind"] == "upload-configuration" and body.get("encoding") == "binary":
|
|
# Documented read-only counterpart to SERV*1.9; no image is written.
|
|
allowed.add("encoding")
|
|
command = b"\x01\x09"
|
|
elif body["kind"] in ("upload-configuration", "download-configuration"):
|
|
allowed.add("password")
|
|
password = body.get("password")
|
|
if not isinstance(password, str) or not re.fullmatch(r"[0-9]{6}", password):
|
|
raise ValueError("invalid password")
|
|
if set(body) - allowed:
|
|
raise ValueError("unknown action fields")
|
|
# B2 manual §10: synchronize with the configured web endpoint. Download
|
|
# may apply settings: the endpoint owns staged data and readback checks.
|
|
number = 9 if body["kind"] == "upload-configuration" else 8
|
|
return {"encoding": "text", "binary": f"{password}*SERV*1.{number}".encode().hex()}
|
|
elif body["kind"] == "set-output":
|
|
allowed |= {"pin", "enabled", "encoding", "password"}
|
|
pin = body.get("pin")
|
|
enabled = body.get("enabled")
|
|
if type(pin) is not int or pin not in (4, 5, 6):
|
|
raise ValueError("unsupported output")
|
|
if type(enabled) is not bool:
|
|
raise ValueError("enabled must be boolean")
|
|
if body.get("encoding", "text") != "text":
|
|
raise ValueError("set-output requires text encoding")
|
|
password = body.get("password")
|
|
if not isinstance(password, str) or not re.fullmatch(r"[0-9]{6}", password):
|
|
raise ValueError("invalid password")
|
|
if set(body) - allowed:
|
|
raise ValueError("unknown action fields")
|
|
# B2 manual section 10: explicit, persistent low-side output state.
|
|
# The caller must request both transitions; there is no implicit retry.
|
|
return {
|
|
"encoding": "text",
|
|
"binary": f"{password}*SERV*8.{pin}.{int(enabled)}".encode("ascii").hex(),
|
|
}
|
|
elif body["kind"] == "pulse-output":
|
|
allowed |= {"pin", "seconds", "encoding"}
|
|
pin, seconds = body["pin"], body["seconds"]
|
|
if type(pin) is not int or pin not in (4, 5, 6):
|
|
raise ValueError("unsupported output")
|
|
if type(seconds) is not int or not 1 <= seconds <= 5:
|
|
raise ValueError("pulse must be 1..5 seconds")
|
|
command = bytes((0x18, pin, 0, seconds))
|
|
encoding = body.get("encoding", "binary")
|
|
if encoding not in ("binary", "text"):
|
|
raise ValueError("unsupported action encoding")
|
|
if encoding == "text":
|
|
allowed.add("password")
|
|
password = body.get("password")
|
|
if not isinstance(password, str) or not re.fullmatch(r"[0-9]{6}", password):
|
|
raise ValueError("invalid password")
|
|
if set(body) - allowed:
|
|
raise ValueError("unknown action fields")
|
|
# B2 manual §10: FC text has no token envelope. A reply to an
|
|
# unrelated command must never turn this into confirmed actuation.
|
|
return {
|
|
"encoding": "text",
|
|
"binary": f"{password}*SERV*24.{pin}.0.{seconds}".encode("ascii").hex(),
|
|
}
|
|
else:
|
|
raise ValueError("unsupported action")
|
|
if set(body) - allowed:
|
|
raise ValueError("unknown action fields")
|
|
result = {"token": secrets.randbits(32), "binary": command.hex()}
|
|
if body["kind"] == "probe-diagnostic-mode":
|
|
result["observation_id"] = body["observation_id"]
|
|
return result
|
|
|
|
|
|
def frame(value):
|
|
if value.get("encoding") == "text":
|
|
raw = bytes.fromhex(value["binary"])
|
|
return bytes((0x7B, len(raw), 0xFC, sum(raw) % 256)) + raw + b"}"
|
|
raw = b"\xff" + value["token"].to_bytes(4, "little") + bytes.fromhex(value["binary"])
|
|
return bytes((0x7B, len(raw), 0xFF, sum(raw) % 256)) + raw + b"}"
|
|
|
|
|
|
def confirmations(raw):
|
|
offset = 2
|
|
while offset < len(raw) - 1:
|
|
size = int.from_bytes(raw[offset + 1 : offset + 3], "little")
|
|
if raw[offset] == 9 and size == 5:
|
|
yield raw[offset + 7], int.from_bytes(raw[offset + 8 : offset + 12], "little")
|
|
offset += size + 8
|