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

Conversation

lure23
Copy link
Contributor

@lure23 lure23 commented Jan 23, 2025

Related to #248 , I'd like to see both 16- and 128-bit Uuid processing logic to be alike. I think this is a good deed for maintainability and clarity.

They currently differ in a few places, and I'd like to discuss those within the PR. If this gets thumbs, could massage the PR to mergeable quality.

}

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.

@@ -9,7 +9,7 @@ use crate::codec::{Decode, Encode, Error, Type};
#[derive(Debug, PartialEq, Clone)]
pub enum Uuid {
/// 16-bit UUID
Uuid16([u8; 2]),
Uuid16([u8; 2]), // Q: why here as slice, when 'u16' in 'host-macros'?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason why the inner contents are arrays here, but u16 in host-macros?

Should we make them both the same way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lulf Would appreciate your comment on this.

Choose a reason for hiding this comment

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

[u8; 2] should be preferred because it has align of 1 instead of align of 2.

Self::Uuid128(val.to_le_bytes())
}
// tbd. this is an API change
pub const fn new_long_from_arr(val: [u8; 16]) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two places (below, in the tests) that use this function, but it's also public.

Is there a need for both; can tests use long hex constants (instead of strings)? How would building from arrays / strings be unified, perhaps using From, see #248

Choose a reason for hiding this comment

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

u128s are fine in a const context but tend to be inefficient at runtime on embedded. I think both of these constructors can be useful, but I'd probably call them something like from_u128 and from_bytes.

@@ -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.

@jamessizeland
Copy link
Collaborator

Looking good, make sure you check that the conversions are equivalent (maybe a test temporarily to show that?) or better if you think we've got an endienness bug?

@lure23
Copy link
Contributor Author

lure23 commented Jan 23, 2025

@jamessizeland Do you know why the CI build is failing? I have no idea what these are:

Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Diff in /home/runner/work/trouble/trouble/host/src/types/uuid.rs:9:
 #[derive(Debug, PartialEq, Clone)]
 pub enum Uuid {
     /// 16-bit UUID
-    Uuid16([u8; 2]),    // Q: why here as slice, when 'u16' in 'host-macros'?
+    Uuid16([u8; 2]), // Q: why here as slice, when 'u16' in 'host-macros'?
     /// 128-bit UUID
     Uuid128([u8; 16]),
 }
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.
Warning: can't set `imports_granularity = Module`, unstable features are only available in nightly channel.
Warning: can't set `group_imports = StdExternalCrate`, unstable features are only available in nightly channel.

@lure23 lure23 changed the title suggestion: equal processing for both 16- and 128-bit Uuids suggestion: equal processing for both 16- and 128-bit Uuids // WIP Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants