11use core:: sync:: atomic:: Ordering ;
22
3- use private:: { AtomicWifiAccessPointState , AtomicWifiStationState } ;
4- pub ( crate ) use private:: { WifiAccessPointState , WifiStationState } ;
3+ use esp_radio_rtos_driver:: semaphore:: { SemaphoreHandle , SemaphoreKind } ;
4+ use esp_sync:: NonReentrantMutex ;
5+ use portable_atomic_enum:: atomic_enum;
56
67use super :: WifiEvent ;
78
8- mod private {
9- use portable_atomic_enum:: atomic_enum;
10-
11- /// Wi-Fi interface for station state.
12- #[ atomic_enum]
13- #[ derive( PartialEq , Debug , Clone , Copy , Hash ) ]
14- #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
15- #[ non_exhaustive]
16- pub enum WifiStationState {
17- /// Start initiated.
18- Starting ,
19- /// Station started.
20- Started ,
21- /// Connect initiated.
22- Connecting ,
23- /// Station connected.
24- Connected ,
25- /// Disconnect initiated.
26- Disconnecting ,
27- /// Station disconnected.
28- Disconnected ,
29- /// Stop initiated.
30- Stopping ,
31- /// Station stopped
32- Stopped ,
33- /// Uninitialized state.
34- Uninitialized ,
35- }
9+ /// Wi-Fi interface for station state.
10+ #[ atomic_enum]
11+ #[ derive( PartialEq , Debug , Clone , Copy , Hash ) ]
12+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
13+ #[ non_exhaustive]
14+ pub ( crate ) enum WifiStationState {
15+ /// Start initiated.
16+ Starting ,
17+ /// Station started.
18+ Started ,
19+ /// Connect initiated.
20+ Connecting ,
21+ /// Station connected.
22+ Connected ,
23+ /// Disconnect initiated.
24+ Disconnecting ,
25+ /// Station disconnected.
26+ Disconnected ,
27+ /// Stop initiated.
28+ Stopping ,
29+ /// Station stopped
30+ Stopped ,
31+ /// Uninitialized state.
32+ Uninitialized ,
33+ }
3634
37- /// Wi-Fi interface for access point state.
38- #[ atomic_enum]
39- #[ derive( PartialEq , Debug , Clone , Copy , Hash ) ]
40- #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
41- #[ non_exhaustive]
42- pub enum WifiAccessPointState {
43- /// Start initiated.
44- Starting ,
45- /// Access point started.
46- Started ,
47- /// Stop initiated.
48- Stopping ,
49- /// Access point stopped.
50- Stopped ,
51- /// Uninitialized state.
52- Uninitialized ,
53- }
35+ /// Wi-Fi interface for access point state.
36+ #[ atomic_enum]
37+ #[ derive( PartialEq , Debug , Clone , Copy , Hash ) ]
38+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
39+ #[ non_exhaustive]
40+ pub ( crate ) enum WifiAccessPointState {
41+ /// Start initiated.
42+ Starting ,
43+ /// Access point started.
44+ Started ,
45+ /// Stop initiated.
46+ Stopping ,
47+ /// Access point stopped.
48+ Stopped ,
49+ /// Uninitialized state.
50+ Uninitialized ,
5451}
5552
5653impl From < WifiEvent > for WifiStationState {
@@ -75,9 +72,9 @@ impl From<WifiEvent> for WifiAccessPointState {
7572 }
7673}
7774
78- pub ( crate ) static STATION_STATE : AtomicWifiStationState =
75+ static STATION_STATE : AtomicWifiStationState =
7976 AtomicWifiStationState :: new ( WifiStationState :: Uninitialized ) ;
80- pub ( crate ) static ACCESS_POINT_STATE : AtomicWifiAccessPointState =
77+ static ACCESS_POINT_STATE : AtomicWifiAccessPointState =
8178 AtomicWifiAccessPointState :: new ( WifiAccessPointState :: Uninitialized ) ;
8279
8380/// Get the current state of the access point.
@@ -118,3 +115,21 @@ pub(crate) fn set_access_point_state(state: WifiAccessPointState) {
118115pub ( crate ) fn set_station_state ( state : WifiStationState ) {
119116 STATION_STATE . store ( state, Ordering :: Relaxed )
120117}
118+
119+ pub ( crate ) fn locked < R > ( f : impl FnOnce ( ) -> R ) -> R {
120+ static LOCK : NonReentrantMutex < Option < SemaphoreHandle > > = NonReentrantMutex :: new ( None ) ;
121+
122+ LOCK . with ( |sem| {
123+ sem. get_or_insert_with ( || SemaphoreHandle :: new ( SemaphoreKind :: Mutex ) )
124+ . take ( None )
125+ } ) ;
126+
127+ let res: R = f ( ) ;
128+
129+ LOCK . with ( |sem| {
130+ sem. get_or_insert_with ( || SemaphoreHandle :: new ( SemaphoreKind :: Mutex ) )
131+ . give ( )
132+ } ) ;
133+
134+ res
135+ }
0 commit comments