-
Notifications
You must be signed in to change notification settings - Fork 369
WiFi Event API #4677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
WiFi Event API #4677
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,12 +22,12 @@ pub(crate) use self::os_adapter::*; | |
| use self::sniffer::Sniffer; | ||
| #[cfg(feature = "wifi-eap")] | ||
| use self::sta::eap::EapStationConfig; | ||
| pub use self::state::*; | ||
| use self::{ | ||
| ap::{AccessPointConfig, AccessPointInfo, convert_ap_info}, | ||
| private::PacketBuffer, | ||
| scan::{FreeApListOnDrop, ScanConfig, ScanResults, ScanTypeConfig}, | ||
| sta::StationConfig, | ||
| state::*, | ||
| }; | ||
| #[cfg(all(feature = "csi", feature = "unstable"))] | ||
| #[instability::unstable] | ||
|
|
@@ -47,7 +47,15 @@ use crate::{ | |
| pub mod ap; | ||
| #[cfg(all(feature = "csi", feature = "unstable"))] | ||
| pub mod csi; | ||
|
|
||
| #[cfg(any(doc, feature = "unstable"))] | ||
| #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] | ||
| #[cfg(feature = "unstable")] | ||
| pub mod event; | ||
| #[cfg(not(any(doc, feature = "unstable")))] | ||
| #[allow(dead_code)] | ||
| #[cfg(not(feature = "unstable"))] | ||
| pub(crate) mod event; | ||
| pub mod scan; | ||
| #[cfg(all(feature = "sniffer", feature = "unstable"))] | ||
| pub mod sniffer; | ||
|
|
@@ -653,6 +661,7 @@ impl From<InitializationError> for WifiError { | |
| #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| #[non_exhaustive] | ||
| #[repr(i32)] | ||
| #[instability::unstable] | ||
| pub enum WifiEvent { | ||
| /// Wi-Fi is ready for operation. | ||
| WifiReady = 0, | ||
|
|
@@ -1998,6 +2007,9 @@ impl Drop for WifiController<'_> { | |
| warn!("Failed to cleanly deinit wifi: {:?}", e); | ||
| } | ||
|
|
||
| set_access_point_state(WifiAccessPointState::Uninitialized); | ||
| set_station_state(WifiStationState::Uninitialized); | ||
|
|
||
| esp_hal::rng::TrngSource::decrease_entropy_source_counter(unsafe { | ||
| esp_hal::Internal::conjure() | ||
| }); | ||
|
|
@@ -2119,6 +2131,9 @@ impl WifiController<'_> { | |
| /// This method is not blocking. To check if the controller has started, use the | ||
| /// [`Self::is_started`] method. | ||
| pub fn start(&mut self) -> Result<(), WifiError> { | ||
| set_access_point_state(WifiAccessPointState::Starting); | ||
| set_station_state(WifiStationState::Starting); | ||
|
|
||
| unsafe { | ||
| esp_wifi_result!(esp_wifi_start())?; | ||
|
|
||
|
|
@@ -2292,15 +2307,22 @@ impl WifiController<'_> { | |
| } | ||
|
|
||
| fn stop_impl(&mut self) -> Result<(), WifiError> { | ||
| set_access_point_state(WifiAccessPointState::Stopping); | ||
| set_station_state(WifiStationState::Stopping); | ||
|
|
||
| esp_wifi_result!(unsafe { esp_wifi_stop() }) | ||
| } | ||
|
|
||
| fn connect_impl(&mut self) -> Result<(), WifiError> { | ||
| set_station_state(WifiStationState::Connecting); | ||
|
|
||
| // TODO: implement ROAMING | ||
| esp_wifi_result!(unsafe { esp_wifi_connect_internal() }) | ||
| } | ||
|
|
||
| fn disconnect_impl(&mut self) -> Result<(), WifiError> { | ||
| set_station_state(WifiStationState::Disconnecting); | ||
|
|
||
| // TODO: implement ROAMING | ||
| esp_wifi_result!(unsafe { esp_wifi_disconnect_internal() }) | ||
| } | ||
|
|
@@ -2413,9 +2435,6 @@ impl WifiController<'_> { | |
|
|
||
| self.wait_for_all_events(events, false).await; | ||
|
|
||
| reset_access_point_state(); | ||
| reset_station_state(); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
|
|
@@ -2460,13 +2479,15 @@ impl WifiController<'_> { | |
| } | ||
|
|
||
| /// Wait for one [`WifiEvent`]. | ||
| #[instability::unstable] | ||
| pub async fn wait_for_event(&mut self, event: WifiEvent) { | ||
| Self::clear_events(event); | ||
| WifiEventFuture::new(event).await | ||
| } | ||
|
|
||
| /// Wait for one of multiple [`WifiEvent`]s. Returns the events that | ||
| /// occurred while waiting. | ||
| #[instability::unstable] | ||
| pub async fn wait_for_events( | ||
| &mut self, | ||
| events: EnumSet<WifiEvent>, | ||
|
|
@@ -2479,6 +2500,7 @@ impl WifiController<'_> { | |
| } | ||
|
|
||
| /// Wait for multiple [`WifiEvent`]s. | ||
| #[instability::unstable] | ||
| pub async fn wait_for_all_events( | ||
| &mut self, | ||
| mut events: EnumSet<WifiEvent>, | ||
|
|
@@ -2494,6 +2516,16 @@ impl WifiController<'_> { | |
| } | ||
| } | ||
|
|
||
| /// Wait for the station to disconnect. | ||
| pub async fn wait_for_station_disconnect(&mut self) { | ||
| self.wait_for_event(WifiEvent::StationDisconnected).await; | ||
| } | ||
|
|
||
| /// Wait for the access point to stop. | ||
| pub async fn wait_for_access_point_stopped(&mut self) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this might be for demonstrative purposes, but isn't this something the user will request to close, not wait for close?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I admit I just mechanically translated the usage in our examples ... thanks for bringing it up, I think at least the comment in the examples is questionable and confusing |
||
| self.wait_for_event(WifiEvent::AccessPointStop).await; | ||
| } | ||
|
|
||
| fn apply_ap_config(&mut self, config: &AccessPointConfig) -> Result<(), WifiError> { | ||
| self.ap_beacon_timeout = config.beacon_timeout; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the idea is in the proper impl this will have state checks to ensure we're connected or in a state that emit the disconnection event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes - I was a bit too lazy in the first iteration 👍
but I admit if this is meant to demonstrate my idea I have to show that