From baa5cef625bf747586f27b7b8bc9e5eff3b54fde Mon Sep 17 00:00:00 2001 From: Luke Date: Fri, 26 Jun 2020 16:23:14 +1200 Subject: [PATCH] Add ability to set BAT0/charge_control_end_threshold --- CHANGELOG.md | 3 +++ rog-core/src/config.rs | 2 ++ rog-core/src/daemon.rs | 5 ++++- rog-core/src/rogcore.rs | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306f6660..deeb9a70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for G531GT - Remove duplicated code: it looks like there is at least *some* consistency in Consumer-Device keycodes that ASUS uses +### BREAKING CHANGE +- `bat_charge_limit = 100` must be appended to the top of `/etc/rogcore.conf` + ## [0.11.1] - 2020-11-06 ### Changed - Use DBUS_NAME instead of DBUS_IFACE when requesting the name diff --git a/rog-core/src/config.rs b/rog-core/src/config.rs index e03b0509..5663c276 100644 --- a/rog-core/src/config.rs +++ b/rog-core/src/config.rs @@ -8,6 +8,7 @@ pub static CONFIG_PATH: &str = "/etc/rogcore.conf"; #[derive(Default, Deserialize, Serialize)] pub struct Config { pub fan_mode: u8, + pub bat_charge_limit: u8, pub brightness: u8, pub current_mode: [u8; 4], pub builtin_modes: BuiltInModeBytes, @@ -29,6 +30,7 @@ impl Config { if l == 0 { // create a default config here let mut c = Config::default(); + c.bat_charge_limit = 100; c.current_mode[0] = 0x5d; c.current_mode[1] = 0xb3; // Should be okay to unwrap this as is since it is a Default diff --git a/rog-core/src/daemon.rs b/rog-core/src/daemon.rs index 43776ccf..8a8e9015 100644 --- a/rog-core/src/daemon.rs +++ b/rog-core/src/daemon.rs @@ -51,8 +51,11 @@ pub async fn start_daemon() -> Result<(), Box> { // Reload settings rogcore .fan_mode_reload(&mut config) - .await .unwrap_or_else(|err| warn!("Fan mode: {}", err)); + rogcore + .bat_charge_limit_reload(&mut config) + .unwrap_or_else(|err| warn!("Battery charge limit: {}", err)); + let mut led_writer = LedWriter::new( rogcore.get_raw_device_handle(), laptop.led_endpoint(), diff --git a/rog-core/src/rogcore.rs b/rog-core/src/rogcore.rs index 48d9149a..c919bcc0 100644 --- a/rog-core/src/rogcore.rs +++ b/rog-core/src/rogcore.rs @@ -16,6 +16,7 @@ use std::time::Duration; static FAN_TYPE_1_PATH: &str = "/sys/devices/platform/asus-nb-wmi/throttle_thermal_policy"; static FAN_TYPE_2_PATH: &str = "/sys/devices/platform/asus-nb-wmi/fan_boost_mode"; static AMD_BOOST_PATH: &str = "/sys/devices/system/cpu/cpufreq/boost"; +static BAT_CHARGE_PATH: &str = "/sys/class/power_supply/BAT0/charge_control_end_threshold"; /// ROG device controller /// @@ -128,7 +129,7 @@ impl RogCore { } } - pub async fn fan_mode_reload(&mut self, config: &mut Config) -> Result<(), Box> { + pub fn fan_mode_reload(&mut self, config: &mut Config) -> Result<(), Box> { let path = RogCore::get_fan_path()?; let mut file = OpenOptions::new().write(true).open(path)?; file.write_all(format!("{:?}\n", config.fan_mode).as_bytes()) @@ -258,6 +259,37 @@ impl RogCore { Ok(()) } + pub fn bat_charge_limit_reload(&self, config: &mut Config) -> Result<(), Box> { + config.read(); + info!("Reloaded battery charge limit"); + self.set_charge_limit(config.bat_charge_limit, config) + } + + pub fn set_charge_limit(&self, limit: u8, config: &mut Config) -> Result<(), Box> { + if limit < 20 || limit > 100 { + warn!( + "Unable to set battery charge limit, must be between 20-100: requested {}", + limit + ); + } + + let mut file = OpenOptions::new() + .write(true) + .open(BAT_CHARGE_PATH) + .map_err(|err| { + warn!("Failed to open battery charge limit path: {:?}", err); + err + })?; + file.write_all(limit.to_string().as_bytes()) + .unwrap_or_else(|err| error!("Could not write to {}, {:?}", BAT_CHARGE_PATH, err)); + info!("Battery charge limit: {}", limit); + + config.bat_charge_limit = limit; + config.write(); + + Ok(()) + } + /// A direct call to systemd to suspend the PC. /// /// This avoids desktop environments being required to handle it