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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::error::Error;
use std::sync::Arc;

#[cfg(all(target_os = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_adapter::Adapter as BluetoothAdapterAndroid;
#[cfg(all(target_os = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_device::Device as BluetoothDeviceAndroid;
#[cfg(all(target_os = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_discovery_session::DiscoverySession as BluetoothDiscoverySessionAndroid;
#[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothAdapter as BluetoothAdapterMac;
#[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothDevice as BluetoothDeviceMac;
#[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothDiscoverySession as BluetoothDiscoverySessionMac;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_adapter::FakeBluetoothAdapter;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_device::FakeBluetoothDevice;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_discovery_session::FakeBluetoothDiscoverySession;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_adapter::BluetoothAdapter as BluetoothAdapterBluez;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_device::BluetoothDevice as BluetoothDeviceBluez;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_discovery_session::BluetoothDiscoverySession as BluetoothDiscoverySessionBluez;

use super::bluetooth::{BluetoothDevice, BluetoothDiscoverySession};
#[cfg(not(any(
    all(target_os = "linux", feature = "native-bluetooth"),
    all(target_os = "android", feature = "native-bluetooth"),
    all(target_os = "macos", feature = "native-bluetooth")
)))]
use super::empty::BluetoothDevice as BluetoothDeviceEmpty;
#[cfg(not(any(
    all(target_os = "linux", feature = "native-bluetooth"),
    all(target_os = "android", feature = "native-bluetooth"),
    all(target_os = "macos", feature = "native-bluetooth")
)))]
use super::empty::BluetoothDiscoverySession as BluetoothDiscoverySessionEmpty;
#[cfg(not(any(
    all(target_os = "linux", feature = "native-bluetooth"),
    all(target_os = "android", feature = "native-bluetooth"),
    all(target_os = "macos", feature = "native-bluetooth")
)))]
use super::empty::EmptyAdapter as BluetoothAdapterEmpty;
use super::macros::get_inner_and_call;
#[cfg(feature = "bluetooth-test")]
use super::macros::get_inner_and_call_test_func;

#[derive(Clone, Debug)]
pub enum BluetoothAdapter {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothAdapterBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothAdapterAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<BluetoothAdapterMac>),
    #[cfg(not(any(
        all(target_os = "linux", feature = "native-bluetooth"),
        all(target_os = "android", feature = "native-bluetooth"),
        all(target_os = "macos", feature = "native-bluetooth")
    )))]
    Empty(Arc<BluetoothAdapterEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothAdapter>),
}

impl BluetoothAdapter {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    pub fn new() -> Result<BluetoothAdapter, Box<dyn Error>> {
        let bluez_adapter = BluetoothAdapterBluez::init()?;
        Ok(Self::Bluez(Arc::new(bluez_adapter)))
    }

    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    pub fn new() -> Result<BluetoothAdapter, Box<dyn Error>> {
        let blurdroid_adapter = BluetoothAdapterAndroid::get_adapter()?;
        Ok(Self::Android(Arc::new(blurdroid_adapter)))
    }

    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    pub fn new() -> Result<BluetoothAdapter, Box<dyn Error>> {
        let mac_adapter = BluetoothAdapterMac::init()?;
        Ok(Self::Mac(Arc::new(mac_adapter)))
    }

