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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* 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::collections::HashMap;
use std::error::Error;
use std::sync::Arc;

#[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 = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_gatt_characteristic::Characteristic as BluetoothGATTCharacteristicAndroid;
#[cfg(all(target_os = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_gatt_descriptor::Descriptor as BluetoothGATTDescriptorAndroid;
#[cfg(all(target_os = "android", feature = "native-bluetooth"))]
use blurdroid::bluetooth_gatt_service::Service as BluetoothGATTServiceAndroid;
#[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(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothGATTCharacteristic as BluetoothGATTCharacteristicMac;
#[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothGATTDescriptor as BluetoothGATTDescriptorMac;
#[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
use blurmac::BluetoothGATTService as BluetoothGATTServiceMac;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_characteristic::FakeBluetoothGATTCharacteristic;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_descriptor::FakeBluetoothGATTDescriptor;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_device::FakeBluetoothDevice;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_discovery_session::FakeBluetoothDiscoverySession;
#[cfg(feature = "bluetooth-test")]
use blurmock::fake_service::FakeBluetoothGATTService;
#[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;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_gatt_characteristic::BluetoothGATTCharacteristic as BluetoothGATTCharacteristicBluez;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_gatt_descriptor::BluetoothGATTDescriptor as BluetoothGATTDescriptorBluez;
#[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
use blurz::bluetooth_gatt_service::BluetoothGATTService as BluetoothGATTServiceBluez;

pub use super::adapter::BluetoothAdapter;
#[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::BluetoothGATTCharacteristic as BluetoothGATTCharacteristicEmpty;
#[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::BluetoothGATTDescriptor as BluetoothGATTDescriptorEmpty;
#[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::BluetoothGATTService as BluetoothGATTServiceEmpty;
use super::macros::get_inner_and_call;
#[cfg(feature = "bluetooth-test")]
use super::macros::get_inner_and_call_test_func;

#[cfg(feature = "bluetooth-test")]
const NOT_SUPPORTED_ON_MOCK_ERROR: &str = "Error! The first parameter must be a mock structure!";

#[derive(Debug)]
pub enum BluetoothDiscoverySession {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothDiscoverySessionBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothDiscoverySessionAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<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")
    )))]
    Empty(Arc<BluetoothDiscoverySessionEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothDiscoverySession>),
}

#[derive(Clone, Debug)]
pub enum BluetoothDevice {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothDeviceBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothDeviceAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<BluetoothDeviceMac>),
    #[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<BluetoothDeviceEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothDevice>),
}

#[derive(Clone, Debug)]
pub enum BluetoothGATTService {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothGATTServiceBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothGATTServiceAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<BluetoothGATTServiceMac>),
    #[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<BluetoothGATTServiceEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothGATTService>),
}

#[derive(Clone, Debug)]
pub enum BluetoothGATTCharacteristic {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothGATTCharacteristicBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothGATTCharacteristicAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<BluetoothGATTCharacteristicMac>),
    #[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<BluetoothGATTCharacteristicEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothGATTCharacteristic>),
}

#[derive(Clone, Debug)]
pub enum BluetoothGATTDescriptor {
    #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
    Bluez(Arc<BluetoothGATTDescriptorBluez>),
    #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
    Android(Arc<BluetoothGATTDescriptorAndroid>),
    #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
    Mac(Arc<BluetoothGATTDescriptorMac>),
    #[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<BluetoothGATTDescriptorEmpty>),
    #[cfg(feature = "bluetooth-test")]
    Mock(Arc<FakeBluetoothGATTDescriptor>),
}

