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:
@@ -52,13 +52,13 @@
|
||||
// only this path). Other ports are dropped. The link message itself is generic: the port says what
|
||||
// the payload is.
|
||||
//
|
||||
// Size caveat: a link message is at most 512 octets (link::maximum_message, which is also the
|
||||
// largest GATT attribute value), so Station::forward_raw() drops a message whose UPER exceeds
|
||||
// 494 bytes and counts it as oversize instead of forwarding it. Nothing is fragmented. The bench
|
||||
// RSU's messages all fit (2026-10-28 capture: SPATEM 58, MAPEM 295 bytes); real road SPATEMs and
|
||||
// MAPEMs often do not (SPATEM 555 bytes median, 1243 max on the 2026-03-18 drive). The radio
|
||||
// itself captures frames of up to 2346 bytes (c5_radio.cpp), so `truncated` does not happen on
|
||||
// this firmware.
|
||||
// Size: a link message is at most 512 octets (link::maximum_message, which is also the largest
|
||||
// GATT attribute value), so a UPER up to 494 bytes goes to the phone as one V2X_RX message. A larger
|
||||
// one (real road SPATEMs and MAPEMs: SPATEM 555 bytes median, 1243 max on the 2026-03-18 drive;
|
||||
// the 2026-11-23 bench node sent 888 and 1198) is sent by Station::forward_raw() in link fragments,
|
||||
// see v2x_rx.hpp (v2x_rx_plan) and link_fragment.hpp. Only a message beyond v2x_rx_max_forward is
|
||||
// dropped and counted as oversize. The radio itself captures frames of up to 2346 bytes
|
||||
// (c5_radio.cpp), so `truncated` does not happen on this firmware.
|
||||
//
|
||||
// No FCS/CRC check here: the WiFi driver has already validated the frame.
|
||||
typedef struct {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
// Splits a link message body into fragments (station-link "Fragmented messages": the link header with
|
||||
// flags 1 first / 2 middle / 3 last and the same opcode and sequence on every fragment, then
|
||||
// u16 total body size, u16 body offset, data). Header-only and free of ESP-IDF, so the host tests compile it
|
||||
// unmodified and check it against the app's reassembly. LinkService::send_fragmented is the only caller.
|
||||
#include "link_protocol.hpp"
|
||||
|
||||
namespace microbu::link {
|
||||
|
||||
/// One fragment: the flags for the link header and the body (u16 total, u16 offset, data).
|
||||
struct Fragment {
|
||||
std::uint8_t flags;
|
||||
Bytes body;
|
||||
};
|
||||
|
||||
/// Largest body that goes out as one unfragmented message on a transport whose messages are at most `limit` octets.
|
||||
constexpr std::size_t unfragmented_capacity(std::size_t limit) {
|
||||
return limit > header_size ? limit - header_size : 0;
|
||||
}
|
||||
|
||||
/// Data octets one fragment carries on a transport whose messages are at most `limit` octets.
|
||||
constexpr std::size_t fragment_capacity(std::size_t limit) {
|
||||
return limit > header_size + fragment_header_size ? limit - header_size - fragment_header_size : 0;
|
||||
}
|
||||
|
||||
/// @brief Fragments `body`, which must not fit one message of `limit` octets (the caller sends those as they are).
|
||||
/// @param body Message body to split.
|
||||
/// @param limit Largest message the transport carries, header included (512, or the BLE ATT MTU - 3).
|
||||
/// @param out Receives the fragments in order; empty when false is returned.
|
||||
/// @param max_fragments Refuse when more fragments would be needed (a V2X_RX on a transport with a tiny MTU).
|
||||
/// @return false when the body fits one message, exceeds 65535 octets, the limit leaves no room for data, or
|
||||
/// more than max_fragments are needed.
|
||||
inline bool split_fragments(const Bytes& body, std::size_t limit, std::vector<Fragment>& out,
|
||||
std::size_t max_fragments = 65535) {
|
||||
out.clear();
|
||||
const std::size_t capacity = fragment_capacity(limit);
|
||||
if (body.size() <= unfragmented_capacity(limit) || body.size() > 65535 || capacity == 0) return false;
|
||||
if ((body.size() + capacity - 1) / capacity > max_fragments) return false;
|
||||
for (std::size_t offset = 0; offset < body.size(); offset += capacity) {
|
||||
const std::size_t n = body.size() - offset < capacity ? body.size() - offset : capacity;
|
||||
Fragment fragment;
|
||||
fragment.flags = offset == 0 ? flag_fragment_first
|
||||
: offset + n == body.size() ? flag_fragment_last : flag_fragment_middle;
|
||||
fragment.body = {static_cast<std::uint8_t>(body.size()), static_cast<std::uint8_t>(body.size() >> 8),
|
||||
static_cast<std::uint8_t>(offset), static_cast<std::uint8_t>(offset >> 8)};
|
||||
fragment.body.insert(fragment.body.end(), body.begin() + static_cast<std::ptrdiff_t>(offset),
|
||||
body.begin() + static_cast<std::ptrdiff_t>(offset + n));
|
||||
out.push_back(std::move(fragment));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace microbu::link
|
||||
@@ -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) {
|
||||
|
||||
@@ -38,7 +38,10 @@ private:
|
||||
/// @param opcode Message opcode.
|
||||
/// @param sequence Sequence number to stamp on the header.
|
||||
/// @param body Encoded message body.
|
||||
void broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body);
|
||||
/// @return whether at least one transport took the whole message (the caller only counts a false).
|
||||
bool broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body);
|
||||
/// Most fragments one received ITS message (V2X_RX) may take on a transport; 8 x 504 octets at the full MTU.
|
||||
static constexpr std::size_t max_received_fragments = 8;
|
||||
/// @brief Sends a CONFIRM reply to the message's origin transport.
|
||||
/// @param origin Transport to reply on.
|
||||
/// @param sequence Sequence number of the message being replied to.
|
||||
|
||||
@@ -162,6 +162,9 @@ bool write(FrameType type, const Bytes& payload) {
|
||||
|
||||
bool try_write(FrameType type, const Bytes& payload) {
|
||||
if (payload.size() > maximum_payload || !writer_lock || stalled) return false;
|
||||
// Like write(): no host on the native port means nothing to queue, or the 8 kB TX buffer fills with
|
||||
// fragments nobody reads and the next host starts on stale bytes.
|
||||
if (!usb_serial_jtag_is_connected()) return false;
|
||||
const auto frame = encode_frame(type, payload);
|
||||
if (xSemaphoreTake(writer_lock, 0) != pdTRUE) return false;
|
||||
const bool queued = usb_serial_jtag_write_bytes(frame.data(), frame.size(), 0) == static_cast<int>(frame.size());
|
||||
|
||||
@@ -644,20 +644,28 @@ void Station::forward_raw(const ByteBuffer& frame, int rssi) {
|
||||
// Most captured frames are not ITS traffic this handles; false is the common case, not an error.
|
||||
gn_rx_t rx;
|
||||
if (!gn_unwrap_its(frame.data(), static_cast<int>(frame.size()), &rx)) return;
|
||||
const bool fits = link::v2x_rx_fits(rx);
|
||||
if (!fits) {
|
||||
++raw_oversize_;
|
||||
static std::int64_t last_report = 0; // one line per 10 s at most, the counter has the rest
|
||||
const auto now = esp_timer_get_time();
|
||||
if (now - last_report > 10000000) {
|
||||
last_report = now;
|
||||
ESP_LOGW(TAG, "V2X_RX: port %u message of %d bytes does not fit a link message (%lu dropped so far)",
|
||||
unsigned(rx.btp_dest_port), rx.payload_len, (unsigned long)raw_oversize_);
|
||||
}
|
||||
return;
|
||||
const auto plan = link::v2x_rx_plan(rx, (raw_mode_ & C5Radio::capture_on) != 0);
|
||||
// A message above one link message goes out in fragments (LinkService), best effort: no retry, no waiting, so
|
||||
// a lost fragment costs that message only and never the receive path.
|
||||
switch (plan) {
|
||||
case link::V2xRxPlan::drop: ++raw_oversize_; break;
|
||||
case link::V2xRxPlan::covered_by_raw: ++raw_covered_; break;
|
||||
case link::V2xRxPlan::single: ++raw_forwarded_; break;
|
||||
case link::V2xRxPlan::fragmented: ++raw_fragmented_; break;
|
||||
}
|
||||
if (plan == link::V2xRxPlan::single || plan == link::V2xRxPlan::fragmented) {
|
||||
if (!raw_its_(link::encode_v2x_rx(rx, rssi)) && plan == link::V2xRxPlan::fragmented) ++raw_fragment_failed_;
|
||||
}
|
||||
if (plan == link::V2xRxPlan::single || plan == link::V2xRxPlan::covered_by_raw) return;
|
||||
static std::int64_t last_report = 0; // one line per 10 s at most, the counters have the rest
|
||||
const auto now = esp_timer_get_time();
|
||||
if (now - last_report > 10000000) {
|
||||
last_report = now;
|
||||
ESP_LOGW(TAG, "V2X_RX: port %u message of %d bytes: %lu fragmented (%lu not taken by any transport), "
|
||||
"%lu dropped as unusable or over %u bytes",
|
||||
unsigned(rx.btp_dest_port), rx.payload_len, (unsigned long)raw_fragmented_,
|
||||
(unsigned long)raw_fragment_failed_, (unsigned long)raw_oversize_, unsigned(link::v2x_rx_max_forward));
|
||||
}
|
||||
++raw_forwarded_;
|
||||
raw_its_(link::encode_v2x_rx(rx, rssi));
|
||||
}
|
||||
|
||||
link::Code Station::unsecured_request(const link::BtpDataRequest& q) {
|
||||
|
||||
@@ -30,8 +30,9 @@ public:
|
||||
using Received = std::function<void()>;
|
||||
/// RAW_FRAME body (u32 counter, u8 flags, i8 RSSI, ITS5 record) for the link service.
|
||||
using RawFrame = std::function<void(const link::Bytes&)>;
|
||||
/// MicrOBU: body of one link V2X_RX message (see link_protocol.hpp).
|
||||
using RawIts = std::function<void(const link::Bytes&)>;
|
||||
/// MicrOBU: body of one link V2X_RX message (see link_protocol.hpp). Returns whether a transport took it;
|
||||
/// a body above one link message goes out in fragments, best effort (a false return is only counted).
|
||||
using RawIts = std::function<bool(const link::Bytes&)>;
|
||||
/// DCC_T_GEN_VAM body for the link service (TS 103 300-3 clause 6.2: T_GenVam from management).
|
||||
using DccUpdate = std::function<void(const link::Bytes&)>;
|
||||
|
||||
@@ -283,7 +284,9 @@ private:
|
||||
bool have_fix_ = false;
|
||||
link::PotiUpdate last_poti_; // MicrOBU: the GN Source Position Vector of unsecured_request()
|
||||
std::uint8_t unsecured_mid_[6] = {2, 0, 0, 0, 0, 1}; // MicrOBU: random per configure(); MAC of unsecured frames
|
||||
std::uint32_t raw_forwarded_ = 0, raw_oversize_ = 0;
|
||||
// MicrOBU V2X_RX: forwarded in one message, forwarded in fragments (and how many of those no transport took),
|
||||
// dropped as unusable or beyond v2x_rx_max_forward, and large ones left to the RAW_FRAME copy.
|
||||
std::uint32_t raw_forwarded_ = 0, raw_fragmented_ = 0, raw_fragment_failed_ = 0, raw_oversize_ = 0, raw_covered_ = 0;
|
||||
// declared before everything that refers to its keys (security_, pki_pending_), so it is destroyed last
|
||||
std::unique_ptr<microbu_v2x::security::SecurityModule> module_;
|
||||
std::unique_ptr<vanetza::ManualRuntime> runtime_;
|
||||
|
||||
@@ -14,11 +14,33 @@ constexpr std::size_t v2x_rx_prefix = 14;
|
||||
/// Largest ITS payload that fits one link message (512 - 4 - 14).
|
||||
constexpr std::size_t v2x_rx_max_payload = maximum_message - header_size - v2x_rx_prefix;
|
||||
|
||||
/// Largest ITS payload forwarded at all (fragmented when it exceeds v2x_rx_max_payload): what one 802.11 frame
|
||||
/// of the radio's 2346-octet buffer can hold. Real road SPATEMs and MAPEMs reach about 1.3 kB.
|
||||
constexpr std::size_t v2x_rx_max_forward = 2300;
|
||||
|
||||
/// @brief Whether an unwrapped message fits one link message. A truncated or empty one never does.
|
||||
inline bool v2x_rx_fits(const gn_rx_t& rx) {
|
||||
return !rx.truncated && rx.payload_len > 0 && static_cast<std::size_t>(rx.payload_len) <= v2x_rx_max_payload;
|
||||
}
|
||||
|
||||
/// What Station::forward_raw does with an unwrapped message.
|
||||
enum class V2xRxPlan {
|
||||
drop, ///< truncated, empty or beyond v2x_rx_max_forward: counted as oversize, not sent
|
||||
single, ///< one link message, exactly as before the fragmentation was added
|
||||
fragmented, ///< larger than one link message: sent in fragments, best effort
|
||||
covered_by_raw, ///< larger than one link message while the raw capture is on: the RAW_FRAME copy carries it
|
||||
};
|
||||
|
||||
/// @brief Decides how an unwrapped message goes to the phone.
|
||||
/// @param raw_capture_on The phone switched RAW_CAPTURE on, so it reads RAW_FRAME and ignores V2X_RX; a large
|
||||
/// message is then not sent a second time as fragmented V2X_RX (small ones still are, as before).
|
||||
inline V2xRxPlan v2x_rx_plan(const gn_rx_t& rx, bool raw_capture_on) {
|
||||
if (rx.truncated || rx.payload_len <= 0 || static_cast<std::size_t>(rx.payload_len) > v2x_rx_max_forward)
|
||||
return V2xRxPlan::drop;
|
||||
if (v2x_rx_fits(rx)) return V2xRxPlan::single;
|
||||
return raw_capture_on ? V2xRxPlan::covered_by_raw : V2xRxPlan::fragmented;
|
||||
}
|
||||
|
||||
/// @brief Builds the V2X_RX body. The caller checks v2x_rx_fits first.
|
||||
/// @param rx Message unwrapped by gn_unwrap_its (its payload pointer must still be valid).
|
||||
/// @param rssi Received signal strength, dBm.
|
||||
|
||||
Reference in New Issue
Block a user