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

suggestion: equal processing for both 16- and 128-bit Uuids // WIP #259

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions examples/esp32/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions examples/serial-hci/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions host-macros/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that here, endianess conversion is non-conditional. This seems like a bug to me, but those would be removed lines in the PR.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is correct. The uuid crate stores UUIDs in big-endian order, but the bluetooth spec requires UUIDs to be transmitted in little-endian order.

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 {
Expand All @@ -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))),
}
}
}
33 changes: 30 additions & 3 deletions host/src/types/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ impl From<u16> 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.
Expand Down Expand Up @@ -95,6 +98,30 @@ impl Uuid {
}
}

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

impl From<u128> 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;

Expand Down
4 changes: 2 additions & 2 deletions host/tests/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Copy link
Contributor Author

@lure23 lure23 Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another approach is:

const SERVICE_UUID: Uuid = Uuid::new_long(
   0x00001000b0cd11ec871fd45ddf138840
);

If turning to this, care must be taken to the endianness. Should the value be as above, or reversed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match current behavior it should be reversed.

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,
]);

Expand Down
4 changes: 2 additions & 2 deletions host/tests/gatt_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down
Loading