    #[cfg(not(any(
        all(target_os = "linux", feature = "native-bluetooth"),
        all(target_os = "android", feature = "native-bluetooth"),
        all(target_os = "macos", feature = "native-bluetooth")
    )))]
    pub fn new() -> Result<BluetoothAdapter, Box<dyn Error>> {
        let adapter = BluetoothAdapterEmpty::init()?;
        Ok(Self::Empty(Arc::new(adapter)))
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn new_mock() -> Result<BluetoothAdapter, Box<dyn Error>> {
        Ok(Self::Mock(FakeBluetoothAdapter::new_empty()))
    }

    pub fn get_id(&self) -> String {
        get_inner_and_call!(self, BluetoothAdapter, get_id)
    }

    pub fn get_devices(&self) -> Result<Vec<BluetoothDevice>, Box<dyn Error>> {
        match self {
            #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
            BluetoothAdapter::Bluez(inner) => {
                let device_list = inner.get_device_list()?;
                Ok(device_list
                    .into_iter()
                    .map(|device| BluetoothDevice::Bluez(BluetoothDeviceBluez::new(device).into()))
                    .collect())
            },
            #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
            BluetoothAdapter::Android(inner) => {
                let device_list = inner.get_device_list()?;
                Ok(device_list
                    .into_iter()
                    .map(|device| {
                        BluetoothDevice::Android(BluetoothDeviceAndroid::new_empty(
                            self.0.clone(),
                            device,
                        ))
                    })
                    .collect())
            },
            #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
            BluetoothAdapter::Mac(inner) => {
                let device_list = inner.get_device_list()?;
                Ok(device_list
                    .into_iter()
                    .map(|device| {
                        BluetoothDevice::Mac(Arc::new(BluetoothDeviceMac::new(
                            inner.clone(),
                            device,
                        )))
                    })
                    .collect())
            },
            #[cfg(not(any(
                all(target_os = "linux", feature = "native-bluetooth"),
                all(target_os = "android", feature = "native-bluetooth"),
                all(target_os = "macos", feature = "native-bluetooth")
            )))]
            BluetoothAdapter::Empty(inner) => {
                let device_list = inner.get_device_list()?;
                Ok(device_list
                    .into_iter()
                    .map(|device| {
                        BluetoothDevice::Empty(Arc::new(BluetoothDeviceEmpty::new(device)))
                    })
                    .collect())
            },
            #[cfg(feature = "bluetooth-test")]
            BluetoothAdapter::Mock(inner) => {
                let device_list = inner.get_device_list()?;
                Ok(device_list
                    .into_iter()
                    .map(|device| {
                        BluetoothDevice::Mock(FakeBluetoothDevice::new_empty(inner.clone(), device))
                    })
                    .collect())
            },
        }
    }

    pub fn get_device(&self, address: String) -> Result<Option<BluetoothDevice>, Box<dyn Error>> {
        let devices = self.get_devices()?;
        for device in devices {
            if device.get_address()? == address {
                return Ok(Some(device));
            }
        }
        Ok(None)
    }

    pub fn create_mock_device(&self, _device: String) -> Result<BluetoothDevice, Box<dyn Error>> {
        match self {
            #[cfg(feature = "bluetooth-test")]
            BluetoothAdapter::Mock(inner) => Ok(BluetoothDevice::Mock(
                FakeBluetoothDevice::new_empty(inner.clone(), _device),
            )),
            _ => Err(Box::from(
                "Error! Test functions are not supported on real devices!",
            )),
        }
    }

    pub fn create_discovery_session(&self) -> Result<BluetoothDiscoverySession, Box<dyn Error>> {
        let discovery_session = match self {
            #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
            #[allow(clippy::arc_with_non_send_sync)] // Problem with underlying library
            BluetoothAdapter::Bluez(inner) => BluetoothDiscoverySession::Bluez(Arc::new(
                BluetoothDiscoverySessionBluez::create_session(inner.get_id())?,
            )),
            #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
            BluetoothAdapter::Android(inner) => BluetoothDiscoverySession::Android(Arc::new(
                BluetoothDiscoverySessionAndroid::create_session(inner.clone())?,
            )),
            #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
            BluetoothAdapter::Mac(_) => {
                BluetoothDiscoverySession::Mac(Arc::new(BluetoothDiscoverySessionMac {}))
            },
            #[cfg(not(any(
                all(target_os = "linux", feature = "native-bluetooth"),
                all(target_os = "android", feature = "native-bluetooth"),
                all(target_os = "macos", feature = "native-bluetooth")
            )))]
            BluetoothAdapter::Empty(_) => {
                BluetoothDiscoverySession::Empty(Arc::new(BluetoothDiscoverySessionEmpty {}))
            },
            #[cfg(feature = "bluetooth-test")]
            BluetoothAdapter::Mock(inner) => BluetoothDiscoverySession::Mock(Arc::new(
                FakeBluetoothDiscoverySession::create_session(inner.clone())?,
            )),
        };
        Ok(discovery_session)
    }

    pub fn get_address(&self) -> Result<String, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_address)
    }

    pub fn get_name(&self) -> Result<String, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_name)
    }

    pub fn get_alias(&self) -> Result<String, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_alias)
    }

    pub fn get_class(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_class)
    }

    pub fn is_powered(&self) -> Result<bool, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, is_powered)
    }

    pub fn is_discoverable(&self) -> Result<bool, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, is_discoverable)
    }

    pub fn is_pairable(&self) -> Result<bool, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, is_pairable)
    }

    pub fn get_pairable_timeout(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_pairable_timeout)
    }

    pub fn get_discoverable_timeout(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_discoverable_timeout)
    }

    pub fn is_discovering(&self) -> Result<bool, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, is_discovering)
    }

    pub fn get_uuids(&self) -> Result<Vec<String>, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_uuids)
    }

    pub fn get_vendor_id_source(&self) -> Result<String, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_vendor_id_source)
    }

    pub fn get_vendor_id(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_vendor_id)
    }

    pub fn get_product_id(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_product_id)
    }

    pub fn get_device_id(&self) -> Result<u32, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_device_id)
    }

    pub fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothAdapter, get_modalias)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_id(&self, id: String) -> Result<(), Box<dyn Error>> {
        match self {
            #[cfg(feature = "bluetooth-test")]
            BluetoothAdapter::Mock(inner) => {
                inner.set_id(id);
                Ok(())
            },
            _ => Err(Box::from(
                "Error! Test functions are not supported on real devices!",
            )),
        }
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_address(&self, address: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_address, address)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_name(&self, name: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_name, name)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_alias(&self, alias: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_alias, alias)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_class(&self, class: u32) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_class, class)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_powered(&self, powered: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_powered, powered)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn is_present(&self) -> Result<bool, Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, is_present)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_present(&self, present: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_present, present)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_discoverable(&self, discoverable: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_discoverable, discoverable)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_pairable(&self, pairable: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_pairable, pairable)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_pairable_timeout(&self, timeout: u32) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_pairable_timeout, timeout)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_can_start_discovery(&self, can_start_discovery: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(
            self,
            BluetoothAdapter,
            set_can_start_discovery,
            can_start_discovery
        )
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_discoverable_timeout(&self, timeout: u32) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_discoverable_timeout, timeout)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_discovering(&self, discovering: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_discovering, discovering)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_can_stop_discovery(&self, can_stop_discovery: bool) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(
            self,
            BluetoothAdapter,
            set_can_stop_discovery,
            can_stop_discovery
        )
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_uuids(&self, uuids: Vec<String>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_uuids, uuids)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_modalias(&self, modalias: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_modalias, modalias)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn get_ad_datas(&self) -> Result<Vec<String>, Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, get_ad_datas)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_ad_datas(&self, ad_datas: Vec<String>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothAdapter, set_ad_datas, ad_datas)
    }
}