2026-09-23 17:27:54 +02:00
|
|
|
#include "c5_radio.hpp"
|
|
|
|
|
#include "otm_tx_custom.h"
|
2026-11-11 00:13:57 +01:00
|
|
|
#include "radio_policy.hpp"
|
|
|
|
|
#include "cca_window.hpp"
|
|
|
|
|
#include <microbu_v2x/its_g5_frame.hpp>
|
2026-09-23 17:27:54 +02:00
|
|
|
#include <esp_event.h>
|
|
|
|
|
#include <esp_log.h>
|
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
|
#include <hal/modem_syscon_ll.h>
|
|
|
|
|
#include <esp_timer.h>
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
#include <freertos/queue.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
extern "C" { //all of these arentt in the esp-idf public api
|
|
|
|
|
// phy_11p_set/phy_change_channel: undocumented esp_phy/lib/esp32c5/libphy.a entry points, not
|
|
|
|
|
// declared in any Espressif header.
|
|
|
|
|
// The call sites and argument values below (phy_11p_set(1, 0), phy_change_channel(freq, 1, 0, 0))
|
|
|
|
|
// are copied from OpenTrafficMap's its-g5-receiver-firmware_txenabled, main/cmd_sniffer.c
|
|
|
|
|
// (https://codeberg.org/opentrafficmap/its-g5-receiver-firmware_txenabled, community reverse
|
|
|
|
|
// engineering, no stated license).
|
|
|
|
|
|
|
|
|
|
void phy_11p_set(int enable, int arg2);
|
|
|
|
|
void phy_change_channel(int freq_mhz, int arg2_ignored, int arg3_ignored, int arg4);
|
|
|
|
|
|
|
|
|
|
// phy_get_cca/phy_set_cca: register 0x600a701c[7:0] holds the configured CCA energy
|
|
|
|
|
// detection threshold (defaults to 191 = 0xBF = -65 dBm in 8-bit two's complement).
|
|
|
|
|
// phy_get_cca() reads this configured threshold.
|
|
|
|
|
int phy_get_cca(void);
|
|
|
|
|
void phy_set_cca(int enable, int threshold);
|
|
|
|
|
|
|
|
|
|
// phy_get_cca_cnt/phy_set_cca_cnt: register 0x600a7c58 arms the 27-bit hardware CCA
|
|
|
|
|
// cycle counters (0x600a7c5c = total cycles, 0x600a7c60 = busy cycles; confirmed on
|
|
|
|
|
// hardware -- out[0] free-runs at ~40 MHz, out[1] stays near zero on a quiet channel).
|
|
|
|
|
// phy_get_cca_cnt returns bit 27 (busy/status bit) and writes both counters to out[2].
|
|
|
|
|
int phy_get_cca_cnt(std::int32_t out[2]);
|
|
|
|
|
void phy_set_cca_cnt(std::int32_t val, bool enable);
|
|
|
|
|
void phy_enable_cca(void);
|
|
|
|
|
void phy_disable_cca(void);
|
|
|
|
|
int phy_get_noise_floor(void);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
namespace microbu {
|
|
|
|
|
void arm_cca_counters();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 17:27:54 +02:00
|
|
|
namespace microbu {
|
|
|
|
|
namespace {
|
|
|
|
|
const char* TAG = "c5_radio";
|
2026-11-11 00:13:57 +01:00
|
|
|
// Driver TX completion of esp_wifi_80211_tx_custom frames (Wi-Fi task): the MAC finished the frame, which says
|
|
|
|
|
// nothing about its reception.
|
|
|
|
|
std::atomic<std::uint32_t> tx_completed {0}, tx_completion_failed {0};
|
|
|
|
|
void tx_done(const esp_80211_tx_info_t* info) {
|
|
|
|
|
if (!info) return;
|
|
|
|
|
if (info->tx_status == WIFI_SEND_SUCCESS) ++tx_completed;
|
|
|
|
|
else ++tx_completion_failed;
|
|
|
|
|
}
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class C5Radio::Impl {
|
|
|
|
|
public:
|
|
|
|
|
/// @brief Raw received frame metadata and bytes from the promiscuous RX callback.
|
|
|
|
|
struct Raw {
|
2026-11-11 00:13:57 +01:00
|
|
|
std::uint16_t length; // sig_len, including FCS (stack path)
|
|
|
|
|
std::uint16_t capture_length; // dump_len, MPDU without FCS (raw capture); 0 = not captured
|
|
|
|
|
bool for_stack; // data frame for the ITS-G5 stack
|
2026-09-23 17:27:54 +02:00
|
|
|
std::int8_t rssi;
|
|
|
|
|
std::uint32_t timestamp;
|
|
|
|
|
std::uint8_t bytes[2346]; // max 802.11 frame size
|
|
|
|
|
};
|
2026-11-11 00:13:57 +01:00
|
|
|
// Reserve the normal RX queue before runtime allocations fragment the heap.
|
|
|
|
|
// Sixteen maximum-sized frames need more than 37 kB in one block; a late
|
|
|
|
|
// xQueueCreate failed despite 141 kB free (largest block 36 kB).
|
|
|
|
|
static constexpr unsigned reserved_rx_frames = 16;
|
|
|
|
|
static StaticQueue_t rx_control;
|
|
|
|
|
alignas(portBYTE_ALIGNMENT) static std::uint8_t rx_storage[reserved_rx_frames * sizeof(Raw)];
|
2026-09-23 17:27:54 +02:00
|
|
|
C5RadioConfig config;
|
|
|
|
|
QueueHandle_t queue = nullptr;
|
|
|
|
|
bool initialized = false, started = false, own_event_loop = false;
|
|
|
|
|
std::uint16_t sequence = 0;
|
|
|
|
|
std::atomic<std::uint32_t> dropped {0};
|
2026-11-11 00:13:57 +01:00
|
|
|
std::atomic<std::uint8_t> capture_mode {0};
|
|
|
|
|
Transmitted transmitted;
|
|
|
|
|
std::uint32_t submitted = 0, no_mem = 0;
|
|
|
|
|
std::int64_t last_tx_report_us = 0;
|
2026-09-23 17:27:54 +02:00
|
|
|
static Impl* active;
|
|
|
|
|
static std::mutex callback_mutex;
|
|
|
|
|
|
|
|
|
|
explicit Impl(C5RadioConfig c) : config(c) {}
|
|
|
|
|
|
|
|
|
|
static void receive(void* buffer, wifi_promiscuous_pkt_type_t type) {
|
2026-11-11 00:13:57 +01:00
|
|
|
// Like the OTM receiver (cmd_sniffer.c wifi_sniffer_cb): no MISC frames, only rx_state 0.
|
|
|
|
|
if (!buffer || type == WIFI_PKT_MISC) return;
|
2026-09-23 17:27:54 +02:00
|
|
|
const auto* packet = static_cast<const wifi_promiscuous_pkt_t*>(buffer);
|
|
|
|
|
if (packet->rx_ctrl.rx_state != 0) return;
|
|
|
|
|
const auto length = packet->rx_ctrl.sig_len;
|
|
|
|
|
std::lock_guard<std::mutex> lock(callback_mutex);
|
|
|
|
|
if (!active || !active->queue) return;
|
2026-11-11 00:13:57 +01:00
|
|
|
const auto mode = active->capture_mode.load();
|
|
|
|
|
bool stack = type == WIFI_PKT_DATA;
|
|
|
|
|
// MicrOBU: a frame too short to hold any GN packet is not ITS traffic, so it is ignored rather
|
|
|
|
|
// than counted; dropped_frames() then means what the phone shows it as: lost frames.
|
|
|
|
|
if (stack && length < 38) stack = false;
|
|
|
|
|
else if (stack && length > sizeof(Raw::bytes)) { ++active->dropped; stack = false; }
|
|
|
|
|
#if CONFIG_SOC_WIFI_HE_SUPPORT
|
|
|
|
|
std::uint16_t capture_length = packet->rx_ctrl.dump_len; // MPDU without FCS (OTM: HE chips)
|
|
|
|
|
#else
|
|
|
|
|
std::uint16_t capture_length = length > 4 ? length - 4 : 0;
|
|
|
|
|
#endif
|
|
|
|
|
if (!(mode & capture_on) || capture_length > sizeof(Raw::bytes)) capture_length = 0;
|
|
|
|
|
if (capture_length && (mode & capture_broadcast_only) &&
|
|
|
|
|
(capture_length < 4 + 6 || std::memcmp(&packet->payload[4], "\xFF\xFF\xFF\xFF\xFF\xFF", 6) != 0)) {
|
|
|
|
|
capture_length = 0; // OTM only_capture_broadcast: address 1 must be the broadcast address
|
|
|
|
|
}
|
|
|
|
|
if (!stack && !capture_length) return;
|
2026-09-23 17:27:54 +02:00
|
|
|
// MicrOBU: static, not a local. Raw is ~2.4 KB and this runs on the Wi-Fi driver's own task,
|
2026-11-11 00:13:57 +01:00
|
|
|
// several frames deep, on a stack of roughly 3.5 KB (the previous firmware hit exactly this
|
|
|
|
|
// with an 800-byte buffer, commit 04b0076). Safe as a static because only that one task calls
|
|
|
|
|
// this, and xQueueSend copies it out before the next call.
|
2026-09-23 17:27:54 +02:00
|
|
|
static Raw raw;
|
|
|
|
|
raw.length = length;
|
2026-11-11 00:13:57 +01:00
|
|
|
raw.capture_length = capture_length;
|
|
|
|
|
raw.for_stack = stack;
|
2026-09-23 17:27:54 +02:00
|
|
|
raw.rssi = packet->rx_ctrl.rssi;
|
|
|
|
|
raw.timestamp = packet->rx_ctrl.timestamp;
|
2026-11-11 00:13:57 +01:00
|
|
|
std::memcpy(raw.bytes, packet->payload, std::min<std::size_t>(std::max<std::size_t>(length, capture_length), sizeof(Raw::bytes)));
|
2026-09-23 17:27:54 +02:00
|
|
|
if (xQueueSend(active->queue, &raw, 0) != pdTRUE) ++active->dropped;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
C5Radio::Impl* C5Radio::Impl::active = nullptr;
|
|
|
|
|
std::mutex C5Radio::Impl::callback_mutex;
|
2026-11-11 00:13:57 +01:00
|
|
|
StaticQueue_t C5Radio::Impl::rx_control;
|
|
|
|
|
alignas(portBYTE_ALIGNMENT) std::uint8_t C5Radio::Impl::rx_storage[reserved_rx_frames * sizeof(Raw)];
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
// A transmitting radio only on a declared operating point (channel and power cap, power
|
|
|
|
|
// in the driver's 0.25 dB steps); a receive-only radio on any ITS-G5 carrier. The laboratory test
|
|
|
|
|
// burst (transmit_burst) retunes past this on purpose.
|
|
|
|
|
bool valid_config(const C5RadioConfig& c) {
|
|
|
|
|
const bool point = c.laboratory_transmission
|
|
|
|
|
? radio_policy::operating_point_allowed(c.channel_number, c.transmit_power_dbm)
|
|
|
|
|
: radio_policy::receive_channel_allowed(c.channel_number) && std::isfinite(c.transmit_power_dbm);
|
|
|
|
|
return point && c.receive_queue_length > 0 && c.receive_queue_length <= 32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 10 MHz channel bandwidth (ITS-G5 / 802.11p) on the channel, and the hardware CCA counters for DCC
|
|
|
|
|
// (40 MHz baseband clock timebase) armed.
|
|
|
|
|
void tune_its_g5_phy(unsigned channel_number) {
|
|
|
|
|
phy_11p_set(1, 0);
|
|
|
|
|
phy_change_channel(5000 + 5 * channel_number, 1, 0, 0); // channel 180 = 5900 MHz
|
|
|
|
|
phy_enable_cca();
|
|
|
|
|
phy_set_cca_cnt(0x07FFFFFF, true);
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
C5Radio::C5Radio(C5RadioConfig c) : impl_(std::make_unique<Impl>(c)) {}
|
|
|
|
|
C5Radio::~C5Radio() { stop(); }
|
|
|
|
|
|
|
|
|
|
esp_err_t C5Radio::start() {
|
|
|
|
|
auto& p = *impl_;
|
|
|
|
|
const auto& c = p.config;
|
|
|
|
|
if (p.initialized) return ESP_ERR_INVALID_STATE;
|
2026-11-11 00:13:57 +01:00
|
|
|
if (!valid_config(c)) return ESP_ERR_INVALID_ARG;
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(Impl::callback_mutex);
|
|
|
|
|
if (Impl::active) return ESP_ERR_INVALID_STATE;
|
2026-11-11 00:13:57 +01:00
|
|
|
p.queue = c.receive_queue_length <= Impl::reserved_rx_frames
|
|
|
|
|
? xQueueCreateStatic(c.receive_queue_length, sizeof(Impl::Raw), Impl::rx_storage, &Impl::rx_control)
|
|
|
|
|
: xQueueCreate(c.receive_queue_length, sizeof(Impl::Raw));
|
2026-09-23 17:27:54 +02:00
|
|
|
if (!p.queue) return ESP_ERR_NO_MEM;
|
|
|
|
|
Impl::active = &p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = esp_event_loop_create_default();
|
|
|
|
|
p.own_event_loop = (result == ESP_OK);
|
|
|
|
|
if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) {
|
|
|
|
|
stop();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Establish modem FE clock for 802.11p OFDM
|
|
|
|
|
modem_syscon_ll_enable_fe_40m_clock(&MODEM_SYSCON, true);
|
|
|
|
|
|
|
|
|
|
wifi_init_config_t wifi = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
|
wifi.nvs_enable = 0;
|
|
|
|
|
result = esp_wifi_init(&wifi);
|
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
|
stop();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
p.initialized = true;
|
|
|
|
|
|
|
|
|
|
auto attempt = [&](esp_err_t r) { if (result == ESP_OK) result = r; };
|
|
|
|
|
attempt(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
|
|
|
|
attempt(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
|
if (result == ESP_OK) {
|
|
|
|
|
result = esp_wifi_start();
|
|
|
|
|
p.started = (result == ESP_OK);
|
|
|
|
|
}
|
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
|
stop();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MicrOBU, from the previous firmware: under the default WIFI_COUNTRY_POLICY_AUTO the driver's
|
|
|
|
|
// 5 GHz table does not authorise transmission on the ITS band, which there left RX working and
|
2026-11-11 00:13:57 +01:00
|
|
|
// TX silent. The raw TX path goes around that gate, so this is belt and braces: manual policy,
|
|
|
|
|
// every 5 GHz channel enabled. Not fatal if refused.
|
2026-09-23 17:27:54 +02:00
|
|
|
wifi_country_t country = {};
|
|
|
|
|
country.cc[0] = 'U'; country.cc[1] = 'S';
|
|
|
|
|
country.schan = 1;
|
|
|
|
|
country.nchan = 11;
|
|
|
|
|
country.policy = WIFI_COUNTRY_POLICY_MANUAL;
|
|
|
|
|
country.wifi_5g_channel_mask = 0x1FFFFFFE;
|
|
|
|
|
if (const auto e = esp_wifi_set_country(&country); e != ESP_OK)
|
|
|
|
|
ESP_LOGW(TAG, "esp_wifi_set_country(MANUAL) failed: %s (continuing)", esp_err_to_name(e));
|
|
|
|
|
attempt(esp_wifi_set_band_mode(WIFI_BAND_MODE_5G_ONLY));
|
|
|
|
|
attempt(esp_wifi_set_ps(WIFI_PS_NONE));
|
|
|
|
|
attempt(esp_wifi_set_max_tx_power(static_cast<std::int8_t>(c.transmit_power_dbm * 4)));
|
|
|
|
|
|
|
|
|
|
wifi_promiscuous_filter_t filter {};
|
|
|
|
|
filter.filter_mask = WIFI_PROMIS_FILTER_MASK_DATA;
|
|
|
|
|
attempt(esp_wifi_set_promiscuous_filter(&filter));
|
|
|
|
|
attempt(esp_wifi_set_promiscuous_rx_cb(Impl::receive));
|
2026-11-11 00:13:57 +01:00
|
|
|
tx_completed = 0;
|
|
|
|
|
tx_completion_failed = 0;
|
|
|
|
|
attempt(esp_wifi_register_80211_tx_cb(tx_done));
|
2026-09-23 17:27:54 +02:00
|
|
|
attempt(esp_wifi_set_promiscuous(true));
|
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
|
stop();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MicrOBU, from the previous firmware: the power request is a ceiling, not a promise. The driver
|
|
|
|
|
// clamps it to its calibrated table, and 5900 MHz is above the chip's rated range, so log what
|
|
|
|
|
// the driver admits to rather than what was asked for.
|
|
|
|
|
std::int8_t power_q = 0;
|
|
|
|
|
if (esp_wifi_get_max_tx_power(&power_q) == ESP_OK) {
|
|
|
|
|
ESP_LOGI(TAG, "tx power: %d quarter-dBm = %d.%02d dBm (%.2f requested)",
|
|
|
|
|
power_q, power_q / 4, (power_q % 4) * 25, c.transmit_power_dbm);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
tune_its_g5_phy(c.channel_number);
|
|
|
|
|
arm_cca_counters(); // the DCC LCBR source (Station::sample_dcc_channel_load); off until armed
|
|
|
|
|
ESP_LOGI(TAG, "ITS-G5 802.11p radio started on channel %u (%u MHz) at %.2f dBm, %s",
|
|
|
|
|
unsigned(c.channel_number), 5000u + 5u * c.channel_number, c.transmit_power_dbm,
|
|
|
|
|
c.laboratory_transmission ? "TX/RX" : "RX only");
|
2026-09-23 17:27:54 +02:00
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void C5Radio::stop() {
|
|
|
|
|
if (!impl_) return;
|
|
|
|
|
auto& p = *impl_;
|
|
|
|
|
if (p.started) esp_wifi_set_promiscuous(false);
|
2026-11-11 00:13:57 +01:00
|
|
|
if (p.initialized) esp_wifi_register_80211_tx_cb(nullptr);
|
2026-09-23 17:27:54 +02:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(Impl::callback_mutex);
|
|
|
|
|
if (Impl::active == &p) Impl::active = nullptr;
|
|
|
|
|
if (p.queue) {
|
|
|
|
|
vQueueDelete(p.queue);
|
|
|
|
|
p.queue = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (p.started) esp_wifi_stop();
|
|
|
|
|
if (p.initialized) esp_wifi_deinit();
|
|
|
|
|
if (p.own_event_loop) esp_event_loop_delete_default();
|
|
|
|
|
p.started = p.initialized = p.own_event_loop = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
microbu_v2x::Result C5Radio::request(microbu_v2x::AlDataRequest request) {
|
2026-09-23 17:27:54 +02:00
|
|
|
auto& p = *impl_;
|
2026-11-11 00:13:57 +01:00
|
|
|
if (!p.started) return microbu_v2x::Result::rejected;
|
|
|
|
|
if (!p.config.laboratory_transmission) return microbu_v2x::Result::unsupported;
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
if (request.bandwidth_mhz != 10 || request.channel_number != p.config.channel_number ||
|
|
|
|
|
request.transceiver_id != 0 || request.transceiver_mode || request.datastream_id ||
|
|
|
|
|
request.transmit_power_dbm != p.config.transmit_power_dbm) {
|
2026-11-11 00:13:57 +01:00
|
|
|
return microbu_v2x::Result::unsupported;
|
2026-09-23 17:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr wifi_phy_rate_t rates[] = {
|
|
|
|
|
WIFI_PHY_RATE_6M, WIFI_PHY_RATE_9M, WIFI_PHY_RATE_12M,
|
|
|
|
|
WIFI_PHY_RATE_18M, WIFI_PHY_RATE_24M, WIFI_PHY_RATE_36M,
|
|
|
|
|
WIFI_PHY_RATE_48M, WIFI_PHY_RATE_54M
|
|
|
|
|
};
|
|
|
|
|
const auto index = static_cast<unsigned>(request.mcs);
|
2026-11-11 00:13:57 +01:00
|
|
|
if (index >= std::size(rates)) return microbu_v2x::Result::invalid_argument;
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
vanetza::ByteBuffer bytes;
|
2026-11-11 00:13:57 +01:00
|
|
|
const auto encoded = microbu_v2x::its_g5::encode_frame(request, p.sequence, bytes);
|
|
|
|
|
if (encoded != microbu_v2x::Result::accepted) {
|
2026-09-23 17:27:54 +02:00
|
|
|
ESP_LOGE(TAG, "encode_frame failed: %d", int(encoded));
|
|
|
|
|
return encoded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.sequence = (p.sequence + 1) & 4095;
|
|
|
|
|
wifi_tx_rate_config_t rate {};
|
|
|
|
|
rate.phymode = WIFI_PHY_MODE_11A;
|
|
|
|
|
rate.rate = rates[index];
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
// Transmit frame via 802.11p driver. ESP_ERR_NO_MEM means the driver's TX buffers are momentarily all in use
|
|
|
|
|
// (frames waiting for the shared radio, e.g. a BLE connection event); the frame is still well inside its
|
|
|
|
|
// lifetime, so it is offered again after a tick, at most twice (2026-09-29: 1 of 470 BLE-path VAMs lost).
|
|
|
|
|
auto result = esp_wifi_80211_tx_custom(
|
2026-09-23 17:27:54 +02:00
|
|
|
WIFI_IF_STA, bytes.data(), bytes.size(), false,
|
|
|
|
|
&rate, WIFI_BAND_5G, WIFI_BW20);
|
2026-11-11 00:13:57 +01:00
|
|
|
if (result == ESP_ERR_NO_MEM) ++p.no_mem;
|
|
|
|
|
for (int retry = 0; result == ESP_ERR_NO_MEM && retry < 2; ++retry) {
|
|
|
|
|
vTaskDelay(std::max<TickType_t>(1, pdMS_TO_TICKS(2)));
|
|
|
|
|
result = esp_wifi_80211_tx_custom(WIFI_IF_STA, bytes.data(), bytes.size(), false, &rate, WIFI_BAND_5G, WIFI_BW20);
|
|
|
|
|
if (result == ESP_ERR_NO_MEM) ++p.no_mem;
|
|
|
|
|
}
|
|
|
|
|
if (result == ESP_OK) ++p.submitted;
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
|
ESP_LOGW(TAG, "esp_wifi_80211_tx_custom failed: %s (0x%x)", esp_err_to_name(result), result);
|
2026-11-11 00:13:57 +01:00
|
|
|
} else if ((p.capture_mode.load() & (capture_on | capture_own_tx)) == (capture_on | capture_own_tx) && p.transmitted) {
|
|
|
|
|
p.transmitted(bytes.data(), bytes.size());
|
2026-09-23 17:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
return result == ESP_OK ? microbu_v2x::Result::accepted :
|
|
|
|
|
result == ESP_ERR_NO_MEM ? microbu_v2x::Result::resource_limit :
|
|
|
|
|
microbu_v2x::Result::rejected;
|
2026-09-23 17:27:54 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
void C5Radio::poll(const Receive& receive, const Capture& capture, const Forward& forward) {
|
2026-09-23 17:27:54 +02:00
|
|
|
auto& p = *impl_;
|
|
|
|
|
if (!p.queue) return;
|
2026-11-11 00:13:57 +01:00
|
|
|
const auto now_us = esp_timer_get_time();
|
|
|
|
|
if (p.submitted && now_us - p.last_tx_report_us >= 1000000) {
|
|
|
|
|
ESP_LOGI(TAG, "TX observation: submitted=%u completed=%u completion_failed=%u no_mem=%u",
|
|
|
|
|
unsigned(p.submitted), unsigned(tx_completed.load()),
|
|
|
|
|
unsigned(tx_completion_failed.load()), unsigned(p.no_mem));
|
|
|
|
|
p.last_tx_report_us = now_us;
|
|
|
|
|
}
|
2026-09-23 17:27:54 +02:00
|
|
|
Impl::Raw raw {};
|
|
|
|
|
for (unsigned i = 0; i < p.config.receive_queue_length && xQueueReceive(p.queue, &raw, 0) == pdTRUE; ++i) {
|
2026-11-11 00:13:57 +01:00
|
|
|
if (raw.for_stack && forward) forward(vanetza::ByteBuffer(raw.bytes, raw.bytes + raw.length), raw.rssi);
|
|
|
|
|
if (raw.for_stack && receive) {
|
|
|
|
|
microbu_v2x::AlDataIndication ind;
|
|
|
|
|
if (microbu_v2x::its_g5::decode_frame(raw.bytes, raw.length, true, ind) == microbu_v2x::Result::accepted) {
|
|
|
|
|
ind.channel_number = p.config.channel_number;
|
|
|
|
|
ind.received_power_dbm = raw.rssi;
|
|
|
|
|
receive(std::move(ind));
|
|
|
|
|
}
|
2026-09-23 17:27:54 +02:00
|
|
|
}
|
2026-11-11 00:13:57 +01:00
|
|
|
// The raw tap uses the same queued frame and runs after normal delivery.
|
|
|
|
|
if (capture && raw.capture_length) {
|
|
|
|
|
capture(vanetza::ByteBuffer(raw.bytes, raw.bytes + raw.capture_length), raw.rssi, raw.timestamp);
|
2026-09-23 17:27:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
void C5Radio::set_capture(std::uint8_t mode) {
|
|
|
|
|
auto& p = *impl_;
|
|
|
|
|
p.capture_mode.store(mode);
|
|
|
|
|
if (!p.started) return;
|
|
|
|
|
wifi_promiscuous_filter_t filter {};
|
|
|
|
|
// OTM receiver default: WIFI_PROMIS_FILTER_MASK_ALL without FCSFAIL; the stack alone needs data frames.
|
|
|
|
|
filter.filter_mask = (mode & capture_on) ? (WIFI_PROMIS_FILTER_MASK_ALL & ~WIFI_PROMIS_FILTER_MASK_FCSFAIL)
|
|
|
|
|
: WIFI_PROMIS_FILTER_MASK_DATA;
|
|
|
|
|
esp_wifi_set_promiscuous_filter(&filter);
|
|
|
|
|
ESP_LOGI(TAG, "raw capture %s (mode 0x%02x)", (mode & capture_on) ? "on" : "off", unsigned(mode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void C5Radio::on_transmitted(Transmitted tap) {
|
|
|
|
|
impl_->transmitted = std::move(tap);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 17:27:54 +02:00
|
|
|
std::uint32_t C5Radio::dropped_frames() const {
|
|
|
|
|
return impl_->dropped.load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-11-11 00:13:57 +01:00
|
|
|
// The CCA cycle counters count one window of up to 2^27 cycles (3.36 s at 40 MHz) after an enable edge on bits
|
|
|
|
|
// [28:27] of 0x600a7c58, then latch bit 27 ("done") and stop; a board whose window has completed reads constant
|
|
|
|
|
// counters (2026-09-29: total 0, status 1). Arming therefore always clears the enable bits first.
|
|
|
|
|
void arm_cca_counters() {
|
|
|
|
|
phy_enable_cca(); // required: without it the counters stay at zero (2026-09-29, COM21)
|
|
|
|
|
phy_set_cca_cnt(0, false);
|
2026-09-23 17:27:54 +02:00
|
|
|
phy_set_cca_cnt(0x07FFFFFF, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CcaCounters C5Radio::read_cca_counters() const {
|
|
|
|
|
CcaCounters c;
|
|
|
|
|
std::int32_t out[2] = {};
|
2026-11-11 00:13:57 +01:00
|
|
|
if (phy_get_cca_cnt(out)) { // window complete: restart it; the caller takes the next reading as a baseline
|
|
|
|
|
arm_cca_counters();
|
|
|
|
|
c.restarted = true;
|
|
|
|
|
}
|
2026-09-23 17:27:54 +02:00
|
|
|
constexpr std::int32_t mask27 = 0x07FFFFFF; // remomve the busy/status bit (bit 27) from the 27-bit hardware counters
|
|
|
|
|
c.total_cycles = static_cast<std::uint32_t>(out[0] & mask27);
|
|
|
|
|
c.busy_cycles = static_cast<std::uint32_t>(out[1] & mask27);
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double C5Radio::calculate_cbr(const CcaCounters& current, const CcaCounters& previous) {
|
2026-11-11 00:13:57 +01:00
|
|
|
const std::uint32_t dt = cca::delta27(current.total_cycles, previous.total_cycles);
|
|
|
|
|
const std::uint32_t db = cca::delta27(current.busy_cycles, previous.busy_cycles);
|
2026-09-23 17:27:54 +02:00
|
|
|
if (dt == 0) return 0.0;
|
|
|
|
|
return static_cast<double>(db) / static_cast<double>(dt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace microbu
|