Skip to content

Commit

Permalink
Merge pull request #84 from embassy-rs/compare-handles
Browse files Browse the repository at this point in the history
Support comparing characteristic handles
  • Loading branch information
lulf authored Aug 21, 2024
2 parents ebfd92a + c8e4124 commit 75dc087
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 deletions.
28 changes: 13 additions & 15 deletions examples/esp32/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,23 @@ async fn main(_s: Spawner) {
let id = b"Trouble ESP32";
let appearance = [0x80, 0x07];
let mut bat_level = [0; 1];
let handle = {
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Battery service
let mut svc = table.add_service(Service::new(0x180f));

svc.add_characteristic(
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Battery service
let handle = table
.add_service(Service::new(0x180f))
.add_characteristic(
0x2a19,
&[CharacteristicProp::Read, CharacteristicProp::Notify],
&mut bat_level,
)
.build()
};
.build();

let mut adv_data = [0; 31];
AdStructure::encode_slice(
Expand Down
36 changes: 18 additions & 18 deletions examples/nrf-sdc/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,23 @@ async fn main(spawner: Spawner) {
let id = b"Trouble";
let appearance = [0x80, 0x07];
let mut bat_level = [23; 1];
let handle = {
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Battery service
let mut svc = table.add_service(Service::new(0x180f));

svc.add_characteristic(
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
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,
)
.build()
};
.build();

let server = ble.gatt_server(&table);
let mut adv_data = [0; 31];
Expand All @@ -150,8 +148,10 @@ async fn main(spawner: Spawner) {
info!("Write event. Value written: {:?}", value);
});
}
Ok(GattEvent::Read { .. }) => {
info!("Read event");
Ok(GattEvent::Read { handle, connection: _ }) => {
if handle == level_handle {
info!("Battery level read");
}
}
Err(e) => {
error!("Error processing GATT events: {:?}", e);
Expand All @@ -176,7 +176,7 @@ async fn main(spawner: Spawner) {
loop {
Timer::after(Duration::from_secs(10)).await;
tick += 1;
unwrap!(server.notify(handle, &conn, &[tick]).await);
unwrap!(server.notify(level_handle, &conn, &[tick]).await);
}
},
)
Expand Down
28 changes: 13 additions & 15 deletions examples/serial-hci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,23 @@ async fn main() {
let id = b"Trouble HCI";
let appearance = [0x80, 0x07];
let mut bat_level = [0; 1];
let handle = {
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Battery service
let mut svc = table.add_service(Service::new(0x180f));

svc.add_characteristic(
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Battery service
let handle = table
.add_service(Service::new(0x180f))
.add_characteristic(
0x2a19,
&[CharacteristicProp::Read, CharacteristicProp::Notify],
&mut bat_level,
)
.build()
};
.build();

let mut adv_data = [0; 31];
AdStructure::encode_slice(
Expand Down
2 changes: 1 addition & 1 deletion host/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl<'r, 'd, M: RawMutex, const MAX: usize> Drop for ServiceBuilder<'r, 'd, M, M
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Characteristic {
pub(crate) cccd_handle: Option<u16>,
pub(crate) handle: u16,
Expand Down
35 changes: 16 additions & 19 deletions host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,22 @@ async fn gatt_client_server() {
// Random starting value to 'prove' the incremented value is correct
let mut value: [u8; 1] = [rand::prelude::random(); 1];
let mut expected = value[0].wrapping_add(1);
{
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Custom service
let mut svc = table.add_service(Service::new(SERVICE_UUID.clone()));

svc.add_characteristic(
VALUE_UUID.clone(),
&[CharacteristicProp::Read, CharacteristicProp::Write, CharacteristicProp::Notify],
&mut value,
)
.build();
}
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance[..]);
svc.build();

// Generic attribute service (mandatory)
table.add_service(Service::new(0x1801));

// Custom service
table.add_service(Service::new(SERVICE_UUID.clone()))
.add_characteristic(
VALUE_UUID.clone(),
&[CharacteristicProp::Read, CharacteristicProp::Write, CharacteristicProp::Notify],
&mut value,
)
.build();

let server = adapter.gatt_server(&table);

Expand Down

0 comments on commit 75dc087

Please sign in to comment.