wifi_rs/platforms/
linux.rs

1use crate::platforms::{Config, WifiError, WifiInterface};
2use std::process::Command;
3
4#[derive(Debug)]
5pub struct Connection {
6    // pub(crate) ssid: String,
7}
8
9/// Wireless network interface for linux operating system.
10#[derive(Debug)]
11pub struct Linux {
12    pub(crate) connection: Option<Connection>,
13    pub(crate) interface: String,
14}
15
16impl Linux {
17    pub fn new(config: Option<Config>) -> Self {
18        Linux {
19            connection: None,
20            interface: config.map_or("wlan0".to_string(), |cfg| {
21                cfg.interface.unwrap_or("wlan0").to_string()
22            }),
23        }
24    }
25}
26
27/// Wifi interface for linux operating system.
28/// This provides basic functionalities for wifi interface.
29impl WifiInterface for Linux {
30    /// Check if wireless network adapter is enabled.
31    fn is_wifi_enabled() -> Result<bool, WifiError> {
32        let output = Command::new("nmcli")
33            .args(&["radio", "wifi"])
34            .output()
35            .map_err(|err| WifiError::IoError(err))?;
36
37        Ok(String::from_utf8_lossy(&output.stdout)
38            .replace(" ", "")
39            .replace("\n", "")
40            .contains("enabled"))
41    }
42
43    /// Turn on the wireless network adapter.
44    fn turn_on() -> Result<(), WifiError> {
45        Command::new("nmcli")
46            .args(&["radio", "wifi", "on"])
47            .output()
48            .map_err(|err| WifiError::IoError(err))?;
49
50        Ok(())
51    }
52
53    /// Turn off the wireless network adapter.
54    fn turn_off() -> Result<(), WifiError> {
55        Command::new("nmcli")
56            .args(&["radio", "wifi", "off"])
57            .output()
58            .map_err(|err| WifiError::IoError(err))?;
59
60        Ok(())
61    }
62}