-
Notifications
You must be signed in to change notification settings - Fork 39
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
add 128 and 16 bit conversions to Uuid #265
add 128 and 16 bit conversions to Uuid #265
Conversation
Appreciated. Observed in my testing: If I use this: const BB_SERVICE_UUID: u128 = 0x_92996405_8C0E_4FA1_A417_67D36995B563; // "92996405-8C0E-4FA1-A417-67D36995B563"; ..nRF Connect shows the service as "UUID: 63b59569-...-...-...-0e8c05649992", ie. in reverse order. |
host/src/types/uuid.rs
Outdated
@@ -20,6 +20,24 @@ impl From<BluetoothUuid16> for Uuid { | |||
} | |||
} | |||
|
|||
impl From<u128> for Uuid { | |||
fn from(val: u128) -> Self { | |||
Uuid::Uuid128(val.to_be_bytes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be .to_le_bytes()
; see https://github.com/embassy-rs/trouble/blob/main/host/src/types/uuid.rs#L76
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm when I add in impl From<u128> for Uuid {
fn from(val: u128) -> Self {
Uuid::Uuid128(val.to_be_bytes())
}
} I'm getting an ambiguity in type conversions elsewhere. This might need to be a NewType like BluetoothUuid16(u16) that we defined in https://github.com/embassy-rs/bt-hci/blob/main/src/uuid.rs |
I keep wondering if we need our own Uuid type here anyway or should fall back onto using https://crates.io/crates/uuid. At the moment we use this in the macro to allow smart conversion from &str to Uuid, but we don't pull it into the main host dependencies. I'm considering making it optional to allow things like: use uuid::{uuid, Uuid};
pub const C2_CHARACTERISTIC_UUID: Uuid = Uuid::from_u128(0x18EE2EF5263D4559959F4F9C429F9D12);
pub const C4_CHARACTERISTIC_UUID: Uuid = uuid!("408813df-5dd4-1f87-ec11-cdb001100000");
/// Matter service
#[gatt_service(uuid = MATTER_BLE_SERVICE_UUID16)]
struct MatterService {
#[characteristic(uuid = C2_CHARACTERISTIC_UUID, write, indicate)]
c2: u8,
#[characteristic(uuid = C4_CHARACTERISTIC_UUID, write, indicate)]
status: bool,
} But then at some point maybe we should just use that Uuid directly. I can't remember if we've discussed this in the past. |
In my similar code (not pushed), I used: fn from(data: u128) -> Self {
Self::Uuid128(data.to_le_bytes())
} Imho, all those |
The newtype kind of defeats the purpose. Given that |
Generally
I think it should be little endian, the spec uses that everywhere from what I've seen. |
You can see from the CI output the ambiguity adding the From adds in, it then stops assuming From is unambiguous. @lulf how do you feel about me pulling out trouble_host::Uuid and replacing with the Uuid crate? I've not dug deep enough to know if there's a good reason for not doing so.
|
I'm not sure I understand, wouldn't just adding From to the type (casting to a u32 and then convert) fix this? The reason I added uuid was that it felt like adding a dependency on the uuid crate is a bit overkill when we don't need to support random generated UUIDs, just representing the bytes. Another point is that there are some restrictions on UUIDs in the BLE spec (i.e. they can't start with the prefixes that are used by spec UUIDs), that I think we should eventually encode into the Uuid type. |
But that's normal as the system now does not know if you want to create a Uuid16 or a Uuid128. dd_service(Service::new(0x1800u16)); |
Cool, if we're happy with having to add the type suffix I can do that, I was trying to avoid doing that as the error could be confusing for users. |
/test |
allow more ways to express Uuids in Gatt Service creation