• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 pub mod hci {
2     #![allow(clippy::all)]
3     #![allow(unused)]
4     #![allow(missing_docs)]
5 
6     pub const EMPTY_ADDRESS: Address = Address { bytes: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00] };
7     pub const ANY_ADDRESS: Address = Address { bytes: [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] };
8 
9     /// A Bluetooth address
10     #[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
11     pub struct Address {
12         pub bytes: [u8; 6],
13     }
14 
15     impl Address {
is_empty(&self) -> bool16         pub fn is_empty(&self) -> bool {
17             *self == EMPTY_ADDRESS
18         }
19     }
20 
21     impl fmt::Display for Address {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result22         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23             write!(
24                 f,
25                 "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
26                 self.bytes[5],
27                 self.bytes[4],
28                 self.bytes[3],
29                 self.bytes[2],
30                 self.bytes[1],
31                 self.bytes[0]
32             )
33         }
34     }
35 
36     #[derive(Debug, Clone)]
37     pub struct InvalidAddressError;
38 
39     impl TryFrom<&[u8]> for Address {
40         type Error = InvalidAddressError;
41 
try_from(slice: &[u8]) -> std::result::Result<Self, Self::Error>42         fn try_from(slice: &[u8]) -> std::result::Result<Self, Self::Error> {
43             match <[u8; 6]>::try_from(slice) {
44                 Ok(bytes) => Ok(Self { bytes }),
45                 Err(_) => Err(InvalidAddressError),
46             }
47         }
48     }
49 
50     impl From<Address> for [u8; 6] {
from(addr: Address) -> [u8; 6]51         fn from(addr: Address) -> [u8; 6] {
52             addr.bytes
53         }
54     }
55 
56     #[derive(Clone, Eq, Copy, PartialEq, Hash, Ord, PartialOrd, Debug)]
57     pub struct ClassOfDevice {
58         pub bytes: [u8; 3],
59     }
60 
61     impl fmt::Display for ClassOfDevice {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result62         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63             write!(
64                 f,
65                 "{:03X}-{:01X}-{:02X}",
66                 ((self.bytes[2] as u16) << 4) | ((self.bytes[1] as u16) >> 4),
67                 self.bytes[1] & 0x0F,
68                 self.bytes[0]
69             )
70         }
71     }
72 
73     #[derive(Debug, Clone)]
74     pub struct InvalidClassOfDeviceError;
75 
76     impl TryFrom<&[u8]> for ClassOfDevice {
77         type Error = InvalidClassOfDeviceError;
78 
try_from(slice: &[u8]) -> std::result::Result<Self, Self::Error>79         fn try_from(slice: &[u8]) -> std::result::Result<Self, Self::Error> {
80             match <[u8; 3]>::try_from(slice) {
81                 Ok(bytes) => Ok(Self { bytes }),
82                 Err(_) => Err(InvalidClassOfDeviceError),
83             }
84         }
85     }
86 
87     impl From<ClassOfDevice> for [u8; 3] {
from(cod: ClassOfDevice) -> [u8; 3]88         fn from(cod: ClassOfDevice) -> [u8; 3] {
89             cod.bytes
90         }
91     }
92 
93     include!(concat!(env!("OUT_DIR"), "/hci_packets.rs"));
94 
command_remote_device_address(command: &CommandPacket) -> Option<Address>95     pub fn command_remote_device_address(command: &CommandPacket) -> Option<Address> {
96         use CommandChild::*;
97         #[allow(unused_imports)]
98         use Option::None; // Overwrite `None` variant of `Child` enum
99 
100         match command.specialize() {
101             LinkKeyRequestReply(packet) => Some(packet.get_bd_addr()),
102             LinkKeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
103             PinCodeRequestReply(packet) => Some(packet.get_bd_addr()),
104             PinCodeRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
105             IoCapabilityRequestReply(packet) => Some(packet.get_bd_addr()),
106             IoCapabilityRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
107             UserConfirmationRequestReply(packet) => Some(packet.get_bd_addr()),
108             UserConfirmationRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
109             UserPasskeyRequestReply(packet) => Some(packet.get_bd_addr()),
110             UserPasskeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
111             RemoteOobDataRequestReply(packet) => Some(packet.get_bd_addr()),
112             RemoteOobDataRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
113             SendKeypressNotification(packet) => Some(packet.get_bd_addr()),
114             _ => None,
115         }
116     }
117 
command_connection_handle(command: &CommandPacket) -> Option<u16>118     pub fn command_connection_handle(command: &CommandPacket) -> Option<u16> {
119         use CommandChild::*;
120         #[allow(unused_imports)]
121         use Option::None; // Overwrite `None` variant of `Child` enum
122 
123         match command.specialize() {
124             AuthenticationRequested(packet) => Some(packet.get_connection_handle()),
125             SetConnectionEncryption(packet) => Some(packet.get_connection_handle()),
126             _ => None,
127         }
128     }
129 }
130 
131 pub mod lmp {
132     #![allow(clippy::all)]
133     #![allow(unused)]
134     #![allow(missing_docs)]
135 
136     include!(concat!(env!("OUT_DIR"), "/lmp_packets.rs"));
137 }
138