Files
MicrOBU/tools/station_status_watch.py
T

98 lines
3.9 KiB
Python
Raw Normal View History

"""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 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, 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
import time
import serial
LINK, LOG = 0x10, 0x7F
STATION_CONFIGURE, STATUS_REQUEST = 0x01, 0x0C
CONFIRM, V2X_RX = 0x80, 0x90
def crc16(data, crc=0xFFFF):
for b in data:
crc ^= b << 8
for _ in range(8):
crc = ((crc << 1) ^ 0x1021) & 0xFFFF if crc & 0x8000 else (crc << 1) & 0xFFFF
return crc
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 link(opcode, seq, body=b""):
return frame(LINK, bytes([opcode, 0]) + struct.pack("<H", seq) + body)
def main():
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__":
main()