use esp_radio::esp_now::EspNow;
use esp_radio::wifi::sniffer::Sniffer;
use esp_radio::wifi::{Interface, SecondaryChannel, WifiController};
use crate::log_ln;
pub(crate) struct RadioInterfaces {
pub(crate) station: Interface,
pub(crate) access_point: Interface,
pub(crate) sniffer: Sniffer,
pub(crate) esp_now: EspNow,
}
impl RadioInterfaces {
pub(crate) fn claim(controller: &WifiController<'_>) -> Self {
Self {
station: Interface::station(),
access_point: Interface::access_point(),
sniffer: controller.sniffer(),
esp_now: controller.esp_now(),
}
}
}
pub(crate) fn suppress_espnow_rx() {
unsafe extern "C" {
fn esp_now_unregister_recv_cb() -> i32;
}
unsafe {
let _ = esp_now_unregister_recv_cb();
}
}
pub(crate) async fn idle_station_for_espnow(controller: &mut WifiController<'_>) {
use esp_radio::wifi::{Config, sta::StationConfig};
unsafe extern "C" {
fn esp_wifi_get_mode(mode: *mut u32) -> i32;
}
const WIFI_MODE_STA: u32 = 1;
if controller.is_connected() && controller.disconnect_async().await.is_err() {
log_ln!("ESP-NOW: disconnecting the previous run's station failed");
}
let mut mode = 0u32;
if unsafe { esp_wifi_get_mode(&mut mode) } == 0
&& mode != WIFI_MODE_STA
&& controller
.set_config(&Config::Station(StationConfig::default()))
.is_err()
{
log_ln!("ESP-NOW: resetting the controller to an idle station failed");
}
}
pub(crate) fn apply_band_for_channel(controller: &mut WifiController, primary: u8) {
#[cfg(feature = "esp32c5")]
{
use esp_radio::wifi::BandMode;
let band = if primary >= 36 {
BandMode::_5G
} else {
BandMode::_2_4G
};
if controller.set_band_mode(band).is_err() {
log_ln!("radio: set_band_mode failed for ch {}", primary);
}
}
#[cfg(not(feature = "esp32c5"))]
{
let _ = (controller, primary);
}
}
#[cfg(feature = "esp32c5")]
pub(crate) fn apply_band_auto(controller: &mut WifiController) {
use esp_radio::wifi::BandMode;
if controller.set_band_mode(BandMode::Auto).is_err() {
log_ln!("radio: set_band_mode(Auto) failed");
}
}
pub(crate) fn apply_ht40_channel(
controller: &mut WifiController,
primary: u8,
secondary: SecondaryChannel,
) {
apply_band_for_channel(controller, primary);
if controller.set_channel(primary, secondary).is_err() {
log_ln!("HT40: set_channel failed");
}
use esp_radio::wifi::Bandwidth;
match controller.bandwidths() {
Ok(bw) => {
#[cfg(feature = "esp32c5")]
let bw = if primary >= 36 {
bw.with_5(Bandwidth::_40MHz)
} else {
bw.with_2_4(Bandwidth::_40MHz)
};
#[cfg(not(feature = "esp32c5"))]
let bw = bw.with_2_4(Bandwidth::_40MHz);
if let Err(e) = controller.set_bandwidths(bw) {
log_ln!("HT40: set_bandwidths failed: {:?}", e);
}
}
Err(_) => log_ln!("HT40: read bandwidths failed"),
}
}