Rebuild obu-firmware on Niklas Dathe's microbu-firmware
Replace the vanetza-idf port with his current station firmware (microbu-firmware e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources: - V2X_RX (opcode 0x90): every received ITS message, signed or not, through gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp. - Unsigned transmission (geonet.c) for security profile 1 or station security 0, from a random MAC per configuration. - Radio polled from STATION_CONFIGURE on; manual country policy, TX power read-back, static RX buffer, short frames ignored. - Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB. - No USB writes without a host; last_frame_ms() for the guard. - 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm. Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned sending, battery rule, revision history). Add 39 host unit tests (make firmware in test/host) next to the existing chain, replay, fuzz and battery tests, and point tools/station_status_watch.py at the new CONFIRM-based status.
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
"""Print the micrOBU's STATUS (incl. the battery fields) from its native USB Serial/JTAG port.
|
||||
"""Poll the micrOBU's station-link STATUS and show its log, from the native USB Serial/JTAG port.
|
||||
|
||||
py -3.11 tools/station_status_watch.py COM10 [seconds]
|
||||
py -3.11 tools/station_status_watch.py COM6 [seconds] [--configure] [--rx]
|
||||
|
||||
Speaks the station-link framing of obu-firmware (0xAA55, type, u16 length, payload, CRC-16/CCITT-FALSE
|
||||
over type..payload, little endian; frame type 0x10 = LINK) and asks for STATUS once a second. The
|
||||
board also broadcasts STATUS itself every second. Opening the port resets the board (DTR/RTS), so
|
||||
the first frames come after the boot. Needs pyserial (the ESP-IDF python env has it).
|
||||
over type..payload, little endian; frame type 0x10 = LINK, 0x7F = LOG text). Sends STATUS_REQUEST once
|
||||
a second; the 131-octet snapshot comes back in CONFIRM.detail (docs/station-link.pdf).
|
||||
|
||||
--configure send STATION_CONFIGURE (cyclist, channel 180, 10 dBm, unsigned, no beaconing) after 2 s,
|
||||
which starts the radio
|
||||
--rx print the first V2X_RX messages (MicrOBU extension, opcode 0x90) and a total
|
||||
|
||||
DTR and RTS stay low: toggling them resets the board over USB Serial/JTAG. Needs pyserial (the
|
||||
ESP-IDF python env has it).
|
||||
"""
|
||||
import struct
|
||||
import sys
|
||||
@@ -13,13 +19,12 @@ import time
|
||||
|
||||
import serial
|
||||
|
||||
LINK = 0x10
|
||||
STATUS_REQUEST = 0x0C
|
||||
STATUS = 0x84
|
||||
LINK, LOG = 0x10, 0x7F
|
||||
STATION_CONFIGURE, STATUS_REQUEST = 0x01, 0x0C
|
||||
CONFIRM, V2X_RX = 0x80, 0x90
|
||||
|
||||
|
||||
def crc16(data: bytes) -> int:
|
||||
crc = 0xFFFF
|
||||
def crc16(data, crc=0xFFFF):
|
||||
for b in data:
|
||||
crc ^= b << 8
|
||||
for _ in range(8):
|
||||
@@ -27,61 +32,66 @@ def crc16(data: bytes) -> int:
|
||||
return crc
|
||||
|
||||
|
||||
def frame(type_: int, payload: bytes) -> bytes:
|
||||
head = bytes([type_]) + struct.pack('<H', len(payload)) + payload
|
||||
return b'\xaa\x55' + head + struct.pack('<H', crc16(head))
|
||||
def frame(ftype, payload):
|
||||
head = bytes([ftype]) + struct.pack("<H", len(payload))
|
||||
return b"\xAA\x55" + head + payload + struct.pack("<H", crc16(head + payload))
|
||||
|
||||
|
||||
def parse(buffer: bytearray):
|
||||
"""Yield (type, payload) for each complete, CRC-valid frame; consumes the buffer."""
|
||||
while True:
|
||||
i = buffer.find(b'\xaa\x55')
|
||||
if i < 0 or len(buffer) < i + 5:
|
||||
if i > 0:
|
||||
del buffer[:i]
|
||||
return
|
||||
length = buffer[i + 3] | (buffer[i + 4] << 8)
|
||||
end = i + 5 + length + 2
|
||||
if len(buffer) < end:
|
||||
if i > 0:
|
||||
del buffer[:i]
|
||||
return
|
||||
head, crc = bytes(buffer[i + 2:end - 2]), buffer[end - 2] | (buffer[end - 1] << 8)
|
||||
del buffer[:end if crc16(head) == crc else i + 2]
|
||||
if crc16(head) == crc:
|
||||
yield head[0], head[3:]
|
||||
|
||||
|
||||
def describe(body: bytes) -> str:
|
||||
uptime, configured = struct.unpack_from('<IB', body, 0)
|
||||
text = f"len {len(body)} uptime {uptime / 1000:.1f}s configured {configured}"
|
||||
if len(body) >= 106:
|
||||
mv, flags = struct.unpack_from('<HB', body, 103)
|
||||
text += f" battery {mv} mV flags 0x{flags:02x}" + (" LOW" if flags & 1 else "") + (" CRITICAL" if flags & 2 else "")
|
||||
else:
|
||||
text += " (no battery fields)"
|
||||
return text
|
||||
def link(opcode, seq, body=b""):
|
||||
return frame(LINK, bytes([opcode, 0]) + struct.pack("<H", seq) + body)
|
||||
|
||||
|
||||
def main():
|
||||
port = sys.argv[1] if len(sys.argv) > 1 else 'COM10'
|
||||
seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 15
|
||||
buffer = bytearray()
|
||||
sequence = 0
|
||||
with serial.Serial(port, 115200, timeout=0.2) as s:
|
||||
s.dtr = True # a CDC host asserts DTR; the firmware may gate TX on it
|
||||
end = time.time() + seconds
|
||||
next_request = 0.0
|
||||
while time.time() < end:
|
||||
if time.time() >= next_request:
|
||||
sequence += 1
|
||||
s.write(frame(LINK, bytes([STATUS_REQUEST, 0]) + struct.pack('<H', sequence)))
|
||||
next_request = time.time() + 1.0
|
||||
buffer += s.read(512)
|
||||
for type_, payload in parse(buffer):
|
||||
if type_ == LINK and len(payload) >= 4 and payload[0] == STATUS:
|
||||
print(time.strftime('%H:%M:%S'), describe(payload[4:]), flush=True)
|
||||
args = [a for a in sys.argv[1:] if not a.startswith("--")]
|
||||
flags = {a for a in sys.argv[1:] if a.startswith("--")}
|
||||
port = args[0]
|
||||
seconds = float(args[1]) if len(args) > 1 else 10
|
||||
ser = serial.Serial(port, 115200, timeout=0.1)
|
||||
ser.dtr = False
|
||||
ser.rts = False
|
||||
buf = bytearray()
|
||||
start = time.time()
|
||||
seq, last, configured, rx = 1, 0.0, False, 0
|
||||
while time.time() - start < seconds:
|
||||
if "--configure" in flags and not configured and time.time() - start > 2:
|
||||
ser.write(link(STATION_CONFIGURE, 100, struct.pack("<BHBBB", 2, 180, 10, 0, 0)))
|
||||
configured = True
|
||||
if time.time() - last > 1:
|
||||
ser.write(link(STATUS_REQUEST, seq))
|
||||
seq, last = seq + 1, time.time()
|
||||
buf += ser.read(4096)
|
||||
while True:
|
||||
i = buf.find(b"\xAA\x55")
|
||||
if i < 0 or len(buf) < i + 7:
|
||||
break
|
||||
ftype, n = buf[i + 2], struct.unpack("<H", buf[i + 3:i + 5])[0]
|
||||
if len(buf) < i + 5 + n + 2:
|
||||
break
|
||||
payload = bytes(buf[i + 5:i + 5 + n])
|
||||
del buf[:i + 5 + n + 2]
|
||||
if ftype == LOG:
|
||||
print("LOG", payload.decode("utf-8", "replace").rstrip())
|
||||
elif ftype == LINK:
|
||||
op, sq, body = payload[0], struct.unpack("<H", payload[2:4])[0], payload[4:]
|
||||
if op == CONFIRM and len(body) >= 2 and body[0] == 0 and body[1] == 131:
|
||||
d = body[2:]
|
||||
u32 = lambda o: struct.unpack_from("<I", d, o)[0]
|
||||
u16 = lambda o: struct.unpack_from("<H", d, o)[0]
|
||||
print(f"STATUS uptime={u32(0)} ms configured={d[4]} tickets={d[22]} signed={u32(23)} "
|
||||
f"radio rx={u32(71)} dropped={u32(75)} tx={u32(63)} failed={u32(67)} "
|
||||
f"heap_free={u32(111)} battery={u16(129)} mV")
|
||||
elif op == CONFIRM:
|
||||
print(f"CONFIRM seq={sq} result=0x{body[0]:02x}")
|
||||
elif op == V2X_RX:
|
||||
rx += 1
|
||||
if rx <= 8:
|
||||
port_, rssi, fl = struct.unpack("<HbB", body[:4])
|
||||
print(f"V2X_RX port={port_} rssi={rssi} dBm flags=0x{fl:02x} payload={len(body) - 14} bytes")
|
||||
else:
|
||||
print(f"LINK opcode=0x{op:02x} seq={sq} {len(body)} bytes")
|
||||
if "--rx" in flags:
|
||||
print("V2X_RX total", rx)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user