From 22d438155191d9269d0574278911fb29394e1bdc Mon Sep 17 00:00:00 2001 From: night-crawler Date: Thu, 30 Nov 2023 10:40:35 +0000 Subject: [PATCH 1/3] Add service_uuid to the ValueNotification struct --- src/api/mod.rs | 2 ++ src/bluez/peripheral.rs | 8 ++++---- src/corebluetooth/internal.rs | 4 ++-- src/corebluetooth/peripheral.rs | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index f24bba61..c18f0d4b 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -84,6 +84,8 @@ impl AddressType { /// A notification sent from a peripheral due to a change in a value. #[derive(Clone, Debug, Eq, PartialEq)] pub struct ValueNotification { + /// UUID of the service that fired the notification. + pub service_uuid: Uuid, /// UUID of the characteristic that fired the notification. pub uuid: Uuid, /// The new value of the characteristic. diff --git a/src/bluez/peripheral.rs b/src/bluez/peripheral.rs index 426ff30b..c95f1956 100644 --- a/src/bluez/peripheral.rs +++ b/src/bluez/peripheral.rs @@ -282,8 +282,8 @@ fn value_notification( event: CharacteristicEvent::Value { value }, } if id.service().device() == *device_id => { let services = services.lock().unwrap(); - let uuid = find_characteristic_by_id(&services, id)?.uuid; - Some(ValueNotification { uuid, value }) + let (service, characteristic) = find_characteristic_by_id(&services, id)?; + Some(ValueNotification { uuid: characteristic.uuid, service_uuid: service.info.uuid, value }) } _ => None, } @@ -292,11 +292,11 @@ fn value_notification( fn find_characteristic_by_id( services: &HashMap, characteristic_id: CharacteristicId, -) -> Option<&CharacteristicInfo> { +) -> Option<(&ServiceInternal, &CharacteristicInfo)> { for service in services.values() { for characteristic in service.characteristics.values() { if characteristic.info.id == characteristic_id { - return Some(&characteristic.info); + return Some((service, &characteristic.info)); } } } diff --git a/src/corebluetooth/internal.rs b/src/corebluetooth/internal.rs index a358cc93..185c13bb 100644 --- a/src/corebluetooth/internal.rs +++ b/src/corebluetooth/internal.rs @@ -155,7 +155,7 @@ pub enum CoreBluetoothReply { #[derive(Debug)] pub enum CBPeripheralEvent { Disconnected, - Notification(Uuid, Vec), + Notification(Uuid, Uuid, Vec), ManufacturerData(u16, Vec, i16), ServiceData(HashMap>, i16), Services(Vec, i16), @@ -737,7 +737,7 @@ impl CoreBluetoothInternal { .set_reply(CoreBluetoothReply::ReadResult(data_clone)); } else if let Err(e) = peripheral .event_sender - .send(CBPeripheralEvent::Notification(characteristic_uuid, data)) + .send(CBPeripheralEvent::Notification(service_uuid, characteristic_uuid, data)) .await { error!("Error sending notification event: {}", e); diff --git a/src/corebluetooth/peripheral.rs b/src/corebluetooth/peripheral.rs index 117faab7..3be0e2de 100644 --- a/src/corebluetooth/peripheral.rs +++ b/src/corebluetooth/peripheral.rs @@ -120,8 +120,8 @@ impl Peripheral { loop { match event_receiver.next().await { - Some(CBPeripheralEvent::Notification(uuid, data)) => { - let notification = ValueNotification { uuid, value: data }; + Some(CBPeripheralEvent::Notification(service_uuid, uuid, data)) => { + let notification = ValueNotification { service_uuid, uuid, value: data }; // Note: we ignore send errors here which may happen while there are no // receivers... From fbec06e624db7e351bd4991b93bbc3f629eb57f7 Mon Sep 17 00:00:00 2001 From: night-crawler Date: Thu, 30 Nov 2023 16:33:46 +0000 Subject: [PATCH 2/3] Add service_uuid to the ValueNotification struct --- src/bluez/peripheral.rs | 6 +++++- src/corebluetooth/internal.rs | 6 +++++- src/corebluetooth/peripheral.rs | 6 +++++- src/winrtble/peripheral.rs | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/bluez/peripheral.rs b/src/bluez/peripheral.rs index c95f1956..b6455479 100644 --- a/src/bluez/peripheral.rs +++ b/src/bluez/peripheral.rs @@ -283,7 +283,11 @@ fn value_notification( } if id.service().device() == *device_id => { let services = services.lock().unwrap(); let (service, characteristic) = find_characteristic_by_id(&services, id)?; - Some(ValueNotification { uuid: characteristic.uuid, service_uuid: service.info.uuid, value }) + Some(ValueNotification { + uuid: characteristic.uuid, + service_uuid: service.info.uuid, + value, + }) } _ => None, } diff --git a/src/corebluetooth/internal.rs b/src/corebluetooth/internal.rs index 185c13bb..8d538196 100644 --- a/src/corebluetooth/internal.rs +++ b/src/corebluetooth/internal.rs @@ -737,7 +737,11 @@ impl CoreBluetoothInternal { .set_reply(CoreBluetoothReply::ReadResult(data_clone)); } else if let Err(e) = peripheral .event_sender - .send(CBPeripheralEvent::Notification(service_uuid, characteristic_uuid, data)) + .send(CBPeripheralEvent::Notification( + service_uuid, + characteristic_uuid, + data, + )) .await { error!("Error sending notification event: {}", e); diff --git a/src/corebluetooth/peripheral.rs b/src/corebluetooth/peripheral.rs index 3be0e2de..5a8cf1bd 100644 --- a/src/corebluetooth/peripheral.rs +++ b/src/corebluetooth/peripheral.rs @@ -121,7 +121,11 @@ impl Peripheral { loop { match event_receiver.next().await { Some(CBPeripheralEvent::Notification(service_uuid, uuid, data)) => { - let notification = ValueNotification { service_uuid, uuid, value: data }; + let notification = ValueNotification { + service_uuid, + uuid, + value: data, + }; // Note: we ignore send errors here which may happen while there are no // receivers... diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index 56f0e43b..1f11b2f9 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -499,7 +499,11 @@ impl ApiPeripheral for Peripheral { let uuid = characteristic.uuid; ble_characteristic .subscribe(Box::new(move |value| { - let notification = ValueNotification { uuid, value }; + let notification = ValueNotification { + service_uuid: ble_service.uuid, + uuid, + value, + }; // Note: we ignore send errors here which may happen while there are no // receivers... let _ = notifications_sender.send(notification); From 2ef55b81d2881caa4fd12e713f944bb9daca9ca6 Mon Sep 17 00:00:00 2001 From: night-crawler Date: Thu, 30 Nov 2023 16:56:10 +0000 Subject: [PATCH 3/3] Add service_uuid to the ValueNotification struct --- src/winrtble/peripheral.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/winrtble/peripheral.rs b/src/winrtble/peripheral.rs index 1f11b2f9..c2ba385f 100644 --- a/src/winrtble/peripheral.rs +++ b/src/winrtble/peripheral.rs @@ -497,10 +497,11 @@ impl ApiPeripheral for Peripheral { .ok_or_else(|| Error::NotSupported("Characteristic not found for subscribe".into()))?; let notifications_sender = self.shared.notifications_channel.clone(); let uuid = characteristic.uuid; + let service_uuid = ble_service.uuid; ble_characteristic .subscribe(Box::new(move |value| { let notification = ValueNotification { - service_uuid: ble_service.uuid, + service_uuid, uuid, value, };