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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// btleplug Source Code File
//
// Copyright 2020 Nonpolynomial Labs LLC. All rights reserved.
//
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root
// for full license information.
//
// Some portions of this file are taken and/or modified from Rumble
// (https://github.com/mwylde/rumble), using a dual MIT/Apache License under the
// following copyright:
//
// Copyright (c) 2014 The Rust Project Developers
use super::{ble::characteristic::BLECharacteristic, ble::device::BLEDevice, utils};
use crate::{
api::{
AdapterManager, AddressType, BDAddr, CentralEvent, Characteristic, CommandCallback,
NotificationHandler, Peripheral as ApiPeripheral, PeripheralProperties, RequestCallback,
ValueNotification, UUID,
},
common::util,
Error, Result,
};
use dashmap::DashMap;
use std::{
collections::BTreeSet,
fmt::{self, Debug, Display, Formatter},
sync::atomic::{AtomicBool, Ordering},
sync::{Arc, Mutex},
};
use winrt::windows::{devices::bluetooth::advertisement::*, storage::streams::DataReader};
#[derive(Clone)]
pub struct Peripheral {
device: Arc<Mutex<Option<BLEDevice>>>,
adapter: AdapterManager<Self>,
address: BDAddr,
properties: Arc<Mutex<PeripheralProperties>>,
characteristics: Arc<Mutex<BTreeSet<Characteristic>>>,
connected: Arc<AtomicBool>,
ble_characteristics: Arc<DashMap<UUID, BLECharacteristic>>,
notification_handlers: Arc<Mutex<Vec<NotificationHandler>>>,
}
impl Peripheral {
pub fn new(adapter: AdapterManager<Self>, address: BDAddr) -> Self {
let device = Arc::new(Mutex::new(None));
let mut properties = PeripheralProperties::default();
properties.address = address;
let properties = Arc::new(Mutex::new(properties));
let characteristics = Arc::new(Mutex::new(BTreeSet::new()));
let connected = Arc::new(AtomicBool::new(false));
let ble_characteristics = Arc::new(DashMap::new());
let notification_handlers = Arc::new(Mutex::new(Vec::new()));
Peripheral {
device,
adapter,
address,
properties,
characteristics,
connected,
ble_characteristics,
notification_handlers,
}
}
pub fn update_properties(&self, args: &BluetoothLEAdvertisementReceivedEventArgs) {
let mut properties = self.properties.lock().unwrap();
let advertisement = args.get_advertisement().unwrap().unwrap();
properties.discovery_count += 1;
// Advertisements are cumulative: set/replace data only if it's set
if let Ok(name) = advertisement.get_local_name() {
if !name.is_empty() {
properties.local_name = Some(name.to_string());
}
}
if let Ok(Some(manufacturer_data)) = advertisement.get_manufacturer_data() {
let mut data = Vec::new();
for i in &manufacturer_data {
let d = i.unwrap();
let company_id = d.get_company_id().unwrap();
let buffer = d.get_data().unwrap().unwrap();
let reader = DataReader::from_buffer(&buffer).unwrap().unwrap();
let len = reader.get_unconsumed_buffer_length().unwrap() as usize;
let mut input = vec![0u8; len + 2];
reader.read_bytes(&mut input[2..(len + 2)]).unwrap();
input[0] = company_id as u8;
input[1] = (company_id >> 8) as u8;
data.append(&mut input);
}
properties.manufacturer_data = Some(data)
}
// windows does not provide the address type in the advertisement event args but only in the device object
// https://social.msdn.microsoft.com/Forums/en-US/c71d51a2-56a1-425a-9063-de44fda48766/bluetooth-address-public-or-random?forum=wdk
properties.address_type = AddressType::default();
properties.has_scan_response =
args.get_advertisement_type().unwrap() == BluetoothLEAdvertisementType::ScanResponse;
properties.tx_power_level = args
.get_raw_signal_strength_in_dbm()
.ok()
.map(|rssi| rssi as i8);
}
}
impl Display for Peripheral {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let connected = if self.is_connected() {
" connected"
} else {
""
};
let properties = self.properties.lock().unwrap();
write!(
f,
"{} {}{}",
self.address,
properties
.local_name
.clone()
.unwrap_or_else(|| "(unknown)".to_string()),
connected
)
}
}
impl Debug for Peripheral {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let connected = if self.is_connected() {
" connected"
} else {
""
};
let properties = self.properties.lock().unwrap();
let characteristics = self.characteristics.lock().unwrap();
write!(
f,
"{} properties: {:?}, characteristics: {:?} {}",
self.address, *properties, *characteristics, connected
)
}
}
impl ApiPeripheral for Peripheral {
/// Returns the address of the peripheral.
fn address(&self) -> BDAddr {
self.address.clone()
}
/// Returns the set of properties associated with the peripheral. These may be updated over time
/// as additional advertising reports are received.
fn properties(&self) -> PeripheralProperties {
let l = self.properties.lock().unwrap();
l.clone()
}
/// The set of characteristics we've discovered for this device. This will be empty until
/// `discover_characteristics` or `discover_characteristics_in_range` is called.
fn characteristics(&self) -> BTreeSet<Characteristic> {
let l = self.characteristics.lock().unwrap();
l.clone()
}
/// Returns true iff we are currently connected to the device.
fn is_connected(&self) -> bool {
self.connected.load(Ordering::Relaxed)
}
/// Creates a connection to the device. This is a synchronous operation; if this method returns
/// Ok there has been successful connection. Note that peripherals allow only one connection at
/// a time. Operations that attempt to communicate with a device will fail until it is connected.
fn connect(&self) -> Result<()> {
let connected = self.connected.clone();
let adapter_clone = self.adapter.clone();
let address_clone = self.address.clone();
let device = BLEDevice::new(
self.address,
Box::new(move |is_connected| {
connected.store(is_connected, Ordering::Relaxed);
if !is_connected {
adapter_clone.emit(CentralEvent::DeviceDisconnected(address_clone));
}
}),
)?;
device.connect()?;
let mut d = self.device.lock().unwrap();
*d = Some(device);
self.adapter
.emit(CentralEvent::DeviceConnected(self.address));
Ok(())
}
/// Terminates a connection to the device. This is a synchronous operation.
fn disconnect(&self) -> Result<()> {
let winrt_error = |e| Error::Other(format!("{:?}", e));
let mut device = self.device.lock().map_err(winrt_error)?;
*device = None;
self.adapter
.emit(CentralEvent::DeviceDisconnected(self.address));
Ok(())
}
/// Discovers all characteristics for the device. This is a synchronous operation.
fn discover_characteristics(&self) -> Result<Vec<Characteristic>> {
let device = self.device.lock().unwrap();
if let Some(ref device) = *device {
let mut characteristics_result = vec![];
let characteristics = device.discover_characteristics()?;
for characteristic in characteristics {
let uuid = utils::to_uuid(&characteristic.get_uuid().unwrap());
let properties =
utils::to_char_props(&characteristic.get_characteristic_properties().unwrap());
let chara = Characteristic {
uuid,
start_handle: 0,
end_handle: 0,
value_handle: 0,
properties,
};
characteristics_result.push(chara);
self.ble_characteristics
.entry(uuid)
.or_insert_with(|| BLECharacteristic::new(characteristic));
}
return Ok(characteristics_result);
}
Err(Error::NotConnected)
}
/// Discovers characteristics within the specified range of handles. This is a synchronous
/// operation.
fn discover_characteristics_in_range(
&self,
_start: u16,
_end: u16,
) -> Result<Vec<Characteristic>> {
Ok(Vec::new())
}
/// Sends a command (`write-without-response`) to the characteristic. Takes an optional callback
/// that will be notified in case of error or when the command has been successfully acked by the
/// device.
fn command_async(
&self,
_characteristic: &Characteristic,
_data: &[u8],
_handler: Option<CommandCallback>,
) {
}
/// Sends a command (write without response) to the characteristic. Synchronously returns a
/// `Result` with an error set if the command was not accepted by the device.
fn command(&self, _characteristic: &Characteristic, _data: &[u8]) -> Result<()> {
if let Some(ble_characteristic) = self.ble_characteristics.get(&_characteristic.uuid) {
ble_characteristic.write_value(_data)
} else {
Err(Error::NotSupported("read_by_type".into()))
}
}
/// Sends a request (write) to the device. Takes an optional callback with either an error if
/// the request was not accepted or the response from the device.
fn request_async(
&self,
_characteristic: &Characteristic,
_data: &[u8],
_handler: Option<RequestCallback>,
) {
}
/// Sends a request (write) to the device. Synchronously returns either an error if the request
/// was not accepted or the response from the device.
fn request(&self, _characteristic: &Characteristic, _data: &[u8]) -> Result<Vec<u8>> {
Ok(Vec::new())
}
/// Sends a read-by-type request to device for the range of handles covered by the
/// characteristic and for the specified declaration UUID. See
/// [here](https://www.bluetooth.com/specifications/gatt/declarations) for valid UUIDs.
/// Takes an optional callback that will be called with an error or the device response.
fn read_by_type_async(
&self,
_characteristic: &Characteristic,
_uuid: UUID,
_handler: Option<RequestCallback>,
) {
}
/// Sends a read-by-type request to device for the range of handles covered by the
/// characteristic and for the specified declaration UUID. See
/// [here](https://www.bluetooth.com/specifications/gatt/declarations) for valid UUIDs.
/// Synchronously returns either an error or the device response.
fn read_by_type(&self, characteristic: &Characteristic, _uuid: UUID) -> Result<Vec<u8>> {
if let Some(ble_characteristic) = self.ble_characteristics.get(&characteristic.uuid) {
return ble_characteristic.read_value();
} else {
Err(Error::NotSupported("read_by_type".into()))
}
}
/// Enables either notify or indicate (depending on support) for the specified characteristic.
/// This is a synchronous call.
fn subscribe(&self, characteristic: &Characteristic) -> Result<()> {
if let Some(mut ble_characteristic) = self.ble_characteristics.get_mut(&characteristic.uuid)
{
let notification_handlers = self.notification_handlers.clone();
let uuid = characteristic.uuid;
ble_characteristic.subscribe(Box::new(move |value| {
let notification = ValueNotification {
uuid: uuid,
handle: None,
value,
};
util::invoke_handlers(¬ification_handlers, ¬ification);
}))
} else {
Err(Error::NotSupported("subscribe".into()))
}
}
/// Disables either notify or indicate (depending on support) for the specified characteristic.
/// This is a synchronous call.
fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()> {
if let Some(mut ble_characteristic) = self.ble_characteristics.get_mut(&characteristic.uuid)
{
ble_characteristic.unsubscribe()
} else {
Err(Error::NotSupported("unsubscribe".into()))
}
}
/// Registers a handler that will be called when value notification messages are received from
/// the device. This method should only be used after a connection has been established. Note
/// that the handler will be called in a common thread, so it should not block.
fn on_notification(&self, handler: NotificationHandler) {
let mut list = self.notification_handlers.lock().unwrap();
list.push(handler);
}
fn read_async(&self, _characteristic: &Characteristic, _handler: Option<RequestCallback>) {}
fn read(&self, _characteristic: &Characteristic) -> Result<Vec<u8>> {
Ok(vec![])
}
}