diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e8ee97a..6e0d71ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed - Fix missing CLI command help for some supported options +- Fix incorrectly selecting profile by name, where the active profile was being copied to the selected profile # [3.7.1] - 2021-06-11 ### Changed diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index cac15ad7..d4a0a301 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -489,12 +489,21 @@ fn handle_profile( } let mut set_profile = false; - let mut profile; + let mut profile = Profile::default(); if cmd.create { - profile = Profile::default(); set_profile = true; - } else { - profile = dbus.proxies().profile().active_data()?; + } else if let Some(ref name) = cmd.profile { + let profiles = dbus.proxies().profile().all_profile_data()?; + for p in profiles { + if p.name == *name { + profile = p; + break; + } + } + if profile.name != *name { + println!("The requested profile doesn't exist, you may need to create it"); + std::process::exit(-1); + } } if let Some(turbo) = cmd.turbo { diff --git a/daemon/src/ctrl_profiles/controller.rs b/daemon/src/ctrl_profiles/controller.rs index 12805434..aba0f5f9 100644 --- a/daemon/src/ctrl_profiles/controller.rs +++ b/daemon/src/ctrl_profiles/controller.rs @@ -1,6 +1,7 @@ use crate::error::RogError; use crate::{config::Config, GetSupported}; -use log::info; +use log::{info, warn}; +use rog_profiles::error::ProfileError; use rog_profiles::profiles::Profile; use rog_types::supported::FanCpuSupportedFunctions; use std::sync::Arc; @@ -23,12 +24,12 @@ impl GetSupported for CtrlFanAndCpu { } impl crate::Reloadable for CtrlFanAndCpu { + /// Fetcht he active profile and use that to set all related components up fn reload(&mut self) -> Result<(), RogError> { if let Ok(mut cfg) = self.config.clone().try_lock() { let active = cfg.active_profile.clone(); if let Some(existing) = cfg.power_profiles.get_mut(&active) { existing.set_system_all()?; - cfg.write(); } } Ok(()) @@ -38,31 +39,38 @@ impl crate::Reloadable for CtrlFanAndCpu { impl CtrlFanAndCpu { pub fn new(config: Arc>) -> Result { Profile::get_fan_path()?; - info!("Device has thermal throttle control"); + info!("Device has fan control available"); Ok(CtrlFanAndCpu { config }) } /// Toggle to next profile in list pub(super) fn do_next_profile(&mut self) -> Result<(), RogError> { if let Ok(mut config) = self.config.clone().try_lock() { + // Read first just incase the user has modified the config before calling this config.read(); - let mut i = config + let mut toggle_index = config .toggle_profiles .binary_search(&config.active_profile) .unwrap_or(0) + 1; - if i >= config.toggle_profiles.len() { - i = 0; + if toggle_index >= config.toggle_profiles.len() { + toggle_index = 0; } - let profile = config.toggle_profiles[i].clone(); + let profile = config.toggle_profiles[toggle_index].clone(); if let Some(existing) = config.power_profiles.get(&profile) { existing.set_system_all()?; config.active_profile = existing.name.clone(); config.write(); - info!("Profile was changed to: {}", profile); + info!("Profile was changed to: {}", &profile); + } else { + warn!( + "toggle_profile {} does not exist in power_profiles", + &profile + ); + return Err(RogError::MissingProfile(profile.to_string())); } } Ok(()) @@ -70,17 +78,25 @@ impl CtrlFanAndCpu { pub(super) fn set_active(&mut self, profile: &str) -> Result<(), RogError> { if let Ok(mut config) = self.config.clone().try_lock() { + // Read first just incase the user has modified the config before calling this config.read(); if let Some(existing) = config.power_profiles.get(profile) { existing.set_system_all()?; config.active_profile = existing.name.clone(); config.write(); info!("Profile was changed to: {}", profile); + } else { + warn!( + "toggle_profile {} does not exist in power_profiles", + profile + ); + return Err(RogError::MissingProfile(profile.to_string())); } } Ok(()) } + /// Create a new profile if the requested name doesn't exist, or modify existing pub(super) fn new_or_modify(&mut self, profile: &Profile) -> Result<(), RogError> { if let Ok(mut config) = self.config.clone().try_lock() { config.read(); diff --git a/daemon/src/error.rs b/daemon/src/error.rs index fdb85342..fa61f61e 100644 --- a/daemon/src/error.rs +++ b/daemon/src/error.rs @@ -8,7 +8,6 @@ use crate::ctrl_gfx::error::GfxError; #[derive(Debug)] pub enum RogError { - ParseFanLevel, ParseVendor, ParseLed, MissingProfile(String), @@ -36,7 +35,6 @@ impl fmt::Display for RogError { // This trait requires `fmt` with this exact signature. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - RogError::ParseFanLevel => write!(f, "Parse profile error"), RogError::ParseVendor => write!(f, "Parse gfx vendor error"), RogError::ParseLed => write!(f, "Parse LED error"), RogError::MissingProfile(profile) => write!(f, "Profile does not exist {}", profile), diff --git a/rog-profiles/src/error.rs b/rog-profiles/src/error.rs index e73538e4..c5dc7c12 100644 --- a/rog-profiles/src/error.rs +++ b/rog-profiles/src/error.rs @@ -6,7 +6,6 @@ use rog_fan_curve::CurveError; #[derive(Debug)] pub enum ProfileError { ParseFanLevel, - MissingProfile(String), Path(String, std::io::Error), Read(String, std::io::Error), Write(String, std::io::Error), @@ -23,9 +22,6 @@ impl fmt::Display for ProfileError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ProfileError::ParseFanLevel => write!(f, "Parse profile error"), - ProfileError::MissingProfile(profile) => { - write!(f, "Profile does not exist {}", profile) - } ProfileError::Path(path, error) => write!(f, "Path {}: {}", path, error), ProfileError::Read(path, error) => write!(f, "Read {}: {}", path, error), ProfileError::Write(path, error) => write!(f, "Write {}: {}", path, error),