diff --git a/src/connectivity/mod.rs b/src/connectivity/mod.rs index 0b27db5..fddc39e 100644 --- a/src/connectivity/mod.rs +++ b/src/connectivity/mod.rs @@ -1,32 +1,23 @@ -pub mod profile_network; -mod providers; - #[cfg(target_os = "windows")] mod handlers; + #[cfg(target_os = "windows")] mod stubs; +mod providers; + +use platforms::WifiError; 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 is_wifi_enabled(&self) -> bool; - fn connnection_up(&self) -> bool; - fn connnection_down(&self) -> bool; + fn connect(&mut self, ssid: &str, password: &str) -> Result; - // Hotspot - // fn create_hotspot(&self, ssid: &str, password: &str) -> Result; + /// Disconnects from a wireless network currently connected to. + fn disconnect(&self) -> Result; } -// #[derive(Debug)] -// pub enum NetworkType { -// WEP, -// WPA, -// WPA2, -// } - +/// Configuration for a wifi network. #[derive(Debug, Clone)] pub struct Config<'a> { pub interface: Option<&'a str>, @@ -35,21 +26,17 @@ pub struct Config<'a> { #[derive(Debug)] pub enum WifiConnectionError { SsidNotFound, - OsNotSupported, IpAssignFailed, AddNetworkProfileFailed, - IoError(io::Error), FailedToConnect(String), FailedToDisconnect(String), - WiFiInterfaceDisabled, -} - -pub enum WifiHotspotError { - CreationFailed, + Other { kind: WifiError }, } impl From for WifiConnectionError { fn from(error: io::Error) -> Self { - WifiConnectionError::IoError(error) + WifiConnectionError::Other { + kind: WifiError::IoError(error), + } } } diff --git a/src/connectivity/profile_network.rs b/src/connectivity/profile_network.rs deleted file mode 100644 index 34773c9..0000000 --- a/src/connectivity/profile_network.rs +++ /dev/null @@ -1,36 +0,0 @@ -use connectivity::{providers::Machine, Config, Network, WifiConnectionError}; - -#[derive(Debug)] -pub struct ProfileNetwork { - handler: Box, -} - -/// Profile Network handler responsible to connect to a wireless network. -impl ProfileNetwork { - pub fn new(name: &str, config: Option) -> Result { - if !(cfg!(target_os = "linux") || cfg!(target_os = "windows")) { - return Err(WifiConnectionError::OsNotSupported); - } - - let handler = Machine::new( - name, - config.map_or(None, |cfg| cfg.interface.map_or(None, |x| Some(x))), - ); - - Ok(ProfileNetwork { - handler: Box::new(handler), - }) - } - - pub fn connect(&self, password: &str) -> Result { - if !self.handler.is_wifi_enabled() { - 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 d81ca0b..b9d0f05 100644 --- a/src/connectivity/providers/linux.rs +++ b/src/connectivity/providers/linux.rs @@ -1,29 +1,15 @@ use connectivity::{Network, WifiConnectionError}; +use platforms::{Connection, Linux}; use std::process::Command; -#[derive(Debug)] -pub struct Linux { - pub name: String, - interface: String, -} - -impl Linux { - pub fn new(name: &str, interface: Option<&str>) -> Self { - Linux { - name: name.into(), - interface: interface.unwrap_or("wlan0").into(), - } - } -} - impl Network for Linux { - fn connect(&self, password: &str) -> Result { + fn connect(&mut self, ssid: &str, password: &str) -> Result { let output = Command::new("nmcli") .args(&[ "d", "wifi", "connect", - &self.name, + ssid, "password", &password, "ifname", @@ -32,9 +18,18 @@ impl Network for Linux { .output() .map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?; - Ok(String::from_utf8_lossy(&output.stdout) + if !String::from_utf8_lossy(&output.stdout) .as_ref() - .contains("successfully activated")) + .contains("successfully activated") + { + return Ok(false); + } + + self.connection = Some(Connection { + ssid: String::from(ssid), + }); + + Ok(true) } fn disconnect(&self) -> Result { @@ -47,41 +42,4 @@ impl Network for Linux { .as_ref() .contains("disconnect")) } - - fn is_wifi_enabled(&self) -> bool { - let output = Command::new("nmcli").args(&["radio", "wifi"]).output(); - - if let Err(_) = output { - return false; - } - - String::from_utf8_lossy(&output.unwrap().stdout) - .replace(" ", "") - .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 db39deb..633a7bb 100644 --- a/src/connectivity/providers/mod.rs +++ b/src/connectivity/providers/mod.rs @@ -1,14 +1,8 @@ -#[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; + +// #[cfg(target_os = "windows")] +mod windows; diff --git a/src/connectivity/providers/windows.rs b/src/connectivity/providers/windows.rs index 1044f92..6135c57 100644 --- a/src/connectivity/providers/windows.rs +++ b/src/connectivity/providers/windows.rs @@ -1,15 +1,11 @@ use connectivity::handlers::NetworkXmlProfileHandler; use connectivity::{Network, WifiConnectionError}; +use platforms::Windows; use std::process::Command; -#[derive(Debug)] -pub(crate) struct Windows { - name: String, -} - impl Windows { #[cfg(target_os = "windows")] - pub fn new(name: &str, interface: Option<&str>) -> Self { + pub fn new(name: &str, _interface: Option<&str>) -> Self { Windows { name: String::from(name), } @@ -63,16 +59,4 @@ impl Network for Windows { .as_ref() .contains("disconnect")) } - - fn is_wifi_enabled(&self) -> bool { - unimplemented!() - } - - fn connnection_up(&self) -> bool { - unimplemented!() - } - - fn connnection_down(&self) -> bool { - unimplemented!() - } } diff --git a/src/lib.rs b/src/lib.rs index 3aeaed7..a0abfba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,11 @@ #![allow(dead_code)] mod connectivity; -pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config}; +mod hotspot; +mod platforms; + +pub use connectivity::{Config, *}; +pub use hotspot::*; #[cfg(test)] mod tests { @@ -9,12 +13,12 @@ mod tests { #[test] fn connect_to_wifi_failed() { - let config = Some(Config { - interface: Some("wlo1"), - }); + // let config = Some(Config { + // interface: Some("wlo1"), + // }); - let wifi = WiFi::new("hello", config).unwrap(); + // let wifi = WiFi::new("hello", config).unwrap(); - assert_eq!(wifi.connect("password").unwrap(), false); + // assert_eq!(wifi.connect("password").unwrap(), false); } } diff --git a/src/main.rs b/src/main.rs index f6844b8..93a9de8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,27 @@ mod connectivity; -pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config, WifiConnectionError}; +mod hotspot; +mod platforms; + +pub use connectivity::{Config, WifiConnectionError}; fn main() -> Result<(), WifiConnectionError> { - let config = Some(Config { - interface: Some("wlo1"), - }); + // let config = Some(Config { + // interface: Some("wlo1"), + // }); - let wifi = WiFi::new("AndroidAPSD22", config)?; + // let wifi = WiFi::new("AndroidAPSD22", config); - match wifi.connect("belm4235") { - Ok(result) => println!( - "{}", - if result == true { - "Connection Successfull." - } else { - "Invalid password." - } - ), - Err(err) => println!("The following error occurred: {:?}", err), - } + // match wifi.connect("belm4235") { + // Ok(result) => println!( + // "{}", + // if result == true { + // "Connection Successfull." + // } else { + // "Invalid password." + // } + // ), + // Err(err) => println!("The following error occurred: {:?}", err), + // } Ok(()) }