From 3fd39c8cfe4c5781952fa85bc3f643ab98551a08 Mon Sep 17 00:00:00 2001 From: Pranay Tulugu Date: Thu, 21 Sep 2023 12:33:11 -0700 Subject: [PATCH] Paring wifi devices from /etc/config/wireless instead of /proc/net/wireless --- althea_kernel_interface/src/hardware_info.rs | 35 ++++++++++++++------ althea_kernel_interface/src/lib.rs | 4 +++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/althea_kernel_interface/src/hardware_info.rs b/althea_kernel_interface/src/hardware_info.rs index 5e6096dd4..44a204996 100644 --- a/althea_kernel_interface/src/hardware_info.rs +++ b/althea_kernel_interface/src/hardware_info.rs @@ -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; @@ -401,19 +402,33 @@ fn get_conntrack_info() -> Option { Some(ret) } +/// Device names are in the form wlan0, wlan1 etc fn parse_wifi_device_names() -> Result, 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::>().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 { diff --git a/althea_kernel_interface/src/lib.rs b/althea_kernel_interface/src/lib.rs index a292e9ab9..e019bd999 100644 --- a/althea_kernel_interface/src/lib.rs +++ b/althea_kernel_interface/src/lib.rs @@ -92,6 +92,7 @@ pub enum KernelInterfaceError { FailedToGetSystemTime, FailedToGetSystemKernelVersion, ParseError(String), + NotOpenwrt, } impl fmt::Display for KernelInterfaceError { @@ -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!") + } } } }