Add a receive path switch: V2X_RX or the spec's RAW_FRAME, with fragment reassembly

Settings > Connection > ESP32-C5 > Receive path chooses where received ITS messages come from.
V2X_RX (default) is the MicrOBU extension the board builds with gn_unwrap.c. Raw frames is the
station-link RAW_FRAME debug capture: the app switches RAW_CAPTURE on (mode 3), reassembles
fragmented link messages as the spec defines (LinkReassembly), parses the ITS5 record
(RawFrame) and unwraps the 802.11 frame with GnUnwrap, a Kotlin port of gn_unwrap.c. Both end
as the same V2X_RX frames, so nothing above the link changes, and the firmware keeps both.

GnUnwrapTest checks the port against the C code on 602 recorded, cut and corrupted frames
(corpus from obu-firmware/test/host: make corpus). 145 app tests. Fix the Connection text that
claimed the app picks the strongest micrOBU.
This commit is contained in:
Ashin Walpola
2026-11-11 16:19:32 +01:00
parent c96fd0086f
commit 1697091d3a
20 changed files with 1503 additions and 7 deletions
+9
View File
@@ -101,6 +101,15 @@ eFuse-based gates, an on-device TS 102 941 client (enrolment and authorisation r
update), the `RAW_CAPTURE` debug path, fragmentation of link messages, the XIAO and `mainboard-v5` boards, and the
battery reading.
### Both receive paths stay
`V2X_RX` (ours) and `RAW_FRAME` / `RAW_CAPTURE` (Niklas Dathe's, in the spec) both stay in the firmware, and the app has a switch
(Settings > Connection > ESP32-C5 > Receive path) that chooses which one it consumes. The firmware does not know which: it
always sends `V2X_RX`, and sends `RAW_FRAME` while `RAW_CAPTURE` is on (the app turns it on, mode 0x03, only in raw mode). In raw
mode the app unwraps the frame itself with `GnUnwrap.kt`, a port of `gn_unwrap.c` that `GnUnwrapTest` checks against the C code
on a corpus (`make corpus` in `test/host` writes `app/src/test/resources/gn_unwrap_corpus.tsv`). The app also reassembles
fragmented link messages as the spec defines them, so a raw frame or an indication above one link message works.
### Measured on the bench, 2026-11-11
Production XIAO on COM10, Pixel 9 Pro on BLE, COM3 board as an on-air listener (details and open points in `TODO.md`):
+12 -1
View File
@@ -27,7 +27,7 @@ CFLAGS = -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Werror -I$(FW) -I. \
FW_SRCS = $(FW)/geonet.c dot11p.c $(FW)/gn_unwrap.c
DEPS = test_util.c test_util.h dot11p.h $(FW_SRCS) $(wildcard $(FW)/*.h)
.PHONY: all check chain replay fuzz battery firmware clean
.PHONY: all check chain replay fuzz battery firmware corpus clean
all: check
@@ -82,3 +82,14 @@ $(BUILD)/test_firmware$(EXE): test_firmware.cpp $(FW_CXX_SRCS) $(wildcard $(FW)/
firmware: $(BUILD)/test_firmware$(EXE)
$(BUILD)/test_firmware$(EXE)
# Corpus for the app's GnUnwrapTest (the Kotlin port of gn_unwrap.c): recorded frames, cut and corrupted copies of
# them, with the verdict and fields of the firmware's own code. Writes ../../../app/src/test/resources/gn_unwrap_corpus.tsv
CORPUS_OUT = ../../../app/src/test/resources/gn_unwrap_corpus.tsv
$(BUILD)/dump_corpus$(EXE): dump_corpus.c test_util.c test_util.h $(FW)/gn_unwrap.c $(FW)/gn_unwrap.h | $(BUILD)
$(CC) $(CFLAGS) -o $@ dump_corpus.c test_util.c $(FW)/gn_unwrap.c
corpus: $(BUILD)/dump_corpus$(EXE)
mkdir -p $(dir $(CORPUS_OUT))
$(BUILD)/dump_corpus$(EXE) $(CORPUS_OUT) $(RECORDINGS)
+96
View File
@@ -0,0 +1,96 @@
// Writes a corpus for the app's GnUnwrapTest: frames from recorded captures, cut and corrupted copies of
// them, and what gn_unwrap_its (the firmware's own code) makes of each. The Kotlin port must give the
// same verdict and the same fields for every line.
//
// dump_corpus <out.tsv> <capture.pcap>...
//
// One line per frame, tab separated:
// frame hex | A (accepted) or R (rejected) | port | flags (bit 0 GBC area, bit 1 signed) |
// area lat | area lon | area distance A | truncated 0/1 | payload hex (empty when rejected)
//
// Selection is deterministic (fixed counts per category and a fixed-seed generator), so the file is
// reproducible. Build and run: `make corpus` in this folder.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gn_unwrap.h"
#include "test_util.h"
#define MAX_PER_CATEGORY 20
#define MAX_REJECTED 60
#define MAX_MUTATED 250
static FILE *out;
static unsigned rng_state = 12345u;
static int accepted_total, rejected_total, mutated_total, cut_total;
static int category_count[5 * 2 * 2 + 5]; // port index * 4 + signed * 2 + gbc
static int seen_accepted;
static unsigned rng(void) { rng_state = rng_state * 1664525u + 1013904223u; return rng_state >> 8; }
static int port_index(int port) {
switch (port) { case 2001: return 0; case 2002: return 1; case 2003: return 2; case 2004: return 3; default: return 4; }
}
static void emit(const uint8_t *frame, int len) {
gn_rx_t rx;
const bool ok = gn_unwrap_its(frame, len, &rx);
for (int i = 0; i < len; i++) fprintf(out, "%02x", frame[i]);
if (!ok) { fprintf(out, "\tR\t0\t0\t0\t0\t0\t0\t\n"); return; }
fprintf(out, "\tA\t%u\t%d\t%d\t%d\t%u\t%d\t", rx.btp_dest_port, (rx.has_geo_area ? 1 : 0) | (rx.signed_unverified ? 2 : 0),
(int)rx.geo_area_lat_tenmicrodeg, (int)rx.geo_area_lon_tenmicrodeg, (unsigned)rx.geo_area_distance_a_m,
rx.truncated ? 1 : 0);
for (int i = 0; i < rx.payload_len; i++) fprintf(out, "%02x", rx.payload[i]);
fprintf(out, "\n");
}
static void on_frame(const uint8_t *frame, int len, int index, void *ctx) {
(void)ctx; (void)index;
gn_rx_t rx;
if (!gn_unwrap_its(frame, len, &rx)) {
if (rejected_total < MAX_REJECTED && (rng() % 7) == 0) { emit(frame, len); ++rejected_total; }
return;
}
++seen_accepted;
const int cat = port_index(rx.btp_dest_port) * 4 + (rx.signed_unverified ? 2 : 0) + (rx.has_geo_area ? 1 : 0);
if (category_count[cat] < MAX_PER_CATEGORY) {
++category_count[cat];
emit(frame, len);
++accepted_total;
// a few cut copies: the frame ends inside the payload, inside the headers, and at odd places
const int cuts[] = {len - 3, len - 17, len / 2, len / 3, 40, 70};
for (unsigned c = 0; c < sizeof cuts / sizeof cuts[0]; c++)
if (cuts[c] > 0 && cuts[c] < len && (rng() % 3) == 0) { emit(frame, cuts[c]); ++cut_total; }
}
// corrupted copies of frames taken at a steady rate: a few flipped bytes, anywhere
if (mutated_total < MAX_MUTATED && (seen_accepted % 40) == 0) {
uint8_t *copy = malloc((size_t)len);
if (!copy) return;
memcpy(copy, frame, (size_t)len);
const int flips = 1 + (int)(rng() % 4);
for (int k = 0; k < flips; k++) copy[rng() % (unsigned)len] ^= (uint8_t)(1u << (rng() % 8));
emit(copy, len);
free(copy);
++mutated_total;
}
}
int main(int argc, char **argv) {
if (argc < 3) { fprintf(stderr, "usage: dump_corpus <out.tsv> <capture.pcap>...\n"); return 2; }
out = fopen(argv[1], "w");
if (!out) { perror(argv[1]); return 2; }
for (int i = 2; i < argc; i++) {
const int n = tu_pcap_foreach(argv[i], on_frame, NULL);
if (n < 0) { fprintf(stderr, "cannot read %s\n", argv[i]); return 2; }
}
fclose(out);
printf("corpus: %d accepted, %d cut, %d corrupted, %d rejected (of %d accepted frames seen)\n", accepted_total, cut_total,
mutated_total, rejected_total, seen_accepted);
for (int p = 0; p < 5; p++)
printf(" port index %d: unsigned SHB %d, unsigned GBC %d, signed SHB %d, signed GBC %d\n", p, category_count[p * 4],
category_count[p * 4 + 1], category_count[p * 4 + 2], category_count[p * 4 + 3]);
return 0;
}