bluetooth_hci/event/
command.rs

1//! Return parameters for HCI commands.
2//!
3//! This module defines the return parameters that can be returned in a [Command
4//! Complete](super::Event::CommandComplete) event for every HCI command.
5//!
6//! For the Command Complete event, see the Bluetooth specification, v4.1 or later, Vol 2, Part E,
7//! Section 7.7.14.
8//!
9//! For the return parameters of the commands, see the description of each command in sections 7.1 -
10//! 7.6 of the same part of the spec.
11
12use super::VendorReturnParameters;
13use crate::{BadStatusError, ConnectionHandle, Status};
14use byteorder::{ByteOrder, LittleEndian};
15use core::convert::{TryFrom, TryInto};
16use core::fmt::{Debug, Formatter, Result as FmtResult};
17use core::mem;
18
19/// The [Command Complete](super::Event::CommandComplete) event is used by the Controller for most
20/// commands to transmit return status of a command and the other event parameters that are
21/// specified for the issued HCI command.
22///
23/// Must be specialized on the return parameters that may be returned by vendor-specific commands.
24///
25/// Defined in the Bluetooth spec, Vol 2, Part E, Section 7.7.14.
26#[derive(Clone, Debug)]
27pub struct CommandComplete<V>
28where
29    V: super::VendorEvent,
30{
31    /// Indicates the number of HCI command packets the Host can send to the Controller. If the
32    /// Controller requires the Host to stop sending commands, `num_hci_command_packets` will be set
33    /// to zero.  To indicate to the Host that the Controller is ready to receive HCI command
34    /// packets, the Controller generates a Command Complete event with `return_params` set to
35    /// [`Spontaneous`](ReturnParameters::Spontaneous) and `num_hci_command_packets` parameter set
36    /// to 1 or more.  [`Spontaneous`](ReturnParameters::Spontaneous) return parameters indicates
37    /// that this event is not associated with a command sent by the Host. The Controller can send a
38    /// Spontaneous Command Complete event at any time to change the number of outstanding HCI
39    /// command packets that the Host can send before waiting.
40    pub num_hci_command_packets: u8,
41
42    /// The type of command that has completed, and any parameters that it returns.
43    pub return_params: ReturnParameters<V>,
44}
45
46impl<V> CommandComplete<V>
47where
48    V: super::VendorEvent,
49{
50    /// Deserializes a buffer into a CommandComplete event.
51    ///
52    /// # Errors
53    ///
54    /// - [`BadLength`](crate::event::Error::BadLength) if the buffer is not large enough to contain
55    ///   a parameter length (1 byte) and opcode (2 bytes)
56    /// - Returns errors that may be generated when deserializing specific events. This may be
57    ///   [`BadLength`](crate::event::Error::BadLength), which indicates the buffer was not large
58    ///   enough to contain all of the required data for the event. Some commands define other
59    ///   errors that indicate parameter values are invalid. The error type must be specialized on
60    ///   potential vendor-specific errors, though vendor-specific errors are never returned by this
61    ///   function.
62    pub fn new(bytes: &[u8]) -> Result<CommandComplete<V>, crate::event::Error<V::Error>> {
63        require_len_at_least!(bytes, 3);
64
65        let params = match crate::opcode::Opcode(LittleEndian::read_u16(&bytes[1..])) {
66            crate::opcode::Opcode(0x0000) => ReturnParameters::Spontaneous,
67            crate::opcode::SET_EVENT_MASK => {
68                ReturnParameters::SetEventMask(to_status(&bytes[3..])?)
69            }
70            crate::opcode::RESET => ReturnParameters::Reset(to_status(&bytes[3..])?),
71            crate::opcode::READ_TX_POWER_LEVEL => {
72                ReturnParameters::ReadTxPowerLevel(to_tx_power_level(&bytes[3..])?)
73            }
74            crate::opcode::READ_LOCAL_VERSION_INFO => {
75                ReturnParameters::ReadLocalVersionInformation(to_local_version_info(&bytes[3..])?)
76            }
77            crate::opcode::READ_LOCAL_SUPPORTED_COMMANDS => {
78                ReturnParameters::ReadLocalSupportedCommands(to_supported_commands(&bytes[3..])?)
79            }
80            crate::opcode::READ_LOCAL_SUPPORTED_FEATURES => {
81                ReturnParameters::ReadLocalSupportedFeatures(to_supported_features(&bytes[3..])?)
82            }
83            crate::opcode::READ_BD_ADDR => ReturnParameters::ReadBdAddr(to_bd_addr(&bytes[3..])?),
84            crate::opcode::READ_RSSI => ReturnParameters::ReadRssi(to_read_rssi(&bytes[3..])?),
85            crate::opcode::LE_SET_EVENT_MASK => {
86                ReturnParameters::LeSetEventMask(to_status(&bytes[3..])?)
87            }
88            crate::opcode::LE_READ_BUFFER_SIZE => {
89                ReturnParameters::LeReadBufferSize(to_le_read_buffer_status(&bytes[3..])?)
90            }
91            crate::opcode::LE_READ_LOCAL_SUPPORTED_FEATURES => {
92                ReturnParameters::LeReadLocalSupportedFeatures(to_le_local_supported_features(
93                    &bytes[3..],
94                )?)
95            }
96            crate::opcode::LE_SET_RANDOM_ADDRESS => {
97                ReturnParameters::LeSetRandomAddress(to_status(&bytes[3..])?)
98            }
99            crate::opcode::LE_SET_ADVERTISING_PARAMETERS => {
100                ReturnParameters::LeSetAdvertisingParameters(to_status(&bytes[3..])?)
101            }
102            crate::opcode::LE_READ_ADVERTISING_CHANNEL_TX_POWER => {
103                ReturnParameters::LeReadAdvertisingChannelTxPower(
104                    to_le_advertising_channel_tx_power(&bytes[3..])?,
105                )
106            }
107            crate::opcode::LE_SET_ADVERTISING_DATA => {
108                ReturnParameters::LeSetAdvertisingData(to_status(&bytes[3..])?)
109            }
110            crate::opcode::LE_SET_SCAN_RESPONSE_DATA => {
111                ReturnParameters::LeSetScanResponseData(to_status(&bytes[3..])?)
112            }
113            crate::opcode::LE_SET_ADVERTISE_ENABLE => {
114                to_le_set_advertise_enable(to_status(&bytes[3..])?)
115            }
116            crate::opcode::LE_SET_SCAN_PARAMETERS => {
117                ReturnParameters::LeSetScanParameters(to_status(&bytes[3..])?)
118            }
119            crate::opcode::LE_SET_SCAN_ENABLE => {
120                ReturnParameters::LeSetScanEnable(to_status(&bytes[3..])?)
121            }
122            crate::opcode::LE_CREATE_CONNECTION_CANCEL => {
123                ReturnParameters::LeCreateConnectionCancel(to_status(&bytes[3..])?)
124            }
125            crate::opcode::LE_READ_WHITE_LIST_SIZE => {
126                ReturnParameters::LeReadWhiteListSize(to_status(&bytes[3..])?, bytes[4] as usize)
127            }
128            crate::opcode::LE_CLEAR_WHITE_LIST => {
129                ReturnParameters::LeClearWhiteList(to_status(&bytes[3..])?)
130            }
131            crate::opcode::LE_ADD_DEVICE_TO_WHITE_LIST => {
132                ReturnParameters::LeAddDeviceToWhiteList(to_status(&bytes[3..])?)
133            }
134            crate::opcode::LE_REMOVE_DEVICE_FROM_WHITE_LIST => {
135                ReturnParameters::LeRemoveDeviceFromWhiteList(to_status(&bytes[3..])?)
136            }
137            crate::opcode::LE_SET_HOST_CHANNEL_CLASSIFICATION => {
138                ReturnParameters::LeSetHostChannelClassification(to_status(&bytes[3..])?)
139            }
140            crate::opcode::LE_READ_CHANNEL_MAP => {
141                ReturnParameters::LeReadChannelMap(to_le_channel_map_parameters(&bytes[3..])?)
142            }
143            crate::opcode::LE_ENCRYPT => {
144                ReturnParameters::LeEncrypt(to_le_encrypted_data(&bytes[3..])?)
145            }
146            crate::opcode::LE_RAND => ReturnParameters::LeRand(to_random_number(&bytes[3..])?),
147            crate::opcode::LE_LTK_REQUEST_REPLY => {
148                ReturnParameters::LeLongTermKeyRequestReply(to_le_ltk_request_reply(&bytes[3..])?)
149            }
150            crate::opcode::LE_LTK_REQUEST_NEGATIVE_REPLY => {
151                ReturnParameters::LeLongTermKeyRequestNegativeReply(to_le_ltk_request_reply(
152                    &bytes[3..],
153                )?)
154            }
155            crate::opcode::LE_READ_STATES => {
156                ReturnParameters::LeReadSupportedStates(to_le_read_states(&bytes[3..])?)
157            }
158            crate::opcode::LE_RECEIVER_TEST => {
159                ReturnParameters::LeReceiverTest(to_status(&bytes[3..])?)
160            }
161            crate::opcode::LE_TRANSMITTER_TEST => {
162                ReturnParameters::LeTransmitterTest(to_status(&bytes[3..])?)
163            }
164            crate::opcode::LE_TEST_END => ReturnParameters::LeTestEnd(to_le_test_end(&bytes[3..])?),
165            other => {
166                const VENDOR_OGF: u16 = 0x3F;
167                if other.ogf() != VENDOR_OGF {
168                    return Err(crate::event::Error::UnknownOpcode(other));
169                }
170
171                ReturnParameters::Vendor(V::ReturnParameters::new(&bytes)?)
172            }
173        };
174        Ok(CommandComplete::<V> {
175            num_hci_command_packets: bytes[0],
176            return_params: params,
177        })
178    }
179}
180
181/// Commands that may generate the [Command Complete](crate::event::Event::CommandComplete) event.
182/// If the commands have defined return parameters, they are included in this enum.
183#[derive(Copy, Clone, Debug)]
184pub enum ReturnParameters<V>
185where
186    V: super::VendorEvent,
187{
188    /// The controller sent an unsolicited command complete event in order to change the number of
189    /// HCI command packets the Host is allowed to send.
190    Spontaneous,
191
192    /// Status returned by the [Set Event Mask](crate::host::Hci::set_event_mask) command.
193    SetEventMask(Status<V::Status>),
194
195    /// Status returned by the [Reset](crate::host::Hci::reset) command.
196    Reset(Status<V::Status>),
197
198    /// [Read Transmit Power Level](crate::host::Hci::read_tx_power_level) return parameters.
199    ReadTxPowerLevel(TxPowerLevel<V::Status>),
200
201    /// Local version info returned by the [Read Local Version
202    /// Information](crate::host::Hci::read_local_version_information) command.
203    ReadLocalVersionInformation(LocalVersionInfo<V::Status>),
204
205    /// Supported commands returned by the [Read Local Supported
206    /// Commands](crate::host::Hci::read_local_supported_commands) command.
207    ReadLocalSupportedCommands(LocalSupportedCommands<V::Status>),
208
209    /// Supported features returned by the [Read Local Supported
210    /// Features](crate::host::Hci::read_local_supported_features) command.
211    ReadLocalSupportedFeatures(LocalSupportedFeatures<V::Status>),
212
213    /// BD ADDR returned by the [Read BD ADDR](crate::host::Hci::read_bd_addr) command.
214    ReadBdAddr(ReadBdAddr<V::Status>),
215
216    /// RSSI returned by the [Read RSSI](crate::host::Hci::read_rssi) command.
217    ReadRssi(ReadRssi<V::Status>),
218
219    /// Status returned by the [LE Set Event Mask](crate::host::Hci::le_set_event_mask) command.
220    LeSetEventMask(Status<V::Status>),
221
222    /// Parameters returned by the [LE Read Buffer Size](crate::host::Hci::le_read_buffer_size)
223    /// command.
224    LeReadBufferSize(LeReadBufferSize<V::Status>),
225
226    /// Parameters returned by the [LE Read Local Supported
227    /// Features](crate::host::Hci::le_read_local_supported_features) command.
228    LeReadLocalSupportedFeatures(LeSupportedFeatures<V::Status>),
229
230    /// Status returned by the [LE Set Random Address](crate::host::Hci::le_set_random_address)
231    /// command.
232    LeSetRandomAddress(Status<V::Status>),
233
234    /// Status returned by the [LE Set Advertising
235    /// Parameters](crate::host::Hci::le_set_advertising_parameters) command.
236    LeSetAdvertisingParameters(Status<V::Status>),
237
238    /// Parameters returned by the [LE Read Advertising Channel TX
239    /// Power](crate::host::Hci::le_read_advertising_channel_tx_power) command.
240    LeReadAdvertisingChannelTxPower(LeAdvertisingChannelTxPower<V::Status>),
241
242    /// Status returned by the [LE Set Advertising Data](crate::host::Hci::le_set_advertising_data)
243    /// command.
244    LeSetAdvertisingData(Status<V::Status>),
245
246    /// Status returned by the [LE Set Scan Response
247    /// Data](crate::host::Hci::le_set_scan_response_data) command.
248    LeSetScanResponseData(Status<V::Status>),
249
250    /// Status returned by the [LE Set Advertise Enable](crate::host::Hci::le_set_advertise_enable)
251    /// command.
252    #[cfg(not(feature = "version-5-0"))]
253    LeSetAdvertiseEnable(Status<V::Status>),
254
255    /// Status returned by the [LE Set Advertising
256    /// Enable](crate::host::Hci::le_set_advertising_enable) command.
257    #[cfg(feature = "version-5-0")]
258    LeSetAdvertisingEnable(Status<V::Status>),
259
260    /// Status returned by the [LE Set Scan Parameters](crate::host::Hci::le_set_scan_parameters)
261    /// command.
262    LeSetScanParameters(Status<V::Status>),
263
264    /// Status returned by the [LE Set Scan Enable](crate::host::Hci::le_set_scan_enable) command.
265    LeSetScanEnable(Status<V::Status>),
266
267    /// Status returned by the [LE Create Connection
268    /// Cancel](crate::host::Hci::le_create_connection_cancel) command.
269    LeCreateConnectionCancel(Status<V::Status>),
270
271    /// Status and white list size returned by the [LE Read White List
272    /// Size](crate::host::Hci::le_read_white_list_size) command.
273    LeReadWhiteListSize(Status<V::Status>, usize),
274
275    /// Status returned by the [LE Clear White List](crate::host::Hci::le_clear_white_list) command.
276    LeClearWhiteList(Status<V::Status>),
277
278    /// Status returned by the [LE Add Device to White
279    /// List](crate::host::Hci::le_add_device_to_white_list) command.
280    LeAddDeviceToWhiteList(Status<V::Status>),
281
282    /// Status returned by the [LE Remove Device from White
283    /// List](crate::host::Hci::le_remove_device_from_white_list) command.
284    LeRemoveDeviceFromWhiteList(Status<V::Status>),
285
286    /// Status returned by the [LE Set Host Channel
287    /// Classification](crate::host::Hci::le_set_host_channel_classification) command.
288    LeSetHostChannelClassification(Status<V::Status>),
289
290    /// Parameters returned by the [LE Read Channel Map](crate::host::Hci::le_read_channel_map)
291    /// command.
292    LeReadChannelMap(ChannelMapParameters<V::Status>),
293
294    /// Parameters returned by the [LE Encrypt](crate::host::Hci::le_encrypt) command.
295    LeEncrypt(EncryptedReturnParameters<V::Status>),
296
297    /// Parameters returned by the [LE Rand](crate::host::Hci::le_rand) command.
298    LeRand(LeRandom<V::Status>),
299
300    /// Parameters returned by the [LE Long Term Key Request
301    /// Reply](crate::host::Hci::le_long_term_key_request_reply) command.
302    LeLongTermKeyRequestReply(LeLongTermRequestReply<V::Status>),
303
304    /// Parameters returned by the [LE Long Term Key Request Negative
305    /// Reply](crate::host::Hci::le_long_term_key_request_negative_reply) command.
306    LeLongTermKeyRequestNegativeReply(LeLongTermRequestReply<V::Status>),
307
308    /// Parameters returned by the [LE Read States](crate::host::Hci::le_read_supported_states))
309    /// command.
310    LeReadSupportedStates(LeReadSupportedStates<V::Status>),
311
312    /// Status returned by the [LE Receiver Test](crate::host::Hci::le_receiver_test) command.
313    LeReceiverTest(Status<V::Status>),
314
315    /// Status returned by the [LE Transmitter Test](crate::host::Hci::le_transmitter_test) command.
316    LeTransmitterTest(Status<V::Status>),
317
318    /// Parameters returned by the [LE Test End](crate::host::Hci::le_test_end) command.
319    LeTestEnd(LeTestEnd<V::Status>),
320
321    /// Parameters returned by vendor-specific commands.
322    Vendor(V::ReturnParameters),
323}
324
325fn to_status<VE, VS>(bytes: &[u8]) -> Result<Status<VS>, crate::event::Error<VE>>
326where
327    Status<VS>: TryFrom<u8, Error = BadStatusError>,
328{
329    bytes[0].try_into().map_err(super::rewrap_bad_status)
330}
331
332/// Values returned by the [Read Transmit Power Level](crate::host::Hci::read_tx_power_level)
333/// command.
334#[derive(Copy, Clone, Debug)]
335pub struct TxPowerLevel<VS> {
336    /// Did the command fail, and if so, how?
337    pub status: Status<VS>,
338
339    /// Specifies which connection handle's transmit power level setting is returned
340    pub conn_handle: ConnectionHandle,
341
342    /// Power level for the connection handle, in dBm.
343    ///
344    /// Valid range is -30 dBm to +20 dBm, but that is not enforced by this implementation.
345    pub tx_power_level_dbm: i8,
346}
347
348fn to_tx_power_level<VE, VS>(bytes: &[u8]) -> Result<TxPowerLevel<VS>, crate::event::Error<VE>>
349where
350    Status<VS>: TryFrom<u8, Error = BadStatusError>,
351{
352    require_len!(bytes, 4);
353    Ok(TxPowerLevel {
354        status: to_status(bytes)?,
355        conn_handle: ConnectionHandle(LittleEndian::read_u16(&bytes[1..])),
356        tx_power_level_dbm: unsafe { mem::transmute::<u8, i8>(bytes[3]) },
357    })
358}
359
360/// Values returned by [Read Local Version
361/// Information](crate::host::Hci::read_local_version_information) command.
362#[derive(Copy, Clone, Debug)]
363pub struct LocalVersionInfo<VS> {
364    /// Did the command fail, and if so, how?
365    pub status: Status<VS>,
366
367    /// The version information of the HCI layer.
368    ///
369    /// See the Bluetooth [Assigned
370    /// Numbers](https://www.bluetooth.com/specifications/assigned-numbers/host-controller-interface).
371    pub hci_version: u8,
372
373    /// Revision of the Current HCI in the BR/EDR Controller.  This value is implementation
374    /// dependent.
375    pub hci_revision: u16,
376
377    /// Version of the Current [LMP] or [PAL] in the Controller.
378    ///
379    /// [LMP]: https://www.bluetooth.com/specifications/assigned-numbers/link-manager
380    /// [PAL]: https://www.bluetooth.com/specifications/assigned-numbers/protocol-adaptation-layer
381    pub lmp_version: u8,
382
383    /// Manufacturer Name of the BR/EDR Controller.  See Bluetooth [Assigned
384    /// Numbers](https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers)
385    pub manufacturer_name: u16,
386
387    /// Subversion of the Current [LMP] or [PAL] in the Controller. This value is implementation
388    /// dependent.
389    ///
390    /// [LMP]: https://www.bluetooth.com/specifications/assigned-numbers/link-manager
391    /// [PAL]: https://www.bluetooth.com/specifications/assigned-numbers/protocol-adaptation-layer
392    pub lmp_subversion: u16,
393}
394
395fn to_local_version_info<VE, VS>(
396    bytes: &[u8],
397) -> Result<LocalVersionInfo<VS>, crate::event::Error<VE>>
398where
399    Status<VS>: TryFrom<u8, Error = BadStatusError>,
400{
401    require_len!(bytes, 9);
402
403    Ok(LocalVersionInfo {
404        status: bytes[0].try_into().map_err(super::rewrap_bad_status)?,
405        hci_version: bytes[1],
406        hci_revision: LittleEndian::read_u16(&bytes[2..]),
407        lmp_version: bytes[4],
408        manufacturer_name: LittleEndian::read_u16(&bytes[5..]),
409        lmp_subversion: LittleEndian::read_u16(&bytes[7..]),
410    })
411}
412
413/// Values returned by the [Read Local Supported
414/// Commands](crate::host::Hci::read_local_supported_commands) command.
415#[derive(Copy, Clone, Debug)]
416pub struct LocalSupportedCommands<VS> {
417    /// Did the command fail, and if so, how?
418    pub status: Status<VS>,
419
420    /// Flags for supported commands.
421    pub supported_commands: CommandFlags,
422}
423
424const COMMAND_FLAGS_SIZE: usize = 64;
425
426bitflag_array! {
427    /// Extended bit field for the command flags of the [`LocalSupportedCommands`] return
428    /// parameters.
429    #[derive(Copy, Clone)]
430    pub struct CommandFlags : COMMAND_FLAGS_SIZE;
431    pub struct CommandFlag;
432
433    /// Inquiry
434    const INQUIRY = 0, 1 << 0;
435    /// Cancel Inquiry
436    const INQUIRY_CANCEL = 0, 1 << 1;
437    /// Periodic Inquiry Mode
438    const PERIODIC_INQUIRY_MODE = 0, 1 << 2;
439    /// Exit Periodic Inquiry Mode
440    const EXIT_PERIODIC_INQUIRY_MODE = 0, 1 << 3;
441    /// Create Connection
442    const CREATE_CONNECTION = 0, 1 << 4;
443    /// Disconnect
444    const DISCONNECT = 0, 1 << 5;
445    /// Add SCO Connection (deprecated by the spec)
446    #[deprecated]
447    const ADD_SCO_CONNECTION = 0, 1 << 6;
448    /// Create Connection Cancel
449    const CREATE_CONNECTION_CANCEL = 0, 1 << 7;
450    /// Accept Connection Request
451    const ACCEPT_CONNECTION_REQUEST = 1, 1 << 0;
452    /// Reject Connection Request
453    const REJECT_CONNECTION_REQUEST = 1, 1 << 1;
454    /// Link Key Request Reply
455    const LINK_KEY_REQUEST_REPLY = 1, 1 << 2;
456    /// Link Key Request Negative Reply
457    const LINK_KEY_REQUEST_NEGATIVE_REPLY = 1, 1 << 3;
458    /// PIN Code Request Reply
459    const PIN_CODE_REQUEST_REPLY = 1, 1 << 4;
460    /// PIN Code Request Negative Reply
461    const PIN_CODE_REQUEST_NEGATIVE_REPLY = 1, 1 << 5;
462    /// Change Connection Packet Type
463    const CHANGE_CONNECTION_PACKET_TYPE = 1, 1 << 6;
464    /// Authentication Requested
465    const AUTHENTICATION_REQUESTED = 1, 1 << 7;
466    /// Set Connection Encryption
467    const SET_CONNECTION_ENCRYPTION = 2, 1 << 0;
468    /// Change Connection Link Key
469    const CHANGE_CONNECTION_LINK_KEY = 2, 1 << 1;
470    /// Master Link Key
471    const MASTER_LINK_KEY = 2, 1 << 2;
472    /// Remote Name Request
473    const REMOTE_NAME_REQUEST = 2, 1 << 3;
474    /// Remote Name Request Cancel
475    const REMOTE_NAME_REQUEST_CANCEL = 2, 1 << 4;
476    /// Read Remote Supported Features
477    const READ_REMOTE_SUPPORTED_FEATURES = 2, 1 << 5;
478    /// Read Remote Extended Features
479    const READ_REMOTE_EXTENDED_FEATURES = 2, 1 << 6;
480    /// Read Remote Version Information
481    const READ_REMOTE_VERSION_INFORMATION = 2, 1 << 7;
482    /// Read Clock Offset
483    const READ_CLOCK_OFFSET = 3, 1 << 0;
484    /// Read LMP Handle
485    const READ_LMP_HANDLE = 3, 1 << 1;
486    /// Hold Mode
487    const HOLD_MODE = 4, 1 << 1;
488    /// Sniff Mode
489    const SNIFF_MODE = 4, 1 << 2;
490    /// Exit Sniff Mode
491    const EXIT_SNIFF_MODE = 4, 1 << 3;
492    /// Park State
493    const PARK_STATE = 4, 1 << 4;
494    /// Exit Park State
495    const EXIT_PARK_STATE = 4, 1 << 5;
496    /// QoS Setup
497    const QOS_SETUP = 4, 1 << 6;
498    /// Role Discovery
499    const ROLE_DISCOVERY = 4, 1 << 7;
500    /// Switch Role
501    const SWITCH_ROLE = 5, 1 << 0;
502    /// Read Link Policy Settings
503    const READ_LINK_POLICY_SETTINGS = 5, 1 << 1;
504    /// Write Link Policy Settings
505    const WRITE_LINK_POLICY_SETTINGS = 5, 1 << 2;
506    /// Read Default Link Policy Settings
507    const READ_DEFAULT_LINK_POLICY_SETTINGS = 5, 1 << 3;
508    /// Write Default Link Policy Settings
509    const WRITE_DEFAULT_LINK_POLICY_SETTINGS = 5, 1 << 4;
510    /// Flow Specification
511    const FLOW_SPECIFICATION = 5, 1 << 5;
512    /// Set Event Mask
513    const SET_EVENT_MASK = 5, 1 << 6;
514    /// Reset
515    const RESET = 5, 1 << 7;
516    /// Set Event Filter
517    const SET_EVENT_FILTER = 6, 1 << 0;
518    /// Flush
519    const FLUSH = 6, 1 << 1;
520    /// Read PIN Type
521    const READ_PIN_TYPE = 6, 1 << 2;
522    /// Write PIN Type
523    const WRITE_PIN_TYPE = 6, 1 << 3;
524    /// Create New Unit Key
525    const CREATE_NEW_UNIT_KEY = 6, 1 << 4;
526    /// Read Stored Link Key
527    const READ_STORED_LINK_KEY = 6, 1 << 5;
528    /// Write Stored Link Key
529    const WRITE_STORED_LINK_KEY = 6, 1 << 6;
530    /// Delete Stored Link Key
531    const DELETE_STORED_LINK_KEY = 6, 1 << 7;
532    /// Write Local Name
533    const WRITE_LOCAL_NAME = 7, 1 << 0;
534    /// Read Local Name
535    const READ_LOCAL_NAME = 7, 1 << 1;
536    /// Read Connection Accept Timeout
537    const READ_CONNECTION_ACCEPT_TIMEOUT = 7, 1 << 2;
538    /// Write Connection Accept Timeout
539    const WRITE_CONNECTION_ACCEPT_TIMEOUT = 7, 1 << 3;
540    /// Read Page Timeout
541    const READ_PAGE_TIMEOUT = 7, 1 << 4;
542    /// Write Page Timeout
543    const WRITE_PAGE_TIMEOUT = 7, 1 << 5;
544    /// Read Scan Enable
545    const READ_SCAN_ENABLE = 7, 1 << 6;
546    /// Write Scan Enable
547    const WRITE_SCAN_ENABLE = 7, 1 << 7;
548    /// Read Page Scan Activity
549    const READ_PAGE_SCAN_ACTIVITY = 8, 1 << 0;
550    /// Write Page Scan Activity
551    const WRITE_PAGE_SCAN_ACTIVITY = 8, 1 << 1;
552    /// Read Inquiry Scan Activity
553    const READ_INQUIRY_SCAN_ACTIVITY = 8, 1 << 2;
554    /// Write Inquiry Scan Activity
555    const WRITE_INQUIRY_SCAN_ACTIVITY = 8, 1 << 3;
556    /// Read Authentication Enable
557    const READ_AUTHENTICATION_ENABLE = 8, 1 << 4;
558    /// Write Authentication Enable
559    const WRITE_AUTHENTICATION_ENABLE = 8, 1 << 5;
560    /// Read Encryption Mode (deprecated by the spec)
561    #[deprecated]
562    const READ_ENCRYPTION_MODE = 8, 1 << 6;
563    /// Write Encryption Mode (deprecated by the spec)
564    #[deprecated]
565    const WRITE_ENCRYPTION_MODE = 8, 1 << 7;
566    /// Read Class Of Device
567    const READ_CLASS_OF_DEVICE = 9, 1 << 0;
568    /// Write Class Of Device
569    const WRITE_CLASS_OF_DEVICE = 9, 1 << 1;
570    /// Read Voice Setting
571    const READ_VOICE_SETTING = 9, 1 << 2;
572    /// Write Voice Setting
573    const WRITE_VOICE_SETTING = 9, 1 << 3;
574    /// Read Automatic Flush Timeout
575    const READ_AUTOMATIC_FLUSH_TIMEOUT = 9, 1 << 4;
576    /// Write Automatic Flush Timeout
577    const WRITE_AUTOMATIC_FLUSH_TIMEOUT = 9, 1 << 5;
578    /// Read Num Broadcast Retransmissions
579    const READ_NUM_BROADCAST_RETRANSMISSIONS = 9, 1 << 6;
580    /// Write Num Broadcast Retransmissions
581    const WRITE_NUM_BROADCAST_RETRANSMISSIONS = 9, 1 << 7;
582    /// Read Hold Mode Activity
583    const READ_HOLD_MODE_ACTIVITY = 10, 1 << 0;
584    /// Write Hold Mode Activity
585    const WRITE_HOLD_MODE_ACTIVITY = 10, 1 << 1;
586    /// Read Transmit Power Level
587    const READ_TRANSMIT_POWER_LEVEL = 10, 1 << 2;
588    /// Read Synchronous Flow Control Enable
589    const READ_SYNCHRONOUS_FLOW_CONTROL_ENABLE = 10, 1 << 3;
590    /// Write Synchronous Flow Control Enable
591    const WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE = 10, 1 << 4;
592    /// Set Controller To Host Flow Control
593    const SET_CONTROLLER_TO_HOST_FLOW_CONTROL = 10, 1 << 5;
594    /// Host Buffer Size
595    const HOST_BUFFER_SIZE = 10, 1 << 6;
596    /// Host Number Of Completed Packets
597    const HOST_NUMBER_OF_COMPLETED_PACKETS = 10, 1 << 7;
598    /// Read Link Supervision Timeout
599    const READ_LINK_SUPERVISION_TIMEOUT = 11, 1 << 0;
600    /// Write Link Supervision Timeout
601    const WRITE_LINK_SUPERVISION_TIMEOUT = 11, 1 << 1;
602    /// Read Number of Supported IAC
603    const READ_NUMBER_OF_SUPPORTED_IAC = 11, 1 << 2;
604    /// Read Current IAC LAP
605    const READ_CURRENT_IAC_LAP = 11, 1 << 3;
606    /// Write Current IAC LAP
607    const WRITE_CURRENT_IAC_LAP = 11, 1 << 4;
608    /// Read Page Scan Mode Period (deprecated by the spec)
609    #[deprecated]
610    const READ_PAGE_SCAN_MODE_PERIOD = 11, 1 << 5;
611    /// Write Page Scan Mode Period (deprecated by the spec)
612    #[deprecated]
613    const WRITE_PAGE_SCAN_MODE_PERIOD = 11, 1 << 6;
614    /// Read Page Scan Mode (deprecated by the spec)
615    #[deprecated]
616    const READ_PAGE_SCAN_MODE = 11, 1 << 7;
617    /// Write Page Scan Mode (deprecated by the spec)
618    #[deprecated]
619    const WRITE_PAGE_SCAN_MODE = 12, 1 << 0;
620    /// Set AFH Host Channel Classification
621    const SET_AFH_HOST_CHANNEL_CLASSIFICATION = 12, 1 << 1;
622    /// Read Inquiry Scan Type
623    const READ_INQUIRY_SCAN_TYPE = 12, 1 << 4;
624    /// Write Inquiry Scan Type
625    const WRITE_INQUIRY_SCAN_TYPE = 12, 1 << 5;
626    /// Read Inquiry Mode
627    const READ_INQUIRY_MODE = 12, 1 << 6;
628    /// Write Inquiry Mode
629    const WRITE_INQUIRY_MODE = 12, 1 << 7;
630    /// Read Page Scan Type
631    const READ_PAGE_SCAN_TYPE = 13, 1 << 0;
632    /// Write Page Scan Type
633    const WRITE_PAGE_SCAN_TYPE = 13, 1 << 1;
634    /// Read AFH Channel Assessment Mode
635    const READ_AFH_CHANNEL_ASSESSMENT_MODE = 13, 1 << 2;
636    /// Write AFH Channel Assessment Mode
637    const WRITE_AFH_CHANNEL_ASSESSMENT_MODE = 13, 1 << 3;
638    /// Read Local Version Information
639    const READ_LOCAL_VERSION_INFORMATION = 14, 1 << 3;
640    /// Read Local Supported Features
641    const READ_LOCAL_SUPPORTED_FEATURES = 14, 1 << 5;
642    /// Read Local Extended Features
643    const READ_LOCAL_EXTENDED_FEATURES = 14, 1 << 6;
644    /// Read Buffer Size
645    const READ_BUFFER_SIZE = 14, 1 << 7;
646    /// Read Country Code [Deprecated by the spec]
647    #[deprecated]
648    const READ_COUNTRY_CODE = 15, 1 << 0;
649    /// Read BD ADDR
650    const READ_BD_ADDR = 15, 1 << 1;
651    /// Read Failed Contact Counter
652    const READ_FAILED_CONTACT_COUNTER = 15, 1 << 2;
653    /// Reset Failed Contact Counter
654    const RESET_FAILED_CONTACT_COUNTER = 15, 1 << 3;
655    /// Read Link Quality
656    const READ_LINK_QUALITY = 15, 1 << 4;
657    /// Read RSSI
658    const READ_RSSI = 15, 1 << 5;
659    /// Read AFH Channel Map
660    const READ_AFH_CHANNEL_MAP = 15, 1 << 6;
661    /// Read Clock
662    const READ_CLOCK = 15, 1 << 7;
663    /// Read Loopback Mode
664    const READ_LOOPBACK_MODE = 16, 1 << 0;
665    /// Write Loopback Mode
666    const WRITE_LOOPBACK_MODE = 16, 1 << 1;
667    /// Enable Device Under Test Mode
668    const ENABLE_DEVICE_UNDER_TEST_MODE = 16, 1 << 2;
669    /// Setup Synchronous Connection Request
670    const SETUP_SYNCHRONOUS_CONNECTION_REQUEST = 16, 1 << 3;
671    /// Accept Synchronous Connection Request
672    const ACCEPT_SYNCHRONOUS_CONNECTION_REQUEST = 16, 1 << 4;
673    /// Reject Synchronous Connection Request
674    const REJECT_SYNCHRONOUS_CONNECTION_REQUEST = 16, 1 << 5;
675    /// Read Extended Inquiry Response
676    const READ_EXTENDED_INQUIRY_RESPONSE = 17, 1 << 0;
677    /// Write Extended Inquiry Response
678    const WRITE_EXTENDED_INQUIRY_RESPONSE = 17, 1 << 1;
679    /// Refresh Encryption Key
680    const REFRESH_ENCRYPTION_KEY = 17, 1 << 2;
681    /// Sniff Subrating
682    const SNIFF_SUBRATING = 17, 1 << 4;
683    /// Read Simple Pairing Mode
684    const READ_SIMPLE_PAIRING_MODE = 17, 1 << 5;
685    /// Write Simple Pairing Mode
686    const WRITE_SIMPLE_PAIRING_MODE = 17, 1 << 6;
687    /// Read Local OOB Data
688    const READ_LOCAL_OOB_DATA = 17, 1 << 7;
689    /// Read Inquiry Response Transmit Power Level
690    const READ_INQUIRY_RESPONSE_TRANSMIT_POWER_LEVEL = 18, 1 << 0;
691    /// Write Inquiry Transmit Power Level
692    const WRITE_INQUIRY_TRANSMIT_POWER_LEVEL = 18, 1 << 1;
693    /// Read Default Erroneous Data Reporting
694    const READ_DEFAULT_ERRONEOUS_DATA_REPORTING = 18, 1 << 2;
695    /// Write Default Erroneous Data Reporting
696    const WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING = 18, 1 << 3;
697    /// IO Capability Request Reply
698    const IO_CAPABILITY_REQUEST_REPLY = 18, 1 << 7;
699    /// User Confirmation Request Reply
700    const USER_CONFIRMATION_REQUEST_REPLY = 19, 1 << 0;
701    /// User Confirmation Request Negative Reply
702    const USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY = 19, 1 << 1;
703    /// User Passkey Request Reply
704    const USER_PASSKEY_REQUEST_REPLY = 19, 1 << 2;
705    /// User Passkey Request Negative Reply
706    const USER_PASSKEY_REQUEST_NEGATIVE_REPLY = 19, 1 << 3;
707    /// Remote OOB Data Request Reply
708    const REMOTE_OOB_DATA_REQUEST_REPLY = 19, 1 << 4;
709    /// Write Simple Pairing Debug Mode
710    const WRITE_SIMPLE_PAIRING_DEBUG_MODE = 19, 1 << 5;
711    /// Enhanced Flush
712    const ENHANCED_FLUSH = 19, 1 << 6;
713    /// Remote OOB Data Request Negative Reply
714    const REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY = 19, 1 << 7;
715    /// Send Keypress Notification
716    const SEND_KEYPRESS_NOTIFICATION = 20, 1 << 2;
717    /// IO Capability Request Negative Reply
718    const IO_CAPABILITY_REQUEST_NEGATIVE_REPLY = 20, 1 << 3;
719    /// Read Encryption Key Size
720    const READ_ENCRYPTION_KEY_SIZE = 20, 1 << 4;
721    /// Create Physical Link
722    const CREATE_PHYSICAL_LINK = 21, 1 << 0;
723    /// Accept Physical Link
724    const ACCEPT_PHYSICAL_LINK = 21, 1 << 1;
725    /// Disconnect Physical Link
726    const DISCONNECT_PHYSICAL_LINK = 21, 1 << 2;
727    /// Create Logical Link
728    const CREATE_LOGICAL_LINK = 21, 1 << 3;
729    /// Accept Logical Link
730    const ACCEPT_LOGICAL_LINK = 21, 1 << 4;
731    /// Disconnect Logical Link
732    const DISCONNECT_LOGICAL_LINK = 21, 1 << 5;
733    /// Logical Link Cancel
734    const LOGICAL_LINK_CANCEL = 21, 1 << 6;
735    /// Flow Spec Modify
736    const FLOW_SPEC_MODIFY = 21, 1 << 7;
737    /// Read Logical Link Accept Timeout
738    const READ_LOGICAL_LINK_ACCEPT_TIMEOUT = 22, 1 << 0;
739    /// Write Logical Link Accept Timeout
740    const WRITE_LOGICAL_LINK_ACCEPT_TIMEOUT = 22, 1 << 1;
741    /// Set Event Mask Page 2
742    const SET_EVENT_MASK_PAGE_2 = 22, 1 << 2;
743    /// Read Location Data
744    const READ_LOCATION_DATA = 22, 1 << 3;
745    /// Write Location Data
746    const WRITE_LOCATION_DATA = 22, 1 << 4;
747    /// Read Local AMP Info
748    const READ_LOCAL_AMP_INFO = 22, 1 << 5;
749    /// Read Local AMP_ASSOC
750    const READ_LOCAL_AMP_ASSOC = 22, 1 << 6;
751    /// Write Remote AMP_ASSOC
752    const WRITE_REMOTE_AMP_ASSOC = 22, 1 << 7;
753    /// Read Flow Control Mode
754    const READ_FLOW_CONTROL_MODE = 23, 1 << 0;
755    /// Write Flow Control Mode
756    const WRITE_FLOW_CONTROL_MODE = 23, 1 << 1;
757    /// Read Data Block Size
758    const READ_DATA_BLOCK_SIZE = 23, 1 << 2;
759    /// Enable AMP Receiver Reports
760    const ENABLE_AMP_RECEIVER_REPORTS = 23, 1 << 5;
761    /// AMP Test End
762    const AMP_TEST_END = 23, 1 << 6;
763    /// AMP Test
764    const AMP_TEST = 23, 1 << 7;
765    /// Read Enhanced Transmit Power Level
766    const READ_ENHANCED_TRANSMIT_POWER_LEVEL = 24, 1 << 0;
767    /// Read Best Effort Flush Timeout
768    const READ_BEST_EFFORT_FLUSH_TIMEOUT = 24, 1 << 2;
769    /// Write Best Effort Flush Timeout
770    const WRITE_BEST_EFFORT_FLUSH_TIMEOUT = 24, 1 << 3;
771    /// Short Range Mode
772    const SHORT_RANGE_MODE = 24, 1 << 4;
773    /// Read LE Host Support
774    const READ_LE_HOST_SUPPORT = 24, 1 << 5;
775    /// Write LE Host Support
776    const WRITE_LE_HOST_SUPPORT = 24, 1 << 6;
777    /// LE Set Event Mask
778    const LE_SET_EVENT_MASK = 25, 1 << 0;
779    /// LE Read Buffer Size
780    const LE_READ_BUFFER_SIZE = 25, 1 << 1;
781    /// LE Read Local Supported Features
782    const LE_READ_LOCAL_SUPPORTED_FEATURES = 25, 1 << 2;
783    /// LE Set Random Address
784    const LE_SET_RANDOM_ADDRESS = 25, 1 << 4;
785    /// LE Set Advertising Parameters
786    const LE_SET_ADVERTISING_PARAMETERS = 25, 1 << 5;
787    /// LE Read Advertising Channel TX Power
788    const LE_READ_ADVERTISING_CHANNEL_TX_POWER = 25, 1 << 6;
789    /// LE Set Advertising Data
790    const LE_SET_ADVERTISING_DATA = 25, 1 << 7;
791    /// LE Set Scan Response Data
792    const LE_SET_SCAN_RESPONSE_DATA = 26, 1 << 0;
793    /// LE Set Advertise Enable
794    const LE_SET_ADVERTISE_ENABLE = 26, 1 << 1;
795    /// LE Set Scan Parameters
796    const LE_SET_SCAN_PARAMETERS = 26, 1 << 2;
797    /// LE Set Scan Enable
798    const LE_SET_SCAN_ENABLE = 26, 1 << 3;
799    /// LE Create Connection
800    const LE_CREATE_CONNECTION = 26, 1 << 4;
801    /// LE Create Connection Cancel
802    const LE_CREATE_CONNECTION_CANCEL = 26, 1 << 5;
803    /// LE Read White List Size
804    const LE_READ_WHITE_LIST_SIZE = 26, 1 << 6;
805    /// LE Clear White List
806    const LE_CLEAR_WHITE_LIST = 26, 1 << 7;
807    /// LE Add Device To White List
808    const LE_ADD_DEVICE_TO_WHITE_LIST = 27, 1 << 0;
809    /// LE Remove Device From White List
810    const LE_REMOVE_DEVICE_FROM_WHITE_LIST = 27, 1 << 1;
811    /// LE Connection Update
812    const LE_CONNECTION_UPDATE = 27, 1 << 2;
813    /// LE Set Host Channel Classification
814    const LE_SET_HOST_CHANNEL_CLASSIFICATION = 27, 1 << 3;
815    /// LE Read Channel Map
816    const LE_READ_CHANNEL_MAP = 27, 1 << 4;
817    /// LE Read Remote Used Features
818    const LE_READ_REMOTE_USED_FEATURES = 27, 1 << 5;
819    /// LE Encrypt
820    const LE_ENCRYPT = 27, 1 << 6;
821    /// LE Rand
822    const LE_RAND = 27, 1 << 7;
823    /// LE Start Encryption
824    const LE_START_ENCRYPTION = 28, 1 << 0;
825    /// LE Long Term Key Request Reply
826    const LE_LONG_TERM_KEY_REQUEST_REPLY = 28, 1 << 1;
827    /// LE Long Term Key Request Negative Reply
828    const LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY = 28, 1 << 2;
829    /// LE Read Supported States
830    const LE_READ_SUPPORTED_STATES = 28, 1 << 3;
831    /// LE Receiver Test
832    const LE_RECEIVER_TEST = 28, 1 << 4;
833    /// LE Transmitter Test
834    const LE_TRANSMITTER_TEST = 28, 1 << 5;
835    /// LE Test End
836    const LE_TEST_END = 28, 1 << 6;
837    /// Enhanced Setup Synchronous Connection
838    const ENHANCED_SETUP_SYNCHRONOUS_CONNECTION = 29, 1 << 3;
839    /// Enhanced Accept Synchronous Connection
840    const ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION = 29, 1 << 4;
841    /// Read Local Supported Codecs
842    const READ_LOCAL_SUPPORTED_CODECS = 29, 1 << 5;
843    /// Set MWS Channel Parameters Command
844    const SET_MWS_CHANNEL_PARAMETERS_COMMAND = 29, 1 << 6;
845    /// Set External Frame Configuration Command
846    const SET_EXTERNAL_FRAME_CONFIGURATION_COMMAND = 29, 1 << 7;
847    /// Set MWS Signaling Command
848    const SET_MWS_SIGNALING_COMMAND = 30, 1 << 0;
849    /// Set Transport Layer Command
850    const SET_TRANSPORT_LAYER_COMMAND = 30, 1 << 1;
851    /// Set MWS Scan Frequency Table Command
852    const SET_MWS_SCAN_FREQUENCY_TABLE_COMMAND = 30, 1 << 2;
853    /// Get Transport Layer Configuration Command
854    const GET_TRANSPORT_LAYER_CONFIGURATION_COMMAND = 30, 1 << 3;
855    /// Set MWS PATTERN Configuration Command
856    const SET_MWS_PATTERN_CONFIGURATION_COMMAND = 30, 1 << 4;
857    /// Set Triggered Clock Capture
858    const SET_TRIGGERED_CLOCK_CAPTURE = 30, 1 << 5;
859    /// Truncated Page
860    const TRUNCATED_PAGE = 30, 1 << 6;
861    /// Truncated Page Cancel
862    const TRUNCATED_PAGE_CANCEL = 30, 1 << 7;
863    /// Set Connectionless Peripheral Broadcast
864    const SET_CONNECTIONLESS_PERIPHERAL_BROADCAST = 31, 1 << 0;
865    /// Set Connectionless Peripheral Broadcast Receive
866    const SET_CONNECTIONLESS_PERIPHERAL_BROADCAST_RECEIVE = 31, 1 << 1;
867    /// Start Synchronization Train
868    const START_SYNCHRONIZATION_TRAIN = 31, 1 << 2;
869    /// Receive Synchronization Train
870    const RECEIVE_SYNCHRONIZATION_TRAIN = 31, 1 << 3;
871    /// Set Connectionless Peripheral Broadcast Data
872    const SET_CONNECTIONLESS_PERIPHERAL_BROADCAST_DATA = 31, 1 << 6;
873    /// Read Synchronization Train Parameters
874    const READ_SYNCHRONIZATION_TRAIN_PARAMETERS = 31, 1 << 7;
875    /// Write Synchronization Train Parameters
876    const WRITE_SYNCHRONIZATION_TRAIN_PARAMETERS = 32, 1 << 0;
877    /// Remote OOB Extended Data Request Reply
878    const REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY = 32, 1 << 1;
879    /// Read Secure Connections Host Support
880    const READ_SECURE_CONNECTIONS_HOST_SUPPORT = 32, 1 << 2;
881    /// Write Secure Connections Host Support
882    const WRITE_SECURE_CONNECTIONS_HOST_SUPPORT = 32, 1 << 3;
883    /// Read Authenticated Payload Timeout
884    const READ_AUTHENTICATED_PAYLOAD_TIMEOUT = 32, 1 << 4;
885    /// Write Authenticated Payload Timeout
886    const WRITE_AUTHENTICATED_PAYLOAD_TIMEOUT = 32, 1 << 5;
887    /// Read Local OOB Extended Data
888    const READ_LOCAL_OOB_EXTENDED_DATA = 32, 1 << 6;
889    /// Write Secure Connections Test Mode
890    const WRITE_SECURE_CONNECTIONS_TEST_MODE = 32, 1 << 7;
891    /// Read Extended Page Timeout
892    const READ_EXTENDED_PAGE_TIMEOUT = 33, 1 << 0;
893    /// Write Extended Page Timeout
894    const WRITE_EXTENDED_PAGE_TIMEOUT = 33, 1 << 1;
895    /// Read Extended Inquiry Length
896    const READ_EXTENDED_INQUIRY_LENGTH = 33, 1 << 2;
897    /// Write Extended Inquiry Length
898    const WRITE_EXTENDED_INQUIRY_LENGTH = 33, 1 << 3;
899    /// LE Remote Connection Parameter Request Reply Command
900    const LE_REMOTE_CONNECTION_PARAMETER_REQUEST_REPLY_COMMAND = 33, 1 << 4;
901    /// LE Remote Connection Parameter Request Negative Reply Command
902    const LE_REMOTE_CONNECTION_PARAMETER_REQUEST_NEGATIVE_REPLY_COMMAND = 33, 1 << 5;
903    /// LE Set Data Length
904    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
905    const LE_SET_DATA_LENGTH = 33, 1 << 6;
906    /// LE Read Suggested Default Data Length
907    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
908    const LE_READ_SUGGESTED_DEFAULT_DATA_LENGTH = 33, 1 << 7;
909    /// LE Write Suggested Default Data Length
910    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
911    const LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH = 34, 1 << 0;
912    /// LE Read Local P-256 Public Key
913    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
914    const LE_READ_LOCAL_P256_PUBLIC_KEY = 34, 1 << 1;
915    /// LE Generate DH Key
916    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
917    const LE_GENERATE_DH_KEY = 34, 1 << 2;
918    /// LE Add Device To Resolving List
919    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
920    const LE_ADD_DEVICE_TO_RESOLVING_LIST = 34, 1 << 3;
921    /// LE Remove Device From Resolving List
922    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
923    const LE_REMOVE_DEVICE_FROM_RESOLVING_LIST = 34, 1 << 4;
924    /// LE Clear Resolving List
925    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
926    const LE_CLEAR_RESOLVING_LIST = 34, 1 << 5;
927    /// LE Read Resolving List Size
928    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
929    const LE_READ_RESOLVING_LIST_SIZE = 34, 1 << 6;
930    /// LE Read Peer Resolvable Address
931    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
932    const LE_READ_PEER_RESOLVABLE_ADDRESS = 34, 1 << 7;
933    /// LE Read Local Resolvable Address
934    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
935    const LE_READ_LOCAL_RESOLVABLE_ADDRESS = 35, 1 << 0;
936    /// LE Set Address Resolution Enable
937    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
938    const LE_SET_ADDRESS_RESOLUTION_ENABLE = 35, 1 << 1;
939    /// LE Set Resolvable Private Address Timeout
940    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
941    const LE_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT = 35, 1 << 2;
942    /// LE Read Maximum Data Length
943    #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
944    const LE_READ_MAXIMUM_DATA_LENGTH = 35, 1 << 3;
945    /// LE Read PHY Command
946    #[cfg(feature = "version-5-0")]
947    const LE_READ_PHY_COMMAND = 35, 1 << 4;
948    /// LE Set Default PHY Command
949    #[cfg(feature = "version-5-0")]
950    const LE_SET_DEFAULT_PHY_COMMAND = 35, 1 << 5;
951    /// LE Set PHY Command
952    #[cfg(feature = "version-5-0")]
953    const LE_SET_PHY_COMMAND = 35, 1 << 6;
954    /// LE Enhanced Receiver Test Command
955    #[cfg(feature = "version-5-0")]
956    const LE_ENHANCED_RECEIVER_TEST_COMMAND = 35, 1 << 7;
957    /// LE Enhanced Transmitter Test Command
958    #[cfg(feature = "version-5-0")]
959    const LE_ENHANCED_TRANSMITTER_TEST_COMMAND = 36, 1 << 0;
960    /// LE Set Advertising Set Random Address Command
961    #[cfg(feature = "version-5-0")]
962    const LE_SET_ADVERTISING_SET_RANDOM_ADDRESS_COMMAND = 36, 1 << 1;
963    /// LE Set Extended Advertising Parameters Command
964    #[cfg(feature = "version-5-0")]
965    const LE_SET_EXTENDED_ADVERTISING_PARAMETERS_COMMAND = 36, 1 << 2;
966    /// LE Set Extended Advertising Data Command
967    #[cfg(feature = "version-5-0")]
968    const LE_SET_EXTENDED_ADVERTISING_DATA_COMMAND = 36, 1 << 3;
969    /// LE Set Extended Scan Response Data Command
970    #[cfg(feature = "version-5-0")]
971    const LE_SET_EXTENDED_SCAN_RESPONSE_DATA_COMMAND = 36, 1 << 4;
972    /// LE Set Extended Advertising Enable Command
973    #[cfg(feature = "version-5-0")]
974    const LE_SET_EXTENDED_ADVERTISING_ENABLE_COMMAND = 36, 1 << 5;
975    /// LE Read Maximum Advertising Data Length Command
976    #[cfg(feature = "version-5-0")]
977    const LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH_COMMAND = 36, 1 << 6;
978    /// LE Read Number of Supported Advertising Sets Command
979    #[cfg(feature = "version-5-0")]
980    const LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS_COMMAND = 36, 1 << 7;
981    /// LE Remove Advertising Set Command
982    #[cfg(feature = "version-5-0")]
983    const LE_REMOVE_ADVERTISING_SET_COMMAND = 37, 1 << 0;
984    /// LE Clear Advertising Sets Command
985    #[cfg(feature = "version-5-0")]
986    const LE_CLEAR_ADVERTISING_SETS_COMMAND = 37, 1 << 1;
987    /// LE Set Periodic Advertising Parameters Command
988    #[cfg(feature = "version-5-0")]
989    const LE_SET_PERIODIC_ADVERTISING_PARAMETERS_COMMAND = 37, 1 << 2;
990    /// LE Set Periodic Advertising Data Command
991    #[cfg(feature = "version-5-0")]
992    const LE_SET_PERIODIC_ADVERTISING_DATA_COMMAND = 37, 1 << 3;
993    /// LE Set Periodic Advertising Enable Command
994    #[cfg(feature = "version-5-0")]
995    const LE_SET_PERIODIC_ADVERTISING_ENABLE_COMMAND = 37, 1 << 4;
996    /// LE Set Extended Scan Parameters Command
997    #[cfg(feature = "version-5-0")]
998    const LE_SET_EXTENDED_SCAN_PARAMETERS_COMMAND = 37, 1 << 5;
999    /// LE Set Extended Scan Enable Command
1000    #[cfg(feature = "version-5-0")]
1001    const LE_SET_EXTENDED_SCAN_ENABLE_COMMAND = 37, 1 << 6;
1002    /// LE Extended Create Connection Command
1003    #[cfg(feature = "version-5-0")]
1004    const LE_EXTENDED_CREATE_CONNECTION_COMMAND = 37, 1 << 7;
1005    /// LE Periodic Advertising Create Sync Command
1006    #[cfg(feature = "version-5-0")]
1007    const LE_PERIODIC_ADVERTISING_CREATE_SYNC_COMMAND = 38, 1 << 0;
1008    /// LE Periodic Advertising Create Sync Cancel Command
1009    #[cfg(feature = "version-5-0")]
1010    const LE_PERIODIC_ADVERTISING_CREATE_SYNC_CANCEL_COMMAND = 38, 1 << 1;
1011    /// LE Periodic Advertising Terminate Sync Command
1012    #[cfg(feature = "version-5-0")]
1013    const LE_PERIODIC_ADVERTISING_TERMINATE_SYNC_COMMAND = 38, 1 << 2;
1014    /// LE Add Device To Periodic Advertiser List Command
1015    #[cfg(feature = "version-5-0")]
1016    const LE_ADD_DEVICE_TO_PERIODIC_ADVERTISER_LIST_COMMAND = 38, 1 << 3;
1017    /// LE Remove Device From Periodic Advertiser List Command
1018    #[cfg(feature = "version-5-0")]
1019    const LE_REMOVE_DEVICE_FROM_PERIODIC_ADVERTISER_LIST_COMMAND = 38, 1 << 4;
1020    /// LE Clear Periodic Advertiser List Command
1021    #[cfg(feature = "version-5-0")]
1022    const LE_CLEAR_PERIODIC_ADVERTISER_LIST_COMMAND = 38, 1 << 5;
1023    /// LE Read Periodic Advertiser List Size Command
1024    #[cfg(feature = "version-5-0")]
1025    const LE_READ_PERIODIC_ADVERTISER_LIST_SIZE_COMMAND = 38, 1 << 6;
1026    /// LE Read Transmit Power Command
1027    #[cfg(feature = "version-5-0")]
1028    const LE_READ_TRANSMIT_POWER_COMMAND = 38, 1 << 7;
1029    /// LE Read RF Path Compensation Command
1030    #[cfg(feature = "version-5-0")]
1031    const LE_READ_RF_PATH_COMPENSATION_COMMAND = 39, 1 << 0;
1032    /// LE Write RF Path Compensation Command
1033    #[cfg(feature = "version-5-0")]
1034    const LE_WRITE_RF_PATH_COMPENSATION_COMMAND = 39, 1 << 1;
1035    /// LE Set Privacy Mode
1036    #[cfg(feature = "version-5-0")]
1037    const LE_SET_PRIVACY_MODE = 39, 1 << 2;
1038}
1039
1040impl Debug for CommandFlags {
1041    fn fmt(&self, f: &mut Formatter) -> FmtResult {
1042        writeln!(f, "{:?}", &self.0[..16])?;
1043        writeln!(f, "{:?}", &self.0[16..32])?;
1044        writeln!(f, "{:?}", &self.0[32..39])
1045    }
1046}
1047
1048impl<'a> TryFrom<&'a [u8]> for CommandFlags {
1049    type Error = crate::event::Error<crate::event::NeverError>;
1050    fn try_from(value: &[u8]) -> Result<CommandFlags, Self::Error> {
1051        require_len!(value, COMMAND_FLAGS_SIZE);
1052
1053        CommandFlags::from_bits(value).ok_or(crate::event::Error::BadCommandFlag)
1054    }
1055}
1056
1057fn to_supported_commands<VE, VS>(
1058    bytes: &[u8],
1059) -> Result<LocalSupportedCommands<VS>, crate::event::Error<VE>>
1060where
1061    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1062{
1063    require_len!(bytes, 1 + COMMAND_FLAGS_SIZE);
1064    Ok(LocalSupportedCommands {
1065        status: bytes[0].try_into().map_err(super::rewrap_bad_status)?,
1066        supported_commands: bytes[1..=COMMAND_FLAGS_SIZE]
1067            .try_into()
1068            .map_err(|e| match e {
1069                crate::event::Error::BadLength(actual, expected) => {
1070                    crate::event::Error::BadLength(actual, expected)
1071                }
1072                crate::event::Error::BadCommandFlag => crate::event::Error::BadCommandFlag,
1073                _ => unreachable!(),
1074            })?,
1075    })
1076}
1077
1078/// Values returned by the [Read Local Supported
1079/// Features](crate::host::Hci::read_local_supported_features) command.
1080#[derive(Copy, Clone, Debug)]
1081pub struct LocalSupportedFeatures<VS> {
1082    /// Did the command fail, and if so, how?
1083    pub status: Status<VS>,
1084
1085    /// Flags for supported features.
1086    pub supported_features: LmpFeatures,
1087}
1088
1089bitflags! {
1090    /// See the Bluetooth Specification, v4.1 or later, Vol 2, Part C, Section 3.3 (Table 3.2).
1091    pub struct LmpFeatures : u64 {
1092        /// 3-slot packets
1093        const THREE_SLOT_PACKETS = 1 << 0;
1094        /// 5-slot packets
1095        const FIVE_SLOT_PACKETS = 1 << 1;
1096        /// Encryption
1097        const ENCRYPTION = 1 << 2;
1098        /// Slot offset
1099        const SLOT_OFFSET = 1 << 3;
1100        /// Timing accuracy
1101        const TIMING_ACCURACY = 1 << 4;
1102        /// Role switch
1103        const ROLE_SWITCH = 1 << 5;
1104        /// Hold mode
1105        const HOLD_MODE = 1 << 6;
1106        /// Sniff mode
1107        const SNIFF_MODE = 1 << 7;
1108        /// Power control requests
1109        const POWER_CONTROL_REQUESTS = 1 << 9;
1110        /// Channel quality driven data rate (CQDDR)
1111        const CHANNEL_QUALITY_DRIVEN_DATA_RATE_CQDDR = 1 << 10;
1112        /// SCO link
1113        const SCO_LINK = 1 << 11;
1114        /// HV2 packets
1115        const HV2_PACKETS = 1 << 12;
1116        /// HV3 packets
1117        const HV3_PACKETS = 1 << 13;
1118        /// μ-law log synchronous data
1119        const MU_LAW_LOG_SYNCHRONOUS_DATA = 1 << 14;
1120        /// A-law log synchronous data
1121        const A_LAW_LOG_SYNCHRONOUS_DATA = 1 << 15;
1122        /// CVSD synchronous data
1123        const CVSD_SYNCHRONOUS_DATA = 1 << 16;
1124        /// Paging parameter negotiation
1125        const PAGING_PARAMETER_NEGOTIATION = 1 << 17;
1126        /// Power control
1127        const POWER_CONTROL = 1 << 18;
1128        /// Transparent synchronous data
1129        const TRANSPARENT_SYNCHRONOUS_DATA = 1 << 19;
1130        /// Flow control lag (least significant bit)
1131        const FLOW_CONTROL_LAG_LSB = 1 << 20;
1132        /// Flow control lag (middle bit)
1133        const FLOW_CONTROL_LAG_MID = 1 << 21;
1134        /// Flow control lag (most significant bit)
1135        const FLOW_CONTROL_LAG_MSB = 1 << 22;
1136        /// Broadcast Encryption
1137        const BROADCAST_ENCRYPTION = 1 << 23;
1138        /// Enhanced Data Rate ACL 2 Mb/s mode
1139        const ENHANCED_DATA_RATE_ACL_2_MB_PER_S_MODE = 1 << 25;
1140        /// Enhanced Data Rate ACL 3 Mb/s mode
1141        const ENHANCED_DATA_RATE_ACL_3_MB_PER_S_MODE = 1 << 26;
1142        /// Enhanced inquiry scan
1143        const ENHANCED_INQUIRY_SCAN = 1 << 27;
1144        /// Interlaced inquiry scan
1145        const INTERLACED_INQUIRY_SCAN = 1 << 28;
1146        /// Interlaced page scan
1147        const INTERLACED_PAGE_SCAN = 1 << 29;
1148        /// RSSI with inquiry results
1149        const RSSI_WITH_INQUIRY_RESULTS = 1 << 30;
1150        /// Extended SCO link (EV3 packets)
1151        const EXTENDED_SCO_LINK_EV3_PACKETS = 1 << 31;
1152        /// EV4 packets
1153        const EV4_PACKETS = 1 << 32;
1154        /// EV5 packets
1155        const EV5_PACKETS = 1 << 33;
1156        /// AFH capable peripheral
1157        const AFH_CAPABLE_PERIPHERAL = 1 << 35;
1158        /// AFH classification peripheral
1159        const AFH_CLASSIFICATION_PERIPHERAL = 1 << 36;
1160        /// BR/EDR Not Supported
1161        const BR_EDR_NOT_SUPPORTED = 1 << 37;
1162        /// LE Supported (Controller)
1163        const LE_SUPPORTED_BY_CONTROLLER = 1 << 38;
1164        /// 3-slot Enhanced Data Rate ACL packets
1165        const THREE_SLOT_ENHANCED_DATA_RATE_ACL_PACKETS = 1 << 39;
1166        /// 5-slot Enhanced Data Rate ACL packets
1167        const FIVE_SLOT_ENHANCED_DATA_RATE_ACL_PACKETS = 1 << 40;
1168        /// Sniff subrating
1169        const SNIFF_SUBRATING = 1 << 41;
1170        /// Pause encryption
1171        const PAUSE_ENCRYPTION = 1 << 42;
1172        /// AFH capable central device
1173        const AFH_CAPABLE_CENTRAL_DEVICE = 1 << 43;
1174        /// AFH classification central device
1175        const AFH_CLASSIFICATION_CENTRAL_DEVICE = 1 << 44;
1176        /// Enhanced Data Rate eSCO 2 Mb/s mode
1177        const ENHANCED_DATA_RATE_ESCO_2_MB_PER_S_MODE = 1 << 45;
1178        /// Enhanced Data Rate eSCO 3 Mb/s mode
1179        const ENHANCED_DATA_RATE_ESCO_3_MB_PER_S_MODE = 1 << 46;
1180        /// 3-slot Enhanced Data Rate eSCO packets
1181        const THREE_SLOT_ENHANCED_DATA_RATE_ESCO_PACKETS = 1 << 47;
1182        /// Extended Inquiry Response
1183        const EXTENDED_INQUIRY_RESPONSE = 1 << 48;
1184        /// Simultaneous LE and BR/EDR to Same Device Capable (Controller)
1185        const SIMULTANEOUS_LE_AND_BR_EDR_TO_SAME_DEVICE_CAPABLE = 1 << 49;
1186        /// Secure Simple Pairing
1187        const SECURE_SIMPLE_PAIRING = 1 << 51;
1188        /// Encapsulated PDU
1189        const ENCAPSULATED_PDU = 1 << 52;
1190        /// Erroneous Data Reporting
1191        const ERRONEOUS_DATA_REPORTING = 1 << 53;
1192        /// Non-flushable Packet Boundary Flag
1193        const NON_FLUSHABLE_PACKET_BOUNDARY_FLAG = 1 << 54;
1194        /// Link Supervision Timeout Changed Event
1195        const LINK_SUPERVISION_TIMEOUT_CHANGED_EVENT = 1 << 56;
1196        /// Inquiry TX Power Level
1197        const INQUIRY_TX_POWER_LEVEL = 1 << 57;
1198        /// Enhanced Power Control
1199        const ENHANCED_POWER_CONTROL = 1 << 58;
1200        /// Extended features
1201        const EXTENDED_FEATURES = 1 << 63;
1202    }
1203}
1204
1205fn to_supported_features<VE, VS>(
1206    bytes: &[u8],
1207) -> Result<LocalSupportedFeatures<VS>, crate::event::Error<VE>>
1208where
1209    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1210{
1211    require_len!(bytes, 9);
1212    Ok(LocalSupportedFeatures {
1213        status: to_status(bytes)?,
1214        supported_features: LmpFeatures::from_bits_truncate(LittleEndian::read_u64(&bytes[1..])),
1215    })
1216}
1217
1218/// Values returned by the [Read BD ADDR](crate::host::Hci::read_bd_addr) command.
1219#[derive(Copy, Clone, Debug)]
1220pub struct ReadBdAddr<VS> {
1221    /// Did the command fail, and if so, how?
1222    pub status: Status<VS>,
1223
1224    /// Address of the device.
1225    pub bd_addr: crate::BdAddr,
1226}
1227
1228fn to_bd_addr<VE, VS>(bytes: &[u8]) -> Result<ReadBdAddr<VS>, crate::event::Error<VE>>
1229where
1230    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1231{
1232    require_len!(bytes, 7);
1233    let mut bd_addr = crate::BdAddr([0; 6]);
1234    bd_addr.0.copy_from_slice(&bytes[1..]);
1235    Ok(ReadBdAddr {
1236        status: to_status(bytes)?,
1237        bd_addr,
1238    })
1239}
1240
1241/// Values returned by the [Read RSSI](crate::host::Hci::read_rssi) command.
1242#[derive(Copy, Clone, Debug)]
1243pub struct ReadRssi<VS> {
1244    /// Did the command fail, and if so, how?
1245    pub status: Status<VS>,
1246
1247    /// The Handle for the connection for which the RSSI has been read.
1248    ///
1249    /// The Handle is a connection handle for a BR/EDR Controller and a physical link handle for an
1250    /// AMP Controller.
1251    pub conn_handle: ConnectionHandle,
1252
1253    /// - BR/EDR
1254    ///   - No range restriction
1255    ///   - Units: dB
1256    /// - AMP:
1257    ///   - Range: AMP type specific
1258    ///   - Units: dBm
1259    /// - LE:
1260    ///   - Range: -127 to 20, range not checked by this implementation. 127 indicates RSSI not
1261    ///     available.
1262    ///   - Units: dBm
1263    pub rssi: i8,
1264}
1265
1266fn to_read_rssi<VE, VS>(bytes: &[u8]) -> Result<ReadRssi<VS>, crate::event::Error<VE>>
1267where
1268    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1269{
1270    require_len!(bytes, 4);
1271    Ok(ReadRssi {
1272        status: to_status(bytes)?,
1273        conn_handle: ConnectionHandle(LittleEndian::read_u16(&bytes[1..])),
1274        rssi: unsafe { mem::transmute::<u8, i8>(bytes[3]) },
1275    })
1276}
1277
1278/// Values returned by the [LE Read Buffer Size](crate::host::Hci::le_read_buffer_size) command.
1279#[derive(Copy, Clone, Debug)]
1280pub struct LeReadBufferSize<VS> {
1281    /// Did the command fail, and if so, how?
1282    pub status: Status<VS>,
1283
1284    /// The size of the L2CAP PDU segments contained in ACL Data Packets, which are transferred from
1285    /// the Host to the Controller to be broken up into packets by the Link Layer. Both the Host and
1286    /// the Controller shall support command and event packets, where the data portion (excluding
1287    /// header) contained in the packets is 255 octets in size.
1288    ///
1289    /// Note: Does not include the length of the HCI Data Packet header.
1290    ///
1291    /// If `data_packet_count` is 0, then the controller has no dedicated LE read buffer, so the
1292    /// caller should use the `read_buffer_size` command.
1293    pub data_packet_length: u16,
1294
1295    /// Contains the total number of HCI ACL Data Packets that can be stored in the data buffers of
1296    /// the Controller. The Host determines how the buffers are to be divided between different
1297    /// Connection Handles.
1298    ///
1299    /// If `data_packet_count` is 0, then the controller has no dedicated LE read buffer, so the
1300    /// caller should use the `read_buffer_size` command.
1301    pub data_packet_count: u8,
1302}
1303
1304fn to_le_read_buffer_status<VE, VS>(
1305    bytes: &[u8],
1306) -> Result<LeReadBufferSize<VS>, crate::event::Error<VE>>
1307where
1308    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1309{
1310    require_len!(bytes, 4);
1311    Ok(LeReadBufferSize {
1312        status: to_status(bytes)?,
1313        data_packet_length: LittleEndian::read_u16(&bytes[1..]),
1314        data_packet_count: bytes[3],
1315    })
1316}
1317
1318/// Values returned by the [LE Read Local Supported
1319/// Features](crate::host::Hci::le_read_local_supported_features) command.
1320#[derive(Copy, Clone, Debug)]
1321pub struct LeSupportedFeatures<VS> {
1322    /// Did the command fail, and if so, how?
1323    pub status: Status<VS>,
1324
1325    /// Supported LE features.
1326    pub supported_features: LeFeatures,
1327}
1328
1329bitflags! {
1330    /// Possible LE features for the [LE Read Local Supported
1331    /// Features](::host::Hci::le_read_local_supported_features) command.  See the Bluetooth
1332    /// specification, Vol 6, Part B, Section 4.6.  See Table 4.3 (v4.1 of the spec), Table 4.4
1333    /// (v4.2 and v5.0).
1334    pub struct LeFeatures : u64 {
1335        /// LE Encryption.  Valid from controller to controller.
1336        const ENCRYPTION = 1 << 0;
1337        /// Connection Parameters Request Procedure.  Valid from controller to controller.
1338        const CONNECTION_PARAMETERS_REQUEST_PROCEDURE = 1 << 1;
1339        /// Extended Reject Indication.  Valid from controller to controller.
1340        const EXTENDED_REJECT_INDICATION = 1 << 2;
1341        /// Peripheral-initiated Features Exchange.  Valid from controller to controller.
1342        const PERIPHERALINITIATED_FEATURES_EXCHANGE = 1 << 3;
1343        /// LE Ping.  Not valid from controller to controller.
1344        const PING = 1 << 4;
1345        /// LE Data Packet Length Extension.  Valid from controller to controller.
1346        #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
1347        const DATA_PACKET_LENGTH_EXTENSION = 1 << 5;
1348        /// LL Privacy.  Not valid from controller to controller.
1349        #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
1350        const LL_PRIVACY = 1 << 6;
1351        /// Extended Scanner Filter Policies.  Not valid from controller to controller.
1352        #[cfg(any(feature = "version-4-2", feature = "version-5-0"))]
1353        const EXTENDED_SCANNER_FILTER_POLICIES = 1 << 7;
1354        /// LE 2M PHY.  Valid from controller to controller.
1355        #[cfg(feature = "version-5-0")]
1356        const PHY_2M = 1 << 8;
1357        /// Stable Modulation Index - Transmitter.  Valid from controller to controller.
1358        #[cfg(feature = "version-5-0")]
1359        const STABLE_MODULATION_INDEX_TX = 1 << 9;
1360        /// Stable Modulation Index - Receiver.  Valid from controller to controller.
1361        #[cfg(feature = "version-5-0")]
1362        const STABLE_MODULATION_INDEX_RX = 1 << 10;
1363        /// LE Coded PHY.  Valid from controller to controller.
1364        #[cfg(feature = "version-5-0")]
1365        const CODED_PHY = 1 << 11;
1366        /// LE Extended Advertising.  Not valid from controller to controller.
1367        #[cfg(feature = "version-5-0")]
1368        const EXTENDED_ADVERTISING = 1 << 12;
1369        /// LE Periodic Advertising.  Not valid from controller to controller.
1370        #[cfg(feature = "version-5-0")]
1371        const PERIODIC_ADVERTISING = 1 << 13;
1372        /// Channel Selection Algorithm #2.  Valid from controller to controller.
1373        #[cfg(feature = "version-5-0")]
1374        const CHANNEL_SELECTION_ALGORITHM_2 = 1 << 14;
1375        /// LE Power Class 1.  Valid from controller to controller.
1376        #[cfg(feature = "version-5-0")]
1377        const POWER_CLASS_1 = 1 << 15;
1378        /// Minimum Number of Used Channels Procedure
1379        #[cfg(feature = "version-5-0")]
1380        const MINIMUM_NUMBER_OF_USED_CHANNELS_PROCEDURE = 1 << 16;
1381    }
1382}
1383
1384fn to_le_local_supported_features<VE, VS>(
1385    bytes: &[u8],
1386) -> Result<LeSupportedFeatures<VS>, crate::event::Error<VE>>
1387where
1388    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1389{
1390    require_len!(bytes, 9);
1391    Ok(LeSupportedFeatures {
1392        status: to_status(bytes)?,
1393        supported_features: LeFeatures::from_bits_truncate(LittleEndian::read_u64(&bytes[1..])),
1394    })
1395}
1396
1397/// Values returned by the [LE Read Advertising Channel TX
1398/// Power](crate::host::Hci::le_read_advertising_channel_tx_power) command.
1399#[derive(Copy, Clone, Debug)]
1400pub struct LeAdvertisingChannelTxPower<VS> {
1401    /// Did the command fail, and if so, how?
1402    pub status: Status<VS>,
1403    /// The transmit power of the advertising channel.
1404    ///   - Range: -20 ≤ N ≤ 10 (this is not enforced in this implementation)
1405    ///   - Units: dBm
1406    ///   - Accuracy: ±4 dB
1407    pub power: i8,
1408}
1409
1410fn to_le_advertising_channel_tx_power<VE, VS>(
1411    bytes: &[u8],
1412) -> Result<LeAdvertisingChannelTxPower<VS>, crate::event::Error<VE>>
1413where
1414    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1415{
1416    require_len!(bytes, 2);
1417    Ok(LeAdvertisingChannelTxPower {
1418        status: to_status(bytes)?,
1419        power: unsafe { mem::transmute::<u8, i8>(bytes[1]) },
1420    })
1421}
1422
1423#[cfg(not(feature = "version-5-0"))]
1424fn to_le_set_advertise_enable<V>(status: Status<V::Status>) -> ReturnParameters<V>
1425where
1426    V: super::VendorEvent,
1427{
1428    ReturnParameters::LeSetAdvertiseEnable(status)
1429}
1430
1431#[cfg(feature = "version-5-0")]
1432fn to_le_set_advertise_enable<V>(status: Status<V::Status>) -> ReturnParameters<V>
1433where
1434    V: super::VendorEvent,
1435{
1436    ReturnParameters::LeSetAdvertisingEnable(status)
1437}
1438
1439/// Parameters returned by the [LE Read Channel Map](crate::host::Hci::le_read_channel_map) command.
1440#[derive(Copy, Clone, Debug)]
1441pub struct ChannelMapParameters<VS> {
1442    /// Did the command fail, and if so, how?
1443    pub status: Status<VS>,
1444
1445    /// Connection handle whose channel map is returned.
1446    pub conn_handle: ConnectionHandle,
1447
1448    /// Channels that may be used for this connection.
1449    pub channel_map: crate::ChannelClassification,
1450}
1451
1452fn to_le_channel_map_parameters<VE, VS>(
1453    bytes: &[u8],
1454) -> Result<ChannelMapParameters<VS>, crate::event::Error<VE>>
1455where
1456    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1457{
1458    require_len!(bytes, 8);
1459
1460    let mut channel_bits = [0; 5];
1461    channel_bits.copy_from_slice(&bytes[3..8]);
1462    let channel_bits = channel_bits;
1463    Ok(ChannelMapParameters {
1464        status: to_status(&bytes[0..])?,
1465        conn_handle: ConnectionHandle(LittleEndian::read_u16(&bytes[1..])),
1466        channel_map: crate::ChannelClassification::from_bits(&bytes[3..])
1467            .ok_or_else(|| crate::event::Error::InvalidChannelMap(channel_bits))?,
1468    })
1469}
1470
1471/// Parameters returned by the [LE Encrypt](crate::host::Hci::le_encrypt) command.
1472#[derive(Copy, Clone, Debug)]
1473pub struct EncryptedReturnParameters<VS> {
1474    /// Did the command fail, and if so, how?
1475    pub status: Status<VS>,
1476
1477    /// Encrypted data block.
1478    ///
1479    /// The most significant octet (last) of the block corresponds to `out[0]` using the notation
1480    /// specified in FIPS 197.
1481    pub encrypted_data: EncryptedBlock,
1482}
1483
1484/// Newtype for a 128-bit encrypted block of data.
1485///
1486/// See [`EncryptedReturnParameters`].
1487#[derive(Copy, Clone)]
1488pub struct EncryptedBlock(pub [u8; 16]);
1489
1490impl Debug for EncryptedBlock {
1491    fn fmt(&self, f: &mut Formatter) -> FmtResult {
1492        writeln!(f, "AES-128 Encrypted Data ({:X?})", &self.0)
1493    }
1494}
1495
1496fn to_le_encrypted_data<VE, VS>(
1497    bytes: &[u8],
1498) -> Result<EncryptedReturnParameters<VS>, crate::event::Error<VE>>
1499where
1500    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1501{
1502    require_len!(bytes, 17);
1503
1504    let mut block = [0; 16];
1505    block.copy_from_slice(&bytes[1..]);
1506    Ok(EncryptedReturnParameters {
1507        status: to_status(&bytes)?,
1508        encrypted_data: EncryptedBlock(block),
1509    })
1510}
1511
1512/// Return parameters for the [LE Rand](crate::host::Hci::le_rand) command.
1513#[derive(Copy, Clone, Debug)]
1514pub struct LeRandom<VS> {
1515    /// Did the command fail, and if so, how?
1516    pub status: Status<VS>,
1517
1518    /// Controller-generated random number.
1519    pub random_number: u64,
1520}
1521
1522fn to_random_number<VE, VS>(bytes: &[u8]) -> Result<LeRandom<VS>, crate::event::Error<VE>>
1523where
1524    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1525{
1526    require_len!(bytes, 9);
1527
1528    Ok(LeRandom {
1529        status: to_status(&bytes)?,
1530        random_number: LittleEndian::read_u64(&bytes[1..]),
1531    })
1532}
1533
1534/// Parameters returned by the [LE LTK Request
1535/// Reply](crate::host::Hci::le_long_term_key_request_reply) command.
1536#[derive(Copy, Clone, Debug)]
1537pub struct LeLongTermRequestReply<VS> {
1538    /// Did the command fail, and if so, how?
1539    pub status: Status<VS>,
1540
1541    /// Connection handle that the request came from
1542    pub conn_handle: ConnectionHandle,
1543}
1544
1545fn to_le_ltk_request_reply<VE, VS>(
1546    bytes: &[u8],
1547) -> Result<LeLongTermRequestReply<VS>, crate::event::Error<VE>>
1548where
1549    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1550{
1551    require_len!(bytes, 3);
1552
1553    Ok(LeLongTermRequestReply {
1554        status: to_status(&bytes)?,
1555        conn_handle: ConnectionHandle(LittleEndian::read_u16(&bytes[1..])),
1556    })
1557}
1558
1559/// Parameters returned by the [LE Read Supported
1560/// States](crate::host::Hci::le_read_supported_states) command.
1561#[derive(Copy, Clone, Debug)]
1562pub struct LeReadSupportedStates<VS> {
1563    /// Did the command fail, and if so, how?
1564    pub status: Status<VS>,
1565
1566    /// States or state combinations supported by the Controller. Multiple state and state
1567    /// combinations may be supported.
1568    pub supported_states: LeStates,
1569}
1570
1571bitflags! {
1572    /// Possible LE states or state combinations for the [LE Read Supported
1573    /// States](::host::Hci::le_read_supported_states) command.
1574    pub struct LeStates : u64 {
1575        /// Non-connectable advertising state alone.
1576        const NON_CONNECTABLE_ADVERTISING = 1 << 0;
1577        /// Scannable advertising state alone
1578        const SCANNABLE_ADVERTISING = 1 << 1;
1579        /// Connectable advertising state alone
1580        const CONNECTABLE_ADVERTISING = 1 << 2;
1581        /// Directed advertising (high duty cycle) state alone
1582        const DIRECTED_ADVERTISING_HIGH_DUTY_CYCLE = 1 << 3;
1583        /// Passive scanning state alone
1584        const PASSIVE_SCANNING = 1 << 4;
1585        /// Active scanning state alone
1586        const ACTIVE_SCANNING = 1 << 5;
1587        /// Initianing state alone
1588        const INITIATING = 1 << 6;
1589        /// Peripheral (slave) connection state alone
1590        const PERIPHERAL_CONNECTION = 1 << 7;
1591        /// Non-connectable advertising and passive scan states.
1592        const NONCONN_AD_AND_PASS_SCAN = 1 << 8;
1593        /// Scannable advertising and passive scan states
1594        const SCAN_AD_AND_PASS_SCAN = 1 << 9;
1595        /// Connectable advertising and passive scan states
1596        const CONN_AD_AND_PASS_SCAN = 1 << 10;
1597        /// Directed advertising (high duty cycle) and passive scan states
1598        const DIR_AD_HDC_AND_PASS_SCAN = 1 << 11;
1599        /// Non-connectable advertising and active scan states.
1600        const NONCONN_AD_AND_ACT_SCAN = 1 << 12;
1601        /// Scannable advertising and active scan states
1602        const SCAN_AD_AND_ACT_SCAN = 1 << 13;
1603        /// Connectable advertising and active scan states
1604        const CONN_AD_AND_ACT_SCAN = 1 << 14;
1605        /// Directed advertising (high duty cycle) and active scan states
1606        const DIR_AD_HDC_AND_ACT_SCAN = 1 << 15;
1607        /// Non-connectable advertising and initiating states.
1608        const NONCONN_AD_AND_INITIATING = 1 << 16;
1609        /// Scannable advertising and initiating states
1610        const SCAN_AD_AND_INITIATING = 1 << 17;
1611        /// Non-connectable advertising and central (master) connection states.
1612        const NONCONN_AD_AND_CENTRAL_CONN = 1 << 18;
1613        /// Scannable advertising and central (master) connection states
1614        const SCAN_AD_AND_CENTRAL_CONN = 1 << 19;
1615        /// Non-connectable advertising and peripheral (slave) connection states.
1616        const NONCONN_AD_AND_PERIPH_CONN = 1 << 20;
1617        /// Scannable advertising and peripheral (slave) connection states
1618        const SCAN_AD_AND_PERIPH_CONN = 1 << 21;
1619        /// Passive scan and initiating states
1620        const PASS_SCAN_AND_INITIATING = 1 << 22;
1621        /// Active scan and initiating states
1622        const ACT_SCAN_AND_INITIATING = 1 << 23;
1623        /// Passive scan and central (master) connection states
1624        const PASS_SCAN_AND_CENTRAL_CONN = 1 << 24;
1625        /// Active scan and central (master) connection states
1626        const ACT_SCAN_AND_CENTRAL_CONN = 1 << 25;
1627        /// Passive scan and peripheral (slave) connection states
1628        const PASS_SCAN_AND_PERIPH_CONN = 1 << 26;
1629        /// Active scan and peripheral (slave) connection states
1630        const ACT_SCAN_AND_PERIPH_CONN = 1 << 27;
1631        /// Initiating and central (master) connection states
1632        const INITIATING_AND_CENTRAL_CONN = 1 << 28;
1633        /// Directed advertising (low duty cycle) state alone
1634        const DIRECTED_ADVERTISING_LOW_DUTY_CYCLE = 1 << 29;
1635        /// Directed advertising (low duty cycle) and passive scan states
1636        const DIR_AD_LDC_AND_PASS_SCAN = 1 << 30;
1637        /// Directed advertising (low duty cycle) and active scan states
1638        const DIR_AD_LDC_AND_ACT_SCAN = 1 << 31;
1639        /// Connectable advertising and initiating states
1640        const CONN_AD_AND_INITIATING = 1 << 32;
1641        /// Directed advertising (high duty cycle) and initiating states
1642        const DIR_AD_HDC_AND_INITIATING = 1 << 33;
1643        /// Directed advertising (low duty cycle) and initiating states
1644        const DIR_AD_LDC_AND_INITIATING = 1 << 34;
1645        /// Connectable advertising and central (master) connection states
1646        const CONN_AD_AND_CENTRAL_CONN = 1 << 35;
1647        /// Directed advertising (high duty cycle) and central (master) states
1648        const DIR_AD_HDC_AND_CENTRAL_CONN = 1 << 36;
1649        /// Directed advertising (low duty cycle) and central (master) states
1650        const DIR_AD_LDC_AND_CENTRAL_CONN = 1 << 37;
1651        /// Connectable advertising and peripheral (slave) connection states
1652        const CONN_AD_AND_PERIPH_CONN = 1 << 38;
1653        /// Directed advertising (high duty cycle) and peripheral (slave) states
1654        const DIR_AD_HDC_AND_PERIPH_CONN = 1 << 39;
1655        /// Directed advertising (low duty cycle) and peripheral (slave) states
1656        const DIR_AD_LDC_AND_PERIPH_CONN = 1 << 40;
1657        /// Initiating and peripheral (slave) connection states
1658        const INITIATING_AND_PERIPH_CONN = 1 << 41;
1659    }
1660}
1661
1662fn to_le_read_states<VE, VS>(
1663    bytes: &[u8],
1664) -> Result<LeReadSupportedStates<VS>, crate::event::Error<VE>>
1665where
1666    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1667{
1668    require_len!(bytes, 9);
1669
1670    let bitfield = LittleEndian::read_u64(&bytes[1..]);
1671    Ok(LeReadSupportedStates {
1672        status: to_status(bytes)?,
1673        supported_states: LeStates::from_bits(bitfield)
1674            .ok_or_else(|| crate::event::Error::InvalidLeStates(bitfield))?,
1675    })
1676}
1677
1678/// Parameters returned by the [LE Test End](crate::host::Hci::le_test_end) command.
1679#[derive(Copy, Clone, Debug)]
1680pub struct LeTestEnd<VS> {
1681    /// Did the command fail, and if so, how?
1682    pub status: Status<VS>,
1683
1684    /// The number of packets received during the test.  For transmitter tests, this value shall be
1685    /// 0.
1686    pub number_of_packets: usize,
1687}
1688
1689fn to_le_test_end<VE, VS>(bytes: &[u8]) -> Result<LeTestEnd<VS>, crate::event::Error<VE>>
1690where
1691    Status<VS>: TryFrom<u8, Error = BadStatusError>,
1692{
1693    require_len!(bytes, 3);
1694
1695    Ok(LeTestEnd {
1696        status: to_status(bytes)?,
1697        number_of_packets: LittleEndian::read_u16(&bytes[1..]) as usize,
1698    })
1699}