1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-28 09:27:44 +00:00

battery: better threshold setting logic

This commit is contained in:
NotAShelf 2025-05-16 02:19:15 +03:00
parent cd68ffd054
commit 3d490db734
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF

View file

@ -161,6 +161,19 @@ fn write_sysfs_value(path: impl AsRef<Path>, value: &str) -> Result<()> {
}) })
} }
/// Read a value from a sysfs file
fn read_sysfs_value(path: impl AsRef<Path>) -> Result<String> {
let p = path.as_ref();
fs::read_to_string(p).map_err(|e| {
let error_msg = format!("Path: {:?}, Error: {}", p.display(), e);
if e.kind() == io::ErrorKind::PermissionDenied {
ControlError::PermissionDenied(error_msg)
} else {
ControlError::ReadError(error_msg)
}
}).map(|s| s.trim().to_string())
}
/// Safely check if a path exists and is writable /// Safely check if a path exists and is writable
fn path_exists_and_writable(path: &Path) -> bool { fn path_exists_and_writable(path: &Path) -> bool {
if !path.exists() { if !path.exists() {
@ -204,36 +217,56 @@ fn apply_thresholds_to_batteries(
let start_path = battery.path.join(battery.pattern.start_path); let start_path = battery.path.join(battery.pattern.start_path);
let stop_path = battery.path.join(battery.pattern.stop_path); let stop_path = battery.path.join(battery.pattern.stop_path);
match ( // Read current thresholds in case we need to restore them
write_sysfs_value(&start_path, &start_threshold.to_string()), let current_start = read_sysfs_value(&start_path).ok();
write_sysfs_value(&stop_path, &stop_threshold.to_string()), let current_stop = read_sysfs_value(&stop_path).ok();
) {
(Ok(()), Ok(())) => { // Write stop threshold first (must be >= start threshold)
debug!( let stop_result = write_sysfs_value(&stop_path, &stop_threshold.to_string());
"Set {}-{}% charge thresholds for {} battery '{}'",
start_threshold, stop_threshold, battery.pattern.description, battery.name // Only proceed to set start threshold if stop threshold was set successfully
); if let Ok(()) = stop_result {
success_count += 1; let start_result = write_sysfs_value(&start_path, &start_threshold.to_string());
}
(start_result, stop_result) => { match start_result {
let mut error_msg = format!( Ok(()) => {
"Failed to set thresholds for {} battery '{}'", debug!(
battery.pattern.description, battery.name "Set {}-{}% charge thresholds for {} battery '{}'",
); start_threshold, stop_threshold, battery.pattern.description, battery.name
if let Err(e) = start_result { );
error_msg.push_str(&format!(": start threshold error: {e}")); success_count += 1;
} }
if let Err(e) = stop_result { Err(e) => {
error_msg.push_str(&format!(": stop threshold error: {e}")); // Start threshold failed, try to restore the previous stop threshold
if let Some(prev_stop) = &current_stop {
let restore_result = write_sysfs_value(&stop_path, prev_stop);
if let Err(re) = restore_result {
warn!(
"Failed to restore previous stop threshold for battery '{}': {}. Battery may be in an inconsistent state.",
battery.name, re
);
} else {
debug!("Restored previous stop threshold ({}) for battery '{}'", prev_stop, battery.name);
}
}
errors.push(format!(
"Failed to set start threshold for {} battery '{}': {}",
battery.pattern.description, battery.name, e
));
} }
errors.push(error_msg);
} }
} else if let Err(e) = stop_result {
errors.push(format!(
"Failed to set stop threshold for {} battery '{}': {}",
battery.pattern.description, battery.name, e
));
} }
} }
if success_count > 0 { if success_count > 0 {
if !errors.is_empty() { if !errors.is_empty() {
debug!( warn!(
"Partial success setting battery thresholds: {}", "Partial success setting battery thresholds: {}",
errors.join("; ") errors.join("; ")
); );