1 /******************************************************************************
2 *
3 * Copyright (C) 2000-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /*****************************************************************************
20 **
21 ** Name: btm_acl.c
22 **
23 ** Description: This file contains functions that handle ACL connections.
24 ** This includes operations such as hold and sniff modes,
25 ** supported packet types.
26 **
27 ** This module contains both internal and external (API)
28 ** functions. External (API) functions are distinguishable
29 ** by their names beginning with uppercase BTM.
30 **
31 **
32 ******************************************************************************/
33
34 #include <stdlib.h>
35 #include <string.h>
36 //#include <stdio.h>
37 #include <stddef.h>
38
39 #include "stack/bt_types.h"
40 #include "common/bt_target.h"
41 #include "device/controller.h"
42 #include "stack/hcimsgs.h"
43 #include "stack/btu.h"
44 #include "stack/btm_api.h"
45 #include "btm_int.h"
46 #include "l2c_int.h"
47 #include "stack/hcidefs.h"
48 //#include "bt_utils.h"
49 #include "osi/list.h"
50
51 static void btm_read_remote_features (UINT16 handle);
52 static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
53 static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages);
54
55 #define BTM_DEV_REPLY_TIMEOUT 3 /* 3 second timeout waiting for responses */
56
57 /*******************************************************************************
58 **
59 ** Function btm_acl_init
60 **
61 ** Description This function is called at BTM startup to initialize
62 **
63 ** Returns void
64 **
65 *******************************************************************************/
btm_acl_init(void)66 void btm_acl_init (void)
67 {
68 BTM_TRACE_DEBUG ("btm_acl_init\n");
69 #if 0 /* cleared in btm_init; put back in if called from anywhere else! */
70 memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
71 memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to FALSE */
72 btm_cb.btm_def_link_policy = 0;
73 btm_cb.p_bl_changed_cb = NULL;
74 #endif
75 btm_cb.p_acl_db_list = list_new(osi_free_func);
76 btm_cb.p_pm_mode_db_list = list_new(osi_free_func);
77
78 /* Initialize nonzero defaults */
79 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
80 btm_cb.acl_disc_reason = 0xff ;
81 }
82
83 /*******************************************************************************
84 **
85 ** Function btm_bda_to_acl
86 **
87 ** Description This function returns the FIRST acl_db entry for the passed BDA.
88 **
89 ** Parameters bda : BD address of the remote device
90 ** transport : Physical transport used for ACL connection (BR/EDR or LE)
91 **
92 ** Returns Returns pointer to the ACL DB for the requested BDA if found.
93 ** NULL if not found.
94 **
95 *******************************************************************************/
btm_get_acl_db(void * p_acl_db_node,void * context)96 BOOLEAN btm_get_acl_db(void *p_acl_db_node, void *context)
97 {
98 tACL_CONN *p_acl_db =(tACL_CONN *)p_acl_db_node;
99 BOOLEAN ret = TRUE;
100 tACL_DB_PARAM *p_param = (tACL_DB_PARAM *)context;
101 switch(p_param->type) {
102 case ACL_DB_BDA:
103 {
104 UINT8 *p_bda = (UINT8 *)p_param->p_data1;
105 #if BLE_INCLUDED == TRUE
106 tBT_TRANSPORT transport = (tBT_TRANSPORT)(*((UINT8 *)p_param->p_data2));
107 #endif
108 if (p_acl_db->in_use
109 && !memcmp(p_bda, p_acl_db->remote_addr, BD_ADDR_LEN)
110 #if BLE_INCLUDED == TRUE
111 && transport == p_acl_db->transport
112 #endif
113 ) {
114 ret = FALSE;
115 }
116 break;
117 }
118 case ACL_DB_HANDLE:
119 {
120 UINT16 handle = (UINT16) *((UINT16 *)p_param->p_data1);
121 if (p_acl_db->in_use && handle == p_acl_db->hci_handle) {
122 ret = FALSE;
123 }
124 break;
125 }
126 default:
127 break;
128 }
129 return ret;
130 }
131
btm_bda_to_acl(BD_ADDR bda,tBT_TRANSPORT transport)132 tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
133 {
134 tACL_CONN *p_acl_db = NULL;
135 list_node_t *p_node = NULL;
136 tACL_DB_PARAM acl_params;
137 acl_params.type = ACL_DB_BDA;
138 acl_params.p_data1 = (void *)bda;
139 acl_params.p_data2 = (void *)&transport;
140 p_node = list_foreach(btm_cb.p_acl_db_list, btm_get_acl_db, (void *)&acl_params);
141 if (p_node) {
142 p_acl_db = list_node(p_node);
143 }
144
145 return (p_acl_db);
146 }
147
148 /*******************************************************************************
149 **
150 ** Function btm_handle_to_acl
151 **
152 ** Description This function returns the FIRST acl_db entry for the passed hci_handle.
153 **
154 ** Returns Returns pointer to the ACL DB for the requested BDA if found.
155 ** NULL if not found.
156 **
157 *******************************************************************************/
btm_handle_to_acl(UINT16 hci_handle)158 tACL_CONN *btm_handle_to_acl (UINT16 hci_handle)
159 {
160 tACL_CONN *p_acl_db = NULL;
161 tACL_DB_PARAM acl_params;
162 list_node_t *p_node = NULL;
163
164 BTM_TRACE_DEBUG ("btm_handle_to_acl_index: %d\n", hci_handle);
165
166 acl_params.type = ACL_DB_HANDLE;
167 acl_params.p_data1 = (void *)&hci_handle;
168 acl_params.p_data2 = NULL;
169 p_node = list_foreach(btm_cb.p_acl_db_list, btm_get_acl_db, (void *)&acl_params);
170 if (p_node) {
171 p_acl_db = list_node(p_node);
172 }
173
174 return (p_acl_db);
175 }
176
177 #if BLE_PRIVACY_SPT == TRUE
178 /*******************************************************************************
179 **
180 ** Function btm_ble_get_acl_remote_addr
181 **
182 ** Description This function reads the active remote address used for the
183 ** connection.
184 **
185 ** Returns success return TRUE, otherwise FALSE.
186 **
187 *******************************************************************************/
btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC * p_dev_rec,BD_ADDR conn_addr,tBLE_ADDR_TYPE * p_addr_type)188 BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
189 tBLE_ADDR_TYPE *p_addr_type)
190 {
191 #if BLE_INCLUDED == TRUE
192 BOOLEAN st = TRUE;
193
194 if (p_dev_rec == NULL) {
195 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address\n");
196 return FALSE;
197 }
198
199 switch (p_dev_rec->ble.active_addr_type) {
200 case BTM_BLE_ADDR_PSEUDO:
201 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
202 * p_addr_type = p_dev_rec->ble.ble_addr_type;
203 break;
204
205 case BTM_BLE_ADDR_RRA:
206 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
207 * p_addr_type = BLE_ADDR_RANDOM;
208 break;
209
210 case BTM_BLE_ADDR_STATIC:
211 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
212 * p_addr_type = p_dev_rec->ble.static_addr_type;
213 break;
214
215 default:
216 BTM_TRACE_ERROR("Unknown active address: %d\n", p_dev_rec->ble.active_addr_type);
217 st = FALSE;
218 break;
219 }
220
221 return st;
222 #else
223 UNUSED(p_dev_rec);
224 UNUSED(conn_addr);
225 UNUSED(p_addr_type);
226 return FALSE;
227 #endif
228 }
229 #endif
230 /*******************************************************************************
231 **
232 ** Function btm_acl_created
233 **
234 ** Description This function is called by L2CAP when an ACL connection
235 ** is created.
236 **
237 ** Returns void
238 **
239 *******************************************************************************/
btm_acl_created(BD_ADDR bda,DEV_CLASS dc,BD_NAME bdn,UINT16 hci_handle,UINT8 link_role,tBT_TRANSPORT transport)240 void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
241 UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
242 {
243 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
244 tACL_CONN *p;
245
246 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d\n",
247 hci_handle, link_role, transport);
248 /* Ensure we don't have duplicates */
249 p = btm_bda_to_acl(bda, transport);
250 if (p != (tACL_CONN *)NULL) {
251 p->hci_handle = hci_handle;
252 p->link_role = link_role;
253 #if BLE_INCLUDED == TRUE
254 p->transport = transport;
255 #endif
256 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
257 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
258 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
259 return;
260 }
261
262 /* Allocate acl_db entry */
263 if (list_length(btm_cb.p_acl_db_list) >= MAX_L2CAP_LINKS) {
264 return;
265 }
266 else {
267 p = (tACL_CONN *)osi_malloc(sizeof(tACL_CONN));
268 if (p && list_append(btm_cb.p_acl_db_list, p)) {
269 memset(p, 0, sizeof(tACL_CONN));
270 p->in_use = TRUE;
271 p->hci_handle = hci_handle;
272 p->link_role = link_role;
273 p->link_up_issued = FALSE;
274 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
275 /* Set the default version of the peer device to version4.0 before exchange the version with it.
276 If the peer device act as a master and don't exchange the version with us, then it can only use the
277 legacy connect instead of secure connection in the pairing step. */
278 p->lmp_version = HCI_PROTO_VERSION_4_0;
279 #if BLE_INCLUDED == TRUE
280 p->transport = transport;
281 #if BLE_PRIVACY_SPT == TRUE
282 if (transport == BT_TRANSPORT_LE) {
283 btm_ble_refresh_local_resolvable_private_addr(bda,
284 btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
285 }
286 #else
287 p->conn_addr_type = BLE_ADDR_PUBLIC;
288 memcpy(p->conn_addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
289 BTM_TRACE_DEBUG ("conn_addr: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
290 p->conn_addr[0], p->conn_addr[1], p->conn_addr[2], p->conn_addr[3], p->conn_addr[4], p->conn_addr[5]);
291 #endif
292 #endif
293 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
294
295 p->p_pm_mode_db = btm_pm_sm_alloc();
296 #if BTM_PM_DEBUG == TRUE
297 BTM_TRACE_DEBUG( "btm_pm_sm_alloc handle:%d st:%d", hci_handle, p->p_pm_mode_db->state);
298 #endif // BTM_PM_DEBUG
299
300 #if (CLASSIC_BT_INCLUDED == TRUE)
301 btm_sec_update_legacy_auth_state(p, BTM_ACL_LEGACY_AUTH_NONE);
302 #endif
303
304 if (dc) {
305 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
306 }
307
308 if (bdn) {
309 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
310 }
311
312 /* if BR/EDR do something more */
313 if (transport == BT_TRANSPORT_BR_EDR) {
314 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
315 btsnd_hcic_rmt_ver_req (p->hci_handle);
316 }
317 p_dev_rec = btm_find_dev_by_handle (hci_handle);
318
319 #if (BLE_INCLUDED == TRUE)
320 if (p_dev_rec ) {
321 BTM_TRACE_DEBUG ("device_type=0x%x\n", p_dev_rec->device_type);
322 }
323 #endif
324
325 if (p_dev_rec && !(transport == BT_TRANSPORT_LE)) {
326 if (!p_dev_rec->remote_secure_connection_previous_state) {
327 /* If remote features already known, copy them and continue connection setup */
328 if ((p_dev_rec->num_read_pages) &&
329 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1))) {
330 memcpy (p->peer_lmp_features, p_dev_rec->features,
331 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
332 p->num_read_pages = p_dev_rec->num_read_pages;
333 #if (CLASSIC_BT_INCLUDED == TRUE)
334 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
335 #endif ///CLASSIC_BT_INCLUDED == TRUE
336 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
337 #if (SMP_INCLUDED == TRUE)
338 btm_sec_set_peer_sec_caps(p, p_dev_rec);
339 #endif ///SMP_INCLUDED == TRUE
340 #if (CLASSIC_BT_INCLUDED == TRUE)
341 BTM_TRACE_API("%s: pend:%d\n", __FUNCTION__, req_pend);
342 if (req_pend) {
343 /* Request for remaining Security Features (if any) */
344 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
345 }
346 #endif ///CLASSIC_BT_INCLUDED == TRUE
347 btm_establish_continue (p);
348 return;
349 }
350 } else {
351 /* If remote features indicated secure connection (SC) mode, check the remote feautres again*/
352 /* this is to prevent from BIAS attack where attacker can downgrade SC mode*/
353 btm_read_remote_features (p->hci_handle);
354 }
355 }
356
357 #if (BLE_INCLUDED == TRUE)
358 /* If here, features are not known yet */
359 if (p_dev_rec && transport == BT_TRANSPORT_LE) {
360 #if BLE_PRIVACY_SPT == TRUE
361 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
362 &p->active_remote_addr_type);
363 #endif
364
365 if (link_role == HCI_ROLE_MASTER) {
366 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
367 } else if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)
368 && link_role == HCI_ROLE_SLAVE) {
369 btsnd_hcic_rmt_ver_req (p->hci_handle);
370 } else {
371 btm_establish_continue(p);
372 }
373 } else
374 #endif
375 {
376 btm_read_remote_features (p->hci_handle);
377 }
378
379 /* read page 1 - on rmt feature event for buffer reasons */
380 return;
381 }
382 }
383 }
384
385
386 /*******************************************************************************
387 **
388 ** Function btm_acl_report_role_change
389 **
390 ** Description This function is called when the local device is deemed
391 ** to be down. It notifies L2CAP of the failure.
392 **
393 ** Returns void
394 **
395 *******************************************************************************/
btm_acl_report_role_change(UINT8 hci_status,BD_ADDR bda)396 void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
397 {
398 tBTM_ROLE_SWITCH_CMPL ref_data;
399 BTM_TRACE_DEBUG ("btm_acl_report_role_change\n");
400 if (btm_cb.devcb.p_switch_role_cb
401 && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN)))) {
402 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
403 ref_data.hci_status = hci_status;
404 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
405 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
406 btm_cb.devcb.p_switch_role_cb = NULL;
407 }
408 }
409
410 /*******************************************************************************
411 **
412 ** Function btm_acl_removed
413 **
414 ** Description This function is called by L2CAP when an ACL connection
415 ** is removed. Since only L2CAP creates ACL links, we use
416 ** the L2CAP link index as our index into the control blocks.
417 **
418 ** Returns void
419 **
420 *******************************************************************************/
btm_acl_removed(BD_ADDR bda,tBT_TRANSPORT transport)421 void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
422 {
423 tACL_CONN *p;
424 tBTM_BL_EVENT_DATA evt_data;
425 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
426 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
427 #endif
428 BTM_TRACE_DEBUG ("btm_acl_removed\n");
429 p = btm_bda_to_acl(bda, transport);
430 if (p != (tACL_CONN *)NULL) {
431 p->in_use = FALSE;
432
433 /* if the disconnected channel has a pending role switch, clear it now */
434 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
435
436 /* Only notify if link up has had a chance to be issued */
437 if (p->link_up_issued) {
438 p->link_up_issued = FALSE;
439
440 /* If anyone cares, tell him database changed */
441 if (btm_cb.p_bl_changed_cb) {
442 evt_data.event = BTM_BL_DISCN_EVT;
443 evt_data.discn.p_bda = bda;
444 #if BLE_INCLUDED == TRUE
445 evt_data.discn.handle = p->hci_handle;
446 evt_data.discn.transport = p->transport;
447 #endif
448 (*btm_cb.p_bl_changed_cb)(&evt_data);
449 }
450
451 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
452 }
453
454 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
455
456 BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d\n",
457 p->hci_handle,
458 p->transport,
459 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
460 p->link_role);
461
462 p_dev_rec = btm_find_dev(bda);
463 if ( p_dev_rec) {
464 BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags);
465 if (p->transport == BT_TRANSPORT_LE) {
466 BTM_TRACE_DEBUG("LE link down\n");
467 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
468 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0) {
469 BTM_TRACE_DEBUG("Not Bonded\n");
470 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
471 } else {
472 BTM_TRACE_DEBUG("Bonded\n");
473 }
474 } else {
475 BTM_TRACE_DEBUG("Bletooth link down\n");
476 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
477 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
478 }
479 BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags);
480 } else {
481 BTM_TRACE_ERROR("Device not found\n");
482
483 }
484 #endif
485
486 list_remove(btm_cb.p_pm_mode_db_list, p->p_pm_mode_db);
487 /* Clear the ACL connection data */
488 memset(p, 0, sizeof(tACL_CONN));
489 if (list_remove(btm_cb.p_acl_db_list, p)) {
490 p = NULL;
491 }
492 }
493 }
494
495
496 /*******************************************************************************
497 **
498 ** Function btm_acl_device_down
499 **
500 ** Description This function is called when the local device is deemed
501 ** to be down. It notifies L2CAP of the failure.
502 **
503 ** Returns void
504 **
505 *******************************************************************************/
btm_acl_device_down(void)506 void btm_acl_device_down (void)
507 {
508 tACL_CONN *p = NULL;
509 BTM_TRACE_DEBUG ("btm_acl_device_down\n");
510 for (list_node_t *p_node = list_begin(btm_cb.p_acl_db_list); p_node; p_node = list_next(p_node)) {
511 p = list_node(p_node);
512 if (p && p->in_use) {
513 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE \n", p->hci_handle );
514 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
515 }
516 }
517 }
518 /*******************************************************************************
519 **
520 ** Function btm_acl_update_busy_level
521 **
522 ** Description This function is called to update the busy level of the system
523 ** .
524 **
525 ** Returns void
526 **
527 *******************************************************************************/
btm_acl_update_busy_level(tBTM_BLI_EVENT event)528 void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
529 {
530 tBTM_BL_UPDATE_DATA evt;
531 UINT8 busy_level;
532 BTM_TRACE_DEBUG ("btm_acl_update_busy_level\n");
533 BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
534 switch (event) {
535 case BTM_BLI_ACL_UP_EVT:
536 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT\n");
537 break;
538 case BTM_BLI_ACL_DOWN_EVT:
539 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT\n");
540 break;
541 case BTM_BLI_PAGE_EVT:
542 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT\n");
543 btm_cb.is_paging = TRUE;
544 evt.busy_level_flags = BTM_BL_PAGING_STARTED;
545 break;
546 case BTM_BLI_PAGE_DONE_EVT:
547 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT\n");
548 btm_cb.is_paging = FALSE;
549 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
550 break;
551 case BTM_BLI_INQ_EVT:
552 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT\n");
553 btm_cb.is_inquiry = TRUE;
554 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
555 break;
556 case BTM_BLI_INQ_CANCEL_EVT:
557 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT\n");
558 btm_cb.is_inquiry = FALSE;
559 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
560 break;
561 case BTM_BLI_INQ_DONE_EVT:
562 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT\n");
563 btm_cb.is_inquiry = FALSE;
564 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
565 break;
566 }
567
568 if (btm_cb.is_paging || btm_cb.is_inquiry) {
569 busy_level = 10;
570 } else {
571 busy_level = BTM_GetNumAclLinks();
572 }
573
574 if ((busy_level != btm_cb.busy_level) || (old_inquiry_state != btm_cb.is_inquiry)) {
575 evt.event = BTM_BL_UPDATE_EVT;
576 evt.busy_level = busy_level;
577 btm_cb.busy_level = busy_level;
578 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK)) {
579 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
580 }
581 }
582 }
583
584 /*******************************************************************************
585 **
586 ** Function BTM_GetRole
587 **
588 ** Description This function is called to get the role of the local device
589 ** for the ACL connection with the specified remote device
590 **
591 ** Returns BTM_SUCCESS if connection exists.
592 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
593 **
594 *******************************************************************************/
BTM_GetRole(BD_ADDR remote_bd_addr,UINT8 * p_role)595 tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
596 {
597 tACL_CONN *p;
598 BTM_TRACE_DEBUG ("BTM_GetRole\n");
599 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
600 *p_role = BTM_ROLE_UNDEFINED;
601 return (BTM_UNKNOWN_ADDR);
602 }
603
604 /* Get the current role */
605 *p_role = p->link_role;
606 return (BTM_SUCCESS);
607 }
608
609
610 /*******************************************************************************
611 **
612 ** Function BTM_SwitchRole
613 **
614 ** Description This function is called to switch role between master and
615 ** slave. If role is already set it will do nothing. If the
616 ** command was initiated, the callback function is called upon
617 ** completion.
618 **
619 ** Returns BTM_SUCCESS if already in specified role.
620 ** BTM_CMD_STARTED if command issued to controller.
621 ** BTM_NO_RESOURCES if couldn't allocate memory to issue command
622 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
623 ** BTM_MODE_UNSUPPORTED if local device does not support role switching
624 ** BTM_BUSY if the previous command is not completed
625 **
626 *******************************************************************************/
BTM_SwitchRole(BD_ADDR remote_bd_addr,UINT8 new_role,tBTM_CMPL_CB * p_cb)627 tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
628 {
629 tACL_CONN *p;
630 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
631 #if BTM_SCO_INCLUDED == TRUE
632 BOOLEAN is_sco_active;
633 #endif
634 tBTM_STATUS status;
635 tBTM_PM_MODE pwr_mode;
636 tBTM_PM_PWR_MD settings;
637 #if (BT_USE_TRACES == TRUE)
638 BD_ADDR_PTR p_bda;
639 #endif
640 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x\n",
641 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
642 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
643
644 /* Make sure the local device supports switching */
645 if (!controller_get_interface()->supports_master_slave_role_switch()) {
646 return (BTM_MODE_UNSUPPORTED);
647 }
648
649 if (btm_cb.devcb.p_switch_role_cb && p_cb) {
650 #if (BT_USE_TRACES == TRUE)
651 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
652 BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x\n",
653 p_bda[0], p_bda[1], p_bda[2],
654 p_bda[3], p_bda[4], p_bda[5]);
655 #endif
656 return (BTM_BUSY);
657 }
658
659 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
660 return (BTM_UNKNOWN_ADDR);
661 }
662
663 /* Finished if already in desired role */
664 if (p->link_role == new_role) {
665 return (BTM_SUCCESS);
666 }
667
668 #if BTM_SCO_INCLUDED == TRUE
669 /* Check if there is any SCO Active on this BD Address */
670 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
671
672 if (is_sco_active == TRUE) {
673 return (BTM_NO_RESOURCES);
674 }
675 #endif
676
677 /* Ignore role switch request if the previous request was not completed */
678 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE) {
679 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d\n",
680 p->switch_role_state);
681 return (BTM_BUSY);
682 }
683
684 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS) {
685 return (status);
686 }
687
688 /* Wake up the link if in sniff or park before attempting switch */
689 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF) {
690 memset( (void *)&settings, 0, sizeof(settings));
691 settings.mode = BTM_PM_MD_ACTIVE;
692 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
693 if (status != BTM_CMD_STARTED) {
694 return (BTM_WRONG_MODE);
695 }
696
697 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
698 }
699 /* some devices do not support switch while encryption is on */
700 else {
701 p_dev_rec = btm_find_dev (remote_bd_addr);
702 if ((p_dev_rec != NULL)
703 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
704 && !BTM_EPR_AVAILABLE(p)) {
705 /* bypass turning off encryption if change link key is already doing it */
706 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF) {
707 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE)) {
708 return (BTM_NO_RESOURCES);
709 } else {
710 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
711 }
712 }
713
714 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
715 } else {
716 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role)) {
717 return (BTM_NO_RESOURCES);
718 }
719
720 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
721
722 #if BTM_DISC_DURING_RS == TRUE
723 if (p_dev_rec) {
724 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
725 }
726 #endif
727 }
728 }
729
730 /* Initialize return structure in case request fails */
731 if (p_cb) {
732 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
733 BD_ADDR_LEN);
734 btm_cb.devcb.switch_role_ref_data.role = new_role;
735 /* initialized to an error code */
736 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
737 btm_cb.devcb.p_switch_role_cb = p_cb;
738 }
739 return (BTM_CMD_STARTED);
740 }
741
742 /*******************************************************************************
743 **
744 ** Function btm_acl_encrypt_change
745 **
746 ** Description This function is when encryption of the connection is
747 ** completed by the LM. Checks to see if a role switch or
748 ** change of link key was active and initiates or continues
749 ** process if needed.
750 **
751 ** Returns void
752 **
753 *******************************************************************************/
btm_acl_encrypt_change(UINT16 handle,UINT8 status,UINT8 encr_enable)754 void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
755 {
756 tACL_CONN *p;
757 tBTM_SEC_DEV_REC *p_dev_rec;
758 tBTM_BL_ROLE_CHG_DATA evt;
759
760 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d\n",
761 handle, status, encr_enable);
762 p = btm_handle_to_acl(handle);
763 if (p == NULL) {
764 return;
765 }
766 /* Process Role Switch if active */
767 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF) {
768 /* if encryption turn off failed we still will try to switch role */
769 if (encr_enable) {
770 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
771 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
772 } else {
773 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
774 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
775 }
776
777 if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role)) {
778 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
779 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
780 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
781 }
782 #if BTM_DISC_DURING_RS == TRUE
783 else {
784 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
785 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
786 }
787 }
788 #endif
789
790 }
791 /* Finished enabling Encryption after role switch */
792 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON) {
793 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
794 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
795 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
796
797 /* if role change event is registered, report it now */
798 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK)) {
799 evt.event = BTM_BL_ROLE_CHG_EVT;
800 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
801 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
802 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
803 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
804
805 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d\n",
806 evt.new_role, evt.hci_status, p->switch_role_state);
807 }
808
809 #if BTM_DISC_DURING_RS == TRUE
810 /* If a disconnect is pending, issue it now that role switch has completed */
811 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
812 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
813 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!\n");
814 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
815 }
816 BTM_TRACE_WARNING("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d\n",
817 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
818 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
819 }
820 #endif
821 }
822 #if (CLASSIC_BT_INCLUDED == TRUE)
823 /* If authentication is done through legacy authentication and esp32 has
824 * not authenticated peer deivce yet, do not proceed for encrytion and
825 * first authenticate it. */
826 else if ((BTM_BothEndsSupportSecureConnections(p->remote_addr) == 0) &&
827 ((p->legacy_auth_state & BTM_ACL_LEGACY_AUTH_SELF) == 0)) {
828 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL) {
829 if (btm_sec_legacy_authentication_mutual(p_dev_rec)) {
830 btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_SELF);
831 } else {
832 BTM_TRACE_ERROR("%s failed, Resources not available for Authentication procedure", __FUNCTION__);
833 }
834 }
835 }
836 #endif
837
838 }
839 /*******************************************************************************
840 **
841 ** Function BTM_SetLinkPolicy
842 **
843 ** Description Create and send HCI "Write Policy Set" command
844 **
845 ** Returns status of the operation
846 **
847 *******************************************************************************/
BTM_SetLinkPolicy(BD_ADDR remote_bda,UINT16 * settings)848 tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
849 {
850 tACL_CONN *p;
851 UINT8 *localFeatures = BTM_ReadLocalFeatures();
852 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy\n");
853 /* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
854
855 /* First, check if hold mode is supported */
856 if (*settings != HCI_DISABLE_ALL_LM_MODES) {
857 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) ) {
858 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
859 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)\n", *settings );
860 }
861 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) ) {
862 *settings &= (~HCI_ENABLE_HOLD_MODE);
863 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)\n", *settings );
864 }
865 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) ) {
866 *settings &= (~HCI_ENABLE_SNIFF_MODE);
867 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)\n", *settings );
868 }
869 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) ) {
870 *settings &= (~HCI_ENABLE_PARK_MODE);
871 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)\n", *settings );
872 }
873 }
874
875 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL) {
876 return (btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
877 }
878
879 /* If here, no BD Addr found */
880 return (BTM_UNKNOWN_ADDR);
881 }
882
883 /*******************************************************************************
884 **
885 ** Function BTM_SetDefaultLinkPolicy
886 **
887 ** Description Set the default value for HCI "Write Policy Set" command
888 ** to use when an ACL link is created.
889 **
890 ** Returns void
891 **
892 *******************************************************************************/
BTM_SetDefaultLinkPolicy(UINT16 settings)893 void BTM_SetDefaultLinkPolicy (UINT16 settings)
894 {
895 UINT8 *localFeatures = BTM_ReadLocalFeatures();
896
897 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x\n", settings);
898
899 if ((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures))) {
900 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
901 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)\n", settings);
902 }
903 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures))) {
904 settings &= ~HCI_ENABLE_HOLD_MODE;
905 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)\n", settings);
906 }
907 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures))) {
908 settings &= ~HCI_ENABLE_SNIFF_MODE;
909 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)\n", settings);
910 }
911 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures))) {
912 settings &= ~HCI_ENABLE_PARK_MODE;
913 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)\n", settings);
914 }
915 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x\n", settings);
916
917 btm_cb.btm_def_link_policy = settings;
918
919 /* Set the default Link Policy of the controller */
920 btsnd_hcic_write_def_policy_set(settings);
921 }
922
923 /*******************************************************************************
924 **
925 ** Function btm_read_remote_version_complete
926 **
927 ** Description This function is called when the command complete message
928 ** is received from the HCI for the remote version info.
929 **
930 ** Returns void
931 **
932 *******************************************************************************/
btm_read_remote_version_complete(UINT8 * p)933 void btm_read_remote_version_complete (UINT8 *p)
934 {
935 tACL_CONN *p_acl_cb = NULL;
936 UINT8 status;
937 UINT16 handle;
938 BTM_TRACE_DEBUG ("btm_read_remote_version_complete\n");
939
940 STREAM_TO_UINT8 (status, p);
941 STREAM_TO_UINT16 (handle, p);
942
943 /* Look up the connection by handle and copy features */
944 p_acl_cb = btm_handle_to_acl(handle);
945 if (p_acl_cb) {
946 if (status == HCI_SUCCESS) {
947 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
948 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
949 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
950 }
951 #if BLE_INCLUDED == TRUE
952 if (p_acl_cb->transport == BT_TRANSPORT_LE) {
953 if(p_acl_cb->link_role == HCI_ROLE_MASTER) {
954 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl_cb->peer_le_features)) {
955 uint16_t data_length = controller_get_interface()->get_ble_default_data_packet_length();
956 uint16_t data_txtime = controller_get_interface()->get_ble_default_data_packet_txtime();
957 if (data_length != p_acl_cb->data_length_params.tx_len) {
958 p_acl_cb->data_len_updating = true;
959 btsnd_hcic_ble_set_data_length(p_acl_cb->hci_handle, data_length, data_txtime);
960 }
961 }
962 l2cble_notify_le_connection (p_acl_cb->remote_addr);
963 } else {
964 //slave role, read remote feature
965 btsnd_hcic_ble_read_remote_feat(p_acl_cb->hci_handle);
966 }
967 }
968 #endif
969 }
970 }
971
972 /*******************************************************************************
973 **
974 ** Function btm_process_remote_ext_features
975 **
976 ** Description Local function called to process all extended features pages
977 ** read from a remote device.
978 **
979 ** Returns void
980 **
981 *******************************************************************************/
btm_process_remote_ext_features(tACL_CONN * p_acl_cb,UINT8 num_read_pages)982 void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
983 {
984 UINT16 handle = p_acl_cb->hci_handle;
985 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
986 UINT8 page_idx;
987
988 BTM_TRACE_DEBUG ("btm_process_remote_ext_features\n");
989
990 /* Make sure we have the record to save remote features information */
991 if (p_dev_rec == NULL) {
992 /* Get a new device; might be doing dedicated bonding */
993 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
994 }
995
996 p_acl_cb->num_read_pages = num_read_pages;
997 p_dev_rec->num_read_pages = num_read_pages;
998
999 /* Move the pages to placeholder */
1000 for (page_idx = 0; page_idx < num_read_pages; page_idx++) {
1001 if (page_idx > HCI_EXT_FEATURES_PAGE_MAX) {
1002 BTM_TRACE_ERROR("%s: page=%d unexpected\n", __FUNCTION__, page_idx);
1003 break;
1004 }
1005 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1006 HCI_FEATURE_BYTES_PER_PAGE);
1007 }
1008
1009 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1010 #if (SMP_INCLUDED == TRUE)
1011 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
1012 btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
1013 #endif ///SMP_INCLUDED == TRUE
1014 BTM_TRACE_API("%s: pend:%d\n", __FUNCTION__, req_pend);
1015 if (req_pend) {
1016 #if (CLASSIC_BT_INCLUDED == TRUE)
1017 /* Request for remaining Security Features (if any) */
1018 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1019 #endif ///CLASSIC_BT_INCLUDED == TRUE
1020 }
1021 }
1022
1023
1024 /*******************************************************************************
1025 **
1026 ** Function btm_read_remote_features
1027 **
1028 ** Description Local function called to send a read remote supported features/
1029 ** remote extended features page[0].
1030 **
1031 ** Returns void
1032 **
1033 *******************************************************************************/
btm_read_remote_features(UINT16 handle)1034 void btm_read_remote_features (UINT16 handle)
1035 {
1036 tACL_CONN *p_acl_cb;
1037
1038 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d\n", handle);
1039
1040 p_acl_cb = btm_handle_to_acl(handle);
1041 if (p_acl_cb == NULL) {
1042 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid\n", handle);
1043 return;
1044 }
1045
1046 p_acl_cb->num_read_pages = 0;
1047 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1048
1049 /* first send read remote supported features HCI command */
1050 /* because we don't know whether the remote support extended feature command */
1051 btsnd_hcic_rmt_features_req (handle);
1052 }
1053
1054
1055 /*******************************************************************************
1056 **
1057 ** Function btm_read_remote_ext_features
1058 **
1059 ** Description Local function called to send a read remote extended features
1060 **
1061 ** Returns void
1062 **
1063 *******************************************************************************/
btm_read_remote_ext_features(UINT16 handle,UINT8 page_number)1064 void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1065 {
1066 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d\n", handle, page_number);
1067
1068 btsnd_hcic_rmt_ext_features(handle, page_number);
1069 }
1070
1071
1072 /*******************************************************************************
1073 **
1074 ** Function btm_read_remote_features_complete
1075 **
1076 ** Description This function is called when the remote supported features
1077 ** complete event is received from the HCI.
1078 **
1079 ** Returns void
1080 **
1081 *******************************************************************************/
btm_read_remote_features_complete(UINT8 * p)1082 void btm_read_remote_features_complete (UINT8 *p)
1083 {
1084 tACL_CONN *p_acl_cb;
1085 UINT8 status;
1086 UINT16 handle;
1087
1088 BTM_TRACE_DEBUG ("btm_read_remote_features_complete\n");
1089 STREAM_TO_UINT8 (status, p);
1090
1091 if (status != HCI_SUCCESS) {
1092 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)\n", status);
1093 return;
1094 }
1095
1096 STREAM_TO_UINT16 (handle, p);
1097
1098 p_acl_cb = btm_handle_to_acl(handle);
1099 if (p_acl_cb == NULL) {
1100 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid\n", handle);
1101 return;
1102 }
1103
1104 /* Copy the received features page */
1105 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1106 HCI_FEATURE_BYTES_PER_PAGE);
1107
1108 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1109 (controller_get_interface()->supports_reading_remote_extended_features())) {
1110 /* if the remote controller has extended features and local controller supports
1111 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1112 ** with extended features page 1 */
1113 BTM_TRACE_DEBUG ("Start reading remote extended features\n");
1114 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1115 return;
1116 }
1117
1118 /* Remote controller has no extended features. Process remote controller supported features
1119 (features page HCI_EXT_FEATURES_PAGE_0). */
1120 btm_process_remote_ext_features (p_acl_cb, 1);
1121
1122 /* Continue with HCI connection establishment */
1123 btm_establish_continue (p_acl_cb);
1124 }
1125
1126 /*******************************************************************************
1127 **
1128 ** Function btm_read_remote_ext_features_complete
1129 **
1130 ** Description This function is called when the remote extended features
1131 ** complete event is received from the HCI.
1132 **
1133 ** Returns void
1134 **
1135 *******************************************************************************/
btm_read_remote_ext_features_complete(UINT8 * p)1136 void btm_read_remote_ext_features_complete (UINT8 *p)
1137 {
1138 tACL_CONN *p_acl_cb;
1139 UINT8 page_num, max_page;
1140 UINT16 handle;
1141
1142 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete\n");
1143
1144 ++p;
1145 STREAM_TO_UINT16 (handle, p);
1146 STREAM_TO_UINT8 (page_num, p);
1147 STREAM_TO_UINT8 (max_page, p);
1148
1149 /* Validate parameters */
1150 p_acl_cb = btm_handle_to_acl(handle);
1151 if (p_acl_cb == NULL) {
1152 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid\n", handle);
1153 return;
1154 }
1155
1156 if (max_page > HCI_EXT_FEATURES_PAGE_MAX) {
1157 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
1158 }
1159
1160
1161 /* Copy the received features page */
1162 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1163
1164 /* If there is the next remote features page and
1165 * we have space to keep this page data - read this page */
1166 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX)) {
1167 page_num++;
1168 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)\n", page_num);
1169 btm_read_remote_ext_features (handle, page_num);
1170 return;
1171 }
1172
1173 /* Reading of remote feature pages is complete */
1174 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)\n", page_num);
1175
1176 /* Process the pages */
1177 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1178
1179 /* Continue with HCI connection establishment */
1180 btm_establish_continue (p_acl_cb);
1181 }
1182
1183 /*******************************************************************************
1184 **
1185 ** Function btm_read_remote_ext_features_failed
1186 **
1187 ** Description This function is called when the remote extended features
1188 ** complete event returns a failed status.
1189 **
1190 ** Returns void
1191 **
1192 *******************************************************************************/
btm_read_remote_ext_features_failed(UINT8 status,UINT16 handle)1193 void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
1194 {
1195 tACL_CONN *p_acl_cb;
1196
1197 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d\n",
1198 status, handle);
1199
1200 p_acl_cb = btm_handle_to_acl(handle);
1201 if (p_acl_cb == NULL) {
1202 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid\n", handle);
1203 return;
1204 }
1205
1206 /* Process supported features only */
1207 btm_process_remote_ext_features (p_acl_cb, 1);
1208
1209 /* Continue HCI connection establishment */
1210 btm_establish_continue (p_acl_cb);
1211 }
1212
1213 /*******************************************************************************
1214 **
1215 ** Function btm_establish_continue
1216 **
1217 ** Description This function is called when the command complete message
1218 ** is received from the HCI for the read local link policy request.
1219 **
1220 ** Returns void
1221 **
1222 *******************************************************************************/
btm_establish_continue(tACL_CONN * p_acl_cb)1223 void btm_establish_continue (tACL_CONN *p_acl_cb)
1224 {
1225 tBTM_BL_EVENT_DATA evt_data;
1226 BTM_TRACE_DEBUG ("btm_establish_continue\n");
1227 #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1228 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
1229 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
1230 #endif
1231 {
1232 /* For now there are a some devices that do not like sending */
1233 /* commands events and data at the same time. */
1234 /* Set the packet types to the default allowed by the device */
1235 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
1236
1237 if (btm_cb.btm_def_link_policy) {
1238 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1239 }
1240 }
1241 #endif
1242 p_acl_cb->link_up_issued = TRUE;
1243
1244 /* If anyone cares, tell him database changed */
1245 if (btm_cb.p_bl_changed_cb) {
1246 evt_data.event = BTM_BL_CONN_EVT;
1247 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1248 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1249 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1250 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1251 evt_data.conn.sc_downgrade = p_acl_cb->sc_downgrade;
1252 #if BLE_INCLUDED == TRUE
1253 evt_data.conn.handle = p_acl_cb->hci_handle;
1254 evt_data.conn.transport = p_acl_cb->transport;
1255 #endif
1256
1257 (*btm_cb.p_bl_changed_cb)(&evt_data);
1258 }
1259 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1260 }
1261
1262
1263 /*******************************************************************************
1264 **
1265 ** Function BTM_SetDefaultLinkSuperTout
1266 **
1267 ** Description Set the default value for HCI "Write Link Supervision Timeout"
1268 ** command to use when an ACL link is created.
1269 **
1270 ** Returns void
1271 **
1272 *******************************************************************************/
BTM_SetDefaultLinkSuperTout(UINT16 timeout)1273 void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1274 {
1275 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout\n");
1276 btm_cb.btm_def_link_super_tout = timeout;
1277 }
1278
1279 /*******************************************************************************
1280 **
1281 ** Function BTM_GetLinkSuperTout
1282 **
1283 ** Description Read the link supervision timeout value of the connection
1284 **
1285 ** Returns status of the operation
1286 **
1287 *******************************************************************************/
BTM_GetLinkSuperTout(BD_ADDR remote_bda,UINT16 * p_timeout)1288 tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1289 {
1290 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1291
1292 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout\n");
1293 if (p != (tACL_CONN *)NULL) {
1294 *p_timeout = p->link_super_tout;
1295 return (BTM_SUCCESS);
1296 }
1297 /* If here, no BD Addr found */
1298 return (BTM_UNKNOWN_ADDR);
1299 }
1300
1301
1302 /*******************************************************************************
1303 **
1304 ** Function BTM_SetLinkSuperTout
1305 **
1306 ** Description Create and send HCI "Write Link Supervision Timeout" command
1307 **
1308 ** Returns status of the operation
1309 **
1310 *******************************************************************************/
BTM_SetLinkSuperTout(BD_ADDR remote_bda,UINT16 timeout)1311 tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1312 {
1313 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1314
1315 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout\n");
1316 if (p != (tACL_CONN *)NULL) {
1317 p->link_super_tout = timeout;
1318
1319 /* Only send if current role is Master; 2.0 spec requires this */
1320 if (p->link_role == BTM_ROLE_MASTER) {
1321 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1322 p->hci_handle, timeout)) {
1323 return (BTM_NO_RESOURCES);
1324 }
1325
1326 return (BTM_CMD_STARTED);
1327 } else {
1328 return (BTM_SUCCESS);
1329 }
1330 }
1331
1332 /* If here, no BD Addr found */
1333 return (BTM_UNKNOWN_ADDR);
1334 }
1335
1336 /*******************************************************************************
1337 **
1338 ** Function BTM_IsAclConnectionUp
1339 **
1340 ** Description This function is called to check if an ACL connection exists
1341 ** to a specific remote BD Address.
1342 **
1343 ** Returns TRUE if connection is up, else FALSE.
1344 **
1345 *******************************************************************************/
BTM_IsAclConnectionUp(BD_ADDR remote_bda,tBT_TRANSPORT transport)1346 BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1347 {
1348 tACL_CONN *p;
1349
1350 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
1351 remote_bda[0], remote_bda[1], remote_bda[2],
1352 remote_bda[3], remote_bda[4], remote_bda[5]);
1353
1354 p = btm_bda_to_acl(remote_bda, transport);
1355 if (p != (tACL_CONN *)NULL) {
1356 return (TRUE);
1357 }
1358
1359 /* If here, no BD Addr found */
1360 return (FALSE);
1361 }
1362
1363 /*******************************************************************************
1364 **
1365 ** Function BTM_GetNumAclLinks
1366 **
1367 ** Description This function is called to count the number of
1368 ** ACL links that are active.
1369 **
1370 ** Returns UINT16 Number of active ACL links
1371 **
1372 *******************************************************************************/
BTM_GetNumAclLinks(void)1373 UINT16 BTM_GetNumAclLinks (void)
1374 {
1375 uint16_t num_acl = 0;
1376
1377 num_acl = list_length(btm_cb.p_acl_db_list);
1378 return num_acl;
1379 }
1380
1381 /*******************************************************************************
1382 **
1383 ** Function btm_get_acl_disc_reason_code
1384 **
1385 ** Description This function is called to get the disconnection reason code
1386 ** returned by the HCI at disconnection complete event.
1387 **
1388 ** Returns TRUE if connection is up, else FALSE.
1389 **
1390 *******************************************************************************/
btm_get_acl_disc_reason_code(void)1391 UINT16 btm_get_acl_disc_reason_code (void)
1392 {
1393 UINT8 res = btm_cb.acl_disc_reason;
1394 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code\n");
1395 return (res);
1396 }
1397
1398
1399 /*******************************************************************************
1400 **
1401 ** Function BTM_GetHCIConnHandle
1402 **
1403 ** Description This function is called to get the handle for an ACL connection
1404 ** to a specific remote BD Address.
1405 **
1406 ** Returns the handle of the connection, or 0xFFFF if none.
1407 **
1408 *******************************************************************************/
BTM_GetHCIConnHandle(BD_ADDR remote_bda,tBT_TRANSPORT transport)1409 UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1410 {
1411 tACL_CONN *p;
1412 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle\n");
1413 p = btm_bda_to_acl(remote_bda, transport);
1414 if (p != (tACL_CONN *)NULL) {
1415 return (p->hci_handle);
1416 }
1417
1418 /* If here, no BD Addr found */
1419 return (0xFFFF);
1420 }
1421
1422 /*******************************************************************************
1423 **
1424 ** Function btm_process_clk_off_comp_evt
1425 **
1426 ** Description This function is called when clock offset command completes.
1427 **
1428 ** Input Parms hci_handle - connection handle associated with the change
1429 ** clock offset
1430 **
1431 ** Returns void
1432 **
1433 *******************************************************************************/
btm_process_clk_off_comp_evt(UINT16 hci_handle,UINT16 clock_offset)1434 void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
1435 {
1436 tACL_CONN *p_acl_cb = NULL;
1437 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt\n");
1438 /* Look up the connection by handle and set the current mode */
1439 p_acl_cb = btm_handle_to_acl(hci_handle);
1440 if (p_acl_cb) {
1441 p_acl_cb->clock_offset = clock_offset;
1442 }
1443
1444 }
1445
1446 /*******************************************************************************
1447 **
1448 ** Function btm_acl_role_changed
1449 **
1450 ** Description This function is called whan a link's master/slave role change
1451 ** event or command status event (with error) is received.
1452 ** It updates the link control block, and calls
1453 ** the registered callback with status and role (if registered).
1454 **
1455 ** Returns void
1456 **
1457 *******************************************************************************/
btm_acl_role_changed(UINT8 hci_status,BD_ADDR bd_addr,UINT8 new_role)1458 void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
1459 {
1460 UINT8 *p_bda = (bd_addr) ? bd_addr :
1461 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
1462 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
1463 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
1464 tBTM_SEC_DEV_REC *p_dev_rec;
1465 tBTM_BL_ROLE_CHG_DATA evt;
1466
1467 BTM_TRACE_DEBUG ("btm_acl_role_changed\n");
1468 /* Ignore any stray events */
1469 if (p == NULL) {
1470 /* it could be a failure */
1471 if (hci_status != HCI_SUCCESS) {
1472 btm_acl_report_role_change(hci_status, bd_addr);
1473 }
1474 return;
1475 }
1476
1477 p_data->hci_status = hci_status;
1478
1479 if (hci_status == HCI_SUCCESS) {
1480 p_data->role = new_role;
1481 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
1482
1483 /* Update cached value */
1484 p->link_role = new_role;
1485
1486 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
1487 if (new_role == BTM_ROLE_MASTER) {
1488 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
1489 }
1490 } else {
1491 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
1492 new_role = p->link_role;
1493 }
1494
1495 /* Check if any SCO req is pending for role change */
1496 btm_sco_chk_pend_rolechange (p->hci_handle);
1497
1498 /* if switching state is switching we need to turn encryption on */
1499 /* if idle, we did not change encryption */
1500 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
1501 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE)) {
1502 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
1503 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
1504 return;
1505 }
1506 }
1507
1508 /* Set the switch_role_state to IDLE since the reply received from HCI */
1509 /* regardless of its result either success or failed. */
1510 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
1511 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1512 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1513 }
1514
1515 /* if role switch complete is needed, report it now */
1516 btm_acl_report_role_change(hci_status, bd_addr);
1517
1518 /* if role change event is registered, report it now */
1519 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK)) {
1520 evt.event = BTM_BL_ROLE_CHG_EVT;
1521 evt.new_role = new_role;
1522 evt.p_bda = p_bda;
1523 evt.hci_status = hci_status;
1524 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1525 }
1526
1527 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d\n",
1528 p_data->role, p_data->hci_status, p->switch_role_state);
1529
1530 #if BTM_DISC_DURING_RS == TRUE
1531 /* If a disconnect is pending, issue it now that role switch has completed */
1532 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL) {
1533 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
1534 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!\n");
1535 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1536 }
1537 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d\n",
1538 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1539 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1540 }
1541
1542 #endif
1543
1544 }
1545
1546 /*******************************************************************************
1547 **
1548 ** Function BTM_AllocateSCN
1549 **
1550 ** Description Look through the Server Channel Numbers for a free one.
1551 **
1552 ** Returns Allocated SCN number or 0 if none.
1553 **
1554 *******************************************************************************/
1555 #if (CLASSIC_BT_INCLUDED == TRUE)
BTM_AllocateSCN(void)1556 UINT8 BTM_AllocateSCN(void)
1557 {
1558 UINT8 x;
1559 BTM_TRACE_DEBUG ("BTM_AllocateSCN\n");
1560 for (x = 1; x < BTM_MAX_SCN; x++) {
1561 if (!btm_cb.btm_scn[x - 1]) {
1562 btm_cb.btm_scn[x - 1] = TRUE;
1563 return x;
1564 }
1565 }
1566 return (0); /* No free ports */
1567 }
1568 #endif ///CLASSIC_BT_INCLUDED == TRUE
1569
1570 /*******************************************************************************
1571 **
1572 ** Function BTM_TryAllocateSCN
1573 **
1574 ** Description Try to allocate a fixed server channel
1575 **
1576 ** Returns Returns TRUE if server channel was available
1577 **
1578 *******************************************************************************/
1579 #if (CLASSIC_BT_INCLUDED == TRUE)
BTM_TryAllocateSCN(UINT8 scn)1580 BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
1581 {
1582 if (scn >= BTM_MAX_SCN) {
1583 return FALSE;
1584 }
1585
1586 /* check if this port is available */
1587 if (!btm_cb.btm_scn[scn - 1]) {
1588 btm_cb.btm_scn[scn - 1] = TRUE;
1589 return TRUE;
1590 }
1591
1592 return (FALSE); /* Port was busy */
1593 }
1594
1595
1596 /*******************************************************************************
1597 **
1598 ** Function BTM_FreeSCN
1599 **
1600 ** Description Free the specified SCN.
1601 **
1602 ** Returns TRUE or FALSE
1603 **
1604 *******************************************************************************/
BTM_FreeSCN(UINT8 scn)1605 BOOLEAN BTM_FreeSCN(UINT8 scn)
1606 {
1607 BTM_TRACE_DEBUG ("BTM_FreeSCN \n");
1608 if (scn <= BTM_MAX_SCN) {
1609 btm_cb.btm_scn[scn - 1] = FALSE;
1610 return (TRUE);
1611 } else {
1612 return (FALSE); /* Illegal SCN passed in */
1613 }
1614 return (FALSE);
1615 }
1616 #endif ///CLASSIC_BT_INCLUDED == TRUE
1617
1618 /*******************************************************************************
1619 **
1620 ** Function btm_set_packet_types
1621 **
1622 ** Description This function sets the packet types used for a specific
1623 ** ACL connection. It is called internally by btm_acl_created
1624 ** or by an application/profile by BTM_SetPacketTypes.
1625 **
1626 ** Returns status of the operation
1627 **
1628 *******************************************************************************/
btm_set_packet_types(tACL_CONN * p,UINT16 pkt_types)1629 tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
1630 {
1631 UINT16 temp_pkt_types;
1632 BTM_TRACE_DEBUG ("btm_set_packet_types\n");
1633 /* Save in the ACL control blocks, types that we support */
1634 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
1635 btm_cb.btm_acl_pkt_types_supported);
1636
1637 /* OR in any exception packet types if at least 2.0 version of spec */
1638 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
1639 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
1640
1641 /* Exclude packet types not supported by the peer */
1642 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
1643
1644 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x\n", temp_pkt_types);
1645
1646 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types)) {
1647 return (BTM_NO_RESOURCES);
1648 }
1649
1650 p->pkt_types_mask = temp_pkt_types;
1651
1652 return (BTM_CMD_STARTED);
1653 }
1654
1655 /*******************************************************************************
1656 **
1657 ** Function btm_get_max_packet_size
1658 **
1659 ** Returns Returns maximum packet size that can be used for current
1660 ** connection, 0 if connection is not established
1661 **
1662 *******************************************************************************/
btm_get_max_packet_size(BD_ADDR addr)1663 UINT16 btm_get_max_packet_size (BD_ADDR addr)
1664 {
1665 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1666 UINT16 pkt_types = 0;
1667 UINT16 pkt_size = 0;
1668 BTM_TRACE_DEBUG ("btm_get_max_packet_size\n");
1669 if (p != NULL) {
1670 pkt_types = p->pkt_types_mask;
1671 } else {
1672 /* Special case for when info for the local device is requested */
1673 if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0) {
1674 pkt_types = btm_cb.btm_acl_pkt_types_supported;
1675 }
1676 }
1677
1678 if (pkt_types) {
1679 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5)) {
1680 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
1681 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5)) {
1682 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
1683 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3)) {
1684 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
1685 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5) {
1686 pkt_size = HCI_DH5_PACKET_SIZE;
1687 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3)) {
1688 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
1689 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5) {
1690 pkt_size = HCI_DM5_PACKET_SIZE;
1691 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3) {
1692 pkt_size = HCI_DH3_PACKET_SIZE;
1693 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3) {
1694 pkt_size = HCI_DM3_PACKET_SIZE;
1695 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1)) {
1696 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
1697 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1)) {
1698 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
1699 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1) {
1700 pkt_size = HCI_DH1_PACKET_SIZE;
1701 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1) {
1702 pkt_size = HCI_DM1_PACKET_SIZE;
1703 }
1704 }
1705
1706 return (pkt_size);
1707 }
1708
1709 /*******************************************************************************
1710 **
1711 ** Function BTM_ReadRemoteVersion
1712 **
1713 ** Returns If connected report peer device info
1714 **
1715 *******************************************************************************/
BTM_ReadRemoteVersion(BD_ADDR addr,UINT8 * lmp_version,UINT16 * manufacturer,UINT16 * lmp_sub_version)1716 tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
1717 UINT16 *manufacturer, UINT16 *lmp_sub_version)
1718 {
1719 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1720 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion\n");
1721 if (p == NULL) {
1722 return (BTM_UNKNOWN_ADDR);
1723 }
1724
1725 if (lmp_version) {
1726 *lmp_version = p->lmp_version;
1727 }
1728
1729 if (manufacturer) {
1730 *manufacturer = p->manufacturer;
1731 }
1732
1733 if (lmp_sub_version) {
1734 *lmp_sub_version = p->lmp_subversion;
1735 }
1736
1737 return (BTM_SUCCESS);
1738 }
1739
1740 /*******************************************************************************
1741 **
1742 ** Function BTM_ReadRemoteFeatures
1743 **
1744 ** Returns pointer to the remote supported features mask (8 bytes)
1745 **
1746 *******************************************************************************/
BTM_ReadRemoteFeatures(BD_ADDR addr)1747 UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
1748 {
1749 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1750 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures\n");
1751 if (p == NULL) {
1752 return (NULL);
1753 }
1754
1755 return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1756 }
1757
1758 /*******************************************************************************
1759 **
1760 ** Function BTM_ReadRemoteExtendedFeatures
1761 **
1762 ** Returns pointer to the remote extended features mask (8 bytes)
1763 ** or NULL if bad page
1764 **
1765 *******************************************************************************/
BTM_ReadRemoteExtendedFeatures(BD_ADDR addr,UINT8 page_number)1766 UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
1767 {
1768 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1769 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures\n");
1770 if (p == NULL) {
1771 return (NULL);
1772 }
1773
1774 if (page_number > HCI_EXT_FEATURES_PAGE_MAX) {
1775 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown\n", page_number);
1776 return NULL;
1777 }
1778
1779 return (p->peer_lmp_features[page_number]);
1780 }
1781
1782 /*******************************************************************************
1783 **
1784 ** Function BTM_ReadNumberRemoteFeaturesPages
1785 **
1786 ** Returns number of features pages read from the remote device.
1787 **
1788 *******************************************************************************/
BTM_ReadNumberRemoteFeaturesPages(BD_ADDR addr)1789 UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
1790 {
1791 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1792 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages\n");
1793 if (p == NULL) {
1794 return (0);
1795 }
1796
1797 return (p->num_read_pages);
1798 }
1799
1800 /*******************************************************************************
1801 **
1802 ** Function BTM_ReadAllRemoteFeatures
1803 **
1804 ** Returns pointer to all features of the remote (24 bytes).
1805 **
1806 *******************************************************************************/
BTM_ReadAllRemoteFeatures(BD_ADDR addr)1807 UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
1808 {
1809 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1810 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures\n");
1811 if (p == NULL) {
1812 return (NULL);
1813 }
1814
1815 return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1816 }
1817
1818 /*******************************************************************************
1819 **
1820 ** Function BTM_RegBusyLevelNotif
1821 **
1822 ** Description This function is called to register a callback to receive
1823 ** busy level change events.
1824 **
1825 ** Returns BTM_SUCCESS if successfully registered, otherwise error
1826 **
1827 *******************************************************************************/
BTM_RegBusyLevelNotif(tBTM_BL_CHANGE_CB * p_cb,UINT8 * p_level,tBTM_BL_EVENT_MASK evt_mask)1828 tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
1829 tBTM_BL_EVENT_MASK evt_mask)
1830 {
1831 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif\n");
1832 if (p_level) {
1833 *p_level = btm_cb.busy_level;
1834 }
1835
1836 btm_cb.bl_evt_mask = evt_mask;
1837
1838 if (!p_cb) {
1839 btm_cb.p_bl_changed_cb = NULL;
1840 } else if (btm_cb.p_bl_changed_cb) {
1841 return (BTM_BUSY);
1842 } else {
1843 btm_cb.p_bl_changed_cb = p_cb;
1844 }
1845
1846 return (BTM_SUCCESS);
1847 }
1848
1849 /*******************************************************************************
1850 **
1851 ** Function BTM_SetQoS
1852 **
1853 ** Description This function is called to setup QoS
1854 **
1855 ** Returns status of the operation
1856 **
1857 *******************************************************************************/
BTM_SetQoS(BD_ADDR bd,FLOW_SPEC * p_flow,tBTM_CMPL_CB * p_cb)1858 tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
1859 {
1860 tACL_CONN *p = NULL;
1861
1862 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x\n",
1863 bd[0], bd[1], bd[2],
1864 bd[3], bd[4], bd[5]);
1865
1866 /* If someone already waiting on the version, do not allow another */
1867 if (btm_cb.devcb.p_qossu_cmpl_cb) {
1868 return (BTM_BUSY);
1869 }
1870
1871 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL) {
1872 btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_QOS, BTM_DEV_REPLY_TIMEOUT);
1873 btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
1874
1875 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
1876 p_flow->token_rate, p_flow->peak_bandwidth,
1877 p_flow->latency, p_flow->delay_variation)) {
1878 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1879 btu_stop_timer(&btm_cb.devcb.qossu_timer);
1880 return (BTM_NO_RESOURCES);
1881 } else {
1882 return (BTM_CMD_STARTED);
1883 }
1884 }
1885
1886 /* If here, no BD Addr found */
1887 return (BTM_UNKNOWN_ADDR);
1888 }
1889
1890 /*******************************************************************************
1891 **
1892 ** Function btm_qos_setup_complete
1893 **
1894 ** Description This function is called when the command complete message
1895 ** is received from the HCI for the qos setup request.
1896 **
1897 ** Returns void
1898 **
1899 *******************************************************************************/
btm_qos_setup_complete(UINT8 status,UINT16 handle,FLOW_SPEC * p_flow)1900 void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
1901 {
1902 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
1903 tBTM_QOS_SETUP_CMPL qossu;
1904 BTM_TRACE_DEBUG ("btm_qos_setup_complete\n");
1905 btu_stop_timer (&btm_cb.devcb.qossu_timer);
1906
1907 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1908
1909 if (p_cb) {
1910 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
1911 qossu.status = status;
1912 qossu.handle = handle;
1913 tACL_CONN *p = btm_handle_to_acl(handle);
1914 if (p != NULL) {
1915 memcpy (qossu.rem_bda, p->remote_addr, BD_ADDR_LEN);
1916 }
1917 if (p_flow != NULL) {
1918 qossu.flow.qos_flags = p_flow->qos_flags;
1919 qossu.flow.service_type = p_flow->service_type;
1920 qossu.flow.token_rate = p_flow->token_rate;
1921 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
1922 qossu.flow.latency = p_flow->latency;
1923 qossu.flow.delay_variation = p_flow->delay_variation;
1924 }
1925 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x\n",
1926 qossu.flow.delay_variation);
1927 (*p_cb)(&qossu);
1928 }
1929 }
1930
1931 /*******************************************************************************
1932 **
1933 ** Function btm_qos_setup_timeout
1934 **
1935 ** Description This function processes a timeout.
1936 ** Currently, we just report an error log
1937 **
1938 ** Returns void
1939 **
1940 *******************************************************************************/
btm_qos_setup_timeout(void * p_tle)1941 void btm_qos_setup_timeout (void *p_tle)
1942 {
1943 BTM_TRACE_DEBUG ("%s\n", __func__);
1944
1945 btm_qos_setup_complete (HCI_ERR_HOST_TIMEOUT, 0, NULL);
1946 }
1947
1948 /*******************************************************************************
1949 **
1950 ** Function BTM_ReadRSSI
1951 **
1952 ** Description This function is called to read the link policy settings.
1953 ** The address of link policy results are returned in the callback.
1954 ** (tBTM_RSSI_RESULTS)
1955 **
1956 ** Returns BTM_CMD_STARTED if successfully initiated or error code
1957 **
1958 *******************************************************************************/
BTM_ReadRSSI(BD_ADDR remote_bda,tBT_TRANSPORT transport,tBTM_CMPL_CB * p_cb)1959 tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
1960 {
1961 tACL_CONN *p;
1962
1963 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
1964 remote_bda[0], remote_bda[1], remote_bda[2],
1965 remote_bda[3], remote_bda[4], remote_bda[5]);
1966 tBTM_RSSI_RESULTS result;
1967 /* If someone already waiting on the version, do not allow another */
1968 if (btm_cb.devcb.p_rssi_cmpl_cb) {
1969 result.status = BTM_BUSY;
1970 (*p_cb)(&result);
1971 return (BTM_BUSY);
1972 }
1973
1974 p = btm_bda_to_acl(remote_bda, transport);
1975 if (p != (tACL_CONN *)NULL) {
1976 btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
1977 BTM_DEV_REPLY_TIMEOUT);
1978
1979 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
1980
1981 if (!btsnd_hcic_read_rssi (p->hci_handle)) {
1982 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
1983 btu_stop_timer (&btm_cb.devcb.rssi_timer);
1984 result.status = BTM_NO_RESOURCES;
1985 (*p_cb)(&result);
1986 return (BTM_NO_RESOURCES);
1987 } else {
1988 return (BTM_CMD_STARTED);
1989 }
1990 }
1991
1992 /* If here, no BD Addr found */
1993 return (BTM_UNKNOWN_ADDR);
1994 }
1995
1996 /*******************************************************************************
1997 **
1998 ** Function BTM_ReadLinkQuality
1999 **
2000 ** Description This function is called to read the link qulaity.
2001 ** The value of the link quality is returned in the callback.
2002 ** (tBTM_LINK_QUALITY_RESULTS)
2003 **
2004 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2005 **
2006 *******************************************************************************/
BTM_ReadLinkQuality(BD_ADDR remote_bda,tBTM_CMPL_CB * p_cb)2007 tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2008 {
2009 tACL_CONN *p;
2010
2011 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2012 remote_bda[0], remote_bda[1], remote_bda[2],
2013 remote_bda[3], remote_bda[4], remote_bda[5]);
2014
2015 /* If someone already waiting on the version, do not allow another */
2016 if (btm_cb.devcb.p_lnk_qual_cmpl_cb) {
2017 return (BTM_BUSY);
2018 }
2019
2020 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2021 if (p != (tACL_CONN *)NULL) {
2022 btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
2023 BTM_DEV_REPLY_TIMEOUT);
2024 btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
2025
2026 if (!btsnd_hcic_get_link_quality (p->hci_handle)) {
2027 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2028 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2029 return (BTM_NO_RESOURCES);
2030 } else {
2031 return (BTM_CMD_STARTED);
2032 }
2033 }
2034
2035 /* If here, no BD Addr found */
2036 return (BTM_UNKNOWN_ADDR);
2037 }
2038
2039 /*******************************************************************************
2040 **
2041 ** Function BTM_ReadTxPower
2042 **
2043 ** Description This function is called to read the current
2044 ** TX power of the connection. The tx power level results
2045 ** are returned in the callback.
2046 ** (tBTM_RSSI_RESULTS)
2047 **
2048 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2049 **
2050 *******************************************************************************/
BTM_ReadTxPower(BD_ADDR remote_bda,tBT_TRANSPORT transport,tBTM_CMPL_CB * p_cb)2051 tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
2052 {
2053 tACL_CONN *p;
2054 BOOLEAN ret;
2055 #define BTM_READ_RSSI_TYPE_CUR 0x00
2056 #define BTM_READ_RSSI_TYPE_MAX 0X01
2057
2058 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2059 remote_bda[0], remote_bda[1], remote_bda[2],
2060 remote_bda[3], remote_bda[4], remote_bda[5]);
2061
2062 /* If someone already waiting on the version, do not allow another */
2063 if (btm_cb.devcb.p_tx_power_cmpl_cb) {
2064 return (BTM_BUSY);
2065 }
2066
2067 p = btm_bda_to_acl(remote_bda, transport);
2068 if (p != (tACL_CONN *)NULL) {
2069 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2070 BTM_DEV_REPLY_TIMEOUT);
2071
2072 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2073
2074 #if BLE_INCLUDED == TRUE
2075 if (p->transport == BT_TRANSPORT_LE) {
2076 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2077 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2078 } else
2079 #endif
2080 {
2081 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2082 }
2083 if (!ret) {
2084 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2085 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2086 return (BTM_NO_RESOURCES);
2087 } else {
2088 return (BTM_CMD_STARTED);
2089 }
2090 }
2091
2092 /* If here, no BD Addr found */
2093 return (BTM_UNKNOWN_ADDR);
2094 }
2095
2096 #if (BLE_INCLUDED == TRUE)
BTM_BleReadAdvTxPower(tBTM_CMPL_CB * p_cb)2097 tBTM_STATUS BTM_BleReadAdvTxPower(tBTM_CMPL_CB *p_cb)
2098 {
2099 BOOLEAN ret;
2100 tBTM_TX_POWER_RESULTS result;
2101 /* If someone already waiting on the version, do not allow another */
2102 if (btm_cb.devcb.p_tx_power_cmpl_cb) {
2103 result.status = BTM_BUSY;
2104 (*p_cb)(&result);
2105 return (BTM_BUSY);
2106 }
2107
2108 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2109 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2110 BTM_DEV_REPLY_TIMEOUT);
2111 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2112
2113 if(!ret) {
2114 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2115 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2116 result.status = BTM_NO_RESOURCES;
2117 (*p_cb)(&result);
2118 return (BTM_NO_RESOURCES);
2119 } else {
2120 return BTM_CMD_STARTED;
2121 }
2122 }
2123
BTM_BleGetWhiteListSize(uint16_t * length)2124 void BTM_BleGetWhiteListSize(uint16_t *length)
2125 {
2126 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
2127 if (p_cb->white_list_avail_size == 0) {
2128 BTM_TRACE_WARNING("%s Whitelist full.", __func__);
2129 }
2130 *length = p_cb->white_list_avail_size;
2131 return;
2132 }
2133 #endif ///BLE_INCLUDED == TRUE
2134
2135 /*******************************************************************************
2136 **
2137 ** Function btm_read_tx_power_complete
2138 **
2139 ** Description This function is called when the command complete message
2140 ** is received from the HCI for the read tx power request.
2141 **
2142 ** Returns void
2143 **
2144 *******************************************************************************/
btm_read_tx_power_complete(UINT8 * p,BOOLEAN is_ble)2145 void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
2146 {
2147 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2148 tBTM_TX_POWER_RESULTS results;
2149 UINT16 handle;
2150 tACL_CONN *p_acl_cb = NULL;
2151 BTM_TRACE_DEBUG ("btm_read_tx_power_complete\n");
2152 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2153
2154 /* If there was a callback registered for read rssi, call it */
2155 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2156
2157 if (p_cb) {
2158 STREAM_TO_UINT8 (results.hci_status, p);
2159
2160 if (results.hci_status == HCI_SUCCESS) {
2161 results.status = BTM_SUCCESS;
2162
2163 if (!is_ble) {
2164 STREAM_TO_UINT16 (handle, p);
2165 STREAM_TO_UINT8 (results.tx_power, p);
2166
2167 /* Search through the list of active channels for the correct BD Addr */
2168 p_acl_cb = btm_handle_to_acl(handle);
2169 if (p_acl_cb) {
2170 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2171 }
2172 }
2173 #if BLE_INCLUDED == TRUE
2174 else {
2175 STREAM_TO_UINT8 (results.tx_power, p);
2176 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
2177 }
2178 #endif
2179 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x\n",
2180 results.tx_power, results.hci_status);
2181 } else {
2182 results.status = BTM_ERR_PROCESSING;
2183 }
2184
2185 (*p_cb)(&results);
2186 }
2187 }
2188
2189 /*******************************************************************************
2190 **
2191 ** Function btm_read_rssi_complete
2192 **
2193 ** Description This function is called when the command complete message
2194 ** is received from the HCI for the read rssi request.
2195 **
2196 ** Returns void
2197 **
2198 *******************************************************************************/
btm_read_rssi_complete(UINT8 * p)2199 void btm_read_rssi_complete (UINT8 *p)
2200 {
2201 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2202 tBTM_RSSI_RESULTS results;
2203 UINT16 handle;
2204 tACL_CONN *p_acl_cb = NULL;
2205 BTM_TRACE_DEBUG ("btm_read_rssi_complete\n");
2206 btu_stop_timer (&btm_cb.devcb.rssi_timer);
2207
2208 /* If there was a callback registered for read rssi, call it */
2209 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2210
2211 if (p_cb) {
2212 STREAM_TO_UINT8 (results.hci_status, p);
2213
2214 if (results.hci_status == HCI_SUCCESS) {
2215 results.status = BTM_SUCCESS;
2216
2217 STREAM_TO_UINT16 (handle, p);
2218
2219 STREAM_TO_UINT8 (results.rssi, p);
2220 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x\n",
2221 results.rssi, results.hci_status);
2222
2223 /* Search through the list of active channels for the correct BD Addr */
2224 p_acl_cb = btm_handle_to_acl(handle);
2225 if (p_acl_cb) {
2226 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2227 }
2228 } else {
2229 results.status = BTM_ERR_PROCESSING;
2230 }
2231
2232 (*p_cb)(&results);
2233 }
2234 }
2235
2236 /*******************************************************************************
2237 **
2238 ** Function btm_read_link_quality_complete
2239 **
2240 ** Description This function is called when the command complete message
2241 ** is received from the HCI for the read link quality.
2242 **
2243 ** Returns void
2244 **
2245 *******************************************************************************/
btm_read_link_quality_complete(UINT8 * p)2246 void btm_read_link_quality_complete (UINT8 *p)
2247 {
2248 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
2249 tBTM_LINK_QUALITY_RESULTS results;
2250 UINT16 handle;
2251 tACL_CONN *p_acl_cb = NULL;
2252 BTM_TRACE_DEBUG ("btm_read_link_quality_complete\n");
2253 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2254
2255 /* If there was a callback registered for read rssi, call it */
2256 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2257
2258 if (p_cb) {
2259 STREAM_TO_UINT8 (results.hci_status, p);
2260
2261 if (results.hci_status == HCI_SUCCESS) {
2262 results.status = BTM_SUCCESS;
2263
2264 STREAM_TO_UINT16 (handle, p);
2265
2266 STREAM_TO_UINT8 (results.link_quality, p);
2267 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x\n",
2268 results.link_quality, results.hci_status);
2269
2270 /* Search through the list of active channels for the correct BD Addr */
2271 p_acl_cb = btm_handle_to_acl(handle);
2272 if (p_acl_cb) {
2273 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2274 }
2275 } else {
2276 results.status = BTM_ERR_PROCESSING;
2277 }
2278
2279 (*p_cb)(&results);
2280 }
2281 }
2282
2283 /*******************************************************************************
2284 **
2285 ** Function btm_remove_acl
2286 **
2287 ** Description This function is called to disconnect an ACL connection
2288 **
2289 ** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
2290 **
2291 *******************************************************************************/
btm_remove_acl(BD_ADDR bd_addr,tBT_TRANSPORT transport)2292 tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
2293 {
2294 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
2295 tBTM_STATUS status = BTM_SUCCESS;
2296
2297 BTM_TRACE_DEBUG ("btm_remove_acl\n");
2298 #if BTM_DISC_DURING_RS == TRUE
2299 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
2300
2301 /* Role Switch is pending, postpone until completed */
2302 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING)) {
2303 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
2304 } else /* otherwise can disconnect right away */
2305 #endif
2306 {
2307 if (hci_handle != 0xFFFF && p_dev_rec &&
2308 p_dev_rec->sec_state != BTM_SEC_STATE_DISCONNECTING) {
2309 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER)) {
2310 status = BTM_NO_RESOURCES;
2311 }
2312 } else {
2313 status = BTM_UNKNOWN_ADDR;
2314 }
2315 }
2316
2317 return status;
2318 }
2319
2320
2321 /*******************************************************************************
2322 **
2323 ** Function BTM_SetTraceLevel
2324 **
2325 ** Description This function sets the trace level for BTM. If called with
2326 ** a value of 0xFF, it simply returns the current trace level.
2327 **
2328 ** Returns The new or current trace level
2329 **
2330 *******************************************************************************/
BTM_SetTraceLevel(UINT8 new_level)2331 UINT8 BTM_SetTraceLevel (UINT8 new_level)
2332 {
2333 BTM_TRACE_DEBUG ("BTM_SetTraceLevel\n");
2334 if (new_level != 0xFF) {
2335 btm_cb.trace_level = new_level;
2336 }
2337
2338 return (btm_cb.trace_level);
2339 }
2340
2341 /*******************************************************************************
2342 **
2343 ** Function btm_cont_rswitch
2344 **
2345 ** Description This function is called to continue processing an active
2346 ** role switch. It first disables encryption if enabled and
2347 ** EPR is not supported
2348 **
2349 ** Returns void
2350 **
2351 *******************************************************************************/
btm_cont_rswitch(tACL_CONN * p,tBTM_SEC_DEV_REC * p_dev_rec,UINT8 hci_status)2352 void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
2353 UINT8 hci_status)
2354 {
2355 BOOLEAN sw_ok = TRUE;
2356 BTM_TRACE_DEBUG ("btm_cont_rswitch\n");
2357 /* Check to see if encryption needs to be turned off if pending
2358 change of link key or role switch */
2359 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2360 /* Must turn off Encryption first if necessary */
2361 /* Some devices do not support switch or change of link key while encryption is on */
2362 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
2363 && !BTM_EPR_AVAILABLE(p)) {
2364 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE)) {
2365 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
2366 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2367 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
2368 }
2369 } else {
2370 /* Error occurred; set states back to Idle */
2371 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2372 sw_ok = FALSE;
2373 }
2374 }
2375 } else /* Encryption not used or EPR supported, continue with switch
2376 and/or change of link key */
2377 {
2378 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2379 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
2380 #if BTM_DISC_DURING_RS == TRUE
2381 if (p_dev_rec) {
2382 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
2383 }
2384 #endif
2385 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
2386 }
2387 }
2388
2389 if (!sw_ok) {
2390 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2391 btm_acl_report_role_change(hci_status, p->remote_addr);
2392 }
2393 }
2394 }
2395
2396 /*******************************************************************************
2397 **
2398 ** Function btm_acl_resubmit_page
2399 **
2400 ** Description send pending page request
2401 **
2402 *******************************************************************************/
btm_acl_resubmit_page(void)2403 void btm_acl_resubmit_page (void)
2404 {
2405 #if (SMP_INCLUDED == TRUE)
2406 tBTM_SEC_DEV_REC *p_dev_rec;
2407 BT_HDR *p_buf;
2408 UINT8 *pp;
2409 BD_ADDR bda;
2410 BTM_TRACE_DEBUG ("btm_acl_resubmit_page\n");
2411 /* If there were other page request schedule can start the next one */
2412 if ((p_buf = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
2413 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
2414 * for both create_conn and rmt_name */
2415 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
2416
2417 STREAM_TO_BDADDR (bda, pp);
2418
2419 p_dev_rec = btm_find_or_alloc_dev (bda);
2420
2421 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2422 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2423
2424 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
2425 } else {
2426 btm_cb.paging = FALSE;
2427 }
2428 #endif ///SMP_INCLUDED == TRUE
2429 }
2430
2431 /*******************************************************************************
2432 **
2433 ** Function btm_acl_reset_paging
2434 **
2435 ** Description set paging to FALSE and free the page queue - called at hci_reset
2436 **
2437 *******************************************************************************/
btm_acl_reset_paging(void)2438 void btm_acl_reset_paging (void)
2439 {
2440 BT_HDR *p;
2441 BTM_TRACE_DEBUG ("btm_acl_reset_paging\n");
2442 /* If we sent reset we are definitely not paging any more */
2443 while ((p = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
2444 osi_free (p);
2445 }
2446
2447 btm_cb.paging = FALSE;
2448 }
2449
2450 /*******************************************************************************
2451 **
2452 ** Function btm_acl_paging
2453 **
2454 ** Description send a paging command or queue it in btm_cb
2455 **
2456 *******************************************************************************/
2457 #if (SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE)
btm_acl_paging(BT_HDR * p,BD_ADDR bda)2458 void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
2459 {
2460 tBTM_SEC_DEV_REC *p_dev_rec;
2461
2462 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x\n",
2463 btm_cb.discing, btm_cb.paging,
2464 (bda[0] << 16) + (bda[1] << 8) + bda[2], (bda[3] << 16) + (bda[4] << 8) + bda[5]);
2465 if (btm_cb.discing) {
2466 btm_cb.paging = TRUE;
2467 fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
2468 } else {
2469 if (!BTM_ACL_IS_CONNECTED (bda)) {
2470 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x\n",
2471 (btm_cb.connecting_bda[0] << 16) + (btm_cb.connecting_bda[1] << 8) +
2472 btm_cb.connecting_bda[2],
2473 (btm_cb.connecting_bda[3] << 16) + (btm_cb.connecting_bda[4] << 8) +
2474 btm_cb.connecting_bda[5]);
2475 if (btm_cb.paging &&
2476 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0) {
2477 fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
2478 } else {
2479 p_dev_rec = btm_find_or_alloc_dev (bda);
2480 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2481 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2482
2483 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2484 }
2485
2486 btm_cb.paging = TRUE;
2487 } else { /* ACL is already up */
2488 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2489 }
2490 }
2491 }
2492 #endif ///SMP_INCLUDED == TRUE
2493
2494 /*******************************************************************************
2495 **
2496 ** Function btm_acl_notif_conn_collision
2497 **
2498 ** Description Send connection collision event to upper layer if registered
2499 **
2500 ** Returns TRUE if sent out to upper layer,
2501 ** FALSE if no one needs the notification.
2502 **
2503 *******************************************************************************/
btm_acl_notif_conn_collision(BD_ADDR bda)2504 BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
2505 {
2506 tBTM_BL_EVENT_DATA evt_data;
2507
2508 /* Report possible collision to the upper layer. */
2509 if (btm_cb.p_bl_changed_cb) {
2510 BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2511 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2512
2513 evt_data.event = BTM_BL_COLLISION_EVT;
2514 evt_data.conn.p_bda = bda;
2515
2516 #if BLE_INCLUDED == TRUE
2517 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
2518 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
2519 #endif
2520 (*btm_cb.p_bl_changed_cb)(&evt_data);
2521 return TRUE;
2522 } else {
2523 return FALSE;
2524 }
2525 }
2526
2527
2528 /*******************************************************************************
2529 **
2530 ** Function btm_acl_chk_peer_pkt_type_support
2531 **
2532 ** Description Check if peer supports requested packets
2533 **
2534 *******************************************************************************/
btm_acl_chk_peer_pkt_type_support(tACL_CONN * p,UINT16 * p_pkt_type)2535 void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
2536 {
2537 /* 3 and 5 slot packets? */
2538 if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2539 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3);
2540 }
2541
2542 if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2543 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
2544 }
2545
2546 /* 2 and 3 MPS support? */
2547 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2548 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
2549 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
2550 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
2551 }
2552
2553 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2554 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
2555 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
2556 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2557 }
2558
2559 /* EDR 3 and 5 slot support? */
2560 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
2561 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2562 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2563 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
2564 {
2565 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
2566 }
2567
2568 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2569 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
2570 {
2571 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2572 }
2573 }
2574 }
2575
2576 /*******************************************************************************
2577 **
2578 ** Function btm_acl_free
2579 **
2580 ** Description Free acl specific lists from btm control block
2581 **
2582 *******************************************************************************/
btm_acl_free(void)2583 void btm_acl_free(void)
2584 {
2585 list_free(btm_cb.p_acl_db_list);
2586 list_free(btm_cb.p_pm_mode_db_list);
2587 }
2588