82 lines
3.9 KiB
Python
82 lines
3.9 KiB
Python
"""Which received ITS messages did the phone get, by size? Compares a V2X log (schema 3, ESP32-C5) with a
|
|||
|
|
pcap recorded at the same time by another receiver (the CiT node's its5_*.pcap, linktype 105 or 127).
|
||
|
|
|
||
|
|
# 1. unwrap the capture with the firmware's own code (obu-firmware/test/host, MSYS2 gcc on PATH):
|
||
|
|
# gcc -std=c11 -O2 -I../../main -I. -o test_replay test_replay.c test_util.c \
|
||
|
|
# ../../main/geonet.c dot11p.c ../../main/gn_unwrap.c
|
||
|
|
# test_replay replay.tsv capture.pcap
|
||
|
|
# 2. py -3.11 tools/rx_size_compare.py LOG.db capture.pcap replay.tsv
|
||
|
|
|
||
|
|
Written for the 2026-11-23 bench run: the node sent a 377-byte MAPEM and a 58-byte SPATEM, then an 1198-byte
|
||
|
|
MAPEM and an 888-byte SPATEM, and the phone received only the small ones (the firmware dropped everything over
|
||
|
|
494 bytes). Run it again after the V2X_RX fragmentation to see the large ones arrive.
|
||
|
|
|
||
|
|
The pcap clock is the capture device's own (often seconds since its boot, and it can wrap), so it is aligned to
|
||
|
|
the phone's wall clock through the CAMs both received byte for byte. A message counts as received when the
|
||
|
|
phone logged the same message type with identical payload bytes within 1 s of it. The pcap receiver is a
|
||
|
|
different antenna in a different place, so even small messages sit below 100 %: compare sizes with each other.
|
||
|
|
"""
|
||
|
|
import collections
|
||
|
|
import csv
|
||
|
|
import sqlite3
|
||
|
|
import statistics
|
||
|
|
import struct
|
||
|
|
import sys
|
||
|
|
|
||
|
|
NAMES = {2001: 'CAM', 2002: 'DENM', 2003: 'MAPEM', 2004: 'SPATEM', 2018: 'VAM'}
|
||
|
|
WINDOW_S = 1.0
|
||
|
|
|
||
|
|
|
||
|
|
def pcap_times(path):
|
||
|
|
data = open(path, 'rb').read()
|
||
|
|
little = struct.unpack('<I', data[:4])[0] in (0xA1B2C3D4, 0xA1B23C4D)
|
||
|
|
e = '<' if little else '>'
|
||
|
|
nano = struct.unpack(e + 'I', data[:4])[0] in (0xA1B23C4D,)
|
||
|
|
out, off = [], 24
|
||
|
|
while off + 16 <= len(data):
|
||
|
|
s, us, incl, _ = struct.unpack(e + 'IIII', data[off:off + 16])
|
||
|
|
out.append(s + us / (1e9 if nano else 1e6))
|
||
|
|
off += 16 + incl
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
def main(log, pcap, tsv):
|
||
|
|
times = pcap_times(pcap)
|
||
|
|
heard = []
|
||
|
|
for r in csv.DictReader(open(tsv), delimiter='\t'):
|
||
|
|
if r['accepted'] == '1' and int(r['port']) in NAMES:
|
||
|
|
heard.append((times[int(r['record'])], int(r['port']), bytes.fromhex(r['payload'])))
|
||
|
|
con = sqlite3.connect('file:%s?mode=ro' % log.replace('\\', '/'), uri=True)
|
||
|
|
phone = con.execute('select ts_ms, msg_type, payload from messages order by ts_ms, id').fetchall()
|
||
|
|
meta = dict(con.execute('select key, value from meta').fetchall())
|
||
|
|
if not phone:
|
||
|
|
raise SystemExit('the log holds no messages')
|
||
|
|
seen = collections.defaultdict(list)
|
||
|
|
for ts, kind, payload in phone:
|
||
|
|
seen[(kind, bytes(payload))].append(ts / 1000)
|
||
|
|
cams = {k[1]: v[0] for k, v in seen.items() if k[0] == 'CAM' and len(v) == 1}
|
||
|
|
offsets = [cams[p] - t for t, port, p in heard if port == 2001 and p in cams]
|
||
|
|
if len(offsets) < 20:
|
||
|
|
raise SystemExit('only %d CAMs in common: cannot align the two clocks' % len(offsets))
|
||
|
|
off = statistics.median(offsets)
|
||
|
|
print('log: %s, hardware %s; %d messages; clock offset %.3f s (spread %.0f ms over %d shared CAMs)'
|
||
|
|
% (log, meta.get('hardware'), len(phone), off, 1000 * statistics.pstdev(offsets), len(offsets)))
|
||
|
|
first, last = phone[0][0] / 1000, phone[-1][0] / 1000
|
||
|
|
table = collections.defaultdict(lambda: [0, 0])
|
||
|
|
for t, port, p in heard:
|
||
|
|
t += off
|
||
|
|
if not first - WINDOW_S <= t <= last + WINDOW_S:
|
||
|
|
continue
|
||
|
|
key = (NAMES[port], len(p))
|
||
|
|
table[key][0] += 1
|
||
|
|
table[key][1] += any(abs(x - t) < WINDOW_S for x in seen.get((NAMES[port], p), ()))
|
||
|
|
print('%-7s %6s %9s %9s %7s' % ('type', 'bytes', 'on air', 'phone', 'share'))
|
||
|
|
for (kind, size), (n, got) in sorted(table.items()):
|
||
|
|
print('%-7s %6d %9d %9d %6.1f%%' % (kind, size, n, got, 100.0 * got / n))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
if len(sys.argv) != 4:
|
||
|
|
raise SystemExit(__doc__)
|
||
|
|
main(*sys.argv[1:])
|