passcod_networkmanager/devices/
any.rs

1use super::{BridgeDevice, EthernetDevice, GenericDevice, VethDevice, WiFiDevice};
2use crate::configs::{Dhcp4Config, Dhcp6Config, Ip4Config, Ip6Config};
3use crate::connection::Connection;
4use crate::errors::Error;
5use crate::gen::OrgFreedesktopNetworkManagerDevice;
6use crate::types::{Capability, ConnectivityState, DeviceInterfaceFlag, DeviceType};
7use num_traits::FromPrimitive;
8
9type HashMapDBusVariant =
10    std::collections::HashMap<String, dbus::arg::Variant<Box<dyn dbus::arg::RefArg + 'static>>>;
11
12type HashMapDBusVariantStr<'a> = std::collections::HashMap<
13    &'a str,
14    std::collections::HashMap<String, dbus::arg::Variant<Box<dyn dbus::arg::RefArg>>>,
15>;
16
17pub trait Any {
18    fn reapply(
19        &self,
20        connection: HashMapDBusVariantStr<'_>,
21        version_id: u64,
22        flags: u32,
23    ) -> Result<(), Error>;
24    fn get_applied_connection(
25        &self,
26        flags: u32,
27    ) -> Result<(::std::collections::HashMap<String, HashMapDBusVariant>, u64), Error>;
28    fn disconnect(&self) -> Result<(), Error>;
29    fn delete(&self) -> Result<(), Error>;
30    fn udi(&self) -> Result<String, Error>;
31    fn interface(&self) -> Result<String, Error>;
32    fn ip_interface(&self) -> Result<String, Error>;
33    fn driver(&self) -> Result<String, Error>;
34    fn driver_version(&self) -> Result<String, Error>;
35    fn firmware_version(&self) -> Result<String, Error>;
36    fn capabilities(&self) -> Result<Capability, Error>;
37    fn ip4_address(&self) -> Result<u32, Error>;
38    fn state(&self) -> Result<u32, Error>;
39    fn state_reason(&self) -> Result<(u32, u32), Error>;
40    fn active_connection(&self) -> Result<Connection, Error>;
41    fn ip4_config(&self) -> Result<Ip4Config, Error>;
42    fn dhcp4_config(&self) -> Result<Dhcp4Config, Error>;
43    fn ip6_config(&self) -> Result<Ip6Config, Error>;
44    fn dhcp6_config(&self) -> Result<Dhcp6Config, Error>;
45    fn managed(&self) -> Result<bool, Error>;
46    fn set_managed(&self, value: bool) -> Result<(), Error>;
47    fn autoconnect(&self) -> Result<bool, Error>;
48    fn set_autoconnect(&self, value: bool) -> Result<(), Error>;
49    fn firmware_missing(&self) -> Result<bool, Error>;
50    fn nm_plugin_missing(&self) -> Result<bool, Error>;
51    fn device_type(&self) -> Result<DeviceType, Error>;
52    fn available_connections(&self) -> Result<Vec<Connection>, Error>;
53    fn physical_port_id(&self) -> Result<String, Error>;
54    fn mtu(&self) -> Result<u32, Error>;
55    fn metered(&self) -> Result<u32, Error>;
56    fn lldp_neighbors(&self) -> Result<Vec<HashMapDBusVariant>, Error>;
57    fn real(&self) -> Result<bool, Error>;
58    fn ip4_connectivity(&self) -> Result<ConnectivityState, Error>;
59    fn ip6_connectivity(&self) -> Result<ConnectivityState, Error>;
60    fn interface_flags(&self) -> Result<DeviceInterfaceFlag, Error>;
61    fn hw_address(&self) -> Result<String, Error>;
62}
63
64macro_rules! impl_any {
65    ($name:ty) => {
66        impl Any for $name {
67            fn reapply(
68                &self,
69                connection: HashMapDBusVariantStr<'_>,
70                version_id: u64,
71                flags: u32,
72            ) -> Result<(), Error> {
73                Ok(proxy!(self).reapply(connection, version_id, flags)?)
74            }
75            fn get_applied_connection(
76                &self,
77                flags: u32,
78            ) -> Result<(std::collections::HashMap<String, HashMapDBusVariant>, u64), Error> {
79                Ok(proxy!(self).get_applied_connection(flags)?)
80            }
81            fn disconnect(&self) -> Result<(), Error> {
82                Ok(proxy!(self).disconnect()?)
83            }
84            fn delete(&self) -> Result<(), Error> {
85                Ok(proxy!(self).delete()?)
86            }
87            fn udi(&self) -> Result<String, Error> {
88                Ok(proxy!(self).udi()?)
89            }
90            fn interface(&self) -> Result<String, Error> {
91                Ok(proxy!(self).interface()?)
92            }
93            fn ip_interface(&self) -> Result<String, Error> {
94                Ok(proxy!(self).ip_interface()?)
95            }
96            fn driver(&self) -> Result<String, Error> {
97                Ok(proxy!(self).driver()?)
98            }
99            fn driver_version(&self) -> Result<String, Error> {
100                Ok(proxy!(self).driver_version()?)
101            }
102            fn firmware_version(&self) -> Result<String, Error> {
103                Ok(proxy!(self).firmware_version()?)
104            }
105            fn capabilities(&self) -> Result<Capability, Error> {
106                let cap = proxy!(self).capabilities()?;
107                match FromPrimitive::from_u32(cap) {
108                    Some(x) => Ok(x),
109                    None => Err(Error::UnsupportedType),
110                }
111            }
112            fn ip4_address(&self) -> Result<u32, Error> {
113                Ok(proxy!(self).ip4_address()?)
114            }
115            fn state(&self) -> Result<u32, Error> {
116                Ok(proxy!(self).state()?)
117            }
118            fn state_reason(&self) -> Result<(u32, u32), Error> {
119                Ok(proxy!(self).state_reason()?)
120            }
121            fn active_connection(&self) -> Result<Connection, Error> {
122                let path = proxy!(self).active_connection()?;
123                Ok(Connection::new(self.dbus_accessor.with_path(path)))
124            }
125            fn ip4_config(&self) -> Result<Ip4Config, Error> {
126                let path = proxy!(self).ip4_config()?;
127                Ok(Ip4Config::new(self.dbus_accessor.with_path(path)))
128            }
129            fn dhcp4_config(&self) -> Result<Dhcp4Config, Error> {
130                let path = proxy!(self).dhcp4_config()?;
131                Ok(Dhcp4Config::new(self.dbus_accessor.with_path(path)))
132            }
133            fn ip6_config(&self) -> Result<Ip6Config, Error> {
134                let path = proxy!(self).ip6_config()?;
135                Ok(Ip6Config::new(self.dbus_accessor.with_path(path)))
136            }
137            fn dhcp6_config(&self) -> Result<Dhcp6Config, Error> {
138                let path = proxy!(self).dhcp6_config()?;
139                Ok(Dhcp6Config::new(self.dbus_accessor.with_path(path)))
140            }
141            fn managed(&self) -> Result<bool, Error> {
142                Ok(proxy!(self).managed()?)
143            }
144            fn set_managed(&self, value: bool) -> Result<(), Error> {
145                Ok(proxy!(self).set_managed(value)?)
146            }
147            fn autoconnect(&self) -> Result<bool, Error> {
148                Ok(proxy!(self).autoconnect()?)
149            }
150            fn set_autoconnect(&self, value: bool) -> Result<(), Error> {
151                Ok(proxy!(self).set_autoconnect(value)?)
152            }
153            fn firmware_missing(&self) -> Result<bool, Error> {
154                Ok(proxy!(self).firmware_missing()?)
155            }
156            fn nm_plugin_missing(&self) -> Result<bool, Error> {
157                Ok(proxy!(self).nm_plugin_missing()?)
158            }
159            fn device_type(&self) -> Result<DeviceType, Error> {
160                let dev_type = proxy!(self).device_type()?;
161                match FromPrimitive::from_u32(dev_type) {
162                    Some(x) => Ok(x),
163                    None => Err(Error::UnsupportedType),
164                }
165            }
166            fn available_connections(&self) -> Result<Vec<Connection>, Error> {
167                let paths = proxy!(self).available_connections()?;
168                let mut connections = Vec::with_capacity(paths.len());
169                for path in paths {
170                    connections.push(Connection::new(self.dbus_accessor.with_path(path)));
171                }
172                Ok(connections)
173            }
174            fn physical_port_id(&self) -> Result<String, Error> {
175                Ok(proxy!(self).physical_port_id()?)
176            }
177            fn mtu(&self) -> Result<u32, Error> {
178                Ok(proxy!(self).mtu()?)
179            }
180            fn lldp_neighbors(&self) -> Result<Vec<HashMapDBusVariant>, Error> {
181                Ok(proxy!(self).lldp_neighbors()?)
182            }
183            fn metered(&self) -> Result<u32, Error> {
184                Ok(proxy!(self).metered()?)
185            }
186            fn real(&self) -> Result<bool, Error> {
187                Ok(proxy!(self).real()?)
188            }
189            fn ip4_connectivity(&self) -> Result<ConnectivityState, Error> {
190                let con = proxy!(self).ip4_connectivity()?;
191                match FromPrimitive::from_u32(con) {
192                    Some(x) => Ok(x),
193                    None => Err(Error::UnsupportedType),
194                }
195            }
196            fn ip6_connectivity(&self) -> Result<ConnectivityState, Error> {
197                let con = proxy!(self).ip6_connectivity()?;
198                match FromPrimitive::from_u32(con) {
199                    Some(x) => Ok(x),
200                    None => Err(Error::UnsupportedType),
201                }
202            }
203            fn interface_flags(&self) -> Result<DeviceInterfaceFlag, Error> {
204                let interface_flag = proxy!(self).interface_flags()?;
205                match FromPrimitive::from_u32(interface_flag) {
206                    Some(x) => Ok(x),
207                    None => Err(Error::UnsupportedType),
208                }
209            }
210            fn hw_address(&self) -> Result<String, Error> {
211                Ok(proxy!(self).hw_address()?)
212            }
213        }
214    };
215}
216
217impl_any!(VethDevice);
218impl_any!(BridgeDevice);
219impl_any!(WiFiDevice);
220impl_any!(EthernetDevice);
221impl_any!(GenericDevice);