Skip to content

Commit

Permalink
Further improve the daemon controller pattern and reduce cloned code
Browse files Browse the repository at this point in the history
  • Loading branch information
flukejones committed Sep 23, 2022
1 parent 7ea1f41 commit 30550aa
Show file tree
Hide file tree
Showing 21 changed files with 382 additions and 416 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `PanelOd` (form PanelOverdrive)
- `SetPanelOd`
- `NotifyPanelOd`
- Path `/org/asuslinux/Charge` changed to `/org/asuslinux/Power`

## [v4.4.0] - 2022-08-29
### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions asus-notify/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use notify_rust::{Hint, Notification, NotificationHandle};
use rog_aura::AuraEffect;
use rog_dbus::{
zbus_charge::ChargeProxy, zbus_led::LedProxy, zbus_platform::RogBiosProxy,
zbus_led::LedProxy, zbus_platform::RogBiosProxy, zbus_power::PowerProxy,
zbus_profile::ProfileProxy,
};
use rog_profiles::Profile;
Expand Down Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
executor
.spawn(async move {
let conn = zbus::Connection::system().await.unwrap();
let proxy = ChargeProxy::new(&conn).await.unwrap();
let proxy = PowerProxy::new(&conn).await.unwrap();
if let Ok(p) = proxy.receive_notify_charge_control_end_threshold().await {
p.for_each(|e| {
if let Ok(out) = e.args() {
Expand Down
2 changes: 1 addition & 1 deletion daemon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "daemon"
version = "4.5.0-rc1"
version = "4.5.0-rc2"
license = "MPL-2.0"
readme = "README.md"
authors = ["Luke <[email protected]>"]
Expand Down
100 changes: 6 additions & 94 deletions daemon/src/ctrl_anime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
pub mod config;
pub mod zbus;
/// Implements CtrlTask, Reloadable, ZbusRun
pub mod trait_impls;

use ::zbus::export::futures_util::lock::{Mutex, MutexGuard};
use ::zbus::SignalContext;
use async_trait::async_trait;
use self::config::{AnimeConfig, AnimeConfigCached};
use crate::{error::RogError, GetSupported};
use ::zbus::export::futures_util::lock::Mutex;
use log::{error, info, warn};
use rog_anime::{
error::AnimeError,
usb::{
get_anime_type, pkt_for_apply, pkt_for_flush, pkt_for_set_boot, pkt_for_set_on,
pkts_for_init,
},
usb::{get_anime_type, pkt_for_flush, pkts_for_init},
ActionData, AnimeDataBuffer, AnimePacketType, AnimeType,
};
use rog_platform::{hid_raw::HidRaw, supported::AnimeSupportedFunctions, usb_raw::USBRaw};
use std::sync::atomic::{AtomicBool, Ordering};
use std::{convert::TryFrom, error::Error, sync::Arc, thread::sleep};

use crate::{error::RogError, GetSupported};

use self::config::{AnimeConfig, AnimeConfigCached};

impl GetSupported for CtrlAnime {
type A = AnimeSupportedFunctions;

Expand Down Expand Up @@ -214,85 +208,3 @@ impl CtrlAnime {
Ok(())
}
}

pub struct CtrlAnimeTask {
inner: Arc<Mutex<CtrlAnime>>,
}

impl CtrlAnimeTask {
pub async fn new(inner: Arc<Mutex<CtrlAnime>>) -> CtrlAnimeTask {
Self { inner }
}
}

#[async_trait]
impl crate::CtrlTask for CtrlAnimeTask {
async fn create_tasks(&self, _: SignalContext<'static>) -> Result<(), RogError> {
let run_action =
|start: bool, lock: MutexGuard<CtrlAnime>, inner: Arc<Mutex<CtrlAnime>>| {
if start {
info!("CtrlAnimeTask running sleep animation");
CtrlAnime::run_thread(inner.clone(), lock.cache.shutdown.clone(), true);
} else {
info!("CtrlAnimeTask running wake animation");
CtrlAnime::run_thread(inner.clone(), lock.cache.wake.clone(), true);
}
};

let inner1 = self.inner.clone();
let inner2 = self.inner.clone();
let inner3 = self.inner.clone();
let inner4 = self.inner.clone();
self.create_sys_event_tasks(
// Loop is required to try an attempt to get the mutex *without* blocking
// other threads - it is possible to end up with deadlocks otherwise.
move || loop {
if let Some(lock) = inner1.try_lock() {
run_action(true, lock, inner1.clone());
break;
}
},
move || loop {
if let Some(lock) = inner2.try_lock() {
run_action(false, lock, inner2.clone());
break;
}
},
move || loop {
if let Some(lock) = inner3.try_lock() {
run_action(true, lock, inner3.clone());
break;
}
},
move || loop {
if let Some(lock) = inner4.try_lock() {
run_action(false, lock, inner4.clone());
break;
}
},
)
.await;

Ok(())
}
}

pub struct CtrlAnimeReloader(pub Arc<Mutex<CtrlAnime>>);

#[async_trait]
impl crate::Reloadable for CtrlAnimeReloader {
async fn reload(&mut self) -> Result<(), RogError> {
if let Some(lock) = self.0.try_lock() {
lock.node
.write_bytes(&pkt_for_set_on(lock.config.awake_enabled))?;
lock.node.write_bytes(&pkt_for_apply())?;
lock.node
.write_bytes(&pkt_for_set_boot(lock.config.boot_anim_enabled))?;
lock.node.write_bytes(&pkt_for_apply())?;

let action = lock.cache.boot.clone();
CtrlAnime::run_thread(self.0.clone(), action, true);
}
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
use super::CtrlAnime;
use crate::error::RogError;
use async_trait::async_trait;
use log::warn;
use log::{info, warn};
use rog_anime::{
usb::{pkt_for_apply, pkt_for_set_boot, pkt_for_set_on},
AnimeDataBuffer, AnimePowerStates,
};
use zbus::{dbus_interface, export::futures_util::lock::Mutex, Connection, SignalContext};

use std::sync::{atomic::Ordering, Arc};
use zbus::{
dbus_interface,
export::futures_util::lock::{Mutex, MutexGuard},
Connection, SignalContext,
};

use super::CtrlAnime;
pub(super) const ZBUS_PATH: &str = "/org/asuslinux/Anime";

#[derive(Clone)]
pub struct CtrlAnimeZbus(pub Arc<Mutex<CtrlAnime>>);

/// The struct with the main dbus methods requires this trait
#[async_trait]
impl crate::ZbusRun for CtrlAnimeZbus {
async fn add_to_server(self, server: &mut Connection) {
Self::add_to_server_helper(self, "/org/asuslinux/Anime", server).await;
Self::add_to_server_helper(self, ZBUS_PATH, server).await;
}
}

Expand Down Expand Up @@ -134,3 +140,77 @@ impl CtrlAnimeZbus {
data: AnimePowerStates,
) -> zbus::Result<()>;
}

#[async_trait]
impl crate::CtrlTask for CtrlAnimeZbus {
fn zbus_path() -> &'static str {
ZBUS_PATH
}

async fn create_tasks(&self, _: SignalContext<'static>) -> Result<(), RogError> {
let run_action =
|start: bool, lock: MutexGuard<CtrlAnime>, inner: Arc<Mutex<CtrlAnime>>| {
if start {
info!("CtrlAnimeTask running sleep animation");
CtrlAnime::run_thread(inner.clone(), lock.cache.shutdown.clone(), true);
} else {
info!("CtrlAnimeTask running wake animation");
CtrlAnime::run_thread(inner.clone(), lock.cache.wake.clone(), true);
}
};

let inner1 = self.0.clone();
let inner2 = self.0.clone();
let inner3 = self.0.clone();
let inner4 = self.0.clone();
self.create_sys_event_tasks(
// Loop is required to try an attempt to get the mutex *without* blocking
// other threads - it is possible to end up with deadlocks otherwise.
move || loop {
if let Some(lock) = inner1.try_lock() {
run_action(true, lock, inner1.clone());
break;
}
},
move || loop {
if let Some(lock) = inner2.try_lock() {
run_action(false, lock, inner2.clone());
break;
}
},
move || loop {
if let Some(lock) = inner3.try_lock() {
run_action(true, lock, inner3.clone());
break;
}
},
move || loop {
if let Some(lock) = inner4.try_lock() {
run_action(false, lock, inner4.clone());
break;
}
},
)
.await;

Ok(())
}
}

#[async_trait]
impl crate::Reloadable for CtrlAnimeZbus {
async fn reload(&mut self) -> Result<(), RogError> {
if let Some(lock) = self.0.try_lock() {
lock.node
.write_bytes(&pkt_for_set_on(lock.config.awake_enabled))?;
lock.node.write_bytes(&pkt_for_apply())?;
lock.node
.write_bytes(&pkt_for_set_boot(lock.config.boot_anim_enabled))?;
lock.node.write_bytes(&pkt_for_apply())?;

let action = lock.cache.boot.clone();
CtrlAnime::run_thread(self.0.clone(), action, true);
}
Ok(())
}
}
Loading

0 comments on commit 30550aa

Please sign in to comment.