index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / network_manager.rs
-rw-r--r-- blob 274ff1495f5efe8b95b845c1daf12b8f219cafac 3182 bytes (plain)
1
use std::{cmp::Reverse, time::Duration};
2
3
use anyhow::Context;
4
use nmrs::{ConnectionError, Network, NetworkInfo, SavedConnection, WifiDevice, WifiSecurity};
5
use tokio::time::{self, timeout};
6
7
#[derive(Clone)]
8
pub struct NetworkManager {
9
    pub nmrs: nmrs::NetworkManager,
10
    pub current_network: Option<Network>,
11
    pub current_network_info: Option<NetworkInfo>,
12
}
13
14
// TODO: AirPlane mode
15
impl NetworkManager {
16
    pub async fn new() -> anyhow::Result<Self> {
17
        let nmrs = nmrs::NetworkManager::new().await?;
18
        let current_network = nmrs.current_network().await?;
19
        let current_network_info = if let Some(network) = &current_network {
20
            Some(nmrs.show_details(network).await?)
21
        } else {
22
            None
23
        };
24
25
        Ok(Self {
26
            nmrs,
27
            current_network,
28
            current_network_info,
29
        })
30
    }
31
32
    pub async fn get_devices(&self) -> Result<Vec<WifiDevice>, ConnectionError> {
33
        self.nmrs.list_wifi_devices().await
34
    }
35
36
    pub async fn current_network(&self) -> Option<Network> {
37
        self.nmrs.current_network().await.ok()?
38
    }
39
40
    pub async fn show_details(&self, network: &Network) -> Result<NetworkInfo, ConnectionError> {
41
        self.nmrs.show_details(network).await
42
    }
43
44
    pub async fn scan_networks(&self) -> anyhow::Result<Vec<Network>> {
45
        self.nmrs
46
            .scan_networks(None)
47
            .await
48
            .context("Failed to trigger WiFi scan via NetworkManager")?;
49
50
        let mut networks = self.nmrs.list_networks(None).await?;
51
        networks.sort_by_key(|b| Reverse(b.strength));
52
        Ok(networks)
53
    }
54
55
    pub async fn networks_list(&self) -> Result<(Vec<Network>, Vec<Network>), ConnectionError> {
56
        let scan_list = self.scan_networks().await.unwrap_or_default();
57
58
        let mut known_final = Vec::new();
59
        let mut new_final = Vec::new();
60
61
        for net in &scan_list {
62
            if net.known {
63
                known_final.push(net.clone());
64
            } else {
65
                new_final.push(net.clone());
66
            }
67
        }
68
        Ok((known_final, new_final))
69
    }
70
71
    pub async fn _saved_connections(&self) -> Result<Vec<SavedConnection>, ConnectionError> {
72
        self.nmrs.list_saved_connections().await
73
    }
74
75
    pub async fn forget(&self, ssid: &str) -> Result<(), ConnectionError> {
76
        self.nmrs.forget(ssid).await
77
    }
78
79
    pub async fn connect(
80
        &self,
81
        ssid: &str,
82
        interface: Option<String>,
83
        credentials: WifiSecurity,
84
    ) -> Result<Result<(), ConnectionError>, time::error::Elapsed> {
85
        timeout(
86
            Duration::from_secs(30),
87
            self.nmrs.connect(ssid, interface.as_deref(), credentials),
88
        )
89
        .await
90
    }
91
92
    pub async fn _is_connected(&self, ssid: &str) -> Result<bool, ConnectionError> {
93
        self.nmrs.is_connected(ssid).await
94
    }
95
96
    #[allow(dead_code)]
97
    pub async fn has_saved_connection(&self, ssid: &str) -> Result<bool, ConnectionError> {
98
        self.nmrs.has_saved_connection(ssid).await
99
    }
100
101
    pub async fn disconnect(&self) -> Result<(), ConnectionError> {
102
        self.nmrs.disconnect(None).await
103
    }
104
}