diff --git a/README.md b/README.md index 8a83aeb..93cfa21 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ Note that only **open**, **WEP** and **WPA-PSK** networks are supported at the m ```RUST extern crate wifi_rs; -use wifi_rs::{WiFi, Config, NetworkError}; +use wifi_rs::{WiFi, Config, WifiConnectionError}; -fn main() -> Result<(), NetworkError> { +fn main() -> Result<(), WifiConnectionError> { let config = Some(Config { interface: Some("wlo1"), // interface : None would default to `wlan0`. }); diff --git a/src/connectivity/handlers/mod.rs b/src/connectivity/handlers/mod.rs index 7d67419..6958c2d 100644 --- a/src/connectivity/handlers/mod.rs +++ b/src/connectivity/handlers/mod.rs @@ -1,3 +1,2 @@ mod xml_profile_handler; - pub(crate) use self::xml_profile_handler::NetworkXmlProfileHandler; diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 0519963..55f36a9 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -1,15 +1,20 @@ -mod handlers; pub mod profile_network; mod providers; + +#[cfg(target_os = "windows")] +mod handlers; +#[cfg(target_os = "windows")] mod stubs; use std::{fmt, io}; pub trait Network: fmt::Debug { /// Makes an attempt to connect to a selected wireless network with password specified. - fn connect(&self, password: &str) -> Result; - fn disconnect(&self) -> Result; + fn connect(&self, password: &str) -> Result; + fn disconnect(&self) -> Result; fn is_wifi_enabled(&self) -> bool; + fn connnection_up(&self) -> bool; + fn connnection_down(&self) -> bool; } // #[derive(Debug)] @@ -25,8 +30,7 @@ pub struct Config<'a> { } #[derive(Debug)] -pub enum NetworkError { - // FromUtf8Error(FromUtf8Error), +pub enum WifiConnectionError { SsidNotFound, OsNotSupported, IpAssignFailed, @@ -37,8 +41,8 @@ pub enum NetworkError { WiFiInterfaceDisabled, } -impl From for NetworkError { +impl From for WifiConnectionError { fn from(error: io::Error) -> Self { - NetworkError::IoError(error) + WifiConnectionError::IoError(error) } } diff --git a/src/connectivity/profile_network.rs b/src/connectivity/profile_network.rs index dad49c0..34773c9 100644 --- a/src/connectivity/profile_network.rs +++ b/src/connectivity/profile_network.rs @@ -1,4 +1,4 @@ -use connectivity::{providers::Machine, Config, Network, NetworkError}; +use connectivity::{providers::Machine, Config, Network, WifiConnectionError}; #[derive(Debug)] pub struct ProfileNetwork { @@ -7,9 +7,9 @@ pub struct ProfileNetwork { /// Profile Network handler responsible to connect to a wireless network. impl ProfileNetwork { - pub fn new(name: &str, config: Option) -> Result { + pub fn new(name: &str, config: Option) -> Result { if !(cfg!(target_os = "linux") || cfg!(target_os = "windows")) { - return Err(NetworkError::OsNotSupported); + return Err(WifiConnectionError::OsNotSupported); } let handler = Machine::new( @@ -22,11 +22,15 @@ impl ProfileNetwork { }) } - pub fn connect(&self, password: &str) -> Result { + pub fn connect(&self, password: &str) -> Result { if !self.handler.is_wifi_enabled() { - return Err(NetworkError::WiFiInterfaceDisabled); + return Err(WifiConnectionError::WiFiInterfaceDisabled); } self.handler.connect(password) } + + pub fn connection_up(&self) -> bool { + false + } } diff --git a/src/connectivity/providers/linux.rs b/src/connectivity/providers/linux.rs index af00815..d81ca0b 100644 --- a/src/connectivity/providers/linux.rs +++ b/src/connectivity/providers/linux.rs @@ -1,4 +1,4 @@ -use connectivity::{Network, NetworkError}; +use connectivity::{Network, WifiConnectionError}; use std::process::Command; #[derive(Debug)] @@ -17,7 +17,7 @@ impl Linux { } impl Network for Linux { - fn connect(&self, password: &str) -> Result { + fn connect(&self, password: &str) -> Result { let output = Command::new("nmcli") .args(&[ "d", @@ -30,18 +30,18 @@ impl Network for Linux { &self.interface, ]) .output() - .map_err(|err| NetworkError::FailedToConnect(format!("{}", err)))?; + .map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?; Ok(String::from_utf8_lossy(&output.stdout) .as_ref() .contains("successfully activated")) } - fn disconnect(&self) -> Result { + fn disconnect(&self) -> Result { let output = Command::new("nmcli") .args(&["d", "disconnect", "ifname", &self.interface]) .output() - .map_err(|err| NetworkError::FailedToDisconnect(format!("{}", err)))?; + .map_err(|err| WifiConnectionError::FailedToDisconnect(format!("{}", err)))?; Ok(String::from_utf8_lossy(&output.stdout) .as_ref() @@ -60,4 +60,28 @@ impl Network for Linux { .replace("\n", "") .contains("enabled") } + + fn connnection_up(&self) -> bool { + let output = Command::new("nmcli") + .args(&["radio", "wifi", "on"]) + .output(); + + if let Err(_) = output { + return false; + } + + false + } + + fn connnection_down(&self) -> bool { + let output = Command::new("nmcli") + .args(&["radio", "wifi", "off"]) + .output(); + + if let Err(_) = output { + return false; + } + + false + } } diff --git a/src/connectivity/providers/mod.rs b/src/connectivity/providers/mod.rs index 94c4be5..db39deb 100644 --- a/src/connectivity/providers/mod.rs +++ b/src/connectivity/providers/mod.rs @@ -1,8 +1,14 @@ -mod linux; +#[cfg(target_os = "windows")] mod windows; - #[cfg(target_os = "windows")] pub(crate) use self::windows::Windows as Machine; +#[cfg(target_os = "linux")] +mod linux; #[cfg(target_os = "linux")] pub(crate) use self::linux::Linux as Machine; + +#[cfg(target_os = "osx")] +mod osx; +#[cfg(target_os = "osx")] +pub(crate) use self::osx::OSX as Machine; diff --git a/src/connectivity/providers/windows.rs b/src/connectivity/providers/windows.rs index b91b512..1044f92 100644 --- a/src/connectivity/providers/windows.rs +++ b/src/connectivity/providers/windows.rs @@ -1,5 +1,5 @@ use connectivity::handlers::NetworkXmlProfileHandler; -use connectivity::{Network, NetworkError}; +use connectivity::{Network, WifiConnectionError}; use std::process::Command; #[derive(Debug)] @@ -15,7 +15,7 @@ impl Windows { } } - pub(crate) fn add_profile(&self, password: &str) -> Result<(), NetworkError> { + pub(crate) fn add_profile(&self, password: &str) -> Result<(), WifiConnectionError> { let mut handler = NetworkXmlProfileHandler::new(); handler.content = handler .content @@ -33,31 +33,31 @@ impl Windows { &format!("filename={}", temp_file.path().to_str().unwrap()), ]) .output() - .map_err(|_| NetworkError::AddNetworkProfileFailed)?; + .map_err(|_| WifiConnectionError::AddNetworkProfileFailed)?; Ok(()) } } impl Network for Windows { - fn connect(&self, password: &str) -> Result { + fn connect(&self, password: &str) -> Result { self.add_profile(password)?; let output = Command::new("netsh") .args(&["wlan", "connect", &format!("name={}", self.name)]) .output() - .map_err(|err| NetworkError::FailedToConnect(format!("{}", err)))?; + .map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?; Ok(String::from_utf8_lossy(&output.stdout) .as_ref() .contains("successfully activated")) } - fn disconnect(&self) -> Result { + fn disconnect(&self) -> Result { let output = Command::new("netsh") .args(&["wlan", "disconnect"]) .output() - .map_err(|err| NetworkError::FailedToDisconnect(format!("{}", err)))?; + .map_err(|err| WifiConnectionError::FailedToDisconnect(format!("{}", err)))?; Ok(String::from_utf8_lossy(&output.stdout) .as_ref() @@ -65,16 +65,14 @@ impl Network for Windows { } fn is_wifi_enabled(&self) -> bool { - // let output = Command::new("nmcli").args(&["radio", "wifi"]).output(); + unimplemented!() + } - // if let Err(_) = output { - // return false; - // } + fn connnection_up(&self) -> bool { + unimplemented!() + } - // match String::from_utf8_lossy(&output.unwrap().stdout).as_ref() { - // "enabled" => true, - // _ => false, - // } - false + fn connnection_down(&self) -> bool { + unimplemented!() } } diff --git a/src/main.rs b/src/main.rs index 11c084b..f6844b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod connectivity; -pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config, NetworkError}; +pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config, WifiConnectionError}; -fn main() -> Result<(), NetworkError> { +fn main() -> Result<(), WifiConnectionError> { let config = Some(Config { interface: Some("wlo1"), });