From 8189db81d132d807ec2754c0d7d4e521c9b7abc2 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 22 Aug 2024 15:59:29 +0200 Subject: [PATCH 1/2] fix an issue where the returned handle would not contain the cccd handle --- host/src/attribute.rs | 28 ++++++++++++++++++++++++++++ host/src/gatt.rs | 15 +++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/host/src/attribute.rs b/host/src/attribute.rs index 6af693d..6bf452d 100644 --- a/host/src/attribute.rs +++ b/host/src/attribute.rs @@ -364,6 +364,34 @@ impl<'d, M: RawMutex, const MAX: usize> AttributeTable<'d, M, MAX> { Err(Error::NotFound) }) } + + pub(crate) fn find_characteristic_by_value_handle(&self, handle: u16) -> Result { + self.iterate(|mut it| { + while let Some(att) = it.next() { + if att.handle == handle { + // If next is CCCD + if let Some(next) = it.next() { + if let AttributeData::Cccd { + notifications: _, + indications: _, + } = &next.data + { + return Ok(Characteristic { + handle, + cccd_handle: Some(next.handle), + }); + } + } else { + return Ok(Characteristic { + handle, + cccd_handle: None, + }); + } + } + } + Err(Error::NotFound) + }) + } } #[cfg_attr(feature = "defmt", derive(defmt::Format))] diff --git a/host/src/gatt.rs b/host/src/gatt.rs index 6612816..b35f171 100644 --- a/host/src/gatt.rs +++ b/host/src/gatt.rs @@ -58,26 +58,17 @@ impl<'reference, 'values, M: RawMutex, const MAX: usize, const MTU: usize> let event = match att { AttReq::Write { handle, data } => Some(GattEvent::Write { connection, - handle: Characteristic { - handle, - cccd_handle: None, - }, + handle: self.server.table.find_characteristic_by_value_handle(handle)?, }), AttReq::Read { handle } => Some(GattEvent::Read { connection, - handle: Characteristic { - handle, - cccd_handle: None, - }, + handle: self.server.table.find_characteristic_by_value_handle(handle)?, }), AttReq::ReadBlob { handle, offset } => Some(GattEvent::Read { connection, - handle: Characteristic { - handle, - cccd_handle: None, - }, + handle: self.server.table.find_characteristic_by_value_handle(handle)?, }), _ => None, }; From cbafa08ef06346e92edb8b151755d1cc336753a1 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 22 Aug 2024 16:11:12 +0200 Subject: [PATCH 2/2] add test for handle issue --- host/tests/gatt.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/host/tests/gatt.rs b/host/tests/gatt.rs index 5d5e473..66ccbd9 100644 --- a/host/tests/gatt.rs +++ b/host/tests/gatt.rs @@ -57,7 +57,7 @@ async fn gatt_client_server() { table.add_service(Service::new(0x1801)); // Custom service - table.add_service(Service::new(SERVICE_UUID.clone())) + let value_handle = table.add_service(Service::new(SERVICE_UUID.clone())) .add_characteristic( VALUE_UUID.clone(), &[CharacteristicProp::Read, CharacteristicProp::Write, CharacteristicProp::Notify], @@ -79,6 +79,7 @@ async fn gatt_client_server() { connection: _, handle, }) => { + assert_eq!(handle, value_handle); let _ = table.get(handle, |value| { assert_eq!(expected, value[0]); expected += 1;