Bugfix: urgent small fixes

This commit is contained in:
Luke D. Jones
2025-01-15 22:19:46 +13:00
parent a790d9a499
commit ad63c429cb
9 changed files with 145 additions and 111 deletions

View File

@@ -1,11 +1,18 @@
use std::str::FromStr;
use std::sync::Arc;
use ::zbus::export::futures_util::lock::Mutex;
use config_traits::StdConfig;
use log::error;
use rog_platform::firmware_attributes::{
AttrValue, Attribute, FirmwareAttribute, FirmwareAttributes
};
use rog_platform::platform::{RogPlatform, ThrottlePolicy};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Type, Value};
use zbus::{fdo, interface, Connection};
use crate::config::Config;
use crate::error::RogError;
use crate::ASUS_ZBUS_PATH;
@@ -21,18 +28,27 @@ fn dbus_path_for_attr(attr_name: &str) -> OwnedObjectPath {
ObjectPath::from_str_unchecked(&format!("{ASUS_ZBUS_PATH}/{MOD_NAME}/{attr_name}")).into()
}
pub struct AsusArmouryAttribute(Attribute);
pub struct AsusArmouryAttribute {
attr: Attribute,
config: Arc<Mutex<Config>>,
/// platform control required here for access to PPD or Throttle profile
platform: RogPlatform
}
impl AsusArmouryAttribute {
pub fn new(attr: Attribute) -> Self {
Self(attr)
pub fn new(attr: Attribute, platform: RogPlatform, config: Arc<Mutex<Config>>) -> Self {
Self {
attr,
config,
platform
}
}
pub async fn start_tasks(self, connection: &Connection) -> Result<(), RogError> {
// self.reload()
// .await
// .unwrap_or_else(|err| warn!("Controller error: {}", err));
let path = dbus_path_for_attr(self.0.name());
let path = dbus_path_for_attr(self.attr.name());
connection
.object_server()
.at(path.clone(), self)
@@ -48,30 +64,30 @@ impl AsusArmouryAttribute {
#[interface(name = "xyz.ljones.AsusArmoury")]
impl AsusArmouryAttribute {
#[zbus(property)]
async fn name(&self) -> FirmwareAttribute {
self.0.name().into()
fn name(&self) -> FirmwareAttribute {
self.attr.name().into()
}
#[zbus(property)]
async fn available_attrs(&self) -> Vec<String> {
let mut attrs = Vec::new();
if !matches!(self.0.default_value(), AttrValue::None) {
if !matches!(self.attr.default_value(), AttrValue::None) {
attrs.push("default_value".to_string());
}
if !matches!(self.0.min_value(), AttrValue::None) {
if !matches!(self.attr.min_value(), AttrValue::None) {
attrs.push("min_value".to_string());
}
if !matches!(self.0.max_value(), AttrValue::None) {
if !matches!(self.attr.max_value(), AttrValue::None) {
attrs.push("max_value".to_string());
}
if !matches!(self.0.scalar_increment(), AttrValue::None) {
if !matches!(self.attr.scalar_increment(), AttrValue::None) {
attrs.push("scalar_increment".to_string());
}
if !matches!(self.0.possible_values(), AttrValue::None) {
if !matches!(self.attr.possible_values(), AttrValue::None) {
attrs.push("possible_values".to_string());
}
// TODO: Don't unwrap, use error
if let Ok(value) = self.0.current_value().map_err(|e| {
if let Ok(value) = self.attr.current_value().map_err(|e| {
error!("Failed to read: {e:?}");
e
}) {
@@ -85,7 +101,7 @@ impl AsusArmouryAttribute {
/// If return is `-1` then there is no default value
#[zbus(property)]
async fn default_value(&self) -> i32 {
match self.0.default_value() {
match self.attr.default_value() {
AttrValue::Integer(i) => *i,
_ => -1
}
@@ -93,7 +109,7 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn min_value(&self) -> i32 {
match self.0.min_value() {
match self.attr.min_value() {
AttrValue::Integer(i) => *i,
_ => -1
}
@@ -101,7 +117,7 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn max_value(&self) -> i32 {
match self.0.max_value() {
match self.attr.max_value() {
AttrValue::Integer(i) => *i,
_ => -1
}
@@ -109,7 +125,7 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn scalar_increment(&self) -> i32 {
match self.0.scalar_increment() {
match self.attr.scalar_increment() {
AttrValue::Integer(i) => *i,
_ => -1
}
@@ -117,7 +133,7 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn possible_values(&self) -> Vec<i32> {
match self.0.possible_values() {
match self.attr.possible_values() {
AttrValue::EnumInt(i) => i.clone(),
_ => Vec::default()
}
@@ -125,7 +141,7 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn current_value(&self) -> fdo::Result<i32> {
if let Ok(AttrValue::Integer(i)) = self.0.current_value() {
if let Ok(AttrValue::Integer(i)) = self.attr.current_value() {
return Ok(i);
}
Err(fdo::Error::Failed(
@@ -135,19 +151,31 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn set_current_value(&mut self, value: i32) -> fdo::Result<()> {
Ok(self
.0
self.attr
.set_current_value(AttrValue::Integer(value))
.map_err(|e| {
error!("Could not set value: {e:?}");
e
})?)
})?;
let profile: ThrottlePolicy =
ThrottlePolicy::from_str(self.platform.get_platform_profile()?.as_str())?;
if let Some(tunings) = self.config.lock().await.tunings.get_mut(&profile) {
if let Some(tune) = tunings.get_mut(&self.name()) {
*tune = value;
}
}
self.config.lock().await.write();
Ok(())
}
}
pub async fn start_attributes_zbus(server: &Connection) -> Result<(), RogError> {
pub async fn start_attributes_zbus(
server: &Connection,
platform: RogPlatform,
config: Arc<Mutex<Config>>
) -> Result<(), RogError> {
for attr in FirmwareAttributes::new().attributes() {
AsusArmouryAttribute::new(attr.clone())
AsusArmouryAttribute::new(attr.clone(), platform.clone(), config.clone())
.start_tasks(server)
.await?;
}