btle 0.1.4

Lightweight Bluetooth Low Energy Drivers. WIP and very not stable yet!! Designed for https://github.com/AndrewGi/BluetoothMeshRust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! HCI Event and event utilities.
use crate::bytes::{StaticBuf, Storage};
use crate::hci::packet::{PacketType, RawPacket};
use crate::hci::{ErrorCode, Opcode, EVENT_CODE_LEN, OPCODE_LEN};
use crate::ConversionError;
use crate::PackError;
use core::convert::{TryFrom, TryInto};
use core::fmt::Formatter;

/// HCI Event Code. 8-bit code corresponding to an HCI Event. Check the Bluetooth Core Spec for more.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum EventCode {
    InquiryComplete = 0x01,
    InquiryResult = 0x02,
    ConnectionComplete = 0x03,
    ConnectionRequest = 0x04,
    DisconnectionComplete = 0x05,
    AuthenticationComplete = 0x06,
    RemoteNameRequestComplete = 0x07,
    EncryptionChange = 0x08,
    ChangeConnectionLinkKeyComplete = 0x09,
    MasterLinkKeyComplete = 0x0A,
    ReadRemoteSupportedFeaturesComplete = 0x0B,
    ReadRemoteVersionInformationComplete = 0x0C,
    QoSSetupComplete = 0x0D,
    CommandComplete = 0x0E,
    CommandStatus = 0x0F,
    FlushOccurred = 0x11,
    RoleChange = 0x12,
    NumberOfCompletedPackets = 0x13,
    ModeChange = 0x14,
    ReturnLinkKeys = 0x15,
    PINCodeRequest = 0x16,
    LinkKeyRequest = 0x17,
    LinkKeyNotification = 0x18,
    LoopbackCommand = 0x19,
    DataBufferOverflow = 0x1A,
    MaxSlotsChange = 0x1B,
    ReadClockOffsetComplete = 0x1C,
    ConnectionPacketTypeChanged = 0x1D,
    QoSViolation = 0x1E,
    PageScanRepetitionModeChange = 0x20,
    FlowSpecificationComplete = 0x21,
    InquiryResultWithRSSI = 0x22,
    ReadRemoteExtendedFeaturesComplete = 0x23,
    SynchronousConnectionComplete = 0x2C,
    SynchronousConnectionChanged = 0x2D,
    SniffSubrating = 0x2E,
    ExtendedInquiryResult = 0x2F,
    EncryptionKeyRefreshComplete = 0x30,
    IOCapabilityRequest = 0x31,
    IOCapabilityResponse = 0x32,
    UserConfirmationRequest = 0x33,
    UserPasskeyRequest = 0x34,
    RemoteOOBDataRequest = 0x35,
    SimplePairingComplete = 0x36,
    LinkSupervisionTimeoutChanged = 0x38,
    EnhancedFlushComplete = 0x39,
    UserPasskeyNotification = 0x3B,
    KeypressNotification = 0x3C,
    RemoteHostSupportedFeaturesNotification = 0x3D,
    PhysicalLinkComplete = 0x40,
    ChannelSelected = 0x41,
    DisconnectionPhysicalLinkComplete = 0x42,
    PhysicalLinkLostEarlyWarning = 0x43,
    PhysicalLinkRecovery = 0x44,
    LogicalLinkComplete = 0x45,
    DisconnectionLogicalLinkComplete = 0x46,
    FlowSpecModifyComplete = 0x47,
    NumberOfCompletedDataBlocks = 0x48,
    ShortRangeModeChangeComplete = 0x4C,
    AMPStatusChange = 0x4D,
    AMPStartTest = 0x49,
    AMPTestEnd = 0x4A,
    AMPReceiverReport = 0x4B,
    LEMeta = 0x3E,
}
impl From<EventCode> for u8 {
    fn from(code: EventCode) -> Self {
        code as u8
    }
}
impl From<EventCode> for u32 {
    fn from(code: EventCode) -> Self {
        code as u32
    }
}
impl TryFrom<u8> for EventCode {
    type Error = ConversionError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0x01 => Ok(EventCode::InquiryComplete),
            0x02 => Ok(EventCode::InquiryResult),
            0x03 => Ok(EventCode::ConnectionComplete),
            0x04 => Ok(EventCode::ConnectionRequest),
            0x05 => Ok(EventCode::DisconnectionComplete),
            0x06 => Ok(EventCode::AuthenticationComplete),
            0x07 => Ok(EventCode::RemoteNameRequestComplete),
            0x08 => Ok(EventCode::EncryptionChange),
            0x09 => Ok(EventCode::ChangeConnectionLinkKeyComplete),
            0x0A => Ok(EventCode::MasterLinkKeyComplete),
            0x0B => Ok(EventCode::ReadRemoteSupportedFeaturesComplete),
            0x0C => Ok(EventCode::ReadRemoteVersionInformationComplete),
            0x0D => Ok(EventCode::QoSSetupComplete),
            0x0E => Ok(EventCode::CommandComplete),
            0x0F => Ok(EventCode::CommandStatus),
            0x11 => Ok(EventCode::FlushOccurred),
            0x12 => Ok(EventCode::RoleChange),
            0x13 => Ok(EventCode::NumberOfCompletedPackets),
            0x14 => Ok(EventCode::ModeChange),
            0x15 => Ok(EventCode::ReturnLinkKeys),
            0x16 => Ok(EventCode::PINCodeRequest),
            0x17 => Ok(EventCode::LinkKeyRequest),
            0x18 => Ok(EventCode::LinkKeyNotification),
            0x19 => Ok(EventCode::LoopbackCommand),
            0x1A => Ok(EventCode::DataBufferOverflow),
            0x1B => Ok(EventCode::MaxSlotsChange),
            0x1C => Ok(EventCode::ReadClockOffsetComplete),
            0x1D => Ok(EventCode::ConnectionPacketTypeChanged),
            0x1E => Ok(EventCode::QoSViolation),
            0x20 => Ok(EventCode::PageScanRepetitionModeChange),
            0x21 => Ok(EventCode::FlowSpecificationComplete),
            0x22 => Ok(EventCode::InquiryResultWithRSSI),
            0x23 => Ok(EventCode::ReadRemoteExtendedFeaturesComplete),
            0x2C => Ok(EventCode::SynchronousConnectionComplete),
            0x2D => Ok(EventCode::SynchronousConnectionChanged),
            0x2E => Ok(EventCode::SniffSubrating),
            0x2F => Ok(EventCode::ExtendedInquiryResult),
            0x30 => Ok(EventCode::EncryptionKeyRefreshComplete),
            0x33 => Ok(EventCode::IOCapabilityRequest),
            0x32 => Ok(EventCode::IOCapabilityResponse),
            0x31 => Ok(EventCode::UserConfirmationRequest),
            0x34 => Ok(EventCode::UserPasskeyRequest),
            0x35 => Ok(EventCode::RemoteOOBDataRequest),
            0x36 => Ok(EventCode::SimplePairingComplete),
            0x38 => Ok(EventCode::LinkSupervisionTimeoutChanged),
            0x39 => Ok(EventCode::EnhancedFlushComplete),
            0x3B => Ok(EventCode::UserPasskeyNotification),
            0x3C => Ok(EventCode::KeypressNotification),
            0x3D => Ok(EventCode::RemoteHostSupportedFeaturesNotification),
            0x40 => Ok(EventCode::PhysicalLinkComplete),
            0x41 => Ok(EventCode::ChannelSelected),
            0x42 => Ok(EventCode::DisconnectionPhysicalLinkComplete),
            0x43 => Ok(EventCode::PhysicalLinkLostEarlyWarning),
            0x44 => Ok(EventCode::PhysicalLinkRecovery),
            0x45 => Ok(EventCode::LogicalLinkComplete),
            0x46 => Ok(EventCode::DisconnectionLogicalLinkComplete),
            0x47 => Ok(EventCode::FlowSpecModifyComplete),
            0x48 => Ok(EventCode::NumberOfCompletedDataBlocks),
            0x4C => Ok(EventCode::ShortRangeModeChangeComplete),
            0x4D => Ok(EventCode::AMPStatusChange),
            0x49 => Ok(EventCode::AMPStartTest),
            0x4A => Ok(EventCode::AMPTestEnd),
            0x4B => Ok(EventCode::AMPReceiverReport),
            0x3E => Ok(EventCode::LEMeta),
            _ => Err(ConversionError(())),
        }
    }
}
pub trait Event {
    const EVENT_CODE: EventCode;
    /// The byte length of the `Event` parameters.
    fn event_byte_len(&self) -> usize;
    fn event_full_byte_len(&self) -> usize {
        self.event_byte_len() + EVENT_CODE_LEN + 1
    }
    /// Unpack the `Event` from a byte buffer.
    fn event_unpack_from(buf: &[u8]) -> Result<Self, PackError>
    where
        Self: Sized;
    fn unpack_event_packet<S: AsRef<[u8]>>(packet: &EventPacket<S>) -> Result<Self, PackError>
    where
        Self: Sized,
    {
        if packet.event_code != Self::EVENT_CODE {
            Err(PackError::BadOpcode)
        } else {
            Self::event_unpack_from(packet.parameters())
        }
    }
    /// Pack the `Event` parameters into a byte buffer.
    fn event_pack_into(&self, buf: &mut [u8]) -> Result<(), PackError>;
    fn event_pack_packet<S: Storage<u8>>(&self) -> Result<EventPacket<S>, PackError> {
        let mut out = S::with_size(self.event_byte_len());
        self.event_pack_into(out.as_mut())?;
        Ok(EventPacket {
            event_code: Self::EVENT_CODE,
            parameters: out,
        })
    }
}
pub const MAX_HCI_PACKET_SIZE: usize = 255 + 2 + 1 + 1;
#[derive(Copy, Clone)]
pub struct FullHCIBuffer(pub [u8; MAX_HCI_PACKET_SIZE]);
impl FullHCIBuffer {
    pub const DEFAULT: FullHCIBuffer = FullHCIBuffer([0_u8; MAX_HCI_PACKET_SIZE]);
}
impl Default for FullHCIBuffer {
    fn default() -> Self {
        Self::DEFAULT
    }
}
impl AsRef<[u8]> for FullHCIBuffer {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}
impl AsMut<[u8]> for FullHCIBuffer {
    fn as_mut(&mut self) -> &mut [u8] {
        self.0.as_mut()
    }
}
impl From<FullHCIBuffer> for [u8; MAX_HCI_PACKET_SIZE] {
    fn from(b: FullHCIBuffer) -> Self {
        b.0
    }
}
pub type StaticHCIBuffer = StaticBuf<u8, FullHCIBuffer>;
/// Unprocessed HCI Event Packet
pub struct EventPacket<Storage> {
    pub event_code: EventCode,
    pub parameters: Storage,
}
impl<Storage: AsRef<[u8]>> EventPacket<Storage> {
    pub fn new(opcode: EventCode, parameters: Storage) -> Self {
        Self {
            event_code: opcode,
            parameters,
        }
    }
    pub fn as_ref(&self) -> EventPacket<&'_ [u8]> {
        EventPacket {
            event_code: self.event_code,
            parameters: self.parameters.as_ref(),
        }
    }
    pub fn event_code(&self) -> EventCode {
        self.event_code
    }
    pub fn parameters(&self) -> &[u8] {
        self.parameters.as_ref()
    }
    pub fn take_parameters(self) -> Storage {
        self.parameters
    }
    pub fn to_raw_packet<NewStorage: crate::bytes::Storage<u8>>(&self) -> RawPacket<NewStorage> {
        let para_len = self.parameters.as_ref().len();
        let len = para_len + 1 + 1;
        let mut buf = NewStorage::with_size(len);
        buf.as_mut()[2..].copy_from_slice(self.parameters.as_ref());
        buf.as_mut()[0] = self.event_code.into();
        buf.as_mut()[1] = para_len.try_into().expect("len bigger than an u8");
        RawPacket {
            packet_type: PacketType::Event,
            buf,
        }
    }
    pub fn to_new_storage<NewStorage: crate::bytes::Storage<u8>>(&self) -> EventPacket<NewStorage> {
        EventPacket {
            event_code: self.event_code,
            parameters: NewStorage::from_slice(self.parameters().as_ref()),
        }
    }
}
impl<'a> TryFrom<RawPacket<&'a [u8]>> for EventPacket<&'a [u8]> {
    type Error = PackError;

    fn try_from(packet: RawPacket<&'a [u8]>) -> Result<Self, Self::Error> {
        if packet.packet_type != PacketType::Event {
            Err(PackError::BadOpcode)
        } else {
            let code = match packet.buf.get(0) {
                None => {
                    return Err(PackError::BadLength {
                        expected: 2,
                        got: 0,
                    })
                }
                Some(&b) => EventCode::try_from(b).ok().ok_or(PackError::bad_index(0))?,
            };
            let len = match packet.buf.get(1) {
                None => {
                    return Err(PackError::BadLength {
                        expected: 2,
                        got: 1,
                    })
                }
                Some(l) => *l,
            };
            if usize::from(len) != (packet.buf.len() - 2) {
                // Packet length is incorrect
                Err(PackError::InvalidFields)
            } else {
                Ok(EventPacket::new(code, &packet.buf[2..]))
            }
        }
    }
}
impl<Storage: AsRef<[u8]>> core::fmt::Debug for EventPacket<Storage> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
        f.debug_struct("EventPacket")
            .field("event_code", &self.event_code)
            .field("parameters", &AsRef::<[u8]>::as_ref(&self.parameters))
            .finish()
    }
}
pub trait ReturnParameters {
    fn byte_len(&self) -> usize;
    fn pack_into(&self, buf: &mut [u8]) -> Result<(), PackError>;
    fn unpack_from(buf: &[u8]) -> Result<Self, PackError>
    where
        Self: Sized;
}
pub struct StatusReturn {
    pub status: ErrorCode,
}
impl ReturnParameters for StatusReturn {
    fn byte_len(&self) -> usize {
        1
    }

