#pragma once #include #include #include #include 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; /// Raw frame tap: MPDU bytes without FCS, RSSI (dBm) and the local receive time in microseconds. using Capture = std::function; /// MicrOBU: every received 802.11 data frame (FCS included) and its RSSI, before the stack sees it. using Forward = std::function; /// Transmit tap: the exact 802.11 frame handed to the driver. using Transmitted = std::function; /// 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_; }; } // namespace microbu