Merge branch '191-full-power-states-combinations' into 'main'

Combination for power state leds boot/sleep/all/keys/side LEDS

Closes #191

See merge request asus-linux/asusctl!111
This commit is contained in:
Luke Jones
2022-06-19 22:03:23 +00:00
8 changed files with 308 additions and 94 deletions

View File

@@ -53,12 +53,16 @@ pub struct LedModeCommand {
meta = "",
help = "set the keyboard LED to enabled while the device is awake"
)]
pub awake_enable: Option<bool>,
pub boot_enable: Option<bool>,
#[options(
meta = "",
help = "set the keyboard LED suspend animation to enabled while the device is suspended"
)]
pub sleep_enable: Option<bool>,
#[options(meta = "", help = "set the full keyboard LEDs (keys and side) to enabled")]
pub all_leds_enable: Option<bool>,
#[options(meta = "", help = "set the keyboard keys LEDs to enabled")]
pub keys_leds_enable: Option<bool>,
#[options(meta = "", help = "set the keyboard side LEDs to enabled")]
pub side_leds_enable: Option<bool>,
#[options(command)]

View File

@@ -342,8 +342,10 @@ fn handle_led_mode(
if mode.command.is_none()
&& !mode.prev_mode
&& !mode.next_mode
&& mode.boot_enable.is_none()
&& mode.sleep_enable.is_none()
&& mode.awake_enable.is_none()
&& mode.all_leds_enable.is_none()
&& mode.keys_leds_enable.is_none()
&& mode.side_leds_enable.is_none()
{
if !mode.help {
@@ -404,14 +406,20 @@ fn handle_led_mode(
}
}
if let Some(enable) = mode.awake_enable {
dbus.proxies().led().set_awake_enabled(enable)?;
if let Some(enable) = mode.boot_enable {
dbus.proxies().led().set_boot_enabled(enable)?;
}
if let Some(enable) = mode.sleep_enable {
dbus.proxies().led().set_sleep_enabled(enable)?;
}
if let Some(enable) = mode.all_leds_enable {
dbus.proxies().led().set_all_leds_enabled(enable)?;
}
if let Some(enable) = mode.keys_leds_enable {
dbus.proxies().led().set_keys_leds_enabled(enable)?;
}
if let Some(enable) = mode.side_leds_enable {
dbus.proxies().led().set_side_leds_enabled(enable)?;
}

View File

@@ -23,8 +23,10 @@ impl AuraConfigV320 {
current_mode: self.current_mode,
builtins: self.builtins,
multizone: self.multizone,
awake_enabled: true,
boot_anim_enabled: true,
sleep_anim_enabled: true,
all_leds_enabled: true,
keys_leds_enabled: true,
side_leds_enabled: true,
}
}
@@ -45,15 +47,17 @@ impl AuraConfigV352 {
current_mode: self.current_mode,
builtins: self.builtins,
multizone: self.multizone,
awake_enabled: true,
boot_anim_enabled: true,
sleep_anim_enabled: true,
all_leds_enabled: true,
keys_leds_enabled: true,
side_leds_enabled: true,
}
}
}
#[derive(Deserialize, Serialize)]
pub struct AuraConfig {
pub struct AuraConfigV407 {
pub brightness: LedBrightness,
pub current_mode: AuraModeNum,
pub builtins: BTreeMap<AuraModeNum, AuraEffect>,
@@ -63,6 +67,36 @@ pub struct AuraConfig {
pub side_leds_enabled: bool,
}
impl AuraConfigV407 {
pub(crate) fn into_current(self) -> AuraConfig {
AuraConfig {
brightness: self.brightness,
current_mode: self.current_mode,
builtins: self.builtins,
multizone: self.multizone,
boot_anim_enabled: true,
sleep_anim_enabled: self.sleep_anim_enabled,
all_leds_enabled: self.awake_enabled,
keys_leds_enabled: self.awake_enabled,
side_leds_enabled: self.side_leds_enabled,
}
}
}
#[derive(Deserialize, Serialize)]
pub struct AuraConfig {
pub brightness: LedBrightness,
pub current_mode: AuraModeNum,
pub builtins: BTreeMap<AuraModeNum, AuraEffect>,
pub multizone: Option<AuraMultiZone>,
pub boot_anim_enabled: bool,
pub sleep_anim_enabled: bool,
pub all_leds_enabled: bool,
pub keys_leds_enabled: bool,
pub side_leds_enabled: bool
}
impl Default for AuraConfig {
fn default() -> Self {
AuraConfig {
@@ -70,8 +104,10 @@ impl Default for AuraConfig {
current_mode: AuraModeNum::Static,
builtins: BTreeMap::new(),
multizone: None,
awake_enabled: true,
boot_anim_enabled: true,
sleep_anim_enabled: true,
all_leds_enabled: true,
keys_leds_enabled: true,
side_leds_enabled: true,
}
}
@@ -108,6 +144,11 @@ impl AuraConfig {
config.write();
info!("Updated AuraConfig version");
return config;
} else if let Ok(data) = serde_json::from_str::<AuraConfigV407>(&buf) {
let config = data.into_current();
config.write();
info!("Updated AuraConfig version");
return config;
}
warn!(
"Could not deserialise {}.\nWill rename to {}-old and recreate config",

View File

@@ -11,8 +11,7 @@ use log::{error, info, warn};
use logind_zbus::manager::ManagerProxy;
use rog_aura::{
usb::{
LED_APPLY, LED_AWAKE_OFF_SLEEP_OFF, LED_AWAKE_OFF_SLEEP_ON, LED_AWAKE_ON_SLEEP_OFF,
LED_AWAKE_ON_SLEEP_ON, LED_SET, SIDE_LEDS_OFF, SIDE_LEDS_ON,
LED_APPLY, LED_SET
},
AuraEffect, LedBrightness, LED_MSG_LEN,
};
@@ -24,11 +23,14 @@ use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use zbus::Connection;
use rog_aura::usb::leds_message;
use crate::GetSupported;
use super::config::AuraConfig;
impl GetSupported for CtrlKbdLed {
type A = LedSupportedFunctions;
@@ -120,11 +122,6 @@ impl CtrlTask for CtrlKbdLedTask {
// lock.set_brightness(lock.config.brightness)
// .map_err(|e| error!("CtrlKbdLedTask: {e}"))
// .ok();
lock.set_side_leds_states(
lock.config.side_leds_enabled,
)
.map_err(|e| error!("CtrlKbdLedTask: {e}"))
.ok();
if let Some(mode) =
lock.config.builtins.get(&lock.config.current_mode)
{
@@ -165,11 +162,7 @@ impl crate::Reloadable for CtrlKbdLedReloader {
ctrl.do_command(mode).ok();
}
ctrl.set_states_enabled(ctrl.config.awake_enabled, ctrl.config.sleep_anim_enabled)
.map_err(|err| warn!("{err}"))
.ok();
ctrl.set_side_leds_states(ctrl.config.side_leds_enabled)
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{err}"))
.ok();
}
@@ -288,33 +281,23 @@ impl CtrlKbdLed {
self.set_brightness(self.config.brightness)
}
/// Set if awake/on LED active, and/or sleep animation active
pub(super) fn set_states_enabled(&self, awake: bool, sleep: bool) -> Result<(), RogError> {
let bytes = if awake && sleep {
LED_AWAKE_ON_SLEEP_ON
} else if awake && !sleep {
LED_AWAKE_ON_SLEEP_OFF
} else if !awake && sleep {
LED_AWAKE_OFF_SLEEP_ON
} else if !awake && !sleep {
LED_AWAKE_OFF_SLEEP_OFF
} else {
LED_AWAKE_ON_SLEEP_ON
};
self.write_bytes(&bytes)?;
self.write_bytes(&LED_SET)?;
// Changes won't persist unless apply is set
self.write_bytes(&LED_APPLY)?;
Ok(())
}
pub(super) fn set_side_leds_states(&self, activated: bool) -> Result<(), RogError> {
let bytes: [u8; LED_MSG_LEN] = if activated {
SIDE_LEDS_ON
} else {
SIDE_LEDS_OFF
};
self.write_bytes(&bytes)?;
/// Set combination state for boot animation/sleep animation/all leds/keys leds/side leds LED active
pub(super) fn set_power_states(&self, config: &AuraConfig) -> Result<(), RogError> {
let bytes = leds_message(config.boot_anim_enabled,
config.sleep_anim_enabled,
config.all_leds_enabled,
config.keys_leds_enabled,
config.side_leds_enabled);
// Quite ugly, must be a more idiomatic way to do
let message = [
0x5d, 0xbd, 0x01, bytes[0], bytes[1], bytes[2], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
self.write_bytes(&message)?;
self.write_bytes(&LED_SET)?;
// Changes won't persist unless apply is set
self.write_bytes(&LED_APPLY)?;

View File

@@ -27,22 +27,26 @@ impl CtrlKbdLedZbus {
}
/// Set the keyboard LED to enabled while the device is awake
async fn set_awake_enabled(
async fn set_boot_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.set_states_enabled(enabled, ctrl.config.sleep_anim_enabled)
.map_err(|err| warn!("{}", err))
.ok();
ctrl.config.awake_enabled = enabled;
ctrl.config.boot_anim_enabled = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(LedPowerStates {
enabled: ctrl.config.awake_enabled,
sleep_anim_enabled: ctrl.config.sleep_anim_enabled,
boot_anim: ctrl.config.boot_anim_enabled,
sleep_anim: ctrl.config.sleep_anim_enabled,
all_leds: ctrl.config.all_leds_enabled,
keys_leds: ctrl.config.keys_leds_enabled,
side_leds: ctrl.config.side_leds_enabled
});
}
// Need to pull state out like this due to MutexGuard
@@ -61,14 +65,19 @@ impl CtrlKbdLedZbus {
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.set_states_enabled(ctrl.config.awake_enabled, enabled)
.map_err(|err| warn!("{}", err))
.ok();
ctrl.config.sleep_anim_enabled = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(LedPowerStates {
enabled: ctrl.config.awake_enabled,
sleep_anim_enabled: ctrl.config.sleep_anim_enabled,
boot_anim: ctrl.config.boot_anim_enabled,
sleep_anim: ctrl.config.sleep_anim_enabled,
all_leds: ctrl.config.all_leds_enabled,
keys_leds: ctrl.config.keys_leds_enabled,
side_leds: ctrl.config.side_leds_enabled
});
}
if let Some(states) = states {
@@ -78,23 +87,96 @@ impl CtrlKbdLedZbus {
}
}
/// Set all the keyboard LEDs (keys and side) to enabled
async fn set_all_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.all_leds_enabled = enabled;
ctrl.config.keys_leds_enabled = enabled;
ctrl.config.side_leds_enabled = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(LedPowerStates {
boot_anim: ctrl.config.boot_anim_enabled,
sleep_anim: ctrl.config.sleep_anim_enabled,
all_leds: ctrl.config.all_leds_enabled,
keys_leds: ctrl.config.keys_leds_enabled,
side_leds: ctrl.config.side_leds_enabled
});
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
}
/// Set the keyboard keys LEDs to enabled
async fn set_keys_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.config.keys_leds_enabled = enabled;
ctrl.config.write();
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(LedPowerStates {
boot_anim: ctrl.config.boot_anim_enabled,
sleep_anim: ctrl.config.sleep_anim_enabled,
all_leds: ctrl.config.all_leds_enabled,
keys_leds: ctrl.config.keys_leds_enabled,
side_leds: ctrl.config.side_leds_enabled
});
}
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
}
/// Set the keyboard side LEDs to enabled
async fn set_side_leds_enabled(
&mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
enabled: bool,
) {
let mut led = None;
let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() {
ctrl.set_side_leds_states(enabled)
.map_err(|err| warn!("{}", err))
.ok();
ctrl.config.side_leds_enabled = enabled;
ctrl.config.write();
led = Some(enabled);
ctrl.set_power_states(&ctrl.config)
.map_err(|err| warn!("{}", err))
.ok();
states = Some(LedPowerStates {
boot_anim: ctrl.config.boot_anim_enabled,
sleep_anim: ctrl.config.sleep_anim_enabled,
all_leds: ctrl.config.all_leds_enabled,
keys_leds: ctrl.config.keys_leds_enabled,
side_leds: ctrl.config.side_leds_enabled
});
}
if let Some(led) = led {
Self::notify_side_leds(&ctxt, led)
// Need to pull state out like this due to MutexGuard
if let Some(states) = states {
Self::notify_power_states(&ctxt, &states)
.await
.unwrap_or_else(|err| warn!("{}", err));
}
@@ -174,9 +256,9 @@ impl CtrlKbdLedZbus {
}
#[dbus_interface(property)]
async fn awake_enabled(&self) -> bool {
async fn boot_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.awake_enabled;
return ctrl.config.boot_anim_enabled;
}
true
}
@@ -189,6 +271,22 @@ impl CtrlKbdLedZbus {
true
}
#[dbus_interface(property)]
async fn all_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.all_leds_enabled;
}
true
}
#[dbus_interface(property)]
async fn keys_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
return ctrl.config.keys_leds_enabled;
}
true
}
#[dbus_interface(property)]
fn side_leds_enabled(&self) -> bool {
if let Ok(ctrl) = self.0.try_lock() {
@@ -236,9 +334,6 @@ impl CtrlKbdLedZbus {
#[dbus_interface(signal)]
async fn notify_led(signal_ctxt: &SignalContext<'_>, data: AuraEffect) -> zbus::Result<()>;
#[dbus_interface(signal)]
async fn notify_side_leds(signal_ctxt: &SignalContext<'_>, data: bool) -> zbus::Result<()>;
#[dbus_interface(signal)]
async fn notify_power_states(
signal_ctxt: &SignalContext<'_>,

View File

@@ -14,8 +14,11 @@ use crate::{error::Error, LED_MSG_LEN};
#[cfg_attr(feature = "dbus", derive(Type))]
#[derive(Debug, PartialEq, Copy, Clone, Deserialize, Serialize)]
pub struct LedPowerStates {
pub enabled: bool,
pub sleep_anim_enabled: bool,
pub boot_anim: bool,
pub sleep_anim: bool,
pub all_leds: bool,
pub keys_leds: bool,
pub side_leds: bool,
}
#[cfg_attr(feature = "dbus", derive(Type))]

View File

@@ -1,3 +1,7 @@
use std::convert::TryFrom;
use std::ops::{BitAnd, BitOr};
use crate::usb::LedCfgState::{Off, On};
pub const LED_INIT1: [u8; 2] = [0x5d, 0xb9];
pub const LED_INIT2: &str = "]ASUS Tech.Inc."; // ] == 0x5d
pub const LED_INIT3: [u8; 6] = [0x5d, 0x05, 0x20, 0x31, 0, 0x08];
@@ -8,6 +12,13 @@ pub const LED_INIT5: [u8; 6] = [0x5e, 0x05, 0x20, 0x31, 0, 0x08];
pub const LED_APPLY: [u8; 17] = [0x5d, 0xb4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const LED_SET: [u8; 17] = [0x5d, 0xb5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const BOOT_MASK:i32 = 0xc31309;
pub const SLEEP_MASK:i32 = 0x300904;
pub const ALL_LEDS_MASK:i32 = 0x000002;
pub const KBD_LEDS_MASK:i32 = 0x080000;
pub const SIDE_LEDS_MASK:i32 = 0x040500;
pub const LEDS_STATE_MASK:i32 = ALL_LEDS_MASK | KBD_LEDS_MASK | SIDE_LEDS_MASK;
/// Writes out the correct byte string for brightness
pub const fn aura_brightness_bytes(brightness: u8) -> [u8; 17] {
[
@@ -15,26 +26,92 @@ pub const fn aura_brightness_bytes(brightness: u8) -> [u8; 17] {
]
}
pub const LED_AWAKE_ON_SLEEP_OFF: [u8; 17] = [
0x5d, 0xbd, 0x01, 0xcf, 0x17, 0x0b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
#[derive(Clone, Copy)]
pub enum LedCfgState {
On = 0xffffff,
Off = 0x0
}
pub const LED_AWAKE_ON_SLEEP_ON: [u8; 17] = [
0x5d, 0xbd, 0x01, 0xff, 0x1f, 0x0f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
impl From<i32> for LedCfgState {
fn from(state: i32) -> Self {
match state {
0xffffff => On,
0x0 => Off,
_ => Off
}
}
}
pub const LED_AWAKE_OFF_SLEEP_OFF: [u8; 17] = [
0x5d, 0xbd, 0x01, 0xc3, 0x13, 0x09, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
impl From<bool> for LedCfgState {
fn from(state: bool) -> Self {
match state {
true => On,
false => Off
}
}
}
pub const LED_AWAKE_OFF_SLEEP_ON: [u8; 17] = [
0x5d, 0xbd, 0x01, 0xf3, 0x1b, 0x0d, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
impl TryFrom <[u8; 3]> for LedCfgState {
type Error = &'static str;
pub const SIDE_LEDS_OFF: [u8; 17] = [
0x5d, 0xbd, 0x01, 0x08, 0x00, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
fn try_from(value: [u8; 3]) -> Result<Self, Self::Error> {
match value {
[0xff, 0xff, 0xff] => Ok(On),
[0, 0, 0] => Ok(Off),
_ => Err("Unconvertible value")
}
}
}
pub const SIDE_LEDS_ON: [u8; 17] = [
0x5d, 0xbd, 0x01, 0x0c, 0x05, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
impl BitAnd<LedCfgState> for i32 {
type Output = i32;
fn bitand(self, rhs: LedCfgState) -> i32 {
return self & rhs as i32
}
}
impl BitOr<LedCfgState> for i32 {
type Output = i32;
fn bitor(self, rhs: LedCfgState) -> Self::Output {
return self | rhs as i32
}
}
impl BitOr<LedCfgState> for LedCfgState {
type Output = i32;
fn bitor(self, rhs: LedCfgState) -> i32 {
return self as i32 | rhs as i32;
}
}
impl BitAnd<LedCfgState> for LedCfgState {
type Output = LedCfgState;
fn bitand(self, rhs: LedCfgState) -> LedCfgState {
return (self as i32 & rhs as i32).into();
}
}
pub fn leds_message (boot_state: bool, sleep_state: bool, all_leds_state: bool, kbd_leds_state: bool, side_leds_state: bool) -> [u8; 3] {
let raw_message = _leds_message(boot_state.into(), sleep_state.into(), all_leds_state.into(), kbd_leds_state.into(), side_leds_state.into());
let [_, lows @ ..] = i32::to_be_bytes(raw_message);
return lows;
}
fn _leds_message (boot_state: LedCfgState, sleep_state: LedCfgState, all_leds_state: LedCfgState, kbd_leds_state: LedCfgState, side_leds_state: LedCfgState) -> i32 {
let full_leds_state = match all_leds_state {
On => (ALL_LEDS_MASK & all_leds_state) | (KBD_LEDS_MASK & kbd_leds_state) | (SIDE_LEDS_MASK & side_leds_state),
Off => 0x0100 & side_leds_state,
};
let boot_xor_sleep = (BOOT_MASK & boot_state) ^ (SLEEP_MASK & sleep_state);
return match (all_leds_state | kbd_leds_state | side_leds_state).into() {
On => boot_xor_sleep ^ ((boot_xor_sleep ^ full_leds_state) & LEDS_STATE_MASK),
_ => boot_xor_sleep
}
}

View File

@@ -50,11 +50,17 @@ trait Led {
fn set_led_mode(&self, effect: &AuraEffect) -> zbus::Result<()>;
/// SetAwakeEnabled method
fn set_awake_enabled(&self, enabled: bool) -> zbus::Result<()>;
fn set_boot_enabled(&self, enabled: bool) -> zbus::Result<()>;
/// SetSleepEnabled method
fn set_sleep_enabled(&self, enabled: bool) -> zbus::Result<()>;
/// SetSideLedsEnabled method
fn set_all_leds_enabled(&self, enabled: bool) -> Result<()>;
/// SetSideLedsEnabled method
fn set_keys_leds_enabled(&self, enabled: bool) -> Result<()>;
/// SetSideLedsEnabled method
fn set_side_leds_enabled(&self, enabled: bool) -> Result<()>;
@@ -62,9 +68,6 @@ trait Led {
#[dbus_proxy(signal)]
fn notify_led(&self, data: AuraEffect) -> zbus::Result<()>;
#[dbus_proxy(signal)]
fn notify_side_leds(&self, data: bool) -> zbus::Result<()>;
#[dbus_proxy(signal)]
fn notify_power_states(&self, data: LedPowerStates) -> zbus::Result<()>;