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