2022-01-11 15:23:09 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2022-01-12 11:23:09 -05:00
|
|
|
active_connection::ActiveConnection,
|
2022-01-11 15:23:09 -05:00
|
|
|
device::Device,
|
2022-01-12 11:23:09 -05:00
|
|
|
interface::{
|
|
|
|
|
active_connection::ActiveConnectionProxy,
|
|
|
|
|
device::DeviceProxy,
|
|
|
|
|
enums::{ConnectivityState, State},
|
|
|
|
|
NetworkManagerProxy,
|
|
|
|
|
},
|
2022-01-21 11:01:06 -05:00
|
|
|
settings::{connection::Connection, NetworkManagerSettings},
|
2022-01-11 15:23:09 -05:00
|
|
|
};
|
2022-01-12 11:23:09 -05:00
|
|
|
use zbus::{zvariant::ObjectPath, Result};
|
2022-01-11 15:23:09 -05:00
|
|
|
|
2022-01-21 11:12:35 -05:00
|
|
|
#[derive(Debug)]
|
2022-01-11 15:23:09 -05:00
|
|
|
pub struct NetworkManager<'a>(NetworkManagerProxy<'a>);
|
|
|
|
|
|
|
|
|
|
impl<'a> NetworkManager<'a> {
|
2022-01-12 11:23:09 -05:00
|
|
|
pub async fn new(connection: &'a zbus::Connection) -> Result<NetworkManager<'a>> {
|
2022-01-11 15:23:09 -05:00
|
|
|
NetworkManagerProxy::new(connection).await.map(Self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 11:23:09 -05:00
|
|
|
pub async fn activate_connection(
|
|
|
|
|
&self,
|
|
|
|
|
connection: &'a Connection<'a>,
|
|
|
|
|
device: &'a Device<'a>,
|
|
|
|
|
) -> Result<ActiveConnection<'a>> {
|
|
|
|
|
let connection = connection.path();
|
|
|
|
|
let device = device.path();
|
|
|
|
|
let specific_object = ObjectPath::from_static_str("/").unwrap();
|
|
|
|
|
let active_connection_path = self
|
|
|
|
|
.0
|
|
|
|
|
.activate_connection(connection, device, &specific_object)
|
|
|
|
|
.await?;
|
|
|
|
|
ActiveConnectionProxy::builder(self.0.connection())
|
|
|
|
|
.path(active_connection_path)?
|
|
|
|
|
.build()
|
|
|
|
|
.await
|
|
|
|
|
.map(ActiveConnection::from)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 11:08:54 -05:00
|
|
|
pub async fn active_connections(&self) -> Result<Vec<ActiveConnection<'a>>> {
|
|
|
|
|
let active_connections = self.0.active_connections().await?;
|
|
|
|
|
let mut out = Vec::with_capacity(active_connections.len());
|
|
|
|
|
for active_connection in active_connections {
|
|
|
|
|
let active_connection = ActiveConnectionProxy::builder(self.0.connection())
|
|
|
|
|
.path(active_connection)?
|
|
|
|
|
.build()
|
|
|
|
|
.await?;
|
|
|
|
|
out.push(active_connection.into());
|
|
|
|
|
}
|
|
|
|
|
Ok(out)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 11:23:09 -05:00
|
|
|
pub async fn connectivity(&self) -> Result<ConnectivityState> {
|
|
|
|
|
self.0.connectivity().await.map(ConnectivityState::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn check_connectivity(&self) -> Result<ConnectivityState> {
|
|
|
|
|
self.0
|
|
|
|
|
.check_connectivity()
|
|
|
|
|
.await
|
|
|
|
|
.map(ConnectivityState::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn deactivate_connection(&self, connection: &'a ActiveConnection<'a>) -> Result<()> {
|
|
|
|
|
self.0.deactivate_connection(connection.path()).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 15:23:09 -05:00
|
|
|
pub async fn devices(&self) -> Result<Vec<Device<'a>>> {
|
|
|
|
|
let devices = self.0.get_all_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)
|
|
|
|
|
}
|
2022-01-11 16:02:29 -05:00
|
|
|
|
|
|
|
|
pub async fn all_devices(&self) -> Result<Vec<Device<'a>>> {
|
|
|
|
|
let devices = self.0.get_all_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)
|
|
|
|
|
}
|
2022-01-12 11:23:09 -05:00
|
|
|
|
2022-01-21 11:01:06 -05:00
|
|
|
pub async fn state(&self) -> Result<State> {
|
|
|
|
|
self.0.state().await.map(State::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn settings(&'a self) -> Result<NetworkManagerSettings<'a>> {
|
|
|
|
|
NetworkManagerSettings::new(self.0.connection()).await
|
2022-01-12 11:23:09 -05:00
|
|
|
}
|
2022-01-11 15:23:09 -05:00
|
|
|
}
|