Files
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

92 lines
3.7 KiB
Python

"""Typed, rate-limited reads between isolated QML and the sole USB owner.
No raw packet forwarding, writes, terminal, CAN routing, captures or code execution.
No background acquisition: a bounded batch is serviced on an operator frame request.
"""
import base64
from collections import OrderedDict
from .motor_test import Rejected
READ_CODES = frozenset((0, 4, 14, 17, 31, 32, 33, 65, 96))
def read_intent(value):
if not isinstance(value, dict):
raise ValueError("Invalid QML read intent")
command = value.get("command")
if type(command) is not int or command not in READ_CODES:
raise ValueError("QML command is not a permitted read")
if set(value) != ({"command", "mask"} if command == 65 else {"command"}):
raise ValueError("Invalid QML read shape")
if command == 65 and (type(value["mask"]) is not int or not 0 <= value["mask"] <= 65535):
raise ValueError("Invalid IMU mask")
return dict(value)
class QmlBroker:
def __init__(self, clock):
self.clock = clock
self.pending = OrderedDict()
self.last = {}
self.batch_at = float("-inf")
self.reads = 0
self.observed_at = None
def enqueue(self, frame):
requests = frame.get("read_requests", [])
if not isinstance(requests, list) or len(requests) > 9:
raise ValueError("QML read queue exceeded")
# Validate the entire untrusted batch before any USB operation.
intents = [read_intent(v) for v in requests]
for intent in intents:
self.pending[intent["command"]] = intent
def exchange(self, device, service, worker, frame):
self.enqueue(frame)
now = self.clock()
packets = []
if self.pending and now - self.batch_at >= 0.5:
service.code_tools.idle(device)
if not device.readable or not device.link:
raise Rejected("Контроллер недоступен для чтения QML.")
if (
service.code_tools.pending(device)
or service.code_tools.path(device, "execution").exists()
):
raise Rejected("Сначала проверьте предыдущее действие программы VESC.")
self.batch_at = now
for code, intent in list(self.pending.items()):
cooldown = 10 if code in (0, 14, 17) else 1
if now - self.last.get(code, float("-inf")) < cooldown:
continue
# Existing owner builds a typed upstream request; never raw bytes from QML.
result = device.link.rpc("qml_read", timeout=1.2, intent=intent)
packet = base64.b64decode(result["payload"], validate=True)
if not 1 <= len(packet) <= 32768 or packet[0] != code:
raise ValueError("QML read response does not match request")
packets.append(base64.b64encode(packet).decode())
del self.pending[code]
self.last[code] = self.clock()
self.reads += 1
self.observed_at = service.qml_utc()
if len(packets) == 2:
break
if packets:
frame = worker.exchange({"op": "frame", "events": [], "packets": packets})
self.enqueue(frame)
return {
**frame,
"hardware_access": True,
"direct_hardware_access": False,
"broker_status": {
"mode": "read_only",
"reads": self.reads,
"pending": len(self.pending),
"observed_at": self.observed_at,
"max_reads_per_frame": 2,
"background_reads": False,
},
}