Skip to content

Commit

Permalink
Paring wifi devices from /etc/config/wireless instead of /proc/net/wi…
Browse files Browse the repository at this point in the history
…reless
  • Loading branch information
Pranay Tulugu committed Sep 21, 2023
1 parent b12c903 commit 3fd39c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
35 changes: 25 additions & 10 deletions althea_kernel_interface/src/hardware_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::io::BufRead;
use std::io::BufReader;
use std::process::Command;
use std::process::Stdio;
use std::str::from_utf8;
use std::time::Duration;
use std::u64;

Expand Down Expand Up @@ -401,19 +402,33 @@ fn get_conntrack_info() -> Option<ConntrackInfo> {
Some(ret)
}

/// Device names are in the form wlan0, wlan1 etc
fn parse_wifi_device_names() -> Result<Vec<String>, Error> {
let mut ret = Vec::new();
let path = "/proc/net/wireless";
let lines = get_lines(path)?;
for line in lines {
if line.contains(':') {
let name: Vec<&str> = line.split(':').collect();
let name = name[0];
let name = name.replace(' ', "");
ret.push(name.to_string());
// We parse /etc/config/wireless which is an openwrt config. We return an error if not openwrt
if KI.is_openwrt() {
let mut ret = Vec::new();

let lines = KI.run_command("uci", &["show", "wireless"])?;
let lines: Vec<&str> = from_utf8(&lines.stdout)?.lines().collect();

// trying to get lines 'wireless.default_radio1.ifname='wlan1''
for line in lines {
if line.contains("wireless.default_radio") && line.contains("ifname") {
let name = match line.split('=').collect::<Vec<&str>>().last() {
Some(a) => *a,
None => {
error!("Cannot parse wifi string {}", line);
continue;
}
};
let name = name.replace('\'', "");
ret.push(name)
}
}
Ok(ret)
} else {
Err(Error::NotOpenwrt)
}
Ok(ret)
}

fn get_wifi_survey_info(dev: &str) -> Vec<WifiSurveyData> {
Expand Down
4 changes: 4 additions & 0 deletions althea_kernel_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub enum KernelInterfaceError {
FailedToGetSystemTime,
FailedToGetSystemKernelVersion,
ParseError(String),
NotOpenwrt,
}

impl fmt::Display for KernelInterfaceError {
Expand Down Expand Up @@ -134,6 +135,9 @@ impl fmt::Display for KernelInterfaceError {
KernelInterfaceError::FailedToGetSystemKernelVersion => {
write!(f, "Failed to get system kernel version!")
}
KernelInterfaceError::NotOpenwrt => {
write!(f, "This is not an Openwrt device!")
}
}
}
}
Expand Down

0 comments on commit 3fd39c8

Please sign in to comment.