dbus-settings-bindings/networkmanager/src/device.rs

94 lines
2.3 KiB
Rust
Raw Normal View History

2022-01-11 15:23:09 -05:00
// SPDX-License-Identifier: MPL-2.0
pub mod bluetooth;
pub mod wired;
pub mod wireless;
2022-01-11 16:02:29 -05:00
use crate::{
config::{ip4::Ipv4Config, ip6::Ipv6Config},
2022-01-11 16:41:10 -05:00
connection::Connection,
2022-01-11 16:02:29 -05:00
interface::{
config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy},
2022-01-11 16:41:10 -05:00
connection::ActiveConnectionProxy,
2022-01-11 16:02:29 -05:00
device::DeviceProxy,
2022-01-11 16:41:10 -05:00
enums::{DeviceCapabilities, DeviceState, DeviceType},
2022-01-11 16:02:29 -05:00
},
};
use std::{net::Ipv4Addr, ops::Deref};
use zbus::Result;
2022-01-11 15:23:09 -05:00
pub struct Device<'a>(DeviceProxy<'a>);
2022-01-11 16:02:29 -05:00
impl<'a> Device<'a> {
2022-01-11 16:41:10 -05:00
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)
}
2022-01-11 16:02:29 -05:00
pub async fn ip4_address(&self) -> Result<Ipv4Addr> {
self.0.ip4_address().await.map(Ipv4Addr::from)
}
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))
}
2022-01-11 16:41:10 -05:00
pub async fn state(&self) -> Result<DeviceState> {
self.0.state().await.map(DeviceState::from)
}
2022-01-11 16:02:29 -05:00
}
2022-01-11 15:23:09 -05:00
impl<'a> Deref for Device<'a> {
type Target = DeviceProxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> From<DeviceProxy<'a>> for Device<'a> {
fn from(device: DeviceProxy<'a>) -> Self {
Device(device)
}
}