bluetooth_host/gatt/
mod.rs1pub mod client;
4pub mod server;
5
6use bluetooth_adv::Uuid;
7use serde::{Deserialize, Serialize};
8
9pub mod uuid {
11 pub const PRIMARY_SERVICE: u16 = 0x2800;
13 pub const SECONDARY_SERVICE: u16 = 0x2801;
15 pub const INCLUDE: u16 = 0x2802;
17 pub const CHARACTERISTIC: u16 = 0x2803;
19 pub const EXTENDED_PROPERTIES: u16 = 0x2900;
21 pub const USER_DESCRIPTION: u16 = 0x2901;
23 pub const CCCD: u16 = 0x2902;
25 pub const SCCD: u16 = 0x2903;
27 pub const PRESENTATION_FORMAT: u16 = 0x2904;
29 pub const AGGREGATE_FORMAT: u16 = 0x2905;
31 pub const GAP: u16 = 0x1800;
33 pub const GATT: u16 = 0x1801;
35 pub const DEVICE_NAME: u16 = 0x2a00;
37 pub const APPEARANCE: u16 = 0x2a01;
39 pub const SERVICE_CHANGED: u16 = 0x2a05;
41 pub const CENTRAL_ADDRESS_RESOLUTION: u16 = 0x2aa6;
43 pub const CLIENT_SUPPORTED_FEATURES: u16 = 0x2b29;
45 pub const DATABASE_HASH: u16 = 0x2b2a;
47 pub const SERVER_SUPPORTED_FEATURES: u16 = 0x2b3a;
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
53#[serde(transparent)]
54pub struct Properties(pub u8);
55
56impl Properties {
57 pub const BROADCAST: u8 = 0x01;
59 pub const READ: u8 = 0x02;
61 pub const WRITE_WITHOUT_RESPONSE: u8 = 0x04;
63 pub const WRITE: u8 = 0x08;
65 pub const NOTIFY: u8 = 0x10;
67 pub const INDICATE: u8 = 0x20;
69 pub const SIGNED_WRITE: u8 = 0x40;
71 pub const EXTENDED: u8 = 0x80;
73
74 pub fn has(&self, bit: u8) -> bool {
76 self.0 & bit != 0
77 }
78
79 pub fn names(&self) -> Vec<&'static str> {
81 [
82 (Self::BROADCAST, "broadcast"),
83 (Self::READ, "read"),
84 (Self::WRITE_WITHOUT_RESPONSE, "write_without_response"),
85 (Self::WRITE, "write"),
86 (Self::NOTIFY, "notify"),
87 (Self::INDICATE, "indicate"),
88 (Self::SIGNED_WRITE, "authenticated_signed_writes"),
89 (Self::EXTENDED, "extended_properties"),
90 ]
91 .iter()
92 .filter(|(b, _)| self.has(*b))
93 .map(|(_, n)| *n)
94 .collect()
95 }
96
97 pub fn from_names<S: AsRef<str>>(names: &[S]) -> Result<Properties, String> {
99 let mut p = 0;
100 for n in names {
101 p |= match n.as_ref() {
102 "broadcast" => Self::BROADCAST,
103 "read" => Self::READ,
104 "write_without_response" => Self::WRITE_WITHOUT_RESPONSE,
105 "write" => Self::WRITE,
106 "notify" => Self::NOTIFY,
107 "indicate" => Self::INDICATE,
108 "authenticated_signed_writes" => Self::SIGNED_WRITE,
109 other => return Err(format!("unknown characteristic property {other:?}")),
110 };
111 }
112 Ok(Properties(p))
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
118pub struct Service {
119 pub uuid: Uuid,
121 #[serde(skip_serializing_if = "Option::is_none")]
123 pub name: Option<&'static str>,
124 pub primary: bool,
126 pub start: u16,
128 pub end: u16,
130 pub characteristics: Vec<Characteristic>,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
136pub struct Characteristic {
137 pub uuid: Uuid,
139 #[serde(skip_serializing_if = "Option::is_none")]
141 pub name: Option<&'static str>,
142 pub declaration: u16,
144 pub value_handle: u16,
146 pub properties: Properties,
148 pub end: u16,
150 pub descriptors: Vec<Descriptor>,
152}
153
154impl Characteristic {
155 pub fn cccd(&self) -> Option<u16> {
157 self.descriptors
158 .iter()
159 .find(|d| d.uuid == Uuid::Uuid16(uuid::CCCD))
160 .map(|d| d.handle)
161 }
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
166pub struct Descriptor {
167 pub uuid: Uuid,
169 pub handle: u16,
171}
172
173pub(crate) fn uuid_bytes(u: &Uuid) -> Vec<u8> {
174 match u.shortened() {
175 Uuid::Uuid16(v) => v.to_le_bytes().to_vec(),
176 other => {
178 let mut a = other.to_u128_bytes();
179 a.reverse();
180 a.to_vec()
181 }
182 }
183}