Add device lock support and demo mode
Device lock (hardware lock of the DSP, independent of the app lock): - detect a locked DSP from the 0x2C answer during connect and wait for the code - unlock with 0x2D, optionally remember the code and unlock automatically - set a new code (0x2F) from the device settings - simulator: --lock CODE Demo mode: - switch in the settings starts a simulated DSP 204/408 inside the server - real DSPs are disconnected and greyed out meanwhile and resume afterwards - demo devices are never saved or exported; sim.py ships with the Pi image and EXEs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe13f261f7
commit
61400df667
13 files changed
+426
-38
No files matched your search
+67
-7
@@ -20,7 +20,8 @@ LINK_RESYNC_DELAY = 1.5 # re-read the config this long after the last change on
|
||||
|
||||
class Device:
|
||||
def __init__(self, dev_id: str, name: str, host: str, port: int = P.PORT, model: str = "auto",
|
||||
addr: int = 1, autoconnect: bool = True, on_event=None, transport: str = "tcp", usb: str = ""):
|
||||
addr: int = 1, autoconnect: bool = True, on_event=None, transport: str = "tcp", usb: str = "",
|
||||
lock_code: str = "", auto_unlock: bool = False):
|
||||
self.id = dev_id
|
||||
self.name = name
|
||||
self.host = host
|
||||
@@ -31,8 +32,12 @@ class Device:
|
||||
self.transport = "usb" if transport == "usb" else "tcp"
|
||||
self.usb = usb # HID device path, "" = first DSP found
|
||||
self.on_event = on_event or (lambda ev: None)
|
||||
# hardware lock of the DSP itself (not the app lock): remembered code for automatic unlocking
|
||||
self.lock_code = lock_code if P.LOCK_CODE.fullmatch(lock_code or "") else ""
|
||||
self.auto_unlock = auto_unlock
|
||||
self.demo = False # in-process simulator of the demo mode, never saved
|
||||
|
||||
self.status = "disconnected" # disconnected | connecting | loading | online | error
|
||||
self.status = "disconnected" # disconnected | connecting | loading | locked | online | error
|
||||
self.error = ""
|
||||
self.info = ""
|
||||
self.model: P.Model | None = P.MODELS.get(model)
|
||||
@@ -53,16 +58,20 @@ class Device:
|
||||
self._want_connected = False
|
||||
self._resync: asyncio.TimerHandle | None = None
|
||||
self._changes = 0 # bumped on every local change, so a resync never overwrites newer edits
|
||||
self._codes: asyncio.Queue | None = None # (code, future) entered while the DSP is locked
|
||||
|
||||
# ------------------------------------------------------------ public API
|
||||
|
||||
def config(self) -> dict:
|
||||
return {"id": self.id, "name": self.name, "host": self.host, "port": self.port,
|
||||
"model": self.model_key, "addr": self.addr, "autoconnect": self.autoconnect,
|
||||
"transport": self.transport, "usb": self.usb}
|
||||
"transport": self.transport, "usb": self.usb,
|
||||
"lock_code": self.lock_code, "auto_unlock": self.auto_unlock}
|
||||
|
||||
def snapshot(self) -> dict:
|
||||
return {**self.config(), "status": self.status, "error": self.error, "info": self.info,
|
||||
cfg = self.config()
|
||||
cfg["lock_code"] = bool(self.lock_code) # clients only learn whether a code is remembered
|
||||
return {**cfg, "demo": self.demo, "status": self.status, "error": self.error, "info": self.info,
|
||||
"detected_model": self.model_name or None,
|
||||
"n_in": self.model.n_in if self.model else 0,
|
||||
"n_out": self.model.n_out if self.model else 0,
|
||||
@@ -75,8 +84,6 @@ class Device:
|
||||
|
||||
async def stop(self):
|
||||
self._want_connected = False
|
||||
self._resync: asyncio.TimerHandle | None = None
|
||||
self._changes = 0 # bumped on every local change, so a resync never overwrites newer edits
|
||||
if self._supervisor:
|
||||
self._supervisor.cancel()
|
||||
try:
|
||||
@@ -121,6 +128,29 @@ class Device:
|
||||
await asyncio.sleep(0.3)
|
||||
await self.refresh()
|
||||
|
||||
async def unlock(self, code: str) -> bool:
|
||||
"""Try a lock code while the DSP waits for it. True if the DSP accepted it."""
|
||||
code = str(code or "").strip().upper()
|
||||
if not P.LOCK_CODE.fullmatch(code):
|
||||
raise ValueError("The code has 4 characters: digits and letters A–Z")
|
||||
if self.status != "locked" or not self._codes:
|
||||
raise RuntimeError("The DSP is not locked")
|
||||
fut = asyncio.get_running_loop().create_future()
|
||||
await self._codes.put((code, fut))
|
||||
return await fut
|
||||
|
||||
async def set_lock_code(self, code: str):
|
||||
"""Store a new lock code in the DSP; it asks for it from the next connection on."""
|
||||
code = str(code or "").strip().upper()
|
||||
if not P.LOCK_CODE.fullmatch(code):
|
||||
raise ValueError("The code has 4 characters: digits and letters A–Z")
|
||||
if self.status != "online":
|
||||
raise RuntimeError("DSP is not connected")
|
||||
if self.lock_code: # keep auto-unlock working with the new code
|
||||
self.lock_code = code
|
||||
await self._request(P.CMD_SET_LOCK_CODE, code.encode("ascii"))
|
||||
await self._request(P.CMD_LOGOUT) # as the original software does after setting the code
|
||||
|
||||
def _schedule_resync(self):
|
||||
"""Re-read the config shortly after edits on linked channels, so the app shows exactly
|
||||
what the DSP applied to the group (e.g. mute, which we don't mirror for certain)."""
|
||||
@@ -293,8 +323,11 @@ class Device:
|
||||
if detected and detected != self.model_key:
|
||||
log.warning("%s: configured as %s but device reports %s", self.name, self.model_key, self.info)
|
||||
count_frame = await self._request(P.CMD_PRESET_COUNT, expect=P.CMD_PRESET_COUNT)
|
||||
count = count_frame.payload[-1] if count_frame and count_frame.payload else 20
|
||||
payload = count_frame.payload if count_frame else b""
|
||||
count = payload[-1] if 0 < len(payload) < 6 else 20 # the long answer carries the lock state instead
|
||||
count = count if 0 < count <= 60 else 20
|
||||
if P.device_locked(payload):
|
||||
await self._wait_for_unlock()
|
||||
await self._request(P.CMD_INIT_22)
|
||||
await self._request(P.CMD_INIT_14)
|
||||
presets = []
|
||||
@@ -304,6 +337,33 @@ class Device:
|
||||
self.presets = presets
|
||||
await self._load_config()
|
||||
|
||||
async def _try_code(self, code: str) -> bool:
|
||||
f = await self._request(P.CMD_UNLOCK, b"\x00" + code.encode("ascii"), timeout=2.0, expect=P.CMD_UNLOCK)
|
||||
return bool(f and len(f.payload) > 1 and f.payload[1] == 1)
|
||||
|
||||
async def _wait_for_unlock(self):
|
||||
"""The DSP is locked with its code: unlock automatically or wait until a code is entered."""
|
||||
if self.auto_unlock and self.lock_code:
|
||||
if await self._try_code(self.lock_code):
|
||||
log.info("%s: unlocked automatically", self.name)
|
||||
return
|
||||
log.warning("%s: remembered lock code was rejected", self.name)
|
||||
self.error = "The remembered lock code was rejected"
|
||||
self._codes = asyncio.Queue()
|
||||
self._set_status("locked")
|
||||
try:
|
||||
while True:
|
||||
code, fut = await self._codes.get()
|
||||
ok = await self._try_code(code)
|
||||
if not fut.done():
|
||||
fut.set_result(ok)
|
||||
if ok:
|
||||
self.error = ""
|
||||
self._set_status("loading")
|
||||
return
|
||||
finally:
|
||||
self._codes = None
|
||||
|
||||
async def _load_config(self):
|
||||
self.state = await self._read_config()
|
||||
|
||||
|
||||
+11
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
import copy
|
||||
import math
|
||||
import re
|
||||
import struct
|
||||
from dataclasses import dataclass
|
||||
|
||||
@@ -10,6 +11,7 @@ PORT = 9761
|
||||
# Read / system commands
|
||||
CMD_ACK = 0x01
|
||||
CMD_HANDSHAKE = 0x10
|
||||
CMD_LOGOUT = 0x11 # sent after setting a lock code
|
||||
CMD_STATUS = 0x12
|
||||
CMD_DEVICE_INFO = 0x13
|
||||
CMD_INIT_14 = 0x14
|
||||
@@ -20,7 +22,9 @@ CMD_CONFIG_CHUNK = 0x24
|
||||
CMD_PRESET_STORE_NAME = 0x26
|
||||
CMD_CONFIG_REQUEST = 0x27
|
||||
CMD_PRESET_NAME = 0x29
|
||||
CMD_PRESET_COUNT = 0x2C
|
||||
CMD_PRESET_COUNT = 0x2C # answer: [time lock][4 bytes time lock data][device lock] (see device_locked)
|
||||
CMD_UNLOCK = 0x2D # [0][code 4] -> answer [0][1 = unlocked, 0 = wrong code]
|
||||
CMD_SET_LOCK_CODE = 0x2F # [code 4]
|
||||
CMD_METER = 0x40
|
||||
|
||||
# Parameter commands
|
||||
@@ -41,6 +45,12 @@ CMD_GEQ = 0x48
|
||||
CMD_LINK = 0x3B # [ch][u16 link mask], see link_masks()
|
||||
|
||||
CHUNK_SIZE = 50
|
||||
LOCK_CODE = re.compile(r"[A-Z0-9]{4}") # device lock code: 4 characters, digits and upper-case letters
|
||||
|
||||
|
||||
def device_locked(count_payload: bytes) -> bool:
|
||||
"""True if the answer to CMD_PRESET_COUNT says the DSP waits for its lock code."""
|
||||
return len(count_payload) > 5 and count_payload[5] != 0
|
||||
|
||||
|
||||
def checksum(data: bytes) -> int:
|
||||
|
||||
Reference in new issue
Block a user