1use core::ops::Deref;
2use fake_characteristic::FakeBluetoothGATTCharacteristic;
3use fake_device::FakeBluetoothDevice;
4use std::error::Error;
5use std::sync::{Arc, Mutex};
6
7#[derive(Clone, Debug)]
8pub struct FakeBluetoothGATTService {
9 id: Arc<Mutex<String>>,
10 device: Arc<FakeBluetoothDevice>,
11 gatt_characteristics: Arc<Mutex<Vec<Arc<FakeBluetoothGATTCharacteristic>>>>,
12 is_primary: Arc<Mutex<bool>>,
13 included_services: Arc<Mutex<Vec<Arc<FakeBluetoothGATTService>>>>,
14 uuid: Arc<Mutex<String>>,
15}
16
17impl FakeBluetoothGATTService {
18 pub fn new(id: String,
19 device: Arc<FakeBluetoothDevice>,
20 gatt_characteristics: Vec<Arc<FakeBluetoothGATTCharacteristic>>,
21 is_primary: bool,
22 included_services: Vec<Arc<FakeBluetoothGATTService>>,
23 uuid: String)
24 -> Arc<FakeBluetoothGATTService> {
25 if let Ok(existing_service) = device.get_gatt_service(id.clone()) {
26 return existing_service;
27 }
28 let service = Arc::new(FakeBluetoothGATTService {
29 id: Arc::new(Mutex::new(id)),
30 device: device.clone(),
31 gatt_characteristics: Arc::new(Mutex::new(gatt_characteristics)),
32 is_primary: Arc::new(Mutex::new(is_primary)),
33 included_services: Arc::new(Mutex::new(included_services)),
34 uuid: Arc::new(Mutex::new(uuid)),
35 });
36 let _ = device.add_service(service.clone());
37 service
38 }
39
40 pub fn new_empty(device: Arc<FakeBluetoothDevice>,
41 service_id: String)
42 -> Arc<FakeBluetoothGATTService> {
43 FakeBluetoothGATTService::new(
44 service_id,
45 device,
46 vec!(),
47 true,
48 vec!(),
49 String::new(),
50 )
51 }
52
53 make_getter!(get_id, id);
54
55 make_setter!(set_id, id);
56
57 make_getter!(get_gatt_characteristic_structs, gatt_characteristics, Vec<Arc<FakeBluetoothGATTCharacteristic>>);
58
59 make_getter!(is_primary);
60
61 make_setter!(set_is_primary, is_primary, bool);
62
63 make_setter!(set_includes, included_services, Vec<Arc<FakeBluetoothGATTService>>);
64
65 make_getter!(get_uuid, uuid, String);
66
67 make_setter!(set_uuid, uuid, String);
68
69 pub fn get_device(&self) -> Result<Arc<FakeBluetoothDevice>, Box<Error>> {
70 Ok(self.device.clone())
71 }
72
73 pub fn get_gatt_characteristics(&self) -> Result<Vec<String>, Box<Error>> {
74 let cloned = self.gatt_characteristics.clone();
75 let gatt_characteristics = match cloned.lock() {
76 Ok(guard) => guard.deref().clone(),
77 Err(_) => return Err(Box::from("Could not get the value.")),
78 };
79 Ok(gatt_characteristics.into_iter().map(|s| s.get_id()).collect())
80 }
81
82 pub fn get_gatt_characteristic(&self, id: String) -> Result<Arc<FakeBluetoothGATTCharacteristic>, Box<Error>> {
83 let characteristics = try!(self.get_gatt_characteristic_structs());
84 for characteristic in characteristics {
85 let characteristic_id = characteristic.get_id();
86 if characteristic_id == id {
87 return Ok(characteristic);
88 }
89 }
90 Err(Box::from("No characteristic exists with the given id."))
91 }
92
93 pub fn add_characteristic(&self, characteristic: Arc<FakeBluetoothGATTCharacteristic>) -> Result<(), Box<Error>> {
94 let cloned = self.gatt_characteristics.clone();
95 let mut gatt_characteristics = match cloned.lock() {
96 Ok(guard) => guard,
97 Err(_) => return Err(Box::from("Could not get the value.")),
98 };
99 Ok(gatt_characteristics.push(characteristic))
100 }
101
102 pub fn remove_characteristic(&self, id: String) -> Result<(), Box<Error>> {
103 let cloned = self.gatt_characteristics.clone();
104 let mut gatt_characteristics = match cloned.lock() {
105 Ok(guard) => guard,
106 Err(_) => return Err(Box::from("Could not get the value.")),
107 };
108 Ok(gatt_characteristics.retain(|c| c.get_id() != id))
109 }
110
111 pub fn add_included_service(&self, service: Arc<FakeBluetoothGATTService>) -> Result<(), Box<Error>> {
112 let cloned = self.included_services.clone();
113 let mut included_services = match cloned.lock() {
114 Ok(guard) => guard,
115 Err(_) => return Err(Box::from("Could not get the value.")),
116 };
117 Ok(included_services.push(service))
118 }
119
120 pub fn remove_included_service(&self, id: String) -> Result<(), Box<Error>> {
121 let cloned = self.included_services.clone();
122 let mut included_services = match cloned.lock() {
123 Ok(guard) => guard,
124 Err(_) => return Err(Box::from("Could not get the value.")),
125 };
126 Ok(included_services.retain(|i| i.get_id() != id))
127 }
128
129 pub fn get_includes(&self) -> Result<Vec<String>, Box<Error>> {
130 let cloned = self.included_services.clone();
131 let included_services = match cloned.lock() {
132 Ok(guard) => guard.deref().clone(),
133 Err(_) => return Err(Box::from("Could not get the value.")),
134 };
135 Ok(included_services.into_iter().map(|s| s.get_id()).collect())
136 }
137
138}