Split fan-curve config to own file

This commit is contained in:
Luke D. Jones
2023-01-06 16:52:04 +13:00
parent e4f79a3e6f
commit d93b870726
9 changed files with 88 additions and 74 deletions

View File

@@ -10,15 +10,12 @@ const CONFIG_FAN_FILE: &str = "fan_curves.conf";
pub struct ProfileConfig {
/// For restore on boot
pub active_profile: Profile,
/// States to restore
pub fan_curves: Option<FanCurveProfiles>,
}
impl StdConfig for ProfileConfig {
fn new() -> Self {
Self {
active_profile: Profile::Balanced,
fan_curves: None,
}
}
@@ -39,4 +36,4 @@ impl StdConfig for FanCurveProfiles {
}
}
impl StdConfigLoad1<ProfileConfig> for FanCurveProfiles {}
impl StdConfigLoad1<ProfileConfig> for FanCurveProfiles {}

View File

@@ -11,6 +11,7 @@ use crate::GetSupported;
pub struct CtrlPlatformProfile {
pub profile_config: ProfileConfig,
pub fan_config: Option<FanCurveProfiles>,
pub platform: AsusPlatform,
}
@@ -51,21 +52,26 @@ impl CtrlPlatformProfile {
if platform.has_platform_profile() || platform.has_throttle_thermal_policy() {
info!("Device has profile control available");
let mut controller = CtrlPlatformProfile { profile_config: config, platform };
let mut controller = CtrlPlatformProfile {
profile_config: config,
fan_config: None,
platform,
};
if FanCurveProfiles::get_device().is_ok() {
info!("Device has fan curves available");
if controller.profile_config.fan_curves.is_none() {
controller.profile_config.fan_curves = Some(Default::default());
if controller.fan_config.is_none() {
controller.fan_config = Some(Default::default());
for _ in [Profile::Balanced, Profile::Performance, Profile::Quiet] {
controller.set_next_profile()?;
controller.set_active_curve_to_defaults()?;
let active = Profile::get_active_profile().unwrap_or(Profile::Balanced);
if let Some(curves) = controller.profile_config.fan_curves.as_ref() {
if let Some(curves) = controller.fan_config.as_ref() {
info!(
"{active:?}: {}",
String::from(curves.get_fan_curves_for(active))
);
curves.write();
}
}
}
@@ -79,6 +85,9 @@ impl CtrlPlatformProfile {
pub fn save_config(&self) {
self.profile_config.write();
if let Some(fans) = self.fan_config.as_ref() {
fans.write();
}
}
/// Toggle to next profile in list. This will first read the config, switch,
@@ -105,18 +114,24 @@ impl CtrlPlatformProfile {
/// Set the curve for the active profile active
pub(super) fn write_profile_curve_to_platform(&mut self) -> Result<(), RogError> {
if let Some(curves) = &mut self.profile_config.fan_curves {
if let Some(curves) = &mut self.fan_config {
if let Ok(mut device) = FanCurveProfiles::get_device() {
curves.write_profile_curve_to_platform(self.profile_config.active_profile, &mut device)?;
curves.write_profile_curve_to_platform(
self.profile_config.active_profile,
&mut device,
)?;
}
}
Ok(())
}
pub(super) fn set_active_curve_to_defaults(&mut self) -> Result<(), RogError> {
if let Some(curves) = self.profile_config.fan_curves.as_mut() {
if let Some(curves) = self.fan_config.as_mut() {
if let Ok(mut device) = FanCurveProfiles::get_device() {
curves.set_active_curve_to_defaults(self.profile_config.active_profile, &mut device)?;
curves.set_active_curve_to_defaults(
self.profile_config.active_profile,
&mut device,
)?;
}
}
Ok(())

View File

@@ -81,7 +81,7 @@ impl ProfileZbus {
async fn enabled_fan_profiles(&mut self) -> zbus::fdo::Result<Vec<Profile>> {
let mut ctrl = self.0.lock().await;
ctrl.profile_config.read();
if let Some(curves) = &ctrl.profile_config.fan_curves {
if let Some(curves) = &ctrl.fan_config {
return Ok(curves.get_enabled_curve_profiles());
}
Err(Error::Failed(UNSUPPORTED_MSG.to_owned()))
@@ -96,7 +96,7 @@ impl ProfileZbus {
) -> zbus::fdo::Result<()> {
let mut ctrl = self.0.lock().await;
ctrl.profile_config.read();
if let Some(curves) = &mut ctrl.profile_config.fan_curves {
if let Some(curves) = &mut ctrl.fan_config {
curves.set_profile_curve_enabled(profile, enabled);
ctrl.write_profile_curve_to_platform()
@@ -114,7 +114,7 @@ impl ProfileZbus {
async fn fan_curve_data(&mut self, profile: Profile) -> zbus::fdo::Result<FanCurveSet> {
let mut ctrl = self.0.lock().await;
ctrl.profile_config.read();
if let Some(curves) = &ctrl.profile_config.fan_curves {
if let Some(curves) = &ctrl.fan_config {
let curve = curves.get_fan_curves_for(profile);
return Ok(curve.clone());
}
@@ -126,7 +126,7 @@ impl ProfileZbus {
async fn set_fan_curve(&self, profile: Profile, curve: CurveData) -> zbus::fdo::Result<()> {
let mut ctrl = self.0.lock().await;
ctrl.profile_config.read();
if let Some(curves) = &mut ctrl.profile_config.fan_curves {
if let Some(curves) = &mut ctrl.fan_config {
curves
.save_fan_curve(curve, profile)
.map_err(|err| zbus::fdo::Error::Failed(err.to_string()))?;
@@ -240,7 +240,7 @@ impl crate::Reloadable for ProfileZbus {
async fn reload(&mut self) -> Result<(), RogError> {
let mut ctrl = self.0.lock().await;
let active = ctrl.profile_config.active_profile;
if let Some(curves) = &mut ctrl.profile_config.fan_curves {
if let Some(curves) = &mut ctrl.fan_config {
if let Ok(mut device) = FanCurveProfiles::get_device() {
// There is a possibility that the curve was default zeroed, so this call
// initialises the data from system read and we need to save it