    fn pack_into(&self, buf: &mut [u8]) -> Result<(), PackError> {
        PackError::expect_length(1, buf)?;
        buf[0] = self.status.into();
        Ok(())
    }

    fn unpack_from(buf: &[u8]) -> Result<Self, PackError>
    where
        Self: Sized,
    {
        PackError::expect_length(1, buf)?;
        Ok(StatusReturn {
            status: ErrorCode::try_from(buf[0]).map_err(|_| PackError::bad_index(0))?,
        })
    }
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct CommandComplete<Params: ReturnParameters> {
    pub num_command_packets: u8,
    pub opcode: Opcode,
    pub params: Params,
}
impl<Params: ReturnParameters> CommandComplete<Params> {}
pub const COMMAND_COMPLETE_HEADER_LEN: usize = 1 + OPCODE_LEN;
impl<Params: ReturnParameters> Event for CommandComplete<Params> {
    const EVENT_CODE: EventCode = EventCode::CommandComplete;

    fn event_byte_len(&self) -> usize {
        COMMAND_COMPLETE_HEADER_LEN + self.params.byte_len()
    }

    fn event_unpack_from(buf: &[u8]) -> Result<Self, PackError>
    where
        Self: Sized,
    {
        if buf.len() < COMMAND_COMPLETE_HEADER_LEN {
            Err(PackError::BadLength {
                expected: COMMAND_COMPLETE_HEADER_LEN,
                got: buf.len(),
            })
        } else {
            let opcode = Opcode::unpack(&buf[1..3])?;
            Ok(CommandComplete {
                num_command_packets: buf[0],
                opcode,
                params: Params::unpack_from(&buf[3..])?,
            })
        }
    }

