1use super::*;
2
3use std::time::Duration;
4
5const DEFAULT_COMMAND_TIMEOUT: Duration = Duration::from_secs(3);
9
10const DEFAULT_ATTACH_RETRIES: usize = 240;
14const DEFAULT_ATTACH_RETRY_DELAY: Duration = Duration::from_millis(250);
16
17const DEFAULT_CHANNEL_SIZE: usize = 32;
19
20pub struct WifiSetup {
22 wifi: WifiAp,
24 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 pub fn with_capacities(request_channel_size: usize, broadcast_channel_size: usize) -> Self {
36 let (self_sender, request_receiver) = mpsc::channel(request_channel_size);
38 let request_client = RequestClient::new(self_sender.clone());
39 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 pub fn set_command_timeout(&mut self, timeout: Duration) {
70 self.wifi.command_timeout = timeout;
71 }
72
73 pub fn set_attach_retries(&mut self, retries: usize) {
77 self.wifi.attach_retries = retries;
78 }
79
80 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}