manager: Clean up non-idiomatic code

This commit is contained in:
Vicki Pfau 2024-03-19 16:38:16 -07:00
parent cf416c8a56
commit 2e55bc2330
2 changed files with 45 additions and 88 deletions

View file

@ -23,7 +23,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
use zbus::{ConnectionBuilder, Result}; use anyhow::Result;
use zbus::ConnectionBuilder;
pub mod manager; pub mod manager;
@ -32,7 +33,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::default(); let manager = manager::SMManager::new()?;
let _system_connection = ConnectionBuilder::system()? let _system_connection = ConnectionBuilder::system()?
.name("com.steampowered.SteamOSManager1")? .name("com.steampowered.SteamOSManager1")?

View file

@ -63,17 +63,11 @@ pub struct SMManager {
} }
impl SMManager { impl SMManager {
pub fn new() -> Self { pub fn new() -> Result<Self> {
SMManager { Ok(SMManager {
wifi_debug_mode: WifiDebugMode::Off, wifi_debug_mode: WifiDebugMode::Off,
should_trace: is_galileo().unwrap(), should_trace: is_galileo()?,
} })
}
}
impl Default for SMManager {
fn default() -> Self {
SMManager::new()
} }
} }
@ -106,26 +100,15 @@ async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Resul
Ok(status.success()) Ok(status.success())
} }
async fn run_script( async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool> {
name: &str,
executable: &str,
args: &[impl AsRef<OsStr>],
) -> 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 { script_exit_code(executable, args)
Ok(value) => Ok(value), .await
Err(err) => { .inspect_err(|message| println!("Error running {name} {message}"))
println!("Error running {} {}", name, err);
Err(err)
}
}
} }
async fn script_output( async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
executable: &str,
args: &[impl AsRef<OsStr>],
) -> Result<String> {
// Run given command and return the output given // Run given command and return the output given
let output = Command::new(executable).args(args).output(); let output = Command::new(executable).args(args).output();
@ -213,60 +196,49 @@ 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
match run_script("factory reset", "steamos-factory-reset-config", &[""]).await { run_script("factory reset", "steamos-factory-reset-config", &[""])
Ok(value) => value, .await
Err(_) => false, .unwrap_or(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
match run_script( 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
{ .unwrap_or(false)
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 {
match run_script( run_script(
"enable fan control", "enable fan control",
"systemcltl", "systemcltl",
&["start", "jupiter-fan-control-service"], &["start", "jupiter-fan-control-service"],
) )
.await .await
{ .unwrap_or(false)
Ok(value) => value,
Err(_) => false,
}
} else { } else {
match run_script( run_script(
"disable fan control", "disable fan control",
"systemctl", "systemctl",
&["stop", "jupiter-fan-control.service"], &["stop", "jupiter-fan-control.service"],
) )
.await .await
{ .unwrap_or(false)
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.
match run_script("check hardware support", "jupiter-check-support", &[""]).await { run_script("check hardware support", "jupiter-check-support", &[""])
Ok(value) => value, .await
Err(_) => false, .unwrap_or(false)
}
} }
async fn read_als_calibration(&self) -> f32 { async fn read_als_calibration(&self) -> f32 {
@ -276,73 +248,61 @@ impl SMManager {
&[""], &[""],
) )
.await; .await;
let mut value: f32 = -1.0;
match result { match result {
Ok(as_string) => value = as_string.trim().parse().unwrap(), Ok(as_string) => as_string.trim().parse().unwrap_or(-1.0),
Err(message) => println!("Unable to run als calibration script : {}", message), Err(message) => {
println!("Unable to run als calibration script: {}", message);
-1.0
}
} }
value
} }
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
match run_script( run_script(
"update bios", "update bios",
"/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate", "/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate",
&["--auto"], &["--auto"],
) )
.await .await
{ .unwrap_or(false)
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
match run_script( 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
{ .unwrap_or(false)
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
match run_script( run_script(
"trim devices", "trim devices",
"/usr/bin/steamos-polkit-helpers/steamos-trim-devices", "/usr/bin/steamos-polkit-helpers/steamos-trim-devices",
&[""], &[""],
) )
.await .await
{ .unwrap_or(false)
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
match run_script( run_script(
"format sdcard", "format sdcard",
"/usr/bin/steamos-polkit-helpers/steamos-format-sdcard", "/usr/bin/steamos-polkit-helpers/steamos-format-sdcard",
&[""], &[""],
) )
.await .await
{ .unwrap_or(false)
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 {
@ -357,9 +317,8 @@ impl SMManager {
// Open sysfs file // Open sysfs file
let result = let result =
File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level").await; File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level").await;
let mut myfile; let mut myfile = match result {
match result { Ok(f) => f,
Ok(f) => myfile = f,
Err(message) => { Err(message) => {
println!("Error opening sysfs file for writing {message}"); println!("Error opening sysfs file for writing {message}");
return false; return false;
@ -386,9 +345,8 @@ impl SMManager {
} }
let result = File::create("/sys/class/drm/card0/device/pp_od_clk_voltage").await; let result = File::create("/sys/class/drm/card0/device/pp_od_clk_voltage").await;
let mut myfile; let mut myfile = match result {
match result { Ok(f) => f,
Ok(f) => myfile = f,
Err(message) => { Err(message) => {
println!("Error opening sysfs file for writing {message}"); println!("Error opening sysfs file for writing {message}");
return false; return false;
@ -435,9 +393,8 @@ impl SMManager {
} }
let result = File::create("/sys/class/hwmon/hwmon5/power1_cap").await; let result = File::create("/sys/class/hwmon/hwmon5/power1_cap").await;
let mut power1file; let mut power1file = match result {
match result { Ok(f) => f,
Ok(f) => power1file = f,
Err(message) => { Err(message) => {
println!("Error opening sysfs power1_cap file for writing TDP limits {message}"); println!("Error opening sysfs power1_cap file for writing TDP limits {message}");
return false; return false;
@ -445,9 +402,8 @@ impl SMManager {
}; };
let result = File::create("/sys/class/hwmon/hwmon5/power2_cap").await; let result = File::create("/sys/class/hwmon/hwmon5/power2_cap").await;
let mut power2file; let mut power2file = match result {
match result { Ok(f) => f,
Ok(f) => power2file = f,
Err(message) => { Err(message) => {
println!("Error opening sysfs power2_cap file for wtriting TDP limits {message}"); println!("Error opening sysfs power2_cap file for wtriting TDP limits {message}");
return false; return false;