1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use core::fmt;
use std::any;

use dbus::{
    arg::{self, Append, Arg, ArgType, Get, IterAppend, RefArg},
    Path, Signature,
};

#[derive(Debug, Clone)]
pub struct Error {
    pub message: &'static str,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

#[derive(Debug, Clone)]
pub struct ConnectionError {
    pub method: &'static str,
}

impl fmt::Display for ConnectionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Could not {} Access Point.", self.method)
    }
}

#[derive(PartialEq, Eq)]
pub enum DeviceType {
    UNKNOWN,
    GENERIC = 1,
    WIFI = 2,
    BT = 5,
    DUMMY = 22,
    OTHER,
}

#[derive(Default, Copy, Clone)]
pub enum WifiStrength {
    Excellent,
    Ok,
    Weak,
    #[default]
    None,
}

impl WifiStrength {
    pub fn from_u8(num: u8) -> Self {
        match num {
            0..=42 => WifiStrength::Weak,
            43..=84 => WifiStrength::Ok,
            85..=128 => WifiStrength::Excellent,
            _ => WifiStrength::None,
        }
    }
}

#[allow(dead_code)]
impl DeviceType {
    pub fn from_u32(num: u32) -> Self {
        match num {
            0 => DeviceType::UNKNOWN,
            1 => DeviceType::GENERIC,
            2 => DeviceType::WIFI,
            5 => DeviceType::BT,
            22 => DeviceType::DUMMY,
            _ => DeviceType::OTHER,
        }
    }
    pub fn _to_u32(&self) -> u32 {
        match self {
            DeviceType::UNKNOWN => 0,
            DeviceType::GENERIC => 1,
            DeviceType::WIFI => 2,
            DeviceType::BT => 5,
            DeviceType::DUMMY => 22,
            DeviceType::OTHER => 90,
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct AccessPoint {
    pub ssid: Vec<u8>,
    pub strength: u8,
    pub associated_connection: Path<'static>,
    pub dbus_path: Path<'static>,
    pub connected: bool,
    pub stored: bool,
}

unsafe impl Send for AccessPoint {}
unsafe impl Sync for AccessPoint {}

impl Append for AccessPoint {
    fn append_by_ref(&self, iter: &mut arg::IterAppend) {
        iter.append_struct(|i| {
            let sig = unsafe { Signature::from_slice_unchecked("y\0") };
            i.append_array(&sig, |i| {
                for byte in self.ssid.iter() {
                    i.append(byte);
                }
            });
            i.append(&self.strength);
            i.append(&self.associated_connection);
            i.append(&self.dbus_path);
            i.append(&self.connected);
            i.append(&self.stored);
        });
    }
}

impl<'a> Get<'a> for AccessPoint {
    fn get(i: &mut arg::Iter<'a>) -> Option<Self> {
        let (ssid, strength, associated_connection, dbus_path, connected, stored) =
            <(Vec<u8>, u8, Path<'static>, Path<'static>, bool, bool)>::get(i)?;
        Some(AccessPoint {
            ssid,
            strength,
            associated_connection,
            dbus_path,
            connected,
            stored,
        })
    }
}

impl Arg for AccessPoint {
    const ARG_TYPE: arg::ArgType = ArgType::Struct;
    fn signature() -> Signature<'static> {
        unsafe { Signature::from_slice_unchecked("(ayyoobb)\0") }
    }
}

impl RefArg for AccessPoint {
    fn arg_type(&self) -> ArgType {
        ArgType::Struct
    }
    fn signature(&self) -> Signature<'static> {
        unsafe { Signature::from_slice_unchecked("(ayyoobb)\0") }
    }
    fn append(&self, i: &mut IterAppend) {
        self.append_by_ref(i);
    }
    #[inline]
    fn as_any(&self) -> &dyn any::Any
    where
        Self: 'static,
    {
        self
    }
    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn any::Any
    where
        Self: 'static,
    {
        self
    }

    fn box_clone(&self) -> Box<dyn RefArg + 'static> {
        Box::new(self.clone())
    }
}