Merge branch 'work/whiting/wifidebug' into 'master'

wifidebug: Add some iwd override and a way to enable/disable.

See merge request holo/steamos-manager!7
This commit is contained in:
Jeremy Whiting 2024-02-07 23:06:37 +00:00
commit 29eb97128b
3 changed files with 328 additions and 28 deletions

View file

@ -182,6 +182,29 @@
<arg type="h" name="descriptor" direction="out"/> <arg type="h" name="descriptor" direction="out"/>
</method> </method>
<!--
SetWifiDebugMode:
@mode: 0 for off, 1 for on. Uses an integer for flexibility.
@buffer_size: The ring buffer size in kilobytes per cpu.
In order to have the buffer written out to disk (at /var/log/wifitrace.dat)
set debug mode back to off after some time.
-->
<method name="SetWifiDebugMode">
<arg type="u" name="mode" direction="in"/>
<arg type="u" name="buffer_size" direction="in"/>
</method>
<!--
GetWifiDebugMode:
@mode: 0 for off, 1 for on. Uses an integer for fleximility.
Get method for SetWifiDebugMode.
-->
<method name="GetWifiDebugMode">
<arg type="u" name="mode" direction="out"/>
</method>
</interface> </interface>
</node> </node>

View file

@ -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 // 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 // level things. It implements com.steampowered.SteamOSManager1 interface
let manager = manager::SMManager {}; let manager = manager::SMManager::default();
let _system_connection = ConnectionBuilder::system()? let _system_connection = ConnectionBuilder::system()?
.name("com.steampowered.SteamOSManager1")? .name("com.steampowered.SteamOSManager1")?

View file

