Skip to content

Commit d56cc1f

Browse files
committed
Make *_state() private
1 parent 497d712 commit d56cc1f

File tree

8 files changed

+71
-85
lines changed

8 files changed

+71
-85
lines changed

esp-radio/src/wifi/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ pub(crate) use self::os_adapter::*;
2222
use self::sniffer::Sniffer;
2323
#[cfg(feature = "wifi-eap")]
2424
use self::sta::eap::EapStationConfig;
25-
pub use self::state::*;
2625
use self::{
2726
ap::{AccessPointConfig, AccessPointInfo, convert_ap_info},
2827
private::PacketBuffer,
2928
scan::{FreeApListOnDrop, ScanConfig, ScanResults, ScanTypeConfig},
3029
sta::StationConfig,
30+
state::*,
3131
};
3232
#[cfg(all(feature = "csi", feature = "unstable"))]
3333
#[instability::unstable]
@@ -2119,6 +2119,9 @@ impl WifiController<'_> {
21192119
/// This method is not blocking. To check if the controller has started, use the
21202120
/// [`Self::is_started`] method.
21212121
pub fn start(&mut self) -> Result<(), WifiError> {
2122+
set_access_point_state(WifiAccessPointState::Starting);
2123+
set_station_state(WifiStationState::Starting);
2124+
21222125
unsafe {
21232126
esp_wifi_result!(esp_wifi_start())?;
21242127

@@ -2292,15 +2295,22 @@ impl WifiController<'_> {
22922295
}
22932296

22942297
fn stop_impl(&mut self) -> Result<(), WifiError> {
2298+
set_access_point_state(WifiAccessPointState::Stopping);
2299+
set_station_state(WifiStationState::Stopping);
2300+
22952301
esp_wifi_result!(unsafe { esp_wifi_stop() })
22962302
}
22972303

22982304
fn connect_impl(&mut self) -> Result<(), WifiError> {
2305+
set_station_state(WifiStationState::Connecting);
2306+
22992307
// TODO: implement ROAMING
23002308
esp_wifi_result!(unsafe { esp_wifi_connect_internal() })
23012309
}
23022310

23032311
fn disconnect_impl(&mut self) -> Result<(), WifiError> {
2312+
set_station_state(WifiStationState::Disconnecting);
2313+
23042314
// TODO: implement ROAMING
23052315
esp_wifi_result!(unsafe { esp_wifi_disconnect_internal() })
23062316
}
@@ -2413,9 +2423,6 @@ impl WifiController<'_> {
24132423

24142424
self.wait_for_all_events(events, false).await;
24152425

2416-
reset_access_point_state();
2417-
reset_station_state();
2418-
24192426
Ok(())
24202427
}
24212428

esp-radio/src/wifi/state.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::sync::atomic::Ordering;
22

33
use private::{AtomicWifiAccessPointState, AtomicWifiStationState};
4-
pub use private::{WifiAccessPointState, WifiStationState};
4+
pub(crate) use private::{WifiAccessPointState, WifiStationState};
55

66
use super::WifiEvent;
77

