use super::DeviceClient;
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.Device.OlpcMesh",
default_service = "org.freedesktop.NetworkManager"
)]
pub trait OlpcMesh {
#[zbus(property)]
fn hw_address(&self) -> ZResult<String>;
#[zbus(property)]
fn companion(&self) -> ZResult<OwnedObjectPath>;
#[zbus(property)]
fn active_channel(&self) -> ZResult<u32>;
}
#[derive(Debug, Clone)]
pub struct OlpcMeshClient {
device: DeviceClient,
proxy: OlpcMeshProxy<'static>,
}
impl OlpcMeshClient {
pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
let device = DeviceClient::new(connection.clone(), service_path.clone()).await?;
let proxy = OlpcMeshProxy::builder(&connection)
.path(service_path)
.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
.hw_address()
.await
.map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;
Ok(Self { device, proxy })
}
pub fn service_path(&self) -> &str {
self.device.service_path()
}
pub async fn interface(&self) -> Result<String, Error> {
self.device.interface().await
}
pub async fn disconnect(&self) -> Result<(), Error> {
self.device.disconnect().await
}
pub async fn hw_address(&self) -> Result<String, Error> {
self.proxy
.hw_address()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn companion(&self) -> Result<OwnedObjectPath, Error> {
self.proxy
.companion()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn active_channel(&self) -> Result<u32, Error> {
self.proxy
.active_channel()
.await
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
pub async fn get_companion_device(
&self,
connection: Arc<Connection>,
) -> Result<super::Device, Error> {
let companion_path = self.companion().await?;
super::Device::new(connection, companion_path.to_string()).await
}
}