Rebuild obu-firmware on Niklas Dathe's microbu-firmware

Replace the vanetza-idf port with his current station firmware (microbu-firmware
e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and
re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources:

- V2X_RX (opcode 0x90): every received ITS message, signed or not, through
  gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is
  the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp.
- Unsigned transmission (geonet.c) for security profile 1 or station security 0,
  from a random MAC per configuration.
- Radio polled from STATION_CONFIGURE on; manual country policy, TX power
  read-back, static RX buffer, short frames ignored.
- Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB.
- No USB writes without a host; last_frame_ms() for the guard.
- 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm.

Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add
docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned
sending, battery rule, revision history). Add 39 host unit tests (make firmware in
test/host) next to the existing chain, replay, fuzz and battery tests, and point
tools/station_status_watch.py at the new CONFIRM-based status.
This commit is contained in:
Ashin Walpola
2026-11-11 00:13:57 +01:00
parent 1c3a66e4da
commit 50bbf6552d
10307 changed files with 53622 additions and 850451 deletions
+64 -35
View File
@@ -1,5 +1,5 @@
#pragma once
// micrOBU station-internal link, message layer version 1 (implementation/station-link/README.md).
// micrOBU station-internal link, message layer (../../docs/station-link.tex).
// Transport independent: the same octets are one GATT attribute value later and one serial
// frame payload today. Little-endian integers; ETSI payloads inside stay opaque.
#include <cstddef>
@@ -30,23 +30,38 @@ enum class Opcode : std::uint8_t {
SF_ID_LOCK = 0x0A,
SF_ID_UNLOCK = 0x0B,
STATUS_REQUEST = 0x0C,
RAW_CAPTURE = 0x0D, // debug path: u8 mode (0x01 on, 0x02 broadcast only, 0x04 own TX)
// TS 102 941 V2.2.1 on the device : the phone relays, the micrOBU builds and answers
PKI_IDENTITY = 0x0E, // -> CONFIRM detail: u8 id length, canonical identifier, u8[65] canonical public key
PKI_ENROL_REQUEST = 0x0F, // u8[8] EA HashedId8 -> CONFIRM, then PKI_REQUEST_DATA
PKI_AUTH_REQUEST = 0x10, // u8[8] EA, u8[8] AA HashedId8 -> CONFIRM, then PKI_REQUEST_DATA
PKI_RETRY = 0x13, // u8 kind (1 only), returns the retained encrypted enrolment request
PKI_TRUST_BOOTSTRAP = 0x16, // USB only, independently pinned TLM certificate
PKI_TRUST_UPDATE = 0x17, // u16 total, u16 offset, snapshot segment
PKI_STATUS = 0x15, // device time, EC and CAM/VAM validity ends, managed trust deadline
PKI_CANCEL = 0x14, // u8 kind, erase the pending transaction and session key
PKI_RESPONSE = 0x11, // u8 kind, u16 total, u16 offset, segment[] -> CONFIRM (last segment: u8 code, u8[8] digest)
// ESP32-C5 -> phone
RESULT = 0x80,
CONFIRM = 0x80,
BTP_DATA_INDICATION = 0x81,
SF_IDCHANGE_EVENT = 0x82,
STATUS = 0x84,
// MicrOBU extension, not in the colleague's station-link v1: every ITS message heard on air,
// unwrapped by gn_unwrap.c whether or not the security entity could verify it. Body is the
// previous firmware's SERIAL_MSG_V2X_RX payload unchanged: [u16 btp_dest_port][i8 rssi]
// [u8 flags: bit0 geo area valid, bit1 signed but not verified][i32 area_lat][i32 area_lon]
// [u16 area_distance_a], then the UPER bytes. See obu-firmware/NOTES.md.
V2X_RX = 0x85,
// 0x84 retired: STATUS_REQUEST returns its snapshot in CONFIRM.detail.
RAW_FRAME = 0x85, // u32 counter, u8 flags, i8 RSSI, ITS5 record (may be fragmented)
// 0x86 reserved: retired DCC_T_GEN_VAM (T_GenVam carried directly in STATUS)
PKI_REQUEST_DATA = 0x87, // u8 kind (1 enrolment, 2 authorization), encoded request (fragmented)
// MicrOBU extension, not in station-link.tex: every ITS message heard on air, verified or not
// (Station::forward_raw). 0x86 is reserved by Niklas Dathe (retired DCC_T_GEN_VAM), so 0x90.
V2X_RX = 0x90, // u16 port, i8 RSSI, u8 flags, i32 lat, i32 lon, u16 dist, ITS payload
BATTERY_STATE = 0x88, // legacy voltage record: u8 version=1, u8 calibrated flag, u16 mV
// 0x89 reserved: retired IMU_TELEMETRY (not streamed over link; reserved for wake interrupts)
};
constexpr std::uint8_t flag_fragment_first = 0x01;
constexpr std::uint8_t flag_fragment_more = 0x02;
constexpr std::uint8_t flag_fragment_middle = 0x02;
constexpr std::uint8_t flag_fragment_last = 0x03;
constexpr std::size_t fragment_header_size = 4; // u16 total body size, u16 body offset (little-endian)
// RESULT codes: vanetza_idf::Result values first (same numbering), then link codes.
// CONFIRM codes: microbu_v2x::Result values first (same numbering), then link codes.
enum class Code : std::uint8_t {
accepted = 0, invalid_argument = 1, unsupported = 2, wrong_entry_point = 3, security_unavailable = 4,
resource_limit = 5, rejected = 6, time_regression = 7, identity_change_pending = 8,
@@ -83,15 +98,20 @@ bool decode(const Bytes& octets, Message& out);
// constants itsGnSecurity, itsGnLocalAddrConfMethod, itsGnDefaultTrafficClass and
// itsGnDefaultPacketLifetime of ETSI TS 103 836-4-1 (GeoNetworking) annex H; channel_number,
// transmit_power_dbm and radio are ITS-G5 access-layer parameters (ETSI EN 303 797).
// STATION_CONFIGURE (0x01): Streamlined 6-octet operational format.
// Active fields: station_type (u8), channel_number (u16), transmit_power_dbm (u8), security (u8), beaconing (u8).
// Retired parameters (radio, address_configuration, mid, default_traffic_class, default_lifetime)
// are fixed to standard defaults: radio=2 (transmit and receive), address_configuration=1 (anonymous),
// default_traffic_class=2 (DP2), default_lifetime=0x05 (1 s).
struct StationConfigure {
std::uint8_t station_type = 2; // TS 102 894-2 StationType (2 = cyclist)
std::uint8_t security = 1; // itsGnSecurity
std::uint8_t address_configuration = 1; // 0 AUTO (mid below), 1 ANONYMOUS (ticket digest)
std::uint8_t mid[6] = {2, 0, 0, 0, 0, 1};
std::uint8_t beaconing = 1;
std::uint16_t channel_number = 180;
std::uint8_t transmit_power_dbm = 10;
std::uint8_t radio = 0; // 0 off, 1 receive only, 2 transmit and receive
std::uint8_t security = 1; // itsGnSecurity (0 off, 1 on)
std::uint8_t beaconing = 1; // 0 off, 1 on
std::uint8_t address_configuration = 1; // 0 AUTO (mid below), 1 ANONYMOUS (ticket digest)
std::uint8_t mid[6] = {2, 0, 0, 0, 0, 1};
std::uint8_t radio = 2; // 0 off, 1 receive only, 2 transmit and receive
std::uint8_t default_traffic_class = 2;
std::uint8_t default_lifetime = 0x05; // 1 s (base One_Second, multiplier 1)
};
@@ -171,6 +191,8 @@ struct BtpDataRequest {
Bytes context;
std::optional<DestinationArea> area; // present for GBC
Bytes fl_sdu;
// Optional trailing u64: ITS time (TimestampIts, ms) at which the phone generated the request. The
std::optional<std::uint64_t> generated_at_ms;
};
/// @param body Raw message body bytes.
@@ -270,6 +292,7 @@ Bytes encode(const IdChangeEvent& value);
// The link's encoding of the SN-IDCHANGE-EVENT.response primitive (ETSI TS 103 836-4-1
// clause 10.2.1.4), acknowledging an IdChangeEvent.
// Correlated by subscription only. Send zero in the unused response-header sequence.
struct IdChangeEventResponse { std::uint64_t subscription = 0; std::uint8_t return_code = 0; };
/// @param body Raw message body bytes.
/// @param out Receives the decoded response.
@@ -292,41 +315,47 @@ struct Status {
std::uint32_t radio_submitted = 0, radio_failed = 0, radio_received = 0, radio_dropped = 0;
std::uint32_t link_rx_frames = 0, link_crc_errors = 0, link_malformed = 0, poti_updates = 0;
std::uint64_t its_time_ms = 0;
// MicrOBU extension, appended after the colleague's 103 octets: [103] battery_mv u16 (0: not
// measured, no cell or no sensor), [105] battery_flags u8. A 103-octet body (the colleague's
// layout, and this firmware before the battery) still decodes, with these two left at 0.
std::uint16_t battery_mv = 0;
std::uint8_t battery_flags = 0;
// DCC diagnostics (units of 1e-4; 0xFFFF = not available) and the value passed to the VBS
std::uint16_t cbr_local = 0xFFFF, cbr_global = 0xFFFF, dcc_delta = 0xFFFF;
std::uint16_t t_gen_vam_dcc_ms = 0; // 0 = none provided (the VBS then uses T_GenVamMin)
// Heap diagnostics (octets): free and lowest-ever free heap, the largest block one allocation can get,
// and the largest DMA-capable block (the Wi-Fi driver's TX buffers). Appended: a 111-octet STATUS still decodes.
std::uint32_t heap_free = 0, heap_minimum_free = 0, heap_largest_block = 0, dma_largest_block = 0;
// Onboard temperature (0.1 deg C; 0x7FFF = not available; TMP112 on mainboard v5)
std::int16_t board_temperature_deci_deg = 0x7FFF;
std::uint16_t battery_mv = 0; // Calibrated battery voltage in mV (0 if unavailable)
};
/// battery_flags bits of Status.
constexpr std::uint8_t battery_flag_low = 0x01; ///< cell below the warning threshold
constexpr std::uint8_t battery_flag_critical = 0x02; ///< cell below the shutdown threshold: the board powers down unless a phone is on USB
/// Octets of the STATUS body without / with the MicrOBU battery extension.
constexpr std::size_t status_size_base = 103;
constexpr std::size_t status_size_battery = 106;
// BATTERY_STATE (0x88)
struct BatteryState {
std::uint8_t version = 1;
std::uint8_t flags = 0; // bit0: calibrated battery-voltage reading
std::uint16_t battery_mv = 0;
bool calibrated() const { return flags & 1; }
};
bool decode(const Bytes& body, BatteryState& out);
Bytes encode(const BatteryState& value);
/// @param body Raw message body bytes.
/// @param out Receives the decoded status. Accepts the 103-octet base body and any body of at
/// least 106 octets (trailing octets after the known fields are ignored, so a later
/// extension does not break this decoder); 104 or 105 octets are malformed.
/// @param out Receives the decoded status.
/// @return false if malformed.
bool decode(const Bytes& body, Status& out);
/// @param value Status to encode.
/// @return Encoded body bytes.
Bytes encode(const Status& value);
struct Result {
struct Confirm {
Code code = Code::accepted;
Bytes detail;
};
/// @param body Raw message body bytes.
/// @param out Receives the decoded result.
/// @return false if malformed.
bool decode(const Bytes& body, Result& out);
/// @param value Result to encode.
bool decode(const Bytes& body, Confirm& out);
/// @param value Confirmation to encode.
/// @return Encoded body bytes.
Bytes encode(const Result& value);
Bytes encode(const Confirm& value);
// ---- little-endian helpers shared with the test channel ----------------------------------
/// @note All multi-byte values are written little-endian.
class Writer {
public: