manager: Implement new API

This commit is contained in:
Vicki Pfau 2024-03-28 16:23:31 -07:00
parent 6a5e693e5b
commit 69e6477053
5 changed files with 419 additions and 196 deletions

View file

@ -5,7 +5,7 @@
* SPDX-License-Identifier: MIT
*/
use anyhow::Result;
use anyhow::{bail, ensure, Result};
use std::fmt;
use tokio::fs;
use tracing::error;
@ -24,11 +24,14 @@ const OVERRIDE_PATH: &str = "/etc/systemd/system/iwd.service.d/override.conf";
const OUTPUT_FILE: &str = "/var/log/wifitrace.dat";
const TRACE_CMD_PATH: &str = "/usr/bin/trace-cmd";
const MIN_BUFFER_SIZE: u32 = 100;
#[derive(PartialEq, Debug, Copy, Clone)]
#[repr(u32)]
pub enum WifiDebugMode {
Off,
On,
UnsupportedFeature = 0,
Off = 1,
On = 2,
}
#[derive(PartialEq, Debug, Copy, Clone)]
@ -43,6 +46,9 @@ impl TryFrom<u32> for WifiDebugMode {
type Error = &'static str;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == WifiDebugMode::UnsupportedFeature as u32 => {
Ok(WifiDebugMode::UnsupportedFeature)
}
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}"),
@ -53,6 +59,7 @@ impl TryFrom<u32> for WifiDebugMode {
impl fmt::Display for WifiDebugMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WifiDebugMode::UnsupportedFeature => write!(f, "Unsupported feature"),
WifiDebugMode::Off => write!(f, "Off"),
WifiDebugMode::On => write!(f, "On"),
}
@ -99,7 +106,7 @@ pub async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> {
}
}
pub async fn restart_iwd() -> Result<bool> {
async fn restart_iwd() -> Result<bool> {
// First reload systemd since we modified the config most likely
// otherwise we wouldn't be restarting iwd.
match run_script("reload systemd", SYSTEMCTL_PATH, &["daemon-reload"]).await {
@ -120,7 +127,7 @@ pub async fn restart_iwd() -> Result<bool> {
}
}
pub async fn stop_tracing() -> Result<bool> {
async fn stop_tracing() -> Result<bool> {
// Stop tracing and extract ring buffer to disk for capture
run_script("stop tracing", TRACE_CMD_PATH, &["stop"]).await?;
// stop tracing worked
@ -132,7 +139,7 @@ pub async fn stop_tracing() -> Result<bool> {
.await
}
pub async fn start_tracing(buffer_size: u32) -> Result<bool> {
async fn start_tracing(buffer_size: u32) -> Result<bool> {
// Start tracing
let size_str = format!("{}", buffer_size);
run_script(
@ -142,3 +149,66 @@ pub async fn start_tracing(buffer_size: u32) -> Result<bool> {
)
.await
}
pub async fn set_wifi_debug_mode(
mode: WifiDebugMode,
buffer_size: u32,
should_trace: bool,
) -> 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 mode {
WifiDebugMode::Off => {
// If mode is 0 disable wifi debug mode
// Stop any existing trace and flush to disk.
if should_trace {
let result = match stop_tracing().await {
Ok(result) => result,
Err(message) => bail!("stop_tracing command got an error: {message}"),
};
ensure!(result, "stop_tracing command returned non-zero");
}
// Stop_tracing was successful
if let Err(message) = setup_iwd_config(false).await {
bail!("setup_iwd_config false got an error: {message}");
}
// setup_iwd_config false worked
let value = match restart_iwd().await {
Ok(value) => value,
Err(message) => {
bail!("restart_iwd got an error: {message}");
}
};
// restart_iwd failed
ensure!(value, "restart_iwd failed, check log above");
}
WifiDebugMode::On => {
ensure!(buffer_size > MIN_BUFFER_SIZE, "Buffer size too small");
if let Err(message) = setup_iwd_config(true).await {
bail!("setup_iwd_config true got an error: {message}");
}
// setup_iwd_config worked
let value = match restart_iwd().await {
Ok(value) => value,
Err(message) => bail!("restart_iwd got an error: {message}"),
};
ensure!(value, "restart_iwd failed");
// restart_iwd worked
if should_trace {
let value = match start_tracing(buffer_size).await {
Ok(value) => value,
Err(message) => bail!("start_tracing got an error: {message}"),
};
ensure!(value, "start_tracing failed");
}
}
mode => {
// Invalid mode requested, more coming later, but add this catch-all for now
bail!("Invalid wifi debug mode {mode} requested");
}
}
Ok(())
}