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

Revert "remove superflous api for adding descriptors" #104

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
13 changes: 8 additions & 5 deletions examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NoopRawMutex, MAX_ATTRIBUTES, L2CAP_MTU>(&table);

Expand Down
62 changes: 56 additions & 6 deletions host/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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<U: Into<Uuid>>(&mut self, uuid: U, value: &'d [u8]) -> Characteristic {
pub fn add_characteristic_ro<U: Into<Uuid>>(
&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 })
}
Expand Down Expand Up @@ -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<U: Into<Uuid>>(
&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<U: Into<Uuid>>(&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 {
Expand Down
3 changes: 2 additions & 1 deletion host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NoopRawMutex, 10, 27>(&table);

Expand Down