dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.AccessPoint",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait AccessPoint {
    #[zbus(property)]
    fn flags(&self) -> ZResult<u32>;

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

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

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

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

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

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

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

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

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

pub struct AccessPoint {
    service_path: String,
    proxy: AccessPointProxy<'static>,
}

impl AccessPoint {
    pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
        let proxy = AccessPointProxy::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()))?;

        // Verify we can access the access point
        proxy
            .hw_address()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self {
            service_path,
            proxy,
        })
    }

    // Service path getter
    pub fn service_path(&self) -> &str {
        &self.service_path
    }

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

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

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

    pub async fn ssid(&self) -> Result<String, Error> {
        let bytes = self
            .proxy
            .ssid()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        String::from_utf8(bytes).map_err(|e| Error::new(ErrorKind::InvalidData, e.to_string()))
    }

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

    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 mode(&self) -> Result<u32, Error> {
        self.proxy
            .mode()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

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

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

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