diff --git a/src/access_point.rs b/src/access_point.rs new file mode 100644 index 0000000..7444275 --- /dev/null +++ b/src/access_point.rs @@ -0,0 +1,106 @@ +use anyhow::Result; +use std::{collections::HashMap, sync::Arc}; + +use zvariant::{OwnedObjectPath, Value}; + +use zbus::{Connection, Proxy}; + +#[derive(Debug, Clone)] +pub struct AccessPoint { + pub(crate) connection: Arc, + pub(crate) dbus_path: OwnedObjectPath, +} + +impl AccessPoint { + pub(crate) fn new(connection: Arc, dbus_path: OwnedObjectPath) -> Self { + Self { + connection, + dbus_path, + } + } + + pub(crate) async fn proxy<'a>(&self) -> Result, zbus::Error> { + Proxy::new( + &self.connection, + "net.connman.iwd", + self.dbus_path.clone(), + "net.connman.iwd.AccessPoint", + ) + .await + } + + // Methods + pub async fn start(&self, ssid: &str, psk: &str) -> Result<()> { + let proxy = self.proxy().await?; + proxy.call_method("Start", &(ssid, psk)).await?; + Ok(()) + } + + pub async fn stop(&self) -> Result<()> { + let proxy = self.proxy().await?; + proxy.call_method("Stop", &()).await?; + Ok(()) + } + + pub async fn start_profile(&self, ssid: &str) -> Result<()> { + let proxy = self.proxy().await?; + proxy.call_method("StartProfile", &(ssid)).await?; + Ok(()) + } + + pub async fn scan(&self) -> Result<()> { + let proxy = self.proxy().await?; + proxy.call("Scan", &()).await?; + Ok(()) + } + + pub async fn networks(&self) -> Result>> { + let proxy = self.proxy().await?; + let networks = proxy.call_method("GetOrderedNetworks", &()).await?; + let body = networks.body(); + let body: Vec> = body.deserialize()?; + let body = body + .into_iter() + .map(|map| { + map.into_iter() + .map(|(k, v)| (k, v.to_string())) + .collect::>() + }) + .collect::>>(); + + Ok(body) + } + + // Propreties + pub async fn has_started(&self) -> Result { + let proxy = self.proxy().await?; + let has_started: bool = proxy.get_property("Started").await?; + Ok(has_started) + } + + pub async fn frequency(&self) -> Result> { + let proxy = self.proxy().await?; + Ok(proxy.get_property("Frequency").await.ok()) + } + + pub async fn is_scanning(&self) -> Result { + let proxy = self.proxy().await?; + let is_scanning: bool = proxy.get_property("Scanning").await?; + Ok(is_scanning) + } + + pub async fn name(&self) -> Result> { + let proxy = self.proxy().await?; + Ok(proxy.get_property("Name").await.ok()) + } + + pub async fn pairwise_ciphers(&self) -> Result>> { + let proxy = self.proxy().await?; + Ok(proxy.get_property("PairwiseCiphers").await.ok()) + } + + pub async fn group_cipher(&self) -> Result> { + let proxy = self.proxy().await?; + Ok(proxy.get_property("GroupCipher").await.ok()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 06721b6..8c74982 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,3 +11,5 @@ pub mod agent; pub mod netowrk; pub mod known_netowk; + +pub mod access_point; diff --git a/src/session.rs b/src/session.rs index e842bdd..382413f 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,17 +1,16 @@ -use anyhow::Result; -use std::{collections::HashMap, sync::Arc}; -use uuid::Uuid; - -use zbus::{Connection, Proxy}; -use zvariant::{OwnedObjectPath, OwnedValue}; - use crate::{ + access_point::AccessPoint, + adapter::Adapter, agent::{Agent, AgentManager}, device::Device, known_netowk::KnownNetwork, station::Station, }; - +use anyhow::Result; +use std::{collections::HashMap, sync::Arc}; +use uuid::Uuid; +use zbus::{Connection, Proxy}; +use zvariant::{OwnedObjectPath, OwnedValue}; #[derive(Debug)] pub struct Session { connection: Arc, @@ -39,6 +38,21 @@ impl Session { }) } + pub fn adapter(&self) -> Option { + let adapter: Option = self + .objects + .iter() + .flat_map(|(path, interfaces)| { + interfaces + .iter() + .filter(|(interface, _)| interface.as_str() == "net.connman.iwd.Adapter") + .map(|_| Adapter::new(self.connection.clone(), path.clone())) + }) + .next(); + + adapter + } + pub fn device(&self) -> Option { let device: Option = self .objects @@ -68,6 +82,20 @@ impl Session { station } + pub fn access_point(&self) -> Option { + let access_point: Option = self + .objects + .iter() + .flat_map(|(path, interfaces)| { + interfaces + .iter() + .filter(|(interface, _)| interface.as_str() == "net.connman.iwd.AccessPoint") + .map(|_| AccessPoint::new(self.connection.clone(), path.clone())) + }) + .next(); + access_point + } + pub async fn register_agent(&self, agent: Agent) -> Result { let path = OwnedObjectPath::try_from(format!("/iwdrs/agent/{}", Uuid::new_v4().as_simple()))?;