blurmock/
fake_adapter.rs

1use core::ops::Deref;
2use fake_device::FakeBluetoothDevice;
3use fake_discovery_session::FakeBluetoothDiscoverySession;
4use hex;
5use std::error::Error;
6use std::sync::{Arc, Mutex};
7
8#[derive(Clone, Debug)]
9pub struct FakeBluetoothAdapter {
10    id: Arc<Mutex<String>>,
11    is_present: Arc<Mutex<bool>>,
12    is_powered: Arc<Mutex<bool>>,
13    can_start_discovery: Arc<Mutex<bool>>,
14    can_stop_discovery: Arc<Mutex<bool>>,
15    devices: Arc<Mutex<Vec<Arc<FakeBluetoothDevice>>>>,
16    ad_datas: Arc<Mutex<Vec<String>>>,
17    address: Arc<Mutex<String>>,
18    name: Arc<Mutex<String>>,
19    alias: Arc<Mutex<String>>,
20    class: Arc<Mutex<u32>>,
21    is_discoverable: Arc<Mutex<bool>>,
22    is_pairable: Arc<Mutex<bool>>,
23    pairable_timeout: Arc<Mutex<u32>>,
24    discoverable_timeout: Arc<Mutex<u32>>,
25    is_discovering: Arc<Mutex<bool>>,
26    uuids: Arc<Mutex<Vec<String>>>,
27    modalias: Arc<Mutex<String>>,
28}
29
30impl FakeBluetoothAdapter {
31    pub fn new(id: String,
32               is_present: bool,
33               is_powered: bool,
34               can_start_discovery: bool,
35               can_stop_discovery: bool,
36               devices: Vec<Arc<FakeBluetoothDevice>>,
37               ad_datas: Vec<String>,
38               address: String,
39               name: String,
40               alias: String,
41               class: u32,
42               is_discoverable: bool,
43               is_pairable: bool,
44               pairable_timeout: u32,
45               discoverable_timeout: u32,
46               is_discovering: bool,
47               uuids: Vec<String>,
48               modalias: String)
49               -> Arc<FakeBluetoothAdapter> {
50        Arc::new(FakeBluetoothAdapter {
51            id: Arc::new(Mutex::new(id)),
52            is_present: Arc::new(Mutex::new(is_present)),
53            is_powered: Arc::new(Mutex::new(is_powered)),
54            can_start_discovery: Arc::new(Mutex::new(can_start_discovery)),
55            can_stop_discovery: Arc::new(Mutex::new(can_stop_discovery)),
56            devices: Arc::new(Mutex::new(devices)),
57            ad_datas: Arc::new(Mutex::new(ad_datas)),
58            address: Arc::new(Mutex::new(address)),
59            name: Arc::new(Mutex::new(name)),
60            alias: Arc::new(Mutex::new(alias)),
61            class: Arc::new(Mutex::new(class)),
62            is_discoverable: Arc::new(Mutex::new(is_discoverable)),
63            is_pairable: Arc::new(Mutex::new(is_pairable)),
64            pairable_timeout: Arc::new(Mutex::new(pairable_timeout)),
65            discoverable_timeout: Arc::new(Mutex::new(discoverable_timeout)),
66            is_discovering: Arc::new(Mutex::new(is_discovering)),
67            uuids: Arc::new(Mutex::new(uuids)),
68            modalias: Arc::new(Mutex::new(modalias)),
69        })
70    }
71
72    pub fn new_empty() -> Arc<FakeBluetoothAdapter> {
73        FakeBluetoothAdapter::new(
74            /*id*/ String::new(),
75            /*is_present*/ true,
76            /*is_powered*/ false,
77            /*can_start_discovery*/ true,
78            /*can_stop_discovery*/ true,
79            /*devices*/ vec![],
80            /*ad_datas*/ vec![],
81            /*address*/ String::new(),
82            /*name*/ String::new(),
83            /*alias*/ String::new(),
84            /*class*/ 0,
85            /*is_discoverable*/ false,
86            /*is_pairable*/ false,
87            /*pairable_timeout*/ 0,
88            /*discoverable_timeout*/ 0,
89            /*is_discovering*/ false,
90            /*uuids*/ vec![],
91            /*modalias*/ String::new(),
92        )
93    }
94
95    make_getter!(get_id, id);
96
97    make_setter!(set_id, id);
98
99    make_getter!(is_present);
100
101    make_setter!(set_present, is_present, bool);
102
103    make_getter!(is_powered);
104
105    make_setter!(set_powered, is_powered, bool);
106
107    make_getter!(get_can_start_discovery, can_start_discovery, bool);
108
109    make_setter!(set_can_start_discovery, can_start_discovery, bool);
110
111    make_getter!(get_can_stop_discovery, can_stop_discovery, bool);
112
113    make_setter!(set_can_stop_discovery, can_stop_discovery, bool);
114
115    make_getter!(get_devices, devices, Vec<Arc<FakeBluetoothDevice>>);
116
117    make_getter!(get_ad_datas, ad_datas, Vec<String>);
118
119    make_setter!(set_ad_datas, ad_datas, Vec<String>);
120
121    make_getter!(get_address, address, String);
122
123    make_setter!(set_address, address, String);
124
125    make_getter!(get_name, name, String);
126
127    make_setter!(set_name, name, String);
128
129    make_getter!(get_alias, alias, String);
130
131    make_setter!(set_alias, alias, String);
132
133    make_getter!(get_class, class, u32);
134
135    make_setter!(set_class, class, u32);
136
137    make_getter!(is_discoverable);
138
139    make_setter!(set_discoverable, is_discoverable, bool);
140
141    make_getter!(is_pairable);
142
143    make_setter!(set_pairable, is_pairable, bool);
144
145    make_getter!(get_pairable_timeout, pairable_timeout, u32);
146
147    make_setter!(set_pairable_timeout, pairable_timeout, u32);
148
149    make_getter!(get_discoverable_timeout, discoverable_timeout, u32);
150
151    make_setter!(set_discoverable_timeout, discoverable_timeout, u32);
152
153    make_getter!(is_discovering);
154
155    make_setter!(set_discovering, is_discovering, bool);
156
157    make_getter!(get_uuids, uuids, Vec<String>);
158
159    make_setter!(set_uuids, uuids, Vec<String>);
160
161    make_setter!(set_modalias, modalias, String);
162
163    pub fn get_device(&self, id: String) -> Result<Arc<FakeBluetoothDevice>, Box<Error>> {
164        let devices = try!(self.get_devices());
165        for device in devices {
166            let device_id = device.get_id();
167            if device_id == id {
168                return Ok(device);
169            }
170        }
171        Err(Box::from("No device exists with the given id."))
172    }
173
174    pub fn get_device_list(&self) -> Result<Vec<String>, Box<Error>> {
175        let devices = try!(self.get_devices());
176        let mut ids = vec![];
177        for device in &devices {
178            let id = device.get_id();
179            ids.push(id);
180        }
181        Ok(ids)
182    }
183
184    pub fn get_first_device(&self) -> Result<Arc<FakeBluetoothDevice>, Box<Error>> {
185        let devices = try!(self.get_devices());
186        if devices.is_empty() {
187            return Err(Box::from("No device found."))
188        }
189        Ok(devices[0].clone())
190    }
191
192    pub fn add_device(&self, device: Arc<FakeBluetoothDevice>) -> Result<(), Box<Error>> {
193        let cloned = self.devices.clone();
194        let mut devices = match cloned.lock() {
195            Ok(guard) => guard,
196            Err(_) => return Err(Box::from("Could not get the value.")),
197        };
198        Ok(devices.push(device))
199    }
200
201    pub fn remove_device(&self, id: String) -> Result<(), Box<Error>> {
202        let cloned = self.devices.clone();
203        let mut devices = match cloned.lock() {
204            Ok(guard) => guard,
205            Err(_) => return Err(Box::from("Could not get the value.")),
206        };
207        Ok(devices.retain(|d| d.get_id() != id))
208    }
209
210    pub fn get_first_ad_data(&self) -> Result<String, Box<Error>> {
211        let ad_datas = try!(self.get_ad_datas());
212        if ad_datas.is_empty() {
213            return Err(Box::from("No ad_data found."))
214        }
215        Ok(ad_datas[0].clone())
216    }
217
218     pub fn create_discovery_session(&self) -> Result<FakeBluetoothDiscoverySession, Box<Error>> {
219        FakeBluetoothDiscoverySession::create_session(Arc::new(self.clone()))
220    }
221
222    pub fn get_modalias(&self) ->  Result<(String, u32, u32, u32), Box<Error>> {
223        let cloned = self.modalias.clone();
224        let modalias = match cloned.lock() {
225            Ok(guard) => guard.deref().clone(),
226            Err(_) => return Err(Box::from("Could not get the value.")),
227        };
228        let ids: Vec<&str> = modalias.split(":").collect();
229
230        let source = String::from(ids[0]);
231        let vendor = hex::decode(&ids[1][1..5]).unwrap();
232        let product = hex::decode(&ids[1][6..10]).unwrap();
233        let device = hex::decode(&ids[1][11..15]).unwrap();
234
235        Ok((source,
236        (vendor[0] as u32) * 16 * 16 + (vendor[1] as u32),
237        (product[0] as u32) * 16 * 16 + (product[1] as u32),
238        (device[0] as u32) * 16 * 16 + (device[1] as u32)))
239    }
240
241    pub fn get_vendor_id_source(&self) -> Result<String, Box<Error>> {
242        let (vendor_id_source,_,_,_) = try!(self.get_modalias());
243        Ok(vendor_id_source)
244    }
245
246    pub fn get_vendor_id(&self) -> Result<u32, Box<Error>> {
247        let (_,vendor_id,_,_) = try!(self.get_modalias());
248        Ok(vendor_id)
249    }
250
251    pub fn get_product_id(&self) -> Result<u32, Box<Error>> {
252        let (_,_,product_id,_) = try!(self.get_modalias());
253        Ok(product_id)
254    }
255
256    pub fn get_device_id(&self) -> Result<u32, Box<Error>> {
257        let (_,_,_,device_id) = try!(self.get_modalias());
258        Ok(device_id)
259    }
260}