Fragment large V2X_RX messages and decode road-RSU SPATEMs

The firmware dropped every received ITS message over 494 bytes (one link
message), so a road RSU's 888-byte SPATEM and 1198-byte MAPEM never reached
the phone. V2X_RX now goes out in link fragments up to 2300 bytes, best
effort and non-blocking; messages up to 494 bytes are unchanged.

The app's SPATEM decoder then refused those messages because they carry
intersection and movement names and advisory speed lists. It now reads past
both, checked against asn1tools on 718 distinct messages.

Adds the pure fragmenter with host tests and a pcap replay, a Kotlin
cross-check on the firmware's own fragments, tools/rx_size_compare.py,
docs/08-large-message-reception.md and station-link revision 3.
This commit is contained in:
Ashin Walpola
2026-11-23 17:34:56 +01:00
parent 7dd83dba3d
commit ce18a393ea
24 changed files with 3305 additions and 2305 deletions
+120
View File
@@ -9,6 +9,7 @@
#include "battery_policy.hpp"
#include "cca_window.hpp"
#include "its_clock.hpp"
#include "link_fragment.hpp"
#include "link_protocol.hpp"
#include "radio_policy.hpp"
#include "raw_capture_policy.hpp"
@@ -369,6 +370,125 @@ TEST(the_494_octet_limit_is_exact) {
}
}
TEST(v2x_rx_plan_keeps_small_messages_single_and_fragments_large_ones) {
gn_rx_t rx {};
const std::uint8_t byte = 0;
rx.payload = &byte;
const auto plan = [&](int len, bool raw_on, bool truncated = false) {
rx.payload_len = len; rx.truncated = truncated;
return link::v2x_rx_plan(rx, raw_on);
};
using P = link::V2xRxPlan;
CHECK(plan(1, false) == P::single);
CHECK(plan(58, false) == P::single);
CHECK(plan(494, false) == P::single);
CHECK(plan(495, false) == P::fragmented);
CHECK(plan(888, false) == P::fragmented);
CHECK(plan(1198, false) == P::fragmented);
CHECK(plan(2300, false) == P::fragmented);
CHECK(plan(2301, false) == P::drop);
CHECK(plan(0, false) == P::drop);
CHECK(plan(-1, false) == P::drop);
CHECK(plan(600, false, true) == P::drop); // truncated is never forwarded
// raw capture on: the app reads RAW_FRAME, so a large message is not sent twice; small ones are as before
CHECK(plan(494, true) == P::single);
CHECK(plan(495, true) == P::covered_by_raw);
CHECK(plan(1198, true) == P::covered_by_raw);
CHECK(plan(2301, true) == P::drop);
}
// What a phone does with the fragments, written here without link::RequestReassembly.
static bool put_back(const std::vector<link::Fragment>& fragments, Bytes& out) {
out.clear();
std::size_t total = 0;
for (std::size_t i = 0; i < fragments.size(); ++i) {
const auto& b = fragments[i].body;
if (b.size() <= link::fragment_header_size) return false;
const std::size_t t = b[0] | (std::size_t(b[1]) << 8), offset = b[2] | (std::size_t(b[3]) << 8);
if (fragments[i].flags != (i == 0 ? 1 : i + 1 == fragments.size() ? 3 : 2)) return false;
if (i == 0) total = t;
if (t != total || offset != out.size()) return false;
out.insert(out.end(), b.begin() + link::fragment_header_size, b.end());
}
return out.size() == total;
}
TEST(fragments_of_a_large_v2x_rx_reassemble_exactly_on_both_transports) {
for (const int len : {495, 504, 888, 1198, 2300}) {
Bytes mapem(len);
for (std::size_t i = 0; i < mapem.size(); ++i) mapem[i] = static_cast<std::uint8_t>(i * 31 + len);
Bytes frame;
CHECK(air_frame(mapem, 2003, frame));
gn_rx_t rx;
CHECK(gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx));
CHECK_EQ(rx.payload_len, len);
const auto body = link::encode_v2x_rx(rx, -48);
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) { // serial / MTU 517, MTU 247
std::vector<link::Fragment> fragments;
const bool split = link::split_fragments(body, limit, fragments, 16);
CHECK(split);
if (!split) continue;
CHECK(fragments.size() >= 2);
Bytes rebuilt;
CHECK(put_back(fragments, rebuilt));
CHECK(rebuilt == body);
for (std::size_t i = 0; i < fragments.size(); ++i) {
Bytes wire;
CHECK(link::encode(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 5}, fragments[i].body}, wire));
CHECK(wire.size() <= limit);
if (i + 1 < fragments.size()) CHECK_EQ(wire.size(), limit); // full except the last
}
}
}
}
TEST(fragment_counts_for_the_bench_messages_at_the_full_mtu) {
// 14 octets of prefix + the UPER payload; a fragment carries 512 - 4 - 4 = 504 octets of it
std::vector<link::Fragment> f;
CHECK(link::split_fragments(Bytes(14 + 888), 512, f)); CHECK_EQ(f.size(), 2u);
CHECK(link::split_fragments(Bytes(14 + 1198), 512, f)); CHECK_EQ(f.size(), 3u);
CHECK(link::split_fragments(Bytes(14 + 495), 512, f)); CHECK_EQ(f.size(), 2u);
CHECK_STR(to_hex(Bytes(f[0].body.begin(), f[0].body.begin() + 4)), std::string("fd010000")); // total 509, offset 0
CHECK_STR(to_hex(Bytes(f[1].body.begin(), f[1].body.begin() + 4)), std::string("fd01f801")); // total 509, offset 504
}
TEST(a_message_that_fits_one_link_message_is_never_fragmented) {
std::vector<link::Fragment> f;
CHECK(!link::split_fragments(Bytes(508), 512, f)); // 508 + 4 = 512: exactly one message
CHECK(f.empty());
CHECK(link::split_fragments(Bytes(509), 512, f));
CHECK(!link::split_fragments(Bytes(240), 244, f)); // MTU 247: 240 + 4 = 244
CHECK(link::split_fragments(Bytes(241), 244, f));
}
TEST(fragmenting_refuses_what_a_transport_cannot_carry_sensibly) {
std::vector<link::Fragment> f;
CHECK(!link::split_fragments(Bytes(1212), 20, f, 8)); // ATT MTU 23: 12 octets a fragment, 101 fragments
CHECK(f.empty());
CHECK(!link::split_fragments(Bytes(1212), 8, f)); // no room for data at all
CHECK(!link::split_fragments(Bytes(65536), 512, f)); // total is a u16
CHECK(link::split_fragments(Bytes(65535), 512, f));
CHECK(link::split_fragments(Bytes(1212), 244, f, 6)); // exactly 6 fragments of 236
CHECK(!link::split_fragments(Bytes(1212), 244, f, 5));
}
TEST(a_lost_fragment_loses_that_message_only) {
std::vector<link::Fragment> a, b;
Bytes first(1212, 0xA1), second(1212, 0xB2);
CHECK(link::split_fragments(first, 512, a));
CHECK(link::split_fragments(second, 512, b));
link::RequestReassembly phone;
std::optional<link::Message> done;
for (std::size_t i = 0; i < a.size(); ++i) {
if (i == 1) continue; // the middle fragment never arrives
done = phone.feed(link::Message {{link::Opcode::V2X_RX, a[i].flags, 1}, a[i].body}, 1, i);
CHECK(!done);
}
for (std::size_t i = 0; i < b.size(); ++i) done = phone.feed(link::Message {{link::Opcode::V2X_RX, b[i].flags, 2}, b[i].body}, 1, 10 + i);
CHECK(done.has_value());
if (done) CHECK(done->body == second);
}
TEST(ports_outside_the_its_set_are_not_forwarded) {
Bytes frame;
CHECK(air_frame(Bytes(20, 1), 1234, frame));