impl BluetoothDiscoverySession {
    pub fn start_discovery(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDiscoverySession, start_discovery)
    }

    pub fn stop_discovery(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDiscoverySession, stop_discovery)
    }
}

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

    #[cfg(feature = "bluetooth-test")]
    pub fn set_id(&self, id: String) {
        if let BluetoothDevice::Mock(fake_adapter) = self {
            fake_adapter.set_id(id)
        }
    }

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

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

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

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

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

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

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

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

    pub fn get_appearance(&self) -> Result<u16, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, get_appearance)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_appearance(&self, appearance: u16) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothDevice, set_appearance, Some(appearance))
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    pub fn get_rssi(&self) -> Result<i16, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, get_rssi)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_rssi(&self, rssi: i16) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothDevice, set_rssi, Some(rssi))
    }

    pub fn get_tx_power(&self) -> Result<i16, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, get_tx_power)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_tx_power(&self, tx_power: i16) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothDevice, set_tx_power, Some(tx_power))
    }

    pub fn get_manufacturer_data(&self) -> Result<HashMap<u16, Vec<u8>>, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, get_manufacturer_data)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_manufacturer_data(
        &self,
        manufacturer_data: HashMap<u16, Vec<u8>>,
    ) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(
            self,
            BluetoothDevice,
            set_manufacturer_data,
            Some(manufacturer_data)
        )
    }

    pub fn get_service_data(&self) -> Result<HashMap<String, Vec<u8>>, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, get_service_data)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_service_data(
        &self,
        service_data: HashMap<String, Vec<u8>>,
    ) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothDevice, set_service_data, Some(service_data))
    }

    pub fn get_gatt_services(&self) -> Result<Vec<BluetoothGATTService>, Box<dyn Error>> {
        let services = get_inner_and_call!(self, BluetoothDevice, get_gatt_services)?;
        Ok(services
            .into_iter()
            .map(|service| BluetoothGATTService::create_service(self.clone(), service))
            .collect())
    }

    pub fn connect(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, connect)
    }

    pub fn disconnect(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, disconnect)
    }

    pub fn connect_profile(&self, uuid: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, connect_profile, uuid)
    }

    pub fn disconnect_profile(&self, uuid: String) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, disconnect_profile, uuid)
    }

    pub fn pair(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, pair)
    }

    pub fn cancel_pairing(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothDevice, cancel_pairing)
    }
}

impl BluetoothGATTService {
    fn create_service(device: BluetoothDevice, service: String) -> BluetoothGATTService {
        match device {
            #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
            BluetoothDevice::Bluez(_bluez_device) => {
                BluetoothGATTService::Bluez(Arc::new(BluetoothGATTServiceBluez::new(service)))
            },
            #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
            BluetoothDevice::Android(android_device) => BluetoothGATTService::Android(Arc::new(
                BluetoothGATTServiceAndroid::new(android_device, service),
            )),
            #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
            BluetoothDevice::Mac(mac_device) => BluetoothGATTService::Mac(Arc::new(
                BluetoothGATTServiceMac::new(mac_device, service),
            )),
            #[cfg(not(any(
                all(target_os = "linux", feature = "native-bluetooth"),
                all(target_os = "android", feature = "native-bluetooth"),
                all(target_os = "macos", feature = "native-bluetooth")
            )))]
            BluetoothDevice::Empty(_device) => {
                BluetoothGATTService::Empty(Arc::new(BluetoothGATTServiceEmpty::new(service)))
            },
            #[cfg(feature = "bluetooth-test")]
            BluetoothDevice::Mock(fake_device) => BluetoothGATTService::Mock(
                FakeBluetoothGATTService::new_empty(fake_device, service),
            ),
        }
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn create_mock_service(
        device: BluetoothDevice,
        service: String,
    ) -> Result<BluetoothGATTService, Box<dyn Error>> {
        match device {
            BluetoothDevice::Mock(fake_device) => Ok(BluetoothGATTService::Mock(
                FakeBluetoothGATTService::new_empty(fake_device, service),
            )),
            _ => Err(Box::from(
                "Error! The first parameter must be a mock structure!",
            )),
        }
    }

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

    #[cfg(feature = "bluetooth-test")]
    pub fn set_id(&self, id: String) {
        if let BluetoothGATTService::Mock(fake_service) = self {
            fake_service.set_id(id)
        }
    }

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

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

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

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

    pub fn get_includes(
        &self,
        device: BluetoothDevice,
    ) -> Result<Vec<BluetoothGATTService>, Box<dyn Error>> {
        let services = get_inner_and_call!(self, BluetoothGATTService, get_includes)?;
        Ok(services
            .into_iter()
            .map(|service| BluetoothGATTService::create_service(device.clone(), service))
            .collect())
    }

    pub fn get_gatt_characteristics(
        &self,
    ) -> Result<Vec<BluetoothGATTCharacteristic>, Box<dyn Error>> {
        let characteristics =
            get_inner_and_call!(self, BluetoothGATTService, get_gatt_characteristics)?;
        Ok(characteristics
            .into_iter()
            .map(|characteristic| {
                BluetoothGATTCharacteristic::create_characteristic(self.clone(), characteristic)
            })
            .collect())
    }
}

