blurmock/
fake_characteristic.rs1use core::ops::Deref;
2use fake_descriptor::FakeBluetoothGATTDescriptor;
3use fake_service::FakeBluetoothGATTService;
4use std::error::Error;
5use std::sync::{Arc, Mutex};
6
7#[derive(Clone, Debug)]
8pub struct FakeBluetoothGATTCharacteristic {
9 id: Arc<Mutex<String>>,
10 uuid: Arc<Mutex<String>>,
11 service: Arc<FakeBluetoothGATTService>,
12 value: Arc<Mutex<Option<Vec<u8>>>>,
13 is_notifying: Arc<Mutex<bool>>,
14 flags: Arc<Mutex<Vec<String>>>,
15 gatt_descriptors: Arc<Mutex<Vec<Arc<FakeBluetoothGATTDescriptor>>>>,
16}
17
18impl FakeBluetoothGATTCharacteristic {
19 pub fn new(id: String,
20 uuid: String,
21 service: Arc<FakeBluetoothGATTService>,
22 value: Option<Vec<u8>>,
23 is_notifying: bool,
24 flags: Vec<String>,
25 gatt_descriptors: Vec<Arc<FakeBluetoothGATTDescriptor>>)
26 -> Arc<FakeBluetoothGATTCharacteristic> {
27 if let Ok(existing_characteristic) = service.get_gatt_characteristic(id.clone()) {
28 return existing_characteristic;
29 }
30 let characteristic = Arc::new(FakeBluetoothGATTCharacteristic {
31 id: Arc::new(Mutex::new(id)),
32 uuid: Arc::new(Mutex::new(uuid)),
33 service: service.clone(),
34 value: Arc::new(Mutex::new(value)),
35 is_notifying: Arc::new(Mutex::new(is_notifying)),
36 flags: Arc::new(Mutex::new(flags)),
37 gatt_descriptors: Arc::new(Mutex::new(gatt_descriptors)),
38 });
39 let _ = service.add_characteristic(characteristic.clone());
40 characteristic
41 }
42
43 pub fn new_empty(service: Arc<FakeBluetoothGATTService>,
44 characteristic_id: String)
45 -> Arc<FakeBluetoothGATTCharacteristic> {
46 FakeBluetoothGATTCharacteristic::new(
47 characteristic_id,
48 String::new(),
49 service,
50 None,
51 false,
52 vec!(),
53 vec!(),
54 )
55 }
56
57 make_getter!(get_id, id);
58
59 make_setter!(set_id, id);
60
61 make_getter!(get_uuid, uuid, String);
62
63 make_setter!(set_uuid, uuid, String);
64
65 make_option_getter!(get_value, value, Vec<u8>);
66
67 make_setter!(set_value, value, Option<Vec<u8>>);
68
69 make_getter!(is_notifying);
70
71 make_setter!(set_notifying, is_notifying, bool);
72
73 make_getter!(get_flags, flags, Vec<String>);
74
75 make_setter!(set_flags, flags, Vec<String>);
76
77 make_getter!(get_gatt_descriptor_structs, gatt_descriptors, Vec<Arc<FakeBluetoothGATTDescriptor>>);
78
79 pub fn get_service(&self) -> Result<Arc<FakeBluetoothGATTService>, Box<Error>> {
80 Ok(self.service.clone())
81 }
82
83 pub fn start_notify(&self) -> Result<(), Box<Error>> {
84 self.set_notifying(true)
85 }
86
87 pub fn stop_notify(&self) -> Result<(), Box<Error>> {
88 self.set_notifying(false)
89 }
90
91 pub fn get_gatt_descriptors(&self) -> Result<Vec<String>, Box<Error>> {
92 let cloned = self.gatt_descriptors.clone();
93 let gatt_descriptors = match cloned.lock() {
94 Ok(guard) => guard.deref().clone(),
95 Err(_) => return Err(Box::from("Could not get the value.")),
96 };
97 Ok(gatt_descriptors.into_iter().map(|s| s.get_id()).collect())
98 }
99
100 pub fn get_gatt_descriptor(&self, id: String) -> Result<Arc<FakeBluetoothGATTDescriptor>, Box<Error>> {
101 let descriptors = try!(self.get_gatt_descriptor_structs());
102 for descriptor in descriptors {
103 let descriptor_id = descriptor.get_id();
104 if descriptor_id == id {
105 return Ok(descriptor);
106 }
107 }
108 Err(Box::from("No descriptor exists with the given id."))
109 }
110
111 pub fn add_descriptor(&self, descriptor: Arc<FakeBluetoothGATTDescriptor>) -> Result<(), Box<Error>> {
112 let cloned = self.gatt_descriptors.clone();
113 let mut gatt_descriptors = match cloned.lock() {
114 Ok(guard) => guard,
115 Err(_) => return Err(Box::from("Could not get the value.")),
116 };
117 Ok(gatt_descriptors.push(descriptor))
118 }
119
120 pub fn remove_descriptor(&self, id: String) -> Result<(), Box<Error>> {
121 let cloned = self.gatt_descriptors.clone();
122 let mut gatt_descriptors = match cloned.lock() {
123 Ok(guard) => guard,
124 Err(_) => return Err(Box::from("Could not get the value.")),
125 };
126 Ok(gatt_descriptors.retain(|d| d.get_id() != id))
127 }
128
129 pub fn read_value(&self) -> Result<Vec<u8>, Box<Error>> {
130 self.get_value()
131 }
132
133 pub fn write_value(&self, value: Vec<u8>) -> Result<(), Box<Error>> {
134 self.set_value(Some(value))
135 }
136}