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.
104 lines
4.9 KiB
C++
104 lines
4.9 KiB
C++
#pragma once
|
|
#include <microbu_v2x/access.hpp>
|
|
#include <esp_err.h>
|
|
#include <functional>
|
|
#include <memory>
|
|
|
|
namespace microbu {
|
|
|
|
struct C5RadioConfig {
|
|
std::uint16_t channel_number = 180;
|
|
double transmit_power_dbm = 10.0;
|
|
unsigned receive_queue_length = 16;
|
|
bool laboratory_transmission = true;
|
|
};
|
|
|
|
|
|
/// Snapshot of the ESP32-C5 27-bit hardware PHY counters for ETSI TS 102 687 / EN 303 797 DCC.
|
|
/// total_cycles: 40 MHz FE clock cycles (register 0x600a7c5c[26:0]).
|
|
/// busy_cycles: cycles during which received RF energy exceeded the CCA threshold (register 0x600a7c60[26:0]).
|
|
struct CcaCounters {
|
|
std::uint32_t total_cycles = 0;
|
|
std::uint32_t busy_cycles = 0;
|
|
/// The counter window had completed (bit 27) and was re-armed by this read: the next reading starts a new
|
|
/// baseline, this one closes no valid window.
|
|
bool restarted = false;
|
|
};
|
|
|
|
/**
|
|
* ITS-G5 802.11p Radio Access Adapter for ESP32-C5 (EN 303 797 Annex B.2).
|
|
* Direct Wi-Fi promiscuous RX mode on 5.9 GHz (ITS-G5 Channel 180, 10 MHz BW)
|
|
* and 802.11p frame transmission via esp_wifi_80211_tx_custom.
|
|
*/
|
|
class C5Radio final : public microbu_v2x::Access {
|
|
public:
|
|
using Receive = std::function<void(microbu_v2x::AlDataIndication)>;
|
|
/// Raw frame tap: MPDU bytes without FCS, RSSI (dBm) and the local receive time in microseconds.
|
|
using Capture = std::function<void(const vanetza::ByteBuffer&, int, std::uint32_t)>;
|
|
/// MicrOBU: every received 802.11 data frame (FCS included) and its RSSI, before the stack sees it.
|
|
using Forward = std::function<void(const vanetza::ByteBuffer&, int)>;
|
|
/// Transmit tap: the exact 802.11 frame handed to the driver.
|
|
using Transmitted = std::function<void(const std::uint8_t*, std::size_t)>;
|
|
|
|
/// Raw capture mode bits (RAW_CAPTURE, ../../docs/station-link.tex).
|
|
static constexpr std::uint8_t capture_on = 0x01; ///< every valid frame, like the OTM receiver
|
|
static constexpr std::uint8_t capture_broadcast_only = 0x02; ///< OTM's broadcast filter (address 1 = FF..FF)
|
|
static constexpr std::uint8_t capture_own_tx = 0x04; ///< also the frames this radio transmits
|
|
|
|
/// @brief Constructs a radio adapter with the given configuration (not yet started).
|
|
/// @param config Radio configuration; defaults to channel 180, 10 dBm, 16-deep RX queue.
|
|
explicit C5Radio(C5RadioConfig config = {});
|
|
/// @brief Stops the radio and releases all resources.
|
|
~C5Radio() override;
|
|
C5Radio(const C5Radio&) = delete;
|
|
C5Radio& operator=(const C5Radio&) = delete;
|
|
|
|
/// @brief Initializes Wi-Fi in promiscuous 802.11p mode and starts the radio.
|
|
/// @return ESP_OK on success, otherwise an ESP-IDF error code.
|
|
esp_err_t start();
|
|
/// @brief Stops the radio and releases all resources.
|
|
void stop();
|
|
|
|
// microbu_v2x::Access
|
|
/// @brief Encodes and transmits an ITS-G5 frame requested by the access layer.
|
|
/// @param request Frame and transmit parameters from the access layer.
|
|
/// @return Result of the transmit attempt.
|
|
microbu_v2x::Result request(microbu_v2x::AlDataRequest request) override;
|
|
|
|
/// @brief Dispatches queued received frames to the owning task callback.
|
|
/// @param receive Invoked once per queued frame with its decoded indication.
|
|
/// @param capture Optional; invoked with the raw frame bytes, RSSI and timestamp for diagnostics.
|
|
/// @param forward Optional MicrOBU tap: called for every data frame, independent of `receive`.
|
|
void poll(const Receive& receive, const Capture& capture = {}, const Forward& forward = {});
|
|
|
|
/// @brief Switches the raw capture tap. While on, the promiscuous filter admits every frame class
|
|
/// except bad FCS (as the OpenTrafficMap receiver firmware does); only data frames reach the stack.
|
|
/// @param mode Combination of capture_on, capture_broadcast_only and capture_own_tx (0 = off).
|
|
void set_capture(std::uint8_t mode);
|
|
|
|
/// @brief Registers the tap invoked for every successfully submitted frame while capture_own_tx is set.
|
|
void on_transmitted(Transmitted tap);
|
|
|
|
|
|
/// @brief Non-blocking read of the hardware CCA counters for periodic DCC evaluation.
|
|
/// Continuous 40 MHz hardware integration satisfies EN 303 797 Profile-1 (>=1 kHz) with zero CPU busy-wait.
|
|
/// @return Snapshot of total and busy cycle counts.
|
|
CcaCounters read_cca_counters() const;
|
|
|
|
/// @brief Calculates Channel Busy Ratio (CBR) between two counter snapshots: delta(busy) / delta(total).
|
|
/// @param current More recent counter snapshot.
|
|
/// @param previous Earlier counter snapshot.
|
|
/// @return CBR in [0, 1].
|
|
static double calculate_cbr(const CcaCounters& current, const CcaCounters& previous);
|
|
|
|
/// @brief Number of frames dropped since start() due to queue overflow or invalid length.
|
|
/// @return Dropped frame count.
|
|
std::uint32_t dropped_frames() const;
|
|
|
|
private:
|
|
class Impl;
|
|
std::unique_ptr<Impl> impl_;
|
|
};
|
|
|
|
} // namespace microbu
|