Skip to content

Commit

Permalink
Merge pull request #88 from embassy-rs/lookup-complete-handle
Browse files Browse the repository at this point in the history
fix an issue where the returned handle would not contain the cccd handle
  • Loading branch information
lulf authored Aug 22, 2024
2 parents bb5002c + cbafa08 commit c4ac325
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
28 changes: 28 additions & 0 deletions host/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Characteristic, Error> {
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))]
Expand Down
15 changes: 3 additions & 12 deletions host/src/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
3 changes: 2 additions & 1 deletion host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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;
Expand Down

0 comments on commit c4ac325

Please sign in to comment.