Janky config save/load. Fine for builtin modes

This commit is contained in:
Luke
2020-04-16 21:55:49 +12:00
parent 0a88b5b20a
commit 5f8ea365ef
7 changed files with 106 additions and 4 deletions

29
Cargo.lock generated
View File

@@ -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"

View File

@@ -1,6 +1,6 @@
[package]
name = "rog-core"
version = "0.3.0"
version = "0.3.1"
authors = ["Luke <luke@ljones.dev>"]
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"

View File

@@ -43,15 +43,20 @@ Some commands may have subcommands:
rog-core <command> <subcommand> --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

49
src/config.rs Normal file
View File

@@ -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<u8>,
}
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");
}
}

View File

@@ -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;

View File

@@ -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<dyn Error>> {
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<dyn Error>> {
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])
}

View File

@@ -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 {