Feat(WifiStatus)): Check Fi Status

This commit is contained in:
Tochukwu Nkemdilim
2018-07-03 17:02:37 +01:00
parent 96c16e70a9
commit c6e4d7359f
8 changed files with 112 additions and 4 deletions
+2
View File
@@ -9,6 +9,7 @@ pub trait Network: fmt::Debug {
/// Makes an attempt to connect to a selected wireless network with password specified.
fn connect(&self, password: &str) -> Result<bool, NetworkError>;
fn disconnect(&self) -> Result<bool, NetworkError>;
fn is_wifi_enabled(&self) -> bool;
}
// #[derive(Debug)]
@@ -33,6 +34,7 @@ pub enum NetworkError {
IoError(io::Error),
FailedToConnect(String),
FailedToDisconnect(String),
WiFiInterfaceDisabled,
}
impl From<io::Error> for NetworkError {
+5 -1
View File
@@ -14,7 +14,7 @@ impl ProfileNetwork {
let handler = Machine::new(
name,
config.map_or(None, |cfg| cfg.interface.map_or(None, |x| Some(&x))),
config.map_or(None, |cfg| cfg.interface.map_or(None, |x| Some(x))),
);
Ok(ProfileNetwork {
@@ -23,6 +23,10 @@ impl ProfileNetwork {
}
pub fn connect(&self, password: &str) -> Result<bool, NetworkError> {
if !self.handler.is_wifi_enabled() {
return Err(NetworkError::WiFiInterfaceDisabled);
}
self.handler.connect(password)
}
}
+13
View File
@@ -47,4 +47,17 @@ 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")
}
}
+14
View File
@@ -69,4 +69,18 @@ impl Network for Windows {
.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;
// }
// match String::from_utf8_lossy(&output.unwrap().stdout).as_ref() {
// "enabled" => true,
// _ => false,
// }
false
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ fn main() -> Result<(), NetworkError> {
interface: Some("wlo1"),
});
let wifi = WiFi::new("AndroidAPSD", config)?;
let wifi = WiFi::new("AndroidAPSD22", config)?;
match wifi.connect("belm4235") {
Ok(result) => println!(