Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
"""VESC CAN fragmentation: upstream comm_can.c, packet IDs 5..8.
|
|
Commands themselves remain generated by the pinned upstream VESC Tool.
|
|
"""
|
|
|
|
import binascii
|
|
import time
|
|
|
|
MAX_PAYLOAD = 10000
|
|
|
|
|
|
def fragment(payload, target, sender):
|
|
if (
|
|
not 1 <= len(payload) <= MAX_PAYLOAD
|
|
or not 0 <= target < 255
|
|
or not 0 <= sender < 255
|
|
or target == sender
|
|
):
|
|
raise ValueError("Invalid VESC CAN endpoint/payload")
|
|
|
|
def f(code, data):
|
|
return {"id": (code << 8) | target, "data": data.hex()}
|
|
|
|
if len(payload) <= 6:
|
|
return [f(8, bytes((sender, 0)) + payload)]
|
|
result, offset = [], 0
|
|
while offset < len(payload):
|
|
width, size, code = (1, 7, 5) if offset <= 255 else (2, 6, 6)
|
|
chunk = payload[offset : offset + size]
|
|
result.append(f(code, offset.to_bytes(width, "big") + chunk))
|
|
offset += len(chunk)
|
|
result.append(
|
|
f(
|
|
7,
|
|
bytes((sender, 0))
|
|
+ len(payload).to_bytes(2, "big")
|
|
+ binascii.crc_hqx(payload, 0).to_bytes(2, "big"),
|
|
)
|
|
)
|
|
return result
|
|
|
|
|
|
def serial_frame(payload):
|
|
width = 1 if len(payload) < 256 else 2
|
|
return (
|
|
bytes((width + 1,))
|
|
+ len(payload).to_bytes(width, "big")
|
|
+ payload
|
|
+ binascii.crc_hqx(payload, 0).to_bytes(2, "big")
|
|
+ b"\x03"
|
|
)
|
|
|
|
|
|
class Reassembly:
|
|
def __init__(self, target, sender, clock=time.monotonic):
|
|
self.target, self.sender, self.clock = target, sender, clock
|
|
self.buffer, self.until = bytearray(), 0
|
|
|
|
def feed(self, frame):
|
|
if (
|
|
not frame.get("extended")
|
|
or frame.get("fd")
|
|
or frame.get("rtr")
|
|
or frame.get("error")
|
|
or frame["id"] & 255 != self.sender
|
|
):
|
|
return None
|
|
data, code = bytes.fromhex(frame["data"]), frame["id"] >> 8
|
|
if len(data) > 8:
|
|
raise ValueError("Invalid classic CAN frame")
|
|
if self.clock() >= self.until:
|
|
self.buffer.clear()
|
|
if code in (5, 6):
|
|
width = 1 if code == 5 else 2
|
|
if len(data) <= width:
|
|
raise ValueError("Empty CAN fragment")
|
|
offset = int.from_bytes(data[:width], "big")
|
|
if offset == 0:
|
|
self.buffer.clear()
|
|
if offset != len(self.buffer) or offset + len(data) - width > MAX_PAYLOAD:
|
|
self.buffer.clear()
|
|
raise ValueError("CAN fragment sequence lost")
|
|
self.buffer.extend(data[width:])
|
|
self.until = self.clock() + 1
|
|
elif code in (7, 8):
|
|
if len(data) < (6 if code == 7 else 3) or data[0] != self.target or data[1] != 1:
|
|
return None
|
|
payload = bytes(self.buffer) if code == 7 else data[2:]
|
|
self.buffer.clear()
|
|
if code == 7 and (
|
|
len(payload) != int.from_bytes(data[2:4], "big")
|
|
or binascii.crc_hqx(payload, 0) != int.from_bytes(data[4:6], "big")
|
|
):
|
|
raise ValueError("CAN response length/CRC mismatch")
|
|
return payload
|
|
return None
|