Skip to content

Commit

Permalink
Split wifi state for AP and STA
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 18, 2023
1 parent d0a448b commit 4b2aa1c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 60 deletions.
34 changes: 17 additions & 17 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,26 +1238,28 @@ impl Wifi for WifiController<'_> {
}

fn connect(&mut self) -> Result<(), Self::Error> {
esp_wifi_result!(unsafe {
WIFI_STATE = -1;
esp_wifi_connect()
})
esp_wifi_result!(unsafe { esp_wifi_connect() })
}

fn disconnect(&mut self) -> Result<(), Self::Error> {
esp_wifi_result!(unsafe { esp_wifi_disconnect() })
}

fn is_started(&self) -> Result<bool, Self::Error> {
match crate::wifi::get_wifi_state() {
crate::wifi::WifiState::Invalid => Ok(false),
// We assume that wifi has been started in every other states
_ => Ok(true),
if matches!(
crate::wifi::get_sta_state(),
WifiState::StaStarted | WifiState::StaConnected | WifiState::StaDisconnected
) {
return Ok(true);
}
if matches!(crate::wifi::get_ap_state(), WifiState::ApStarted) {
return Ok(true);
}
Ok(false)
}

fn is_connected(&self) -> Result<bool, Self::Error> {
match crate::wifi::get_wifi_state() {
match crate::wifi::get_sta_state() {
crate::wifi::WifiState::StaConnected => Ok(true),
crate::wifi::WifiState::StaDisconnected => Err(WifiError::Disconnected),
//FIXME: Should any other enum value trigger an error instead of returning false?
Expand Down Expand Up @@ -1355,19 +1357,14 @@ pub(crate) mod embassy {

match self.get_wifi_mode() {
Ok(WifiMode::Sta) => {
if matches!(get_wifi_state(), WifiState::StaConnected) {
if matches!(get_sta_state(), WifiState::StaConnected) {
embassy_net_driver::LinkState::Up
} else {
embassy_net_driver::LinkState::Down
}
}
Ok(WifiMode::Ap) => {
if matches!(
get_wifi_state(),
WifiState::ApStart
| WifiState::ApStaConnected
| WifiState::ApStaDisconnected
) {
if matches!(get_ap_state(), WifiState::ApStarted) {
embassy_net_driver::LinkState::Up
} else {
embassy_net_driver::LinkState::Down
Expand Down Expand Up @@ -1463,7 +1460,10 @@ mod asynch {
embedded_svc::wifi::Wifi::stop(self)?;
WifiEventFuture::new(event).await;

unsafe { WIFI_STATE = -1 };
unsafe {
AP_STATE = WifiState::Invalid;
STA_STATE = WifiState::Invalid;
}

Ok(())
}
Expand Down
76 changes: 39 additions & 37 deletions esp-wifi/src/wifi/os_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,56 @@ use crate::compat::common::syslog;

use super::WifiEvent;

pub(crate) static mut WIFI_STATE: i32 = -1;
pub(crate) static mut STA_STATE: WifiState = WifiState::Invalid;
pub(crate) static mut AP_STATE: WifiState = WifiState::Invalid;

// useful for waiting for events - clear and wait for the event bit to be set again
pub(crate) static WIFI_EVENTS: Mutex<RefCell<EnumSet<WifiEvent>>> =
Mutex::new(RefCell::new(enumset::enum_set!()));

pub fn is_connected() -> bool {
unsafe { WIFI_STATE == wifi_event_t_WIFI_EVENT_STA_CONNECTED as i32 }
unsafe { STA_STATE == WifiState::StaConnected }
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WifiState {
WifiReady,
StaStart,
StaStop,
StaStarted,
StaConnected,
StaDisconnected,
ApStart,
ApStop,
ApStaConnected,
ApStaDisconnected,
StaStopped,

ApStarted,
ApStopped,

Invalid,
}

#[allow(non_upper_case_globals)]
impl From<WifiEvent> for WifiState {
fn from(event: WifiEvent) -> WifiState {
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,
}
}
}

pub fn get_ap_state() -> WifiState {
unsafe { AP_STATE }
}

pub fn get_sta_state() -> WifiState {
unsafe { STA_STATE }
}

pub fn get_wifi_state() -> WifiState {
match unsafe { WIFI_STATE as u32 } {
wifi_event_t_WIFI_EVENT_WIFI_READY => WifiState::WifiReady,
wifi_event_t_WIFI_EVENT_STA_START => WifiState::StaStart,
wifi_event_t_WIFI_EVENT_STA_STOP => WifiState::StaStop,
wifi_event_t_WIFI_EVENT_STA_CONNECTED => WifiState::StaConnected,
wifi_event_t_WIFI_EVENT_STA_DISCONNECTED => WifiState::StaDisconnected,
wifi_event_t_WIFI_EVENT_AP_START => WifiState::ApStart,
wifi_event_t_WIFI_EVENT_AP_STOP => WifiState::ApStop,
wifi_event_t_WIFI_EVENT_AP_STACONNECTED => WifiState::ApStaConnected,
wifi_event_t_WIFI_EVENT_AP_STADISCONNECTED => WifiState::ApStaDisconnected,
match super::get_wifi_mode() {
Ok(super::WifiMode::Sta) => get_sta_state(),
Ok(super::WifiMode::Ap) => get_ap_state(),
_ => WifiState::Invalid,
}
}
Expand Down Expand Up @@ -921,25 +935,13 @@ pub unsafe extern "C" fn event_post(
trace!("EVENT: {:?}", event);
critical_section::with(|cs| WIFI_EVENTS.borrow_ref_mut(cs).insert(event));

let take_state = match event {
match event {
WifiEvent::StaConnected
| WifiEvent::StaDisconnected
| WifiEvent::StaStart
| WifiEvent::StaStop
| WifiEvent::WifiReady
| WifiEvent::ScanDone
| WifiEvent::ApStart
| WifiEvent::ApStop
| WifiEvent::ApStaconnected
| WifiEvent::ApStadisconnected => true,
other => {
info!("Unhandled event: {:?}", other);
false
}
};

if take_state {
WIFI_STATE = event_id;
| WifiEvent::StaStop => STA_STATE = WifiState::from(event),
WifiEvent::ApStart | WifiEvent::ApStop => AP_STATE = WifiState::from(event),
other => debug!("Unhandled event: {:?}", other),
}

#[cfg(feature = "async")]
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c2/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c3/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32c6/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32s2/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down
2 changes: 1 addition & 1 deletion examples-esp32s3/examples/embassy_access_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn connection(mut controller: WifiController<'static>) {
println!("Device capabilities: {:?}", controller.get_capabilities());
loop {
match esp_wifi::wifi::get_wifi_state() {
WifiState::ApStart => {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
Expand Down

0 comments on commit 4b2aa1c

Please sign in to comment.