Add BetterDSPControl: web UI, desktop app and Pi writer for t.racks DSP 204/408
- Python stdlib server with SSE live updates, PIN login, remote access - Vanilla JS web UI (English/German): mixer, channel, routing, links, limiter calculator - Preset file export/import with undo, settings backup, lock for the desktop app - Network (TCP 9761) and USB HID transport (Windows, macOS, Linux) - Desktop app via pywebview, Raspberry Pi SD card writer, simulator and protocol tests - README with import guide for Claude Code, CLAUDE.md, protocol documentation Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ce02ae65a9
commit
fe13f261f7
31 files changed
+6445
No files matched your search
+304
@@ -0,0 +1,304 @@
|
||||
"""Raspberry Pi OS images and first-boot setup for the DSP Control SD card writer.
|
||||
|
||||
The current Raspberry Pi OS Lite images are configured with cloud-init: files in the FAT boot
|
||||
partition (user-data, network-config, meta-data) plus a NoCloud entry in cmdline.txt - the same
|
||||
layout Raspberry Pi Imager writes. Our user-data then installs DSP Control from the boot partition
|
||||
as a systemd service on port 80, so the Pi needs no internet connection.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import secrets
|
||||
import shutil
|
||||
import time
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
import server
|
||||
|
||||
OS_LIST_URL = "https://downloads.raspberrypi.com/os_list_imagingutility_v4.json"
|
||||
IMAGE_NAMES = {64: "Raspberry Pi OS Lite (64-bit)", 32: "Raspberry Pi OS Lite (32-bit)"}
|
||||
APP_FILES = ["server.py", "dsp", "static"] # what gets installed on the Pi
|
||||
USER_AGENT = "DSP-Control-Pi-Writer/1.0"
|
||||
|
||||
# Pi models offered in the writer: (name, description, os bits)
|
||||
MODELS = [
|
||||
("Raspberry Pi 5", "Pi 5, Pi 500, CM5", 64),
|
||||
("Raspberry Pi 4", "Pi 4 B, Pi 400, CM4", 64),
|
||||
("Raspberry Pi 3", "Pi 3 A+/B/B+, CM3", 64),
|
||||
("Raspberry Pi Zero 2 W", "Zero 2 W", 64),
|
||||
("Raspberry Pi 2", "Pi 2 B", 32),
|
||||
("Raspberry Pi 1", "Pi 1 A/A+/B/B+", 32),
|
||||
("Raspberry Pi Zero", "Zero, Zero W, Zero WH", 32),
|
||||
]
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ OS list / download
|
||||
|
||||
def fetch_os_list() -> dict:
|
||||
req = urllib.request.Request(OS_LIST_URL, headers={"User-Agent": USER_AGENT})
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return json.loads(r.read())
|
||||
|
||||
|
||||
def find_image(os_list: dict, bits: int) -> dict:
|
||||
"""The Raspberry Pi OS Lite entry for 32 or 64 bit."""
|
||||
want = IMAGE_NAMES[bits]
|
||||
|
||||
def walk(items):
|
||||
for it in items:
|
||||
if it.get("name") == want and it.get("url"):
|
||||
return it
|
||||
found = walk(it.get("subitems") or [])
|
||||
if found:
|
||||
return found
|
||||
return None
|
||||
|
||||
img = walk(os_list.get("os_list", []))
|
||||
if not img:
|
||||
raise RuntimeError(f"„{want}“ nicht in der Raspberry-Pi-Liste gefunden")
|
||||
if img.get("init_format") not in ("cloudinit", "cloudinit-rpi"):
|
||||
raise RuntimeError(f"Unbekanntes Einrichtungsformat „{img.get('init_format')}“ – Writer aktualisieren")
|
||||
return img
|
||||
|
||||
|
||||
def download(img: dict, cache_dir: Path, progress) -> Path:
|
||||
"""Download (or reuse) the compressed image and check its SHA-256. progress(done, total)."""
|
||||
cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
name = img["url"].rsplit("/", 1)[-1]
|
||||
dest = cache_dir / name
|
||||
want = (img.get("image_download_sha256") or "").lower()
|
||||
if dest.exists() and want and sha256_file(dest) == want:
|
||||
return dest
|
||||
for old in cache_dir.glob("*.img.xz"): # keep only the current image
|
||||
old.unlink(missing_ok=True)
|
||||
tmp = dest.with_suffix(dest.suffix + ".part")
|
||||
req = urllib.request.Request(img["url"], headers={"User-Agent": USER_AGENT})
|
||||
h = hashlib.sha256()
|
||||
with urllib.request.urlopen(req, timeout=60) as r, open(tmp, "wb") as f:
|
||||
total = int(r.headers.get("Content-Length") or img.get("image_download_size") or 0)
|
||||
done = 0
|
||||
while chunk := r.read(1 << 20):
|
||||
f.write(chunk)
|
||||
h.update(chunk)
|
||||
done += len(chunk)
|
||||
progress(done, total)
|
||||
if want and h.hexdigest() != want:
|
||||
tmp.unlink(missing_ok=True)
|
||||
raise RuntimeError("Download beschädigt (Prüfsumme stimmt nicht) – bitte nochmal versuchen")
|
||||
tmp.replace(dest)
|
||||
return dest
|
||||
|
||||
|
||||
def sha256_file(path: Path) -> str:
|
||||
h = hashlib.sha256()
|
||||
with open(path, "rb") as f:
|
||||
while chunk := f.read(1 << 20):
|
||||
h.update(chunk)
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ password hash
|
||||
|
||||
_ITOA = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
|
||||
def sha512_crypt(password: str, salt: str | None = None, rounds: int = 5000) -> str:
|
||||
"""glibc SHA-512 crypt ("$6$..."), accepted by Linux login/PAM. Python has no crypt module on Windows."""
|
||||
if salt is None:
|
||||
salt = "".join(secrets.choice(_ITOA) for _ in range(16))
|
||||
p, s = password.encode(), salt.encode()[:16]
|
||||
rep = lambda block, n: (block * (n // len(block) + 1))[:n]
|
||||
b = hashlib.sha512(p + s + p).digest()
|
||||
a = hashlib.sha512(p + s + rep(b, len(p)))
|
||||
i = len(p)
|
||||
while i:
|
||||
a.update(b if i & 1 else p)
|
||||
i >>= 1
|
||||
c = a.digest()
|
||||
pp = rep(hashlib.sha512(p * len(p)).digest(), len(p))
|
||||
ss = rep(hashlib.sha512(s * (16 + c[0])).digest(), len(s))
|
||||
for r in range(rounds):
|
||||
d = hashlib.sha512(pp if r & 1 else c)
|
||||
if r % 3:
|
||||
d.update(ss)
|
||||
if r % 7:
|
||||
d.update(pp)
|
||||
d.update(c if r & 1 else pp)
|
||||
c = d.digest()
|
||||
out = []
|
||||
|
||||
def b64(v, n):
|
||||
for _ in range(n):
|
||||
out.append(_ITOA[v & 63])
|
||||
v >>= 6
|
||||
|
||||
for x, y, z in ((0, 21, 42), (22, 43, 1), (44, 2, 23), (3, 24, 45), (25, 46, 4), (47, 5, 26), (6, 27, 48),
|
||||
(28, 49, 7), (50, 8, 29), (9, 30, 51), (31, 52, 10), (53, 11, 32), (12, 33, 54), (34, 55, 13),
|
||||
(56, 14, 35), (15, 36, 57), (37, 58, 16), (59, 17, 38), (18, 39, 60), (40, 61, 19), (62, 20, 41)):
|
||||
b64(c[x] << 16 | c[y] << 8 | c[z], 4)
|
||||
b64(c[63], 2)
|
||||
return "$6$" + ("" if rounds == 5000 else f"rounds={rounds}$") + s.decode() + "$" + "".join(out)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ first-boot files
|
||||
|
||||
def yq(v: str) -> str:
|
||||
"""YAML double-quoted scalar."""
|
||||
return '"' + str(v).replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n").replace("\r", "") + '"'
|
||||
|
||||
|
||||
def validate(cfg: dict) -> list[str]:
|
||||
errs = []
|
||||
if not re.fullmatch(r"[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?", cfg["hostname"]):
|
||||
errs.append("Hostname: nur Kleinbuchstaben, Ziffern und „-“")
|
||||
if not re.fullmatch(r"[a-z_][a-z0-9_-]{0,31}", cfg["user"]) or cfg["user"] in ("root", "dspctl"):
|
||||
errs.append("Benutzername: Kleinbuchstaben/Ziffern, nicht „root“")
|
||||
if len(cfg["password"]) < 4:
|
||||
errs.append("Passwort: mindestens 4 Zeichen")
|
||||
if cfg["ssid"] and cfg["wifi_password"] and not 8 <= len(cfg["wifi_password"]) <= 63:
|
||||
errs.append("WLAN-Passwort: 8 bis 63 Zeichen (leer = offenes WLAN)")
|
||||
if cfg["ssid"] and not re.fullmatch(r"[A-Za-z]{2}", cfg["country"]):
|
||||
errs.append("WLAN-Land: zweistelliger Code, z. B. AT")
|
||||
if not re.fullmatch(r"\d{4}", cfg["pin"]):
|
||||
errs.append("PIN: 4 Ziffern")
|
||||
for k in ("dsp204", "dsp408"):
|
||||
if cfg.get(k) and not re.fullmatch(r"\d{1,3}(\.\d{1,3}){3}", cfg[k]):
|
||||
errs.append(f"IP-Adresse {k[3:]}: ungültig")
|
||||
return errs
|
||||
|
||||
|
||||
def user_data(cfg: dict) -> str:
|
||||
lines = [
|
||||
"#cloud-config",
|
||||
"manage_resolv_conf: false",
|
||||
"",
|
||||
f"hostname: {yq(cfg['hostname'])}",
|
||||
"manage_etc_hosts: true",
|
||||
f"timezone: {yq(cfg.get('timezone') or 'Europe/Vienna')}",
|
||||
"",
|
||||
"user:",
|
||||
f" name: {yq(cfg['user'])}",
|
||||
" shell: /bin/bash",
|
||||
" lock_passwd: false",
|
||||
f" passwd: {yq(sha512_crypt(cfg['password']))}",
|
||||
" sudo: null",
|
||||
"",
|
||||
]
|
||||
if cfg.get("ssh"):
|
||||
lines += ["ssh_pwauth: true", ""]
|
||||
lines.append("runcmd:")
|
||||
if cfg.get("ssh"):
|
||||
lines.append(" - [ systemctl, enable, --now, ssh ]")
|
||||
if not cfg["ssid"] and cfg.get("country"):
|
||||
lines.append(" - [ rfkill, unblock, wifi ]")
|
||||
lines.append(" - [ sh, /boot/firmware/dsp-control/install.sh ]")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
def network_config(cfg: dict) -> str | None:
|
||||
if not cfg["ssid"]:
|
||||
return None # Ethernet works by default
|
||||
lines = [
|
||||
"network:",
|
||||
" version: 2",
|
||||
" ethernets:",
|
||||
" eth0:",
|
||||
" dhcp4: true",
|
||||
" dhcp6: true",
|
||||
" optional: true",
|
||||
" wifis:",
|
||||
" wlan0:",
|
||||
" dhcp4: true",
|
||||
f" regulatory-domain: {yq(cfg['country'].upper())}",
|
||||
" access-points:",
|
||||
f" {yq(cfg['ssid'])}:",
|
||||
]
|
||||
if cfg["wifi_password"]:
|
||||
lines.append(f" password: {yq(cfg['wifi_password'])}")
|
||||
else:
|
||||
lines += [" auth:", " key-management: none"]
|
||||
lines.append(" optional: true")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
INSTALL_SH = """#!/bin/sh
|
||||
# DSP Control first-boot install (run once by cloud-init)
|
||||
set -e
|
||||
SRC=/boot/firmware/dsp-control
|
||||
id dspctl >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin dspctl
|
||||
mkdir -p /opt/dsp-control
|
||||
cp -r "$SRC/app/." /opt/dsp-control/
|
||||
install -d -o dspctl -g dspctl -m 700 /var/lib/dsp-control
|
||||
if [ -f "$SRC/config.json" ]; then
|
||||
[ -f /var/lib/dsp-control/config.json ] || install -o dspctl -g dspctl -m 600 "$SRC/config.json" /var/lib/dsp-control/config.json
|
||||
rm -f "$SRC/config.json"
|
||||
fi
|
||||
# DSPs on USB (HID, VID 0168 / PID 0821): let the service user open them
|
||||
echo 'SUBSYSTEM=="hidraw", ATTRS{idVendor}=="0168", ATTRS{idProduct}=="0821", MODE="0666"' > /etc/udev/rules.d/60-dsp-control.rules
|
||||
udevadm control --reload-rules || true
|
||||
udevadm trigger --subsystem-match=hidraw || true
|
||||
install -m 644 "$SRC/dsp-control.service" /etc/systemd/system/dsp-control.service
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now dsp-control.service
|
||||
"""
|
||||
|
||||
SERVICE = """[Unit]
|
||||
Description=DSP Control WebUI for t.racks DSP 204/408
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
User=dspctl
|
||||
ExecStart=/usr/bin/python3 /opt/dsp-control/server.py --port 80 --config /var/lib/dsp-control/config.json
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
"""
|
||||
|
||||
|
||||
def initial_config(cfg: dict) -> dict:
|
||||
salt = secrets.token_hex(16)
|
||||
devices = []
|
||||
for key, name, model in (("dsp204", "DSP 204", "204"), ("dsp408", "DSP 408", "408")):
|
||||
if cfg.get(key):
|
||||
devices.append({"id": key, "name": name, "host": cfg[key], "port": 9761, "model": model,
|
||||
"addr": 1, "autoconnect": True})
|
||||
return {"devices": devices, "auth": {"salt": salt, "hash": server.hash_pin(cfg["pin"], salt)}, "sessions": {}}
|
||||
|
||||
|
||||
def write_boot_files(boot: Path, cfg: dict, app_root: Path):
|
||||
"""Put the first-boot setup and the DSP Control files onto the mounted boot partition."""
|
||||
if not (boot / "cmdline.txt").exists():
|
||||
raise RuntimeError(f"{boot} ist keine Raspberry-Pi-Bootpartition (cmdline.txt fehlt)")
|
||||
target = boot / "dsp-control"
|
||||
if target.exists():
|
||||
shutil.rmtree(target)
|
||||
(target / "app").mkdir(parents=True)
|
||||
for name in APP_FILES:
|
||||
src = app_root / name
|
||||
if src.is_dir():
|
||||
shutil.copytree(src, target / "app" / name, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
||||
else:
|
||||
shutil.copy2(src, target / "app" / name)
|
||||
lf = lambda p, text: p.write_text(text, encoding="utf-8", newline="\n")
|
||||
lf(target / "install.sh", INSTALL_SH)
|
||||
lf(target / "dsp-control.service", SERVICE)
|
||||
lf(target / "config.json", json.dumps(initial_config(cfg), indent=2))
|
||||
|
||||
instance = f"dsp-control-{int(time.time() * 1000)}"
|
||||
lf(boot / "meta-data", f"instance-id: {instance}\n")
|
||||
lf(boot / "user-data", user_data(cfg))
|
||||
net = network_config(cfg)
|
||||
if net:
|
||||
lf(boot / "network-config", net)
|
||||
cmdline = (boot / "cmdline.txt").read_text(encoding="utf-8").strip()
|
||||
cmdline = re.sub(r"\s*ds=nocloud;i=\S+", "", cmdline)
|
||||
cmdline += f" ds=nocloud;i={instance}"
|
||||
if not cfg["ssid"] and cfg.get("country") and "cfg80211.ieee80211_regdom" not in cmdline:
|
||||
cmdline += f" cfg80211.ieee80211_regdom={cfg['country'].upper()}"
|
||||
lf(boot / "cmdline.txt", cmdline + "\n")
|
||||
Reference in new issue
Block a user