Break config-traits out in to crate

This commit is contained in:
Luke D. Jones
2023-01-07 20:46:00 +13:00
parent ea5e5db490
commit 90b711c7b9
26 changed files with 161 additions and 110 deletions

View File

@@ -1,7 +1,11 @@
use std::path::PathBuf;
use config_traits::{StdConfig, StdConfigLoad1};
use rog_profiles::fan_curve_set::FanCurveSet;
use rog_profiles::{FanCurveProfiles, Profile};
use serde_derive::{Deserialize, Serialize};
use crate::config_traits::{StdConfig, StdConfigLoad1};
use crate::CONFIG_PATH_BASE;
const CONFIG_FILE: &str = "profile.conf";
const CONFIG_FAN_FILE: &str = "fan_curves.conf";
@@ -19,6 +23,10 @@ impl StdConfig for ProfileConfig {
}
}
fn config_dir() -> std::path::PathBuf {
PathBuf::from(CONFIG_PATH_BASE)
}
fn file_name() -> &'static str {
CONFIG_FILE
}
@@ -26,9 +34,46 @@ impl StdConfig for ProfileConfig {
impl StdConfigLoad1<ProfileConfig> for ProfileConfig {}
impl StdConfig for FanCurveProfiles {
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct FanCurveConfig {
balanced: FanCurveSet,
performance: FanCurveSet,
quiet: FanCurveSet,
#[serde(skip)]
device: FanCurveProfiles,
}
impl FanCurveConfig {
pub fn update_device_config(&mut self) {
self.balanced = self.device.balanced.clone();
self.performance = self.device.performance.clone();
self.quiet = self.device.quiet.clone();
}
pub fn update_config(&mut self) {
self.balanced = self.device.balanced.clone();
self.performance = self.device.performance.clone();
self.quiet = self.device.quiet.clone();
}
pub fn device(&self) -> &FanCurveProfiles {
&self.device
}
pub fn device_mut(&mut self) -> &mut FanCurveProfiles {
&mut self.device
}
}
impl StdConfig for FanCurveConfig {
fn new() -> Self {
Self::default()
let mut tmp = Self::default();
tmp.update_device_config();
tmp
}
fn config_dir() -> std::path::PathBuf {
PathBuf::from(CONFIG_PATH_BASE)
}
fn file_name() -> &'static str {
@@ -36,4 +81,4 @@ impl StdConfig for FanCurveProfiles {
}
}
impl StdConfigLoad1<ProfileConfig> for FanCurveProfiles {}
impl StdConfigLoad1<ProfileConfig> for FanCurveConfig {}

View File

@@ -1,17 +1,17 @@
use config_traits::StdConfig;
use log::{info, warn};
use rog_platform::platform::AsusPlatform;
use rog_platform::supported::PlatformProfileFunctions;
use rog_profiles::error::ProfileError;
use rog_profiles::{FanCurveProfiles, Profile};
use super::config::ProfileConfig;
use crate::config_traits::StdConfig;
use super::config::{FanCurveConfig, ProfileConfig};
use crate::error::RogError;
use crate::GetSupported;
pub struct CtrlPlatformProfile {
pub profile_config: ProfileConfig,
pub fan_config: Option<FanCurveProfiles>,
pub fan_config: Option<FanCurveConfig>,
pub platform: AsusPlatform,
}
@@ -69,7 +69,7 @@ impl CtrlPlatformProfile {
if let Some(curves) = controller.fan_config.as_ref() {
info!(
"{active:?}: {}",
String::from(curves.get_fan_curves_for(active))
String::from(curves.device().get_fan_curves_for(active))
);
curves.write();
}
@@ -83,9 +83,10 @@ impl CtrlPlatformProfile {
Err(ProfileError::NotSupported.into())
}
pub fn save_config(&self) {
pub fn save_config(&mut self) {
self.profile_config.write();
if let Some(fans) = self.fan_config.as_ref() {
if let Some(fans) = self.fan_config.as_mut() {
fans.update_config();
fans.write();
}
}
@@ -116,7 +117,7 @@ impl CtrlPlatformProfile {
pub(super) fn write_profile_curve_to_platform(&mut self) -> Result<(), RogError> {
if let Some(curves) = &mut self.fan_config {
if let Ok(mut device) = FanCurveProfiles::get_device() {
curves.write_profile_curve_to_platform(
curves.device_mut().write_profile_curve_to_platform(
self.profile_config.active_profile,
&mut device,
)?;
@@ -128,10 +129,11 @@ impl CtrlPlatformProfile {
pub(super) fn set_active_curve_to_defaults(&mut self) -> Result<(), RogError> {
if let Some(curves) = self.fan_config.as_mut() {
if let Ok(mut device) = FanCurveProfiles::get_device() {
curves.set_active_curve_to_defaults(
curves.device_mut().set_active_curve_to_defaults(
self.profile_config.active_profile,
&mut device,
)?;
curves.update_config();
}
}
Ok(())

View File

@@ -2,6 +2,7 @@ use std::str::FromStr;
use std::sync::Arc;
use async_trait::async_trait;
use config_traits::StdConfig;
use log::{error, info, warn};
use rog_profiles::fan_curve_set::{CurveData, FanCurveSet};
use rog_profiles::{FanCurveProfiles, Profile};
@@ -11,7 +12,6 @@ use zbus::fdo::Error;
use zbus::{dbus_interface, Connection, SignalContext};
use super::controller::CtrlPlatformProfile;
use crate::config_traits::StdConfig;
use crate::error::RogError;
use crate::CtrlTask;
@@ -82,8 +82,8 @@ 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.fan_config {
return Ok(curves.get_enabled_curve_profiles());
if let Some(curves) = &mut ctrl.fan_config {
return Ok(curves.device().get_enabled_curve_profiles());
}
Err(Error::Failed(UNSUPPORTED_MSG.to_owned()))
}
@@ -98,7 +98,10 @@ impl ProfileZbus {
let mut ctrl = self.0.lock().await;
ctrl.profile_config.read();
if let Some(curves) = &mut ctrl.fan_config {
curves.set_profile_curve_enabled(profile, enabled);
curves
.device_mut()
.set_profile_curve_enabled(profile, enabled);
curves.update_config();
ctrl.write_profile_curve_to_platform()
.map_err(|e| warn!("write_profile_curve_to_platform, {}", e))
@@ -115,8 +118,8 @@ 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.fan_config {
let curve = curves.get_fan_curves_for(profile);
if let Some(curves) = &mut ctrl.fan_config {
let curve = curves.device().get_fan_curves_for(profile);
return Ok(curve.clone());
}
Err(Error::Failed(UNSUPPORTED_MSG.to_owned()))
@@ -129,8 +132,10 @@ impl ProfileZbus {
ctrl.profile_config.read();
if let Some(curves) = &mut ctrl.fan_config {
curves
.device_mut()
.save_fan_curve(curve, profile)
.map_err(|err| zbus::fdo::Error::Failed(err.to_string()))?;
curves.update_config();
} else {
return Err(Error::Failed(UNSUPPORTED_MSG.to_owned()));
}
@@ -295,7 +300,10 @@ impl crate::Reloadable for ProfileZbus {
// 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
// after
curves.write_profile_curve_to_platform(active, &mut device)?;
curves
.device_mut()
.write_profile_curve_to_platform(active, &mut device)?;
curves.update_config();
ctrl.profile_config.write();
}
}