blurmock/
fake_descriptor.rs

1use core::ops::Deref;
2use fake_characteristic::FakeBluetoothGATTCharacteristic;
3use std::error::Error;
4use std::sync::{Arc, Mutex};
5
6#[derive(Clone, Debug)]
7pub struct FakeBluetoothGATTDescriptor {
8    id: Arc<Mutex<String>>,
9    uuid: Arc<Mutex<String>>,
10    characteristic: Arc<FakeBluetoothGATTCharacteristic>,
11    value: Arc<Mutex<Option<Vec<u8>>>>,
12    flags: Arc<Mutex<Vec<String>>>,
13}
14
15impl FakeBluetoothGATTDescriptor {
16    pub fn new(id: String,
17               uuid: String,
18               characteristic: Arc<FakeBluetoothGATTCharacteristic>,
19               value: Option<Vec<u8>>,
20               flags: Vec<String>)
21               -> Arc<FakeBluetoothGATTDescriptor> {
22        if let Ok(existing_descriptor) = characteristic.get_gatt_descriptor(id.clone()) {
23            return existing_descriptor;
24        }
25        let descriptor = Arc::new(FakeBluetoothGATTDescriptor {
26            id: Arc::new(Mutex::new(id)),
27            uuid: Arc::new(Mutex::new(uuid)),
28            characteristic: characteristic.clone(),
29            value: Arc::new(Mutex::new(value)),
30            flags: Arc::new(Mutex::new(flags)),
31        });
32        let _ = characteristic.add_descriptor(descriptor.clone());
33        descriptor
34    }
35
36    pub fn new_empty(characteristic: Arc<FakeBluetoothGATTCharacteristic>,
37                     descriptor_id: String)
38                     -> Arc<FakeBluetoothGATTDescriptor> {
39        FakeBluetoothGATTDescriptor::new(
40            /*id*/ descriptor_id,
41            /*uuid*/ String::new(),
42            /*characteristic*/ characteristic,
43            /*value*/ None,
44            /*flags*/ vec!(),
45        )
46    }
47
48    make_getter!(get_id, id);
49
50    make_setter!(set_id, id);
51
52    make_getter!(get_uuid, uuid, String);
53
54    make_setter!(set_uuid, uuid, String);
55
56    make_option_getter!(get_value, value, Vec<u8>);
57
58    make_setter!(set_value, value, Option<Vec<u8>>);
59
60    make_getter!(get_flags, flags, Vec<String>);
61
62    make_setter!(set_flags, flags, Vec<String>);
63
64    pub fn get_characteristic(&self) -> Result<Arc<FakeBluetoothGATTCharacteristic>, Box<Error>> {
65        Ok(self.characteristic.clone())
66    }
67
68    pub fn read_value(&self) -> Result<Vec<u8>, Box<Error>> {
69        self.get_value()
70    }
71
72    pub fn write_value(&self, value: Vec<u8>) -> Result<(), Box<Error>> {
73        self.set_value(Some(value))
74    }
75}