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

#[derive(Clone, Debug)]
pub struct FakeBluetoothGATTService {
    id: Arc<Mutex<String>>,
    device: Arc<FakeBluetoothDevice>,
    gatt_characteristics: Arc<Mutex<Vec<Arc<FakeBluetoothGATTCharacteristic>>>>,
    is_primary: Arc<Mutex<bool>>,
    included_services: Arc<Mutex<Vec<Arc<FakeBluetoothGATTService>>>>,
    uuid: Arc<Mutex<String>>,
}

impl FakeBluetoothGATTService {
    pub fn new(id: String,
               device: Arc<FakeBluetoothDevice>,
               gatt_characteristics: Vec<Arc<FakeBluetoothGATTCharacteristic>>,
               is_primary: bool,
               included_services: Vec<Arc<FakeBluetoothGATTService>>,
               uuid: String)
               -> Arc<FakeBluetoothGATTService> {
        if let Ok(existing_service) = device.get_gatt_service(id.clone()) {
            return existing_service;
        }
        let service = Arc::new(FakeBluetoothGATTService {
            id: Arc::new(Mutex::new(id)),
            device: device.clone(),
            gatt_characteristics: Arc::new(Mutex::new(gatt_characteristics)),
            is_primary: Arc::new(Mutex::new(is_primary)),
            included_services: Arc::new(Mutex::new(included_services)),
            uuid: Arc::new(Mutex::new(uuid)),
        });
        let _ = device.add_service(service.clone());
        service
    }

    pub fn new_empty(device: Arc<FakeBluetoothDevice>,
                     service_id: String)
                     -> Arc<FakeBluetoothGATTService> {
        FakeBluetoothGATTService::new(
            /*id*/ service_id,
            /*device*/ device,
            /*gatt_characteristics*/ vec!(),
            /*is_primary*/ true,
            /*included_services*/ vec!(),
            /*uuid*/ String::new(),
        )
    }

    make_getter!(get_id, id);

    make_setter!(set_id, id);

    make_getter!(get_gatt_characteristic_structs, gatt_characteristics, Vec<Arc<FakeBluetoothGATTCharacteristic>>);

    make_getter!(is_primary);

    make_setter!(set_is_primary, is_primary, bool);

    make_setter!(set_includes, included_services, Vec<Arc<FakeBluetoothGATTService>>);

    make_getter!(get_uuid, uuid, String);

    make_setter!(set_uuid, uuid, String);

    pub fn get_device(&self) -> Result<Arc<FakeBluetoothDevice>, Box<Error>> {
        Ok(self.device.clone())
    }

    pub fn get_gatt_characteristics(&self) -> Result<Vec<String>, Box<Error>> {
        let cloned = self.gatt_characteristics.clone();
        let gatt_characteristics = match cloned.lock() {
            Ok(guard) => guard.deref().clone(),
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(gatt_characteristics.into_iter().map(|s| s.get_id()).collect())
    }

    pub fn get_gatt_characteristic(&self, id: String) -> Result<Arc<FakeBluetoothGATTCharacteristic>, Box<Error>> {
        let characteristics = try!(self.get_gatt_characteristic_structs());
        for characteristic in characteristics {
            let characteristic_id = characteristic.get_id();
            if characteristic_id == id {
                return Ok(characteristic);
            }
        }
        Err(Box::from("No characteristic exists with the given id."))
    }

    pub fn add_characteristic(&self, characteristic: Arc<FakeBluetoothGATTCharacteristic>) -> Result<(), Box<Error>> {
        let cloned = self.gatt_characteristics.clone();
        let mut gatt_characteristics = match cloned.lock() {
            Ok(guard) => guard,
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(gatt_characteristics.push(characteristic))
    }

    pub fn remove_characteristic(&self, id: String) -> Result<(), Box<Error>> {
        let cloned = self.gatt_characteristics.clone();
        let mut gatt_characteristics = match cloned.lock() {
            Ok(guard) => guard,
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(gatt_characteristics.retain(|c| c.get_id() != id))
    }

    pub fn add_included_service(&self, service: Arc<FakeBluetoothGATTService>) -> Result<(), Box<Error>> {
        let cloned = self.included_services.clone();
        let mut included_services = match cloned.lock() {
            Ok(guard) => guard,
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(included_services.push(service))
    }

    pub fn remove_included_service(&self, id: String) -> Result<(), Box<Error>> {
        let cloned = self.included_services.clone();
        let mut included_services = match cloned.lock() {
            Ok(guard) => guard,
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(included_services.retain(|i| i.get_id() != id))
    }

    pub fn get_includes(&self) -> Result<Vec<String>, Box<Error>> {
        let cloned = self.included_services.clone();
        let included_services = match cloned.lock() {
            Ok(guard) => guard.deref().clone(),
            Err(_) => return Err(Box::from("Could not get the value.")),
        };
        Ok(included_services.into_iter().map(|s| s.get_id()).collect())
    }

}