summaryrefslogtreecommitdiff
path: root/tools/rootcanal/rust/src/packets.rs
blob: a64a22aa6326dcf62ab2d6ade2333a474db64af0 (plain)
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
// Copyright 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod hci {
    #![allow(clippy::all)]
    #![allow(unused)]
    #![allow(missing_docs)]
    #![allow(non_camel_case_types)]

    include!(concat!(env!("OUT_DIR"), "/hci_packets.rs"));

    pub const EMPTY_ADDRESS: Address = Address(0x000000000000);
    pub const ANY_ADDRESS: Address = Address(0xffffffffffff);

    impl fmt::Display for Address {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            let bytes = u64::to_le_bytes(self.0);
            write!(
                f,
                "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0],
            )
        }
    }

    impl From<&[u8; 6]> for Address {
        fn from(bytes: &[u8; 6]) -> Self {
            Self(u64::from_le_bytes([
                bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], 0, 0,
            ]))
        }
    }

    impl From<Address> for [u8; 6] {
        fn from(Address(addr): Address) -> Self {
            let bytes = u64::to_le_bytes(addr);
            bytes[0..6].try_into().unwrap()
        }
    }

    impl Address {
        pub fn is_empty(&self) -> bool {
            *self == EMPTY_ADDRESS
        }
    }

    pub fn command_remote_device_address(command: &Command) -> Option<Address> {
        use CommandChild::*;
        #[allow(unused_imports)]
        use Option::None; // Overwrite `None` variant of `Child` enum

        match command.specialize() {
            LinkKeyRequestReply(packet) => Some(packet.get_bd_addr()),
            LinkKeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            PinCodeRequestReply(packet) => Some(packet.get_bd_addr()),
            PinCodeRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            IoCapabilityRequestReply(packet) => Some(packet.get_bd_addr()),
            IoCapabilityRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            UserConfirmationRequestReply(packet) => Some(packet.get_bd_addr()),
            UserConfirmationRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            UserPasskeyRequestReply(packet) => Some(packet.get_bd_addr()),
            UserPasskeyRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            RemoteOobDataRequestReply(packet) => Some(packet.get_bd_addr()),
            RemoteOobDataRequestNegativeReply(packet) => Some(packet.get_bd_addr()),
            SendKeypressNotification(packet) => Some(packet.get_bd_addr()),
            _ => None,
        }
    }

    pub fn command_connection_handle(command: &Command) -> Option<u16> {
        use CommandChild::*;
        #[allow(unused_imports)]
        use Option::None; // Overwrite `None` variant of `Child` enum

        match command.specialize() {
            AuthenticationRequested(packet) => Some(packet.get_connection_handle()),
            SetConnectionEncryption(packet) => Some(packet.get_connection_handle()),
            _ => None,
        }
    }
}

pub mod lmp {
    #![allow(clippy::all)]
    #![allow(unused)]
    #![allow(missing_docs)]

    include!(concat!(env!("OUT_DIR"), "/lmp_packets.rs"));
}

pub mod llcp {
    #![allow(clippy::all)]
    #![allow(unused)]
    #![allow(missing_docs)]

    include!(concat!(env!("OUT_DIR"), "/llcp_packets.rs"));
}