Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dbus crate. Fixed some warnings #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 18 additions & 17 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,7 +126,7 @@ impl DBusApi {
}

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

Expand Down Expand Up @@ -159,7 +160,7 @@ impl DBusApi {
None => property_error("no details", false),
};
Err(e).chain_err(|| dbus_err)
},
}
}
}

Expand Down Expand Up @@ -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 Down
89 changes: 47 additions & 42 deletions src/dbus_nm.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::collections::HashMap;
use std::net::Ipv4Addr;

use dbus::Path;
use dbus::arg::{Array, Dict, Iter, RefArg, Variant};
use dbus::Path;

use ascii::AsciiStr;

use connection::{ConnectionSettings, ConnectionState};
use dbus_api::{extract, path_to_string, variant_iter_to_vec_u8, DBusApi, VariantTo};
use device::{DeviceState, DeviceType};
use errors::*;
use dbus_api::{extract, path_to_string, DBusApi, VariantTo, variant_iter_to_vec_u8};
use manager::{Connectivity, NetworkManagerState};
use connection::{ConnectionSettings, ConnectionState};
use ssid::{AsSsidSlice, Ssid};
use device::{DeviceState, DeviceType};
use wifi::{AccessPoint, AccessPointCredentials, NM80211ApFlags, NM80211ApSecurityFlags};

type VariantMap = HashMap<String, Variant<Box<RefArg>>>;
type VariantMap = HashMap<String, Variant<Box<dyn RefArg>>>;

const NM_SERVICE_MANAGER: &str = "org.freedesktop.NetworkManager";

Expand Down Expand Up @@ -51,7 +51,8 @@ impl DBusNetworkManager {
}

pub fn get_state(&self) -> Result<NetworkManagerState> {
let response = self.dbus
let response = self
.dbus
.call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "state")?;

let state: u32 = self.dbus.extract(&response)?;
Expand All @@ -60,8 +61,9 @@ impl DBusNetworkManager {
}

pub fn check_connectivity(&self) -> Result<Connectivity> {
let response = self.dbus
.call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "CheckConnectivity")?;
let response =
self.dbus
.call(NM_SERVICE_PATH, NM_SERVICE_INTERFACE, "CheckConnectivity")?;

let connectivity: u32 = self.dbus.extract(&response)?;

Expand All @@ -79,8 +81,9 @@ impl DBusNetworkManager {
}

pub fn list_connections(&self) -> Result<Vec<String>> {
let response = self.dbus
.call(NM_SETTINGS_PATH, NM_SETTINGS_INTERFACE, "ListConnections")?;
let response =
self.dbus
.call(NM_SETTINGS_PATH, NM_SETTINGS_INTERFACE, "ListConnections")?;

let array: Array<Path, _> = self.dbus.extract(&response)?;

Expand Down Expand Up @@ -108,7 +111,8 @@ impl DBusNetworkManager {
}

pub fn get_connection_settings(&self, path: &str) -> Result<ConnectionSettings> {
let response = self.dbus
let response = self
.dbus
.call(path, NM_CONNECTION_INTERFACE, "GetSettings")?;

let dict: Dict<&str, Dict<&str, Variant<Iter>, _>, _> = self.dbus.extract(&response)?;
Expand All @@ -124,20 +128,20 @@ impl DBusNetworkManager {
match k2 {
"id" => {
id = extract::<String>(&mut v2)?;
},
}
"uuid" => {
uuid = extract::<String>(&mut v2)?;
},
}
"type" => {
kind = extract::<String>(&mut v2)?;
},
}
"ssid" => {
ssid = Ssid::from_bytes(variant_iter_to_vec_u8(&mut v2)?)?;
},
}
"mode" => {
mode = extract::<String>(&mut v2)?;
},
_ => {},
}
_ => {}
}
}
}
Expand Down Expand Up @@ -167,9 +171,9 @@ impl DBusNetworkManager {
NM_SERVICE_INTERFACE,
"ActivateConnection",
&[
&Path::new(path)? as &RefArg,
&Path::new("/")? as &RefArg,
&Path::new("/")? as &RefArg,
&Path::new(path)? as &dyn RefArg,
&Path::new("/")? as &dyn RefArg,
&Path::new("/")? as &dyn RefArg,
],
)?;

Expand All @@ -181,7 +185,7 @@ impl DBusNetworkManager {
NM_SERVICE_PATH,
NM_SERVICE_INTERFACE,
"DeactivateConnection",
&[&Path::new(path)? as &RefArg],
&[&Path::new(path)? as &dyn RefArg],
)?;

