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

Serde optional feature #128

Open
wants to merge 3 commits 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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ ascii = "0.8"
log = "0.3"

[dependencies.error-chain]
version = "0.11"
version = "0.12"
default-features = false

[dependencies.serde]
version = "1.0"
features = ["derive"]
optional = true

[dev-dependencies]
clap = "2.32"
20 changes: 13 additions & 7 deletions src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use std::rc::Rc;
use std::fmt;
use std::net::Ipv4Addr;
use std::rc::Rc;

use errors::*;
use dbus_nm::DBusNetworkManager;
use errors::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use wifi::{AccessPoint, AccessPointCredentials};
use device::{get_active_connection_devices, Device};
use ssid::{AsSsidSlice, Ssid};
use wifi::{AccessPoint, AccessPointCredentials};

#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Connection {
#[cfg_attr(feature = "serde", serde(skip))]
dbus_manager: Rc<DBusNetworkManager>,
path: String,
settings: ConnectionSettings,
Expand All @@ -23,7 +27,7 @@ impl Connection {
Ok(Connection {
dbus_manager: Rc::clone(dbus_manager),
path: path.to_string(),
settings: settings,
settings,
})
}

Expand Down Expand Up @@ -78,7 +82,7 @@ impl Connection {
&ConnectionState::Activated,
self.dbus_manager.method_timeout(),
)
},
}
}
}

Expand Down Expand Up @@ -120,7 +124,7 @@ impl Connection {
} else {
Ok(ConnectionState::Deactivated)
}
},
}
}
}

Expand Down Expand Up @@ -178,6 +182,7 @@ impl<'a> From<&'a Connection> for i32 {
}

#[derive(Default, Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ConnectionSettings {
pub kind: String, // `type` is a reserved word, so we are using `kind` instead
pub id: String,
Expand All @@ -187,6 +192,7 @@ pub struct ConnectionSettings {
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ConnectionState {
Unknown = 0,
Activating = 1,
Expand All @@ -206,7 +212,7 @@ impl From<i64> for ConnectionState {
_ => {
warn!("Undefined connection state: {}", state);
ConnectionState::Unknown
},
}
}
}
}
Expand Down
21 changes: 11 additions & 10 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::Connection as DBusConnection;
use dbus::{BusType, ConnPath, Message, Path};

use errors::*;

Expand All @@ -26,10 +26,10 @@ impl DBusApi {
let method_timeout = method_timeout.unwrap_or(DEFAULT_TIMEOUT);

DBusApi {
connection: connection,
method_timeout: method_timeout,
base: base,
method_retry_error_names: method_retry_error_names,
connection,
method_timeout,
base,
method_retry_error_names,
}
}

Expand Down Expand Up @@ -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
59 changes: 32 additions & 27 deletions src/dbus_nm.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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>>>;
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,30 +128,30 @@ 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)?;
},
_ => {},
}
_ => {}
}
}
}

Ok(ConnectionSettings {
kind: kind,
id: id,
uuid: uuid,
ssid: ssid,
mode: mode,
kind,
id,
uuid,
ssid,
mode,
})
}

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,8 +252,8 @@ 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(
Expand Down Expand Up @@ -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 @@ -508,6 +513,6 @@ fn verify_ascii_password(password: &str) -> Result<&str> {
} else {
Ok(password)
}
},
}
}
}
22 changes: 14 additions & 8 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::rc::Rc;
use std::fmt;
use std::rc::Rc;

use errors::*;
use dbus_nm::DBusNetworkManager;
use errors::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use wifi::{new_wifi_device, WiFiDevice};

#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Device {
#[cfg_attr(feature = "serde", serde(skip))]
dbus_manager: Rc<DBusNetworkManager>,
path: String,
interface: String,
Expand All @@ -23,8 +27,8 @@ impl Device {
Ok(Device {
dbus_manager: Rc::clone(dbus_manager),
path: path.to_string(),
interface: interface,
device_type: device_type,
interface,
device_type,
})
}

Expand Down Expand Up @@ -72,7 +76,7 @@ impl Device {
&DeviceState::Activated,
self.dbus_manager.method_timeout(),
)
},
}
}
}

Expand Down Expand Up @@ -100,7 +104,7 @@ impl Device {
&DeviceState::Disconnected,
self.dbus_manager.method_timeout(),
)
},
}
}
}
}
Expand All @@ -126,6 +130,7 @@ impl PathGetter for Device {
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DeviceType {
Unknown,
Ethernet,
Expand Down Expand Up @@ -181,12 +186,13 @@ impl From<i64> for DeviceType {
_ => {
warn!("Undefined device type: {}", device_type);
DeviceType::Unknown
},
}
}
}
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DeviceState {
Unknown,
Unmanaged,
Expand Down Expand Up @@ -222,7 +228,7 @@ impl From<i64> for DeviceState {
_ => {
warn!("Undefined device state: {}", state);
DeviceState::Unknown
},
}
}
}
}
Expand Down
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