Skip to main content

wifi_ctrl/ap/
setup.rs

1use super::*;
2
3use std::time::Duration;
4
5/// Default time to wait for a reply to a control command/request before giving
6/// up. Chosen to comfortably cover slower hostapd operations while still
7/// unblocking the single-task runtime if a reply never arrives.
8const DEFAULT_COMMAND_TIMEOUT: Duration = Duration::from_secs(3);
9
10/// Default number of `ATTACH`/`LOG_LEVEL` handshake attempts. With
11/// [`DEFAULT_ATTACH_RETRY_DELAY`] between tries this bounds the wait to roughly
12/// a minute, unlike the socket-open path which retries for 5 minutes.
13const DEFAULT_ATTACH_RETRIES: usize = 240;
14/// Default delay between attach handshake attempts.
15const DEFAULT_ATTACH_RETRY_DELAY: Duration = Duration::from_millis(250);
16
17/// Default capacity of the request and broadcast channels.
18const DEFAULT_CHANNEL_SIZE: usize = 32;
19
20/// Setup struct for the WiFiAp process.
21pub struct WifiSetup {
22    /// Struct for handling runtime process
23    wifi: WifiAp,
24    /// Client for making requests
25    request_client: RequestClient,
26}
27
28impl WifiSetup {
29    pub fn new() -> Self {
30        Self::with_capacities(DEFAULT_CHANNEL_SIZE, DEFAULT_CHANNEL_SIZE)
31    }
32
33    /// Like [`Self::new`] but with explicit request and broadcast channel
34    /// capacities (both default to 32).
35    pub fn with_capacities(request_channel_size: usize, broadcast_channel_size: usize) -> Self {
36        // setup the channel for client requests
37        let (self_sender, request_receiver) = mpsc::channel(request_channel_size);
38        let request_client = RequestClient::new(self_sender.clone());
39        // setup the sender for broadcasts; receivers subscribe on demand
40        let broadcast_sender = broadcast::Sender::new(broadcast_channel_size);
41
42        Self {
43            wifi: WifiAp {
44                socket_path: PATH_DEFAULT_SERVER.into(),
45                attach_options: vec![],
46                request_receiver,
47                broadcast_sender,
48                self_sender,
49                command_timeout: DEFAULT_COMMAND_TIMEOUT,
50                attach_retries: DEFAULT_ATTACH_RETRIES,
51                attach_retry_delay: DEFAULT_ATTACH_RETRY_DELAY,
52            },
53            request_client,
54        }
55    }
56
57    pub fn set_socket_path<S: Into<std::path::PathBuf>>(&mut self, path: S) {
58        self.wifi.socket_path = path.into();
59    }
60
61    pub fn add_attach_options(&mut self, options: &[&str]) {
62        for o in options {
63            self.wifi.attach_options.push(o.to_string());
64        }
65    }
66
67    /// Set how long to wait for a reply to a control command/request before
68    /// giving up with [`ClientError::Timeout`](crate::error::ClientError::Timeout).
69    pub fn set_command_timeout(&mut self, timeout: Duration) {
70        self.wifi.command_timeout = timeout;
71    }
72
73    /// Set how many times to retry the hostapd `ATTACH`/`LOG_LEVEL` handshake
74    /// before giving up with
75    /// [`SocketError::AttachFailed`](crate::error::SocketError::AttachFailed).
76    pub fn set_attach_retries(&mut self, retries: usize) {
77        self.wifi.attach_retries = retries;
78    }
79
80    /// Set how long to wait between attach handshake attempts.
81    pub fn set_attach_retry_delay(&mut self, delay: Duration) {
82        self.wifi.attach_retry_delay = delay;
83    }
84
85    pub fn get_broadcast_receiver(&self) -> BroadcastReceiver {
86        self.wifi.broadcast_sender.subscribe()
87    }
88    pub fn get_request_client(&self) -> RequestClient {
89        self.request_client.clone()
90    }
91
92    pub fn complete(self) -> WifiAp {
93        self.wifi
94    }
95}
96
97impl Default for WifiSetup {
98    fn default() -> Self {
99        Self::new()
100    }
101}