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:
+11
-3
@@ -35,8 +35,16 @@ custom PCB; pick it in `menuconfig -> micrOBU firmware -> Board`.
|
||||
(`Station::forward_raw`) and reaches the phone as `V2X_RX`. Body: `u16 port, i8 rssi, u8 flags
|
||||
(bit0 GBC area, bit1 signed), i32 lat, i32 lon, u16 distance_a, ITS payload`. `0x85` is the spec's
|
||||
`RAW_FRAME` debug capture and `0x86` is reserved by Niklas Dathe, hence `0x90`. BTP ports 2001
|
||||
CAM, 2002 DENM, 2003 MAPEM, 2004 SPATEM, 2018 VAM pass; a message over 494 bytes does not fit a
|
||||
512-octet link message and is dropped and counted (not fragmented).
|
||||
CAM, 2002 DENM, 2003 MAPEM, 2004 SPATEM, 2018 VAM pass. A message up to 494 bytes is one link
|
||||
message, unchanged. Since 2026-11-23 a larger one (a road SPATEM or MAPEM, 555-1243 bytes; up to
|
||||
`v2x_rx_max_forward` = 2300) goes out in link fragments (`link_fragment.hpp`, flags 1/2/3 as the spec
|
||||
defines them), best effort: no retry and no waiting, a fragment no transport takes ends that message
|
||||
(the app drops the incomplete stream) and `raw_fragment_failed_` counts it, and the serial fragments use
|
||||
the non-blocking `try_write` so a stalled USB host cannot hold the station task that drains the radio queue.
|
||||
A transport that would need more than 8 fragments (ATT MTU 23) skips the message. While `RAW_CAPTURE` is on
|
||||
the app reads `RAW_FRAME`, so a large message is not sent a second time as `V2X_RX` (`raw_covered_`).
|
||||
Before this change a message over 494 bytes was dropped and counted (the 2026-11-23 bench run lost every
|
||||
888-byte SPATEM and 1198-byte MAPEM that way).
|
||||
- **Unsigned transmission.** Niklas Dathe's station signs everything. A request with GN security
|
||||
profile 1 (or 0 while the station is configured `security = 0`) goes out unsigned with
|
||||
`geonet.c`'s header and the position of the last PoTi (`Station::unsecured_request`), from a
|
||||
@@ -64,7 +72,7 @@ custom PCB; pick it in `menuconfig -> micrOBU firmware -> Board`.
|
||||
- **Flash size 8 MB** (XIAO), TX power ceiling 20 dBm (`radio_policy.hpp` admits 20 dBm on channel 180).
|
||||
- **ATT MTU 517** (`CONFIG_BT_NIMBLE_ATT_PREFERRED_MTU`, Niklas Dathe's 512): the app asks for 517 and a 494-octet
|
||||
V2X_RX is a 512-octet link message, which needs a value of 512, i.e. an MTU of 515. At 512 such a message was
|
||||
split into fragments the app does not reassemble.
|
||||
split into fragments the app did not reassemble then (it does now, `LinkReassembly`).
|
||||
- **Link-layer address.** A signed station without a named MAC uses the address derived from its ticket; with the
|
||||
16-octet STATION_CONFIGURE the phone names it (see "Unsigned transmission"). The CAM and VAM station ID still
|
||||
rotates in the app; the app now rotates the MAC with it on a ride and keeps one fixed for the bench pinger.
|
||||
|
||||
@@ -90,7 +90,7 @@ It prints the log, the 131-octet STATUS once a second (uptime, signing and radio
|
||||
battery) and the first V2X_RX messages. Status exchange: the host sends `STATUS_REQUEST` every
|
||||
1000 ms; `CONFIRM.detail` contains the snapshot.
|
||||
|
||||
Received messages reach the phone on two paths that both stay in the firmware: `V2X_RX` (ours, always on, already unwrapped, up to 494 octets) and `RAW_FRAME` (the spec's debug capture, on after `RAW_CAPTURE`, the whole radio frame, fragmented when large). The Android app has a switch for which one it consumes; the board always sends `V2X_RX`. See [NOTES.md](NOTES.md).
|
||||
Received messages reach the phone on two paths that both stay in the firmware: `V2X_RX` (ours, always on, already unwrapped, one message up to 494 octets, fragmented above that up to 2300) and `RAW_FRAME` (the spec's debug capture, on after `RAW_CAPTURE`, the whole radio frame, fragmented when large). The Android app has a switch for which one it consumes; the board always sends `V2X_RX`. See [NOTES.md](NOTES.md).
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
@@ -99,7 +99,7 @@ flowchart LR
|
||||
Radio --> Unwrap[gn_unwrap.c: 802.11, LLC, GN, BTP, signature envelope]
|
||||
Radio -->|only while RAW_CAPTURE is on| Cap[Raw capture tap: ITS5 record]
|
||||
Stack -->|verified only| Ind[BTP_DATA_INDICATION 0x81]
|
||||
Unwrap -->|always, signed or not, up to 494 octets| Rx[V2X_RX 0x90, MicrOBU]
|
||||
Unwrap -->|always, signed or not, fragmented above 494 octets| Rx[V2X_RX 0x90, MicrOBU]
|
||||
Cap -->|fragmented if large| Raw[RAW_FRAME 0x85, spec]
|
||||
Ind --> Link[Station link: USB or BLE]
|
||||
Rx --> Link
|
||||
|
||||
+2400
-2240
File diff suppressed because it is too large
Load Diff
@@ -21,15 +21,15 @@
|
||||
\newcolumntype{F}{@{}|P{12mm}|P{43mm}|P{36mm}|P{25mm}|P{48.15mm}|@{}}
|
||||
\pagestyle{fancy}
|
||||
\fancyhf{}
|
||||
\fancyfoot[L]{\footnotesize Document revision 2 (MicrOBU)}
|
||||
\fancyfoot[L]{\footnotesize Document revision 3 (MicrOBU)}
|
||||
\fancyfoot[R]{\footnotesize\thepage\ / \pageref{LastPage}}
|
||||
\renewcommand{\headrulewidth}{0pt}
|
||||
\renewcommand{\footrulewidth}{0pt}
|
||||
\hypersetup{pdftitle={microbu station link interface reference, revision 2 (MicrOBU)}}
|
||||
\hypersetup{pdftitle={microbu station link interface reference, revision 3 (MicrOBU)}}
|
||||
\begin{document}
|
||||
microbu station link interface reference, revision 2 (MicrOBU)\par
|
||||
microbu station link interface reference, revision 3 (MicrOBU)\par
|
||||
\vspace{3mm}
|
||||
Revision 1 was written by Niklas Dathe (microbu-firmware, commit e4e4bd8); revision 2 is revision 1 plus the additions of the MicrOBU production firmware (obu-firmware). Every addition is marked \textbf{MicrOBU extension}; the revision history is at the end.\par\smallskip
|
||||
Revision 1 was written by Niklas Dathe (microbu-firmware, commit e4e4bd8); revisions 2 and 3 are revision 1 plus the additions of the MicrOBU production firmware (obu-firmware). Every addition is marked \textbf{MicrOBU extension}; the revision history is at the end.\par\smallskip
|
||||
The maximum link message is 512 bytes, including the four-byte header.\par\smallskip
|
||||
USB Serial/JTAG and BLE GATT carry the same messages.
|
||||
The host sends a status request as a link heartbeat.\par\smallskip
|
||||
@@ -1147,7 +1147,7 @@ Marks record origin and time basis.
|
||||
{\ttfamily 12} & {\ttfamily frame\_\allowbreak{}length} & {\ttfamily u16} & bytes & Gives the raw radio-frame size in bytes. \\
|
||||
{\ttfamily 14} & {\ttfamily frame} & {\ttfamily bytes[\allowbreak{}frame\_\allowbreak{}length]} & \textemdash & Contains the raw radio-frame bytes. \\
|
||||
\end{longtable}}
|
||||
\Needspace{7\baselineskip}\section{MicrOBU extensions (revision 2)}\label{sec:microbu-extensions}
|
||||
\Needspace{7\baselineskip}\section{MicrOBU extensions (revisions 2 and 3)}\label{sec:microbu-extensions}
|
||||
These additions are not in revision 1. A host written for revision 1 keeps working: it never sees opcode 0x90 unless it listens for it, and it never sends security profile 0x01.
|
||||
\Needspace{8\baselineskip}\subsection{{\ttfamily V2X\_\allowbreak{}RX}}
|
||||
{\small\color{ink}\textbf{0x90}\quad STATION TO HOST \quad (MicrOBU extension)}
|
||||
@@ -1156,8 +1156,8 @@ Delivers every ITS message heard on air, verified or not. The station stack deca
|
||||
\toprule\textbf{Parameter} & \textbf{Setting} & \textbf{Description} \\\midrule\endfirsthead
|
||||
\toprule\textbf{Parameter} & \textbf{Setting} & \textbf{Description} \\\midrule\endhead
|
||||
\bottomrule\endfoot
|
||||
{\ttfamily reply} & {\ttfamily none} & One-way indication, not fragmented. \\
|
||||
{\ttfamily maximum\_\allowbreak{}payload} & {\ttfamily 494 bytes} & 512 minus the four-byte header and the 14-byte prefix. A longer message is dropped and counted, never fragmented. \\
|
||||
{\ttfamily reply} & {\ttfamily none} & One-way indication. Fragmented when the body exceeds one link message (revision 3, Section~\ref{sec:microbu-v2xrx-frag}). \\
|
||||
{\ttfamily maximum\_\allowbreak{}payload} & {\ttfamily 2300 bytes} & Up to 494 bytes (512 minus the four-byte header and the 14-byte prefix) the message is one link message. Revision 3: a longer one is fragmented, up to 2300 bytes; beyond that it is dropped and counted. Revision 2 dropped everything over 494 bytes. \\
|
||||
{\ttfamily btp\_\allowbreak{}ports} & {\ttfamily 2001--2004, 2018} & CAM, DENM, MAPEM, SPATEM, VAM. Other ports are not delivered. \\
|
||||
{\ttfamily availability} & {\ttfamily from configure} & The radio is polled from STATION\_CONFIGURE on, before the first POTI\_UPDATE. \\
|
||||
\end{longtable}}
|
||||
@@ -1177,10 +1177,27 @@ Delivers every ITS message heard on air, verified or not. The station stack deca
|
||||
\Needspace{8\baselineskip}\subsection{\texttt{V\_\allowbreak{}2X\_\allowbreak{}RX} and \texttt{RAW\_\allowbreak{}FRAME}: both stay}\label{sec:microbu-rxpaths}
|
||||
Revision 2 does not replace the debug capture of revision 1 with \texttt{V\_2X\_RX}: the station keeps both, and the host chooses.
|
||||
\begin{itemize}
|
||||
\item \texttt{V\_2X\_RX} is sent for every ITS message heard, always, once the radio runs (it needs no \texttt{RAW\_CAPTURE}). The station has already unwrapped the frame, so a host decodes the payload at once; the message is at most 494 octets and is never fragmented.
|
||||
\item \texttt{RAW\_FRAME} (revision 1) is sent only while \texttt{RAW\_CAPTURE} is on, as a copy of the whole 802.11 frame, fragmented when it exceeds one link message. A host that uses it unwraps the frame itself, is not limited to 494 octets, and accepts that copies may be dropped when the transport is busy.
|
||||
\item \texttt{V\_2X\_RX} is sent for every ITS message heard, always, once the radio runs (it needs no \texttt{RAW\_CAPTURE}). The station has already unwrapped the frame, so a host decodes the payload at once; a message of up to 494 octets is one link message, a larger one is fragmented (revision 3, Section~\ref{sec:microbu-v2xrx-frag}).
|
||||
\item \texttt{RAW\_FRAME} (revision 1) is sent only while \texttt{RAW\_CAPTURE} is on, as a copy of the whole 802.11 frame, fragmented when it exceeds one link message. A host that uses it unwraps the frame itself, is not limited to 494 octets (neither is \texttt{V\_2X\_RX} since revision 3), and accepts that copies may be dropped when the transport is busy.
|
||||
\end{itemize}
|
||||
The station does not know which path the host uses and sends \texttt{V\_2X\_RX} regardless. The MicrOBU Android app offers both as a setting (Receive path). In raw mode it sends \texttt{RAW\_CAPTURE} with mode 0x03 (capture on, broadcast frames only), reassembles fragments as described in Section~\ref{sec:fragmentation-host}, unwraps each frame with the same rules as the station does for \texttt{V\_2X\_RX} (single-hop broadcast and GeoBroadcast, BTP-B ports 2001--2004 and 2018, a signature envelope unwrapped without verification) and ignores the \texttt{V\_2X\_RX} messages. In \texttt{V\_2X\_RX} mode it sends mode 0 and ignores \texttt{RAW\_FRAME}.
|
||||
\Needspace{8\baselineskip}\subsection{Fragmented \texttt{V\_\allowbreak{}2X\_\allowbreak{}RX} (revision 3)}\label{sec:microbu-v2xrx-frag}
|
||||
Road roadside units send SPATEMs and MAPEMs of 555 to 1243 octets (the Berliner Tor unit: 888 and 1198), more than the 494 octets one link message carries. Revision 2 dropped them in the station, silently as far as the host could tell. From revision 3 the station sends them in fragments, with the framing of the fragmentation section of revision 1 (flags 1, 2 and 3; the same opcode 0x90 and sequence on every fragment; each fragment body starts with total length and offset). The body that is reassembled is the ordinary \texttt{V\_2X\_RX} body of this section, the 14-octet prefix included.
|
||||
{\small\begin{longtable}{@{}P{45mm}P{55mm}P{72.8mm}@{}}
|
||||
\toprule\textbf{Parameter} & \textbf{Setting} & \textbf{Description} \\\midrule\endfirsthead
|
||||
\toprule\textbf{Parameter} & \textbf{Setting} & \textbf{Description} \\\midrule\endhead
|
||||
\bottomrule\endfoot
|
||||
{\ttfamily single message} & {\ttfamily up to 494 bytes} & Unchanged from revision 2: flags 0, one link message, byte for byte. \\
|
||||
{\ttfamily fragmented} & {\ttfamily 495 to 2300 bytes} & Body of 14 + payload octets, split into fragments of at most the transport message size minus eight octets (four of link header, four of fragment header): 504 octets of body on serial and on BLE with ATT MTU 517. An 888-byte SPATEM is two fragments, a 1198-byte MAPEM three. \\
|
||||
{\ttfamily dropped} & {\ttfamily over 2300 bytes} & Counted by the station, not sent. Truncated and empty messages are never sent. \\
|
||||
{\ttfamily delivery} & {\ttfamily best effort} & No acknowledgement, retry or wait. A fragment the transport does not take ends that message; the host discards the incomplete stream (2000 ms idle, or the next first fragment of the same opcode and sequence). A lost fragment costs only that message and never slows reception. \\
|
||||
{\ttfamily serial} & {\ttfamily non-blocking} & Fragments are queued without waiting for the host, as \texttt{RAW\_FRAME} is, so a stalled USB host cannot hold up the station. Nothing is queued while no USB host is connected. A message that fits one link message keeps its blocking write. \\
|
||||
{\ttfamily fragment cap} & {\ttfamily 8 per message} & A transport that would need more than eight fragments (an ATT MTU that was never raised, 23) skips the message. \\
|
||||
{\ttfamily raw capture on} & {\ttfamily large not sent} & While \texttt{RAW\_CAPTURE} is on the host reads \texttt{RAW\_FRAME}, so a message above 494 octets is left to that copy instead of being sent a second time. Messages up to 494 octets are still sent as \texttt{V\_2X\_RX}. \\
|
||||
\end{longtable}}
|
||||
A host that already reassembles fragmented messages (the revision 1 requirement, Section~\ref{sec:fragmentation-host}) needs no change. A host that handled only unfragmented \texttt{V\_2X\_RX} sees flags 1 to 3 on opcode 0x90 for the first time and must reassemble or ignore them. The wire format of every other message is unchanged.
|
||||
\Needspace{5\baselineskip}\paragraph{Host decoding of the larger messages}
|
||||
Roadside units also fill the messages out: a SPATEM names the intersection and each movement (\texttt{DescriptiveName}, IA5String of 1 to 63 characters) and gives each signal event a list of advisory speeds (greenwave, ecoDrive). A host decoder must read past these optional fields, not refuse the message. A decoder written against shorter messages and refusing them showed no signal state for the Berliner Tor unit, although every SPATEM had arrived and was in the host's log. The MicrOBU app reads past both since 2026-11-23.
|
||||
\Needspace{8\baselineskip}\subsection{Fragmentation on the host side}\label{sec:fragmentation-host}
|
||||
The MicrOBU host reassembles fragmented station-to-host messages as the fragmentation section of revision 1 states: fragments of one message share opcode and sequence, flags 1, 2 and 3 mark first, middle and last, each fragment body starts with total length and offset, a gap, a duplicate, a wrong order or a changed total drops the stream, an unfinished stream is discarded after 2000 ms, at most 8 streams are held, and a complete body is at most 65535 octets. A message that fits one link message (flags 0) passes through unchanged.
|
||||
\Needspace{8\baselineskip}\subsection{Unsecured transmission}\label{sec:microbu-unsecured}
|
||||
@@ -1235,5 +1252,6 @@ MicrOBU extension. While no USB host is connected to the native port (the phone
|
||||
\bottomrule\endfoot
|
||||
1 & 2026-11 & Niklas Dathe: the microbu-firmware interface reference, commit e4e4bd8. \\
|
||||
2 & 2026-11-10 & MicrOBU additions: opcode 0x90 V2X\_RX (all heard ITS messages, unverified); unsecured transmission for security profile 0x01 and for profile 0x00 with \texttt{security} 0, from a random MAC per configuration, or the MAC named by the host in the 16-octet STATION\_CONFIGURE; V\_2X\_RX and RAW\_FRAME both kept, the host choosing (Section~\ref{sec:microbu-rxpaths}); the radio is polled from STATION\_CONFIGURE on; battery protection by deep sleep; no USB writes without a host. Wire format of revision 1 unchanged; the original is kept as microbu-firmware/docs/station-link.pdf. \\
|
||||
3 & 2026-11-23 & \texttt{V\_2X\_RX} larger than 494 octets is fragmented, up to 2300 octets, best effort and non-blocking (Section~\ref{sec:microbu-v2xrx-frag}); revision 2 dropped it. Serial writes of fragments and of \texttt{RAW\_FRAME} are skipped while no USB host is connected. Found on the 2026-11-23 bench run against the Berliner Tor roadside unit (888-byte SPATEM, 1198-byte MAPEM); the findings are in docs/08-large-message-reception.md. Wire format of messages up to 494 octets unchanged. \\
|
||||
\end{longtable}}
|
||||
\end{document}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -27,11 +27,11 @@ 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 corpus clean
|
||||
.PHONY: all check chain replay fuzz battery firmware fragments corpus clean
|
||||
|
||||
all: check
|
||||
|
||||
check: chain replay fuzz battery firmware
|
||||
check: chain replay fuzz battery firmware fragments
|
||||
|
||||
$(BUILD):
|
||||
mkdir -p $@
|
||||
@@ -83,6 +83,23 @@ $(BUILD)/test_firmware$(EXE): test_firmware.cpp $(FW_CXX_SRCS) $(wildcard $(FW)/
|
||||
firmware: $(BUILD)/test_firmware$(EXE)
|
||||
$(BUILD)/test_firmware$(EXE)
|
||||
|
||||
# Recorded traffic through unwrap -> V2X_RX body -> fragments -> reassembly (the large MAPEMs and SPATEMs of a road RSU).
|
||||
# Needs a capture: the CiT node's 2026-11-23 recording has 1198-byte MAPEMs and 888-byte SPATEMs.
|
||||
FRAG_PCAPS = $(wildcard ../../../databases/its5_20261123_150746.pcap) $(RECORDINGS)
|
||||
|
||||
$(BUILD)/fw_c_test_util.o: test_util.c test_util.h | $(BUILD)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
$(BUILD)/test_v2x_fragments$(EXE): test_v2x_fragments.cpp $(BUILD)/fw_c_test_util.o $(BUILD)/fw_c_gn_unwrap.o $(FW)/link_protocol.cpp $(wildcard $(FW)/*.hpp) | $(BUILD)
|
||||
$(CXX) $(CXXFLAGS) -std=c++20 -I. -fsanitize=undefined -fsanitize-undefined-trap-on-error -o $@ test_v2x_fragments.cpp $(FW)/link_protocol.cpp $(BUILD)/fw_c_test_util.o $(BUILD)/fw_c_gn_unwrap.o
|
||||
|
||||
fragments: $(BUILD)/test_v2x_fragments$(EXE)
|
||||
ifeq ($(FRAG_PCAPS),)
|
||||
@echo "fragments: no captures, skipped"
|
||||
else
|
||||
$(BUILD)/test_v2x_fragments$(EXE) $(FRAG_PCAPS)
|
||||
endif
|
||||
|
||||
# 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
|
||||
|
||||
@@ -8,7 +8,7 @@ installed 2026-09-11).
|
||||
|
||||
**Firmware unit tests (`make firmware`, `test_firmware.cpp`, 40 tests):** the station-link codec byte-exact against
|
||||
the app's `StationLinkTest` hex, the 131-octet STATUS offsets, request reassembly, the V2X_RX extension
|
||||
(`main/v2x_rx.hpp`; the 494-octet limit, the 14-octet prefix, TX code -> air frame -> RX path), radio operating
|
||||
(`main/v2x_rx.hpp`; the 494-octet limit, the 14-octet prefix, TX code -> air frame -> RX path; the plan for large messages and `link_fragment.hpp`'s fragments, round-tripped through two reassemblers), radio operating
|
||||
points, the BLE TX guard, the ITS clock, CCA counters, BLE security and the battery policy. It prints PASS or FAIL
|
||||
per test and a `RESULT:` line, and exits non-zero on a failure. It cannot test the radio, BLE, NVS or USB: those are
|
||||
on the bench list in `TODO.md`.
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
// Replays recorded air traffic through the whole V2X_RX send path that needs no ESP32: gn_unwrap_its ->
|
||||
// v2x_rx_plan -> encode_v2x_rx -> split_fragments -> link::encode of every fragment -> reassembly, and checks
|
||||
// that what a phone would reassemble is byte for byte the body that was sent. Messages that fit one link message
|
||||
// must stay one message and be byte-identical to the unfragmented encoding.
|
||||
//
|
||||
// Usage: test_v2x_fragments capture.pcap [capture.pcap ...]
|
||||
//
|
||||
// The reassembly here is written independently of link::RequestReassembly (which is also run, for bodies it
|
||||
// accepts) and of the app's Kotlin LinkReassembly (checked by LinkReassemblyTest on the same fragments).
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
#include "gn_unwrap.h"
|
||||
#include "test_util.h"
|
||||
}
|
||||
#include "link_fragment.hpp"
|
||||
#include "request_reassembly.hpp"
|
||||
#include "v2x_rx.hpp"
|
||||
|
||||
using namespace microbu;
|
||||
using link::Bytes;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int rx_frame_max_len = 2346; // c5_radio.cpp's Raw::bytes, as in test_replay.c
|
||||
|
||||
struct Stats {
|
||||
long records = 0, accepted = 0, single = 0, fragmented = 0, dropped = 0, fragments = 0, failures = 0;
|
||||
std::map<std::pair<unsigned, std::size_t>, long> by_port_size;
|
||||
};
|
||||
|
||||
FILE* dump = nullptr; // --dump out.tsv
|
||||
std::set<std::pair<unsigned, std::size_t>> dumped;
|
||||
|
||||
bool fail(Stats& s, const char* what, int index) {
|
||||
std::fprintf(stderr, "FAIL record %d: %s\n", index, what);
|
||||
++s.failures;
|
||||
return false;
|
||||
}
|
||||
|
||||
// What a phone does with the link messages of one body: independent of every other reassembler.
|
||||
bool reassemble(const std::vector<link::Message>& messages, Bytes& out) {
|
||||
out.clear();
|
||||
std::size_t total = 0;
|
||||
for (std::size_t i = 0; i < messages.size(); ++i) {
|
||||
const auto& m = messages[i];
|
||||
const auto& b = m.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);
|
||||
const std::uint8_t want = i == 0 ? 1 : (i + 1 == messages.size() ? 3 : 2);
|
||||
if (m.header.flags != want || m.header.opcode != link::Opcode::V2X_RX) 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;
|
||||
}
|
||||
|
||||
void check(Stats& s, const gn_rx_t& rx, int index) {
|
||||
++s.accepted;
|
||||
const auto plan = link::v2x_rx_plan(rx, false);
|
||||
if (link::v2x_rx_plan(rx, true) == link::V2xRxPlan::fragmented) { fail(s, "raw capture on still fragments", index); return; }
|
||||
if (plan == link::V2xRxPlan::drop) { ++s.dropped; return; }
|
||||
const Bytes body = link::encode_v2x_rx(rx, -60);
|
||||
if (body.size() != link::v2x_rx_prefix + static_cast<std::size_t>(rx.payload_len)) { fail(s, "body size", index); return; }
|
||||
++s.by_port_size[{rx.btp_dest_port, static_cast<std::size_t>(rx.payload_len)}];
|
||||
|
||||
if (plan == link::V2xRxPlan::single) {
|
||||
++s.single;
|
||||
std::vector<link::Fragment> none;
|
||||
Bytes wire;
|
||||
if (link::split_fragments(body, link::maximum_message, none)) { fail(s, "a single message was fragmented", index); return; }
|
||||
if (!link::encode(link::Message {{link::Opcode::V2X_RX, 0, 7}, body}, wire) || wire.size() != body.size() + link::header_size)
|
||||
fail(s, "single message does not encode", index);
|
||||
return;
|
||||
}
|
||||
++s.fragmented;
|
||||
if (dump && dumped.insert({rx.btp_dest_port, static_cast<std::size_t>(rx.payload_len)}).second) {
|
||||
// For the app's LinkReassemblyTest: the first message of each large size, its body and the link messages
|
||||
// (hex) both transports would carry. `port size limit body wire wire ...`
|
||||
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) {
|
||||
std::vector<link::Fragment> fragments;
|
||||
if (!link::split_fragments(body, limit, fragments, 8)) continue;
|
||||
std::fprintf(dump, "%u\t%d\t%zu\t", rx.btp_dest_port, rx.payload_len, limit);
|
||||
for (const auto b : body) std::fprintf(dump, "%02x", b);
|
||||
for (const auto& f : fragments) {
|
||||
Bytes wire;
|
||||
link::encode(link::Message {{link::Opcode::V2X_RX, f.flags, 77}, f.body}, wire);
|
||||
std::fputc('\t', dump);
|
||||
for (const auto b : wire) std::fprintf(dump, "%02x", b);
|
||||
}
|
||||
std::fputc('\n', dump);
|
||||
}
|
||||
}
|
||||
// 512: serial and the full BLE MTU 517. 244: ATT MTU 247. Both must carry the message; 20 (MTU 23) must refuse it.
|
||||
for (const std::size_t limit : {std::size_t(512), std::size_t(244)}) {
|
||||
std::vector<link::Fragment> fragments;
|
||||
if (!link::split_fragments(body, limit, fragments, 8)) { fail(s, "split refused", index); return; }
|
||||
std::vector<link::Message> messages;
|
||||
link::RequestReassembly oracle;
|
||||
std::optional<link::Message> done;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
link::Message m {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body};
|
||||
Bytes wire;
|
||||
if (!link::encode(m, wire) || wire.size() > limit) { fail(s, "fragment exceeds the transport limit", index); return; }
|
||||
link::Message back;
|
||||
if (!link::decode(wire, back)) { fail(s, "fragment does not decode", index); return; }
|
||||
messages.push_back(back);
|
||||
done = oracle.feed(back, 0, i);
|
||||
++s.fragments;
|
||||
}
|
||||
Bytes rebuilt;
|
||||
if (!reassemble(messages, rebuilt) || rebuilt != body) { fail(s, "independent reassembly differs", index); return; }
|
||||
if (body.size() <= link::maximum_request_body && (!done || done->body != body)) { fail(s, "RequestReassembly differs", index); return; }
|
||||
// One fragment lost anywhere but at the end, then the next message: nothing completes, nothing is poisoned.
|
||||
if (fragments.size() > 2) {
|
||||
link::RequestReassembly lossy;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i) {
|
||||
if (i == 1) continue;
|
||||
const auto got = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 9}, fragments[i].body}, 0, i);
|
||||
if (got) { fail(s, "a message with a lost fragment completed", index); return; }
|
||||
}
|
||||
if (body.size() <= link::maximum_request_body) {
|
||||
std::optional<link::Message> next;
|
||||
for (std::size_t i = 0; i < fragments.size(); ++i)
|
||||
next = lossy.feed(link::Message {{link::Opcode::V2X_RX, fragments[i].flags, 10}, fragments[i].body}, 0, 100 + i);
|
||||
if (!next || next->body != body) { fail(s, "the message after a lost fragment did not complete", index); return; }
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<link::Fragment> too_small;
|
||||
if (link::split_fragments(body, 20, too_small, 8)) fail(s, "MTU 23 should skip a large message", index);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int first = 1;
|
||||
if (argc > 2 && std::strcmp(argv[1], "--dump") == 0) {
|
||||
dump = std::fopen(argv[2], "w");
|
||||
if (!dump) { std::fprintf(stderr, "cannot write %s\n", argv[2]); return 2; }
|
||||
first = 3;
|
||||
}
|
||||
if (argc <= first) { std::fprintf(stderr, "usage: test_v2x_fragments [--dump out.tsv] capture.pcap [capture.pcap ...]\n"); return 2; }
|
||||
tu_install_crash_handler();
|
||||
tu_guard_init();
|
||||
Stats stats;
|
||||
for (int a = first; a < argc; ++a) {
|
||||
struct Ctx { Stats* s; const char* path; } ctx {&stats, argv[a]};
|
||||
const int n = tu_pcap_foreach(argv[a], [](const uint8_t* frame, int len, int index, void* c) {
|
||||
auto* x = static_cast<Ctx*>(c);
|
||||
++x->s->records;
|
||||
if (len > rx_frame_max_len) len = rx_frame_max_len;
|
||||
tu_set_context("fragments %s record %d (%d bytes)", x->path, index, len);
|
||||
gn_rx_t rx;
|
||||
if (gn_unwrap_its(tu_guarded(frame, len), len, &rx)) check(*x->s, rx, index);
|
||||
}, &ctx);
|
||||
if (n < 0) { std::fprintf(stderr, "cannot read %s as a pcap\n", argv[a]); return 2; }
|
||||
}
|
||||
std::printf("test_v2x_fragments: %ld records, %ld accepted: %ld single, %ld fragmented (%ld fragments built for "
|
||||
"two transports), %ld dropped, %ld failures\n",
|
||||
stats.records, stats.accepted, stats.single, stats.fragmented, stats.fragments, stats.dropped, stats.failures);
|
||||
for (const auto& [key, count] : stats.by_port_size)
|
||||
if (key.second > link::v2x_rx_max_payload) std::printf(" port %u, %zu bytes: %ld\n", key.first, key.second, count);
|
||||
return stats.failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user