diff --git a/networkmanager/src/connection.rs b/networkmanager/src/active_connection.rs similarity index 97% rename from networkmanager/src/connection.rs rename to networkmanager/src/active_connection.rs index 9fdf1bf..377aed9 100644 --- a/networkmanager/src/connection.rs +++ b/networkmanager/src/active_connection.rs @@ -4,8 +4,8 @@ use crate::{ config::{ip4::Ipv4Config, ip6::Ipv6Config}, device::Device, interface::{ + active_connection::ActiveConnectionProxy, config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy}, - connection::ActiveConnectionProxy, device::DeviceProxy, enums::{ActivationStateFlags, State}, }, diff --git a/networkmanager/src/device.rs b/networkmanager/src/device.rs index c804fe3..c146519 100644 --- a/networkmanager/src/device.rs +++ b/networkmanager/src/device.rs @@ -5,11 +5,11 @@ pub mod wired; pub mod wireless; use crate::{ + active_connection::ActiveConnection, config::{ip4::Ipv4Config, ip6::Ipv6Config}, - connection::ActiveConnection, interface::{ + active_connection::ActiveConnectionProxy, config::{ip4::Ipv4ConfigProxy, ip6::Ipv6ConfigProxy}, - connection::ActiveConnectionProxy, device::{ bluetooth::BluetoothDeviceProxy, wired::WiredDeviceProxy, wireless::WirelessDeviceProxy, DeviceProxy, diff --git a/networkmanager/src/interface/connection.rs b/networkmanager/src/interface/active_connection.rs similarity index 100% rename from networkmanager/src/interface/connection.rs rename to networkmanager/src/interface/active_connection.rs diff --git a/networkmanager/src/interface/mod.rs b/networkmanager/src/interface/mod.rs index d5b2c10..69f8ca4 100644 --- a/networkmanager/src/interface/mod.rs +++ b/networkmanager/src/interface/mod.rs @@ -22,8 +22,8 @@ #![allow(clippy::type_complexity)] pub mod access_point; +pub mod active_connection; pub mod config; -pub mod connection; pub mod device; pub mod enums; pub mod settings; diff --git a/networkmanager/src/lib.rs b/networkmanager/src/lib.rs index 764371f..452ba02 100644 --- a/networkmanager/src/lib.rs +++ b/networkmanager/src/lib.rs @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MPL-2.0 +pub mod active_connection; pub mod config; -pub mod connection; pub mod device; pub mod interface; pub mod nm; +pub mod settings; diff --git a/networkmanager/src/nm.rs b/networkmanager/src/nm.rs index ad6a698..50d959b 100644 --- a/networkmanager/src/nm.rs +++ b/networkmanager/src/nm.rs @@ -1,18 +1,59 @@ // SPDX-License-Identifier: MPL-2.0 use crate::{ + active_connection::ActiveConnection, device::Device, - interface::{device::DeviceProxy, NetworkManagerProxy}, + interface::{ + active_connection::ActiveConnectionProxy, + device::DeviceProxy, + enums::{ConnectivityState, State}, + NetworkManagerProxy, + }, + settings::connection::Connection, }; -use zbus::{Connection, Result}; +use zbus::{zvariant::ObjectPath, Result}; pub struct NetworkManager<'a>(NetworkManagerProxy<'a>); impl<'a> NetworkManager<'a> { - pub async fn new(connection: &'a Connection) -> Result> { + pub async fn new(connection: &'a zbus::Connection) -> Result> { NetworkManagerProxy::new(connection).await.map(Self) } + pub async fn activate_connection( + &self, + connection: &'a Connection<'a>, + device: &'a Device<'a>, + ) -> Result> { + let connection = connection.path(); + let device = device.path(); + let specific_object = ObjectPath::from_static_str("/").unwrap(); + let active_connection_path = self + .0 + .activate_connection(connection, device, &specific_object) + .await?; + ActiveConnectionProxy::builder(self.0.connection()) + .path(active_connection_path)? + .build() + .await + .map(ActiveConnection::from) + } + + pub async fn connectivity(&self) -> Result { + self.0.connectivity().await.map(ConnectivityState::from) + } + + pub async fn check_connectivity(&self) -> Result { + self.0 + .check_connectivity() + .await + .map(ConnectivityState::from) + } + + pub async fn deactivate_connection(&self, connection: &'a ActiveConnection<'a>) -> Result<()> { + self.0.deactivate_connection(connection.path()).await + } + pub async fn devices(&self) -> Result>> { let devices = self.0.get_all_devices().await?; let mut out = Vec::with_capacity(devices.len()); @@ -38,4 +79,8 @@ impl<'a> NetworkManager<'a> { } Ok(out) } + + pub async fn state(&self) -> State { + self.0.state().await.map(State::from).unwrap() + } } diff --git a/networkmanager/src/settings.rs b/networkmanager/src/settings.rs new file mode 100644 index 0000000..4d5ecc8 --- /dev/null +++ b/networkmanager/src/settings.rs @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: MPL-2.0 + +pub mod connection; diff --git a/networkmanager/src/settings/connection.rs b/networkmanager/src/settings/connection.rs new file mode 100644 index 0000000..99e9f1b --- /dev/null +++ b/networkmanager/src/settings/connection.rs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MPL-2.0 + +use crate::interface::settings::connection::ConnectionSettingsProxy; +use std::ops::Deref; + +pub struct Connection<'a>(ConnectionSettingsProxy<'a>); + +impl<'a> Deref for Connection<'a> { + type Target = ConnectionSettingsProxy<'a>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'a> From> for Connection<'a> { + fn from(conn: ConnectionSettingsProxy<'a>) -> Self { + Connection(conn) + } +}