Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service_uuid to the ValueNotification struct #352

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions src/bluez/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,12 @@ 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,
}
Expand All @@ -292,11 +296,11 @@ fn value_notification(
fn find_characteristic_by_id(
services: &HashMap<Uuid, ServiceInternal>,
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));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub enum CoreBluetoothReply {
#[derive(Debug)]
pub enum CBPeripheralEvent {
Disconnected,
Notification(Uuid, Vec<u8>),
Notification(Uuid, Uuid, Vec<u8>),
ManufacturerData(u16, Vec<u8>, i16),
ServiceData(HashMap<Uuid, Vec<u8>>, i16),
Services(Vec<Uuid>, i16),
Expand Down Expand Up @@ -742,7 +742,11 @@ 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);
Expand Down
8 changes: 6 additions & 2 deletions src/corebluetooth/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ 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...
Expand Down
7 changes: 6 additions & 1 deletion src/winrtble/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,14 @@ 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 { uuid, value };
let notification = ValueNotification {
service_uuid,
uuid,
value,
};
// Note: we ignore send errors here which may happen while there are no
// receivers...
let _ = notifications_sender.send(notification);
Expand Down
Loading