Forward MAPEM and VAM from the ESP32-C5, and add a bench listener

gn_unwrap.c now passes BTP ports 2003 (MAPEM) and 2018 (VAM), signed or
not, besides CAM, DENM and SPATEM. A message over 494 bytes still does not
fit one 512-octet link message (also the largest GATT value) and is dropped
and counted, not fragmented. The bench RSU's MAPEM is 295 bytes.

CONFIG_MICROBU_BENCH_LISTEN (off in sdkconfig) is for a board whose only PC
connection is the UART0 console: it configures itself receive-only at boot
and logs every V2X_RX message; tools/bench_listen_watch.py tallies the log.
On air on 2026-10-28 it forwarded 104 MAPEM, 185 SPATEM and 472 CAM in
90 s, with nothing oversize. The RSU sent all of them unsigned.

Host tests: signed GeoBroadcast SPATEM/MAPEM frames of 58 to 1243 bytes
with every truncation length; the replay cuts frames at c5_radio.cpp's
2346 bytes (not the old C firmware's 800) and checks MAPEMs with asn1tools.
This commit is contained in:
Ashin Walpola
2026-10-28 16:48:37 +01:00
parent 9f77f9f0e4
commit fc3354a4ba
12 changed files with 246 additions and 55 deletions
+15 -10
View File
@@ -22,20 +22,22 @@ import sys
import asn1tools
ROOT = pathlib.Path(__file__).resolve().parents[3]
PORT_NAMES = {2001: "CAM", 2002: "DENM", 2004: "SPATEM"}
# main.c's RX_FRAME_MAX_LEN: the most of any frame the board hands to gn_unwrap_its. test_replay
PORT_NAMES = {2001: "CAM", 2002: "DENM", 2003: "MAPEM", 2004: "SPATEM", 2018: "VAM"}
# c5_radio.cpp's Raw::bytes: the most of any frame the board hands to gn_unwrap_its. test_replay
# cuts frames to it, so this does too.
RX_FRAME_MAX_LEN = 800
RX_FRAME_MAX_LEN = 2346
def compile_specs():
a = ROOT / "asn1"
spat_map = asn1tools.compile_files(
[str(a / n) for n in ("spatem_2_2_1.asn", "mapem_2_2_1.asn", "dsrc_2_2_1.asn", "cdd_2_2_1.asn")], "uper")
uper = {
2001: asn1tools.compile_files([str(a / "cam_1_4_1.asn"), str(a / "cdd_1_3_1_1.asn")], "uper"),
2002: asn1tools.compile_files([str(a / "denm_1_3_1.asn"), str(a / "cdd_1_3_1_1.asn")], "uper"),
2004: asn1tools.compile_files(
[str(a / n) for n in ("spatem_2_2_1.asn", "mapem_2_2_1.asn", "dsrc_2_2_1.asn", "cdd_2_2_1.asn")],
"uper"),
2003: spat_map,
2004: spat_map,
# no VAM module in asn1/: VAMs are checked against the C parse only, not decoded
}
names = ("IEEE1609dot2BaseTypes.asn", "IEEE1609dot2.asn")
for d in (a, ROOT / "vanetza" / "asn1"):
@@ -205,10 +207,10 @@ def main(argv):
for key in got:
disagreements.append((key, "row from test_replay for a record this did not see"))
print("check_replay: %d records (%d cut to %d bytes), %d accepted (CAM %d, DENM %d, SPATEM %d; "
"%d signed, %d truncated)"
print("check_replay: %d records (%d cut to %d bytes), %d accepted (CAM %d, DENM %d, MAPEM %d, "
"SPATEM %d, VAM %d; %d signed, %d truncated)"
% (stats["records"], stats["capped"], RX_FRAME_MAX_LEN, stats["accepted"], stats["CAM"],
stats["DENM"], stats["SPATEM"], stats["signed"], stats["truncated"]))
stats["DENM"], stats["MAPEM"], stats["SPATEM"], stats["VAM"], stats["signed"], stats["truncated"]))
if sec_dir:
print(" envelopes decoded with asn1tools using %s" % sec_dir.relative_to(ROOT))
print(" C vs independent parse: %s" % ("agree on every record" if not disagreements
@@ -218,7 +220,10 @@ def main(argv):
outcome, failures = collections.Counter(), []
for (port, payload), key in messages.items():
name, spec = PORT_NAMES[port], uper[port]
name, spec = PORT_NAMES[port], uper.get(port)
if spec is None:
outcome[(name, "not decoded (no module)")] += 1
continue
try:
decoded = spec.decode(name, payload)
except Exception as e:
+81 -17
View File
@@ -374,7 +374,9 @@ static uint8_t *put32(uint8_t *p, uint32_t v)
return put16(p, (uint16_t)v);
}
static int build_gbc(uint8_t subtype, int32_t area_lat, int32_t area_lon, uint16_t dist_a, uint8_t *f)
// Any BTP-B message by GeoBroadcast, the way the bench RSU sends SPATEM and MAPEM too.
static int build_gbc_msg(uint8_t subtype, int32_t area_lat, int32_t area_lon, uint16_t dist_a,
uint16_t port, const uint8_t *payload, int payload_len, uint8_t *f)
{
uint8_t *p = f;
// 802.11 QoS Data: FC, duration, addr1..3, sequence control, QoS control
@@ -392,7 +394,7 @@ static int build_gbc(uint8_t subtype, int32_t area_lat, int32_t area_lon, uint16
// Common: NH BTP-B, HT 4 (GBC) / subtype = area shape, TC 1, flags 0 (stationary), PL, MHL, reserved
const uint8_t common[4] = {0x20, (uint8_t)(0x40 | subtype), 0x01, 0x00};
p = put(p, common, 4);
p = put16(p, (uint16_t)(BTP_B + DENM_STUB_LEN));
p = put16(p, (uint16_t)(BTP_B + payload_len));
*p++ = 10;
*p++ = 0;
// GBC extended header: SN, reserved, SO PV (GN_ADDR with station type 15 = RSU, MID, TST, lat,
@@ -412,13 +414,18 @@ static int build_gbc(uint8_t subtype, int32_t area_lat, int32_t area_lon, uint16
p = put16(p, subtype ? 40 : 0);
p = put16(p, subtype ? 900 : 0);
p = put16(p, 0);
// BTP-B: DENM
p = put16(p, 2002);
// BTP-B
p = put16(p, port);
p = put16(p, 0);
p = put(p, k_denm_stub, DENM_STUB_LEN);
p = put(p, payload, payload_len);
return (int)(p - f);
}
static int build_gbc(uint8_t subtype, int32_t area_lat, int32_t area_lon, uint16_t dist_a, uint8_t *f)
{
return build_gbc_msg(subtype, area_lat, area_lon, dist_a, 2002, k_denm_stub, DENM_STUB_LEN, f);
}
static void test_gbc_rx(void)
{
static const struct { uint8_t subtype; int32_t lat, lon; uint16_t dist_a; } areas[] = {
@@ -481,10 +488,12 @@ static void test_rejections(void)
{"Beacon (HeaderType 1)", 37, 0x10, false, 0},
{"GeoUnicast (HeaderType 2)", 37, 0x20, false, 0},
{"multi-hop TSB (HeaderType 5, subtype 1)", 37, 0x51, false, 0},
{"BTP port 2003 (MAPEM, not accepted yet)", 73, 0xD3, false, 0},
{"BTP port 2018 (VAM, not accepted yet)", 73, 0xE2, false, 0},
{"BTP port 2002 (DENM)", 73, 0xD2, true, 2002},
{"BTP port 2003 (MAPEM)", 73, 0xD3, true, 2003},
{"BTP port 2004 (SPATEM)", 73, 0xD4, true, 2004},
{"BTP port 2018 (VAM)", 73, 0xE2, true, 2018},
{"BTP port 2005 (IVIM, not accepted)", 73, 0xD5, false, 0},
{"BTP port 2006 (SREM, not accepted)", 73, 0xD6, false, 0},
};
for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
tu_set_context("mutation: %s", cases[i].what);
@@ -583,16 +592,14 @@ static int put_coer_length(int len, uint8_t *out)
return 3;
}
// A secured CAM frame shaped like the recorded ones: our own SHB packet with the Basic Header's
// NextHeader set to 2 and everything after the Basic Header wrapped as `prefix` + COER length +
// inner packet, then the signature stand-in. Sets where the ITS payload starts and ends.
static int build_secured(const uint8_t *prefix, int prefix_len, const uint8_t *payload,
int payload_len, uint8_t *f, int *payload_start, int *payload_end)
// Secures an unsecured frame `plain` shaped like the recorded signed ones: the Basic Header (at
// `basic`) gets NextHeader 2 and everything after it is wrapped as `prefix` + COER length + inner
// packet, then the signature stand-in. `ext_len` is the inner packet's extended-header length.
// Sets where the ITS payload starts and ends.
static int secure_frame(const uint8_t *plain, int plain_len, int basic, int ext_len,
const uint8_t *prefix, int prefix_len, uint8_t *f,
int *payload_start, int *payload_end)
{
const gn_lpv_t lpv = test_lpv();
uint8_t plain[FRAME_BUF_LEN];
const int plain_len = build(payload, payload_len, &lpv, 2001, false, plain, sizeof plain);
const int basic = MAC_HDR + LLC_SNAP;
const int inner = basic + GN_BASIC; // the Common Header onward
const int inner_len = plain_len - inner;
@@ -603,7 +610,7 @@ static int build_secured(const uint8_t *prefix, int prefix_len, const uint8_t *p
n += prefix_len;
n += put_coer_length(inner_len, f + n);
memcpy(f + n, plain + inner, (size_t)inner_len);
*payload_start = n + GN_COMMON + SHB_EXT + BTP_B;
*payload_start = n + GN_COMMON + ext_len + BTP_B;
n += inner_len;
*payload_end = n;
for (int i = 0; i < SIG_STUB_LEN; i++) {
@@ -612,6 +619,17 @@ static int build_secured(const uint8_t *prefix, int prefix_len, const uint8_t *p
return n;
}
// A secured CAM: our own SHB packet, secured.
static int build_secured(const uint8_t *prefix, int prefix_len, const uint8_t *payload,
int payload_len, uint8_t *f, int *payload_start, int *payload_end)
{
const gn_lpv_t lpv = test_lpv();
uint8_t plain[FRAME_BUF_LEN];
const int plain_len = build(payload, payload_len, &lpv, 2001, false, plain, sizeof plain);
return secure_frame(plain, plain_len, MAC_HDR + LLC_SNAP, SHB_EXT, prefix, prefix_len, f,
payload_start, payload_end);
}
static const uint8_t k_signed_prefix[] = {0x03, 0x81, 0x00, 0x40, 0x03, 0x80};
static const uint8_t k_unsecured_prefix[] = {0x03, 0x80};
@@ -690,6 +708,51 @@ static void test_secured(void)
CHECK(!gn_unwrap_its(tu_guarded(f, len), len, &rx), "payload overrunning its envelope: accepted");
}
// Signed SPATEM and MAPEM by GeoBroadcast. No recording holds one yet (the bench RSU sent all three
// of its messages unsigned on 2026-10-28), so these are the RSU's recorded GBC frames with the
// signed envelope of the recorded CAMs and DENMs around them. 494 bytes is the largest message a
// link message carries (Station::forward_raw); 1243 is the largest SPATEM of the 2026-03-18 drive,
// which gn_unwrap_its must still unwrap whole even though the station then drops it as oversize.
static void test_secured_gbc(void)
{
static const struct { const char *what; uint16_t port; uint8_t message_id; int payload_len; } cases[] = {
{"signed GBC SPATEM, bench size", 2004, 4, 58},
{"signed GBC MAPEM, bench size", 2003, 5, 295},
{"signed GBC MAPEM, largest that fits a link message", 2003, 5, 494},
{"signed GBC SPATEM, largest seen on the road", 2004, 4, 1243},
};
static uint8_t payload[1243], plain[1400], f[1400];
for (size_t c = 0; c < sizeof cases / sizeof cases[0]; c++) {
tu_set_context("%s", cases[c].what);
const int n = cases[c].payload_len;
payload[0] = 0x02; // ItsPduHeader: protocolVersion 2, messageID, stationID
payload[1] = cases[c].message_id;
for (int i = 2; i < n; i++) {
payload[i] = (uint8_t)(i * 7 + cases[c].port);
}
const int plain_len = build_gbc_msg(0, 535540000, 100220000, 400, cases[c].port, payload, n, plain);
int start, end;
const int len = secure_frame(plain, plain_len, MAC_HDR + QOS_CTRL + LLC_SNAP, GBC_EXT,
k_signed_prefix, (int)sizeof k_signed_prefix, f, &start, &end);
gn_rx_t rx;
const uint8_t *g = tu_guarded(f, len);
const bool ok = gn_unwrap_its(g, len, &rx);
CHECK(ok, "%s: rejected", cases[c].what);
if (ok) {
CHECK(rx.btp_dest_port == cases[c].port && rx.signed_unverified && !rx.truncated &&
rx.has_geo_area && rx.geo_area_distance_a_m == 400,
"%s: port %d signed %d truncated %d area %d/%d", cases[c].what, rx.btp_dest_port,
rx.signed_unverified, rx.truncated, rx.has_geo_area, rx.geo_area_distance_a_m);
CHECK(rx.payload == g + start && rx.payload_len == n &&
memcmp(rx.payload, payload, (size_t)n) == 0,
"%s: payload offset %d length %d, expected %d / %d", cases[c].what,
(int)(rx.payload - g), rx.payload_len, start, n);
}
tu_pcap_add(f, len);
sweep(cases[c].what, f, len, start, end);
}
}
static void test_truncation(void)
{
const gn_lpv_t lpv = test_lpv();
@@ -722,6 +785,7 @@ int main(int argc, char **argv)
test_payload_length();
test_trailer();
test_secured();
test_secured_gbc();
test_truncation();
tu_pcap_close();
+10 -7
View File
@@ -17,14 +17,15 @@
#include "gn_unwrap.h"
#include "test_util.h"
// main.c's RX_FRAME_MAX_LEN: the promiscuous callback copies at most this many bytes of a frame,
// so it is the most gn_unwrap_its ever sees on the board. Keep in step with main.c.
#define RX_FRAME_MAX_LEN 800
// c5_radio.cpp's Raw::bytes: the promiscuous callback copies at most this many bytes of a frame,
// so it is the most gn_unwrap_its ever sees on the board. Keep in step with c5_radio.cpp. (The old
// C firmware's main.c cut frames at 800 bytes.)
#define RX_FRAME_MAX_LEN 2346
typedef struct {
FILE *out;
const char *path;
long records, capped, accepted, cam, denm, spatem, signed_frames, truncated, bad;
long records, capped, accepted, cam, denm, mapem, spatem, vam, signed_frames, truncated, bad;
} replay_t;
static void on_frame(const uint8_t *frame, int len, int index, void *ctx)
@@ -57,7 +58,9 @@ static void on_frame(const uint8_t *frame, int len, int index, void *ctx)
r->accepted++;
r->cam += rx.btp_dest_port == 2001;
r->denm += rx.btp_dest_port == 2002;
r->mapem += rx.btp_dest_port == 2003;
r->spatem += rx.btp_dest_port == 2004;
r->vam += rx.btp_dest_port == 2018;
r->signed_frames += rx.signed_unverified;
r->truncated += rx.truncated;
}
@@ -91,9 +94,9 @@ int main(int argc, char **argv)
}
fclose(r.out);
printf("test_replay: %ld records (%ld cut to %d bytes as main.c does), %ld accepted (CAM %ld, "
"DENM %ld, SPATEM %ld; %ld signed, %ld truncated), %ld bad\n",
r.records, r.capped, RX_FRAME_MAX_LEN, r.accepted, r.cam, r.denm, r.spatem,
printf("test_replay: %ld records (%ld cut to %d bytes as c5_radio.cpp does), %ld accepted (CAM %ld, "
"DENM %ld, MAPEM %ld, SPATEM %ld, VAM %ld; %ld signed, %ld truncated), %ld bad\n",
r.records, r.capped, RX_FRAME_MAX_LEN, r.accepted, r.cam, r.denm, r.mapem, r.spatem, r.vam,
r.signed_frames, r.truncated, r.bad);
return (r.bad || unreadable) ? 1 : 0;
}