use std::collections::HashMap;
use tracing::instrument;
use zbus::{
Connection,
zvariant::{OwnedObjectPath, OwnedValue},
};
use super::types::AppliedConnection;
use crate::{
error::Error,
proxy::devices::DeviceProxy,
types::device::{NMDeviceManaged, NMDeviceManagedFlags},
};
pub(super) struct DeviceControls;
impl DeviceControls {
#[instrument(
skip(connection),
fields(device = %path, managed = managed),
err
)]
pub(super) async fn set_managed(
connection: &Connection,
path: &OwnedObjectPath,
managed: bool,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.set_managed(managed)
.await
.map_err(|e| Error::OperationFailed {
operation: "set managed",
source: e.into(),
})?;
Ok(())
}
#[instrument(
skip(connection),
fields(device = %path, managed = ?managed, flags = ?flags),
err
)]
pub(super) async fn set_managed_ext(
connection: &Connection,
path: &OwnedObjectPath,
managed: NMDeviceManaged,
flags: NMDeviceManagedFlags,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.set_managed_ext(managed as u32, flags.bits())
.await
.map_err(|err| Error::OperationFailed {
operation: "set managed (ext)",
source: err.into(),
})?;
Ok(())
}
#[instrument(
skip(connection),
fields(device = %path, autoconnect = autoconnect),
err
)]
pub(super) async fn set_autoconnect(
connection: &Connection,
path: &OwnedObjectPath,
autoconnect: bool,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.set_autoconnect(autoconnect)
.await
.map_err(|e| Error::OperationFailed {
operation: "set autoconnect",
source: e.into(),
})?;
Ok(())
}
#[instrument(
skip(connection, connection_settings),
fields(device = %path, version = version_id, flags = flags),
err
)]
pub(super) async fn reapply(
connection: &Connection,
path: &OwnedObjectPath,
connection_settings: HashMap<String, HashMap<String, OwnedValue>>,
version_id: u64,
flags: u32,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.reapply(connection_settings, version_id, flags)
.await
.map_err(|e| Error::OperationFailed {
operation: "reapply connection",
source: e.into(),
})?;
Ok(())
}
#[instrument(
skip(connection),
fields(device = %path, flags = flags),
err
)]
pub(super) async fn get_applied_connection(
connection: &Connection,
path: &OwnedObjectPath,
flags: u32,
) -> Result<AppliedConnection, Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.get_applied_connection(flags)
.await
.map(AppliedConnection::from)
.map_err(|e| Error::OperationFailed {
operation: "get applied connection",
source: e.into(),
})
}
#[instrument(skip(connection), fields(device = %path), err)]
pub(super) async fn disconnect(
connection: &Connection,
path: &OwnedObjectPath,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy
.disconnect()
.await
.map_err(|e| Error::OperationFailed {
operation: "disconnect device",
source: e.into(),
})?;
Ok(())
}
#[instrument(skip(connection), fields(device = %path), err)]
pub(super) async fn delete(
connection: &Connection,
path: &OwnedObjectPath,
) -> Result<(), Error> {
let proxy = DeviceProxy::new(connection, path)
.await
.map_err(Error::DbusError)?;
proxy.delete().await.map_err(|e| Error::OperationFailed {
operation: "delete device",
source: e.into(),
})?;
Ok(())
}
}