Minor test of platform attributes

This commit is contained in:
Luke D. Jones
2024-12-27 21:21:32 +13:00
parent a1a9c7077a
commit fd3384decc
16 changed files with 200 additions and 333 deletions

View File

@@ -1,8 +1,11 @@
use rog_platform::firmware_attributes::FirmwareAttributes;
use zbus::Connection;
use log::error;
use rog_platform::firmware_attributes::{AttrValue, Attribute};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Type, Value};
use zbus::{fdo, interface, Connection};
use zbus::{fdo, interface};
use crate::error::RogError;
use crate::ASUS_ZBUS_PATH;
@@ -43,13 +46,43 @@ impl AsusArmouryAttribute {
/// If return is `-1` on a property then there is avilable value for that
/// property
#[interface(name = "org.asuslinux.AsusArmoury")]
#[interface(name = "xyz.ljones.AsusArmoury")]
impl AsusArmouryAttribute {
#[zbus(property)]
async fn name(&self) -> String {
self.0.name().to_string()
}
#[zbus(property)]
async fn available_attrs(&self) -> Vec<String> {
let mut attrs = Vec::new();
if !matches!(self.0.default_value(), AttrValue::None) {
attrs.push("default_value".to_string());
}
if !matches!(self.0.min_value(), AttrValue::None) {
attrs.push("min_value".to_string());
}
if !matches!(self.0.max_value(), AttrValue::None) {
attrs.push("max_value".to_string());
}
if !matches!(self.0.scalar_increment(), AttrValue::None) {
attrs.push("scalar_increment".to_string());
}
if !matches!(self.0.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| {
error!("Failed to read: {e:?}");
e
}) {
if !matches!(value, AttrValue::None) {
attrs.push("current_value".to_string());
}
}
attrs
}
/// If return is `-1` then there is no default value
#[zbus(property)]
async fn default_value(&self) -> i32 {
@@ -77,7 +110,18 @@ impl AsusArmouryAttribute {
#[zbus(property)]
async fn scalar_increment(&self) -> i32 {
self.0.scalar_increment().unwrap_or(1)
match self.0.scalar_increment() {
AttrValue::Integer(i) => *i,
_ => -1,
}
}
#[zbus(property)]
async fn possible_values(&self) -> Vec<i32> {
match self.0.possible_values() {
AttrValue::EnumInt(i) => i.clone(),
_ => Vec::default(),
}
}
#[zbus(property)]
@@ -103,3 +147,12 @@ impl AsusArmouryAttribute {
})?)
}
}
pub async fn start_attributes_zbus(server: &Connection) -> Result<(), RogError> {
for attr in FirmwareAttributes::new().attributes() {
AsusArmouryAttribute::new(attr.clone())
.start_tasks(server)
.await?;
}
Ok(())
}

View File

@@ -1,89 +0,0 @@
use log::error;
use rog_platform::firmware_attributes::{AttrValue, Attribute};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Type, Value};
use zbus::{fdo, interface, Connection};
use crate::error::RogError;
use crate::ASUS_ZBUS_PATH;
const MOD_NAME: &str = "asus_armoury";
#[derive(Debug, Default, Clone, Deserialize, Serialize, Type, Value, OwnedValue)]
pub struct PossibleValues {
strings: Vec<String>,
nums: Vec<i32>,
}
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);
impl AsusArmouryAttribute {
pub fn new(attr: Attribute) -> Self {
Self(attr)
}
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());
connection
.object_server()
.at(path.clone(), self)
.await
.map_err(|e| error!("Couldn't add server at path: {path}, {e:?}"))
.ok();
Ok(())
}
}
#[interface(name = "org.asuslinux.AsusArmoury")]
impl AsusArmouryAttribute {
#[zbus(property)]
async fn name(&self) -> String {
self.0.name().to_string()
}
#[zbus(property)]
async fn default_value(&self) -> i32 {
match self.0.default_value() {
AttrValue::Integer(i) => *i,
_ => -1,
}
}
#[zbus(property)]
async fn possible_values(&self) -> Vec<i32> {
match self.0.possible_values() {
AttrValue::EnumInt(i) => i.clone(),
_ => Vec::default(),
}
}
#[zbus(property)]
async fn current_value(&self) -> fdo::Result<i32> {
if let Ok(v) = self.0.current_value() {
if let AttrValue::Integer(i) = v {
return Ok(i);
}
}
Err(fdo::Error::Failed(
"Could not read current value".to_string(),
))
}
#[zbus(property)]
async fn set_current_value(&mut self, value: i32) -> fdo::Result<()> {
Ok(self
.0
.set_current_value(AttrValue::Integer(value))
.map_err(|e| {
error!("Could not set value: {e:?}");
e
})?)
}
}

View File

@@ -1,89 +0,0 @@
use log::error;
use rog_platform::firmware_attributes::{AttrType, AttrValue, Attribute};
use serde::{Deserialize, Serialize};
use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Type, Value};
use zbus::{fdo, interface, Connection};
use crate::error::RogError;
use crate::ASUS_ZBUS_PATH;
const MOD_NAME: &str = "asus_armoury";
#[derive(Debug, Default, Clone, Deserialize, Serialize, Type, Value, OwnedValue)]
pub struct PossibleValues {
strings: Vec<String>,
nums: Vec<i32>,
}
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);
impl AsusArmouryAttribute {
pub fn new(attr: Attribute) -> Self {
Self(attr)
}
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());
connection
.object_server()
.at(path.clone(), self)
.await
.map_err(|e| error!("Couldn't add server at path: {path}, {e:?}"))
.ok();
Ok(())
}
}
#[interface(name = "org.asuslinux.AsusArmoury")]
impl AsusArmouryAttribute {
#[zbus(property)]
async fn name(&self) -> String {
self.0.name().to_string()
}
#[zbus(property)]
async fn default_value(&self) -> String {
match self.0.default_value() {
AttrValue::String(s) => *s,
_ => String::default(),
}
}
#[zbus(property)]
async fn possible_values(&self) -> Vec<String> {
match self.0.possible_values() {
AttrValue::EnumStr(s) => s.clone(),
_ => Vec::default(),
}
}
#[zbus(property)]
async fn current_value(&self) -> fdo::Result<String> {
if let Ok(v) = self.0.current_value() {
if let AttrValue::String(s) = v {
return Ok(s);
}
}
Err(fdo::Error::Failed(
"Could not read current value".to_string(),
))
}
#[zbus(property)]
async fn set_current_value(&mut self, value: String) -> fdo::Result<()> {
Ok(self
.0
.set_current_value(AttrValue::String(value))
.map_err(|e| {
error!("Could not set value: {e:?}");
e
})?)
}
}

View File

@@ -1,33 +0,0 @@
use rog_platform::firmware_attributes::{AttrType, FirmwareAttributes};
use zbus::Connection;
use crate::error::RogError;
pub mod attr_enum_int;
pub mod attr_enum_str;
pub mod attr_int;
pub async fn start_attributes_zbus(server: &Connection) -> Result<(), RogError> {
for attr in FirmwareAttributes::new().attributes() {
match attr.attribute_type() {
AttrType::MinMax => {
attr_int::AsusArmouryAttribute::new(attr.clone())
.start_tasks(server)
.await?;
}
AttrType::EnumInt => {
attr_enum_int::AsusArmouryAttribute::new(attr.clone())
.start_tasks(server)
.await?;
}
AttrType::EnumStr => {
attr_enum_str::AsusArmouryAttribute::new(attr.clone())
.start_tasks(server)
.await?;
}
AttrType::Unbounded => {}
}
}
Ok(())
}