1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum Protocol {
10 BleGatt,
12 Classic,
14 Rfcomm,
16 ClassicL2cap,
18 L2cap,
20 Sdp,
22 Profiles,
24 Mesh,
26 RawAcl,
28 RawSco,
30 LeAudio,
32}
33
34#[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 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 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 pub const fn ble_gatt(self) -> bool {
94 self.ble_gatt
95 }
96
97 pub const fn l2cap(self) -> bool {
99 self.l2cap
100 }
101
102 pub const fn classic(self) -> bool {
104 self.classic
105 }
106 pub const fn rfcomm(self) -> bool {
108 self.rfcomm
109 }
110 pub const fn classic_l2cap(self) -> bool {
112 self.classic_l2cap
113 }
114 pub const fn sdp(self) -> bool {
116 self.sdp
117 }
118 pub const fn profiles(self) -> bool {
120 self.profiles
121 }
122 pub const fn mesh(self) -> bool {
124 self.mesh
125 }
126 pub const fn raw_acl(self) -> bool {
128 self.raw_acl
129 }
130 pub const fn raw_sco(self) -> bool {
132 self.raw_sco
133 }
134 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}