"""Play the phone over USB: configure the station, send CAMs at a fixed rate, and look at what goes on air. py -3.11 tools/station_tx_test.py COM10 [--seconds 60] [--rate 1] [--unsigned] [--mac 02:11:22:33:44:55] What it does, all over the native USB Serial/JTAG link (docs/station-link.pdf): - STATION_CONFIGURE: the 6-octet form, or with --mac the 16-octet form (address_configuration 0, the phone names the MAC, as the app does so that a pseudonym rotates it); - POTI_UPDATE once a second and a BTP_DATA_REQUEST (port 2001, ITS-AID 36, SSP 010000) at --rate; - RAW_CAPTURE with the own-transmission bit, so the station copies every frame it puts on the radio back over the link (RAW_FRAME, an ITS5 record): that shows the 802.11 source address and whether the packet is signed, without a second board; - STATUS once a second, and the board's "TX observation" log lines (submitted / completed / failed). At the end it prints how many requests were accepted, how many frames the radio submitted, how many the driver reported as completed, and the MAC and security of the captured frames. Pair it with a second board listening on the air (the bench listener on COM3) to count what actually arrives. DTR and RTS stay low, but opening the port resets the board (USB Serial/JTAG), so a phone on BLE is dropped. Needs pyserial (the ESP-IDF python env has it). The demo credentials must already be on the board (the app provisions them on its first connect) for signed requests. """ import argparse import collections import struct import sys import time import serial LINK, LOG = 0x10, 0x7F STATION_CONFIGURE, POTI_UPDATE, BTP_DATA_REQUEST, STATUS_REQUEST, RAW_CAPTURE = 0x01, 0x02, 0x03, 0x0C, 0x0D CONFIRM, RAW_FRAME = 0x80, 0x85 ITS_EPOCH_UNIX_MS = 1072915200000 # 2004-01-01T00:00:00Z LEAP_SECONDS_MS = 5000 # TimestampIts counts TAI: 5 leap seconds since 2004 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(" (own_tx, rssi, 802.11 frame) or None. Layout: u32 counter, u8 flags, i8 rssi, 'ITS5' record.""" if len(body) < 6 + 14 or body[6:10] != b"ITS5": return None flags, rssi = body[4], struct.unpack("= 4: op, fl, sq = payload[0], payload[1], struct.unpack("= 2: if body[1] == 131: last_status = body[2:] else: results[body[0]] += 1 elif op == RAW_FRAME: if fl: # fragmented: total u16, offset u16, data total, off = struct.unpack("= next_poti: send(POTI_UPDATE, poti_body()) next_poti += 1 if now >= next_cam: send(BTP_DATA_REQUEST, btp_request_body(secured, payload)) sent += 1 next_cam += 1.0 / args.rate if now >= next_status: send(STATUS_REQUEST) next_status += 1 pump() end = time.time() + 2 send(STATUS_REQUEST) while time.time() < end: pump() print(f"\nrequests sent {sent}, CONFIRM results {dict(results)} (0 = accepted)") if last_status: u32 = lambda o: struct.unpack_from("