Skip to content

Commit

Permalink
#56
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Dec 30, 2023
1 parent 8a1fb54 commit 3ebaf63
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
34 changes: 17 additions & 17 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Fn() -> Result<bool, EspError>>(
&self,
matcher: F,
pub async fn eth_wait_while<F: FnMut(&mut Self) -> Result<bool, EspError>>(
&mut self,
mut matcher: F,
timeout: Option<Duration>,
) -> Result<(), EspError> {
use embedded_svc::utils::asyncify::event_bus::AsyncEventBus;
Expand All @@ -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::<EthEvent, _>::new(&event_loop, &timer_service)?;
let mut wait = crate::eventloop::AsyncWait::<EthEvent, _>::new(event_loop, timer_service)?;

wait.wait_while(matcher, timeout).await
wait.wait_while(|| matcher(self), timeout).await
}
}

Expand All @@ -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<F: FnMut() -> Result<bool, EspError>>(
&self,
matcher: F,
pub async fn ip_wait_while<F: FnMut(&mut Self) -> Result<bool, EspError>>(
&mut self,
mut matcher: F,
timeout: Option<core::time::Duration>,
) -> Result<(), EspError> {
use embedded_svc::utils::asyncify::event_bus::AsyncEventBus;
Expand All @@ -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::<IpEvent, _>::new(&event_loop, &timer_service)?;
let mut wait = crate::eventloop::AsyncWait::<IpEvent, _>::new(event_loop, timer_service)?;

wait.wait_while(matcher, timeout).await
wait.wait_while(|| matcher(self), timeout).await
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
>,
timer_service: &AsyncTimerService<EspTaskTimerService>,
timer_service: AsyncTimerService<EspTaskTimerService>,
) -> Result<Self, EspError> {
Ok(Self {
subscription: event_loop.subscribe()?,
Expand Down Expand Up @@ -1161,7 +1161,7 @@ mod async_wait {
TT: super::EspEventLoopType,
{
while matcher()? {
subscription.recv().await?;
subscription.recv_mut().await?;
}

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions src/netif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: FnMut() -> Result<bool, EspError>>(
&self,
matcher: F,
pub async fn ip_wait_while<F: FnMut(&mut Self) -> Result<bool, EspError>>(
&mut self,
mut matcher: F,
timeout: Option<core::time::Duration>,
) -> Result<(), EspError> {
use embedded_svc::utils::asyncify::event_bus::AsyncEventBus;
Expand All @@ -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::<IpEvent, _>::new(&event_loop, &timer_service)?;
let mut wait = crate::eventloop::AsyncWait::<IpEvent, _>::new(event_loop, timer_service)?;

wait.wait_while(matcher, timeout).await
wait.wait_while(|| matcher(self), timeout).await
}
}

Expand Down
39 changes: 19 additions & 20 deletions src/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,23 +2222,23 @@ 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
}

/// As per [`WifiDriver::stop()`], but as an async call that awaits until
/// 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
/// the wifi driver has connected.
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
Expand All @@ -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()`],
Expand All @@ -2258,7 +2258,7 @@ where
) -> Result<(heapless::Vec<AccessPointInfo, N>, 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()
Expand All @@ -2270,7 +2270,7 @@ where
pub async fn scan(&mut self) -> Result<alloc::vec::Vec<AccessPointInfo>, 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()
Expand All @@ -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<F: Fn() -> Result<bool, EspError>>(
&self,
matcher: F,
pub async fn wifi_wait<F: FnMut(&mut Self) -> Result<bool, EspError>>(
&mut self,
mut matcher: F,
timeout: Option<Duration>,
) -> Result<(), EspError> {
use embedded_svc::utils::asyncify::event_bus::AsyncEventBus;
Expand All @@ -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::<WifiEvent, _>::new(&event_loop, &timer_service)?;
let mut wait = crate::eventloop::AsyncWait::<WifiEvent, _>::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
Expand All @@ -2315,7 +2314,7 @@ where
pub async fn start_wps(&mut self, config: &WpsConfig<'_>) -> Result<WpsStatus, EspError> {
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?;
Expand All @@ -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<F: Fn() -> Result<bool, EspError>>(
&self,
matcher: F,
pub async fn ip_wait_while<F: FnMut(&mut Self) -> Result<bool, EspError>>(
&mut self,
mut matcher: F,
timeout: Option<core::time::Duration>,
) -> Result<(), EspError> {
use embedded_svc::utils::asyncify::event_bus::AsyncEventBus;
Expand All @@ -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::<IpEvent, _>::new(&event_loop, &timer_service)?;
let mut wait = crate::eventloop::AsyncWait::<IpEvent, _>::new(event_loop, timer_service)?;

wait.wait_while(matcher, timeout).await
wait.wait_while(|| matcher(self), timeout).await
}
}

Expand Down

0 comments on commit 3ebaf63

Please sign in to comment.