passcod_networkmanager/
networkmanager.rs1use std::rc::Rc;
2
3use crate::dbus_api::DBusAccessor;
4use crate::devices::Device;
5use crate::errors::Error;
6use crate::gen::OrgFreedesktopNetworkManager;
7use crate::settings::Settings;
8use crate::types::ReloadFlag;
9use dbus::blocking::Connection;
10
11use num_traits::ToPrimitive;
12
13const NETWORK_MANAGER_BUS: &str = "org.freedesktop.NetworkManager";
14const NETWORK_MANAGER_PATH: &str = "/org/freedesktop/NetworkManager";
15
16#[derive(Clone, Debug)]
17pub struct NetworkManager {
18 dbus_accessor: DBusAccessor,
19}
20
21impl NetworkManager {
22 pub fn new() -> Result<Self, Error> {
23 Connection::new_system()
24 .map(Self::new_with_dbus)
25 .map_err(Error::DBus)
26 }
27
28 pub fn new_with_dbus(dbus_connection: Connection) -> Self {
29 NetworkManager {
30 dbus_accessor: DBusAccessor::new(
31 Rc::new(dbus_connection),
32 NETWORK_MANAGER_BUS,
33 NETWORK_MANAGER_PATH,
34 ),
35 }
36 }
37
38 fn paths_to_devices(&self, paths: Vec<dbus::Path<'_>>) -> Result<Vec<Device>, Error> {
39 let mut res = Vec::new();
40 for path in paths {
41 res.push(Device::new(self.dbus_accessor.with_path(path))?);
42 }
43 Ok(res)
44 }
45
46 fn path_to_device(&self, path: dbus::Path<'_>) -> Result<Device, Error> {
47 Device::new(self.dbus_accessor.with_path(path))
48 }
49
50 pub fn reload(&self, flags: ReloadFlag) -> Result<(), Error> {
52 match ToPrimitive::to_u32(&flags) {
53 Some(x) => Ok(proxy!(self).reload(x)?),
54 None => Err(Error::UnsupportedType),
55 }
56 }
57
58 pub fn get_devices(&self) -> Result<Vec<Device>, Error> {
60 let dev_paths = proxy!(self).get_devices()?;
61 self.paths_to_devices(dev_paths)
62 }
63
64 pub fn get_all_devices(&self) -> Result<Vec<Device>, Error> {
66 let dev_paths = proxy!(self).get_all_devices()?;
67 self.paths_to_devices(dev_paths)
68 }
69
70 pub fn get_device_by_ip_iface(&self, iface: &str) -> Result<Device, Error> {
71 let dev_path = proxy!(self).get_device_by_ip_iface(iface)?;
72 self.path_to_device(dev_path)
73 }
74
75 pub fn networking_enabled(&self) -> Result<bool, Error> {
76 Ok(proxy!(self).networking_enabled()?)
77 }
78
79 pub fn wireless_enabled(&self) -> Result<bool, Error> {
80 Ok(proxy!(self).wireless_enabled()?)
81 }
82
83 pub fn wireless_hardware_enabled(&self) -> Result<bool, Error> {
84 Ok(proxy!(self).wireless_hardware_enabled()?)
85 }
86
87 pub fn startup(&self) -> Result<bool, Error> {
89 Ok(proxy!(self).startup()?)
90 }
91
92 pub fn settings(&self) -> Result<Settings, Error> {
94 Ok(Settings::new(self.dbus_accessor.clone()))
95 }
96}