Feat(Windows/Adapter): Add Wrapper Around Wifi Adapter

This commit is contained in:
Tochukwu Nkemdilim
2018-08-21 19:46:35 +01:00
parent 03325c939a
commit 37cfe1e729
4 changed files with 63 additions and 5 deletions
+3
View File
@@ -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<bool, WifiError> {
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"])
+2 -2
View File
@@ -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};
+3
View File
@@ -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<bool, WifiError> {
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"])
+55 -3
View File
@@ -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<bool, WifiError> {
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(())
}
}