From cd241445b45cf2d8bea9301f15e099e4b01c5b50 Mon Sep 17 00:00:00 2001 From: Jeremy Whiting Date: Tue, 23 Jan 2024 10:48:31 -0700 Subject: [PATCH] wifidebug: Add some iwd override and a way to enable/disable. Adds set_wifi_debug_mode to enable or disable wifi debug mode. Parameters are mode (0 for off, 1 for on for now, expandable in the future if more modes are needed). buffer_size: a size in kilobytes per cpu (so 16 on the deck) for the ring buffer that debug messages will get written into. For now just adds or removes a iwd debug mode override and restarts iwd. next will add some trace-cmd commands to write the debug messages to a file on disk. Signed-off-by: Jeremy Whiting --- src/main.rs | 2 +- src/manager.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5c2ec4..bed6c57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ async fn main() -> Result<()> { // This daemon is responsible for creating a dbus api that steam client can use to do various OS // level things. It implements com.steampowered.SteamOSManager1 interface - let manager = manager::SMManager {}; + let manager = manager::SMManager::new(); let _system_connection = ConnectionBuilder::system()? .name("com.steampowered.SteamOSManager1")? diff --git a/src/manager.rs b/src/manager.rs index ea78a17..3840fcf 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -30,7 +30,35 @@ use std::{ use tokio::{fs::File, io::AsyncWriteExt, process::Command}; use zbus::zvariant::OwnedFd; use zbus_macros::dbus_interface; -pub struct SMManager {} + +pub struct SMManager { + wifi_debug_mode: u32, +} + +impl SMManager +{ + pub fn new() -> Self + { + SMManager { wifi_debug_mode: 0 } + } +} + +impl Default for SMManager +{ + fn default() -> Self + { + SMManager::new() + } + +} + +const OVERRIDE_CONTENTS: &str = +"[Service] +ExecStart= +ExecStart=/usr/lib/iwd/iwd -d +"; +const OVERRIDE_FOLDER: &str = "/etc/systemd/system/iwd.service.d"; +const OVERRIDE_PATH: &str = "/etc/systemd/system/iwd.service.d/override.conf"; async fn script_exit_code( executable: &str, @@ -73,6 +101,35 @@ async fn script_output( Ok(s.to_string()) } +async fn setup_iwd_config(want_override: bool) -> Result<(), std::io::Error> +{ + // Copy override.conf file into place or out of place depending + // on install value + + if want_override { + // Copy it in + // Make sure the folder exists + tokio::fs::create_dir(OVERRIDE_FOLDER).await?; + // Then write the contents into the file + tokio::fs::write(OVERRIDE_PATH, OVERRIDE_CONTENTS).await + } else { + // Delete it + tokio::fs::remove_file(OVERRIDE_PATH).await + } +} + +async fn reload_systemd() -> bool +{ + // Reload systemd so it will see our add or removal of changed files + run_script("reload systemd", "systemctl", &["daemon-reload"]).await +} + +async fn restart_iwd() -> bool +{ + // Restart the iwd service by running "systemctl restart iwd" + run_script("restart iwd", "systemctl", &["restart", "iwd"]).await +} + #[dbus_interface(name = "com.steampowered.SteamOSManager1")] impl SMManager { const API_VERSION: u32 = 1; @@ -333,6 +390,32 @@ impl SMManager { } } + async fn set_wifi_debug_mode(&mut self, mode: u32, buffer_size: u32) -> bool { + // 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 + + // If mode is 0 disable wifi debug mode + if mode == 0 { + let _ = setup_iwd_config(false).await; + reload_systemd().await; + restart_iwd().await; + } + // If mode is 1 enable wifi debug mode + else if mode == 1 { + let _ = setup_iwd_config(true).await; + reload_systemd().await; + restart_iwd().await; + } + else { + // Invalid mode requested, more coming later, but add this catch-all for now + println!("Invalid wifi debug mode {mode} requested"); + } + self.wifi_debug_mode = mode; + + true + } + /// A version property. #[dbus_interface(property)] async fn version(&self) -> u32 {