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
use core::ops::Deref;
use fake_characteristic::FakeBluetoothGATTCharacteristic;
use std::error::Error;
use std::sync::{Arc, Mutex};

#[derive(Clone, Debug)]
pub struct FakeBluetoothGATTDescriptor {
    id: Arc<Mutex<String>>,
    uuid: Arc<Mutex<String>>,
    characteristic: Arc<FakeBluetoothGATTCharacteristic>,
    value: Arc<Mutex<Option<Vec<u8>>>>,
    flags: Arc<Mutex<Vec<String>>>,
}

impl FakeBluetoothGATTDescriptor {
    pub fn new(id: String,
               uuid: String,
               characteristic: Arc<FakeBluetoothGATTCharacteristic>,
               value: Option<Vec<u8>>,
               flags: Vec<String>)
               -> Arc<FakeBluetoothGATTDescriptor> {
        if let Ok(existing_descriptor) = characteristic.get_gatt_descriptor(id.clone()) {
            return existing_descriptor;
        }
        let descriptor = Arc::new(FakeBluetoothGATTDescriptor {
            id: Arc::new(Mutex::new(id)),
            uuid: Arc::new(Mutex::new(uuid)),
            characteristic: characteristic.clone(),
            value: Arc::new(Mutex::new(value)),
            flags: Arc::new(Mutex::new(flags)),
        });
        let _ = characteristic.add_descriptor(descriptor.clone());
        descriptor
    }

    pub fn new_empty(characteristic: Arc<FakeBluetoothGATTCharacteristic>,
                     descriptor_id: String)
                     -> Arc<FakeBluetoothGATTDescriptor> {
        FakeBluetoothGATTDescriptor::new(
            /*id*/ descriptor_id,
            /*uuid*/ String::new(),
            /*characteristic*/ characteristic,
            /*value*/ None,
            /*flags*/ vec!(),
        )
    }

    make_getter!(get_id, id);

    make_setter!(set_id, id);

    make_getter!(get_uuid, uuid, String);

    make_setter!(set_uuid, uuid, String);

    make_option_getter!(get_value, value, Vec<u8>);

    make_setter!(set_value, value, Option<Vec<u8>>);

    make_getter!(get_flags, flags, Vec<String>);

    make_setter!(set_flags, flags, Vec<String>);

    pub fn get_characteristic(&self) -> Result<Arc<FakeBluetoothGATTCharacteristic>, Box<Error>> {
        Ok(self.characteristic.clone())
    }

    pub fn read_value(&self) -> Result<Vec<u8>, Box<Error>> {
        self.get_value()
    }

    pub fn write_value(&self, value: Vec<u8>) -> Result<(), Box<Error>> {
        self.set_value(Some(value))
    }
}