Skip to main content

drivers_core/wifi/
runner.rs

1use super::controller::{Pending, Shared};
2use embassy_futures::select::{Either, select};
3use network::WifiConnectionErrorKind;
4use synchronization::Arc;
5
6/// Consumed by run, so cancelling its task also marks the controller unavailable.
7pub struct MockWifiRunner {
8    pub(super) shared: Arc<Shared>,
9}
10impl MockWifiRunner {
11    pub async fn run(self) -> ! {
12        loop {
13            let pending = self.shared.state.lock(|state| {
14                let state = state.borrow();
15                state.pending.as_ref().map(|pending| {
16                    (
17                        state.revision,
18                        match pending {
19                            Pending::Scan(_) => state.configuration.scan_latency,
20                            Pending::Connect(_) | Pending::Disconnect => {
21                                state.configuration.connect_latency
22                            }
23                        },
24                    )
25                })
26            });
27            if let Some((revision, delay)) = pending {
28                if let Either::First(()) = select(task::sleep(delay), self.shared.wake.wait()).await
29                {
30                    self.shared.state.lock(|state| {
31                        let mut state = state.borrow_mut();
32                        if state.revision == revision {
33                            state.complete();
34                        }
35                    });
36                }
37            } else {
38                self.shared.wake.wait().await;
39            }
40        }
41    }
42}
43impl Drop for MockWifiRunner {
44    fn drop(&mut self) {
45        self.shared.state.lock(|state| {
46            let mut state = state.borrow_mut();
47            state.cancel();
48            state.alive = false;
49            state.error = WifiConnectionErrorKind::DRIVER;
50        });
51    }
52}