1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "osi/include/alarm.h"
19 #include "stack/include/bt_device_type.h"
20 #include "stack/include/bt_name.h"
21 #include "stack/include/btm_status.h"
22 #include "stack/include/hci_error_code.h"
23 #include "stack/include/rnr_interface.h"
24 #include "stack/include/security_client_callbacks.h"
25 #include "types/raw_address.h"
26 
27 namespace bluetooth {
28 namespace stack {
29 namespace rnr {
30 
31 class RemoteNameRequest {
32 public:
33   tBTM_NAME_CMPL_CB* p_remname_cmpl_cb{nullptr};
34   alarm_t* remote_name_timer{nullptr};
35   RawAddress remname_bda{};   /* Name of bd addr for active remote name request */
36   bool remname_active{false}; /* State of a remote name request by external API */
37   tBT_DEVICE_TYPE remname_dev_type{
38           BT_DEVICE_TYPE_UNKNOWN}; /* Whether it's LE or BREDR name request */
39 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2
40   tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS]{nullptr, nullptr};
41 };
42 
43 }  // namespace rnr
44 }  // namespace stack
45 }  // namespace bluetooth
46 
47 /*******************************************************************************
48  *
49  * Function         BTM_SecAddRmtNameNotifyCallback
50  *
51  * Description      Any profile can register to be notified when name of the
52  *                  remote device is resolved.
53  *
54  * Parameters:      p_callback: Callback to add after each remote name
55  *                  request has completed or timed out.
56  *
57  * Returns          true if registered OK, else false
58  *
59  ******************************************************************************/
60 bool BTM_SecAddRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK* p_callback);
61 
62 /*******************************************************************************
63  *
64  * Function         BTM_SecDeleteRmtNameNotifyCallback
65  *
66  * Description      Any profile can deregister notification when a new Link Key
67  *                  is generated per connection.
68  *
69  * Parameters:      p_callback: Callback to remove after each remote name
70  *                  request has completed or timed out.
71  *
72  * Returns          true if unregistered OK, else false
73  *
74  ******************************************************************************/
75 bool BTM_SecDeleteRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK* p_callback);
76 
77 /*******************************************************************************
78  *
79  * Function         BTM_IsRemoteNameKnown
80  *
81  * Description      Look up the device record using the bluetooth device
82  *                  address and if a record is found check if the name
83  *                  has been acquired and cached.
84  *
85  * Parameters:      bd_addr: Bluetooth device address
86  *                  transport: UNUSED
87  *
88  * Returns          true if name is cached, false otherwise
89  *
90  ******************************************************************************/
91 bool BTM_IsRemoteNameKnown(const RawAddress& bd_addr, tBT_TRANSPORT transport);
92 
93 /*******************************************************************************
94  *
95  * Function         BTM_ReadRemoteDeviceName
96  *
97  * Description      This function initiates a remote device HCI command to the
98  *                  controller and calls the callback when the process has
99  *                  completed.
100  *
101  * Input Params:    remote_bda      - bluetooth device address of name to
102  *                                    retrieve
103  *                  p_cb            - callback function called when
104  *                                    remote name is received or when procedure
105  *                                    timed out.
106  *                  transport       - transport used to query the remote name
107  * Returns
108  *                  tBTM_STATUS::BTM_CMD_STARTED is returned if the request was successfully
109  *                                    sent to HCI.
110  *                  BTM_BUSY if already in progress
111  *                  BTM_UNKNOWN_ADDR if device address is bad
112  *                  BTM_NO_RESOURCES if could not allocate resources to start
113  *                                   the command
114  *                  BTM_WRONG_MODE if the device is not up.
115  *
116  ******************************************************************************/
117 tBTM_STATUS BTM_ReadRemoteDeviceName(const RawAddress& remote_bda, tBTM_NAME_CMPL_CB* p_cb,
118                                      tBT_TRANSPORT transport);
119 
120 /*******************************************************************************
121  *
122  * Function         BTM_CancelRemoteDeviceName
123  *
124  * Description      This function initiates the cancel request for the specified
125  *                  remote device.
126  *
127  * Input Params:    None
128  *
129  * Returns
130  *                  tBTM_STATUS::BTM_CMD_STARTED is returned if the request was successfully
131  *                                  sent to HCI.
132  *                  BTM_NO_RESOURCES if could not allocate resources to start
133  *                                   the command
134  *                  BTM_WRONG_MODE if there is not an active remote name
135  *                                 request.
136  *
137  ******************************************************************************/
138 tBTM_STATUS BTM_CancelRemoteDeviceName(void);
139 
140 /*******************************************************************************
141  *
142  * Function         btm_process_remote_name
143  *
144  * Description      This function is called when a remote name is received from
145  *                  the device. If remote names are cached, it updates the
146  *                  inquiry database.
147  *
148  * Returns          void
149  *
150  ******************************************************************************/
151 void btm_process_remote_name(const RawAddress* bda, const BD_NAME bdn, uint16_t /* evt_len */,
152                              tHCI_STATUS hci_status);
153 
154 void btm_inq_remote_name_timer_timeout(void* data);
155 
156 namespace bluetooth {
157 namespace stack {
158 namespace rnr {
159 
160 class Impl : public bluetooth::stack::rnr::Interface {
161 public:
162   Impl() = default;
163 
164   [[nodiscard]] bool BTM_SecAddRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK* p_callback) override;
165   [[nodiscard]] bool BTM_SecDeleteRmtNameNotifyCallback(tBTM_RMT_NAME_CALLBACK* p_callback);
166   [[nodiscard]] bool BTM_IsRemoteNameKnown(const RawAddress& bd_addr, tBT_TRANSPORT transport);
167   [[nodiscard]] tBTM_STATUS BTM_ReadRemoteDeviceName(const RawAddress& remote_bda,
168                                                      tBTM_NAME_CMPL_CB* p_cb,
169                                                      tBT_TRANSPORT transport);
170   [[nodiscard]] tBTM_STATUS BTM_CancelRemoteDeviceName(void);
171   void btm_process_remote_name(const RawAddress* bda, const BD_NAME bdn, uint16_t /* evt_len */,
172                                tHCI_STATUS hci_status);
173 };
174 
175 }  // namespace rnr
176 }  // namespace stack
177 }  // namespace bluetooth
178