diff --git a/esp-radio/CHANGELOG.md b/esp-radio/CHANGELOG.md index 7eace8961..3056dd2f0 100644 --- a/esp-radio/CHANGELOG.md +++ b/esp-radio/CHANGELOG.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ap_mac` and `sta_mac` returns `[u8; 6]` instead of taking an `[u8; 6]` argument (#4017) - `RxControlInfo` hidden behind `esp-now` feature (#4017) - `set_configuration()` to `set_config() (#4017) +- `WifiState` split into `WifiStaState` and `WifiApState` (#4046) ### Fixed @@ -49,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `scan_with_config_sync_max`, `scan_with_config_sync_max`, `scan_n`, and `scan_n_async` functions (#3963) - `EnumSetType` from `Protocol`, `Country` enums (#4017) - `AtomicWifiState` and `WifiDeviceMode` are not available anymore (#4029) +- `wifi_state()` and `WifiState` are not available anymore (#4046) ## [v0.15.0] - 2025-07-16 diff --git a/esp-radio/MIGRATING-0.15.0.md b/esp-radio/MIGRATING-0.15.0.md index dfaddf0c0..bbe44ce6d 100644 --- a/esp-radio/MIGRATING-0.15.0.md +++ b/esp-radio/MIGRATING-0.15.0.md @@ -87,4 +87,13 @@ Same for `set_configuration()` to `set_config()`: - config - }); + let ap_config = Config::AccessPoint(AccessPointConfig::default().with_ssid("esp-radio".into())); +``` + +## WifiState + +`wifi_state()` is removed and `WifiState` is split into `WifiStaState` and `WifiApState`: + +```diff +- if esp_radio::wifi::wifi_state() == WifiState::StaConnected { ... } ++ if esp_radio::wifi::sta_state() == WifiStaState::Connected { ... } ``` \ No newline at end of file diff --git a/esp-radio/src/wifi/mod.rs b/esp-radio/src/wifi/mod.rs index 182db44c7..f7f49371b 100644 --- a/esp-radio/src/wifi/mod.rs +++ b/esp-radio/src/wifi/mod.rs @@ -1884,14 +1884,14 @@ impl WifiDeviceMode { fn link_state(&self) -> embassy_net_driver::LinkState { match self { WifiDeviceMode::Sta => { - if matches!(sta_state(), WifiState::StaConnected) { + if matches!(sta_state(), WifiStaState::Connected) { embassy_net_driver::LinkState::Up } else { embassy_net_driver::LinkState::Down } } WifiDeviceMode::Ap => { - if matches!(ap_state(), WifiState::ApStarted) { + if matches!(ap_state(), WifiApState::Started) { embassy_net_driver::LinkState::Up } else { embassy_net_driver::LinkState::Down @@ -3027,18 +3027,18 @@ impl WifiController<'_> { esp_wifi_result!(unsafe { esp_wifi_disconnect() }) } - /// Checks if the Wi-Fi controller has started. + /// Checks if the Wi-Fi controller has started. Returns true if STA and/or AP are started. /// /// This function should be called after the `start` method to verify if the /// Wi-Fi has started successfully. pub fn is_started(&self) -> Result { if matches!( crate::wifi::sta_state(), - WifiState::StaStarted | WifiState::StaConnected | WifiState::StaDisconnected + WifiStaState::Started | WifiStaState::Connected | WifiStaState::Disconnected ) { return Ok(true); } - if matches!(crate::wifi::ap_state(), WifiState::ApStarted) { + if matches!(crate::wifi::ap_state(), WifiApState::Started) { return Ok(true); } Ok(false) @@ -3050,8 +3050,8 @@ impl WifiController<'_> { /// the connection was successful. pub fn is_connected(&self) -> Result { match crate::wifi::sta_state() { - crate::wifi::WifiState::StaConnected => Ok(true), - crate::wifi::WifiState::StaDisconnected => Err(WifiError::Disconnected), + crate::wifi::WifiStaState::Connected => Ok(true), + crate::wifi::WifiStaState::Disconnected => Err(WifiError::Disconnected), // FIXME: Should any other enum value trigger an error instead of returning false? _ => Ok(false), } diff --git a/esp-radio/src/wifi/state.rs b/esp-radio/src/wifi/state.rs index 3d25baeb9..b59a9b3c5 100644 --- a/esp-radio/src/wifi/state.rs +++ b/esp-radio/src/wifi/state.rs @@ -1,62 +1,78 @@ use core::sync::atomic::Ordering; -use private::AtomicWifiState; -pub use private::WifiState; +use private::{AtomicWifiApState, AtomicWifiStaState}; +pub use private::{WifiApState, WifiStaState}; use super::WifiEvent; mod private { use portable_atomic_enum::atomic_enum; - /// Wi-Fi interface state. + /// Wi-Fi interface for station state. #[atomic_enum] #[derive(PartialEq, Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[non_exhaustive] - pub enum WifiState { + pub enum WifiStaState { /// Station started. - StaStarted, + Started, /// Station connected. - StaConnected, + Connected, /// Station disconnected. - StaDisconnected, + Disconnected, /// Station stopped - StaStopped, + Stopped, + /// Invalid state. + Invalid, + } + /// Wi-Fi interface for access point state. + #[atomic_enum] + #[derive(PartialEq, Debug)] + #[cfg_attr(feature = "defmt", derive(defmt::Format))] + #[non_exhaustive] + pub enum WifiApState { /// Access point started. - ApStarted, + Started, /// Access point stopped. - ApStopped, - - /// Invalid Wi-Fi state. + Stopped, + /// Invalid state. Invalid, } } -impl From for WifiState { - fn from(event: WifiEvent) -> WifiState { +impl From for WifiStaState { + fn from(event: WifiEvent) -> WifiStaState { match event { - WifiEvent::StaStart => WifiState::StaStarted, - WifiEvent::StaConnected => WifiState::StaConnected, - WifiEvent::StaDisconnected => WifiState::StaDisconnected, - WifiEvent::StaStop => WifiState::StaStopped, - WifiEvent::ApStart => WifiState::ApStarted, - WifiEvent::ApStop => WifiState::ApStopped, - _ => WifiState::Invalid, + WifiEvent::StaStart => WifiStaState::Started, + WifiEvent::StaConnected => WifiStaState::Connected, + WifiEvent::StaDisconnected => WifiStaState::Disconnected, + WifiEvent::StaStop => WifiStaState::Stopped, + _ => WifiStaState::Invalid, } } } -pub(crate) static STA_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); -pub(crate) static AP_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); +impl From for WifiApState { + fn from(event: WifiEvent) -> WifiApState { + match event { + WifiEvent::ApStart => WifiApState::Started, + WifiEvent::ApStop => WifiApState::Stopped, + _ => WifiApState::Invalid, + } + } +} + +pub(crate) static STA_STATE: AtomicWifiStaState = AtomicWifiStaState::new(WifiStaState::Invalid); +pub(crate) static AP_STATE: AtomicWifiApState = AtomicWifiApState::new(WifiApState::Invalid); /// Get the current state of the AP. -pub fn ap_state() -> WifiState { +pub fn ap_state() -> WifiApState { AP_STATE.load(Ordering::Relaxed) } /// Get the current state of the STA. -pub fn sta_state() -> WifiState { +pub fn sta_state() -> WifiStaState { STA_STATE.load(Ordering::Relaxed) } @@ -65,10 +81,10 @@ pub(crate) fn update_state(event: WifiEvent, handled: bool) { WifiEvent::StaConnected | WifiEvent::StaDisconnected | WifiEvent::StaStart - | WifiEvent::StaStop => STA_STATE.store(WifiState::from(event), Ordering::Relaxed), + | WifiEvent::StaStop => STA_STATE.store(WifiStaState::from(event), Ordering::Relaxed), WifiEvent::ApStart | WifiEvent::ApStop => { - AP_STATE.store(WifiState::from(event), Ordering::Relaxed) + AP_STATE.store(WifiApState::from(event), Ordering::Relaxed) } other => { @@ -80,23 +96,9 @@ pub(crate) fn update_state(event: WifiEvent, handled: bool) { } pub(crate) fn reset_ap_state() { - AP_STATE.store(WifiState::Invalid, Ordering::Relaxed) + AP_STATE.store(WifiApState::Invalid, Ordering::Relaxed) } pub(crate) fn reset_sta_state() { - STA_STATE.store(WifiState::Invalid, Ordering::Relaxed) -} - -/// Returns the current state of the Wi-Fi stack. -/// -/// This does not support AP-STA mode. Use one of `sta_state` or -/// `ap_state` instead. -#[instability::unstable] -pub fn wifi_state() -> WifiState { - use super::WifiMode; - match WifiMode::current() { - Ok(WifiMode::Sta) => sta_state(), - Ok(WifiMode::Ap) => ap_state(), - _ => WifiState::Invalid, - } + STA_STATE.store(WifiStaState::Invalid, Ordering::Relaxed) } diff --git a/examples/wifi/embassy_access_point/src/main.rs b/examples/wifi/embassy_access_point/src/main.rs index 24005f3f3..ce3343201 100644 --- a/examples/wifi/embassy_access_point/src/main.rs +++ b/examples/wifi/embassy_access_point/src/main.rs @@ -32,7 +32,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup}; use esp_println::{print, println}; use esp_radio::{ Controller, - wifi::{AccessPointConfig, Config, WifiController, WifiDevice, WifiEvent, WifiState}, + wifi::{AccessPointConfig, Config, WifiApState, WifiController, WifiDevice, WifiEvent}, }; esp_bootloader_esp_idf::esp_app_desc!(); @@ -240,8 +240,8 @@ async fn connection(mut controller: WifiController<'static>) { println!("start connection task"); println!("Device capabilities: {:?}", controller.capabilities()); loop { - match esp_radio::wifi::wifi_state() { - WifiState::ApStarted => { + match esp_radio::wifi::ap_state() { + WifiApState::Started => { // wait until we're no longer connected controller.wait_for_event(WifiEvent::ApStop).await; Timer::after(Duration::from_millis(5000)).await diff --git a/examples/wifi/embassy_access_point_with_sta/src/main.rs b/examples/wifi/embassy_access_point_with_sta/src/main.rs index 6f9d4338a..6b244ca3b 100644 --- a/examples/wifi/embassy_access_point_with_sta/src/main.rs +++ b/examples/wifi/embassy_access_point_with_sta/src/main.rs @@ -42,10 +42,10 @@ use esp_radio::{ AccessPointConfig, ClientConfig, Config, + WifiApState, WifiController, WifiDevice, WifiEvent, - WifiState, }, }; @@ -328,7 +328,7 @@ async fn connection(mut controller: WifiController<'static>) { loop { match esp_radio::wifi::ap_state() { - WifiState::ApStarted => { + WifiApState::Started => { println!("About to connect..."); match controller.connect_async().await { diff --git a/examples/wifi/embassy_dhcp/src/main.rs b/examples/wifi/embassy_dhcp/src/main.rs index 478dda988..a0f3b82ab 100644 --- a/examples/wifi/embassy_dhcp/src/main.rs +++ b/examples/wifi/embassy_dhcp/src/main.rs @@ -22,7 +22,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_radio::{ Controller, - wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiState}, + wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiStaState}, }; esp_bootloader_esp_idf::esp_app_desc!(); @@ -150,8 +150,8 @@ async fn connection(mut controller: WifiController<'static>) { println!("start connection task"); println!("Device capabilities: {:?}", controller.capabilities()); loop { - match esp_radio::wifi::wifi_state() { - WifiState::StaConnected => { + match esp_radio::wifi::sta_state() { + WifiStaState::Connected => { // wait until we're no longer connected controller.wait_for_event(WifiEvent::StaDisconnected).await; Timer::after(Duration::from_millis(5000)).await diff --git a/examples/wifi/sntp/src/main.rs b/examples/wifi/sntp/src/main.rs index b94fc370f..244f445a5 100644 --- a/examples/wifi/sntp/src/main.rs +++ b/examples/wifi/sntp/src/main.rs @@ -26,7 +26,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, rtc_cntl::Rtc, timer::timg::TimerGroup} use esp_println::println; use esp_radio::{ Controller, - wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiState}, + wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiStaState}, }; use log::{error, info}; use sntpc::{NtpContext, NtpTimestampGenerator, get_time}; @@ -209,7 +209,7 @@ async fn connection(mut controller: WifiController<'static>) { println!("start connection task"); println!("Device capabilities: {:?}", controller.capabilities()); loop { - if esp_radio::wifi::wifi_state() == WifiState::StaConnected { + if esp_radio::wifi::sta_state() == WifiStaState::Connected { // wait until we're no longer connected controller.wait_for_event(WifiEvent::StaDisconnected).await; Timer::after(Duration::from_millis(5000)).await diff --git a/qa-test/src/bin/embassy_wifi_bench.rs b/qa-test/src/bin/embassy_wifi_bench.rs index c06039170..d29f2b987 100644 --- a/qa-test/src/bin/embassy_wifi_bench.rs +++ b/qa-test/src/bin/embassy_wifi_bench.rs @@ -32,7 +32,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup}; use esp_println::println; use esp_radio::{ Controller, - wifi::{ClientConfig, Config, WifiController, WifiDevice, WifiEvent, WifiState}, + wifi::{ClientConfig, Config, WifiController, WifiDevice, WifiEvent, WifiStaState}, }; esp_bootloader_esp_idf::esp_app_desc!(); @@ -152,8 +152,8 @@ async fn connection(mut controller: WifiController<'static>) { println!("start connection task"); println!("Device capabilities: {:?}", controller.capabilities()); loop { - match esp_radio::wifi::wifi_state() { - WifiState::StaConnected => { + match esp_radio::wifi::sta_state() { + WifiStaState::Connected => { // wait until we're no longer connected controller.wait_for_event(WifiEvent::StaDisconnected).await; Timer::after(Duration::from_millis(5000)).await