impl BluetoothGATTCharacteristic {
    fn create_characteristic(
        service: BluetoothGATTService,
        characteristic: String,
    ) -> BluetoothGATTCharacteristic {
        match service {
            #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
            BluetoothGATTService::Bluez(_bluez_service) => BluetoothGATTCharacteristic::Bluez(
                Arc::new(BluetoothGATTCharacteristicBluez::new(characteristic)),
            ),
            #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
            BluetoothGATTService::Android(android_service) => {
                BluetoothGATTCharacteristic::Android(Arc::new(
                    BluetoothGATTCharacteristicAndroid::new(android_service, characteristic),
                ))
            },
            #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
            BluetoothGATTService::Mac(mac_service) => BluetoothGATTCharacteristic::Mac(Arc::new(
                BluetoothGATTCharacteristicMac::new(mac_service, characteristic),
            )),
            #[cfg(not(any(
                all(target_os = "linux", feature = "native-bluetooth"),
                all(target_os = "android", feature = "native-bluetooth"),
                all(target_os = "macos", feature = "native-bluetooth")
            )))]
            BluetoothGATTService::Empty(_service) => BluetoothGATTCharacteristic::Empty(Arc::new(
                BluetoothGATTCharacteristicEmpty::new(characteristic),
            )),
            #[cfg(feature = "bluetooth-test")]
            BluetoothGATTService::Mock(fake_service) => BluetoothGATTCharacteristic::Mock(
                FakeBluetoothGATTCharacteristic::new_empty(fake_service, characteristic),
            ),
        }
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn create_mock_characteristic(
        service: BluetoothGATTService,
        characteristic: String,
    ) -> Result<BluetoothGATTCharacteristic, Box<dyn Error>> {
        match service {
            BluetoothGATTService::Mock(fake_service) => Ok(BluetoothGATTCharacteristic::Mock(
                FakeBluetoothGATTCharacteristic::new_empty(fake_service, characteristic),
            )),
            _ => Err(Box::from(
                "Error! The first parameter must be a mock structure!",
            )),
        }
    }

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

    #[cfg(feature = "bluetooth-test")]
    pub fn set_id(&self, id: String) {
        if let BluetoothGATTCharacteristic::Mock(fake_characteristic) = self {
            fake_characteristic.set_id(id)
        }
    }

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

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

    pub fn get_value(&self) -> Result<Vec<u8>, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothGATTCharacteristic, get_value)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_value(&self, value: Vec<u8>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothGATTCharacteristic, set_value, Some(value))
    }

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

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

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

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

    pub fn get_gatt_descriptors(&self) -> Result<Vec<BluetoothGATTDescriptor>, Box<dyn Error>> {
        let descriptors =
            get_inner_and_call!(self, BluetoothGATTCharacteristic, get_gatt_descriptors)?;
        Ok(descriptors
            .into_iter()
            .map(|descriptor| BluetoothGATTDescriptor::create_descriptor(self.clone(), descriptor))
            .collect())
    }

    pub fn read_value(&self) -> Result<Vec<u8>, Box<dyn Error>> {
        get_inner_and_call!(@with_bluez_offset, self, BluetoothGATTCharacteristic, read_value)
    }

    pub fn write_value(&self, values: Vec<u8>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(@with_bluez_offset, self, BluetoothGATTCharacteristic, write_value, values)
    }

    pub fn start_notify(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothGATTCharacteristic, start_notify)
    }

    pub fn stop_notify(&self) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothGATTCharacteristic, stop_notify)
    }
}

