| 1 |
use std::{cmp::Reverse, time::Duration}; |
| 2 |
|
| 3 |
use nmrs::{ |
| 4 |
AirplaneModeState, ConnectionError, Network, NetworkInfo, RadioState, SavedConnection, |
| 5 |
SettingsPatch, WifiDevice, WifiSecurity, |
| 6 |
}; |
| 7 |
use tokio::{ |
| 8 |
sync::mpsc::UnboundedSender, |
| 9 |
time::{self, timeout}, |
| 10 |
}; |
| 11 |
|
| 12 |
use crate::action::Action; |
| 13 |
|
| 14 |
#[derive(Clone)] |
| 15 |
pub struct NetworkManager { |
| 16 |
pub nmrs: nmrs::NetworkManager, |
| 17 |
pub current_network: Option<Network>, |
| 18 |
pub current_network_info: Option<NetworkInfo>, |
| 19 |
} |
| 20 |
|
| 21 |
impl NetworkManager { |
| 22 |
pub async fn new() -> anyhow::Result<Self> { |
| 23 |
let nmrs = nmrs::NetworkManager::new().await?; |
| 24 |
let current_network = nmrs.current_network().await?; |
| 25 |
let current_network_info = if let Some(network) = ¤t_network { |
| 26 |
Some(nmrs.show_details(network).await?) |
| 27 |
} else { |
| 28 |
None |
| 29 |
}; |
| 30 |
|
| 31 |
Ok(Self { |
| 32 |
nmrs, |
| 33 |
current_network, |
| 34 |
current_network_info, |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub async fn scan_networks(&self) { |
| 41 |
if let Err(e) = self.nmrs.scan_networks(None).await { |
| 42 |
debug!("Failed to trigger WiFi scan via NetworkManager: {e}"); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
pub async fn update_network_on_changes( |
| 47 |
&self, |
| 48 |
tx: UnboundedSender<Action>, |
| 49 |
) -> Result<nmrs::MonitorHandle, ConnectionError> { |
| 50 |
self.nmrs |
| 51 |
.monitor_network_changes(move || _ = tx.send(Action::Reload)) |
| 52 |
.await |
| 53 |
} |
| 54 |
|
| 55 |
pub async fn networks_list(&self) -> Result<(Vec<Network>, Vec<Network>), ConnectionError> { |
| 56 |
let mut networks = self.nmrs.list_networks(None).await?; |
| 57 |
networks.sort_by_key(|b| Reverse(b.strength)); |
| 58 |
Ok(networks.into_iter().partition(|net| net.known)) |
| 59 |
} |
| 60 |
|
| 61 |
pub async fn get_devices(&self) -> Result<Vec<WifiDevice>, ConnectionError> { |
| 62 |
self.nmrs.list_wifi_devices().await |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pub async fn current_network(&self) -> Option<Network> { |
| 68 |
self.nmrs.current_network().await.ok()? |
| 69 |
} |
| 70 |
|
| 71 |
pub async fn show_details(&self, network: &Network) -> Result<NetworkInfo, ConnectionError> { |
| 72 |
self.nmrs.show_details(network).await |
| 73 |
} |
| 74 |
|
| 75 |
pub async fn connect( |
| 76 |
&self, |
| 77 |
ssid: &str, |
| 78 |
interface: Option<String>, |
| 79 |
credentials: WifiSecurity, |
| 80 |
) -> Result<Result<(), ConnectionError>, time::error::Elapsed> { |
| 81 |
timeout( |
| 82 |
Duration::from_secs(30), |
| 83 |
self.nmrs.connect(ssid, interface.as_deref(), credentials), |
| 84 |
) |
| 85 |
.await |
| 86 |
} |
| 87 |
|
| 88 |
pub async fn disconnect(&self) -> Result<(), ConnectionError> { |
| 89 |
self.nmrs.disconnect(None).await |
| 90 |
} |
| 91 |
|
| 92 |
pub async fn forget(&self, ssid: &str) -> Result<(), ConnectionError> { |
| 93 |
self.nmrs.forget(ssid).await |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub async fn get_saved_connection_uuid( |
| 99 |
&self, |
| 100 |
ssid: &str, |
| 101 |
) -> Result<Option<String>, ConnectionError> { |
| 102 |
self.nmrs.get_saved_connection_uuid(ssid).await |
| 103 |
} |
| 104 |
|
| 105 |
pub async fn get_saved_connection( |
| 106 |
&self, |
| 107 |
uuid: &str, |
| 108 |
) -> Result<SavedConnection, ConnectionError> { |
| 109 |
self.nmrs.get_saved_connection(uuid).await |
| 110 |
} |
| 111 |
|
| 112 |
pub async fn update_saved_connection( |
| 113 |
&self, |
| 114 |
uuid: &str, |
| 115 |
patch: SettingsPatch, |
| 116 |
) -> Result<(), ConnectionError> { |
| 117 |
self.nmrs.update_saved_connection(uuid, patch).await |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
pub async fn wifi_state(&self) -> Result<RadioState, ConnectionError> { |
| 123 |
self.nmrs.wifi_state().await |
| 124 |
} |
| 125 |
|
| 126 |
pub async fn set_wireless_enabled(&self, enabled: bool) -> Result<(), ConnectionError> { |
| 127 |
self.nmrs.set_wireless_enabled(enabled).await |
| 128 |
} |
| 129 |
|
| 130 |
pub async fn airplane_mode_state(&self) -> Result<AirplaneModeState, ConnectionError> { |
| 131 |
self.nmrs.airplane_mode_state().await |
| 132 |
} |
| 133 |
|
| 134 |
pub async fn set_airplane_mode(&self, enabled: bool) -> Result<(), ConnectionError> { |
| 135 |
self.nmrs.set_airplane_mode(enabled).await |
| 136 |
} |
| 137 |
} |