@ -23,36 +23,112 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
use std::{ use std::{ fs, ffi::OsStr, fmt, os::fd::{FromRawFd, IntoRawFd} };
ffi::OsStr,
os::fd::{FromRawFd, IntoRawFd},
};
use tokio::{fs::File, io::AsyncWriteExt, process::Command}; use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use zbus::zvariant::OwnedFd; use zbus::zvariant::OwnedFd;
use zbus_macros::dbus_interface; use zbus_macros::dbus_interface;
pub struct SMManager {}
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(u32)]
enum WifiDebugMode {
Off,
On,
}
impl TryFrom<u32> for WifiDebugMode {
type Error = &'static str;
fn try_from(v: u32) -> Result<Self, Self::Error>
{
match v {
x if x == WifiDebugMode::Off as u32 => Ok(WifiDebugMode::Off),
x if x == WifiDebugMode::On as u32 => Ok(WifiDebugMode::On),
_ => { Err("No enum match for value {v}") },
}
}
}
impl fmt::Display for WifiDebugMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WifiDebugMode::Off => write!(f, "Off"),
WifiDebugMode::On => write!(f, "On"),
}
}
}
pub struct SMManager {
wifi_debug_mode: WifiDebugMode,
// Whether we should use trace-cmd or not.
// True on galileo devices, false otherwise
should_trace: bool,
}
impl SMManager
{
pub fn new() -> Self
{
SMManager {
wifi_debug_mode: WifiDebugMode::Off,
should_trace: is_galileo().unwrap(),
}
}
}
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";
// Only use one path for output for now. If needed we can add a timestamp later
// to have multiple files, etc.
const OUTPUT_FILE: &str = "/var/log/wifitrace.dat";
const MIN_BUFFER_SIZE: u32 = 100;
const BOARD_NAME_PATH: &str = "/sys/class/dmi/id/board_name";
const GALILEO_NAME: &str = "Galileo";
fn is_galileo() -> std::io::Result<bool>
{
let mut board_name = fs::read_to_string(BOARD_NAME_PATH)?;
board_name = board_name.trim().to_string();
let matches = board_name == GALILEO_NAME;
Ok(matches)
}
async fn script_exit_code( async fn script_exit_code(
executable: &str, executable: &str,
args: &[impl AsRef<OsStr>], args: &[impl AsRef<OsStr>],
) -> Result<bool, Box<dyn std::error::Error>> { ) -> std::io::Result<bool> {
// Run given script and return true on success // Run given script and return true on success
let mut child = Command::new(executable) let mut child = Command::new(executable)
.args(args) .args(args)
.spawn() .spawn()?;
.expect("Failed to spawn {executable}");
let status = child.wait().await?; let status = child.wait().await?;
Ok(status.success()) Ok(status.success())
} }
async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> bool { async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> std::io::Result<bool> {
// Run given script to get exit code and return true on success. // Run given script to get exit code and return true on success.
// Return false on failure, but also print an error if needed // Return false on failure, but also print an error if needed
match script_exit_code(executable, args).await { match script_exit_code(executable, args).await {
Ok(value) => value, Ok(value) => Ok(value),
Err(err) => { Err(err) => {
println!("Error running {} {}", name, err); println!("Error running {} {}", name, err);
false Err(err)
} }
} }
} }
@ -73,6 +149,68 @@ async fn script_output(
Ok(s.to_string()) 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_all(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 restart_iwd() -> std::io::Result<bool>
{
// First reload systemd since we modified the config most likely
// othorwise we wouldn't be restarting iwd.
match run_script("reload systemd", "systemctl", &["daemon-reload"]).await {
Ok(value) => {
if value {
// worked, now restart iwd
run_script("restart iwd", "systemctl", &["restart", "iwd"]).await
} else {
// reload failed
println!("restart_iwd: reload systemd failed somehow");
Ok(false)
}
},
Err(message) => {
println!("restart_iwd: reload systemd got an error {message}");
Err(message)
}
}
}
async fn stop_tracing(should_trace: bool) -> std::io::Result<bool>
{
if !should_trace {
return Ok(true);
}
// Stop tracing and extract ring buffer to disk for capture
run_script("stop tracing", "trace-cmd", &["stop"]).await?;
// stop tracing worked
run_script("extract traces", "trace-cmd", &["extract", "-o", OUTPUT_FILE]).await
}
async fn start_tracing(buffer_size:u32, should_trace: bool) -> std::io::Result<bool>
{
if !should_trace {
return Ok(true);
}
// Start tracing
let size_str = format!("{}", buffer_size);
run_script("start tracing", "trace-cmd", &["start", "-e", "ath11k_wmi_diag", "-b", &size_str]).await
}
#[dbus_interface(name = "com.steampowered.SteamOSManager1")] #[dbus_interface(name = "com.steampowered.SteamOSManager1")]
impl SMManager { impl SMManager {
const API_VERSION: u32 = 1; const API_VERSION: u32 = 1;
@ -83,42 +221,57 @@ impl SMManager {
async fn factory_reset(&self) -> bool { async fn factory_reset(&self) -> bool {
// Run steamos factory reset script and return true on success // Run steamos factory reset script and return true on success
run_script("factory reset", "steamos-factory-reset-config", &[""]).await match run_script("factory reset", "steamos-factory-reset-config", &[""]).await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn disable_wifi_power_management(&self) -> bool { async fn disable_wifi_power_management(&self) -> bool {
// Run polkit helper script and return true on success // Run polkit helper script and return true on success
run_script( match run_script(
"disable wifi power management", "disable wifi power management",
"/usr/bin/steamos-polkit-helpers/steamos-disable-wireless-power-management", "/usr/bin/steamos-polkit-helpers/steamos-disable-wireless-power-management",
&[""], &[""],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn enable_fan_control(&self, enable: bool) -> bool { async fn enable_fan_control(&self, enable: bool) -> bool {
// Run what steamos-polkit-helpers/jupiter-fan-control does // Run what steamos-polkit-helpers/jupiter-fan-control does
if enable { if enable {
run_script( match run_script(
"enable fan control", "enable fan control",
"systemcltl", "systemcltl",
&["start", "jupiter-fan-control-service"], &["start", "jupiter-fan-control-service"],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} else { } else {
run_script( match run_script(
"disable fan control", "disable fan control",
"systemctl", "systemctl",
&["stop", "jupiter-fan-control.service"], &["stop", "jupiter-fan-control.service"],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
} }
async fn hardware_check_support(&self) -> bool { async fn hardware_check_support(&self) -> bool {
// Run jupiter-check-support note this script does exit 1 for "Support: No" case // Run jupiter-check-support note this script does exit 1 for "Support: No" case
// so no need to parse output, etc. // so no need to parse output, etc.
run_script("check hardware support", "jupiter-check-support", &[""]).await match run_script("check hardware support", "jupiter-check-support", &[""]).await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn read_als_calibration(&self) -> f32 { async fn read_als_calibration(&self) -> f32 {
@ -140,45 +293,57 @@ impl SMManager {
async fn update_bios(&self) -> bool { async fn update_bios(&self) -> bool {
// Update the bios as needed // Update the bios as needed
// Return true if the script was successful (though that might mean no update was needed), false otherwise // Return true if the script was successful (though that might mean no update was needed), false otherwise
run_script( match run_script(
"update bios", "update bios",
"/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate", "/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate",
&["--auto"], &["--auto"],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn update_dock(&self) -> bool { async fn update_dock(&self) -> bool {
// Update the dock firmware as needed // Update the dock firmware as needed
// Retur true if successful, false otherwise // Retur true if successful, false otherwise
run_script( match run_script(
"update dock firmware", "update dock firmware",
"/usr/bin/steamos-polkit-helpers/jupiter-dock-updater", "/usr/bin/steamos-polkit-helpers/jupiter-dock-updater",
&[""], &[""],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn trim_devices(&self) -> bool { async fn trim_devices(&self) -> bool {
// Run steamos-trim-devices script // Run steamos-trim-devices script
// return true on success, false otherwise // return true on success, false otherwise
run_script( match run_script(
"trim devices", "trim devices",
"/usr/bin/steamos-polkit-helpers/steamos-trim-devices", "/usr/bin/steamos-polkit-helpers/steamos-trim-devices",
&[""], &[""],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn format_sdcard(&self) -> bool { async fn format_sdcard(&self) -> bool {
// Run steamos-format-sdcard script // Run steamos-format-sdcard script
// return true on success, false otherwise // return true on success, false otherwise
run_script( match run_script(
"format sdcard", "format sdcard",
"/usr/bin/steamos-polkit-helpers/steamos-format-sdcard", "/usr/bin/steamos-polkit-helpers/steamos-format-sdcard",
&[""], &[""],
) )
.await .await {
Ok(value) => { value },
Err(_) => { false }
}
} }
async fn set_gpu_performance_level(&self, level: i32) -> bool { async fn set_gpu_performance_level(&self, level: i32) -> bool {
@ -333,6 +498,118 @@ impl SMManager {
} }
} }
async fn get_wifi_debug_mode(&mut self) -> u32 {
// Get the wifi debug mode
self.wifi_debug_mode as u32
}
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
let wanted_mode = WifiDebugMode::try_from(mode);
match wanted_mode {
Ok(WifiDebugMode::Off) => {
// If mode is 0 disable wifi debug mode
// Stop any existing trace and flush to disk.
match stop_tracing(self.should_trace).await {
Ok(result) => {
if result {
// Stop_tracing was successful
match setup_iwd_config(false).await {
Ok(_) => {
// setup_iwd_config false worked
match restart_iwd().await {
Ok(value) => {
if value {
// restart iwd worked
self.wifi_debug_mode = WifiDebugMode::Off;
} else {
// restart_iwd failed
println!("restart_iwd failed somehow, check log above");
return false;
}
},
Err(message) => {
println!("restart_iwd got an error {message}");
return false;
}
}
},
Err(message) => {
println!("setup_iwd_config false got an error somehow {message}");
return false;
}
}
} else {
println!("stop_tracing command failed somehow, bailing");
return false;
}
},
Err(message) => {
println!("stop_tracing command had an error {message}");
return false;
}
}
},
Ok(WifiDebugMode::On) => {
// If mode is 1 enable wifi debug mode
if buffer_size < MIN_BUFFER_SIZE {
return false;
}
match setup_iwd_config(true).await {
Ok(_) => {
// setup_iwd_config worked
match restart_iwd().await {
Ok(value) => {
if value {
// restart_iwd worked
match start_tracing(buffer_size, self.should_trace).await {
Ok(value) => {
if value {
// start_tracing worked
self.wifi_debug_mode = WifiDebugMode::On;
} else {
// start_tracing failed
println!("start_tracing failed somehow");
return false;
}
},
Err(message) => {
println!("start_tracing got an error {message}");
return false;
}
}
} else {
println!("restart_iwd failed somehow");
return false;
}
},
Err(message) => {
println!("restart_iwd got an error {message}");
return false;
}
}
},
Err(message) => {
println!("setup_iwd_config true got an error somehow {message}");
return false;
}
}
},
Err(_) => {
// Invalid mode requested, more coming later, but add this catch-all for now
println!("Invalid wifi debug mode {mode} requested");
return false;
},
}
true
}
/// A version property. /// A version property.
#[dbus_interface(property)] #[dbus_interface(property)]
async fn version(&self) -> u32 { async fn version(&self) -> u32 {