From 3ebaf6337dc00d8a4fa9a4f02640508c22a0faac Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Sat, 30 Dec 2023 20:07:43 +0000 Subject: [PATCH] #56 --- src/eth.rs | 34 +++++++++++++++++----------------- src/eventloop.rs | 6 +++--- src/netif.rs | 14 +++++++------- src/wifi.rs | 39 +++++++++++++++++++-------------------- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/eth.rs b/src/eth.rs index 98a0b4ae6ef..6bd1f923bf1 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -1199,26 +1199,27 @@ where pub async fn start(&mut self) -> Result<(), EspError> { self.eth.start()?; - self.eth_wait_while(|| self.eth.is_started().map(|s| !s), None) + self.eth_wait_while(|this| this.eth.is_started().map(|s| !s), None) .await } pub async fn stop(&mut self) -> Result<(), EspError> { self.eth.stop()?; - self.eth_wait_while(|| self.eth.is_started(), None).await + self.eth_wait_while(|this| this.eth.is_started(), None) + .await } - pub async fn wait_connected(&self) -> Result<(), EspError> { + pub async fn wait_connected(&mut self) -> Result<(), EspError> { self.eth_wait_while( - || self.eth.is_connected().map(|s| !s), + |this| this.eth.is_connected().map(|s| !s), Some(CONNECT_TIMEOUT), ) .await } - pub async fn eth_wait_while Result>( - &self, - matcher: F, + pub async fn eth_wait_while Result>( + &mut self, + mut matcher: F, timeout: Option, ) -> Result<(), EspError> { use embedded_svc::utils::asyncify::event_bus::AsyncEventBus; @@ -1227,10 +1228,9 @@ where let event_loop = AsyncEventBus::new((), self.event_loop.clone()); let timer_service = AsyncTimerService::new(self.timer_service.clone()); - let mut wait = - crate::eventloop::AsyncWait::::new(&event_loop, &timer_service)?; + let mut wait = crate::eventloop::AsyncWait::::new(event_loop, timer_service)?; - wait.wait_while(matcher, timeout).await + wait.wait_while(|| matcher(self), timeout).await } } @@ -1244,14 +1244,14 @@ where self.eth.is_up() } - pub async fn wait_netif_up(&self) -> Result<(), EspError> { - self.ip_wait_while(|| self.eth.is_up().map(|s| !s), Some(CONNECT_TIMEOUT)) + pub async fn wait_netif_up(&mut self) -> Result<(), EspError> { + self.ip_wait_while(|this| this.eth.is_up().map(|s| !s), Some(CONNECT_TIMEOUT)) .await } - pub async fn ip_wait_while Result>( - &self, - matcher: F, + pub async fn ip_wait_while Result>( + &mut self, + mut matcher: F, timeout: Option, ) -> Result<(), EspError> { use embedded_svc::utils::asyncify::event_bus::AsyncEventBus; @@ -1260,9 +1260,9 @@ where let event_loop = AsyncEventBus::new((), self.event_loop.clone()); let timer_service = AsyncTimerService::new(self.timer_service.clone()); - let mut wait = crate::eventloop::AsyncWait::::new(&event_loop, &timer_service)?; + let mut wait = crate::eventloop::AsyncWait::::new(event_loop, timer_service)?; - wait.wait_while(matcher, timeout).await + wait.wait_while(|| matcher(self), timeout).await } } diff --git a/src/eventloop.rs b/src/eventloop.rs index b0edb27f5f9..bff18c68541 100644 --- a/src/eventloop.rs +++ b/src/eventloop.rs @@ -1100,12 +1100,12 @@ mod async_wait { T: super::EspEventLoopType + 'static, { pub fn new( - event_loop: &AsyncEventBus< + event_loop: AsyncEventBus< (), crate::private::mutex::RawCondvar, super::EspEventLoop, >, - timer_service: &AsyncTimerService, + timer_service: AsyncTimerService, ) -> Result { Ok(Self { subscription: event_loop.subscribe()?, @@ -1161,7 +1161,7 @@ mod async_wait { TT: super::EspEventLoopType, { while matcher()? { - subscription.recv().await?; + subscription.recv_mut().await?; } Ok(()) diff --git a/src/netif.rs b/src/netif.rs index bb7a5d9c510..a63f0ba06d0 100644 --- a/src/netif.rs +++ b/src/netif.rs @@ -750,14 +750,14 @@ where self.netif.is_up() } - pub async fn wait_netif_up(&self) -> Result<(), EspError> { - self.ip_wait_while(|| self.netif.is_up().map(|s| !s), Some(UP_TIMEOUT)) + pub async fn wait_netif_up(&mut self) -> Result<(), EspError> { + self.ip_wait_while(|this| this.netif.is_up().map(|s| !s), Some(UP_TIMEOUT)) .await } - pub async fn ip_wait_while Result>( - &self, - matcher: F, + pub async fn ip_wait_while Result>( + &mut self, + mut matcher: F, timeout: Option, ) -> Result<(), EspError> { use embedded_svc::utils::asyncify::event_bus::AsyncEventBus; @@ -766,9 +766,9 @@ where let event_loop = AsyncEventBus::new((), self.event_loop.clone()); let timer_service = AsyncTimerService::new(self.timer_service.clone()); - let mut wait = crate::eventloop::AsyncWait::::new(&event_loop, &timer_service)?; + let mut wait = crate::eventloop::AsyncWait::::new(event_loop, timer_service)?; - wait.wait_while(matcher, timeout).await + wait.wait_while(|| matcher(self), timeout).await } } diff --git a/src/wifi.rs b/src/wifi.rs index 8676471d959..238153cc148 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -2222,7 +2222,7 @@ where /// the wifi driver has started. pub async fn start(&mut self) -> Result<(), EspError> { self.wifi.start()?; - self.wifi_wait(|| self.wifi.is_started().map(|s| !s), None) + self.wifi_wait(|this| this.wifi.is_started().map(|s| !s), None) .await } @@ -2230,7 +2230,7 @@ where /// the wifi driver has stopped. pub async fn stop(&mut self) -> Result<(), EspError> { self.wifi.stop()?; - self.wifi_wait(|| self.wifi.is_started(), None).await + self.wifi_wait(|this| this.wifi.is_started(), None).await } /// As per [`WifiDriver::connect()`], but as an async call that awaits until @@ -2238,7 +2238,7 @@ where pub async fn connect(&mut self) -> Result<(), EspError> { self.wifi.connect()?; self.wifi_wait( - || self.wifi.is_connected().map(|s| !s), + |this| this.wifi.is_connected().map(|s| !s), Some(CONNECT_TIMEOUT), ) .await @@ -2248,7 +2248,7 @@ where /// the wifi driver has disconnected. pub async fn disconnect(&mut self) -> Result<(), EspError> { self.wifi.disconnect()?; - self.wifi_wait(|| self.wifi.is_connected(), None).await + self.wifi_wait(|this| this.wifi.is_connected(), None).await } /// As per [`WifiDriver::start_scan()`] plus [`WifiDriver::get_scan_result_n()`], @@ -2258,7 +2258,7 @@ where ) -> Result<(heapless::Vec, usize), EspError> { self.wifi.start_scan(&Default::default(), false)?; - self.wifi_wait(|| self.wifi.is_scan_done().map(|s| !s), None) + self.wifi_wait(|this| this.wifi.is_scan_done().map(|s| !s), None) .await?; self.wifi.get_scan_result_n() @@ -2270,7 +2270,7 @@ where pub async fn scan(&mut self) -> Result, EspError> { self.wifi.start_scan(&Default::default(), false)?; - self.wifi_wait(|| self.wifi.is_scan_done().map(|s| !s), None) + self.wifi_wait(|this| this.wifi.is_scan_done().map(|s| !s), None) .await?; self.wifi.get_scan_result() @@ -2287,9 +2287,9 @@ where /// driver posts a Wifi event on the system event loop. The reasoning behind /// this is that changes to the state of the Wifi driver are always /// accompanied by posting Wifi events. - pub async fn wifi_wait Result>( - &self, - matcher: F, + pub async fn wifi_wait Result>( + &mut self, + mut matcher: F, timeout: Option, ) -> Result<(), EspError> { use embedded_svc::utils::asyncify::event_bus::AsyncEventBus; @@ -2298,10 +2298,9 @@ where let event_loop = AsyncEventBus::new((), self.event_loop.clone()); let timer_service = AsyncTimerService::new(self.timer_service.clone()); - let mut wait = - crate::eventloop::AsyncWait::::new(&event_loop, &timer_service)?; + let mut wait = crate::eventloop::AsyncWait::::new(event_loop, timer_service)?; - wait.wait_while(matcher, timeout).await + wait.wait_while(|| matcher(self), timeout).await } /// Start WPS and perform a wait asynchronously until it connects, fails or @@ -2315,7 +2314,7 @@ where pub async fn start_wps(&mut self, config: &WpsConfig<'_>) -> Result { self.wifi.start_wps(config)?; self.wifi_wait( - || self.wifi.is_wps_finished().map(|x| !x), + |this| this.wifi.is_wps_finished().map(|x| !x), Some(WPS_TIMEOUT), ) .await?; @@ -2335,16 +2334,16 @@ where } /// Waits until the underlaying network interface is up. - pub async fn wait_netif_up(&self) -> Result<(), EspError> { - self.ip_wait_while(|| self.wifi.is_up().map(|s| !s), Some(CONNECT_TIMEOUT)) + pub async fn wait_netif_up(&mut self) -> Result<(), EspError> { + self.ip_wait_while(|this| this.wifi.is_up().map(|s| !s), Some(CONNECT_TIMEOUT)) .await } /// As [`AsyncWifi::wifi_wait()`], but for `EspWifi` events related to the /// IP layer, instead of `WifiDriver` events on the data link layer. - pub async fn ip_wait_while Result>( - &self, - matcher: F, + pub async fn ip_wait_while Result>( + &mut self, + mut matcher: F, timeout: Option, ) -> Result<(), EspError> { use embedded_svc::utils::asyncify::event_bus::AsyncEventBus; @@ -2353,9 +2352,9 @@ where let event_loop = AsyncEventBus::new((), self.event_loop.clone()); let timer_service = AsyncTimerService::new(self.timer_service.clone()); - let mut wait = crate::eventloop::AsyncWait::::new(&event_loop, &timer_service)?; + let mut wait = crate::eventloop::AsyncWait::::new(event_loop, timer_service)?; - wait.wait_while(matcher, timeout).await + wait.wait_while(|| matcher(self), timeout).await } }