Run cargo fmt --all.

Fixes code styling in all source files.
This commit is contained in:
Jeremy Whiting 2023-11-15 16:05:54 -07:00
parent bf3cca29ac
commit 0c4b661d16
2 changed files with 135 additions and 60 deletions

View file

@ -23,14 +23,19 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use std::{ffi::OsStr, os::fd::{FromRawFd, IntoRawFd}};
use tokio::{process::Command, fs::File, io::AsyncWriteExt};
use std::{
ffi::OsStr,
os::fd::{FromRawFd, IntoRawFd},
};
use tokio::{fs::File, io::AsyncWriteExt, process::Command};
use zbus::zvariant::OwnedFd;
use zbus_macros::dbus_interface;
pub struct SMManager {
}
pub struct SMManager {}
async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<bool, Box<dyn std::error::Error>> {
async fn script_exit_code(
executable: &str,
args: &[impl AsRef<OsStr>],
) -> Result<bool, Box<dyn std::error::Error>> {
// Run given script and return true on success
let mut child = Command::new(executable)
.args(args)
@ -45,14 +50,19 @@ async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) ->
// Return false on failure, but also print an error if needed
match script_exit_code(executable, args).await {
Ok(value) => value,
Err(err) => { println!("Error running {} {}", name, err); false}
Err(err) => {
println!("Error running {} {}", name, err);
false
}
}
}
async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String, Box<dyn std::error::Error>> {
async fn script_output(
executable: &str,
args: &[impl AsRef<OsStr>],
) -> Result<String, Box<dyn std::error::Error>> {
// Run given command and return the output given
let output = Command::new(executable)
.args(args).output();
let output = Command::new(executable).args(args).output();
let output = output.await?;
@ -67,11 +77,10 @@ async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<S
impl SMManager {
const API_VERSION: u32 = 1;
async fn say_hello(&self, name: &str) -> String {
format!("Hello {}!", name)
}
async fn factory_reset(&self) -> bool {
// Run steamos factory reset script and return true on success
run_script("factory reset", "steamos-factory-reset-config", &[""]).await
@ -79,15 +88,30 @@ impl SMManager {
async fn disable_wifi_power_management(&self) -> bool {
// Run polkit helper script and return true on success
run_script("disable wifi power management", "/usr/bin/steamos-polkit-helpers/steamos-disable-wireless-power-management", &[""]).await
run_script(
"disable wifi power management",
"/usr/bin/steamos-polkit-helpers/steamos-disable-wireless-power-management",
&[""],
)
.await
}
async fn enable_fan_control(&self, enable: bool) -> bool {
// Run what steamos-polkit-helpers/jupiter-fan-control does
if enable {
run_script("enable fan control", "systemcltl", &["start", "jupiter-fan-control-service"]).await
run_script(
"enable fan control",
"systemcltl",
&["start", "jupiter-fan-control-service"],
)
.await
} else {
run_script("disable fan control", "systemctl", &["stop", "jupiter-fan-control.service"]).await
run_script(
"disable fan control",
"systemctl",
&["stop", "jupiter-fan-control.service"],
)
.await
}
}
@ -99,68 +123,93 @@ impl SMManager {
async fn read_als_calibration(&self) -> f32 {
// Run script to get calibration value
let result = script_output("/usr/bin/steamos-polkit-helpers/jupiter-get-als-gain", &[""]).await;
let result = script_output(
"/usr/bin/steamos-polkit-helpers/jupiter-get-als-gain",
&[""],
)
.await;
let mut value: f32 = -1.0;
match result {
Ok(as_string) => value = as_string.trim().parse().unwrap(),
Err(message) => println!("Unable to run als calibration script : {}", message),
}
value
}
async fn update_bios(&self) -> bool {
// Update the bios as needed
// Return true if the script was successful (though that might mean no update was needed), false otherwise
run_script("update bios", "/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate", &["--auto"]).await
run_script(
"update bios",
"/usr/bin/steamos-potlkit-helpers/jupiter-biosupdate",
&["--auto"],
)
.await
}
async fn update_dock(&self) -> bool {
// Update the dock firmware as needed
// Retur true if successful, false otherwise
run_script("update dock firmware", "/usr/bin/steamos-polkit-helpers/jupiter-dock-updater", &[""]).await
run_script(
"update dock firmware",
"/usr/bin/steamos-polkit-helpers/jupiter-dock-updater",
&[""],
)
.await
}
async fn trim_devices(&self) -> bool {
// Run steamos-trim-devices script
// return true on success, false otherwise
run_script("trim devices", "/usr/bin/steamos-polkit-helpers/steamos-trim-devices", &[""]).await
run_script(
"trim devices",
"/usr/bin/steamos-polkit-helpers/steamos-trim-devices",
&[""],
)
.await
}
async fn format_sdcard(&self) -> bool {
// Run steamos-format-sdcard script
// return true on success, false otherwise
run_script("format sdcard", "/usr/bin/steamos-polkit-helpers/steamos-format-sdcard", &[""]).await
run_script(
"format sdcard",
"/usr/bin/steamos-polkit-helpers/steamos-format-sdcard",
&[""],
)
.await
}
async fn set_gpu_performance_level(&self, level: i32) -> bool {
// Set given level to sysfs path /sys/class/drm/card0/device/power_dpm_force_performance_level
// Levels are defined below
// return true if able to write, false otherwise or if level is out of range, etc.
let levels = [
"auto",
"low",
"high",
"manual",
"peak_performance"
];
let levels = ["auto", "low", "high", "manual", "peak_performance"];
if level < 0 || level >= levels.len() as i32 {
return false;
}
// Open sysfs file
let result = File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level").await;
let mut myfile;
let result =
File::create("/sys/class/drm/card0/device/power_dpm_force_performance_level").await;
let mut myfile;
match result {
Ok(f) => myfile = f,
Err(message) => { println!("Error opening sysfs file for writing {message}"); return false; }
Err(message) => {
println!("Error opening sysfs file for writing {message}");
return false;
}
};
// write value
let result = myfile.write_all(levels[level as usize].as_bytes()).await;
match result {
Ok(_worked) => true,
Err(message) => { println!("Error writing to sysfs file {message}"); false }
Err(message) => {
println!("Error writing to sysfs file {message}");
false
}
}
}
@ -176,7 +225,10 @@ impl SMManager {
let mut myfile;
match result {
Ok(f) => myfile = f,
Err(message) => { println!("Error opening sysfs file for writing {message}"); return false; }
Err(message) => {
println!("Error opening sysfs file for writing {message}");
return false;
}
};
// write value
@ -191,16 +243,25 @@ impl SMManager {
let result = myfile.write("c\n".as_bytes()).await;
match result {
Ok(_worked) => true,
Err(message) => { println!("Error writing to sysfs file {message}"); false }
Err(message) => {
println!("Error writing to sysfs file {message}");
false
}
}
},
Err(message) => { println!("Error writing to sysfs file {message}"); false }
}
Err(message) => {
println!("Error writing to sysfs file {message}");
false
}
}
},
Err(message) => { println!("Error writing to sysfs file {message}"); false }
}
Err(message) => {
println!("Error writing to sysfs file {message}");
false
}
}
}
async fn set_tdp_limit(&self, limit: i32) -> bool {
// Set TDP limit given if within range (3-15)
// Returns false on error or out of range
@ -213,14 +274,20 @@ impl SMManager {
let mut power1file;
match result {
Ok(f) => power1file = f,
Err(message) => { println!("Error opening sysfs power1_cap file for writing TDP limits {message}"); return false; }
Err(message) => {
println!("Error opening sysfs power1_cap file for writing TDP limits {message}");
return false;
}
};
let result = File::create("/sys/class/hwmon/hwmon5/power2_cap").await;
let mut power2file;
match result {
Ok(f) => power2file = f,
Err(message) => { println!("Error opening sysfs power2_cap file for wtriting TDP limits {message}"); return false; }
Err(message) => {
println!("Error opening sysfs power2_cap file for wtriting TDP limits {message}");
return false;
}
};
// Now write the value * 1,000,000
@ -231,14 +298,22 @@ impl SMManager {
let result = power2file.write(data.as_bytes()).await;
match result {
Ok(_worked) => true,
Err(message) => { println!("Error writing to power2_cap file: {message}"); false }
Err(message) => {
println!("Error writing to power2_cap file: {message}");
false
}
}
},
Err(message) => { println!("Error writing to power1_cap file: {message}"); false }
}
Err(message) => {
println!("Error writing to power1_cap file: {message}");
false
}
}
}
async fn get_als_integration_time_file_descriptor(&self) -> Result<zbus::zvariant::OwnedFd, zbus::fdo::Error> {
async fn get_als_integration_time_file_descriptor(
&self,
) -> Result<zbus::zvariant::OwnedFd, zbus::fdo::Error> {
// Get the file descriptor for the als integration time sysfs path
// /sys/devices/platform/AMDI0010:00/i2c-0/i2c-PRP0001:01/iio:device0/in_illuminance_integration_time
// Return -1 on error
@ -250,8 +325,11 @@ impl SMManager {
let fd: OwnedFd = OwnedFd::from_raw_fd(raw);
Ok(fd)
}
},
Err(message) => { println!("Error opening sysfs file for giving file descriptor {message}"); Err(zbus::fdo::Error::IOError(message.to_string())) }
}
Err(message) => {
println!("Error opening sysfs file for giving file descriptor {message}");
Err(zbus::fdo::Error::IOError(message.to_string()))
}
}
}
@ -261,4 +339,3 @@ impl SMManager {
SMManager::API_VERSION
}
}