From 2acdb272fb05a98b2c0718997b580d770c9855a8 Mon Sep 17 00:00:00 2001 From: Lucy Date: Tue, 11 Jan 2022 16:41:10 -0500 Subject: [PATCH] Work on bindings some more --- Cargo.lock | 1 + networkmanager/Cargo.toml | 1 + networkmanager/src/connection.rs | 72 +++++++++++ networkmanager/src/device.rs | 40 ++++++ networkmanager/src/device/bluetooth.rs | 6 + networkmanager/src/device/wired.rs | 6 + networkmanager/src/device/wireless.rs | 18 ++- networkmanager/src/interface/connection.rs | 2 +- networkmanager/src/interface/enums.rs | 144 +++++++++++++++++++++ networkmanager/src/interface/mod.rs | 1 + networkmanager/src/lib.rs | 1 + 11 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 networkmanager/src/connection.rs create mode 100644 networkmanager/src/interface/enums.rs diff --git a/Cargo.lock b/Cargo.lock index 6c2b4e4..a3e6939 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,7 @@ dependencies = [ name = "cosmic-dbus-networkmanager" version = "0.1.0" dependencies = [ + "bitflags", "zbus", ] diff --git a/networkmanager/Cargo.toml b/networkmanager/Cargo.toml index 00927cc..88be212 100644 --- a/networkmanager/Cargo.toml +++ b/networkmanager/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" license = "MPL-2.0" [dependencies] +bitflags = "1.3.2" zbus = "2.0.0" [features] diff --git a/networkmanager/src/connection.rs b/networkmanager/src/connection.rs new file mode 100644 index 0000000..107b7b6 --- /dev/null +++ b/networkmanager/src/connection.rs @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: MPL-2.0 + +use crate::{ + config::{ip4::Ipv4Config, ip6::Ipv6Config}, + device::Device, + interface::{ + config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy}, + connection::ActiveConnectionProxy, + device::DeviceProxy, + enums::{ActivationStateFlags, State}, + }, +}; +use std::ops::Deref; +use zbus::Result; + +pub struct Connection<'a>(ActiveConnectionProxy<'a>); + +impl<'a> Connection<'a> { + pub async fn devices(&self) -> Result>> { + let devices = self.0.devices().await?; + let mut out = Vec::with_capacity(devices.len()); + for device in devices { + let device = DeviceProxy::builder(self.0.connection()) + .path(device)? + .build() + .await?; + out.push(device.into()); + } + Ok(out) + } + + pub async fn ip4_config(&self) -> Result> { + let config = Ipv4ConfigProxy::builder(self.0.connection()) + .path(self.0.ip4_config().await?)? + .build() + .await?; + Ok(Ipv4Config::from(config)) + } + + pub async fn ip6_config(&self) -> Result> { + let config = Ipv6ConfigProxy::builder(self.0.connection()) + .path(self.0.ip6_config().await?)? + .build() + .await?; + Ok(Ipv6Config::from(config)) + } + + pub async fn state(&self) -> Result { + self.0.state().await.map(State::from) + } + + pub async fn state_flags(&self) -> Result { + self.0 + .state_flags() + .await + .map(ActivationStateFlags::from_bits_truncate) + } +} + +impl<'a> Deref for Connection<'a> { + type Target = ActiveConnectionProxy<'a>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'a> From> for Connection<'a> { + fn from(connection: ActiveConnectionProxy<'a>) -> Self { + Connection(connection) + } +} diff --git a/networkmanager/src/device.rs b/networkmanager/src/device.rs index 049cf58..63ff06f 100644 --- a/networkmanager/src/device.rs +++ b/networkmanager/src/device.rs @@ -6,9 +6,12 @@ pub mod wireless; use crate::{ config::{ip4::Ipv4Config, ip6::Ipv6Config}, + connection::Connection, interface::{ config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy}, + connection::ActiveConnectionProxy, device::DeviceProxy, + enums::{DeviceCapabilities, DeviceState, DeviceType}, }, }; use std::{net::Ipv4Addr, ops::Deref}; @@ -17,6 +20,39 @@ use zbus::Result; pub struct Device<'a>(DeviceProxy<'a>); impl<'a> Device<'a> { + pub async fn active_connection(&self) -> Result> { + let active_connection = self.0.active_connection().await?; + Ok(ActiveConnectionProxy::builder(self.0.connection()) + .path(active_connection)? + .build() + .await? + .into()) + } + + pub async fn available_connections(&self) -> Result>> { + let available_connections = self.0.available_connections().await?; + let mut out = Vec::with_capacity(available_connections.len()); + for connection in available_connections { + let connection = ActiveConnectionProxy::builder(self.0.connection()) + .path(connection)? + .build() + .await?; + out.push(connection.into()); + } + Ok(out) + } + + pub async fn capabilities(&self) -> Result { + self.0 + .capabilities() + .await + .map(DeviceCapabilities::from_bits_truncate) + } + + pub async fn device_type(&self) -> Result { + self.0.device_type().await.map(DeviceType::from) + } + pub async fn ip4_address(&self) -> Result { self.0.ip4_address().await.map(Ipv4Addr::from) } @@ -36,6 +72,10 @@ impl<'a> Device<'a> { .await?; Ok(Ipv6Config::from(config)) } + + pub async fn state(&self) -> Result { + self.0.state().await.map(DeviceState::from) + } } impl<'a> Deref for Device<'a> { diff --git a/networkmanager/src/device/bluetooth.rs b/networkmanager/src/device/bluetooth.rs index a06073c..efee043 100644 --- a/networkmanager/src/device/bluetooth.rs +++ b/networkmanager/src/device/bluetooth.rs @@ -12,3 +12,9 @@ impl<'a> Deref for BluetoothDevice<'a> { &self.0 } } + +impl<'a> From> for BluetoothDevice<'a> { + fn from(device: BluetoothDeviceProxy<'a>) -> Self { + BluetoothDevice(device) + } +} diff --git a/networkmanager/src/device/wired.rs b/networkmanager/src/device/wired.rs index d0ff0d3..283ec28 100644 --- a/networkmanager/src/device/wired.rs +++ b/networkmanager/src/device/wired.rs @@ -12,3 +12,9 @@ impl<'a> Deref for WiredDevice<'a> { &self.0 } } + +impl<'a> From> for WiredDevice<'a> { + fn from(device: WiredDeviceProxy<'a>) -> Self { + WiredDevice(device) + } +} diff --git a/networkmanager/src/device/wireless.rs b/networkmanager/src/device/wireless.rs index c98e1dc..d33f20e 100644 --- a/networkmanager/src/device/wireless.rs +++ b/networkmanager/src/device/wireless.rs @@ -1,10 +1,20 @@ // SPDX-License-Identifier: MPL-2.0 -use crate::interface::device::wireless::WirelessDeviceProxy; +use crate::interface::{device::wireless::WirelessDeviceProxy, enums::WifiCapabilities}; use std::ops::Deref; +use zbus::Result; pub struct WirelessDevice<'a>(WirelessDeviceProxy<'a>); +impl<'a> WirelessDevice<'a> { + pub async fn wireless_capabilities(&self) -> Result { + self.0 + .wireless_capabilities() + .await + .map(WifiCapabilities::from_bits_truncate) + } +} + impl<'a> Deref for WirelessDevice<'a> { type Target = WirelessDeviceProxy<'a>; @@ -12,3 +22,9 @@ impl<'a> Deref for WirelessDevice<'a> { &self.0 } } + +impl<'a> From> for WirelessDevice<'a> { + fn from(device: WirelessDeviceProxy<'a>) -> Self { + WirelessDevice(device) + } +} diff --git a/networkmanager/src/interface/connection.rs b/networkmanager/src/interface/connection.rs index 7f3cd38..d16d6bf 100644 --- a/networkmanager/src/interface/connection.rs +++ b/networkmanager/src/interface/connection.rs @@ -29,7 +29,7 @@ use zbus::dbus_proxy; pub trait ActiveConnection { /// Connection property #[dbus_proxy(property)] - fn connection(&self) -> zbus::Result; + fn connection_(&self) -> zbus::Result; /// Default property #[dbus_proxy(property)] diff --git a/networkmanager/src/interface/enums.rs b/networkmanager/src/interface/enums.rs new file mode 100644 index 0000000..d57243b --- /dev/null +++ b/networkmanager/src/interface/enums.rs @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: MPL-2.0 +use bitflags::bitflags; + +pub enum State { + Asleep, + Disconnected, + Disconnecting, + Connecting, + ConnectedLocal, + ConnectedSite, + ConnectedGlobal, + Unknown, +} + +impl From for State { + fn from(state: u32) -> State { + match state { + 10 => State::Asleep, + 20 => State::Disconnected, + 30 => State::Disconnecting, + 40 => State::Connecting, + 50 => State::ConnectedLocal, + 60 => State::ConnectedSite, + 70 => State::ConnectedGlobal, + _ => State::Unknown, + } + } +} + +pub enum ConnectivityState { + None, + Portal, + Loss, + Full, + Unknown, +} + +impl From for ConnectivityState { + fn from(state: u32) -> ConnectivityState { + match state { + 1 => ConnectivityState::None, + 2 => ConnectivityState::Portal, + 3 => ConnectivityState::Loss, + 4 => ConnectivityState::Full, + _ => ConnectivityState::Unknown, + } + } +} + +pub enum DeviceType { + Ethernet, + Wifi, + Bluetooth, + Other, + Unknown, +} + +impl From for DeviceType { + fn from(device_type: u32) -> DeviceType { + match device_type { + 1 => DeviceType::Ethernet, + 2 => DeviceType::Wifi, + 5 => DeviceType::Bluetooth, + 3..=4 | 6..=32 => DeviceType::Other, + _ => DeviceType::Unknown, + } + } +} + +pub enum DeviceState { + Unmanaged, + Unavailable, + Disconnected, + Prepare, + Config, + NeedAuth, + IpConfig, + IpCheck, + Secondaries, + Activated, + Deactivating, + Failed, + Unknown, +} + +impl From for DeviceState { + fn from(device_state: u32) -> Self { + match device_state { + 10 => DeviceState::Unmanaged, + 20 => DeviceState::Unavailable, + 30 => DeviceState::Disconnected, + 40 => DeviceState::Prepare, + 50 => DeviceState::Config, + 60 => DeviceState::NeedAuth, + 70 => DeviceState::IpConfig, + 80 => DeviceState::IpCheck, + 90 => DeviceState::Secondaries, + 100 => DeviceState::Activated, + 110 => DeviceState::Deactivating, + 120 => DeviceState::Failed, + _ => DeviceState::Unknown, + } + } +} + +bitflags! { + pub struct DeviceCapabilities: u32 { + const SUPPORTED = 0x00000001; + const CARRIER_DETECT = 0x00000002; + const SOFTWARE = 0x00000004; + const SINGLE_ROOT_IO_VIRT = 0x00000008; + } +} + +bitflags! { + pub struct WifiCapabilities: u32 { + const CIPHER_WEP40 = 0x00000001; + const CIPHER_WEP104 = 0x00000002; + const CIPHER_TKIP = 0x00000004; + const CIPHER_CCMP = 0x00000008; + const WPA = 0x00000010; + const RSN = 0x00000020; + const AP = 0x00000040; + const AD_HOC = 0x00000080; + const FREQ_VALID = 0x00000100; + const FREQ_2GHZ = 0x00000200; + const FREQ_5GHZ = 0x00000400; + const MESH = 0x00001000; + const IBSS_RSN = 0x00002000; + } +} + +bitflags! { + pub struct ActivationStateFlags: u32 { + const IS_MASTER = 0x1; + const IS_SLAVE = 0x2; + const LAYER2_READY = 0x4; + const IP4_READY = 0x8; + const IP6_READY = 0x10; + const MASTER_HAS_SLAVES = 0x20; + const LIFETIME_BOUND_TO_PROFILE_VISIBILITY = 0x40; + const EXTERNAL = 0x80; + } +} diff --git a/networkmanager/src/interface/mod.rs b/networkmanager/src/interface/mod.rs index 89f5e32..b45485f 100644 --- a/networkmanager/src/interface/mod.rs +++ b/networkmanager/src/interface/mod.rs @@ -24,6 +24,7 @@ pub mod access_point; pub mod config; pub mod connection; pub mod device; +pub mod enums; pub mod settings; pub mod statistics; diff --git a/networkmanager/src/lib.rs b/networkmanager/src/lib.rs index c1b3a5d..764371f 100644 --- a/networkmanager/src/lib.rs +++ b/networkmanager/src/lib.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MPL-2.0 pub mod config; +pub mod connection; pub mod device; pub mod interface; pub mod nm;