dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
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.Macsec",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Macsec {
    // Properties
    #[zbus(property)]
    fn parent(&self) -> ZResult<OwnedObjectPath>;

    #[zbus(property)]
    fn sci(&self) -> ZResult<u64>;

    #[zbus(property)]
    fn icv_length(&self) -> ZResult<u8>;

    #[zbus(property)]
    fn cipher_suite(&self) -> ZResult<u64>;

    #[zbus(property)]
    fn window(&self) -> ZResult<u32>;

    #[zbus(property)]
    fn encoding_sa(&self) -> ZResult<u8>;

    #[zbus(property)]
    fn validation(&self) -> ZResult<String>;

    #[zbus(property)]
    fn encrypt(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn protect(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn include_sci(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn es(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn scb(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn replay_protect(&self) -> ZResult<bool>;
}

#[derive(Debug, Clone)]
pub struct MacsecClient {
    device: DeviceClient,
    proxy: MacsecProxy<'static>,
}

impl MacsecClient {
    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 = MacsecProxy::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()))?;

        // Verify we can access macsec-specific properties
        proxy
            .sci()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self { device, proxy })
    }

    // Forward common device methods to the base device client
    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
    }

    // Macsec-specific methods
    pub async fn parent(&self) -> Result<OwnedObjectPath, Error> {
        self.proxy
            .parent()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn sci(&self) -> Result<u64, Error> {
        self.proxy
            .sci()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn icv_length(&self) -> Result<u8, Error> {
        self.proxy
            .icv_length()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn cipher_suite(&self) -> Result<u64, Error> {
        self.proxy
            .cipher_suite()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn window(&self) -> Result<u32, Error> {
        self.proxy
            .window()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn encoding_sa(&self) -> Result<u8, Error> {
        self.proxy
            .encoding_sa()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn validation(&self) -> Result<String, Error> {
        self.proxy
            .validation()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn encrypt(&self) -> Result<bool, Error> {
        self.proxy
            .encrypt()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn protect(&self) -> Result<bool, Error> {
        self.proxy
            .protect()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn include_sci(&self) -> Result<bool, Error> {
        self.proxy
            .include_sci()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn es(&self) -> Result<bool, Error> {
        self.proxy
            .es()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn scb(&self) -> Result<bool, Error> {
        self.proxy
            .scb()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn replay_protect(&self) -> Result<bool, Error> {
        self.proxy
            .replay_protect()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    // Helper method to get the parent device
    pub async fn get_parent_device(
        &self,
        connection: Arc<Connection>,
    ) -> Result<super::Device, Error> {
        let parent_path = self.parent().await?;
        super::Device::new(connection, parent_path.to_string()).await
    }
}