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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::gatt::*;
use crate::{Bluetooth, Error, MAC, UUID};
use rustbus::params;
use rustbus::params::{Base, Param};
use std::collections::hash_map;
use std::collections::HashMap;
use std::ffi::OsString;
use std::path::PathBuf;
use std::rc::Rc;

pub enum AddrType {
    Public,
    Random,
}
pub trait Device<'a>: Sized {
    type ServiceBase;
    type ServiceType: Service<'a>;
    fn services(&mut self) -> hash_map::Keys<UUID, Self::ServiceBase>;
    fn get_service(&'a mut self, uuid: &UUID) -> Option<Self::ServiceType>;
    fn has_service(&self, uuid: &UUID) -> bool;
    fn address(&self) -> &MAC;
    fn address_type(&mut self) -> AddrType;
    fn name(&mut self) -> String;
}
pub struct RemoteDeviceBase {
    pub(crate) mac: MAC,
    pub(crate) path: PathBuf,
    pub(crate) services: HashMap<MAC, RemoteServiceBase>,
    connected: State,
    paired: State,
    //comp_map: HashMap<OsString, MAC>,
}
impl RemoteDeviceBase {
    pub(crate) fn from_props(
        value: &mut HashMap<String, params::Variant>,
        path: PathBuf,
    ) -> Result<Self, Error> {
        let mac = match value.remove("Address") {
            Some(addr) => {
                if let Param::Base(Base::String(addr)) = addr.value {
                    addr.into()
                } else {
                    return Err(Error::DbusReqErr(
                        "Invalid device returned; Address field is invalid type".to_string(),
                    ));
                }
            }
            None => {
                return Err(Error::DbusReqErr(
                    "Invalid device returned; missing Address field".to_string(),
                ))
            }
        };
        let connected = match value.remove("Connected") {
            Some(val) => {
                if let Param::Base(Base::Boolean(val)) = val.value {
                    val.into()
                } else {
                    return Err(Error::DbusReqErr(
                        "Invalid device returned; Address field is invalid type".to_string(),
                    ));
                }
            }
            None => {
                return Err(Error::DbusReqErr(
                    "Invalid device returned; missing Address field".to_string(),
                ))
            }
        };
        let paired = match value.remove("Paired") {
            Some(val) => {
                if let Param::Base(Base::Boolean(val)) = val.value {
                    val.into()
                } else {
                    return Err(Error::DbusReqErr(
                        "Invalid device returned; Address field is invalid type".to_string(),
                    ));
                }
            }
            None => {
                return Err(Error::DbusReqErr(
                    "Invalid device returned; missing Address field".to_string(),
                ))
            }
        };
        Ok(RemoteDeviceBase {
            mac,
            path,
            connected,
            paired,
            services: HashMap::new(),
        })
    }
}
enum State {
    WaitRes(u32),
    Yes,
    No,
}
impl From<bool> for State {
    fn from(b: bool) -> Self {
        if b {
            State::Yes
        } else {
            State::No
        }
    }
}

pub struct RemoteDevice<'a> {
    pub(crate) mac: MAC,
    pub(crate) blue: &'a mut Bluetooth,
    #[cfg(feature = "unsafe-opt")]
    ptr: *mut RemoteDeviceBase,
}
impl RemoteDevice<'_> {
    fn get_base(&self) -> &RemoteDeviceBase {
        #[cfg(feature = "unsafe-opt")]
        {
            return &*self.ptr;
        }
        &self.blue.devices[&self.mac]
    }
    fn get_base_mut(&mut self) -> &mut RemoteDeviceBase {
        #[cfg(feature = "unsafe-opt")]
        {
            return &mut *self.ptr;
        }
        self.blue.devices.get_mut(&self.mac).unwrap()
    }
    #[inline]
    pub fn connected(&self) -> bool {
        if let State::Yes = self.get_base().connected {
            true
        } else {
            false
        }
    }
    pub fn connect(&mut self) -> Result<(), Error> {
        unimplemented!()
    }
    #[inline]
    pub fn paired(&self) -> bool {
        if let State::Yes = self.get_base().paired {
            true
        } else {
            false
        }
    }
    pub fn pair(&mut self) -> Result<(), Error> {
        unimplemented!()
    }
    pub fn forget_service(&mut self, uuid: &UUID) -> bool {
        self.get_base_mut().services.remove(uuid).is_some()
    }
}
impl<'a, 'c: 'a> Device<'a> for RemoteDevice<'c> {
    type ServiceBase = RemoteServiceBase;
    type ServiceType = RemoteService<'a, 'c>;
    fn services(&mut self) -> hash_map::Keys<UUID, Self::ServiceBase> {
        let base = self.get_base_mut();
        base.services.keys()
    }
    fn get_service(&'a mut self, uuid: &UUID) -> Option<Self::ServiceType> {
        let base = self.get_base_mut();
        let _serv_base = base.services.get_mut(uuid)?;
        Some(RemoteService {
            dev: self,
            uuid: uuid.clone(),
            #[cfg(feature = "unsafe-opt")]
            base: _serv_base,
        })
    }
    fn has_service(&self, uuid: &UUID) -> bool {
        self.get_base().services.contains_key(uuid)
    }
    fn address(&self) -> &MAC {
        &self.mac
    }
    fn address_type(&mut self) -> AddrType {
        unimplemented!()
    }
    fn name(&mut self) -> String {
        unimplemented!()
    }
}

impl<'a, 'b: 'a, 'c: 'a> Device<'a> for Bluetooth {
    type ServiceBase = LocalServiceBase;
    type ServiceType = LocalService<'a>;
    fn services(&mut self) -> hash_map::Keys<UUID, Self::ServiceBase> {
        self.services.keys()
    }
    fn get_service(&'a mut self, uuid: &UUID) -> Option<Self::ServiceType> {
        let _base = self.services.get_mut(uuid)?;
        Some(LocalService {
            bt: self,
            uuid: uuid.clone(),
            #[cfg(feature = "unsafe-opt")]
            ptr: _base,
        })
    }
    fn has_service(&self, uuid: &UUID) -> bool {
        self.devices.contains_key(uuid)
    }
    fn address(&self) -> &MAC {
        unimplemented!()
    }
    fn address_type(&mut self) -> AddrType {
        unimplemented!()
    }
    fn name(&mut self) -> String {
        unimplemented!()
    }
}