From 37cfe1e7297fa40d0cd6ab94988d90331ca2ee66 Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Tue, 21 Aug 2018 19:46:35 +0100 Subject: [PATCH] Feat(Windows/Adapter): Add Wrapper Around Wifi Adapter --- src/platforms/linux.rs | 3 +++ src/platforms/mod.rs | 4 +-- src/platforms/osx.rs | 3 +++ src/platforms/windows.rs | 58 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/platforms/linux.rs b/src/platforms/linux.rs index 62d20e3..8b1b6b3 100644 --- a/src/platforms/linux.rs +++ b/src/platforms/linux.rs @@ -27,6 +27,7 @@ impl Linux { /// Wifi interface for linux operating system. /// This provides basic functionalities for wifi interface. impl WifiInterface for Linux { + /// Check if wireless network adapter is enabled. fn is_wifi_enabled() -> Result { let output = Command::new("nmcli") .args(&["radio", "wifi"]) @@ -39,6 +40,7 @@ impl WifiInterface for Linux { .contains("enabled")) } + /// Turn on the wireless network adapter. fn turn_on() -> Result<(), WifiError> { let _output = Command::new("nmcli") .args(&["radio", "wifi", "on"]) @@ -48,6 +50,7 @@ impl WifiInterface for Linux { Ok(()) } + /// Turn off the wireless network adapter. fn turn_off() -> Result<(), WifiError> { let _output = Command::new("nmcli") .args(&["radio", "wifi", "off"]) diff --git a/src/platforms/mod.rs b/src/platforms/mod.rs index d9c661e..5a8c62a 100644 --- a/src/platforms/mod.rs +++ b/src/platforms/mod.rs @@ -2,14 +2,14 @@ mod linux; #[cfg(target_os = "osx")] mod osx; -#[cfg(target_os = "windows")] +// #[cfg(target_os = "windows")] mod windows; #[cfg(target_os = "linux")] pub use self::linux::{Connection, Linux as WiFi}; #[cfg(target_os = "osx")] pub use self::osx::{Connection, Osx as WiFi}; -#[cfg(target_os = "windows")] +// #[cfg(target_os = "windows")] pub use self::windows::{Connection, Windows as WiFi}; use std::{fmt, io}; diff --git a/src/platforms/osx.rs b/src/platforms/osx.rs index 39d3e49..c743899 100644 --- a/src/platforms/osx.rs +++ b/src/platforms/osx.rs @@ -27,6 +27,7 @@ impl Osx { /// Wifi interface for osx operating system. /// This provides basic functionalities for wifi interface. impl WifiInterface for Osx { + /// Check if wireless network adapter is enabled. fn is_wifi_enabled() -> Result { let output = Command::new("networksetup") .args(&["radio", "wifi"]) @@ -39,6 +40,7 @@ impl WifiInterface for Osx { .contains("enabled")) } + /// Turn on the wireless network adapter. fn turn_on() -> Result<(), WifiError> { let output = Command::new("networksetup") .args(&["-setairportpower", "en0", "on"]) @@ -48,6 +50,7 @@ impl WifiInterface for Osx { Ok(()) } + /// Turn off the wireless network adapter. fn turn_off() -> Result<(), WifiError> { let output = Command::new("networksetup") .args(&["-setairportpower", "en0", "off"]) diff --git a/src/platforms/windows.rs b/src/platforms/windows.rs index 5c7f23c..6082056 100644 --- a/src/platforms/windows.rs +++ b/src/platforms/windows.rs @@ -1,7 +1,9 @@ use platforms::Config; -// use platforms::WifiError; +use platforms::WifiError; use platforms::WifiInterface; -// use std::process::Command; +use std::process::Command; + +const WINDOWS_INTERFACE: &'static str = "Wireless Network Connection"; #[derive(Debug)] pub struct Connection { @@ -28,4 +30,54 @@ impl Windows { /// Wifi interface for windows operating system. /// This provides basic functionalities for wifi interface. -impl WifiInterface for Windows {} +impl WifiInterface for Windows { + /// Check if wireless network adapter is enabled. + fn is_wifi_enabled() -> Result { + let output = Command::new("netsh") + .args(&[ + "wlan", + "show", + "interface", + &format!("name= \"{}\"", WINDOWS_INTERFACE), + ]) + .output() + .map_err(|err| WifiError::IoError(err))?; + + Ok(String::from_utf8_lossy(&output.stdout) + .replace(" ", "") + .replace("\n", "") + .contains("enabled")) + } + + /// Turn on the wireless network adapter. + fn turn_on() -> Result<(), WifiError> { + let _output = Command::new("netsh") + .args(&[ + "interface", + "set", + "interface", + &format!("name= \"{}\"", WINDOWS_INTERFACE), + "ENABLED", + ]) + .output() + .map_err(|err| WifiError::IoError(err))?; + + Ok(()) + } + + /// Turn off the wireless network adapter. + fn turn_off() -> Result<(), WifiError> { + let _output = Command::new("nmcli") + .args(&[ + "interface", + "set", + "interface", + &format!("name= \"{}\"", WINDOWS_INTERFACE), + "DISABLED", + ]) + .output() + .map_err(|err| WifiError::IoError(err))?; + + Ok(()) + } +}