1#![doc = include_str!("../README.md")]
2#![no_std]
3
4include!(concat!(env!("OUT_DIR"), "/tables.rs"));
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct Uuid16 {
9 pub uuid: u16,
11 pub kind: UuidKind,
13 pub name: &'static str,
15 pub id: &'static str,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum UuidKind {
23 Service,
25 Characteristic,
27 Descriptor,
29 Declaration,
31 Unit,
33 Member,
35 Sdo,
37 ServiceClass,
39 Protocol,
41 ObjectType,
43 BrowseGroup,
45 MeshProfile,
47}
48
49impl UuidKind {
50 pub const ALL: [UuidKind; 12] = [
52 UuidKind::Service,
53 UuidKind::Characteristic,
54 UuidKind::Descriptor,
55 UuidKind::Declaration,
56 UuidKind::Unit,
57 UuidKind::Member,
58 UuidKind::Sdo,
59 UuidKind::ServiceClass,
60 UuidKind::Protocol,
61 UuidKind::ObjectType,
62 UuidKind::BrowseGroup,
63 UuidKind::MeshProfile,
64 ];
65
66 fn table(self) -> &'static [(u16, &'static str, &'static str)] {
67 match self {
68 UuidKind::Service => SERVICES,
69 UuidKind::Characteristic => CHARACTERISTICS,
70 UuidKind::Descriptor => DESCRIPTORS,
71 UuidKind::Declaration => DECLARATIONS,
72 UuidKind::Unit => UNITS,
73 UuidKind::Member => MEMBERS,
74 UuidKind::Sdo => SDOS,
75 UuidKind::ServiceClass => SERVICE_CLASSES,
76 UuidKind::Protocol => PROTOCOLS,
77 UuidKind::ObjectType => OBJECT_TYPES,
78 UuidKind::BrowseGroup => BROWSE_GROUPS,
79 UuidKind::MeshProfile => MESH_PROFILES,
80 }
81 }
82
83 pub fn as_str(self) -> &'static str {
85 match self {
86 UuidKind::Service => "service",
87 UuidKind::Characteristic => "characteristic",
88 UuidKind::Descriptor => "descriptor",
89 UuidKind::Declaration => "declaration",
90 UuidKind::Unit => "unit",
91 UuidKind::Member => "member",
92 UuidKind::Sdo => "sdo",
93 UuidKind::ServiceClass => "service_class",
94 UuidKind::Protocol => "protocol",
95 UuidKind::ObjectType => "object_type",
96 UuidKind::BrowseGroup => "browse_group",
97 UuidKind::MeshProfile => "mesh_profile",
98 }
99 }
100}
101
102fn find<T: Copy>(table: &'static [(u16, T)], key: u16) -> Option<T> {
103 table
104 .binary_search_by_key(&key, |e| e.0)
105 .ok()
106 .map(|i| table[i].1)
107}
108
109pub fn uuid16_of(kind: UuidKind, uuid: u16) -> Option<Uuid16> {
111 let table = kind.table();
112 table
113 .binary_search_by_key(&uuid, |e| e.0)
114 .ok()
115 .map(|i| Uuid16 {
116 uuid,
117 kind,
118 name: table[i].1,
119 id: table[i].2,
120 })
121}
122
123pub fn uuid16(uuid: u16) -> Option<Uuid16> {
126 UuidKind::ALL.iter().find_map(|k| uuid16_of(*k, uuid))
127}
128
129pub fn service(uuid: u16) -> Option<Uuid16> {
131 uuid16_of(UuidKind::Service, uuid)
132}
133
134pub fn characteristic(uuid: u16) -> Option<Uuid16> {
136 uuid16_of(UuidKind::Characteristic, uuid)
137}
138
139pub fn descriptor(uuid: u16) -> Option<Uuid16> {
141 uuid16_of(UuidKind::Descriptor, uuid)
142}
143
144pub fn unit(uuid: u16) -> Option<Uuid16> {
146 uuid16_of(UuidKind::Unit, uuid)
147}
148
149pub fn member(uuid: u16) -> Option<Uuid16> {
151 uuid16_of(UuidKind::Member, uuid)
152}
153
154pub fn company(id: u16) -> Option<&'static str> {
156 find(COMPANIES, id)
157}
158
159pub fn ad_type(value: u8) -> Option<&'static str> {
161 find(AD_TYPES, u16::from(value))
162}
163
164pub fn uri_scheme(code: u16) -> Option<&'static str> {
166 find(URI_SCHEMES, code)
167}
168
169pub fn core_version(value: u8) -> Option<&'static str> {
171 find(CORE_VERSIONS, u16::from(value))
172}
173
174pub fn psm(value: u16) -> Option<&'static str> {
176 find(PSMS, value)
177}
178
179pub fn format_type(value: u8) -> Option<(&'static str, &'static str)> {
181 let key = u16::from(value);
182 FORMAT_TYPES
183 .binary_search_by_key(&key, |e| e.0)
184 .ok()
185 .map(|i| (FORMAT_TYPES[i].1, FORMAT_TYPES[i].2))
186}
187
188pub fn mesh_opcode(opcode: u32) -> Option<&'static str> {
191 MESH_OPCODES
192 .binary_search_by_key(&opcode, |e| e.0)
193 .ok()
194 .map(|i| MESH_OPCODES[i].1)
195}
196
197pub fn mesh_model(id: u16) -> Option<&'static str> {
199 find(MESH_MODELS, id)
200}
201
202#[derive(Debug, Clone, Copy, PartialEq, Eq)]
204pub struct Appearance {
205 pub value: u16,
207 pub category: Option<&'static str>,
209 pub subcategory: Option<&'static str>,
212}
213
214pub fn appearance(value: u16) -> Appearance {
216 Appearance {
217 value,
218 category: find(APPEARANCE_CATEGORIES, value >> 6),
219 subcategory: find(APPEARANCE_SUBCATEGORIES, value),
220 }
221}
222
223#[cfg(test)]
224mod tests {
225 use super::*;
226
227 #[test]
228 fn tables_are_sorted_and_found() {
229 for kind in UuidKind::ALL {
230 let t = kind.table();
231 assert!(t.windows(2).all(|w| w[0].0 < w[1].0), "{kind:?}");
232 }
233 assert!(COMPANIES.len() > 3000);
234 assert_eq!(company(0x0000), Some("Ericsson AB"));
235 assert_eq!(
236 service(0x180f).unwrap().id,
237 "org.bluetooth.service.battery_service"
238 );
239 assert_eq!(characteristic(0x2a19).unwrap().name, "Battery Level");
240 assert_eq!(
241 descriptor(0x2902).unwrap().name,
242 "Client Characteristic Configuration"
243 );
244 assert_eq!(member(0xfeaa).unwrap().name, "Google LLC");
245 assert_eq!(ad_type(0xff), Some("Manufacturer Specific Data"));
246 assert_eq!(ad_type(0x01), Some("Flags"));
247 assert_eq!(uri_scheme(0x0017), Some("https:"));
248 assert!(core_version(0x0d).unwrap().contains("5.4"));
249 assert_eq!(psm(0x0001), Some("SDP"));
250 assert_eq!(format_type(0x06).unwrap().0, "uint16");
251 let a = appearance(0x00c1);
252 assert_eq!(a.category, Some("Watch"));
253 assert_eq!(a.subcategory, Some("Sports Watch"));
254 assert_eq!(uuid16(0x2a19).unwrap().kind, UuidKind::Characteristic);
255 assert_eq!(mesh_opcode(0x8201), Some("Generic OnOff Get"));
256 assert_eq!(mesh_opcode(0x00), Some("Config AppKey Add"));
257 assert_eq!(mesh_model(0x0000), Some("Configuration Server"));
258 assert_eq!(mesh_model(0x1000), Some("Generic OnOff Server"));
259 }
260}