Files
MicrOBU/obu-firmware/components/microbu_v2x/include/microbu_v2x/stack.hpp
T
Ashin Walpola 50bbf6552d 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.
2026-11-11 00:13:57 +01:00

175 lines
8.5 KiB
C++

#pragma once
#include <microbu_v2x/access.hpp>
#include <microbu_v2x/id_change.hpp>
#include <vanetza/common/manual_runtime.hpp>
#include <vanetza/common/position_fix.hpp>
#include <vanetza/dcc/channel_load.hpp>
#include <vanetza/geonet/data_request.hpp>
#include <vanetza/geonet/data_indication.hpp>
#include <vanetza/geonet/data_confirm.hpp>
#include <vanetza/security/security_entity.hpp>
#include <functional>
#include <memory>
#include <optional>
namespace microbu_v2x {
enum class BtpType { a, b };
/** NF-SAP / BTP-DATA.request: TS 103 836-5-1 V2.1.1 Annex A.2
* and clauses 7, 8.2; TS 102 723-11 V2.0.0 clause 5
* incorporates V1.1.1, including its clause 5.1 GeoAware SAP.
* All port numbers are HOST order. data.size() is the Length parameter.
* Omitted GN parameters inherit the configured MIB. No magic sentinel values.
*/
struct BtpRequest {
BtpType type = BtpType::b;
std::optional<std::uint16_t> source_port;
std::uint16_t destination_port = 0;
std::optional<std::uint16_t> destination_port_info;
vanetza::geonet::TransportType transport = vanetza::geonet::TransportType::SHB;
vanetza::geonet::DestinationVariant destination = nullptr;
vanetza::geonet::CommunicationProfile communication_profile = vanetza::geonet::CommunicationProfile::ITS_G5;
// The selected security entity implements this profile; ITS-AID, SSP and
// context information are SN-ENCAP inputs (TS 102 723-8 V2.0.0 clause 5 /
// V1.1.1 Table 24) that GN forwards unchanged (TS 103 836-4-1 V2.2.1
// Table 34, Annex J.2 "Security context information").
std::optional<std::uint32_t> security_profile;
vanetza::ItsAid its_aid = 0;
vanetza::ByteBuffer permissions;
vanetza::ByteBuffer security_context; // see security::context for the values this library defines
std::optional<vanetza::geonet::Lifetime> maximum_lifetime;
std::optional<vanetza::geonet::DataRequest::Repetition> repetition;
std::optional<unsigned> maximum_hop_limit;
vanetza::geonet::TrafficClass traffic_class;
vanetza::ByteBuffer data;
};
/** BTP-DATA.indication: TS 103 836-5-1 V2.1.1 Annex A.3/8.3.
* gn retains the transport, destination, source vector, report, permissions,
* traffic class, remaining lifetime and hop limit. Owns the received payload.
*/
struct BtpIndication {
BtpType type;
std::optional<std::uint16_t> source_port;
std::uint16_t destination_port;
std::optional<std::uint16_t> destination_port_info;
vanetza::geonet::DataIndication gn;
std::optional<vanetza::ByteBuffer> certificate_id;
vanetza::ByteBuffer data;
};
/** GN-DATA.request: TS 103 836-4-1 V2.2.1 clause 9.3 N-SAP (EN 302 636-4-1
* clause 9.3 heritage). Unlike BtpRequest, data is the raw SDU
* with no assumed upper-layer header; the Common Header next_header is
* "Any". Only SHB and GBC are implemented; GUC, GAC and TSB are rejected
* as Result::unsupported, matching Stack::request(BtpRequest).
*/
struct GnRequest {
vanetza::geonet::TransportType transport = vanetza::geonet::TransportType::SHB;
vanetza::geonet::DestinationVariant destination = nullptr; // Area for GBC, null for SHB
vanetza::geonet::CommunicationProfile communication_profile = vanetza::geonet::CommunicationProfile::ITS_G5;
vanetza::geonet::TrafficClass traffic_class;
std::optional<vanetza::geonet::Lifetime> maximum_lifetime;
std::optional<unsigned> maximum_hop_limit;
std::optional<vanetza::geonet::DataRequest::Repetition> repetition;
vanetza::ItsAid its_aid = 0;
vanetza::ByteBuffer permissions;
vanetza::ByteBuffer security_context; // TS 103 836-4-1 V2.2.1 Annex J.2 Security context information
vanetza::ByteBuffer data;
};
/** GN-DATA.indication for a packet whose Common Header next_header is "Any"
* (no upper protocol registered by BtpRequest's handler), TS 103 836-4-1
* V2.2.1 clause 9.3. gn retains the complete router indication.
*/
struct GnIndication {
vanetza::geonet::DataIndication gn;
vanetza::ByteBuffer data;
};
struct StackConfig {
vanetza::geonet::MIB mib;
std::size_t maximum_gnpdu = 4096;
std::uint32_t security_profile = 0;
AlDataRequest radio_parameters;
StackConfig();
};
/** Portable network/transport module for the NF-SAP entry point.
* The caller serializes all calls in ONE task/event loop, including reception,
* clock advancement and destruction. Access/SecurityEntity must outlive Stack.
* Timer source is injectable for host tests, HIL and ESP-IDF esp_timer.
* No FreeRTOS task, radio, BLE link, socket or filesystem is opened internally.
* This is a port of the upstream router; see docs/conformance.md for R2 gaps.
*
* Identifier change (TS 103 836-4-1 V2.2.1 clause 10.2.1.4, anonymous address
* configuration): with itsGnLocalAddrConfMethod == Anonymous and an
* IdChangeService, the GN core subscribes at construction and unsubscribes at
* destruction. On COMMIT the GN address MID becomes the 48 least significant
* bits of the new HashedId8 (TS 102 940 V2.1.1 clause 6.5), with the I/G bit
* cleared (a source address is individual) and the U/L bit set; the link-layer
* source follows because every request takes it from the GN address. Between
* PREPARE and COMMIT the forwarding buffers are flushed and request() returns
* Result::identity_change_pending (TS 102 723-8 clause 6.3.1.3). The access
* adapter is not notified by the stack: it may subscribe to the same service
* (TS 102 723-7 analogy for the IN-SAP side is not defined here).
*/
class Stack {
public:
using Receive = std::function<void(BtpIndication)>;
using ReceiveGn = std::function<void(GnIndication)>;
using Report = std::function<void(Result)>;
/** id_change may be omitted: when security is a microbu_v2x::security::SecurityEntity
* its own identifier-change service is used. */
Stack(StackConfig, vanetza::ManualRuntime&, Access&,
vanetza::security::SecurityEntity* security = nullptr,
security::IdChangeService* id_change = nullptr);
~Stack();
Stack(const Stack&) = delete;
Stack& operator=(const Stack&) = delete;
Result request(BtpRequest);
Result request(GnRequest);
Result indicate(AlDataIndication);
Result update_position(const vanetza::PositionFix&);
Result advance(vanetza::Clock::time_point);
void on_receive(Receive);
void on_receive_gn(ReceiveGn);
void on_access_result(Report);
/// Observe the native Vanetza packet drop reason without changing its decision.
void on_packet_drop(std::function<void(const std::string&)>);
const StackConfig& config() const;
/// the identifier-change service this stack subscribed to, nullptr without one
security::IdChangeService* id_change();
vanetza::security::SecurityEntity* security_entity();
/// true between PREPARE and COMMIT (or ABORT) of an identifier change
bool identity_change_pending() const;
/// GeoNetworking address currently in use (MID follows the identifier change)
const vanetza::geonet::Address& address() const;
/** Managed address configuration (TS 103 836-4-1 V2.2.1 clause 10.2.1.3.3): the N&T
* management entity updates the GN address with an unsolicited CORE_MMT.response
* (MN_SAP::CORE_MMT_response_apply). Result::unsupported with Auto (10.2.1.2) or
* Anonymous (10.2.1.4) configuration, identity_change_pending during a change. */
Result set_address(const vanetza::geonet::Address&);
/** Release-2 DCC_NET (TS 103 836-4-2 V2.1.1 clauses 5, 6.2, 6.3.3, 7.2).
* Always active for this ITS-G5-only Stack: outgoing SHB packets carry a real DCC-MCO
* field (local/one-hop CBR, TX power) instead of the upstream NullDccFieldGenerator's
* reserved zero field, and every received SHB packet's DCC-MCO updates LocTEX-G5
* (already unconditional in the upstream router once itsGnIfType is ITS_G5, which this
* Stack requires). report_local_channel_load feeds DCC_NET's own CBR_G calculation
* (clause 5.3); it is independent of AccessStack::report_channel_load (DCC_ACC), which
* the caller should feed with global_channel_busy_ratio() when available, else the same
* local measurement.
*/
void report_local_channel_load(vanetza::dcc::ChannelLoad);
/// EIRP of the station's own transmissions, for the outgoing DCC-MCO field's TX-power octet.
void report_tx_power(unsigned dbm);
/** CBR_G (TS 103 836-4-2 clause 5.3), once at least one 100 ms aggregation cycle has run;
* std::nullopt beforehand (no neighbour data collected yet). */
std::optional<vanetza::dcc::ChannelLoad> global_channel_busy_ratio() const;
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
}