Skip to content

Commit

Permalink
add access point api
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Jun 13, 2024
1 parent b91180f commit e70a66a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 8 deletions.
106 changes: 106 additions & 0 deletions src/access_point.rs
Original file line number Diff line number Diff line change
@@ -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<Connection>,
pub(crate) dbus_path: OwnedObjectPath,
}

impl AccessPoint {
pub(crate) fn new(connection: Arc<Connection>, dbus_path: OwnedObjectPath) -> Self {
Self {
connection,
dbus_path,
}
}

pub(crate) async fn proxy<'a>(&self) -> Result<zbus::Proxy<'a>, 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<Vec<HashMap<String, String>>> {
let proxy = self.proxy().await?;
let networks = proxy.call_method("GetOrderedNetworks", &()).await?;
let body = networks.body();
let body: Vec<HashMap<String, Value>> = body.deserialize()?;
let body = body
.into_iter()
.map(|map| {
map.into_iter()
.map(|(k, v)| (k, v.to_string()))
.collect::<HashMap<String, String>>()
})
.collect::<Vec<HashMap<String, String>>>();

Ok(body)
}

// Propreties
pub async fn has_started(&self) -> Result<bool> {
let proxy = self.proxy().await?;
let has_started: bool = proxy.get_property("Started").await?;
Ok(has_started)
}

pub async fn frequency(&self) -> Result<Option<u32>> {
let proxy = self.proxy().await?;
Ok(proxy.get_property("Frequency").await.ok())
}

pub async fn is_scanning(&self) -> Result<bool> {
let proxy = self.proxy().await?;
let is_scanning: bool = proxy.get_property("Scanning").await?;
Ok(is_scanning)
}

pub async fn name(&self) -> Result<Option<String>> {
let proxy = self.proxy().await?;
Ok(proxy.get_property("Name").await.ok())
}

pub async fn pairwise_ciphers(&self) -> Result<Option<Vec<String>>> {
let proxy = self.proxy().await?;
Ok(proxy.get_property("PairwiseCiphers").await.ok())
}

pub async fn group_cipher(&self) -> Result<Option<String>> {
let proxy = self.proxy().await?;
Ok(proxy.get_property("GroupCipher").await.ok())
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub mod agent;
pub mod netowrk;

pub mod known_netowk;

pub mod access_point;
44 changes: 36 additions & 8 deletions src/session.rs
Original file line number Diff line number Diff line change
@@ -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<Connection>,
Expand Down Expand Up @@ -39,6 +38,21 @@ impl Session {
})
}

pub fn adapter(&self) -> Option<Adapter> {
let adapter: Option<Adapter> = 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<Device> {
let device: Option<Device> = self
.objects
Expand Down Expand Up @@ -68,6 +82,20 @@ impl Session {
station
}

pub fn access_point(&self) -> Option<AccessPoint> {
let access_point: Option<AccessPoint> = 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<AgentManager> {
let path =
OwnedObjectPath::try_from(format!("/iwdrs/agent/{}", Uuid::new_v4().as_simple()))?;
Expand Down

0 comments on commit e70a66a

Please sign in to comment.