Skip to content

Commit

Permalink
add 128 and 16 bit conversions to Uuid (#265)
Browse files Browse the repository at this point in the history
* rename val to data for consistency

* disambiguate u16 UUIDs in gatt test

* add const u128 example to gatt_derive test
  • Loading branch information
jamessizeland authored Jan 25, 2025
1 parent 94d58a8 commit 0950a9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
34 changes: 26 additions & 8 deletions host/src/types/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,32 @@ pub enum Uuid {
}

impl From<BluetoothUuid16> for Uuid {
fn from(val: bt_hci::uuid::BluetoothUuid16) -> Self {
Uuid::Uuid16(val.into())
fn from(data: bt_hci::uuid::BluetoothUuid16) -> Self {
Uuid::Uuid16(data.into())
}
}

impl From<u128> for Uuid {
fn from(data: u128) -> Self {
Uuid::Uuid128(data.to_le_bytes())
}
}

impl From<[u8; 16]> for Uuid {
fn from(data: [u8; 16]) -> Self {
Uuid::Uuid128(data)
}
}

impl From<[u8; 2]> for Uuid {
fn from(data: [u8; 2]) -> Self {
Uuid::Uuid16(data)
}
}

impl From<u16> for Uuid {
fn from(data: u16) -> Self {
Uuid::Uuid16(data.to_le_bytes())
}
}

Expand Down Expand Up @@ -71,12 +95,6 @@ impl Uuid {
}
}

impl From<u16> for Uuid {
fn from(data: u16) -> Self {
Uuid::Uuid16(data.to_le_bytes())
}
}

impl TryFrom<&[u8]> for Uuid {
type Error = crate::Error;

Expand Down
8 changes: 4 additions & 4 deletions host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ async fn gatt_client_server() {
let mut expected = value.wrapping_add(1);

let mut table: AttributeTable<'_, NoopRawMutex, 10> = AttributeTable::new();
let mut svc = table.add_service(Service::new(0x1800));
let _ = svc.add_characteristic_ro(0x2a00, id);
let _ = svc.add_characteristic_ro(0x2a01, &appearance);
let mut svc = table.add_service(Service::new(0x1800u16));
let _ = svc.add_characteristic_ro(0x2a00u16, id);
let _ = svc.add_characteristic_ro(0x2a01u16, &appearance);
svc.build();

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

// Custom service
let _handle: Characteristic<u8> = table.add_service(Service::new(SERVICE_UUID.clone()))
Expand Down
4 changes: 3 additions & 1 deletion host/tests/gatt_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ struct Server {
bas: BatteryService,
}

const UUID_U128: u128 = 0x408813df_5dd4_1f87_ec11_cdb000100000;

#[gatt_service(uuid = "408813df-5dd4-1f87-ec11-cdb000100000")]
struct CustomService {
#[descriptor(uuid = "2b20", value = "Read Only Descriptor", read)]
#[characteristic(uuid = "408813df-5dd4-1f87-ec11-cdb001100000", value = 42, read, write, notify)]
#[descriptor(uuid = "2b21", value = [0x01,0x02,0x03], read)]
pub value: u8,
#[characteristic(uuid = "408814df-5dd4-1f87-ec11-cdb001100000", value = 123.321, read, write, notify)]
#[characteristic(uuid = UUID_U128, value = 123.321, read, write, notify)]
/// Order doesn't matter
#[descriptor(uuid = "2b20", read, value = 42u16.to_le_bytes())] // empty descriptor
pub second: f32,
Expand Down

0 comments on commit 0950a9c

Please sign in to comment.