-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathadapter.rs
More file actions
291 lines (259 loc) · 10.4 KB
/
adapter.rs
File metadata and controls
291 lines (259 loc) · 10.4 KB
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
// 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::watcher::BLEWatcher, peripheral::Peripheral, peripheral::PeripheralId};
use crate::{
api::{BDAddr, Central, CentralEvent, CentralState, ScanFilter},
common::adapter_manager::AdapterManager,
Error, Result,
};
use async_trait::async_trait;
use futures::stream::Stream;
use log::trace;
use std::convert::TryInto;
use std::fmt::{self, Debug, Formatter};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
use windows::{
Devices::Bluetooth::BluetoothLEDevice,
Devices::Enumeration::DeviceInformation,
Devices::Radios::{Radio, RadioState},
Foundation::TypedEventHandler,
};
/// Implementation of [api::Central](crate::api::Central).
#[derive(Clone)]
pub struct Adapter {
watcher: Arc<Mutex<BLEWatcher>>,
manager: Arc<AdapterManager<Peripheral>>,
radio: Radio,
}
// https://github.com/microsoft/windows-rs/blob/master/crates/libs/windows/src/Windows/Devices/Radios/mod.rs
fn get_central_state(radio: &Radio) -> CentralState {
let state = radio.State().unwrap_or(RadioState::Unknown);
match state {
RadioState::On => CentralState::PoweredOn,
RadioState::Off => CentralState::PoweredOff,
_ => CentralState::Unknown,
}
}
impl Adapter {
pub(crate) fn new(radio: Radio) -> Result<Self> {
let watcher = Arc::new(Mutex::new(BLEWatcher::new()?));
let manager = Arc::new(AdapterManager::default());
let radio_clone = radio.clone();
let manager_clone = manager.clone();
let handler = TypedEventHandler::new(move |_sender, _args| {
let state = get_central_state(&radio_clone);
manager_clone.emit(CentralEvent::StateUpdate(state.into()));
Ok(())
});
if let Err(err) = radio.StateChanged(&handler) {
eprintln!("radio.StateChanged error: {}", err);
}
Ok(Adapter {
watcher,
manager,
radio,
})
}
pub fn clear_cache(&self) {
self.manager.clear_peripherals();
}
}
impl Debug for Adapter {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("Adapter")
.field("manager", &self.manager)
.finish()
}
}
#[async_trait]
impl Central for Adapter {
type Peripheral = Peripheral;
async fn events(&self) -> Result<Pin<Box<dyn Stream<Item = CentralEvent> + Send>>> {
Ok(self.manager.event_stream())
}
async fn start_scan(&self, filter: ScanFilter) -> Result<()> {
let watcher = self.watcher.lock().map_err(Into::<Error>::into)?;
let manager = self.manager.clone();
watcher.start(
filter,
Box::new(move |args| {
let bluetooth_address = args.BluetoothAddress()?;
let address: BDAddr = bluetooth_address.try_into().unwrap();
if let Some(mut entry) = manager.peripheral_mut(&address.into()) {
entry.value_mut().update_properties(args);
manager.emit(CentralEvent::DeviceUpdated(address.into()));
} else {
let peripheral = Peripheral::new(Arc::downgrade(&manager), address);
peripheral.update_properties(args);
manager.add_peripheral(peripheral);
manager.emit(CentralEvent::DeviceDiscovered(address.into()));
}
Ok(())
}),
)
}
async fn stop_scan(&self) -> Result<()> {
let watcher = self.watcher.lock().map_err(Into::<Error>::into)?;
watcher.stop()?;
Ok(())
}
async fn connected_peripherals(&self, filter: ScanFilter) -> Result<()> {
let base_selector = BluetoothLEDevice::GetDeviceSelector()
.map_err(|e| Error::Other(format!("GetDeviceSelector failed: {:?}", e).into()))?;
let aqs = format!(
"{} AND System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#True",
base_selector.to_string()
);
// Query all BLE devices that are currently connected to the system
let devices = DeviceInformation::FindAllAsyncAqsFilter(&windows::core::HSTRING::from(aqs))
.map_err(|e| Error::Other(format!("FindAllAsyncAqsFilter failed: {:?}", e).into()))?
.get()
.map_err(|e| Error::Other(format!("FindAllAsync().get() failed: {:?}", e).into()))?;
let manager = self.manager.clone();
let required_services: Vec<Uuid> = filter.services.clone();
trace!(
"Scanning for connected peripherals with {} service filters",
required_services.len()
);
// Iterate through each connected device
for device in devices {
let device_id = match device.Id() {
Ok(id) => id,
Err(e) => {
trace!("Failed to get device ID: {:?}", e);
continue;
}
};
trace!("Checking connected device: {:?}", device_id);
// BluetoothLEDevice from the device ID
let ble_device = match BluetoothLEDevice::FromIdAsync(&device_id) {
Ok(async_op) => match async_op.get() {
Ok(dev) => dev,
Err(e) => {
trace!("FromIdAsync.get() failed for {:?}: {:?}", device_id, e);
continue;
}
},
Err(e) => {
trace!("FromIdAsync failed for {:?}: {:?}", device_id, e);
continue;
}
};
// Double-check the connection status
match ble_device.ConnectionStatus() {
Ok(status)
if status
== windows::Devices::Bluetooth::BluetoothConnectionStatus::Connected => {}
Ok(_) => {
trace!("Device {:?} not connected, skipping", device_id);
continue;
}
Err(e) => {
trace!("Failed to get connection status: {:?}", e);
continue;
}
}
// Service filtering logic:
// - If no services specified in filter, accept all connected devices
// - Otherwise, accept only if the device has at least one matching service
let mut accept_device = required_services.is_empty();
if !accept_device {
// Query the device's GATT services to check for matches
let services_result = match ble_device.GetGattServicesAsync() {
Ok(async_op) => async_op.get(),
Err(e) => {
trace!("GetGattServicesAsync failed: {:?}", e);
continue;
}
};
let services = match services_result {
Ok(gatt_services) => match gatt_services.Services() {
Ok(service_list) => service_list,
Err(e) => {
trace!("Failed to get Services list: {:?}", e);
continue;
}
},
Err(e) => {
trace!("GetGattServicesAsync.get() failed: {:?}", e);
continue;
}
};
// Check if any of the device's services match the filter
for service in &services {
if let Ok(guid) = service.Uuid() {
let service_uuid = Uuid::from_u128(guid.to_u128());
if required_services.contains(&service_uuid) {
trace!("Found matching service: {:?}", service_uuid);
accept_device = true;
break;
}
}
}
}
if !accept_device {
trace!("Device does not match service filter, skipping");
continue;
}
// Convert Bluetooth address to BDAddr
let address: BDAddr = match ble_device.BluetoothAddress() {
Ok(addr) => match (addr as u64).try_into() {
Ok(bd_addr) => bd_addr,
Err(_) => {
trace!("Failed to convert Bluetooth address: {}", addr);
continue;
}
},
Err(e) => {
trace!("BluetoothAddress() failed: {:?}", e);
continue;
}
};
// Update the peripheral in the manager
match manager.peripheral_mut(&address.into()) {
Some(_) => {
trace!("Peripheral already exists in manager: {:?}", address);
manager.emit(CentralEvent::DeviceDiscovered(address.into()));
}
None => {
trace!("Adding new peripheral: {:?}", address);
let peripheral = Peripheral::new(Arc::downgrade(&manager), address);
manager.add_peripheral(peripheral);
manager.emit(CentralEvent::DeviceDiscovered(address.into()));
}
}
}
trace!("Finished scanning for connected peripherals");
Ok(())
}
async fn peripherals(&self) -> Result<Vec<Peripheral>> {
Ok(self.manager.peripherals())
}
async fn peripheral(&self, id: &PeripheralId) -> Result<Peripheral> {
self.manager.peripheral(id).ok_or(Error::DeviceNotFound)
}
async fn add_peripheral(&self, _address: &PeripheralId) -> Result<Peripheral> {
Err(Error::NotSupported(
"Can't add a Peripheral from a BDAddr".to_string(),
))
}
async fn adapter_info(&self) -> Result<String> {
// TODO: Get information about the adapter.
Ok("WinRT".to_string())
}
async fn adapter_state(&self) -> Result<CentralState> {
Ok(get_central_state(&self.radio))
}
}