1 // Copyright 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 pub mod hci {
16     #![allow(clippy::all)]
17     #![allow(unused)]
18     #![allow(missing_docs)]
19     #![allow(non_camel_case_types)]
20 
21     include!(concat!(env!("OUT_DIR"), "/hci_packets.rs"));
22 
23     pub const EMPTY_ADDRESS: Address = Address(0x000000000000);
24     pub const ANY_ADDRESS: Address = Address(0xffffffffffff);
25 
26     impl fmt::Display for Address {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result27         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28             let bytes = u64::to_le_bytes(self.0);
29             write!(
30                 f,
31                 "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
32                 bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0],
33             )
34         }
35     }
36 
37     impl From<&[u8; 6]> for Address {
from(bytes: &[u8; 6]) -> Self38         fn from(bytes: &[u8; 6]) -> Self {
39             Self(u64::from_le_bytes([
40                 bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0,
41             ]))
42         }
43     }
44 
45     impl From<Address> for [u8; 6] {
from(Address(addr): Address) -> Self46         fn from(Address(addr): Address) -> Self {
47             let bytes = u64::to_le_bytes(addr);
48             bytes[0..6].try_into().unwrap()
49         }
50     }
51 
52     impl Address {
is_empty(&self) -> bool53         pub fn is_empty(&self) -> bool {
54             *self == EMPTY_ADDRESS
55         }
56     }
57 
command_remote_device_address(command: &Command) -> Option<Address>58     pub fn command_remote_device_address(command: &Command) -> Option<Address> {
59         use CommandChild::*;
60         #[allow(unused_imports)]
61         use Option::None; // Overwrite `None` variant of `Child` enum
62 
63         match command.specialize() {
64             LinkKeyRequestReply(packet) => Some(packet.get_bd_addr()),
65             LinkKeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
66             PinCodeRequestReply(packet) => Some(packet.get_bd_addr()),
67             PinCodeRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
68             IoCapabilityRequestReply(packet) => Some(packet.get_bd_addr()),
69             IoCapabilityRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
70             UserConfirmationRequestReply(packet) => Some(packet.get_bd_addr()),
71             UserConfirmationRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
72             UserPasskeyRequestReply(packet) => Some(packet.get_bd_addr()),
73             UserPasskeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
74             RemoteOobDataRequestReply(packet) => Some(packet.get_bd_addr()),
75             RemoteOobDataRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
76             SendKeypressNotification(packet) => Some(packet.get_bd_addr()),
77             _ => None,
78         }
79     }
80 
command_connection_handle(command: &Command) -> Option<u16>81     pub fn command_connection_handle(command: &Command) -> Option<u16> {
82         use CommandChild::*;
83         #[allow(unused_imports)]
84         use Option::None; // Overwrite `None` variant of `Child` enum
85 
86         match command.specialize() {
87             AuthenticationRequested(packet) => Some(packet.get_connection_handle()),
88             SetConnectionEncryption(packet) => Some(packet.get_connection_handle()),
89             _ => None,
90         }
91     }
92 }
93 
94 pub mod lmp {
95     #![allow(clippy::all)]
96     #![allow(unused)]
97     #![allow(missing_docs)]
98 
99     include!(concat!(env!("OUT_DIR"), "/lmp_packets.rs"));
100 }
101 
102 pub mod llcp {
103     #![allow(clippy::all)]
104     #![allow(unused)]
105     #![allow(missing_docs)]
106 
107     include!(concat!(env!("OUT_DIR"), "/llcp_packets.rs"));
108 }
109