    fn event_pack_into(&self, buf: &mut [u8]) -> Result<(), PackError> {
        PackError::expect_length(self.event_byte_len(), buf)?;
        self.params.pack_into(&mut buf[3..])?;
        self.opcode.pack(&mut buf[1..3])?;
        buf[0] = self.num_command_packets;
        Ok(())
    }
}
pub trait ReturnEvent: Event {
    fn command_opcode(&self) -> Opcode;
    fn guess_command_opcode(buf: &[u8]) -> Option<Opcode>;
}
impl<Params: ReturnParameters> ReturnEvent for CommandComplete<Params> {
    fn command_opcode(&self) -> Opcode {
        self.opcode
    }
    fn guess_command_opcode(buf: &[u8]) -> Option<Opcode> {
        if buf.len() >= 3 {
            Opcode::unpack(&buf[1..3]).ok()
        } else {
            None
        }
    }
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct CommandStatus {
    pub status: ErrorCode,
    pub num_command_packets: u8,
    pub opcode: Opcode,
}
pub const COMMAND_STATUS_LEN: usize = 1 + 1 + OPCODE_LEN;
impl Event for CommandStatus {
    const EVENT_CODE: EventCode = EventCode::CommandStatus;

    fn event_byte_len(&self) -> usize {
        COMMAND_STATUS_LEN
    }

    fn event_unpack_from(buf: &[u8]) -> Result<Self, PackError>
    where
        Self: Sized,
    {
        PackError::expect_length(COMMAND_STATUS_LEN, buf)?;
        let opcode = Opcode::unpack(&buf[2..4])?;
        let status =
            ErrorCode::try_from(buf[0]).map_err(|_| PackError::BadBytes { index: Some(0) })?;
        Ok(CommandStatus {
            status,
            num_command_packets: buf[1],
            opcode,
        })
    }

    fn event_pack_into(&self, buf: &mut [u8]) -> Result<(), PackError> {
        PackError::expect_length(COMMAND_STATUS_LEN, buf)?;
        self.opcode.pack(&mut buf[2..4])?;
        buf[0] = self.status.into();
        buf[1] = self.num_command_packets;
        Ok(())
    }
}

impl ReturnEvent for CommandStatus {
    fn command_opcode(&self) -> Opcode {
        self.opcode
    }
    fn guess_command_opcode(buf: &[u8]) -> Option<Opcode> {
        if buf.len() >= COMMAND_STATUS_LEN {
            Opcode::unpack(&buf[2..4]).ok()
        } else {
            None
        }
    }
}