1 /* Copyright 2020 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 
16 #pragma once
17 
18 #include <cstdint>
19 
20 #include "stack/acl/acl.h"
21 #include "stack/btm/security_device_record.h"
22 #include "stack/include/btm_api_types.h"
23 #include "stack/include/btm_status.h"
24 #include "stack/include/hci_error_code.h"
25 #include "types/ble_address_with_type.h"
26 #include "types/bt_transport.h"
27 #include "types/hci_role.h"
28 #include "types/raw_address.h"
29 
30 // Note: From stack/include/btm_api.h
31 
32 /*****************************************************************************
33  *  ACL CHANNEL MANAGEMENT FUNCTIONS
34  ****************************************************************************/
35 bool BTM_is_sniff_allowed_for(const RawAddress& peer_addr);
36 
37 void BTM_unblock_sniff_mode_for(const RawAddress& peer_addr);
38 void BTM_block_sniff_mode_for(const RawAddress& peer_addr);
39 void BTM_unblock_role_switch_for(const RawAddress& peer_addr);
40 void BTM_block_role_switch_for(const RawAddress& peer_addr);
41 void BTM_unblock_role_switch_and_sniff_mode_for(const RawAddress& peer_addr);
42 void BTM_block_role_switch_and_sniff_mode_for(const RawAddress& peer_addr);
43 
44 void BTM_default_unblock_role_switch();
45 
46 void BTM_acl_after_controller_started();
47 
48 /*******************************************************************************
49  *
50  * Function         BTM_SetLinkSuperTout
51  *
52  * Description      Create and send HCI "Write Link Supervision Timeout" command
53  *
54  * Returns          BTM_CMD_STARTED if successfully initiated, otherwise error
55  *
56  ******************************************************************************/
57 tBTM_STATUS BTM_SetLinkSuperTout(const RawAddress& remote_bda,
58                                  uint16_t timeout);
59 /*******************************************************************************
60  *
61  * Function         BTM_GetLinkSuperTout
62  *
63  * Description      Read the link supervision timeout value of the connection
64  *
65  * Returns          status of the operation
66  *
67  ******************************************************************************/
68 tBTM_STATUS BTM_GetLinkSuperTout(const RawAddress& remote_bda,
69                                  uint16_t* p_timeout);
70 
71 /*******************************************************************************
72  *
73  * Function         BTM_IsAclConnectionUp
74  *
75  * Description      This function is called to check if an ACL connection exists
76  *                  to a specific remote BD Address.  The second version ensures
77  *                  the hci handle is valid (Unsure if needed)
78  *
79  * Returns          true if connection is up, else false.
80  *
81  ******************************************************************************/
82 bool BTM_IsAclConnectionUp(const RawAddress& remote_bda,
83                            tBT_TRANSPORT transport);
84 
85 bool BTM_IsAclConnectionUpAndHandleValid(const RawAddress& remote_bda,
86                                          tBT_TRANSPORT transport);
87 
88 /*******************************************************************************
89  *
90  * Function         BTM_GetRole
91  *
92  * Description      This function is called to get the role of the local device
93  *                  for the ACL connection with the specified remote device
94  *
95  * Returns          BTM_SUCCESS if connection exists.
96  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
97  *
98  ******************************************************************************/
99 tBTM_STATUS BTM_GetRole(const RawAddress& remote_bd_addr, tHCI_ROLE* p_role);
100 
101 /*******************************************************************************
102  *
103  * Function         BTM_SwitchRoleToCentral
104  *
105  * Description      This function is called to switch role between central and
106  *                  peripheral.  If role is already set it will do nothing.
107  *
108  * Returns          BTM_SUCCESS if already in specified role.
109  *                  BTM_CMD_STARTED if command issued to controller.
110  *                  BTM_NO_RESOURCES if memory couldn't be allocated to issue
111  *                                   the command
112  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
113  *                  BTM_MODE_UNSUPPORTED if the local device does not support
114  *                                       role switching
115  *
116  ******************************************************************************/
117 tBTM_STATUS BTM_SwitchRoleToCentral(const RawAddress& remote_bd_addr);
118 
119 /*******************************************************************************
120  *
121  * Function         BTM_ReadRSSI
122  *
123  * Description      This function is called to read the link policy settings.
124  *                  The address of link policy results are returned in the
125  *                  callback. (tBTM_RSSI_RESULT)
126  *
127  * Returns          BTM_CMD_STARTED if command issued to controller.
128  *                  BTM_NO_RESOURCES if memory couldn't be allocated to issue
129  *                                   the command
130  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
131  *                  BTM_BUSY if command is already in progress
132  *
133  ******************************************************************************/
134 tBTM_STATUS BTM_ReadRSSI(const RawAddress& remote_bda, tBTM_CMPL_CB* p_cb);
135 
136 /*******************************************************************************
137  *
138  * Function         BTM_ReadFailedContactCounter
139  *
140  * Description      This function is called to read the failed contact counter.
141  *                  The result is returned in the callback.
142  *                  (tBTM_FAILED_CONTACT_COUNTER_RESULT)
143  *
144  * Returns          BTM_CMD_STARTED if command issued to controller.
145  *                  BTM_NO_RESOURCES if memory couldn't be allocated to issue
146  *                                   the command
147  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
148  *                  BTM_BUSY if command is already in progress
149  *
150  ******************************************************************************/
151 tBTM_STATUS BTM_ReadFailedContactCounter(const RawAddress& remote_bda,
152                                          tBTM_CMPL_CB* p_cb);
153 
154 /*******************************************************************************
155  *
156  * Function         BTM_ReadTxPower
157  *
158  * Description      This function is called to read the current connection
159  *                  TX power of the connection. The TX power level results
160  *                  are returned in the callback.
161  *                  (tBTM_RSSI_RESULT)
162  *
163  * Returns          BTM_CMD_STARTED if command issued to controller.
164  *                  BTM_NO_RESOURCES if memory couldn't be allocated to issue
165  *                                   the command
166  *                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
167  *                  BTM_BUSY if command is already in progress
168  *
169  ******************************************************************************/
170 tBTM_STATUS BTM_ReadTxPower(const RawAddress& remote_bda,
171                             tBT_TRANSPORT transport, tBTM_CMPL_CB* p_cb);
172 
173 /*******************************************************************************
174  *
175  * Function         BTM_GetNumAclLinks
176  *
177  * Description      This function is called to count the number of
178  *                  ACL links that are active.
179  *
180  * Returns          uint16_t Number of active ACL links
181  *
182  ******************************************************************************/
183 uint16_t BTM_GetNumAclLinks(void);
184 
185 void btm_set_packet_types_from_address(const RawAddress& bda,
186                                        uint16_t pkt_types);
187 
188 #define BLE_RESOLVE_ADDR_MASK 0xc0
189 #define BLE_RESOLVE_ADDR_MSB 0x40
190 
191 bool BTM_BLE_IS_RESOLVE_BDA(const RawAddress& x);
192 
193 bool acl_refresh_remote_address(const RawAddress& identity_address,
194                                 tBLE_ADDR_TYPE identity_address_type,
195                                 const RawAddress& remote_bda,
196                                 tBLE_RAND_ADDR_TYPE rra_type,
197                                 const RawAddress& rpa);
198 
199 void btm_establish_continue_from_address(const RawAddress& remote_bda,
200                                          tBT_TRANSPORT transport);
201 
202 bool acl_peer_supports_ble_connection_parameters_request(
203     const RawAddress& remote_bda);
204 void acl_ble_connection_parameters_request(
205     uint16_t handle, uint16_t conn_int_min, uint16_t conn_int_max,
206     uint16_t conn_latency, uint16_t conn_timeout, uint16_t min_ce_len,
207     uint16_t max_ce_len);
208 
209 bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle);
210 bool acl_peer_supports_ble_2m_phy(uint16_t hci_handle);
211 bool acl_peer_supports_ble_coded_phy(uint16_t hci_handle);
212 
213 bool acl_is_switch_role_idle(const RawAddress& bd_addr,
214                              tBT_TRANSPORT transport);
215 
216 bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle);
217 
218 /*******************************************************************************
219  *
220  * Function         BTM_ReadConnectionAddr
221  *
222  * Description      Read the local device random address.
223  *
224  * Returns          void
225  *
226  ******************************************************************************/
227 void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
228                             RawAddress& local_conn_addr,
229                             tBLE_ADDR_TYPE* p_addr_type,
230                             bool ota_address = false);
231 
232 /*******************************************************************************
233  *
234  * Function         BTM_IsBleConnection
235  *
236  * Description      This function is called to check if the connection handle
237  *                  for an LE link
238  *
239  * Returns          true if connection is LE link, otherwise false.
240  *
241  ******************************************************************************/
242 bool BTM_IsBleConnection(uint16_t hci_handle);
243 
244 const RawAddress acl_address_from_handle(uint16_t hci_handle);
245 
246 void btm_cont_rswitch_from_handle(uint16_t hci_handle);
247 
248 uint8_t acl_link_role_from_handle(uint16_t handle);
249 
250 void acl_set_disconnect_reason(tHCI_STATUS acl_disc_reason);
251 
252 void acl_set_locally_initiated(bool is_locally_initiated);
253 
254 bool acl_is_role_switch_allowed();
255 
256 uint16_t acl_get_supported_packet_types();
257 
258 bool acl_set_peer_le_features_from_handle(uint16_t hci_handle,
259                                           const uint8_t* p);
260 
261 tBTM_STATUS btm_read_power_mode_state(const RawAddress& remote_bda,
262                                       tBTM_PM_STATE* pmState);
263 
264 void btm_acl_notif_conn_collision(const RawAddress& bda);
265 
266 /*******************************************************************************
267  *
268  * Function         BTM_ReadPowerMode
269  *
270  * Description      This returns the current mode for a specific
271  *                  ACL connection.
272  *
273  * Input Param      remote_bda - device address of desired ACL connection
274  *
275  * Output Param     p_mode - address where the current mode is copied into.
276  *                          BTM_ACL_MODE_NORMAL
277  *                          BTM_ACL_MODE_HOLD
278  *                          BTM_ACL_MODE_SNIFF
279  *                          BTM_ACL_MODE_PARK
280  *                          (valid only if return code is BTM_SUCCESS)
281  *
282  * Returns          true if successful, false otherwise.
283  *
284  ******************************************************************************/
285 bool BTM_ReadPowerMode(const RawAddress& remote_bda, tBTM_PM_MODE* p_mode);
286 
287 void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
288                      tHCI_ROLE link_role, tBT_TRANSPORT transport);
289 
290 void btm_acl_create_failed(const RawAddress& bda, tBT_TRANSPORT transport,
291                            tHCI_STATUS reason);
292 
293 void btm_acl_removed(uint16_t handle);
294 
295 void btm_acl_flush(uint16_t handle);
296 
297 void acl_disconnect_from_handle(uint16_t handle, tHCI_STATUS reason,
298                                 std::string comment);
299 void acl_disconnect_after_role_switch(uint16_t conn_handle, tHCI_STATUS reason,
300                                       std::string comment);
301 
302 bool acl_peer_supports_sniff_subrating(const RawAddress& remote_bda);
303 bool acl_peer_supports_ble_connection_subrating(const RawAddress& remote_bda);
304 bool acl_peer_supports_ble_connection_subrating_host(
305     const RawAddress& remote_bda);
306 
307 uint8_t btm_handle_to_acl_index(uint16_t hci_handle);
308 
309 tHCI_REASON btm_get_acl_disc_reason_code(void);
310 
311 bool btm_is_acl_locally_initiated(void);
312 
313 tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr, tBT_TRANSPORT transport);
314 
315 void btm_acl_device_down(void);
316 
317 void ACL_RegisterClient(struct acl_client_callback_s* callbacks);
318 void ACL_UnregisterClient(struct acl_client_callback_s* callbacks);
319 void btm_acl_consolidate(const RawAddress& identity_addr,
320                          const RawAddress& rpa);
321