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/btm/btm_int_types.h"
21 #include "stack/include/acl_api_types.h"
22 #include "stack/include/bt_types.h"
23 #include "stack/include/btm_status.h"
24 #include "stack/include/hci_error_code.h"
25 #include "types/raw_address.h"
26 
27 // Note: From stack/include/btm_api.h
28 
29 /*****************************************************************************
30  *  ACL CHANNEL MANAGEMENT FUNCTIONS
31  ****************************************************************************/
32 bool BTM_is_sniff_allowed_for(const RawAddress& peer_addr);
33 
34 void BTM_unblock_sniff_mode_for(const RawAddress& peer_addr);
35 void BTM_block_sniff_mode_for(const RawAddress& peer_addr);
36 void BTM_unblock_role_switch_for(const RawAddress& peer_addr);
37 void BTM_block_role_switch_for(const RawAddress& peer_addr);
38 void BTM_unblock_role_switch_and_sniff_mode_for(const RawAddress& peer_addr);
39 void BTM_block_role_switch_and_sniff_mode_for(const RawAddress& peer_addr);
40 
41 void BTM_default_unblock_role_switch();
42 void BTM_default_block_role_switch();
43 
44 void BTM_acl_after_controller_started(const controller_t* controller);
45 
46 /*******************************************************************************
47  *
48  * Function         BTM_SetLinkSuperTout
49  *
50  * Description      Create and send HCI "Write Link Supervision Timeout" command
51  *
52  * Returns          BTM_CMD_STARTED if successfully initiated, otherwise error
53  *
54  ******************************************************************************/
55 tBTM_STATUS BTM_SetLinkSuperTout(const RawAddress& remote_bda,
56                                  uint16_t timeout);
57 /*******************************************************************************
58  *
59  * Function         BTM_GetLinkSuperTout
60  *
61  * Description      Read the link supervision timeout value of the connection
62  *
63  * Returns          status of the operation
64  *
65  ******************************************************************************/
66 tBTM_STATUS BTM_GetLinkSuperTout(const RawAddress& remote_bda,
67                                  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,
81                            tBT_TRANSPORT transport);
82 
83 bool BTM_IsAclConnectionUpAndHandleValid(const RawAddress& remote_bda,
84                                          tBT_TRANSPORT transport);
85 
86 bool BTM_IsAclConnectionUpFromHandle(uint16_t hci_handle);
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, uint8_t rra_type,
196                                 const RawAddress& rpa);
197 
198 void btm_establish_continue_from_address(const RawAddress& remote_bda,
199                                          tBT_TRANSPORT transport);
200 
201 bool acl_peer_supports_ble_connection_parameters_request(
202     const RawAddress& remote_bda);
203 
204 bool sco_peer_supports_esco_2m_phy(const RawAddress& remote_bda);
205 bool sco_peer_supports_esco_3m_phy(const RawAddress& remote_bda);
206 
207 bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle);
208 bool acl_peer_supports_ble_2m_phy(uint16_t hci_handle);
209 bool acl_peer_supports_ble_coded_phy(uint16_t hci_handle);
210 
211 bool acl_is_switch_role_idle(const RawAddress& bd_addr,
212                              tBT_TRANSPORT transport);
213 
214 bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle);
215 
216 /*******************************************************************************
217  *
218  * Function         BTM_ReadConnectionAddr
219  *
220  * Description      Read the local device random address.
221  *
222  * Returns          void
223  *
224  ******************************************************************************/
225 void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
226                             RawAddress& local_conn_addr,
227                             tBLE_ADDR_TYPE* p_addr_type);
228 
229 /*******************************************************************************
230  *
231  * Function         BTM_IsBleConnection
232  *
233  * Description      This function is called to check if the connection handle
234  *                  for an LE link
235  *
236  * Returns          true if connection is LE link, otherwise false.
237  *
238  ******************************************************************************/
239 bool BTM_IsBleConnection(uint16_t hci_handle);
240 
241 const RawAddress acl_address_from_handle(uint16_t hci_handle);
242 
243 void btm_ble_refresh_local_resolvable_private_addr(
244     const RawAddress& pseudo_addr, const RawAddress& local_rpa);
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 bool acl_is_role_switch_allowed();
253 
254 uint16_t acl_get_supported_packet_types();
255 
256 bool acl_set_peer_le_features_from_handle(uint16_t hci_handle,
257                                           const uint8_t* p);
258 
259 tBTM_STATUS btm_read_power_mode_state(const RawAddress& remote_bda,
260                                       tBTM_PM_STATE* pmState);
261 
262 void btm_acl_notif_conn_collision(const RawAddress& bda);
263 
264 void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address);
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_removed(uint16_t handle);
291 
292 void acl_disconnect_from_handle(uint16_t handle, tHCI_STATUS reason);
293 void acl_disconnect_after_role_switch(uint16_t conn_handle, tHCI_STATUS reason);
294 
295 bool acl_peer_supports_sniff_subrating(const RawAddress& remote_bda);
296 
297 void btm_acl_set_paging(bool value);
298 
299 void btm_process_cancel_complete(uint8_t status, uint8_t mode);
300 
301 uint8_t btm_handle_to_acl_index(uint16_t hci_handle);
302 
303 tHCI_REASON btm_get_acl_disc_reason_code(void);
304 
305 extern tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr,
306                                   tBT_TRANSPORT transport);
307 
308 void btm_acl_device_down(void);
309 void btm_acl_update_inquiry_status(uint8_t status);
310 
311 void ACL_RegisterClient(struct acl_client_callback_s* callbacks);
312 void ACL_UnregisterClient(struct acl_client_callback_s* callbacks);
313 bool ACL_SupportTransparentSynchronousData(const RawAddress& bd_addr);
314 
315 void acl_add_to_ignore_auto_connect_after_disconnect(const RawAddress& bd_addr);
316 bool acl_check_and_clear_ignore_auto_connect_after_disconnect(
317     const RawAddress& bd_addr);
318 void acl_clear_all_ignore_auto_connect_after_disconnect();
319