From b94416e07018472d4e703bb3fe6929b4f64d36bf Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 2 Sep 2024 18:04:01 +0200 Subject: [PATCH 1/2] Revert "remove superflous api for adding descriptors" This reverts commit 13a5d962a913a96119825535dda324255bccb701. --- examples/apps/src/ble_bas_peripheral.rs | 13 ++++-- host/src/attribute.rs | 62 ++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/examples/apps/src/ble_bas_peripheral.rs b/examples/apps/src/ble_bas_peripheral.rs index d63ab86..640718f 100644 --- a/examples/apps/src/ble_bas_peripheral.rs +++ b/examples/apps/src/ble_bas_peripheral.rs @@ -48,11 +48,14 @@ where table.add_service(Service::new(0x1801)); // Battery service - let level_handle = table.add_service(Service::new(0x180f)).add_characteristic( - 0x2a19, - &[CharacteristicProp::Read, CharacteristicProp::Notify], - &mut bat_level, - ); + let level_handle = table + .add_service(Service::new(0x180f)) + .add_characteristic( + 0x2a19, + &[CharacteristicProp::Read, CharacteristicProp::Notify], + &mut bat_level, + ) + .build(); let server = ble.gatt_server::(&table); diff --git a/host/src/attribute.rs b/host/src/attribute.rs index 33f9586..4efc874 100644 --- a/host/src/attribute.rs +++ b/host/src/attribute.rs @@ -420,7 +420,7 @@ impl<'r, 'd, M: RawMutex, const MAX: usize> ServiceBuilder<'r, 'd, M, MAX> { uuid: Uuid, props: CharacteristicProps, data: AttributeData<'d>, - ) -> Characteristic { + ) -> CharacteristicBuilder<'_, 'd, M, MAX> { // First the characteristic declaration let next = self.table.handle + 1; let cccd = self.table.handle + 2; @@ -459,9 +459,12 @@ impl<'r, 'd, M: RawMutex, const MAX: usize> ServiceBuilder<'r, 'd, M, MAX> { None }; - Characteristic { - handle: next, - cccd_handle, + CharacteristicBuilder { + handle: Characteristic { + handle: next, + cccd_handle, + }, + table: self.table, } } @@ -470,12 +473,16 @@ impl<'r, 'd, M: RawMutex, const MAX: usize> ServiceBuilder<'r, 'd, M, MAX> { uuid: U, props: &[CharacteristicProp], storage: &'d mut [u8], - ) -> Characteristic { + ) -> CharacteristicBuilder<'_, 'd, M, MAX> { let props = props.into(); self.add_characteristic_internal(uuid.into(), props, AttributeData::Data { props, value: storage }) } - pub fn add_characteristic_ro>(&mut self, uuid: U, value: &'d [u8]) -> Characteristic { + pub fn add_characteristic_ro>( + &mut self, + uuid: U, + value: &'d [u8], + ) -> CharacteristicBuilder<'_, 'd, M, MAX> { let props = [CharacteristicProp::Read].into(); self.add_characteristic_internal(uuid.into(), props, AttributeData::ReadOnlyData { props, value }) } @@ -506,6 +513,49 @@ pub struct Characteristic { pub(crate) handle: u16, } +pub struct CharacteristicBuilder<'r, 'd, M: RawMutex, const MAX: usize> { + handle: Characteristic, + table: &'r mut AttributeTable<'d, M, MAX>, +} + +impl<'r, 'd, M: RawMutex, const MAX: usize> CharacteristicBuilder<'r, 'd, M, MAX> { + fn add_descriptor_internal( + &mut self, + uuid: Uuid, + props: CharacteristicProps, + data: AttributeData<'d>, + ) -> DescriptorHandle { + let handle = self.table.handle; + self.table.push(Attribute { + uuid, + handle: 0, + last_handle_in_group: 0, + data, + }); + + DescriptorHandle { handle } + } + + pub fn add_descriptor>( + &mut self, + uuid: U, + props: &[CharacteristicProp], + data: &'d mut [u8], + ) -> DescriptorHandle { + let props = props.into(); + self.add_descriptor_internal(uuid.into(), props, AttributeData::Data { props, value: data }) + } + + pub fn add_descriptor_ro>(&mut self, uuid: U, data: &'d [u8]) -> DescriptorHandle { + let props = [CharacteristicProp::Read].into(); + self.add_descriptor_internal(uuid.into(), props, AttributeData::ReadOnlyData { props, value: data }) + } + + pub fn build(self) -> Characteristic { + self.handle + } +} + #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Clone, Copy, Debug)] pub struct DescriptorHandle { From 4cc892a0174c909ae42bedf558979e3fbbe50f65 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Mon, 2 Sep 2024 18:09:57 +0200 Subject: [PATCH 2/2] fix integration test --- 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 933601d..6f352f8 100644 --- a/host/tests/gatt.rs +++ b/host/tests/gatt.rs @@ -61,7 +61,8 @@ async fn gatt_client_server() { .add_characteristic( VALUE_UUID.clone(), &[CharacteristicProp::Read, CharacteristicProp::Write, CharacteristicProp::Notify], - &mut value); + &mut value) + .build(); let server = adapter.gatt_server::(&table);