use crate::dbus_api::ActiveConnectionState;
use crate::dbus_api::configs::ipv4::Ipv4Client;
use crate::dbus_api::settings::connection::ConnectionSettingsClient;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::zvariant::OwnedObjectPath;
use zbus::{Connection, Result as ZResult, proxy};
#[proxy(
interface = "org.freedesktop.NetworkManager.Connection.Active",
default_service = "org.freedesktop.NetworkManager"
)]
pub trait ActiveConnection {
#[zbus(property)]
fn connection(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn specific_object(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn id(&self) -> ZResult<String>;
#[zbus(property)]
fn uuid(&self) -> ZResult<String>;
#[zbus(property)]
fn type_(&self) -> ZResult<String>;
#[zbus(property)]
fn devices(&self) -> ZResult<Vec<OwnedObjectPath>>;
#[zbus(property)]
fn state(&self) -> ZResult<u32>;
#[zbus(property)]
fn state_flags(&self) -> ZResult<u32>;
#[zbus(property)]
fn default(&self) -> ZResult<bool>;
#[zbus(property)]
fn ip4_config(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn dhcp4_config(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn default6(&self) -> ZResult<bool>;
#[zbus(property)]
fn ip6_config(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn dhcp6_config(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn vpn(&self) -> ZResult<bool>;
#[zbus(property)]
fn master(&self) -> ZResult<OwnedObjectPath>;
}
pub struct ActiveConnectionClient {
service_path: String,
proxy: ActiveConnectionProxy<'static>,
bus_connection: Arc<Connection>,
}
impl ActiveConnectionClient {
pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
let proxy = ActiveConnectionProxy::builder(&connection)
.path(service_path.clone())
.map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?
.cache_properties(CacheProperties::Yes)
.build()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
proxy
.id()
.await
.map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;
Ok(Self {
service_path,
proxy,
bus_connection: connection,
})
}
pub fn service_path(&self) -> &str {
&self.service_path
}
pub async fn connection(&self) -> Result<ConnectionSettingsClient, Error> {
let con_path = self
.proxy
.connection()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
ConnectionSettingsClient::new(self.bus_connection.clone(), con_path.to_string()).await
}
pub async fn specific_object(&self) -> Result<String, Error> {
self.proxy
.specific_object()
.await
.map(|path| path.to_string())
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn id(&self) -> Result<String, Error> {
self.proxy
.id()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn uuid(&self) -> Result<String, Error> {
self.proxy
.uuid()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn type_(&self) -> Result<String, Error> {
self.proxy
.type_()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn devices(&self) -> Result<Vec<OwnedObjectPath>, Error> {
self.proxy
.devices()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn state(&self) -> Result<ActiveConnectionState, Error> {
let state = self
.proxy
.state()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
Ok(ActiveConnectionState::from(state))
}
pub async fn state_flags(&self) -> Result<u32, Error> {
self.proxy
.state_flags()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn default(&self) -> Result<bool, Error> {
self.proxy
.default()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn ip4_config(&self) -> Result<Ipv4Client, Error> {
let object_path = self
.proxy
.ip4_config()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
Ipv4Client::new(&self.bus_connection, object_path.to_string()).await
}
pub async fn dhcp4_config(&self) -> Result<OwnedObjectPath, Error> {
self.proxy
.dhcp4_config()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn default6(&self) -> Result<bool, Error> {
self.proxy
.default6()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn ip6_config(&self) -> Result<OwnedObjectPath, Error> {
self.proxy
.ip6_config()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn dhcp6_config(&self) -> Result<OwnedObjectPath, Error> {
self.proxy
.dhcp6_config()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn vpn(&self) -> Result<bool, Error> {
self.proxy
.vpn()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn master(&self) -> Result<OwnedObjectPath, Error> {
self.proxy
.master()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn wait_for_state(
&self,
target_state: ActiveConnectionState,
timeout_secs: u64,
) -> Result<(), Error> {
let start = std::time::Instant::now();
loop {
let current_state = self.state().await?;
if current_state == target_state {
return Ok(());
}
if start.elapsed().as_secs() > timeout_secs {
return Err(Error::new(
ErrorKind::TimedOut,
format!("Timed out waiting for connection state {:?}", target_state),
));
}
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
}
}
}