1use uuid::Uuid;
2
3const BLUETOOTH_BASE_UUID: u128 = 0x00000000_0000_1000_8000_00805f9b34fb;
4const BLUETOOTH_BASE_MASK: u128 = 0x00000000_ffff_ffff_ffff_ffffffffffff;
5const BLUETOOTH_BASE_MASK_16: u128 = 0xffff0000_ffff_ffff_ffff_ffffffffffff;
6
7pub const fn uuid_from_u32(short: u32) -> Uuid {
10 Uuid::from_u128(BLUETOOTH_BASE_UUID | ((short as u128) << 96))
11}
12
13pub const fn uuid_from_u16(short: u16) -> Uuid {
16 uuid_from_u32(short as u32)
17}
18
19pub trait BleUuid {
21 fn to_ble_u32(&self) -> Option<u32>;
23
24 fn to_ble_u16(&self) -> Option<u16>;
27
28 fn succinctly(&self) -> String;
30}
31
32impl BleUuid for Uuid {
33 fn to_ble_u32(&self) -> Option<u32> {
34 let value = self.as_u128();
35 if value & BLUETOOTH_BASE_MASK == BLUETOOTH_BASE_UUID {
36 Some((value >> 96) as u32)
37 } else {
38 None
39 }
40 }
41
42 fn to_ble_u16(&self) -> Option<u16> {
43 let value = self.as_u128();
44 if value & BLUETOOTH_BASE_MASK_16 == BLUETOOTH_BASE_UUID {
45 Some((value >> 96) as u16)
46 } else {
47 None
48 }
49 }
50
51 fn succinctly(&self) -> String {
52 if let Some(uuid16) = self.to_ble_u16() {
53 format!("{:#04x}", uuid16)
54 } else if let Some(uuid32) = self.to_ble_u32() {
55 format!("{:#06x}", uuid32)
56 } else {
57 self.to_string()
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn uuid_from_u32_test() {
68 assert_eq!(
69 uuid_from_u32(0x11223344),
70 Uuid::parse_str("11223344-0000-1000-8000-00805f9b34fb").unwrap()
71 );
72 }
73
74 #[test]
75 fn uuid_from_u16_test() {
76 assert_eq!(
77 uuid_from_u16(0x1122),
78 Uuid::parse_str("00001122-0000-1000-8000-00805f9b34fb").unwrap()
79 );
80 }
81
82 #[test]
83 fn uuid_to_from_u16_success() {
84 let uuid = Uuid::parse_str("00001234-0000-1000-8000-00805f9b34fb").unwrap();
85 assert_eq!(uuid_from_u16(uuid.to_ble_u16().unwrap()), uuid);
86 }
87
88 #[test]
89 fn uuid_to_from_u32_success() {
90 let uuid = Uuid::parse_str("12345678-0000-1000-8000-00805f9b34fb").unwrap();
91 assert_eq!(uuid_from_u32(uuid.to_ble_u32().unwrap()), uuid);
92 }
93
94 #[test]
95 fn uuid_to_u16_fail() {
96 assert_eq!(
97 Uuid::parse_str("12345678-0000-1000-8000-00805f9b34fb")
98 .unwrap()
99 .to_ble_u16(),
100 None
101 );
102 assert_eq!(
103 Uuid::parse_str("12340000-0000-1000-8000-00805f9b34fb")
104 .unwrap()
105 .to_ble_u16(),
106 None
107 );
108 assert_eq!(Uuid::nil().to_ble_u16(), None);
109 }
110
111 #[test]
112 fn uuid_to_u32_fail() {
113 assert_eq!(
114 Uuid::parse_str("12345678-9000-1000-8000-00805f9b34fb")
115 .unwrap()
116 .to_ble_u32(),
117 None
118 );
119 assert_eq!(Uuid::nil().to_ble_u32(), None);
120 }
121
122 #[test]
123 fn succinctly_u16() {
124 let uuid = uuid_from_u16(0x1122);
125 assert_eq!(uuid.succinctly(), "0x1122");
126 }
127
128 #[test]
129 fn succinctly_u32() {
130 let uuid = uuid_from_u32(0x11223344);
131 assert_eq!(uuid.succinctly(), "0x11223344");
132 }
133
134 #[test]
135 fn succinctly_long() {
136 let uuid_str = "12345678-9000-1000-8000-00805f9b34fb";
137 let uuid = Uuid::parse_str(uuid_str).unwrap();
138 assert_eq!(uuid.succinctly(), uuid_str);
139 }
140}