Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/connectivity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{fmt, io};
use crate::platforms::WifiError;

#[cfg(target_os = "windows")]
mod handlers;
mod providers;
#[cfg(target_os = "windows")]
mod stubs;

use crate::platforms::WifiError;
use std::{fmt, io};

/// Wireless network connectivity functionality.
pub trait Connectivity: fmt::Debug {
Expand All @@ -14,6 +15,10 @@ pub trait Connectivity: fmt::Debug {

/// Disconnects from a wireless network currently connected to.
fn disconnect(&self) -> Result<bool, WifiConnectionError>;

// Determines speed when connected to a network
// Adding unimplemented block
fn speed(&self) -> Result<String, WifiConnectionError> { unimplemented!(); }
}

/// Error that occurs when attempting to connect to a wireless network.
Expand Down
26 changes: 25 additions & 1 deletion src/connectivity/providers/linux.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::connectivity::{Connectivity, WifiConnectionError};
use crate::platforms::{Connection, WiFi, WifiError, WifiInterface};
use std::process::Command;
use std::process::{Command, Stdio};
use std::str;
use std::format;

/// Wireless network connectivity functionality.
impl Connectivity for WiFi {
Expand Down Expand Up @@ -51,4 +53,26 @@ impl Connectivity for WiFi {
.as_ref()
.contains("disconnect"))
}

fn speed(&self) -> Result<String, WifiConnectionError> {
let nmcli_speed = Command::new("nmcli")
.arg("dev")
.arg("wifi")
.arg("list")
.stdout(Stdio::piped())
.spawn()
.unwrap();

let piped_awk = Command::new("awk")
.arg(r"/\*/{if (NR!=1) {print $6}}")
.stdin(Stdio::from(nmcli_speed.stdout.unwrap())) // Pipe through.
.stdout(Stdio::piped())
.spawn()
.unwrap();
Comment on lines +66 to +71
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be parsed manually. We'll want to reduce as much dependency as possible on external programs.

In the future, I plan to remove dependency on nmcli for a similar reason.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, when parsing manually, could you add test cases to verify we're parsing the output of nmcli correctly?


let result = piped_awk.wait_with_output().unwrap();
let result = str::from_utf8(&result.stdout).unwrap().to_string();
let formatted_result = format!("{} Mbit/s", result.trim());
Ok(formatted_result)
}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ mod connectivity;
mod hotspot;
mod platforms;

use std::env::args;
use crate::connectivity::{Connectivity, WifiConnectionError};
use crate::platforms::{Config, WiFi};

use std::process::{Command, Stdio};

fn main() -> Result<(), WifiConnectionError> {
let config = Some(Config {
interface: Some("wlo1"),
Expand All @@ -24,5 +27,8 @@ fn main() -> Result<(), WifiConnectionError> {
Err(err) => println!("The following error occurred: {:?}", err),
}

let speed = wifi.speed().unwrap();
println!("Speed is: {}", speed);

Ok(())
}