wifi_ctrl/sta/
setup.rs

1use super::*;
2
3/// A convenient default type for setting up the WiFi Station process.
4pub type WifiSetup = WifiSetupGeneric<32, 32>;
5
6/// The generic WifiSetup struct which has generic constant parameters for adjusting queue size.
7/// WiFiSetup type is provided for convenience.
8pub struct WifiSetupGeneric<const C: usize = 32, const B: usize = 32> {
9    /// Struct for handling runtime process
10    wifi: WifiStation,
11    /// Client for making requests
12    request_client: RequestClient,
13    #[allow(unused)]
14    /// Client for receiving alerts
15    broadcast_receiver: BroadcastReceiver,
16}
17
18impl<const C: usize, const B: usize> WifiSetupGeneric<C, B> {
19    pub fn new() -> Result<Self> {
20        // setup the channel for client requests
21        let (self_sender, request_receiver) = mpsc::channel(C);
22        let request_client = RequestClient::new(self_sender.clone());
23        // setup the channel for broadcasts
24        let (broadcast_sender, broadcast_receiver) = broadcast::channel(B);
25
26        Ok(Self {
27            wifi: WifiStation {
28                socket_path: PATH_DEFAULT_SERVER.into(),
29                request_receiver,
30                broadcast_sender,
31                self_sender,
32                select_timeout: Duration::from_secs(10),
33            },
34            request_client,
35            broadcast_receiver,
36        })
37    }
38
39    pub fn set_socket_path<S: Into<std::path::PathBuf>>(&mut self, path: S) {
40        self.wifi.socket_path = path.into();
41    }
42
43    pub fn set_select_timeout(&mut self, timeout: Duration) {
44        self.wifi.select_timeout = timeout;
45    }
46
47    pub fn get_broadcast_receiver(&self) -> BroadcastReceiver {
48        self.wifi.broadcast_sender.subscribe()
49    }
50    pub fn get_request_client(&self) -> RequestClient {
51        self.request_client.clone()
52    }
53
54    pub fn complete(self) -> WifiStation {
55        self.wifi
56    }
57}