Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
Merged commit balena-io-modules#132, Updated dbus crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Stjin committed May 28, 2022
1 parent 09b8034 commit cfa667e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ categories = ["api-bindings", "network-programming", "os::unix-apis"]
license = "Apache-2.0"

[dependencies]
dbus = "0.5"
dbus = "0.7"
futures = "0.1"
futures-cpupool = "0.1"
tokio-timer = "0.1"
Expand Down
55 changes: 28 additions & 27 deletions src/dbus_api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dbus::Connection as DBusConnection;
use dbus::{BusType, ConnPath, Message, Path};
use dbus::arg::{Array, Get, Iter, RefArg, Variant};
use dbus::stdintf::OrgFreedesktopDBusProperties;
use dbus::ffidisp::stdintf::OrgFreedesktopDBusProperties;
use dbus::ffidisp::{BusType, ConnPath, Connection as DBusConnection};
use dbus::{Message, Path};

use errors::*;

Expand Down Expand Up @@ -46,7 +46,7 @@ impl DBusApi {
path: &str,
interface: &str,
method: &str,
args: &[&RefArg],
args: &[&dyn RefArg],
) -> Result<Message> {
self.call_with_args_retry(path, interface, method, args)
.map_err(|e| {
Expand All @@ -61,7 +61,7 @@ impl DBusApi {
path: &str,
interface: &str,
method: &str,
args: &[&RefArg],
args: &[&dyn RefArg],
) -> Result<Message> {
let mut retries = 0;

Expand Down Expand Up @@ -93,7 +93,7 @@ impl DBusApi {
path: &str,
interface: &str,
method: &str,
args: &[&RefArg],
args: &[&dyn RefArg],
) -> Option<Result<Message>> {
match Message::new_method_call(self.base, path, interface, method) {
Ok(mut message) => {
Expand All @@ -102,13 +102,14 @@ impl DBusApi {
}

self.send_message_checked(message)
},
}
Err(details) => Some(Err(ErrorKind::DBusAPI(details).into())),
}
}

fn send_message_checked(&self, message: Message) -> Option<Result<Message>> {
match self.connection
match self
.connection
.send_with_reply_and_block(message, self.method_timeout as i32 * 1000)
{
Ok(response) => Some(Ok(response)),
Expand All @@ -125,13 +126,13 @@ impl DBusApi {
}

Some(Err(Error::from(e)))
},
}
}
}

pub fn property<T>(&self, path: &str, interface: &str, name: &str) -> Result<T>
where
DBusApi: VariantTo<T>,
where
DBusApi: VariantTo<T>,
{
let property_error = |details: &str, err: bool| {
let message = format!(
Expand Down Expand Up @@ -159,23 +160,23 @@ impl DBusApi {
None => property_error("no details", false),
};
Err(e).chain_err(|| dbus_err)
},
}
}
}

pub fn extract<'a, T>(&self, response: &'a Message) -> Result<T>
where
T: Get<'a>,
where
T: Get<'a>,
{
response
.get1()
.ok_or_else(|| ErrorKind::DBusAPI("Wrong response type".into()).into())
}

pub fn extract_two<'a, T1, T2>(&self, response: &'a Message) -> Result<(T1, T2)>
where
T1: Get<'a>,
T2: Get<'a>,
where
T1: Get<'a>,
T2: Get<'a>,
{
let (first, second) = response.get2();

Expand All @@ -195,35 +196,35 @@ impl DBusApi {
}

pub trait VariantTo<T> {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<T>;
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<T>;
}

impl VariantTo<String> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<String> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<String> {
value.0.as_str().and_then(|v| Some(v.to_string()))
}
}

impl VariantTo<i64> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<i64> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<i64> {
value.0.as_i64()
}
}

impl VariantTo<u32> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<u32> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<u32> {
value.0.as_i64().and_then(|v| Some(v as u32))
}
}

impl VariantTo<bool> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<bool> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<bool> {
value.0.as_i64().and_then(|v| Some(v == 0))
}
}

impl VariantTo<Vec<String>> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<Vec<String>> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<Vec<String>> {
let mut result = Vec::new();

if let Some(list) = value.0.as_iter() {
Expand All @@ -243,7 +244,7 @@ impl VariantTo<Vec<String>> for DBusApi {
}

impl VariantTo<Vec<u8>> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<Vec<u8>> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<Vec<u8>> {
let mut result = Vec::new();

if let Some(list) = value.0.as_iter() {
Expand All @@ -263,8 +264,8 @@ impl VariantTo<Vec<u8>> for DBusApi {
}

pub fn extract<'a, T>(var: &mut Variant<Iter<'a>>) -> Result<T>
where
T: Get<'a>,
where
T: Get<'a>,
{
var.0
.get::<T>()
Expand Down Expand Up @@ -293,4 +294,4 @@ pub fn path_to_string(path: &Path) -> Result<String> {
path
)))
}
}
}
Loading

0 comments on commit cfa667e

Please sign in to comment.