Skip to main content

webbluetooth_core/
capabilities.rs

1//! Compile-time Bluetooth protocol capability reporting.
2//!
3//! This is deliberately descriptive: a capability being `false` means the
4//! current build does not expose that protocol. It does not claim that a
5//! controller or operating system could never support it.
6
7/// A protocol family that can be queried from [`ProtocolCapabilities`].
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum Protocol {
10    /// Bluetooth Low Energy GATT client/server operations.
11    BleGatt,
12    /// Bluetooth Classic BR/EDR transport.
13    Classic,
14    /// RFCOMM byte streams.
15    Rfcomm,
16    /// Bluetooth Classic L2CAP channels.
17    ClassicL2cap,
18    /// BLE/Classic L2CAP exposed by the public transport layer.
19    L2cap,
20    /// SDP service discovery.
21    Sdp,
22    /// Profile implementations such as A2DP, AVRCP, HFP, HID, and PAN.
23    Profiles,
24    /// Bluetooth Mesh.
25    Mesh,
26    /// Raw ACL packet transport.
27    RawAcl,
28    /// Raw SCO/eSCO packet transport.
29    RawSco,
30    /// LE Isochronous Channels and LE Audio.
31    LeAudio,
32}
33
34/// Protocol capabilities for the current target and Cargo feature set.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct ProtocolCapabilities {
37    ble_gatt: bool,
38    classic: bool,
39    rfcomm: bool,
40    l2cap: bool,
41    classic_l2cap: bool,
42    sdp: bool,
43    profiles: bool,
44    mesh: bool,
45    raw_acl: bool,
46    raw_sco: bool,
47    le_audio: bool,
48}
49
50impl ProtocolCapabilities {
51    /// Construct the capabilities for this build.
52    pub const fn current() -> Self {
53        Self {
54            ble_gatt: true,
55            classic: cfg!(feature = "classic"),
56            rfcomm: cfg!(all(
57                feature = "rfcomm",
58                any(
59                    target_os = "linux",
60                    target_os = "android",
61                    target_os = "windows"
62                )
63            )),
64            l2cap: !cfg!(any(target_os = "windows", target_arch = "wasm32")),
65            classic_l2cap: cfg!(feature = "classic-l2cap"),
66            sdp: cfg!(feature = "classic"),
67            profiles: cfg!(feature = "profiles"),
68            mesh: cfg!(feature = "mesh"),
69            raw_acl: cfg!(all(feature = "raw-acl", target_os = "linux")),
70            raw_sco: cfg!(all(feature = "raw-sco", target_os = "linux")),
71            le_audio: cfg!(feature = "le-audio"),
72        }
73    }
74
75    /// Whether a protocol family is exposed by this build.
76    pub const fn supports(self, protocol: Protocol) -> bool {
77        match protocol {
78            Protocol::BleGatt => self.ble_gatt,
79            Protocol::L2cap => self.l2cap,
80            Protocol::Classic => self.classic,
81            Protocol::Rfcomm => self.rfcomm,
82            Protocol::ClassicL2cap => self.classic_l2cap,
83            Protocol::Sdp => self.sdp,
84            Protocol::Profiles => self.profiles,
85            Protocol::Mesh => self.mesh,
86            Protocol::RawAcl => self.raw_acl,
87            Protocol::RawSco => self.raw_sco,
88            Protocol::LeAudio => self.le_audio,
89        }
90    }
91
92    /// Whether BLE GATT is enabled.
93    pub const fn ble_gatt(self) -> bool {
94        self.ble_gatt
95    }
96
97    /// Whether public L2CAP channels are enabled.
98    pub const fn l2cap(self) -> bool {
99        self.l2cap
100    }
101
102    /// Whether Classic BR/EDR is enabled.
103    pub const fn classic(self) -> bool {
104        self.classic
105    }
106    /// Whether RFCOMM is enabled.
107    pub const fn rfcomm(self) -> bool {
108        self.rfcomm
109    }
110    /// Whether Classic L2CAP is enabled.
111    pub const fn classic_l2cap(self) -> bool {
112        self.classic_l2cap
113    }
114    /// Whether SDP support is enabled.
115    pub const fn sdp(self) -> bool {
116        self.sdp
117    }
118    /// Whether profile APIs are enabled.
119    pub const fn profiles(self) -> bool {
120        self.profiles
121    }
122    /// Whether Mesh support is enabled.
123    pub const fn mesh(self) -> bool {
124        self.mesh
125    }
126    /// Whether raw ACL is enabled.
127    pub const fn raw_acl(self) -> bool {
128        self.raw_acl
129    }
130    /// Whether raw SCO/eSCO is enabled.
131    pub const fn raw_sco(self) -> bool {
132        self.raw_sco
133    }
134    /// Whether LE Audio support is enabled.
135    pub const fn le_audio(self) -> bool {
136        self.le_audio
137    }
138}
139
140#[cfg(test)]
141mod tests {
142    use super::*;
143
144    #[test]
145    fn current_capabilities_match_feature_flags() {
146        let capabilities = ProtocolCapabilities::current();
147        assert!(capabilities.ble_gatt());
148        assert_eq!(
149            capabilities.l2cap(),
150            !cfg!(any(target_os = "windows", target_arch = "wasm32"))
151        );
152        assert_eq!(capabilities.classic(), cfg!(feature = "classic"));
153        assert_eq!(
154            capabilities.rfcomm(),
155            cfg!(all(
156                feature = "rfcomm",
157                any(
158                    target_os = "linux",
159                    target_os = "android",
160                    target_os = "windows"
161                )
162            ))
163        );
164        assert_eq!(
165            capabilities.classic_l2cap(),
166            cfg!(feature = "classic-l2cap")
167        );
168        assert_eq!(capabilities.sdp(), cfg!(feature = "classic"));
169        assert_eq!(capabilities.profiles(), cfg!(feature = "profiles"));
170        assert_eq!(capabilities.mesh(), cfg!(feature = "mesh"));
171        assert_eq!(
172            capabilities.raw_acl(),
173            cfg!(all(feature = "raw-acl", target_os = "linux"))
174        );
175        assert_eq!(
176            capabilities.raw_sco(),
177            cfg!(all(feature = "raw-sco", target_os = "linux"))
178        );
179        assert_eq!(capabilities.le_audio(), cfg!(feature = "le-audio"));
180
181        assert!(capabilities.supports(Protocol::BleGatt));
182        assert_eq!(
183            capabilities.supports(Protocol::Classic),
184            cfg!(feature = "classic")
185        );
186        assert_eq!(
187            capabilities.supports(Protocol::Rfcomm),
188            cfg!(all(
189                feature = "rfcomm",
190                any(
191                    target_os = "linux",
192                    target_os = "android",
193                    target_os = "windows"
194                )
195            ))
196        );
197        assert_eq!(
198            capabilities.supports(Protocol::ClassicL2cap),
199            cfg!(feature = "classic-l2cap")
200        );
201        assert_eq!(
202            capabilities.supports(Protocol::L2cap),
203            !cfg!(any(target_os = "windows", target_arch = "wasm32"))
204        );
205        assert_eq!(
206            capabilities.supports(Protocol::Sdp),
207            cfg!(feature = "classic")
208        );
209        assert_eq!(
210            capabilities.supports(Protocol::Profiles),
211            cfg!(feature = "profiles")
212        );
213        assert_eq!(
214            capabilities.supports(Protocol::Mesh),
215            cfg!(feature = "mesh")
216        );
217        assert_eq!(
218            capabilities.supports(Protocol::RawAcl),
219            cfg!(all(feature = "raw-acl", target_os = "linux"))
220        );
221        assert_eq!(
222            capabilities.supports(Protocol::RawSco),
223            cfg!(all(feature = "raw-sco", target_os = "linux"))
224        );
225        assert_eq!(
226            capabilities.supports(Protocol::LeAudio),
227            cfg!(feature = "le-audio")
228        );
229    }
230}