Ok(())
Expand Down Expand Up @@ -219,7 +223,7 @@ impl DBusNetworkManager {
);

settings.insert("802-11-wireless-security".to_string(), security_settings);
},
}
AccessPointCredentials::Wpa { ref passphrase } => {
let mut security_settings: VariantMap = HashMap::new();

Expand All @@ -231,7 +235,7 @@ impl DBusNetworkManager {
);

settings.insert("802-11-wireless-security".to_string(), security_settings);
},
}
AccessPointCredentials::Enterprise {
ref identity,
ref passphrase,
Expand All @@ -248,18 +252,18 @@ impl DBusNetworkManager {

settings.insert("802-11-wireless-security".to_string(), security_settings);
settings.insert("802-1x".to_string(), eap);
},
AccessPointCredentials::None => {},
}
AccessPointCredentials::None => {}
};

let response = self.dbus.call_with_args(
NM_SERVICE_PATH,
NM_SERVICE_INTERFACE,
"AddAndActivateConnection",
&[
&settings as &RefArg,
&Path::new(device_path.to_string())? as &RefArg,
&Path::new(access_point.path.to_string())? as &RefArg,
&settings as &dyn RefArg,
&Path::new(device_path.to_string())? as &dyn RefArg,
&Path::new(access_point.path.to_string())? as &dyn RefArg,
],
)?;

Expand Down Expand Up @@ -333,9 +337,9 @@ impl DBusNetworkManager {
NM_SERVICE_INTERFACE,
"AddAndActivateConnection",
&[
&settings as &RefArg,
&Path::new(device_path)? as &RefArg,
&Path::new("/")? as &RefArg,
&settings as &dyn RefArg,
&Path::new(device_path)? as &dyn RefArg,
&Path::new("/")? as &dyn RefArg,
],
)?;

Expand All @@ -357,7 +361,7 @@ impl DBusNetworkManager {
NM_SERVICE_PATH,
NM_SERVICE_INTERFACE,
"GetDeviceByIpIface",
&[&interface.to_string() as &RefArg],
&[&interface.to_string() as &dyn RefArg],
)?;

let path: Path = self.dbus.extract(&response)?;
Expand All @@ -383,9 +387,9 @@ impl DBusNetworkManager {
NM_SERVICE_INTERFACE,
"ActivateConnection",
&[
&Path::new("/")? as &RefArg,
&Path::new(path)? as &RefArg,
&Path::new("/")? as &RefArg,
&Path::new("/")? as &dyn RefArg,
&Path::new(path)? as &dyn RefArg,
&Path::new("/")? as &dyn RefArg,
],
)?;

Expand All @@ -404,7 +408,7 @@ impl DBusNetworkManager {
path,
NM_WIRELESS_INTERFACE,
"RequestScan",
&[&options as &RefArg],
&[&options as &dyn RefArg],
)?;

Ok(())
Expand All @@ -416,7 +420,8 @@ impl DBusNetworkManager {
}

pub fn get_access_point_ssid(&self, path: &str) -> Option<Ssid> {
if let Ok(ssid_vec) = self.dbus
if let Ok(ssid_vec) = self
.dbus
.property::<Vec<u8>>(path, NM_ACCESS_POINT_INTERFACE, "Ssid")
{
Ssid::from_bytes(ssid_vec).ok()
Expand Down Expand Up @@ -446,19 +451,19 @@ impl DBusNetworkManager {
}

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

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

impl VariantTo<NM80211ApFlags> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<NM80211ApFlags> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<NM80211ApFlags> {
value
.0
.as_i64()
Expand All @@ -467,7 +472,7 @@ impl VariantTo<NM80211ApFlags> for DBusApi {
}

impl VariantTo<NM80211ApSecurityFlags> for DBusApi {
fn variant_to(value: &Variant<Box<RefArg>>) -> Option<NM80211ApSecurityFlags> {
fn variant_to(value: &Variant<Box<dyn RefArg>>) -> Option<NM80211ApSecurityFlags> {
value
.0
.as_i64()
Expand Down Expand Up @@ -508,6 +513,6 @@ fn verify_ascii_password(password: &str) -> Result<&str> {
} else {
Ok(password)
}
},
}
}
}
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error_chain!{
error_chain! {
foreign_links {
Ascii(::ascii::AsAsciiStrError);
Utf8(::std::str::Utf8Error);
Expand Down
Loading