Remove wifi_state and split WifiState into WifiStaState and WifiApState (#4046)

* Remove wifi_state adn split WifiState into WifiStaState and WifiApState

* MG

* changelog

* review
This commit is contained in:
Juraj Sadel 2025-09-04 13:28:00 +02:00 committed by GitHub
parent 227ad1fced
commit 34b908dc56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 76 additions and 63 deletions

View File

@ -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) - `ap_mac` and `sta_mac` returns `[u8; 6]` instead of taking an `[u8; 6]` argument (#4017)
- `RxControlInfo` hidden behind `esp-now` feature (#4017) - `RxControlInfo` hidden behind `esp-now` feature (#4017)
- `set_configuration()` to `set_config() (#4017) - `set_configuration()` to `set_config() (#4017)
- `WifiState` split into `WifiStaState` and `WifiApState` (#4046)
### Fixed ### 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) - `scan_with_config_sync_max`, `scan_with_config_sync_max`, `scan_n`, and `scan_n_async` functions (#3963)
- `EnumSetType` from `Protocol`, `Country` enums (#4017) - `EnumSetType` from `Protocol`, `Country` enums (#4017)
- `AtomicWifiState` and `WifiDeviceMode` are not available anymore (#4029) - `AtomicWifiState` and `WifiDeviceMode` are not available anymore (#4029)
- `wifi_state()` and `WifiState` are not available anymore (#4046)
## [v0.15.0] - 2025-07-16 ## [v0.15.0] - 2025-07-16

View File

@ -87,4 +87,13 @@ Same for `set_configuration()` to `set_config()`:
- config - config
- }); - });
+ let ap_config = Config::AccessPoint(AccessPointConfig::default().with_ssid("esp-radio".into())); + 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 { ... }
``` ```

View File

@ -1884,14 +1884,14 @@ impl WifiDeviceMode {
fn link_state(&self) -> embassy_net_driver::LinkState { fn link_state(&self) -> embassy_net_driver::LinkState {
match self { match self {
WifiDeviceMode::Sta => { WifiDeviceMode::Sta => {
if matches!(sta_state(), WifiState::StaConnected) { if matches!(sta_state(), WifiStaState::Connected) {
embassy_net_driver::LinkState::Up embassy_net_driver::LinkState::Up
} else { } else {
embassy_net_driver::LinkState::Down embassy_net_driver::LinkState::Down
} }
} }
WifiDeviceMode::Ap => { WifiDeviceMode::Ap => {
if matches!(ap_state(), WifiState::ApStarted) { if matches!(ap_state(), WifiApState::Started) {
embassy_net_driver::LinkState::Up embassy_net_driver::LinkState::Up
} else { } else {
embassy_net_driver::LinkState::Down embassy_net_driver::LinkState::Down
@ -3027,18 +3027,18 @@ impl WifiController<'_> {
esp_wifi_result!(unsafe { esp_wifi_disconnect() }) 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 /// This function should be called after the `start` method to verify if the
/// Wi-Fi has started successfully. /// Wi-Fi has started successfully.
pub fn is_started(&self) -> Result<bool, WifiError> { pub fn is_started(&self) -> Result<bool, WifiError> {
if matches!( if matches!(
crate::wifi::sta_state(), crate::wifi::sta_state(),
WifiState::StaStarted | WifiState::StaConnected | WifiState::StaDisconnected WifiStaState::Started | WifiStaState::Connected | WifiStaState::Disconnected
) { ) {
return Ok(true); return Ok(true);
} }
if matches!(crate::wifi::ap_state(), WifiState::ApStarted) { if matches!(crate::wifi::ap_state(), WifiApState::Started) {
return Ok(true); return Ok(true);
} }
Ok(false) Ok(false)
@ -3050,8 +3050,8 @@ impl WifiController<'_> {
/// the connection was successful. /// the connection was successful.
pub fn is_connected(&self) -> Result<bool, WifiError> { pub fn is_connected(&self) -> Result<bool, WifiError> {
match crate::wifi::sta_state() { match crate::wifi::sta_state() {
crate::wifi::WifiState::StaConnected => Ok(true), crate::wifi::WifiStaState::Connected => Ok(true),
crate::wifi::WifiState::StaDisconnected => Err(WifiError::Disconnected), crate::wifi::WifiStaState::Disconnected => Err(WifiError::Disconnected),
// FIXME: Should any other enum value trigger an error instead of returning false? // FIXME: Should any other enum value trigger an error instead of returning false?
_ => Ok(false), _ => Ok(false),
} }

View File

@ -1,62 +1,78 @@
use core::sync::atomic::Ordering; use core::sync::atomic::Ordering;
use private::AtomicWifiState; use private::{AtomicWifiApState, AtomicWifiStaState};
pub use private::WifiState; pub use private::{WifiApState, WifiStaState};
use super::WifiEvent; use super::WifiEvent;
mod private { mod private {
use portable_atomic_enum::atomic_enum; use portable_atomic_enum::atomic_enum;
/// Wi-Fi interface state. /// Wi-Fi interface for station state.
#[atomic_enum] #[atomic_enum]
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive] #[non_exhaustive]
pub enum WifiState { pub enum WifiStaState {
/// Station started. /// Station started.
StaStarted, Started,
/// Station connected. /// Station connected.
StaConnected, Connected,
/// Station disconnected. /// Station disconnected.
StaDisconnected, Disconnected,
/// Station stopped /// 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. /// Access point started.
ApStarted, Started,
/// Access point stopped. /// Access point stopped.
ApStopped, Stopped,
/// Invalid state.
/// Invalid Wi-Fi state.
Invalid, Invalid,
} }
} }
impl From<WifiEvent> for WifiState { impl From<WifiEvent> for WifiStaState {
fn from(event: WifiEvent) -> WifiState { fn from(event: WifiEvent) -> WifiStaState {
match event { match event {
WifiEvent::StaStart => WifiState::StaStarted, WifiEvent::StaStart => WifiStaState::Started,
WifiEvent::StaConnected => WifiState::StaConnected, WifiEvent::StaConnected => WifiStaState::Connected,
WifiEvent::StaDisconnected => WifiState::StaDisconnected, WifiEvent::StaDisconnected => WifiStaState::Disconnected,
WifiEvent::StaStop => WifiState::StaStopped, WifiEvent::StaStop => WifiStaState::Stopped,
WifiEvent::ApStart => WifiState::ApStarted, _ => WifiStaState::Invalid,
WifiEvent::ApStop => WifiState::ApStopped,
_ => WifiState::Invalid,
} }
} }
} }
pub(crate) static STA_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); impl From<WifiEvent> for WifiApState {
pub(crate) static AP_STATE: AtomicWifiState = AtomicWifiState::new(WifiState::Invalid); 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. /// Get the current state of the AP.
pub fn ap_state() -> WifiState { pub fn ap_state() -> WifiApState {
AP_STATE.load(Ordering::Relaxed) AP_STATE.load(Ordering::Relaxed)
} }
/// Get the current state of the STA. /// Get the current state of the STA.
pub fn sta_state() -> WifiState { pub fn sta_state() -> WifiStaState {
STA_STATE.load(Ordering::Relaxed) STA_STATE.load(Ordering::Relaxed)
} }
@ -65,10 +81,10 @@ pub(crate) fn update_state(event: WifiEvent, handled: bool) {
WifiEvent::StaConnected WifiEvent::StaConnected
| WifiEvent::StaDisconnected | WifiEvent::StaDisconnected
| WifiEvent::StaStart | 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 => { WifiEvent::ApStart | WifiEvent::ApStop => {
AP_STATE.store(WifiState::from(event), Ordering::Relaxed) AP_STATE.store(WifiApState::from(event), Ordering::Relaxed)
} }
other => { other => {
@ -80,23 +96,9 @@ pub(crate) fn update_state(event: WifiEvent, handled: bool) {
} }
pub(crate) fn reset_ap_state() { 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() { pub(crate) fn reset_sta_state() {
STA_STATE.store(WifiState::Invalid, Ordering::Relaxed) STA_STATE.store(WifiStaState::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,
}
} }

View File

@ -32,7 +32,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup};
use esp_println::{print, println}; use esp_println::{print, println};
use esp_radio::{ use esp_radio::{
Controller, Controller,
wifi::{AccessPointConfig, Config, WifiController, WifiDevice, WifiEvent, WifiState}, wifi::{AccessPointConfig, Config, WifiApState, WifiController, WifiDevice, WifiEvent},
}; };
esp_bootloader_esp_idf::esp_app_desc!(); esp_bootloader_esp_idf::esp_app_desc!();
@ -240,8 +240,8 @@ async fn connection(mut controller: WifiController<'static>) {
println!("start connection task"); println!("start connection task");
println!("Device capabilities: {:?}", controller.capabilities()); println!("Device capabilities: {:?}", controller.capabilities());
loop { loop {
match esp_radio::wifi::wifi_state() { match esp_radio::wifi::ap_state() {
WifiState::ApStarted => { WifiApState::Started => {
// wait until we're no longer connected // wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await; controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await Timer::after(Duration::from_millis(5000)).await

View File

@ -42,10 +42,10 @@ use esp_radio::{
AccessPointConfig, AccessPointConfig,
ClientConfig, ClientConfig,
Config, Config,
WifiApState,
WifiController, WifiController,
WifiDevice, WifiDevice,
WifiEvent, WifiEvent,
WifiState,
}, },
}; };
@ -328,7 +328,7 @@ async fn connection(mut controller: WifiController<'static>) {
loop { loop {
match esp_radio::wifi::ap_state() { match esp_radio::wifi::ap_state() {
WifiState::ApStarted => { WifiApState::Started => {
println!("About to connect..."); println!("About to connect...");
match controller.connect_async().await { match controller.connect_async().await {

View File

@ -22,7 +22,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup};
use esp_println::println; use esp_println::println;
use esp_radio::{ use esp_radio::{
Controller, Controller,
wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiState}, wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiStaState},
}; };
esp_bootloader_esp_idf::esp_app_desc!(); esp_bootloader_esp_idf::esp_app_desc!();
@ -150,8 +150,8 @@ async fn connection(mut controller: WifiController<'static>) {
println!("start connection task"); println!("start connection task");
println!("Device capabilities: {:?}", controller.capabilities()); println!("Device capabilities: {:?}", controller.capabilities());
loop { loop {
match esp_radio::wifi::wifi_state() { match esp_radio::wifi::sta_state() {
WifiState::StaConnected => { WifiStaState::Connected => {
// wait until we're no longer connected // wait until we're no longer connected
controller.wait_for_event(WifiEvent::StaDisconnected).await; controller.wait_for_event(WifiEvent::StaDisconnected).await;
Timer::after(Duration::from_millis(5000)).await Timer::after(Duration::from_millis(5000)).await

View File

@ -26,7 +26,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, rtc_cntl::Rtc, timer::timg::TimerGroup}
use esp_println::println; use esp_println::println;
use esp_radio::{ use esp_radio::{
Controller, Controller,
wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiState}, wifi::{ClientConfig, Config, ScanConfig, WifiController, WifiDevice, WifiEvent, WifiStaState},
}; };
use log::{error, info}; use log::{error, info};
use sntpc::{NtpContext, NtpTimestampGenerator, get_time}; use sntpc::{NtpContext, NtpTimestampGenerator, get_time};
@ -209,7 +209,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("start connection task"); println!("start connection task");
println!("Device capabilities: {:?}", controller.capabilities()); println!("Device capabilities: {:?}", controller.capabilities());
loop { loop {
if esp_radio::wifi::wifi_state() == WifiState::StaConnected { if esp_radio::wifi::sta_state() == WifiStaState::Connected {
// wait until we're no longer connected // wait until we're no longer connected
controller.wait_for_event(WifiEvent::StaDisconnected).await; controller.wait_for_event(WifiEvent::StaDisconnected).await;
Timer::after(Duration::from_millis(5000)).await Timer::after(Duration::from_millis(5000)).await

View File

@ -32,7 +32,7 @@ use esp_hal::{clock::CpuClock, rng::Rng, timer::timg::TimerGroup};
use esp_println::println; use esp_println::println;
use esp_radio::{ use esp_radio::{
Controller, Controller,
wifi::{ClientConfig, Config, WifiController, WifiDevice, WifiEvent, WifiState}, wifi::{ClientConfig, Config, WifiController, WifiDevice, WifiEvent, WifiStaState},
}; };
esp_bootloader_esp_idf::esp_app_desc!(); esp_bootloader_esp_idf::esp_app_desc!();
@ -152,8 +152,8 @@ async fn connection(mut controller: WifiController<'static>) {
println!("start connection task"); println!("start connection task");
println!("Device capabilities: {:?}", controller.capabilities()); println!("Device capabilities: {:?}", controller.capabilities());
loop { loop {
match esp_radio::wifi::wifi_state() { match esp_radio::wifi::sta_state() {
WifiState::StaConnected => { WifiStaState::Connected => {
// wait until we're no longer connected // wait until we're no longer connected
controller.wait_for_event(WifiEvent::StaDisconnected).await; controller.wait_for_event(WifiEvent::StaDisconnected).await;
Timer::after(Duration::from_millis(5000)).await Timer::after(Duration::from_millis(5000)).await