mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-13 09:52:00 -04:00
wifi: Move more logic out of the manager
This commit is contained in:
parent
5b612fb7a2
commit
f3d8e97633
2 changed files with 35 additions and 35 deletions
|
@ -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<u32> {
|
||||
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())),
|
||||
|
|
33
src/wifi.rs
33
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<WifiPowerManagement> {
|
||||
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::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue