1use crate::{AsHciBytes, ByteAlignedValue, FixedSizeValue, FromHciBytes, FromHciBytesError, WriteHci};
4
5mod classic;
6mod cmd_mask;
7mod event_masks;
8mod feature_masks;
9mod le;
10mod macros;
11mod primitives;
12mod status;
13
14pub use classic::*;
15pub use cmd_mask::*;
16pub use event_masks::*;
17pub use feature_masks::*;
18pub use le::*;
19pub(crate) use macros::{param, param_slice};
20pub use status::*;
21
22#[repr(transparent)]
24#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[cfg_attr(feature = "defmt", derive(defmt::Format))]
26pub struct RemainingBytes<'a>(&'a [u8]);
27
28impl core::ops::Deref for RemainingBytes<'_> {
29 type Target = [u8];
30
31 fn deref(&self) -> &Self::Target {
32 self.0
33 }
34}
35
36impl WriteHci for RemainingBytes<'_> {
37 #[inline(always)]
38 fn size(&self) -> usize {
39 self.0.len()
40 }
41
42 #[inline(always)]
43 fn write_hci<W: embedded_io::Write>(&self, mut writer: W) -> Result<(), W::Error> {
44 writer.write_all(self.0)
45 }
46
47 #[inline(always)]
48 async fn write_hci_async<W: embedded_io_async::Write>(&self, mut writer: W) -> Result<(), W::Error> {
49 writer.write_all(self.0).await
50 }
51}
52
53impl AsHciBytes for RemainingBytes<'_> {
54 fn as_hci_bytes(&self) -> &[u8] {
55 self.0
56 }
57}
58
59impl<'a> FromHciBytes<'a> for RemainingBytes<'a> {
60 fn from_hci_bytes(data: &'a [u8]) -> Result<(Self, &'a [u8]), FromHciBytesError> {
61 Ok((RemainingBytes(data), &[]))
62 }
63}
64
65impl<'a> RemainingBytes<'a> {
66 pub(crate) fn into_inner(self) -> &'a [u8] {
67 self.0
68 }
69}
70
71param!(struct BdAddr([u8; 6]));
72
73impl BdAddr {
74 pub fn new(val: [u8; 6]) -> Self {
76 Self(val)
77 }
78
79 pub fn raw(&self) -> &[u8] {
81 &self.0[..]
82 }
83}
84
85unsafe impl ByteAlignedValue for BdAddr {}
86
87impl<'de> crate::FromHciBytes<'de> for &'de BdAddr {
88 #[inline(always)]
89 fn from_hci_bytes(data: &'de [u8]) -> Result<(Self, &'de [u8]), crate::FromHciBytesError> {
90 <BdAddr as crate::ByteAlignedValue>::ref_from_hci_bytes(data)
91 }
92}
93
94param!(struct ConnHandle(u16));
95
96impl ConnHandle {
97 pub fn new(val: u16) -> Self {
99 assert!(val <= 0xeff);
100 Self(val)
101 }
102
103 pub fn raw(&self) -> u16 {
105 self.0
106 }
107}
108
109#[repr(transparent)]
111#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
112#[cfg_attr(feature = "defmt", derive(defmt::Format))]
113pub struct Duration<const US: u32 = 625>(u16);
114
115unsafe impl<const US: u32> FixedSizeValue for Duration<US> {
116 #[inline(always)]
117 fn is_valid(_data: &[u8]) -> bool {
118 true
119 }
120}
121
122impl<const US: u32> Duration<US> {
123 #[inline(always)]
124 pub const fn from_u16(val: u16) -> Self {
126 Self(val)
127 }
128
129 #[inline(always)]
131 pub fn from_micros(val: u64) -> Self {
132 Self::from_u16(unwrap!((val / u64::from(US)).try_into()))
133 }
134
135 #[inline(always)]
137 pub fn from_millis(val: u32) -> Self {
138 Self::from_micros(u64::from(val) * 1000)
139 }
140
141 #[inline(always)]
143 pub fn from_secs(val: u32) -> Self {
144 Self::from_micros(u64::from(val) * 1_000_000)
145 }
146
147 #[inline(always)]
149 pub fn as_u16(&self) -> u16 {
150 self.0
151 }
152
153 #[inline(always)]
155 pub fn as_micros(&self) -> u64 {
156 u64::from(self.as_u16()) * u64::from(US)
157 }
158
159 #[inline(always)]
161 pub fn as_millis(&self) -> u32 {
162 unwrap!((self.as_micros() / 1000).try_into())
163 }
164
165 #[inline(always)]
167 pub fn as_secs(&self) -> u32 {
168 (self.as_micros() / 1_000_000) as u32
170 }
171}
172
173#[cfg(feature = "embassy-time")]
174impl<const US: u32> From<embassy_time::Duration> for Duration<US> {
175 fn from(duration: embassy_time::Duration) -> Self {
176 Self::from_micros(duration.as_micros())
177 }
178}
179
180#[repr(transparent)]
182#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
183#[cfg_attr(feature = "defmt", derive(defmt::Format))]
184pub struct ExtDuration<const US: u16 = 1>([u8; 3]);
185
186unsafe impl<const US: u16> FixedSizeValue for ExtDuration<US> {
187 #[inline(always)]
188 fn is_valid(_data: &[u8]) -> bool {
189 true
190 }
191}
192
193unsafe impl<const US: u16> ByteAlignedValue for ExtDuration<US> {}
194
195impl<'de, const US: u16> FromHciBytes<'de> for &'de ExtDuration<US> {
196 #[inline(always)]
197 fn from_hci_bytes(data: &'de [u8]) -> Result<(Self, &'de [u8]), crate::FromHciBytesError> {
198 <ExtDuration<US> as crate::ByteAlignedValue>::ref_from_hci_bytes(data)
199 }
200}
201
202impl<const US: u16> ExtDuration<US> {
203 #[inline(always)]
205 pub fn from_u32(val: u32) -> Self {
206 assert!(val < (1 << 24));
207 Self(*unwrap!(val.to_le_bytes().first_chunk()))
208 }
209
210 #[inline(always)]
212 pub fn from_micros(val: u64) -> Self {
213 Self::from_u32(unwrap!((val / u64::from(US)).try_into()))
214 }
215
216 #[inline(always)]
218 pub fn from_millis(val: u32) -> Self {
219 Self::from_micros(u64::from(val) * 1000)
220 }
221
222 #[inline(always)]
224 pub fn from_secs(val: u32) -> Self {
225 Self::from_micros(u64::from(val) * 1_000_000)
226 }
227
228 #[inline(always)]
230 pub fn as_micros(&self) -> u64 {
231 u64::from_le_bytes([self.0[0], self.0[1], self.0[2], 0, 0, 0, 0, 0]) * u64::from(US)
232 }
233
234 #[inline(always)]
236 pub fn as_millis(&self) -> u32 {
237 (self.as_micros() / 1000) as u32
239 }
240
241 #[inline(always)]
243 pub fn as_secs(&self) -> u32 {
244 (self.as_micros() / 1_000_000) as u32
246 }
247}
248
249#[cfg(feature = "embassy-time")]
250impl<const US: u16> From<embassy_time::Duration> for ExtDuration<US> {
251 fn from(duration: embassy_time::Duration) -> Self {
252 Self::from_micros(duration.as_micros())
253 }
254}
255
256param!(
257 enum DisconnectReason {
258 AuthenticationFailure = 0x05,
259 RemoteUserTerminatedConn = 0x13,
260 RemoteDeviceTerminatedConnLowResources = 0x14,
261 RemoteDeviceTerminatedConnPowerOff = 0x15,
262 UnsupportedRemoteFeature = 0x1A,
263 PairingWithUnitKeyNotSupported = 0x29,
264 UnacceptableConnParameters = 0x3b,
265 }
266);
267
268param!(
269 enum RemoteConnectionParamsRejectReason {
270 UnacceptableConnParameters = 0x3b,
271 }
272);
273
274param! {
275 #[derive(Default)]
276 enum PowerLevelKind {
277 #[default]
278 Current = 0,
279 Maximum = 1,
280 }
281}
282
283param! {
284 #[derive(Default)]
285 enum ControllerToHostFlowControl {
286 #[default]
287 Off = 0,
288 AclOnSyncOff = 1,
289 AclOffSyncOn = 2,
290 BothOn = 3,
291 }
292}
293
294param!(struct CoreSpecificationVersion(u8));
295
296#[allow(missing_docs)]
297impl CoreSpecificationVersion {
298 pub const VERSION_1_0B: CoreSpecificationVersion = CoreSpecificationVersion(0x00);
299 pub const VERSION_1_1: CoreSpecificationVersion = CoreSpecificationVersion(0x01);
300 pub const VERSION_1_2: CoreSpecificationVersion = CoreSpecificationVersion(0x02);
301 pub const VERSION_2_0_EDR: CoreSpecificationVersion = CoreSpecificationVersion(0x03);
302 pub const VERSION_2_1_EDR: CoreSpecificationVersion = CoreSpecificationVersion(0x04);
303 pub const VERSION_3_0_HS: CoreSpecificationVersion = CoreSpecificationVersion(0x05);
304 pub const VERSION_4_0: CoreSpecificationVersion = CoreSpecificationVersion(0x06);
305 pub const VERSION_4_1: CoreSpecificationVersion = CoreSpecificationVersion(0x07);
306 pub const VERSION_4_2: CoreSpecificationVersion = CoreSpecificationVersion(0x08);
307 pub const VERSION_5_0: CoreSpecificationVersion = CoreSpecificationVersion(0x09);
308 pub const VERSION_5_1: CoreSpecificationVersion = CoreSpecificationVersion(0x0A);
309 pub const VERSION_5_2: CoreSpecificationVersion = CoreSpecificationVersion(0x0B);
310 pub const VERSION_5_3: CoreSpecificationVersion = CoreSpecificationVersion(0x0C);
311 pub const VERSION_5_4: CoreSpecificationVersion = CoreSpecificationVersion(0x0D);
312}
313
314unsafe impl ByteAlignedValue for CoreSpecificationVersion {}
315
316impl<'de> crate::FromHciBytes<'de> for &'de CoreSpecificationVersion {
317 #[inline(always)]
318 fn from_hci_bytes(data: &'de [u8]) -> Result<(Self, &'de [u8]), crate::FromHciBytesError> {
319 <CoreSpecificationVersion as crate::ByteAlignedValue>::ref_from_hci_bytes(data)
320 }
321}
322
323param! {
324 #[derive(Default)]
325 enum LinkType {
326 #[default]
327 SyncData = 0,
328 AclData = 1,
329 IsoData = 2,
330 }
331}
332
333param_slice! {
334 [ConnHandleCompletedPackets; 4] {
335 handle[0]: ConnHandle,
336 num_completed_packets[2]: u16,
337 }
338}
339
340impl ConnHandleCompletedPackets {
341 pub fn new(handle: ConnHandle, num_completed_packets: u16) -> Self {
343 let mut dest = [0; 4];
344 handle.write_hci(&mut dest[0..2]).unwrap();
345 num_completed_packets.write_hci(&mut dest[2..4]).unwrap();
346 Self(dest)
347 }
348}
349
350#[cfg(test)]
351mod tests {
352 #[cfg(feature = "serde")]
353 use postcard;
354
355 use super::*;
356
357 #[test]
358 fn test_encode_decode_conn_handle_completed_packets() {
359 let completed = ConnHandleCompletedPackets::new(ConnHandle::new(42), 2334);
360
361 assert_eq!(completed.handle().unwrap(), ConnHandle::new(42));
362 assert_eq!(completed.num_completed_packets().unwrap(), 2334);
363 }
364
365 #[cfg(feature = "serde")]
366 #[test]
367 fn test_serialize_bdaddr() {
368 let bytes = [0x01, 0xaa, 0x55, 0x04, 0x05, 0xfe];
369
370 let address = BdAddr::new(bytes);
371
372 let mut buffer = [0u8; 32];
373 let vykort = postcard::to_slice(&address, &mut buffer).unwrap();
374
375 assert_eq!(vykort, &bytes);
376 }
377
378 #[cfg(feature = "serde")]
379 #[test]
380 fn test_deserialize_bdaddr() {
381 let bytes = [0xff, 0x5a, 0xa5, 0x00, 0x05, 0xfe];
382
383 let address = postcard::from_bytes::<BdAddr>(&bytes).unwrap();
384
385 let expected = BdAddr::new(bytes);
386
387 assert_eq!(address, expected);
388 }
389}