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
+21 -17
View File
@@ -1,4 +1,5 @@
#include "link_service.hpp"
#include "link_fragment.hpp"
#include "simple_ble.hpp"
#include "serial_link.hpp"
#include <esp_log.h>
@@ -19,7 +20,7 @@ LinkService::LinkService(Station& station) : station_(station) {
});
// MicrOBU: every ITS message heard on air, verified or not (Station::on_raw_its).
station_.on_raw_its([this](const link::Bytes& body) {
broadcast(link::Opcode::V2X_RX, ++own_sequence_, body);
return broadcast(link::Opcode::V2X_RX, ++own_sequence_, body);
});
// T_GenVam is included in the status snapshot returned on request.
station_.on_id_event([this](const link::IdChangeEvent& event) {
@@ -34,7 +35,11 @@ bool LinkService::send(LinkTransport transport, link::Opcode opcode, std::uint16
link::Message message {{opcode, flags, sequence}, body};
if (!link::encode(message, octets)) { ESP_LOGE(TAG, "message 0x%02x too long (%u)", unsigned(opcode), unsigned(body.size())); return false; }
if (transport == LinkTransport::serial) {
if (opcode == link::Opcode::RAW_FRAME) return serial::try_write(serial::FrameType::LINK, octets);
// Never wait for the host: RAW_FRAME and the fragments of a large V2X_RX are best effort, a stalled USB
// host must not hold the station task that also drains the radio queue. A V2X_RX that fits one message
// keeps the blocking write it always had.
if (opcode == link::Opcode::RAW_FRAME || (opcode == link::Opcode::V2X_RX && flags != 0))
return serial::try_write(serial::FrameType::LINK, octets);
return serial::write(serial::FrameType::LINK, octets);
}
return ble::write(octets);
@@ -42,26 +47,25 @@ bool LinkService::send(LinkTransport transport, link::Opcode opcode, std::uint16
bool LinkService::send_fragmented(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
const std::size_t limit = transport == LinkTransport::ble ? ble::max_message() : link::maximum_message;
const std::size_t chunk = limit - link::header_size;
if (body.size() <= chunk) return send(transport, opcode, sequence, body);
if (body.size() > 65535 || chunk <= link::fragment_header_size) return false;
const std::size_t capacity = chunk - link::fragment_header_size;
for (std::size_t offset = 0; offset < body.size(); offset += capacity) {
const std::size_t n = std::min(capacity, body.size() - offset);
const std::uint8_t flags = offset == 0 ? link::flag_fragment_first :
(offset + n == body.size() ? link::flag_fragment_last : link::flag_fragment_middle);
link::Bytes part {std::uint8_t(body.size()), std::uint8_t(body.size() >> 8),
std::uint8_t(offset), std::uint8_t(offset >> 8)};
part.insert(part.end(), body.begin() + offset, body.begin() + offset + n);
if (!send(transport, opcode, sequence, part, flags)) return false;
if (body.size() <= link::unfragmented_capacity(limit)) return send(transport, opcode, sequence, body);
// A received message is never worth a flood of notifications: with a small ATT MTU (no exchange yet, 23) one
// MAPEM would need dozens, so beyond max_received_fragments that transport skips the message.
const std::size_t max_fragments = opcode == link::Opcode::V2X_RX ? max_received_fragments : 65535;
std::vector<link::Fragment> fragments;
if (!link::split_fragments(body, limit, fragments, max_fragments)) return false;
for (const auto& fragment : fragments) {
// The first failed fragment ends this message (the app drops the incomplete stream by itself); nothing is
// retried, and nothing here waits for the transport.
if (!send(transport, opcode, sequence, fragment.body, fragment.flags)) return false;
}
return true;
}
void LinkService::broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
bool LinkService::broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
// Fragmented when needed: a MAPEM indication is larger than one message on either transport.
send_fragmented(LinkTransport::serial, opcode, sequence, body);
if (ble::connected()) send_fragmented(LinkTransport::ble, opcode, sequence, body);
const bool serial_taken = send_fragmented(LinkTransport::serial, opcode, sequence, body);
const bool ble_taken = ble::connected() && send_fragmented(LinkTransport::ble, opcode, sequence, body);
return serial_taken || ble_taken;
}
void LinkService::reply(LinkTransport transport, std::uint16_t sequence, link::Code code, const link::Bytes& detail) {