diff --git a/examples/esp32/Cargo.lock b/examples/esp32/Cargo.lock index 8cc0456f..c8dcf72c 100644 --- a/examples/esp32/Cargo.lock +++ b/examples/esp32/Cargo.lock @@ -789,9 +789,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "indexmap" -version = "2.7.0" +version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" dependencies = [ "equivalent", "hashbrown", @@ -1353,9 +1353,9 @@ checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69" [[package]] name = "unicode-ident" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +checksum = "11cd88e12b17c6494200a9c1b683a04fcac9573ed74cd1b62aeb2727c5592243" [[package]] name = "usb-device" @@ -1369,9 +1369,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.12.0" +version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4" +checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b" [[package]] name = "vcell" diff --git a/examples/serial-hci/Cargo.lock b/examples/serial-hci/Cargo.lock index 57457610..5bf7b102 100644 --- a/examples/serial-hci/Cargo.lock +++ b/examples/serial-hci/Cargo.lock @@ -872,11 +872,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.9" +version = "2.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" +checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" dependencies = [ - "thiserror-impl 2.0.9", + "thiserror-impl 2.0.11", ] [[package]] @@ -892,9 +892,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.9" +version = "2.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" +checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" dependencies = [ "proc-macro2", "quote", @@ -973,7 +973,7 @@ dependencies = [ "log", "num_enum", "static_cell", - "thiserror 2.0.9", + "thiserror 2.0.11", "trouble-host-macros", ] diff --git a/host-macros/src/uuid.rs b/host-macros/src/uuid.rs index d252e5ed..aa246619 100644 --- a/host-macros/src/uuid.rs +++ b/host-macros/src/uuid.rs @@ -11,15 +11,13 @@ use quote::quote; #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Uuid { Uuid16(u16), - Uuid128([u8; 16]), + Uuid128(u128) } impl FromMeta for Uuid { fn from_string(value: &str) -> darling::Result { if let Ok(u) = uuid::Uuid::from_str(value) { - let mut bytes = u.as_bytes().to_owned(); - bytes.reverse(); // Little-endian, as per Bluetooth spec - return Ok(Uuid::Uuid128(bytes)); + return Ok(Uuid::Uuid128(u.as_u128())) // tbd. does this treat big/little the right way? } if value.len() == 4 { @@ -38,13 +36,7 @@ impl quote::ToTokens for Uuid { fn to_tokens(&self, tokens: &mut TokenStream2) { match self { Uuid::Uuid16(u) => tokens.extend(quote!(::trouble_host::types::uuid::Uuid::new_short(#u))), - Uuid::Uuid128(u) => { - let mut s = TokenStream2::new(); - for b in u { - s.extend(quote!(#b,)) - } - tokens.extend(quote!(::trouble_host::types::uuid::Uuid::new_long([#s]))); - } + Uuid::Uuid128(u) => tokens.extend(quote!(::trouble_host::types::uuid::Uuid::new_long(#u))), } } } diff --git a/host/src/types/uuid.rs b/host/src/types/uuid.rs index 23214975..e27f75e4 100644 --- a/host/src/types/uuid.rs +++ b/host/src/types/uuid.rs @@ -45,14 +45,17 @@ impl From for Uuid { } impl Uuid { + // Note: 'new_{short|long}' are 'const fn'; '::from' implementations cannot be. + // Thus, there might be a place for both, though they look superficially overlapping. + /// Create a new 16-bit UUID. pub const fn new_short(val: u16) -> Self { Self::Uuid16(val.to_le_bytes()) } - /// Create a new 128-bit UUID. - pub const fn new_long(val: [u8; 16]) -> Self { - Self::Uuid128(val) + /// Create a new 128-bit UUID. THIS MIGHT NOT NEED TO EXIST + pub const fn new_long(val: u128) -> Self { + Self::Uuid128(val.to_le_bytes()) } /// Copy the UUID bytes into a slice. @@ -95,6 +98,30 @@ impl Uuid { } } +impl From for Uuid { + fn from(data: u16) -> Self { + Uuid::Uuid16(data.to_le_bytes()) + } +} + +impl From for Uuid { + fn from(data: u128) -> Self { + Self::Uuid128(data.to_le_bytes()) + } +} + +impl From<[u8; 2]> for Uuid { + fn from(data: [u8; 2]) -> Self { + Self::Uuid16(data) + } +} + +impl From<[u8; 16]> for Uuid { + fn from(data: [u8; 16]) -> Self { + Self::Uuid128(data) + } +} + impl TryFrom<&[u8]> for Uuid { type Error = crate::Error; diff --git a/host/tests/gatt.rs b/host/tests/gatt.rs index 7d12d128..c7db350f 100644 --- a/host/tests/gatt.rs +++ b/host/tests/gatt.rs @@ -9,10 +9,10 @@ mod common; const CONNECTIONS_MAX: usize = 1; const L2CAP_CHANNELS_MAX: usize = 3; -const SERVICE_UUID: Uuid = Uuid::new_long([ +const SERVICE_UUID: Uuid = Uuid::new_long_from_arr([ 0x00, 0x00, 0x10, 0x00, 0xb0, 0xcd, 0x11, 0xec, 0x87, 0x1f, 0xd4, 0x5d, 0xdf, 0x13, 0x88, 0x40, ]); -const VALUE_UUID: Uuid = Uuid::new_long([ +const VALUE_UUID: Uuid = Uuid::new_long_from_arr([ 0x00, 0x00, 0x10, 0x01, 0xb0, 0xcd, 0x11, 0xec, 0x87, 0x1f, 0xd4, 0x5d, 0xdf, 0x13, 0x88, 0x40, ]); diff --git a/host/tests/gatt_derive.rs b/host/tests/gatt_derive.rs index 301b8202..a275a9a7 100644 --- a/host/tests/gatt_derive.rs +++ b/host/tests/gatt_derive.rs @@ -10,10 +10,10 @@ mod common; const CONNECTIONS_MAX: usize = 1; const L2CAP_CHANNELS_MAX: usize = 3; -const SERVICE_UUID: Uuid = Uuid::new_long([ +const SERVICE_UUID: Uuid = Uuid::new_long_from_arr([ 0x00, 0x00, 0x10, 0x00, 0xb0, 0xcd, 0x11, 0xec, 0x87, 0x1f, 0xd4, 0x5d, 0xdf, 0x13, 0x88, 0x40, ]); -const VALUE_UUID: Uuid = Uuid::new_long([ +const VALUE_UUID: Uuid = Uuid::new_long_from_arr([ 0x00, 0x00, 0x10, 0x01, 0xb0, 0xcd, 0x11, 0xec, 0x87, 0x1f, 0xd4, 0x5d, 0xdf, 0x13, 0x88, 0x40, ]);