esp_radio/wifi/
state.rs

1use core::sync::atomic::Ordering;
2
3use private::{AtomicWifiApState, AtomicWifiStaState};
4pub use private::{WifiApState, WifiStaState};
5
6use super::WifiEvent;
7
8mod private {
9    use portable_atomic_enum::atomic_enum;
10
11    /// Wi-Fi interface for station state.
12    #[atomic_enum]
13    #[derive(PartialEq, Debug)]
14    #[cfg_attr(feature = "defmt", derive(defmt::Format))]
15    #[non_exhaustive]
16    pub enum WifiStaState {
17        /// Station started.
18        Started,
19        /// Station connected.
20        Connected,
21        /// Station disconnected.
22        Disconnected,
23        /// Station stopped
24        Stopped,
25        /// Invalid state.
26        Invalid,
27    }
28
29    /// Wi-Fi interface for access point state.
30    #[atomic_enum]
31    #[derive(PartialEq, Debug)]
32    #[cfg_attr(feature = "defmt", derive(defmt::Format))]
33    #[non_exhaustive]
34    pub enum WifiApState {
35        /// Access point started.
36        Started,
37        /// Access point stopped.
38        Stopped,
39        /// Invalid state.
40        Invalid,
41    }
42}
43
44impl From<WifiEvent> for WifiStaState {
45    fn from(event: WifiEvent) -> WifiStaState {
46        match event {
47            WifiEvent::StaStart => WifiStaState::Started,
48            WifiEvent::StaConnected => WifiStaState::Connected,
49            WifiEvent::StaDisconnected => WifiStaState::Disconnected,
50            WifiEvent::StaStop => WifiStaState::Stopped,
51            _ => WifiStaState::Invalid,
52        }
53    }
54}
55
56impl From<WifiEvent> for WifiApState {
57    fn from(event: WifiEvent) -> WifiApState {
58        match event {
59            WifiEvent::ApStart => WifiApState::Started,
60            WifiEvent::ApStop => WifiApState::Stopped,
61            _ => WifiApState::Invalid,
62        }
63    }
64}
65
66pub(crate) static STA_STATE: AtomicWifiStaState = AtomicWifiStaState::new(WifiStaState::Invalid);
67pub(crate) static AP_STATE: AtomicWifiApState = AtomicWifiApState::new(WifiApState::Invalid);
68
69/// Get the current state of the AP.
70pub fn ap_state() -> WifiApState {
71    AP_STATE.load(Ordering::Relaxed)
72}
73
74/// Get the current state of the STA.
75pub fn sta_state() -> WifiStaState {
76    STA_STATE.load(Ordering::Relaxed)
77}
78
79pub(crate) fn update_state(event: WifiEvent, handled: bool) {
80    match event {
81        WifiEvent::StaConnected
82        | WifiEvent::StaDisconnected
83        | WifiEvent::StaStart
84        | WifiEvent::StaStop => STA_STATE.store(WifiStaState::from(event), Ordering::Relaxed),
85
86        WifiEvent::ApStart | WifiEvent::ApStop => {
87            AP_STATE.store(WifiApState::from(event), Ordering::Relaxed)
88        }
89
90        other => {
91            if !handled {
92                debug!("Unhandled event: {:?}", other)
93            }
94        }
95    }
96}
97
98pub(crate) fn reset_ap_state() {
99    AP_STATE.store(WifiApState::Invalid, Ordering::Relaxed)
100}
101
102pub(crate) fn reset_sta_state() {
103    STA_STATE.store(WifiStaState::Invalid, Ordering::Relaxed)
104}