ReSet_Lib/network/
network.rs

1use core::fmt;
2use std::any;
3
4use dbus::{
5    arg::{self, Append, Arg, ArgType, Get, IterAppend, RefArg},
6    Path, Signature,
7};
8
9#[derive(Debug, Clone)]
10pub struct Error {
11    pub message: &'static str,
12}
13
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        write!(f, "{}", self.message)
17    }
18}
19
20#[derive(Debug, Clone)]
21pub struct ConnectionError {
22    pub method: &'static str,
23}
24
25impl fmt::Display for ConnectionError {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        write!(f, "Could not {} Access Point.", self.method)
28    }
29}
30
31#[derive(PartialEq, Eq)]
32pub enum DeviceType {
33    UNKNOWN,
34    GENERIC = 1,
35    WIFI = 2,
36    BT = 5,
37    DUMMY = 22,
38    OTHER,
39}
40
41#[derive(Default, Copy, Clone)]
42pub enum WifiStrength {
43    Excellent,
44    Ok,
45    Weak,
46    #[default]
47    None,
48}
49
50impl WifiStrength {
51    pub fn from_u8(num: u8) -> Self {
52        match num {
53            0..=42 => WifiStrength::Weak,
54            43..=84 => WifiStrength::Ok,
55            85..=128 => WifiStrength::Excellent,
56            _ => WifiStrength::None,
57        }
58    }
59}
60
61#[allow(dead_code)]
62impl DeviceType {
63    pub fn from_u32(num: u32) -> Self {
64        match num {
65            0 => DeviceType::UNKNOWN,
66            1 => DeviceType::GENERIC,
67            2 => DeviceType::WIFI,
68            5 => DeviceType::BT,
69            22 => DeviceType::DUMMY,
70            _ => DeviceType::OTHER,
71        }
72    }
73    pub fn _to_u32(&self) -> u32 {
74        match self {
75            DeviceType::UNKNOWN => 0,
76            DeviceType::GENERIC => 1,
77            DeviceType::WIFI => 2,
78            DeviceType::BT => 5,
79            DeviceType::DUMMY => 22,
80            DeviceType::OTHER => 90,
81        }
82    }
83}
84
85#[derive(Debug, Clone, Default)]
86pub struct AccessPoint {
87    pub ssid: Vec<u8>,
88    pub strength: u8,
89    pub associated_connection: Path<'static>,
90    pub dbus_path: Path<'static>,
91    pub stored: bool,
92}
93
94unsafe impl Send for AccessPoint {}
95unsafe impl Sync for AccessPoint {}
96
97impl Append for AccessPoint {
98    fn append_by_ref(&self, iter: &mut arg::IterAppend) {
99        iter.append_struct(|i| {
100            let sig = unsafe { Signature::from_slice_unchecked("y\0") };
101            i.append_array(&sig, |i| {
102                for byte in self.ssid.iter() {
103                    i.append(byte);
104                }
105            });
106            i.append(self.strength);
107            i.append(&self.associated_connection);
108            i.append(&self.dbus_path);
109            i.append(self.stored);
110        });
111    }
112}
113
114impl<'a> Get<'a> for AccessPoint {
115    fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
116        let (ssid, strength, associated_connection, dbus_path, stored) =
117            <(Vec<u8>, u8, Path<'static>, Path<'static>, bool)>::get(i)?;
118        Some(AccessPoint {
119            ssid,
120            strength,
121            associated_connection,
122            dbus_path,
123            stored,
124        })
125    }
126}
127
128impl Arg for AccessPoint {
129    const ARG_TYPE: arg::ArgType = ArgType::Struct;
130    fn signature() -> Signature<'static> {
131        unsafe { Signature::from_slice_unchecked("(ayyoob)\0") }
132    }
133}
134
135impl RefArg for AccessPoint {
136    fn arg_type(&self) -> ArgType {
137        ArgType::Struct
138    }
139    fn signature(&self) -> Signature<'static> {
140        unsafe { Signature::from_slice_unchecked("(ayyoob)\0") }
141    }
142    fn append(&self, i: &mut IterAppend) {
143        self.append_by_ref(i);
144    }
145    #[inline]
146    fn as_any(&self) -> &dyn any::Any
147    where
148        Self: 'static,
149    {
150        self
151    }
152    #[inline]
153    fn as_any_mut(&mut self) -> &mut dyn any::Any
154    where
155        Self: 'static,
156    {
157        self
158    }
159
160    fn box_clone(&self) -> Box<dyn RefArg + 'static> {
161        Box::new(self.clone())
162    }
163}
164
165#[derive(Debug, Clone, Default)]
166pub struct WifiDevice {
167    pub path: Path<'static>,
168    pub name: String,
169    pub active_access_point: Path<'static>,
170}
171
172unsafe impl Send for WifiDevice {}
173unsafe impl Sync for WifiDevice {}
174
175impl Append for WifiDevice {
176    fn append_by_ref(&self, iter: &mut arg::IterAppend) {
177        iter.append_struct(|i| {
178            i.append(&self.path);
179            i.append(&self.name);
180            i.append(&self.active_access_point);
181        });
182    }
183}
184
185impl<'a> Get<'a> for WifiDevice {
186    fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
187        let (path, name, active_access_point) = <(Path<'static>, String, Path<'static>)>::get(i)?;
188        Some(WifiDevice {
189            path,
190            name,
191            active_access_point,
192        })
193    }
194}
195
196impl Arg for WifiDevice {
197    const ARG_TYPE: arg::ArgType = ArgType::Struct;
198    fn signature() -> Signature<'static> {
199        unsafe { Signature::from_slice_unchecked("(oso)\0") }
200    }
201}
202
203impl RefArg for WifiDevice {
204    fn arg_type(&self) -> ArgType {
205        ArgType::Struct
206    }
207    fn signature(&self) -> Signature<'static> {
208        unsafe { Signature::from_slice_unchecked("(oso)\0") }
209    }
210    fn append(&self, i: &mut IterAppend) {
211        self.append_by_ref(i);
212    }
213    #[inline]
214    fn as_any(&self) -> &dyn any::Any
215    where
216        Self: 'static,
217    {
218        self
219    }
220    #[inline]
221    fn as_any_mut(&mut self) -> &mut dyn any::Any
222    where
223        Self: 'static,
224    {
225        self
226    }
227
228    fn box_clone(&self) -> Box<dyn RefArg + 'static> {
229        Box::new(self.clone())
230    }
231}