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
use crate::api::{BDAddr, CentralEvent, Peripheral};
use dashmap::DashMap;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};
#[derive(Clone, Debug)]
pub struct AdapterManager<PeripheralType>
where
PeripheralType: Peripheral,
{
peripherals: Arc<DashMap<BDAddr, PeripheralType>>,
event_sender: Arc<Mutex<Sender<CentralEvent>>>,
event_receiver: Arc<Mutex<Option<Receiver<CentralEvent>>>>,
}
impl<PeripheralType> AdapterManager<PeripheralType>
where
PeripheralType: Peripheral + 'static,
{
pub fn new() -> Self {
let peripherals = Arc::new(DashMap::new());
let (event_sender, event_receiver) = channel();
AdapterManager {
peripherals,
event_sender: Arc::new(Mutex::new(event_sender)),
event_receiver: Arc::new(Mutex::new(Some(event_receiver))),
}
}
pub fn emit(&self, event: CentralEvent) {
match event {
CentralEvent::DeviceDisconnected(addr) => {
self.peripherals.remove(&addr);
}
CentralEvent::DeviceLost(addr) => {
self.peripherals.remove(&addr);
}
_ => {}
}
self.event_sender.lock().unwrap().send(event).unwrap();
}
pub fn event_receiver(&self) -> Option<Receiver<CentralEvent>> {
self.event_receiver.lock().unwrap().take()
}
pub fn has_peripheral(&self, addr: &BDAddr) -> bool {
self.peripherals.contains_key(addr)
}
pub fn add_peripheral(&self, addr: BDAddr, peripheral: PeripheralType) {
assert!(
!self.peripherals.contains_key(&addr),
"Adding a peripheral that's already in the map."
);
assert_eq!(peripheral.address(), addr, "Device has unexpected address.");
self.peripherals.insert(addr, peripheral);
}
pub fn update_peripheral(&self, addr: BDAddr, peripheral: PeripheralType) {
assert!(
self.peripherals.contains_key(&addr),
"Updating a peripheral that's not in the map."
);
assert_eq!(peripheral.address(), addr, "Device has unexpected address.");
let current_peripheral = self.peripherals.get_mut(&addr).unwrap();
if peripheral.properties().local_name.is_some()
&& current_peripheral.properties().local_name.is_none()
{
current_peripheral.properties().local_name = peripheral.properties().local_name;
}
if peripheral.properties().manufacturer_data.is_some()
&& current_peripheral.properties().manufacturer_data.is_none()
{
current_peripheral.properties().manufacturer_data =
peripheral.properties().manufacturer_data;
}
current_peripheral.properties().tx_power_level = peripheral.properties().tx_power_level;
}
pub fn peripherals(&self) -> Vec<PeripheralType> {
self.peripherals
.iter()
.map(|val| val.value().clone())
.collect()
}
pub fn peripheral(&self, address: BDAddr) -> Option<PeripheralType> {
self.peripherals
.get(&address)
.and_then(|val| Some(val.value().clone()))
}
}