Skip to main content

bluetooth_numbers/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3
4include!(concat!(env!("OUT_DIR"), "/tables.rs"));
5
6/// A 16-bit UUID's assignment.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct Uuid16 {
9    /// The UUID.
10    pub uuid: u16,
11    /// What kind of thing it names.
12    pub kind: UuidKind,
13    /// The SIG's name for it.
14    pub name: &'static str,
15    /// The SIG's identifier, such as `org.bluetooth.service.battery_service`,
16    /// or empty where the SIG gives none (member and SDO UUIDs).
17    pub id: &'static str,
18}
19
20/// The kinds of 16-bit UUID the SIG assigns.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub enum UuidKind {
23    /// A GATT service.
24    Service,
25    /// A GATT characteristic.
26    Characteristic,
27    /// A GATT descriptor.
28    Descriptor,
29    /// A GATT declaration (primary service, characteristic, ...).
30    Declaration,
31    /// A unit, used in the Characteristic Presentation Format.
32    Unit,
33    /// A UUID assigned to a SIG member company.
34    Member,
35    /// A UUID assigned to a standards body.
36    Sdo,
37    /// A BR/EDR service class (SDP).
38    ServiceClass,
39    /// A protocol identifier (SDP).
40    Protocol,
41    /// An Object Transfer object type.
42    ObjectType,
43    /// An SDP browse group.
44    BrowseGroup,
45    /// A Mesh profile.
46    MeshProfile,
47}
48
49impl UuidKind {
50    /// Every kind, in the order [`uuid16`] searches them.
51    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    /// The kind's name, in snake case, as used in JSON output.
84    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
109/// A 16-bit UUID of one kind.
110pub 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
123/// A 16-bit UUID of any kind; services first, then characteristics and so on
124/// (see [`UuidKind::ALL`]). The SIG's ranges do not overlap in practice.
125pub fn uuid16(uuid: u16) -> Option<Uuid16> {
126    UuidKind::ALL.iter().find_map(|k| uuid16_of(*k, uuid))
127}
128
129/// A GATT service.
130pub fn service(uuid: u16) -> Option<Uuid16> {
131    uuid16_of(UuidKind::Service, uuid)
132}
133
134/// A GATT characteristic.
135pub fn characteristic(uuid: u16) -> Option<Uuid16> {
136    uuid16_of(UuidKind::Characteristic, uuid)
137}
138
139/// A GATT descriptor.
140pub fn descriptor(uuid: u16) -> Option<Uuid16> {
141    uuid16_of(UuidKind::Descriptor, uuid)
142}
143
144/// A unit (Characteristic Presentation Format).
145pub fn unit(uuid: u16) -> Option<Uuid16> {
146    uuid16_of(UuidKind::Unit, uuid)
147}
148
149/// A UUID assigned to a member company, such as `0xFEAA` (Google, Eddystone).
150pub fn member(uuid: u16) -> Option<Uuid16> {
151    uuid16_of(UuidKind::Member, uuid)
152}
153
154/// The company a Company Identifier is assigned to (manufacturer specific data, LMP).
155pub fn company(id: u16) -> Option<&'static str> {
156    find(COMPANIES, id)
157}
158
159/// The name of an advertising data (AD) type.
160pub fn ad_type(value: u8) -> Option<&'static str> {
161    find(AD_TYPES, u16::from(value))
162}
163
164/// The scheme of the URI AD type's code point, such as `"https:"`.
165pub fn uri_scheme(code: u16) -> Option<&'static str> {
166    find(URI_SCHEMES, code)
167}
168
169/// A Bluetooth Core Specification version, as reported in HCI and LMP version fields.
170pub fn core_version(value: u8) -> Option<&'static str> {
171    find(CORE_VERSIONS, u16::from(value))
172}
173
174/// The protocol or profile assigned an L2CAP PSM.
175pub fn psm(value: u16) -> Option<&'static str> {
176    find(PSMS, value)
177}
178
179/// A Characteristic Presentation Format type: its short name and description.
180pub 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
188/// The name of a Mesh message opcode (1, 2 or 3 octets, as a number:
189/// `0x8201` is Generic OnOff Get).
190pub 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
197/// The name of a SIG Mesh model.
198pub fn mesh_model(id: u16) -> Option<&'static str> {
199    find(MESH_MODELS, id)
200}
201
202/// An Appearance value, split into its category and subcategory.
203#[derive(Debug, Clone, Copy, PartialEq, Eq)]
204pub struct Appearance {
205    /// The raw 16-bit value.
206    pub value: u16,
207    /// The category's name (bits 15 to 6), when assigned.
208    pub category: Option<&'static str>,
209    /// The subcategory's name (bits 5 to 0), when assigned; `None` for the
210    /// generic subcategory 0.
211    pub subcategory: Option<&'static str>,
212}
213
214/// Splits and names an Appearance value.
215pub 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}