From 5f8ea365ef7e19f041364ffa4680cb225a225340 Mon Sep 17 00:00:00 2001 From: Luke Date: Thu, 16 Apr 2020 21:55:49 +1200 Subject: [PATCH] Janky config save/load. Fine for builtin modes --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 5 ++++- README.md | 7 ++++++- src/config.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/core.rs | 1 + src/daemon.rs | 17 +++++++++++++++-- src/main.rs | 2 ++ 7 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index d40f46a0..b82af6ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,6 +168,9 @@ dependencies = [ "dbus", "gumdrop", "rusb", + "serde", + "serde_derive", + "toml", ] [[package]] @@ -181,6 +184,23 @@ dependencies = [ "libusb1-sys", ] +[[package]] +name = "serde" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" + +[[package]] +name = "serde_derive" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.17" @@ -210,6 +230,15 @@ dependencies = [ "xattr", ] +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +dependencies = [ + "serde", +] + [[package]] name = "unicode-xid" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index fb4cfd76..ad537753 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rog-core" -version = "0.3.0" +version = "0.3.1" authors = ["Luke "] edition = "2018" @@ -8,3 +8,6 @@ edition = "2018" rusb = "0.5" gumdrop = "0.8" dbus = "0.7.1" +serde = "1.0" +serde_derive = "1.0" +toml = "0.5" \ No newline at end of file diff --git a/README.md b/README.md index d73fbd1a..46abc753 100644 --- a/README.md +++ b/README.md @@ -43,15 +43,20 @@ Some commands may have subcommands: rog-core --help ``` +## Daemon mode + +Currently the last used brightness and builtin mode will be saved when set, and loaded when the daemon is started. The effect here is the last settings used are the ones loaded on boot. + ## Implemented - [X] Setting/modifying built-in LED modes - [ ] Per-key LED setting - [ ] Fancy LED modes (custom programs) - [X] Daemon mode (functionally done) -- [ ] Saving settings for reload (CLI and daemon mode) +- [X] Saving settings for reload (needs further work) - [ ] System control - [ ] Capture and use hotkeys, e.g, Aura controls to control LEDs +- [ ] Logging - required for journalctl ## Wireshark captures diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 00000000..3c30ae85 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,49 @@ +use crate::{ + aura::{ModeMessage, SetAuraBuiltin}, + CONFIG_PATH, +}; +use serde_derive::{Deserialize, Serialize}; +use std::fs::{File, OpenOptions}; +use std::io::{Read, Write}; + +#[derive(Default, Deserialize, Serialize)] +pub(crate) struct Config { + pub brightness: u8, + pub builtin: Vec, +} + +impl Config { + pub fn read(mut self) -> Self { + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(&CONFIG_PATH) + .expect("config file error"); + let mut buf = String::new(); + if let Ok(l) = file.read_to_string(&mut buf) { + if l == 0 { + // create a default config here + let d = SetAuraBuiltin::default(); + let c = Config { + brightness: 1u8, + builtin: ModeMessage::from(d).0.to_vec(), + }; + let toml = toml::to_string(&c).unwrap(); + file.write_all(toml.as_bytes()) + .expect("Writing default config failed"); + self = c; + } else { + self = toml::from_str(&buf).unwrap(); + } + } + self + } + + pub fn write(&self) { + let mut file = File::create(CONFIG_PATH).expect("Couldn't overwrite config"); + let toml = toml::to_string_pretty(self).expect("Parse config to JSON failed"); + file.write_all(toml.as_bytes()) + .expect("Saving config failed"); + } +} diff --git a/src/core.rs b/src/core.rs index 59912f39..a9b8d120 100644 --- a/src/core.rs +++ b/src/core.rs @@ -127,6 +127,7 @@ impl RogCore { } pub fn aura_brightness_bytes(brightness: u8) -> Result<[u8; 17], Error> { + // TODO: check brightness range let mut bright = [0u8; LED_MSG_LEN]; bright[0] = 0x5a; bright[1] = 0xba; diff --git a/src/daemon.rs b/src/daemon.rs index 7120d29b..f3324e21 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,4 +1,4 @@ -use crate::{core::RogCore, DBUS_IFACE, DBUS_PATH}; +use crate::{config::Config, core::RogCore, DBUS_IFACE, DBUS_PATH}; use dbus::{ blocking::Connection, tree::{Factory, MethodErr}, @@ -7,12 +7,18 @@ use std::error::Error; use std::time::Duration; pub fn start_daemon() -> Result<(), Box> { + let mut config = Config::default().read(); + let mut core = RogCore::new()?; + core.aura_set_mode(&config.builtin)?; + let bright = RogCore::aura_brightness_bytes(config.brightness)?; + core.aura_set_mode(&bright)?; + let mut c = Connection::new_system()?; c.request_name(DBUS_IFACE, false, true, false)?; // The choice of factory tells us what type of tree we want, // and if we want any extra data inside. We pick the simplest variant. - let f = Factory::new_fn::<()>(); + let f = Factory::new_fnmut::<()>(); // We create a tree with one object path inside and make that path introspectable. let tree = f.tree(()).add( @@ -30,6 +36,13 @@ pub fn start_daemon() -> Result<(), Box> { if let Ok(mut core) = RogCore::new() { match core.aura_set_mode(&bytes[..]) { Ok(_) => { + // TODO: create statics out of header bytes + if bytes[0] == 0x5a && bytes[1] == 0xba { + config.brightness = bytes[4]; + } else if bytes[0] == 0x5d && bytes[1] == 0xb3 { + config.builtin = bytes; + } + config.write(); let mret = m.msg.method_return().append1(s); Ok(vec![mret]) } diff --git a/src/main.rs b/src/main.rs index 53a481d7..38a1978d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // TODO: use /sys/class/dmi/id/board_name to detect model mod aura; +mod config; mod core; mod daemon; mod error; @@ -12,6 +13,7 @@ use gumdrop::Options; pub static DBUS_NAME: &'static str = "org.rogcore.Daemon"; pub static DBUS_PATH: &'static str = "/org/rogcore/Daemon"; pub static DBUS_IFACE: &'static str = "org.rogcore.Daemon"; +pub static CONFIG_PATH: &'static str = "/etc/rogcore.conf"; #[derive(Debug, Options)] struct CLIStart {