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
extern crate hex;
extern crate core;

macro_rules! make_getter(
    ($function_name: ident, $attr: ident, $ret_type:ty) => {
        pub fn $function_name(&self) -> Result<$ret_type, Box<Error>> {
            let cloned = self.$attr.clone();
            let attr_value = match cloned.lock() {
                Ok(guard) => guard.deref().clone(),
                Err(_) => return Err(Box::from("Could not get the value.")),
            };
            Ok(attr_value)
        }
    };

    ($function_name: ident, $attr: ident) => {
        pub fn $function_name(&self) -> String {
            let cloned = self.$attr.clone();
            let attr_value = match cloned.lock() {
                Ok(guard) => guard.deref().clone(),
                Err(_) => String::new(),
            };
            attr_value
        }
    };

    ($attr_name: ident) => {
        pub fn $attr_name(&self) -> Result<bool, Box<Error>> {
            let cloned = self.$attr_name.clone();
            let attr_value = match cloned.lock() {
                Ok(guard) => guard.deref().clone(),
                Err(_) => return Err(Box::from("Could not get the value.")),
            };
            Ok(attr_value)
        }
    };
);

macro_rules! make_option_getter(
    ($function_name: ident, $attr: ident, $ret_type:ty) => {
        pub fn $function_name(&self) -> Result<$ret_type, Box<Error>> {
            let cloned = self.$attr.clone();
            let attr_value = match cloned.lock() {
                Ok(guard) => guard.deref().clone(),
                Err(_) => return Err(Box::from("Could not get the value.")),
            };
            match attr_value {
                Some(value) => return Ok(value),
                None => return Err(Box::from("Could not get the value.")),
            }
        }
    };
);

macro_rules! make_setter(
    ($function_name: ident, $attr: ident, $attr_type:ty ) => {
        pub fn $function_name(&self, value: $attr_type) -> Result<(), Box<Error>> {
            let cloned = self.$attr.clone();
            let mut value_to_change = match cloned.lock() {
                Ok(guard) => guard,
                Err(_) => return Err(Box::from("Could not get the value.")),
            };
            Ok(*value_to_change = value)
        }
    };

    ($function_name: ident, $attr: ident) => {
        pub fn $function_name(&self, value: String) {
            let cloned = self.$attr.clone();
            let mut value_to_change = match cloned.lock() {
                Ok(guard) => guard,
                Err(_) => return (),
            };
            *value_to_change = value
        }
    };
);

pub mod fake_adapter;
pub mod fake_device;
pub mod fake_service;
pub mod fake_characteristic;
pub mod fake_descriptor;
pub mod fake_discovery_session;