@@ -14,12 +14,20 @@ mod private {
1414
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1515
#[non_exhaustive]
1616
pub enum WifiStationState {
17+
/// Start initiated.
18+
Starting,
1719
/// Station started.
1820
Started,
21+
/// Connect initiated.
22+
Connecting,
1923
/// Station connected.
2024
Connected,
25+
/// Disconnect initiated.
26+
Disconnecting,
2127
/// Station disconnected.
2228
Disconnected,
29+
/// Stop initiated.
30+
Stopping,
2331
/// Station stopped
2432
Stopped,
2533
/// Invalid state.
@@ -32,8 +40,12 @@ mod private {
3240
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3341
#[non_exhaustive]
3442
pub enum WifiAccessPointState {
43+
/// Start initiated.
44+
Starting,
3545
/// Access point started.
3646
Started,
47+
/// Stop initiated.
48+
Stopping,
3749
/// Access point stopped.
3850
Stopped,
3951
/// Invalid state.
@@ -69,12 +81,12 @@ pub(crate) static ACCESS_POINT_STATE: AtomicWifiAccessPointState =
6981
AtomicWifiAccessPointState::new(WifiAccessPointState::Invalid);
7082

7183
/// Get the current state of the access point.
72-
pub fn access_point_state() -> WifiAccessPointState {
84+
pub(crate) fn access_point_state() -> WifiAccessPointState {
7385
ACCESS_POINT_STATE.load(Ordering::Relaxed)
7486
}
7587

7688
/// Get the current state of the Station.
77-
pub fn station_state() -> WifiStationState {
89+
pub(crate) fn station_state() -> WifiStationState {
7890
STATION_STATE.load(Ordering::Relaxed)
7991
}
8092

@@ -99,10 +111,10 @@ pub(crate) fn update_state(event: WifiEvent, handled: bool) {
99111
}
100112
}
101113

102-
pub(crate) fn reset_access_point_state() {
103-
ACCESS_POINT_STATE.store(WifiAccessPointState::Invalid, Ordering::Relaxed)
114+
pub(crate) fn set_access_point_state(state: WifiAccessPointState) {
115+
ACCESS_POINT_STATE.store(state, Ordering::Relaxed)
104116
}
105117

106-
pub(crate) fn reset_station_state() {
107-
STATION_STATE.store(WifiStationState::Invalid, Ordering::Relaxed)
118+
pub(crate) fn set_station_state(state: WifiStationState) {
119+
STATION_STATE.store(state, Ordering::Relaxed)
108120
}

examples/wifi/embassy_access_point/src/main.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ use esp_hal::{
3434
timer::timg::TimerGroup,
3535
};
3636
use esp_println::{print, println};
37-
use esp_radio::wifi::{
38-
ModeConfig,
39-
WifiAccessPointState,
40-
WifiController,
41-
WifiDevice,
42-
WifiEvent,
43-
ap::AccessPointConfig,
44-
};
37+
use esp_radio::wifi::{ModeConfig, WifiController, WifiDevice, WifiEvent, ap::AccessPointConfig};
4538

4639
esp_bootloader_esp_idf::esp_app_desc!();
4740

@@ -238,21 +231,17 @@ async fn connection(mut controller: WifiController<'static>) {
238231
println!("start connection task");
239232
println!("Device capabilities: {:?}", controller.capabilities());
240233
loop {
241-
match esp_radio::wifi::access_point_state() {
242-
WifiAccessPointState::Started => {
243-
// wait until we're no longer connected
244-
controller.wait_for_event(WifiEvent::AccessPointStop).await;
245-
Timer::after(Duration::from_millis(5000)).await
246-
}
247-
_ => {}
248-
}
249234
if !matches!(controller.is_started(), Ok(true)) {
250235
let station_config =
251236
ModeConfig::AccessPoint(AccessPointConfig::default().with_ssid("esp-radio".into()));
252237
controller.set_config(&station_config).unwrap();
253238
println!("Starting wifi");
254239
controller.start_async().await.unwrap();
255240
println!("Wifi started!");
241+
} else {
242+
// wait until we're no longer connected
243+
controller.wait_for_event(WifiEvent::AccessPointStop).await;
244+
Timer::after(Duration::from_millis(5000)).await
256245
}
257246
}
258247
}

examples/wifi/embassy_access_point_with_sta/src/main.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use esp_hal::{
4444
use esp_println::{print, println};
4545
use esp_radio::wifi::{
4646
ModeConfig,
47-
WifiAccessPointState,
4847
WifiController,
4948
WifiDevice,
5049
WifiEvent,
@@ -315,25 +314,24 @@ async fn connection(mut controller: WifiController<'static>) {
315314
println!("Wifi started!");
316315

317316
loop {
318-
match esp_radio::wifi::access_point_state() {
319-
WifiAccessPointState::Started => {
320-
println!("About to connect...");
321-
322-
match controller.connect_async().await {
323-
Ok(_) => {
324-
// wait until we're no longer connected
325-
controller
326-
.wait_for_event(WifiEvent::StationDisconnected)
327-
.await;
328-
println!("Station disconnected");
329-
}
330-
Err(e) => {
331-
println!("Failed to connect to wifi: {e:?}");
332-
Timer::after(Duration::from_millis(5000)).await
333-
}
317+
if matches!(controller.is_started(), Ok(true)) {
318+
println!("About to connect...");
319+
320+
match controller.connect_async().await {
321+
Ok(_) => {
322+
// wait until we're no longer connected
323+
controller
324+
.wait_for_event(WifiEvent::StationDisconnected)
325+
.await;
326+
println!("Station disconnected");
327+
}
328+
Err(e) => {
329+
println!("Failed to connect to wifi: {e:?}");
330+
Timer::after(Duration::from_millis(5000)).await
334331
}
335332
}
336-
_ => return,
333+
} else {
334+
return;
337335
}
338336
}
339337
}

examples/wifi/embassy_dhcp/src/main.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use esp_radio::wifi::{
2929
WifiController,
3030
WifiDevice,
3131
WifiEvent,
32-
WifiStationState,
3332
scan::ScanConfig,
3433
sta::StationConfig,
3534
};
@@ -149,16 +148,14 @@ async fn connection(mut controller: WifiController<'static>) {
149148
println!("start connection task");
150149
println!("Device capabilities: {:?}", controller.capabilities());
151150
loop {
152-
match esp_radio::wifi::station_state() {
153-
WifiStationState::Connected => {
154-
// wait until we're no longer connected
155-
controller
156-
.wait_for_event(WifiEvent::StationDisconnected)
157-
.await;
158-
Timer::after(Duration::from_millis(5000)).await
159-
}
160-
_ => {}
151+
if matches!(controller.is_connected(), Ok(true)) {
152+
// wait until we're no longer connected
153+
controller
154+
.wait_for_event(WifiEvent::StationDisconnected)
155+
.await;
156+
Timer::after(Duration::from_millis(5000)).await
161157
}
158+
162159
if !matches!(controller.is_started(), Ok(true)) {
163160
let station_config = ModeConfig::Station(
164161
StationConfig::default()

examples/wifi/embassy_sntp/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use esp_radio::wifi::{
3535
WifiController,
3636
WifiDevice,
3737
WifiEvent,
38-
WifiStationState,
3938
scan::ScanConfig,
4039
sta::StationConfig,
4140
};
@@ -209,13 +208,14 @@ async fn connection(mut controller: WifiController<'static>) {
209208
println!("start connection task");
210209
println!("Device capabilities: {:?}", controller.capabilities());
211210
loop {
212-
if esp_radio::wifi::station_state() == WifiStationState::Connected {
211+
if matches!(controller.is_connected(), Ok(true)) {
213212
// wait until we're no longer connected
214213
controller
215214
.wait_for_event(WifiEvent::StationDisconnected)
216215
.await;
217216
Timer::after(Duration::from_millis(5000)).await
218217
}
218+
219219
if !matches!(controller.is_started(), Ok(true)) {
220220
let station_config = ModeConfig::Station(
221221
StationConfig::default()

qa-test/src/bin/embassy_wifi_bench.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ use esp_hal::{
3333
timer::timg::TimerGroup,
3434
};
3535
use esp_println::println;
36-
use esp_radio::wifi::{
37-
ModeConfig,
38-
WifiController,
39-
WifiDevice,
40-
WifiEvent,
41-
WifiStationState,
42-
sta::StationConfig,
43-
};
36+
use esp_radio::wifi::{ModeConfig, WifiController, WifiDevice, WifiEvent, sta::StationConfig};
4437

4538
esp_bootloader_esp_idf::esp_app_desc!();
4639

@@ -146,16 +139,14 @@ async fn connection(mut controller: WifiController<'static>) {
146139
println!("start connection task");
147140
println!("Device capabilities: {:?}", controller.capabilities());
148141
loop {
149-
match esp_radio::wifi::station_state() {
150-
WifiStationState::Connected => {
151-
// wait until we're no longer connected
152-
controller
153-
.wait_for_event(WifiEvent::StationDisconnected)
154-
.await;
155-
Timer::after(Duration::from_millis(5000)).await
156-
}
157-
_ => {}
142+
if matches!(controller.is_connected(), Ok(true)) {
143+
// wait until we're no longer connected
144+
controller
145+
.wait_for_event(WifiEvent::StationDisconnected)
146+
.await;
147+
Timer::after(Duration::from_millis(5000)).await
158148
}
149+
159150
if !matches!(controller.is_started(), Ok(true)) {
160151
let station_config = ModeConfig::Station(
161152
StationConfig::default()

qa-test/src/bin/embassy_wifi_i2s.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ use esp_hal::{
2222
timer::timg::TimerGroup,
2323
};
2424
use esp_println::println;
25-
use esp_radio::wifi::{
26-
ModeConfig,
27-
WifiController,
28-
WifiDevice,
29-
WifiEvent,
30-
WifiStationState,
31-
sta::StationConfig,
32-
station_state,
33-
};
25+
use esp_radio::wifi::{ModeConfig, WifiController, WifiDevice, WifiEvent, sta::StationConfig};
3426
use static_cell::StaticCell;
3527

3628
esp_bootloader_esp_idf::esp_app_desc!();
@@ -86,8 +78,8 @@ async fn connection_manager(
8678
}
8779

8880
loop {
89-
match station_state() {
90-
WifiStationState::Connected => {
81+
match controller.is_connected() {
82+
Ok(true) => {
9183
controller
9284
.wait_for_event(WifiEvent::StationDisconnected)
9385
.await;

0 commit comments

Comments
 (0)