Fleshing out functions and zbus controls

This commit is contained in:
Luke D. Jones
2021-09-08 23:33:42 +12:00
parent bfaa478a4a
commit 0a565a7a5c
5 changed files with 93 additions and 50 deletions

View File

@@ -10,7 +10,7 @@ pub struct ProfileConfig {
#[serde(skip)]
config_path: String,
/// For restore on boot
pub active: Profile,
pub active_profile: Profile,
/// States to restore
pub fan_curves: Option<FanCurves>,
}
@@ -19,13 +19,13 @@ impl ProfileConfig {
fn new(config_path: String) -> Self {
let mut platform = ProfileConfig {
config_path,
active: Profile::Balanced,
active_profile: Profile::Balanced,
fan_curves: None,
};
if let Ok(res) = FanCurveSet::is_supported() {
if res {
let mut curves = FanCurves::default();
let curves = FanCurves::default();
platform.fan_curves = Some(curves);
}
}

View File

@@ -57,7 +57,7 @@ impl crate::Reloadable for CtrlPlatformProfile {
fn reload(&mut self) -> Result<(), RogError> {
if let Some(curves) = &self.config.fan_curves {
if let Ok(mut device) = FanCurveSet::get_device() {
curves.write_to_platform(self.config.active, &mut device);
curves.write_to_platform(self.config.active_profile, &mut device);
}
}
Ok(())
@@ -86,18 +86,18 @@ impl CtrlPlatformProfile {
// Read first just incase the user has modified the config before calling this
self.config.read();
match self.config.active {
match self.config.active_profile {
Profile::Balanced => {
Profile::set_profile(Profile::Performance)?;
self.config.active = Profile::Performance;
self.config.active_profile = Profile::Performance;
}
Profile::Performance => {
Profile::set_profile(Profile::Quiet)?;
self.config.active = Profile::Quiet;
self.config.active_profile = Profile::Quiet;
}
Profile::Quiet => {
Profile::set_profile(Profile::Balanced)?;
self.config.active = Profile::Balanced;
self.config.active_profile = Profile::Balanced;
}
}

View File

@@ -1,4 +1,5 @@
use log::warn;
use rog_profiles::fan_curves::CurveData;
use rog_profiles::fan_curves::FanCurveSet;
use rog_profiles::Profile;
@@ -47,7 +48,7 @@ impl ProfileZbus {
fn active_profile(&mut self) -> zbus::fdo::Result<Profile> {
if let Ok(mut ctrl) = self.inner.try_lock() {
ctrl.config.read();
return Ok(ctrl.config.active);
return Ok(ctrl.config.active_profile);
}
Err(Error::Failed(
"Failed to get active profile name".to_string(),
@@ -62,7 +63,7 @@ impl ProfileZbus {
Profile::set_profile(profile)
.map_err(|e| warn!("Profile::set_profile, {}", e))
.ok();
ctrl.config.active = profile;
ctrl.config.active_profile = profile;
ctrl.save_config();
}
@@ -74,7 +75,21 @@ impl ProfileZbus {
if let Ok(mut ctrl) = self.inner.try_lock() {
ctrl.config.read();
if let Some(curves) = &ctrl.config.fan_curves {
return Ok(curves.get_enabled_curve_names().to_vec());
return Ok(curves.get_enabled_curve_profiles().to_vec());
}
return Err(Error::Failed(UNSUPPORTED_MSG.to_string()));
}
Err(Error::Failed(
"Failed to get enabled fan curve names".to_string(),
))
}
/// Get a list of profiles that have fan-curves enabled.
fn set_enabled_fan_profiles(&mut self, profiles: Vec<Profile>) -> zbus::fdo::Result<()> {
if let Ok(mut ctrl) = self.inner.try_lock() {
ctrl.config.read();
if let Some(curves) = &mut ctrl.config.fan_curves {
curves.set_enabled_curve_profiles(profiles);
}
return Err(Error::Failed(UNSUPPORTED_MSG.to_string()));
}
@@ -108,12 +123,13 @@ impl ProfileZbus {
}
/// Set this fan-curve data
fn set_fan_curve(&self, curve: FanCurveSet) -> zbus::fdo::Result<()> {
fn set_fan_curve(&self, curve: CurveData) -> zbus::fdo::Result<()> {
if let Ok(mut ctrl) = self.inner.try_lock() {
ctrl.config.read();
let profile = ctrl.config.active_profile;
if let Some(mut device) = ctrl.get_device() {
if let Some(curves) = &mut ctrl.config.fan_curves {
curves.set_fan_curve(curve, &mut device);
curves.write_and_set_fan_curve(curve, profile, &mut device);
}
} else {
return Err(Error::Failed(UNSUPPORTED_MSG.to_string()));
@@ -132,7 +148,7 @@ impl ProfileZbus {
impl ProfileZbus {
fn do_notification(&self) {
if let Ok(ctrl) = self.inner.try_lock() {
self.notify_profile(&ctrl.config.active)
self.notify_profile(&ctrl.config.active_profile)
.unwrap_or_else(|err| warn!("{}", err));
}
}

View File

@@ -4,7 +4,7 @@ use udev::Device;
#[cfg(feature = "dbus")]
use zvariant_derive::Type;
use crate::error::ProfileError;
use crate::{FanCurvePU, error::ProfileError, write_to_fan};
pub fn pwm_str(fan: char, index: char) -> String {
let mut buf = "pwm1_auto_point1_pwm".to_string();
@@ -29,6 +29,7 @@ pub fn temp_str(fan: char, index: char) -> String {
#[cfg_attr(feature = "dbus", derive(Type))]
#[derive(Deserialize, Serialize, Default, Debug, Clone)]
pub struct CurveData {
pub fan: FanCurvePU,
pub pwm: [u8; 8],
pub temp: [u8; 8],
}
@@ -63,7 +64,12 @@ impl FanCurveSet {
cpu: CurveData::default(),
gpu: CurveData::default(),
};
fans.cpu.fan = FanCurvePU::CPU;
fans.cpu.fan = FanCurvePU::GPU;
fans.init_from_device(&device);
return Ok((fans, device));
}
@@ -109,39 +115,11 @@ impl FanCurveSet {
}
}
fn write_to_fan(curve: &CurveData, pwm_num: char, device: &mut Device) {
let mut pwm = "pwmN_auto_pointN_pwm".to_string();
for (index,out) in curve.pwm.iter().enumerate() {
unsafe {
let buf = pwm.as_bytes_mut();
buf[3] = pwm_num as u8;
// Should be quite safe to unwrap as we're not going over 8
buf[15] = char::from_digit(index as u32, 10).unwrap() as u8;
}
let out = out.to_string();
device.set_attribute_value(&pwm, &out).unwrap();
}
let mut pwm = "pwmN_auto_pointN_temp".to_string();
for (index,out) in curve.temp.iter().enumerate() {
unsafe {
let buf = pwm.as_bytes_mut();
buf[3] = pwm_num as u8;
// Should be quite safe to unwrap as we're not going over 8
buf[15] = char::from_digit(index as u32, 10).unwrap() as u8;
}
let out = out.to_string();
device.set_attribute_value(&pwm, &out).unwrap();
}
}
pub fn write_cpu_fan(&self, device: &mut Device) {
Self::write_to_fan(&self.cpu, '1', device);
write_to_fan(&self.cpu, '1', device);
}
pub fn write_gpu_fan(&self, device: &mut Device) {
Self::write_to_fan(&self.gpu, '2', device);
write_to_fan(&self.gpu, '2', device);
}
}
}

View File

@@ -165,10 +165,14 @@ impl FanCurves {
fans.write_gpu_fan(device);
}
pub fn get_enabled_curve_names(&self) -> &[Profile] {
pub fn get_enabled_curve_profiles(&self) -> &[Profile] {
&self.enabled
}
pub fn set_enabled_curve_profiles(&mut self, profiles: Vec<Profile>) {
self.enabled = profiles
}
pub fn get_all_fan_curves(&self) -> Vec<FanCurveSet> {
vec![
self.balanced.clone(),
@@ -210,8 +214,53 @@ impl FanCurves {
}
}
pub fn set_fan_curve(&self, curve: FanCurveSet, device: &mut Device) {
curve.write_cpu_fan(device);
curve.write_gpu_fan(device);
pub fn write_and_set_fan_curve(&mut self, curve: CurveData, profile: Profile, device: &mut Device) {
match curve.fan {
FanCurvePU::CPU => write_to_fan(&curve, '1', device),
FanCurvePU::GPU => write_to_fan(&curve, '2', device),
}
match profile {
Profile::Balanced => match curve.fan {
FanCurvePU::CPU => self.balanced.cpu = curve,
FanCurvePU::GPU => self.balanced.gpu = curve,
},
Profile::Performance => match curve.fan {
FanCurvePU::CPU => self.performance.cpu = curve,
FanCurvePU::GPU => self.performance.gpu = curve,
},
Profile::Quiet => match curve.fan {
FanCurvePU::CPU => self.quiet.cpu = curve,
FanCurvePU::GPU => self.quiet.gpu = curve,
},
}
}
}
pub fn write_to_fan(curve: &CurveData, pwm_num: char, device: &mut Device) {
let mut pwm = "pwmN_auto_pointN_pwm".to_string();
for (index,out) in curve.pwm.iter().enumerate() {
unsafe {
let buf = pwm.as_bytes_mut();
buf[3] = pwm_num as u8;
// Should be quite safe to unwrap as we're not going over 8
buf[15] = char::from_digit(index as u32, 10).unwrap() as u8;
}
let out = out.to_string();
device.set_attribute_value(&pwm, &out).unwrap();
}
let mut pwm = "pwmN_auto_pointN_temp".to_string();
for (index,out) in curve.temp.iter().enumerate() {
unsafe {
let buf = pwm.as_bytes_mut();
buf[3] = pwm_num as u8;
// Should be quite safe to unwrap as we're not going over 8
buf[15] = char::from_digit(index as u32, 10).unwrap() as u8;
}
let out = out.to_string();
device.set_attribute_value(&pwm, &out).unwrap();
}
}