From 72922aa486290bf7ab430f4efccc881a14892327 Mon Sep 17 00:00:00 2001 From: David Zemon Date: Tue, 9 Jan 2024 13:20:01 -0500 Subject: [PATCH 1/3] fix: Ignore failed descriptor reads on Windows This brings the Windows implementation in line with Linux Fixes #361 --- src/winrtble/peripheral.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index 5dd429a9..f4d5aea2 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -415,29 +415,23 @@ impl ApiPeripheral for Peripheral { Ok(characteristics) => { let characteristics = characteristics.into_iter().map(|characteristic| async { - match BLEDevice::get_characteristic_descriptors(&characteristic) - .await - { - Ok(descriptors) => { - let descriptors: HashMap = - descriptors - .into_iter() - .map(|descriptor| { - let descriptor = - BLEDescriptor::new(descriptor); - (descriptor.uuid(), descriptor) - }) - .collect(); - Ok((characteristic, descriptors)) - } - Err(e) => { - error!("get_characteristic_descriptors_async {:?}", e); - Err(e) - } - } + let c = characteristic.clone(); + ( + characteristic, + BLEDevice::get_characteristic_descriptors(&c) + .await + .unwrap_or(Vec::new()) + .into_iter() + .map(|descriptor| { + let descriptor = BLEDescriptor::new(descriptor); + (descriptor.uuid(), descriptor) + }) + .collect(), + ) }); - let characteristics = futures::future::try_join_all(characteristics) - .await? + + let characteristics = futures::future::join_all(characteristics) + .await .into_iter() .map(|(characteristic, descriptors)| { let characteristic = From 05b33d9ab5a800752d8a28d54b3e893da3bbb1c0 Mon Sep 17 00:00:00 2001 From: David Zemon Date: Tue, 9 Jan 2024 14:56:45 -0500 Subject: [PATCH 2/3] fix: Increase resilience to characteristic read errors Discovery of a peripheral's services should not error out merely because one of the characteristics encountered an "AccessDenied" or similar error --- src/winrtble/ble/device.rs | 30 +++++++++++++++++++++--------- src/winrtble/peripheral.rs | 5 ++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/winrtble/ble/device.rs b/src/winrtble/ble/device.rs index 560203c6..9fd2707f 100644 --- a/src/winrtble/ble/device.rs +++ b/src/winrtble/ble/device.rs @@ -100,15 +100,27 @@ impl BLEDevice { let async_result = service .GetCharacteristicsWithCacheModeAsync(BluetoothCacheMode::Uncached)? .await?; - let status = async_result.Status(); - if status == Ok(GattCommunicationStatus::Success) { - let results = async_result.Characteristics()?; - debug!("characteristics {:?}", results.Size()); - Ok(results.into_iter().collect()) - } else { - Err(Error::Other( - format!("get_characteristics for {:?} failed: {:?}", service, status).into(), - )) + + match async_result.Status() { + Ok(GattCommunicationStatus::Success) => { + let results = async_result.Characteristics()?; + debug!("characteristics {:?}", results.Size()); + Ok(results.into_iter().collect()) + } + Ok(GattCommunicationStatus::ProtocolError) => Err(Error::Other( + format!( + "get_characteristics for {:?} encountered a protocol error", + service + ) + .into(), + )), + Ok(status) => { + debug!("characteristic read failed due to {:?}", status); + Ok(vec![]) + } + Err(e) => Err(Error::Other( + format!("get_characteristics for {:?} failed: {:?}", service, e).into(), + )), } } diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index f4d5aea2..fda061f9 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -27,7 +27,7 @@ use crate::{ use async_trait::async_trait; use dashmap::DashMap; use futures::stream::Stream; -use log::{error, trace}; +use log::{trace, warn}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "serde")] @@ -449,8 +449,7 @@ impl ApiPeripheral for Peripheral { ); } Err(e) => { - error!("get_characteristics_async {:?}", e); - return Err(e); + warn!("get_characteristics_async {:?}", e); } } } From 1937ff5badba24c2a857e48c2c38a1c2a1ecf6b3 Mon Sep 17 00:00:00 2001 From: David Zemon Date: Tue, 9 Jan 2024 14:35:22 -0500 Subject: [PATCH 3/3] doc: Roll version/CHANGELOG for 0.11.5 --- CHANGELOG.md | 6 ++++++ Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16b598eb..5dd7f1bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.11.5 (2024-01-10) + +## Bugfixes + +- Fix issue with Windows failing to read characteristic descriptors + # 0.11.4 (2024-01-01) ## Bugfixes diff --git a/Cargo.toml b/Cargo.toml index 6b788a53..8897c430 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "btleplug" -version = "0.11.4" +version = "0.11.5" authors = ["Nonpolynomial, LLC "] license = "MIT/Apache-2.0/BSD-3-Clause" repository = "https://github.com/deviceplug/btleplug"