dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use super::DeviceClient;
use crate::dbus_api::active_connection::ActiveConnectionClient;
use crate::dbus_api::devices::statistics::DeviceStatistics;
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.Wired",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait Wired {
    #[zbus(property)]
    fn hw_address(&self) -> ZResult<String>;

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

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

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

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

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

impl WiredClient {
    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 = WiredProxy::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 the wired-specific properties
        proxy
            .hw_address()
            .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
    }

    pub async fn mtu(&self) -> Result<u32, Error> {
        self.device.mtu().await
    }

    pub async fn active_connection(&self) -> Result<Option<ActiveConnectionClient>, Error> {
        self.device.active_connection().await
    }

    pub async fn ip4_config(&self) -> Result<OwnedObjectPath, Error> {
        self.device.ip4_config().await
    }

    pub async fn get_statistics(&self) -> Result<DeviceStatistics, Error> {
        self.device.get_statistics().await
    }

    pub async fn set_statistics_refresh_rate(&self, rate_ms: u32) -> Result<(), Error> {
        self.device.set_statistics_refresh_rate(rate_ms).await
    }

    pub async fn get_tx_bytes(&self) -> Result<u64, Error> {
        self.device.get_tx_bytes().await
    }

    pub async fn get_rx_bytes(&self) -> Result<u64, Error> {
        self.device.get_rx_bytes().await
    }

    // Wired-specific methods
    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 perm_hw_address(&self) -> Result<String, Error> {
        self.proxy
            .perm_hw_address()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

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

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

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

    // Convenience methods
    pub async fn is_connected(&self) -> Result<bool, Error> {
        // First check if the carrier is present (physical connection)
        if !self.carrier().await? {
            return Ok(false);
        }

        // Then check the device state
        let state = self.device.state().await?;

        // Device state 100 = NM_DEVICE_STATE_ACTIVATED
        // See https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html
        Ok(state == 100)
    }

    pub async fn get_connection_info(&self) -> Result<String, Error> {
        if !self.is_connected().await? {
            return Ok("Not connected".to_string());
        }

        let speed = self.speed().await?;
        let interface = self.interface().await?;

        Ok(format!("Connected on {} at {} Mbps", interface, speed))
    }
}