Work on bindings some more

This commit is contained in:
Lucy 2022-01-11 16:41:10 -05:00
parent da5c6ed172
commit 2acdb272fb
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
11 changed files with 290 additions and 2 deletions

1
Cargo.lock generated
View file

@ -152,6 +152,7 @@ dependencies = [
name = "cosmic-dbus-networkmanager"
version = "0.1.0"
dependencies = [
"bitflags",
"zbus",
]

View file

@ -5,6 +5,7 @@ edition = "2021"
license = "MPL-2.0"
[dependencies]
bitflags = "1.3.2"
zbus = "2.0.0"
[features]

View file

@ -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<Vec<Device<'a>>> {
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<Ipv4Config<'a>> {
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<Ipv6Config<'a>> {
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<State> {
self.0.state().await.map(State::from)
}
pub async fn state_flags(&self) -> Result<ActivationStateFlags> {
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<ActiveConnectionProxy<'a>> for Connection<'a> {
fn from(connection: ActiveConnectionProxy<'a>) -> Self {
Connection(connection)
}
}

View file

@ -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<Connection<'a>> {
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<Vec<Connection<'a>>> {
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<DeviceCapabilities> {
self.0
.capabilities()
.await
.map(DeviceCapabilities::from_bits_truncate)
}
pub async fn device_type(&self) -> Result<DeviceType> {
self.0.device_type().await.map(DeviceType::from)
}
pub async fn ip4_address(&self) -> Result<Ipv4Addr> {
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<DeviceState> {
self.0.state().await.map(DeviceState::from)
}
}
impl<'a> Deref for Device<'a> {

View file

@ -12,3 +12,9 @@ impl<'a> Deref for BluetoothDevice<'a> {
&self.0
}
}
impl<'a> From<BluetoothDeviceProxy<'a>> for BluetoothDevice<'a> {
fn from(device: BluetoothDeviceProxy<'a>) -> Self {
BluetoothDevice(device)
}
}

View file

@ -12,3 +12,9 @@ impl<'a> Deref for WiredDevice<'a> {
&self.0
}
}
impl<'a> From<WiredDeviceProxy<'a>> for WiredDevice<'a> {
fn from(device: WiredDeviceProxy<'a>) -> Self {
WiredDevice(device)
}
}

View file

@ -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<WifiCapabilities> {
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<WirelessDeviceProxy<'a>> for WirelessDevice<'a> {
fn from(device: WirelessDeviceProxy<'a>) -> Self {
WirelessDevice(device)
}
}

View file

@ -29,7 +29,7 @@ use zbus::dbus_proxy;
pub trait ActiveConnection {
/// Connection property
#[dbus_proxy(property)]
fn connection(&self) -> zbus::Result<zbus::zvariant::OwnedObjectPath>;
fn connection_(&self) -> zbus::Result<zbus::zvariant::OwnedObjectPath>;
/// Default property
#[dbus_proxy(property)]

View file

@ -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<u32> 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<u32> 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<u32> 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<u32> 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;
}
}

View file

@ -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;

View file

@ -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;