Skip to main content

wifi_ctrl/sta/
setup.rs

1use super::*;
2
3/// Default time to wait for a reply to a control command/request before giving
4/// up. Chosen to comfortably cover slower wpa_supplicant operations while still
5/// unblocking the single-task runtime if a reply never arrives.
6const DEFAULT_COMMAND_TIMEOUT: Duration = Duration::from_secs(3);
7
8/// Default capacity of the request and broadcast channels.
9const DEFAULT_CHANNEL_SIZE: usize = 32;
10
11/// Setup struct for the WiFi Station process.
12pub struct WifiSetup {
13    /// Struct for handling runtime process
14    wifi: WifiStation,
15    /// Client for making requests
16    request_client: RequestClient,
17}
18
19impl WifiSetup {
20    pub fn new() -> Self {
21        Self::with_capacities(DEFAULT_CHANNEL_SIZE, DEFAULT_CHANNEL_SIZE)
22    }
23
24    /// Like [`Self::new`] but with explicit request and broadcast channel
25    /// capacities (both default to 32).
26    pub fn with_capacities(request_channel_size: usize, broadcast_channel_size: usize) -> Self {
27        // setup the channel for client requests
28        let (self_sender, request_receiver) = mpsc::channel(request_channel_size);
29        let request_client = RequestClient::new(self_sender.clone());
30        // setup the sender for broadcasts; receivers subscribe on demand
31        let broadcast_sender = broadcast::Sender::new(broadcast_channel_size);
32
33        Self {
34            wifi: WifiStation {
35                socket_path: PATH_DEFAULT_SERVER.into(),
36                request_receiver,
37                broadcast_sender,
38                self_sender,
39                select_timeout: Duration::from_secs(10),
40                command_timeout: DEFAULT_COMMAND_TIMEOUT,
41            },
42            request_client,
43        }
44    }
45
46    pub fn set_socket_path<S: Into<std::path::PathBuf>>(&mut self, path: S) {
47        self.wifi.socket_path = path.into();
48    }
49
50    pub fn set_select_timeout(&mut self, timeout: Duration) {
51        self.wifi.select_timeout = timeout;
52    }
53
54    /// Set how long to wait for a reply to a control command/request before
55    /// giving up with [`ClientError::Timeout`](crate::error::ClientError::Timeout).
56    pub fn set_command_timeout(&mut self, timeout: Duration) {
57        self.wifi.command_timeout = timeout;
58    }
59
60    pub fn get_broadcast_receiver(&self) -> BroadcastReceiver {
61        self.wifi.broadcast_sender.subscribe()
62    }
63    pub fn get_request_client(&self) -> RequestClient {
64        self.request_client.clone()
65    }
66
67    pub fn complete(self) -> WifiStation {
68        self.wifi
69    }
70}
71
72impl Default for WifiSetup {
73    fn default() -> Self {
74        Self::new()
75    }
76}