impl BluetoothGATTDescriptor {
    fn create_descriptor(
        characteristic: BluetoothGATTCharacteristic,
        descriptor: String,
    ) -> BluetoothGATTDescriptor {
        match characteristic {
            #[cfg(all(target_os = "linux", feature = "native-bluetooth"))]
            BluetoothGATTCharacteristic::Bluez(_bluez_characteristic) => {
                BluetoothGATTDescriptor::Bluez(Arc::new(BluetoothGATTDescriptorBluez::new(
                    descriptor,
                )))
            },
            #[cfg(all(target_os = "android", feature = "native-bluetooth"))]
            BluetoothGATTCharacteristic::Android(android_characteristic) => {
                BluetoothGATTDescriptor::Android(Arc::new(BluetoothGATTDescriptorAndroid::new(
                    android_characteristic,
                    descriptor,
                )))
            },
            #[cfg(all(target_os = "macos", feature = "native-bluetooth"))]
            BluetoothGATTCharacteristic::Mac(_mac_characteristic) => {
                BluetoothGATTDescriptor::Mac(Arc::new(BluetoothGATTDescriptorMac::new(descriptor)))
            },
            #[cfg(not(any(
                all(target_os = "linux", feature = "native-bluetooth"),
                all(target_os = "android", feature = "native-bluetooth"),
                all(target_os = "macos", feature = "native-bluetooth")
            )))]
            BluetoothGATTCharacteristic::Empty(_characteristic) => BluetoothGATTDescriptor::Empty(
                Arc::new(BluetoothGATTDescriptorEmpty::new(descriptor)),
            ),
            #[cfg(feature = "bluetooth-test")]
            BluetoothGATTCharacteristic::Mock(fake_characteristic) => {
                BluetoothGATTDescriptor::Mock(FakeBluetoothGATTDescriptor::new_empty(
                    fake_characteristic,
                    descriptor,
                ))
            },
        }
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn create_mock_descriptor(
        characteristic: BluetoothGATTCharacteristic,
        descriptor: String,
    ) -> Result<BluetoothGATTDescriptor, Box<dyn Error>> {
        match characteristic {
            BluetoothGATTCharacteristic::Mock(fake_characteristic) => {
                Ok(BluetoothGATTDescriptor::Mock(
                    FakeBluetoothGATTDescriptor::new_empty(fake_characteristic, descriptor),
                ))
            },
            _ => Err(Box::from(NOT_SUPPORTED_ON_MOCK_ERROR)),
        }
    }

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

    #[cfg(feature = "bluetooth-test")]
    pub fn set_id(&self, id: String) {
        if let BluetoothGATTDescriptor::Mock(fake_descriptor) = self {
            fake_descriptor.set_id(id)
        }
    }

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

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

    pub fn get_value(&self) -> Result<Vec<u8>, Box<dyn Error>> {
        get_inner_and_call!(self, BluetoothGATTDescriptor, get_value)
    }

    #[cfg(feature = "bluetooth-test")]
    pub fn set_value(&self, value: Vec<u8>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call_test_func!(self, BluetoothGATTDescriptor, set_value, Some(value))
    }

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

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

    pub fn read_value(&self) -> Result<Vec<u8>, Box<dyn Error>> {
        get_inner_and_call!(@with_bluez_offset, self, BluetoothGATTDescriptor, read_value)
    }

    pub fn write_value(&self, values: Vec<u8>) -> Result<(), Box<dyn Error>> {
        get_inner_and_call!(@with_bluez_offset, self, BluetoothGATTDescriptor, write_value, values)
    }
}