Use atomics for dbus thread. Update deps

This commit is contained in:
Luke
2020-04-23 10:36:11 +12:00
parent 8c1166c615
commit 7b3c2acd5e
3 changed files with 51 additions and 53 deletions

24
Cargo.lock generated
View File

@@ -82,9 +82,9 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.50" version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" checksum = "c3d87b23d6a92cd03af510a5ade527033f6aa6fa92161e2d5863a907d4c5e31d"
[[package]] [[package]]
name = "cexpr" name = "cexpr"
@@ -138,9 +138,9 @@ dependencies = [
[[package]] [[package]]
name = "dbus" name = "dbus"
version = "0.7.1" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add8dd36d6d34a084220eb9fe216d3e230d52b37c31702e1ffda4fb2d4ef950e" checksum = "38f8875bb7afbc20dec12db09e18af3dcbd672b08592d2932950326a6437c616"
dependencies = [ dependencies = [
"libc", "libc",
"libdbus-sys", "libdbus-sys",
@@ -148,18 +148,18 @@ dependencies = [
[[package]] [[package]]
name = "enumflags2" name = "enumflags2"
version = "0.6.3" version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a80e524ebf194285b57e5e7944018721c7fffc673253f5183f7accd88a2a3b0c" checksum = "83c8d82922337cd23a15f88b70d8e4ef5f11da38dd7cdb55e84dd5de99695da0"
dependencies = [ dependencies = [
"enumflags2_derive", "enumflags2_derive",
] ]
[[package]] [[package]]
name = "enumflags2_derive" name = "enumflags2_derive"
version = "0.6.3" version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ed9afacaea0301eefb738c9deea725e6d53938004597cdc518a8cf9a7aa2f03" checksum = "946ee94e3dbf58fdd324f9ce245c7b238d46a66f00e86a020b71996349e46cce"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -472,9 +472,9 @@ dependencies = [
[[package]] [[package]]
name = "sysfs-class" name = "sysfs-class"
version = "0.1.2" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "718e4a5d0e144ff01e05bb8618a4997d8901cdec74cf25826bca2535ef406f3e" checksum = "5e1bbcf869732c45a77898f7f61ed6d411dfc37613517e444842f58d428856d1"
dependencies = [ dependencies = [
"numtoa", "numtoa",
] ]
@@ -555,9 +555,9 @@ dependencies = [
[[package]] [[package]]
name = "uhid-virt" name = "uhid-virt"
version = "0.0.3" version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03cee38747e38b73c5dd979a88eebc20829b78e785c2fb120c452305231a6340" checksum = "057ecb2608f960fd97c220557339335bf7cef77ae77b71fb342d9555e396c00c"
dependencies = [ dependencies = [
"enumflags2", "enumflags2",
"libc", "libc",

View File

@@ -13,16 +13,16 @@ name = "rog-core"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
rusb = "0.5" rusb = "^0.5.5"
gumdrop = "0.8" gumdrop = "^0.8.0"
dbus = "0.7" dbus = "^0.8.2"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
toml = "0.5" toml = "0.5"
sysfs-class = "0.1.2" sysfs-class = "^0.1.2"
aho-corasick = "0.7" aho-corasick = "^0.7.10"
thiserror = "1.0.15" thiserror = "^1.0.15"
log = "0.4" log = "^0.4.8"
uhid-virt = "0.0.3" uhid-virt = "^0.0.4"
#keycode = "0.3" #keycode = "0.3"
env_logger = "0.7" env_logger = "^0.7.1"

View File

@@ -9,8 +9,8 @@ use dbus::{
}; };
use log::{error, info, warn}; use log::{error, info, warn};
use std::error::Error; use std::error::Error;
use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use std::{cell::RefCell, rc::Rc};
pub fn start_daemon() -> Result<(), Box<dyn Error>> { pub fn start_daemon() -> Result<(), Box<dyn Error>> {
let laptop = match_laptop(); let laptop = match_laptop();
@@ -36,32 +36,27 @@ pub fn start_daemon() -> Result<(), Box<dyn Error>> {
}, },
); );
connection.request_name(DBUS_IFACE, false, true, false)?; connection.request_name(DBUS_IFACE, false, true, false)?;
let factory = Factory::new_fnmut::<()>(); let factory = Factory::new_sync::<()>();
let daemon = Rc::new(RefCell::new(rogcore)); let daemon = Arc::new(Mutex::new(rogcore));
// We create a tree with one object path inside and make that path introspectable.
let tree = factory.tree(()).add( let tree = factory.tree(()).add(
factory.object_path(DBUS_PATH, ()).introspectable().add( factory.object_path(DBUS_PATH, ()).add(
// We add an interface to the object path... factory.interface(DBUS_IFACE, ()).add_m(
factory factory
.interface(DBUS_IFACE, ()) // method for ledmessage
// ...and a method inside the interface .method("ledmessage", (), {
.add_m( let daemon = daemon.clone();
factory let supported = Vec::from(laptop.supported_modes());
.method("ledmessage", (), { move |m| {
let daemon = daemon.clone(); if let Ok(mut lock) = daemon.try_lock() {
let supported = Vec::from(laptop.supported_modes());
move |m| {
// Reads the args passed to the method
let bytes: Vec<u8> = m.msg.read1()?; let bytes: Vec<u8> = m.msg.read1()?;
match daemon match lock.aura_set_and_save(&supported, &bytes[..]) {
.borrow_mut()
.aura_set_and_save(&supported, &bytes[..])
{
Ok(_) => { Ok(_) => {
let s = format!("Wrote {:x?}", bytes); let mret = m
let mret = m.msg.method_return().append1(&s); .msg
.method_return()
.append1(&format!("Wrote {:x?}", bytes));
Ok(vec![mret]) Ok(vec![mret])
} }
Err(err) => { Err(err) => {
@@ -69,17 +64,19 @@ pub fn start_daemon() -> Result<(), Box<dyn Error>> {
Err(MethodErr::failed(&err)) Err(MethodErr::failed(&err))
} }
} }
} else {
Err(MethodErr::failed("Could not lock daemon for access"))
} }
}) }
// Input? })
.outarg::<&str, _>("reply") .outarg::<&str, _>("reply")
.inarg::<Vec<u8>, _>("bytearray"), .inarg::<Vec<u8>, _>("bytearray"),
), ),
), ),
); );
// We add the tree to the connection so that incoming method calls will be handled. // We add the tree to the connection so that incoming method calls will be handled.
tree.start_receive(&connection); tree.start_receive_send(&connection);
loop { loop {
connection connection
@@ -90,9 +87,10 @@ pub fn start_daemon() -> Result<(), Box<dyn Error>> {
}); });
// TODO: this needs to move to a thread, but there is unsafety // TODO: this needs to move to a thread, but there is unsafety
let mut borrowed_daemon = daemon.borrow_mut(); if let Ok(mut lock) = daemon.try_lock() {
laptop.run(&mut borrowed_daemon).unwrap_or_else(|err| { laptop.run(&mut lock).unwrap_or_else(|err| {
error!("{:?}", err); error!("{:?}", err);
}); });
}
} }
} }