Add dbus methods and CLI args for battery charge limit

This commit is contained in:
Luke
2020-06-26 16:23:37 +12:00
5 changed files with 127 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ use std::error::Error;
use std::sync::Arc;
use tokio::sync::Mutex;
pub(super) type FanModeType = Arc<Mutex<Option<u8>>>;
pub(super) type DbusU8Type = Arc<Mutex<Option<u8>>>;
// Timing is such that:
// - interrupt write is minimum 1ms (sometimes lower)
@@ -93,6 +93,7 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
mut aura_command_recv,
mut animatrix_recv,
fan_mode,
charge_limit,
effect_cancel_signal,
) = dbus_create_tree();
// We add the tree to the connection so that incoming method calls will be handled.
@@ -119,6 +120,16 @@ pub async fn start_daemon() -> Result<(), Box<dyn Error>> {
.unwrap_or_else(|err| warn!("{:?}", err));
}
}
// Charge limit
if let Ok(mut lock) = charge_limit.try_lock() {
if let Some(n) = lock.take() {
let mut config = config1.lock().await;
rogcore
.set_charge_limit(n, &mut config)
.unwrap_or_else(|err| warn!("{:?}", err));
}
}
// Keyboard reads
let data = keyboard_reader.poll_keyboard().await;
if let Some(bytes) = data {
laptop

View File

@@ -22,6 +22,8 @@ struct CLIStart {
bright: Option<LedBrightness>,
#[options(meta = "FAN", help = "<silent, normal, boost>")]
fan_mode: Option<FanLevel>,
#[options(meta = "CHRG", help = "<20-100>")]
charge_limit: Option<u8>,
#[options(command)]
command: Option<Command>,
}
@@ -87,5 +89,11 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(err) => println!("Error: {}", err),
}
}
if let Some(charge_limit) = parsed.charge_limit {
match writer.write_charge_limit(charge_limit) {
Ok(msg) => println!("Daemon response: {}", msg),
Err(err) => println!("Error: {}", err),
}
}
Ok(())
}

View File

@@ -1,4 +1,4 @@
use crate::daemon::FanModeType;
use crate::daemon::DbusU8Type;
use crate::led_control::AuraCommand;
use crate::rogcore::FanLevel;
use dbus::tree::{Factory, MTSync, Method, MethodErr, Signal, Tree};
@@ -138,13 +138,13 @@ pub(super) fn dbus_create_animatrix_method(
.annotate("org.freedesktop.DBus.Method.NoReply", "true")
}
pub(super) fn dbus_create_fan_mode_method(fan_mode: FanModeType) -> Method<MTSync, ()> {
pub(super) fn dbus_create_fan_mode_method(data: DbusU8Type) -> Method<MTSync, ()> {
let factory = Factory::new_sync::<()>();
factory
// method for ledmessage
.method("FanMode", (), {
move |m| {
if let Ok(mut lock) = fan_mode.try_lock() {
if let Ok(mut lock) = data.try_lock() {
let mut iter = m.msg.iter_init();
let byte: u8 = iter.read()?;
*lock = Some(byte);
@@ -162,21 +162,49 @@ pub(super) fn dbus_create_fan_mode_method(fan_mode: FanModeType) -> Method<MTSyn
.inarg::<u8, _>("byte")
}
pub(super) fn dbus_create_charge_limit_method(data: DbusU8Type) -> Method<MTSync, ()> {
let factory = Factory::new_sync::<()>();
factory
// method for ledmessage
.method("ChargeLimit", (), {
move |m| {
if let Ok(mut lock) = data.try_lock() {
let mut iter = m.msg.iter_init();
let byte: u8 = iter.read()?;
*lock = Some(byte);
let mret = m
.msg
.method_return()
.append1(format!("Battery charge limit set to {}", byte));
Ok(vec![mret])
} else {
Err(MethodErr::failed("Could not lock daemon for access"))
}
}
})
.outarg::<&str, _>("reply")
.inarg::<u8, _>("byte")
}
#[allow(clippy::type_complexity)]
pub(super) fn dbus_create_tree() -> (
Tree<MTSync, ()>,
Sender<AuraCommand>,
Receiver<AuraCommand>,
Receiver<Vec<Vec<u8>>>,
FanModeType,
DbusU8Type,
DbusU8Type,
Arc<Signal<()>>,
) {
let (aura_command_send, aura_command_recv) = channel::<AuraCommand>(1);
let (animatrix_send, animatrix_recv) = channel::<Vec<Vec<u8>>>(1);
let fan_mode: FanModeType = Arc::new(Mutex::new(None));
let fan_mode: DbusU8Type = Arc::new(Mutex::new(None));
let charge_limit: DbusU8Type = Arc::new(Mutex::new(None));
let factory = Factory::new_sync::<()>();
let effect_cancel_sig = Arc::new(factory.signal("LedCancelEffect", ()));
let tree = factory
.tree(())
.add(
@@ -194,6 +222,7 @@ pub(super) fn dbus_create_tree() -> (
)))
.add_m(dbus_create_animatrix_method(Mutex::new(animatrix_send)))
.add_m(dbus_create_fan_mode_method(fan_mode.clone()))
.add_m(dbus_create_charge_limit_method(charge_limit.clone()))
.add_s(effect_cancel_sig.clone()),
),
)
@@ -204,6 +233,7 @@ pub(super) fn dbus_create_tree() -> (
aura_command_recv,
animatrix_recv,
fan_mode,
charge_limit,
effect_cancel_sig,
)
}