From f3d8e9763315288bba0b754a07b5bcd2db6cd743 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 15 Apr 2024 19:43:23 -0700 Subject: [PATCH] wifi: Move more logic out of the manager --- src/manager.rs | 37 ++++++------------------------------- src/wifi.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/manager.rs b/src/manager.rs index d609554..3123b27 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -21,8 +21,8 @@ use crate::power::{ use crate::process::{run_script, script_output}; use crate::systemd::SystemdUnit; use crate::wifi::{ - get_wifi_backend, set_wifi_backend, set_wifi_debug_mode, WifiBackend, WifiDebugMode, - WifiPowerManagement, + get_wifi_backend, get_wifi_power_management_state, set_wifi_backend, set_wifi_debug_mode, + set_wifi_power_management_state, WifiBackend, WifiDebugMode, WifiPowerManagement, }; use crate::{anyhow_to_zbus, anyhow_to_zbus_fdo}; @@ -95,19 +95,10 @@ impl SteamOSManager { #[zbus(property(emits_changed_signal = "false"))] async fn wifi_power_management_state(&self) -> zbus::fdo::Result { - let output = script_output("/usr/bin/iwconfig", &["wlan0"]) - .await - .map_err(anyhow_to_zbus_fdo)?; - for line in output.lines() { - return Ok(match line.trim() { - "Power Management:on" => WifiPowerManagement::Enabled as u32, - "Power Management:off" => WifiPowerManagement::Disabled as u32, - _ => continue, - }); + match get_wifi_power_management_state().await { + Ok(state) => Ok(state as u32), + Err(e) => Err(anyhow_to_zbus_fdo(e)), } - Err(zbus::fdo::Error::Failed(String::from( - "Failed to query power management state", - ))) } #[zbus(property)] @@ -116,14 +107,8 @@ impl SteamOSManager { Ok(state) => state, Err(err) => return Err(zbus::fdo::Error::InvalidArgs(err.to_string()).into()), }; - let state = match state { - WifiPowerManagement::Disabled => "off", - WifiPowerManagement::Enabled => "on", - }; - - run_script("/usr/bin/iwconfig", &["wlan0", "power", state]) + set_wifi_power_management_state(state) .await - .inspect_err(|message| error!("Error setting wifi power management state: {message}")) .map_err(anyhow_to_zbus) } @@ -329,16 +314,6 @@ impl SteamOSManager { // Set the wifi debug mode to mode, using an int for flexibility going forward but only // doing things on 0 or 1 for now // Return false on error - match get_wifi_backend().await { - Ok(WifiBackend::Iwd) => (), - Ok(backend) => { - return Err(zbus::fdo::Error::Failed(format!( - "Setting wifi debug mode not supported when backend is {backend}", - ))); - } - Err(e) => return Err(anyhow_to_zbus_fdo(e)), - } - let wanted_mode = match WifiDebugMode::try_from(mode) { Ok(mode) => mode, Err(e) => return Err(zbus::fdo::Error::InvalidArgs(e.to_string())), diff --git a/src/wifi.rs b/src/wifi.rs index 6e08bc8..8b4eb69 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -13,7 +13,7 @@ use tracing::error; use zbus::Connection; use crate::path; -use crate::process::run_script; +use crate::process::{run_script, script_output}; use crate::systemd::{daemon_reload, SystemdUnit}; const OVERRIDE_CONTENTS: &str = "[Service] @@ -180,9 +180,11 @@ pub async fn set_wifi_debug_mode( should_trace: bool, connection: Connection, ) -> Result<()> { - // Set the wifi debug mode to mode, using an int for flexibility going forward but only - // doing things on 0 or 1 for now - // Return false on error + match get_wifi_backend().await { + Ok(WifiBackend::Iwd) => (), + Ok(backend) => bail!("Setting wifi debug mode not supported when backend is {backend}"), + Err(e) => return Err(e), + } match mode { WifiDebugMode::Off => { @@ -242,6 +244,29 @@ pub async fn set_wifi_backend(backend: WifiBackend) -> Result<()> { run_script("/usr/bin/steamos-wifi-set-backend", &[backend.to_string()]).await } +pub async fn get_wifi_power_management_state() -> Result { + let output = script_output("/usr/bin/iwconfig", &["wlan0"]).await?; + for line in output.lines() { + return Ok(match line.trim() { + "Power Management:on" => WifiPowerManagement::Enabled, + "Power Management:off" => WifiPowerManagement::Disabled, + _ => continue, + }); + } + bail!("Failed to query power management state") +} + +pub async fn set_wifi_power_management_state(state: WifiPowerManagement) -> Result<()> { + let state = match state { + WifiPowerManagement::Disabled => "off", + WifiPowerManagement::Enabled => "on", + }; + + run_script("/usr/bin/iwconfig", &["wlan0", "power", state]) + .await + .inspect_err(|message| error!("Error setting wifi power management state: {message}")) +} + #[cfg(test)] mod test { use super::*;