1 /******************************************************************************
3 * Copyright (C) 2000-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /*****************************************************************************
23 ** Description: This file contains functions that handle ACL connections.
24 ** This includes operations such as hold and sniff modes,
25 ** supported packet types.
27 ** This module contains both internal and external (API)
28 ** functions. External (API) functions are distinguishable
29 ** by their names beginning with uppercase BTM.
32 ******************************************************************************/
40 #include "bt_target.h"
41 #include "device/include/controller.h"
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);
55 #define BTM_DEV_REPLY_TIMEOUT 3 /* 3 second timeout waiting for responses */
57 /*******************************************************************************
59 ** Function btm_acl_init
61 ** Description This function is called at BTM startup to initialize
65 *******************************************************************************/
66 void btm_acl_init (void)
68 BTM_TRACE_DEBUG ("btm_acl_init");
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;
76 /* Initialize nonzero defaults */
77 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
78 btm_cb.acl_disc_reason = 0xff ;
81 /*******************************************************************************
83 ** Function btm_bda_to_acl
85 ** Description This function returns the FIRST acl_db entry for the passed BDA.
87 ** Parameters bda : BD address of the remote device
88 ** transport : Physical transport used for ACL connection (BR/EDR or LE)
90 ** Returns Returns pointer to the ACL DB for the requested BDA if found.
93 *******************************************************************************/
94 tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
96 tACL_CONN *p = &btm_cb.acl_db[0];
100 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
102 if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
103 #if BLE_INCLUDED == TRUE
104 && p->transport == transport
108 BTM_TRACE_DEBUG ("btm_bda_to_acl found");
114 /* If here, no BD Addr found */
115 return((tACL_CONN *)NULL);
118 /*******************************************************************************
120 ** Function btm_handle_to_acl_index
122 ** Description This function returns the FIRST acl_db entry for the passed hci_handle.
124 ** Returns index to the acl_db or MAX_L2CAP_LINKS.
126 *******************************************************************************/
127 UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
129 tACL_CONN *p = &btm_cb.acl_db[0];
131 BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
132 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
134 if ((p->in_use) && (p->hci_handle == hci_handle))
140 /* If here, no BD Addr found */
144 #if BLE_PRIVACY_SPT == TRUE
145 /*******************************************************************************
147 ** Function btm_ble_get_acl_remote_addr
149 ** Description This function reads the active remote address used for the
152 ** Returns success return TRUE, otherwise FALSE.
154 *******************************************************************************/
155 BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
156 tBLE_ADDR_TYPE *p_addr_type)
158 #if BLE_INCLUDED == TRUE
161 if (p_dev_rec == NULL)
163 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
167 switch (p_dev_rec->ble.active_addr_type)
169 case BTM_BLE_ADDR_PSEUDO:
170 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
171 * p_addr_type = p_dev_rec->ble.ble_addr_type;
174 case BTM_BLE_ADDR_RRA:
175 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
176 * p_addr_type = BLE_ADDR_RANDOM;
179 case BTM_BLE_ADDR_STATIC:
180 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
181 * p_addr_type = p_dev_rec->ble.static_addr_type;
185 BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
199 /*******************************************************************************
201 ** Function btm_acl_created
203 ** Description This function is called by L2CAP when an ACL connection
208 *******************************************************************************/
209 void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
210 UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
212 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
216 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d",
217 hci_handle,link_role, transport);
218 /* Ensure we don't have duplicates */
219 p = btm_bda_to_acl(bda, transport);
220 if (p != (tACL_CONN *)NULL)
222 p->hci_handle = hci_handle;
223 p->link_role = link_role;
224 #if BLE_INCLUDED == TRUE
225 p->transport = transport;
227 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
228 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
229 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
233 /* Allocate acl_db entry */
234 for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
239 p->hci_handle = hci_handle;
240 p->link_role = link_role;
241 p->link_up_issued = FALSE;
242 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
244 #if BLE_INCLUDED == TRUE
245 p->transport = transport;
246 #if BLE_PRIVACY_SPT == TRUE
247 if (transport == BT_TRANSPORT_LE)
248 btm_ble_refresh_local_resolvable_private_addr(bda,
249 btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
252 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
258 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
261 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
263 /* if BR/EDR do something more */
264 if (transport == BT_TRANSPORT_BR_EDR)
266 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
267 btsnd_hcic_rmt_ver_req (p->hci_handle);
269 p_dev_rec = btm_find_dev_by_handle (hci_handle);
271 #if (BLE_INCLUDED == TRUE)
274 BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
278 if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
280 /* If remote features already known, copy them and continue connection setup */
281 if ((p_dev_rec->num_read_pages) &&
282 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)))
284 memcpy (p->peer_lmp_features, p_dev_rec->features,
285 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
286 p->num_read_pages = p_dev_rec->num_read_pages;
288 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
290 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
291 btm_sec_set_peer_sec_caps(p, p_dev_rec);
293 BTM_TRACE_API("%s: pend:%d", __FUNCTION__, req_pend);
296 /* Request for remaining Security Features (if any) */
297 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
299 btm_establish_continue (p);
304 #if (BLE_INCLUDED == TRUE)
305 /* If here, features are not known yet */
306 if (p_dev_rec && transport == BT_TRANSPORT_LE)
308 #if BLE_PRIVACY_SPT == TRUE
309 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
310 &p->active_remote_addr_type);
313 if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)
314 || link_role == HCI_ROLE_MASTER)
316 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
320 btm_establish_continue(p);
326 btm_read_remote_features (p->hci_handle);
329 /* read page 1 - on rmt feature event for buffer reasons */
336 /*******************************************************************************
338 ** Function btm_acl_report_role_change
340 ** Description This function is called when the local device is deemed
341 ** to be down. It notifies L2CAP of the failure.
345 *******************************************************************************/
346 void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
348 tBTM_ROLE_SWITCH_CMPL ref_data;
349 BTM_TRACE_DEBUG ("btm_acl_report_role_change");
350 if (btm_cb.devcb.p_switch_role_cb
351 && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN))))
353 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
354 ref_data.hci_status = hci_status;
355 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
356 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
357 btm_cb.devcb.p_switch_role_cb = NULL;
361 /*******************************************************************************
363 ** Function btm_acl_removed
365 ** Description This function is called by L2CAP when an ACL connection
366 ** is removed. Since only L2CAP creates ACL links, we use
367 ** the L2CAP link index as our index into the control blocks.
371 *******************************************************************************/
372 void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
375 tBTM_BL_EVENT_DATA evt_data;
376 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
377 tBTM_SEC_DEV_REC *p_dev_rec=NULL;
379 BTM_TRACE_DEBUG ("btm_acl_removed");
380 p = btm_bda_to_acl(bda, transport);
381 if (p != (tACL_CONN *)NULL)
385 /* if the disconnected channel has a pending role switch, clear it now */
386 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
388 /* Only notify if link up has had a chance to be issued */
389 if (p->link_up_issued)
391 p->link_up_issued = FALSE;
393 /* If anyone cares, tell him database changed */
394 if (btm_cb.p_bl_changed_cb)
396 evt_data.event = BTM_BL_DISCN_EVT;
397 evt_data.discn.p_bda = bda;
398 #if BLE_INCLUDED == TRUE
399 evt_data.discn.handle = p->hci_handle;
400 evt_data.discn.transport = p->transport;
402 (*btm_cb.p_bl_changed_cb)(&evt_data);
405 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
408 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
410 BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d",
413 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
416 p_dev_rec = btm_find_dev(bda);
419 BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
420 if (p->transport == BT_TRANSPORT_LE)
422 BTM_TRACE_DEBUG("LE link down");
423 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
424 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
426 BTM_TRACE_DEBUG("Not Bonded");
427 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
431 BTM_TRACE_DEBUG("Bonded");
436 BTM_TRACE_DEBUG("Bletooth link down");
437 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
438 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
440 BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
444 BTM_TRACE_ERROR("Device not found");
454 /*******************************************************************************
456 ** Function btm_acl_device_down
458 ** Description This function is called when the local device is deemed
459 ** to be down. It notifies L2CAP of the failure.
463 *******************************************************************************/
464 void btm_acl_device_down (void)
466 tACL_CONN *p = &btm_cb.acl_db[0];
468 BTM_TRACE_DEBUG ("btm_acl_device_down");
469 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
473 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
474 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
479 /*******************************************************************************
481 ** Function btm_acl_update_busy_level
483 ** Description This function is called to update the busy level of the system
488 *******************************************************************************/
489 void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
491 tBTM_BL_UPDATE_DATA evt;
493 BTM_TRACE_DEBUG ("btm_acl_update_busy_level");
494 BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
497 case BTM_BLI_ACL_UP_EVT:
498 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
500 case BTM_BLI_ACL_DOWN_EVT:
501 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT");
503 case BTM_BLI_PAGE_EVT:
504 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
505 btm_cb.is_paging = TRUE;
506 evt.busy_level_flags= BTM_BL_PAGING_STARTED;
508 case BTM_BLI_PAGE_DONE_EVT:
509 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
510 btm_cb.is_paging = FALSE;
511 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
513 case BTM_BLI_INQ_EVT:
514 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
515 btm_cb.is_inquiry = TRUE;
516 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
518 case BTM_BLI_INQ_CANCEL_EVT:
519 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
520 btm_cb.is_inquiry = FALSE;
521 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
523 case BTM_BLI_INQ_DONE_EVT:
524 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
525 btm_cb.is_inquiry = FALSE;
526 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
530 if (btm_cb.is_paging || btm_cb.is_inquiry)
533 busy_level = BTM_GetNumAclLinks();
535 if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
537 evt.event = BTM_BL_UPDATE_EVT;
538 evt.busy_level = busy_level;
539 btm_cb.busy_level = busy_level;
540 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
542 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
547 /*******************************************************************************
549 ** Function BTM_GetRole
551 ** Description This function is called to get the role of the local device
552 ** for the ACL connection with the specified remote device
554 ** Returns BTM_SUCCESS if connection exists.
555 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
557 *******************************************************************************/
558 tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
561 BTM_TRACE_DEBUG ("BTM_GetRole");
562 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
564 *p_role = BTM_ROLE_UNDEFINED;
565 return(BTM_UNKNOWN_ADDR);
568 /* Get the current role */
569 *p_role = p->link_role;
574 /*******************************************************************************
576 ** Function BTM_SwitchRole
578 ** Description This function is called to switch role between master and
579 ** slave. If role is already set it will do nothing. If the
580 ** command was initiated, the callback function is called upon
583 ** Returns BTM_SUCCESS if already in specified role.
584 ** BTM_CMD_STARTED if command issued to controller.
585 ** BTM_NO_RESOURCES if couldn't allocate memory to issue command
586 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
587 ** BTM_MODE_UNSUPPORTED if local device does not support role switching
588 ** BTM_BUSY if the previous command is not completed
590 *******************************************************************************/
591 tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
594 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
595 #if BTM_SCO_INCLUDED == TRUE
596 BOOLEAN is_sco_active;
599 tBTM_PM_MODE pwr_mode;
600 tBTM_PM_PWR_MD settings;
601 #if (BT_USE_TRACES == TRUE)
604 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
605 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
606 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
608 /* Make sure the local device supports switching */
609 if (!controller_get_interface()->supports_master_slave_role_switch())
610 return(BTM_MODE_UNSUPPORTED);
612 if (btm_cb.devcb.p_switch_role_cb && p_cb)
614 #if (BT_USE_TRACES == TRUE)
615 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
616 BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x",
617 p_bda[0], p_bda[1], p_bda[2],
618 p_bda[3], p_bda[4], p_bda[5]);
623 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
624 return(BTM_UNKNOWN_ADDR);
626 /* Finished if already in desired role */
627 if (p->link_role == new_role)
630 #if BTM_SCO_INCLUDED == TRUE
631 /* Check if there is any SCO Active on this BD Address */
632 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
634 if (is_sco_active == TRUE)
635 return(BTM_NO_RESOURCES);
638 /* Ignore role switch request if the previous request was not completed */
639 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
641 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
642 p->switch_role_state);
646 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
649 /* Wake up the link if in sniff or park before attempting switch */
650 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
652 memset( (void*)&settings, 0, sizeof(settings));
653 settings.mode = BTM_PM_MD_ACTIVE;
654 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
655 if (status != BTM_CMD_STARTED)
656 return(BTM_WRONG_MODE);
658 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
660 /* some devices do not support switch while encryption is on */
663 p_dev_rec = btm_find_dev (remote_bd_addr);
664 if ((p_dev_rec != NULL)
665 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
666 && !BTM_EPR_AVAILABLE(p))
668 /* bypass turning off encryption if change link key is already doing it */
669 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
671 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
672 return(BTM_NO_RESOURCES);
674 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
677 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
681 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
682 return(BTM_NO_RESOURCES);
684 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
686 #if BTM_DISC_DURING_RS == TRUE
688 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
693 /* Initialize return structure in case request fails */
696 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
698 btm_cb.devcb.switch_role_ref_data.role = new_role;
699 /* initialized to an error code */
700 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
701 btm_cb.devcb.p_switch_role_cb = p_cb;
703 return(BTM_CMD_STARTED);
706 /*******************************************************************************
708 ** Function btm_acl_encrypt_change
710 ** Description This function is when encryption of the connection is
711 ** completed by the LM. Checks to see if a role switch or
712 ** change of link key was active and initiates or continues
713 ** process if needed.
717 *******************************************************************************/
718 void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
722 tBTM_SEC_DEV_REC *p_dev_rec;
723 tBTM_BL_ROLE_CHG_DATA evt;
725 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
726 handle, status, encr_enable);
727 xx = btm_handle_to_acl_index(handle);
728 /* don't assume that we can never get a bad hci_handle */
729 if (xx < MAX_L2CAP_LINKS)
730 p = &btm_cb.acl_db[xx];
734 /* Process Role Switch if active */
735 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
737 /* if encryption turn off failed we still will try to switch role */
740 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
741 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
745 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
746 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
749 if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role))
751 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
752 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
753 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
755 #if BTM_DISC_DURING_RS == TRUE
758 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
759 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
764 /* Finished enabling Encryption after role switch */
765 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
767 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
768 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
769 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
771 /* if role change event is registered, report it now */
772 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
774 evt.event = BTM_BL_ROLE_CHG_EVT;
775 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
776 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
777 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
778 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
780 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
781 evt.new_role, evt.hci_status, p->switch_role_state);
784 #if BTM_DISC_DURING_RS == TRUE
785 /* If a disconnect is pending, issue it now that role switch has completed */
786 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
788 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
790 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
791 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
793 BTM_TRACE_ERROR("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
794 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
795 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
800 /*******************************************************************************
802 ** Function BTM_SetLinkPolicy
804 ** Description Create and send HCI "Write Policy Set" command
806 ** Returns status of the operation
808 *******************************************************************************/
809 tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
812 UINT8 *localFeatures = BTM_ReadLocalFeatures();
813 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
814 /* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
816 /* First, check if hold mode is supported */
817 if (*settings != HCI_DISABLE_ALL_LM_MODES)
819 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
821 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
822 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
824 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
826 *settings &= (~HCI_ENABLE_HOLD_MODE);
827 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
829 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
831 *settings &= (~HCI_ENABLE_SNIFF_MODE);
832 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
834 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
836 *settings &= (~HCI_ENABLE_PARK_MODE);
837 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
841 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
842 return(btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
844 /* If here, no BD Addr found */
845 return(BTM_UNKNOWN_ADDR);
848 /*******************************************************************************
850 ** Function BTM_SetDefaultLinkPolicy
852 ** Description Set the default value for HCI "Write Policy Set" command
853 ** to use when an ACL link is created.
857 *******************************************************************************/
858 void BTM_SetDefaultLinkPolicy (UINT16 settings)
860 UINT8 *localFeatures = BTM_ReadLocalFeatures();
862 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
864 if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
866 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
867 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
869 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
871 settings &= ~HCI_ENABLE_HOLD_MODE;
872 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
874 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
876 settings &= ~HCI_ENABLE_SNIFF_MODE;
877 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
879 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
881 settings &= ~HCI_ENABLE_PARK_MODE;
882 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
884 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
886 btm_cb.btm_def_link_policy = settings;
888 /* Set the default Link Policy of the controller */
889 btsnd_hcic_write_def_policy_set(settings);
892 /*******************************************************************************
894 ** Function btm_read_remote_version_complete
896 ** Description This function is called when the command complete message
897 ** is received from the HCI for the remote version info.
901 *******************************************************************************/
902 void btm_read_remote_version_complete (UINT8 *p)
904 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
908 BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
909 STREAM_TO_UINT8 (status, p);
910 if (status == HCI_SUCCESS)
912 STREAM_TO_UINT16 (handle, p);
914 /* Look up the connection by handle and copy features */
915 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
917 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
919 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
920 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
921 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
929 /*******************************************************************************
931 ** Function btm_process_remote_ext_features
933 ** Description Local function called to process all extended features pages
934 ** read from a remote device.
938 *******************************************************************************/
939 void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
941 UINT16 handle = p_acl_cb->hci_handle;
942 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
945 BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
947 /* Make sure we have the record to save remote features information */
948 if (p_dev_rec == NULL)
950 /* Get a new device; might be doing dedicated bonding */
951 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
954 p_acl_cb->num_read_pages = num_read_pages;
955 p_dev_rec->num_read_pages = num_read_pages;
957 /* Move the pages to placeholder */
958 for (page_idx = 0; page_idx < num_read_pages; page_idx++)
960 if (page_idx > HCI_EXT_FEATURES_PAGE_MAX)
962 BTM_TRACE_ERROR("%s: page=%d unexpected", __FUNCTION__, page_idx);
965 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
966 HCI_FEATURE_BYTES_PER_PAGE);
969 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
971 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
972 btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
974 BTM_TRACE_API("%s: pend:%d", __FUNCTION__, req_pend);
977 /* Request for remaining Security Features (if any) */
978 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
983 /*******************************************************************************
985 ** Function btm_read_remote_features
987 ** Description Local function called to send a read remote supported features/
988 ** remote extended features page[0].
992 *******************************************************************************/
993 void btm_read_remote_features (UINT16 handle)
998 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
1000 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1002 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
1006 p_acl_cb = &btm_cb.acl_db[acl_idx];
1007 p_acl_cb->num_read_pages = 0;
1008 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1010 /* first send read remote supported features HCI command */
1011 /* because we don't know whether the remote support extended feature command */
1012 btsnd_hcic_rmt_features_req (handle);
1016 /*******************************************************************************
1018 ** Function btm_read_remote_ext_features
1020 ** Description Local function called to send a read remote extended features
1024 *******************************************************************************/
1025 void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1027 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
1029 btsnd_hcic_rmt_ext_features(handle, page_number);
1033 /*******************************************************************************
1035 ** Function btm_read_remote_features_complete
1037 ** Description This function is called when the remote supported features
1038 ** complete event is received from the HCI.
1042 *******************************************************************************/
1043 void btm_read_remote_features_complete (UINT8 *p)
1045 tACL_CONN *p_acl_cb;
1050 BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
1051 STREAM_TO_UINT8 (status, p);
1053 if (status != HCI_SUCCESS)
1055 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
1059 STREAM_TO_UINT16 (handle, p);
1061 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1063 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
1067 p_acl_cb = &btm_cb.acl_db[acl_idx];
1069 /* Copy the received features page */
1070 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1071 HCI_FEATURE_BYTES_PER_PAGE);
1073 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1074 (controller_get_interface()->supports_reading_remote_extended_features()))
1076 /* if the remote controller has extended features and local controller supports
1077 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1078 ** with extended features page 1 */
1079 BTM_TRACE_DEBUG ("Start reading remote extended features");
1080 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1084 /* Remote controller has no extended features. Process remote controller supported features
1085 (features page HCI_EXT_FEATURES_PAGE_0). */
1086 btm_process_remote_ext_features (p_acl_cb, 1);
1088 /* Continue with HCI connection establishment */
1089 btm_establish_continue (p_acl_cb);
1092 /*******************************************************************************
1094 ** Function btm_read_remote_ext_features_complete
1096 ** Description This function is called when the remote extended features
1097 ** complete event is received from the HCI.
1101 *******************************************************************************/
1102 void btm_read_remote_ext_features_complete (UINT8 *p)
1104 tACL_CONN *p_acl_cb;
1105 UINT8 page_num, max_page;
1109 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
1112 STREAM_TO_UINT16 (handle, p);
1113 STREAM_TO_UINT8 (page_num, p);
1114 STREAM_TO_UINT8 (max_page, p);
1116 /* Validate parameters */
1117 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1119 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
1123 if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1125 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
1129 p_acl_cb = &btm_cb.acl_db[acl_idx];
1131 /* Copy the received features page */
1132 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1134 /* If there is the next remote features page and
1135 * we have space to keep this page data - read this page */
1136 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1139 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
1140 btm_read_remote_ext_features (handle, page_num);
1144 /* Reading of remote feature pages is complete */
1145 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
1147 /* Process the pages */
1148 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1150 /* Continue with HCI connection establishment */
1151 btm_establish_continue (p_acl_cb);
1154 /*******************************************************************************
1156 ** Function btm_read_remote_ext_features_failed
1158 ** Description This function is called when the remote extended features
1159 ** complete event returns a failed status.
1163 *******************************************************************************/
1164 void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
1166 tACL_CONN *p_acl_cb;
1169 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
1172 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1174 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
1178 p_acl_cb = &btm_cb.acl_db[acl_idx];
1180 /* Process supported features only */
1181 btm_process_remote_ext_features (p_acl_cb, 1);
1183 /* Continue HCI connection establishment */
1184 btm_establish_continue (p_acl_cb);
1187 /*******************************************************************************
1189 ** Function btm_establish_continue
1191 ** Description This function is called when the command complete message
1192 ** is received from the HCI for the read local link policy request.
1196 *******************************************************************************/
1197 void btm_establish_continue (tACL_CONN *p_acl_cb)
1199 tBTM_BL_EVENT_DATA evt_data;
1200 BTM_TRACE_DEBUG ("btm_establish_continue");
1201 #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1202 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
1203 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
1206 /* For now there are a some devices that do not like sending */
1207 /* commands events and data at the same time. */
1208 /* Set the packet types to the default allowed by the device */
1209 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
1211 if (btm_cb.btm_def_link_policy)
1212 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1215 p_acl_cb->link_up_issued = TRUE;
1217 /* If anyone cares, tell him database changed */
1218 if (btm_cb.p_bl_changed_cb)
1220 evt_data.event = BTM_BL_CONN_EVT;
1221 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1222 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1223 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1224 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1225 #if BLE_INCLUDED == TRUE
1226 evt_data.conn.handle = p_acl_cb->hci_handle;
1227 evt_data.conn.transport = p_acl_cb->transport;
1230 (*btm_cb.p_bl_changed_cb)(&evt_data);
1232 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1236 /*******************************************************************************
1238 ** Function BTM_SetDefaultLinkSuperTout
1240 ** Description Set the default value for HCI "Write Link Supervision Timeout"
1241 ** command to use when an ACL link is created.
1245 *******************************************************************************/
1246 void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1248 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
1249 btm_cb.btm_def_link_super_tout = timeout;
1252 /*******************************************************************************
1254 ** Function BTM_GetLinkSuperTout
1256 ** Description Read the link supervision timeout value of the connection
1258 ** Returns status of the operation
1260 *******************************************************************************/
1261 tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1263 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1265 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
1266 if (p != (tACL_CONN *)NULL)
1268 *p_timeout = p->link_super_tout;
1269 return(BTM_SUCCESS);
1271 /* If here, no BD Addr found */
1272 return(BTM_UNKNOWN_ADDR);
1276 /*******************************************************************************
1278 ** Function BTM_SetLinkSuperTout
1280 ** Description Create and send HCI "Write Link Supervision Timeout" command
1282 ** Returns status of the operation
1284 *******************************************************************************/
1285 tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1287 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1289 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
1290 if (p != (tACL_CONN *)NULL)
1292 p->link_super_tout = timeout;
1294 /* Only send if current role is Master; 2.0 spec requires this */
1295 if (p->link_role == BTM_ROLE_MASTER)
1297 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1298 p->hci_handle, timeout))
1299 return(BTM_NO_RESOURCES);
1301 return(BTM_CMD_STARTED);
1304 return(BTM_SUCCESS);
1307 /* If here, no BD Addr found */
1308 return(BTM_UNKNOWN_ADDR);
1311 /*******************************************************************************
1313 ** Function BTM_IsAclConnectionUp
1315 ** Description This function is called to check if an ACL connection exists
1316 ** to a specific remote BD Address.
1318 ** Returns TRUE if connection is up, else FALSE.
1320 *******************************************************************************/
1321 BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1325 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
1326 remote_bda[0], remote_bda[1], remote_bda[2],
1327 remote_bda[3], remote_bda[4], remote_bda[5]);
1329 p = btm_bda_to_acl(remote_bda, transport);
1330 if (p != (tACL_CONN *)NULL)
1335 /* If here, no BD Addr found */
1339 /*******************************************************************************
1341 ** Function BTM_GetNumAclLinks
1343 ** Description This function is called to count the number of
1344 ** ACL links that are active.
1346 ** Returns UINT16 Number of active ACL links
1348 *******************************************************************************/
1349 UINT16 BTM_GetNumAclLinks (void)
1351 uint16_t num_acl = 0;
1353 for (uint16_t i = 0; i < MAX_L2CAP_LINKS; ++i)
1355 if (btm_cb.acl_db[i].in_use)
1362 /*******************************************************************************
1364 ** Function btm_get_acl_disc_reason_code
1366 ** Description This function is called to get the disconnection reason code
1367 ** returned by the HCI at disconnection complete event.
1369 ** Returns TRUE if connection is up, else FALSE.
1371 *******************************************************************************/
1372 UINT16 btm_get_acl_disc_reason_code (void)
1374 UINT8 res = btm_cb.acl_disc_reason;
1375 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
1380 /*******************************************************************************
1382 ** Function BTM_GetHCIConnHandle
1384 ** Description This function is called to get the handle for an ACL connection
1385 ** to a specific remote BD Address.
1387 ** Returns the handle of the connection, or 0xFFFF if none.
1389 *******************************************************************************/
1390 UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1393 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
1394 p = btm_bda_to_acl(remote_bda, transport);
1395 if (p != (tACL_CONN *)NULL)
1397 return(p->hci_handle);
1400 /* If here, no BD Addr found */
1404 /*******************************************************************************
1406 ** Function btm_process_clk_off_comp_evt
1408 ** Description This function is called when clock offset command completes.
1410 ** Input Parms hci_handle - connection handle associated with the change
1415 *******************************************************************************/
1416 void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
1419 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
1420 /* Look up the connection by handle and set the current mode */
1421 if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
1422 btm_cb.acl_db[xx].clock_offset = clock_offset;
1425 /*******************************************************************************
1427 ** Function btm_acl_role_changed
1429 ** Description This function is called whan a link's master/slave role change
1430 ** event or command status event (with error) is received.
1431 ** It updates the link control block, and calls
1432 ** the registered callback with status and role (if registered).
1436 *******************************************************************************/
1437 void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
1439 UINT8 *p_bda = (bd_addr) ? bd_addr :
1440 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
1441 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
1442 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
1443 tBTM_SEC_DEV_REC *p_dev_rec;
1444 tBTM_BL_ROLE_CHG_DATA evt;
1446 BTM_TRACE_DEBUG ("btm_acl_role_changed");
1447 /* Ignore any stray events */
1450 /* it could be a failure */
1451 if (hci_status != HCI_SUCCESS)
1452 btm_acl_report_role_change(hci_status, bd_addr);
1456 p_data->hci_status = hci_status;
1458 if (hci_status == HCI_SUCCESS)
1460 p_data->role = new_role;
1461 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
1463 /* Update cached value */
1464 p->link_role = new_role;
1466 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
1467 if (new_role == BTM_ROLE_MASTER)
1469 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
1474 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
1475 new_role = p->link_role;
1478 /* Check if any SCO req is pending for role change */
1479 btm_sco_chk_pend_rolechange (p->hci_handle);
1481 /* if switching state is switching we need to turn encryption on */
1482 /* if idle, we did not change encryption */
1483 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
1485 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
1487 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
1488 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
1493 /* Set the switch_role_state to IDLE since the reply received from HCI */
1494 /* regardless of its result either success or failed. */
1495 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
1497 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1498 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1501 /* if role switch complete is needed, report it now */
1502 btm_acl_report_role_change(hci_status, bd_addr);
1504 /* if role change event is registered, report it now */
1505 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
1507 evt.event = BTM_BL_ROLE_CHG_EVT;
1508 evt.new_role = new_role;
1510 evt.hci_status = hci_status;
1511 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1514 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
1515 p_data->role, p_data->hci_status, p->switch_role_state);
1517 #if BTM_DISC_DURING_RS == TRUE
1518 /* If a disconnect is pending, issue it now that role switch has completed */
1519 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
1521 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1523 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
1524 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1526 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
1527 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1528 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1535 /*******************************************************************************
1537 ** Function BTM_AllocateSCN
1539 ** Description Look through the Server Channel Numbers for a free one.
1541 ** Returns Allocated SCN number or 0 if none.
1543 *******************************************************************************/
1545 UINT8 BTM_AllocateSCN(void)
1548 BTM_TRACE_DEBUG ("BTM_AllocateSCN");
1550 // stack reserves scn 1 for HFP, HSP we still do the correct way
1551 for (x = 1; x < BTM_MAX_SCN; x++)
1553 if (!btm_cb.btm_scn[x])
1555 btm_cb.btm_scn[x] = TRUE;
1560 return(0); /* No free ports */
1563 /*******************************************************************************
1565 ** Function BTM_TryAllocateSCN
1567 ** Description Try to allocate a fixed server channel
1569 ** Returns Returns TRUE if server channel was available
1571 *******************************************************************************/
1573 BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
1575 /* Make sure we don't exceed max port range.
1576 * Stack reserves scn 1 for HFP, HSP we still do the correct way.
1578 if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
1581 /* check if this port is available */
1582 if (!btm_cb.btm_scn[scn-1])
1584 btm_cb.btm_scn[scn-1] = TRUE;
1588 return (FALSE); /* Port was busy */
1591 /*******************************************************************************
1593 ** Function BTM_FreeSCN
1595 ** Description Free the specified SCN.
1597 ** Returns TRUE or FALSE
1599 *******************************************************************************/
1600 BOOLEAN BTM_FreeSCN(UINT8 scn)
1602 BTM_TRACE_DEBUG ("BTM_FreeSCN ");
1603 if (scn <= BTM_MAX_SCN)
1605 btm_cb.btm_scn[scn-1] = FALSE;
1609 return(FALSE); /* Illegal SCN passed in */
1612 /*******************************************************************************
1614 ** Function btm_set_packet_types
1616 ** Description This function sets the packet types used for a specific
1617 ** ACL connection. It is called internally by btm_acl_created
1618 ** or by an application/profile by BTM_SetPacketTypes.
1620 ** Returns status of the operation
1622 *******************************************************************************/
1623 tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
1625 UINT16 temp_pkt_types;
1626 BTM_TRACE_DEBUG ("btm_set_packet_types");
1627 /* Save in the ACL control blocks, types that we support */
1628 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
1629 btm_cb.btm_acl_pkt_types_supported);
1631 /* OR in any exception packet types if at least 2.0 version of spec */
1632 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
1633 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
1635 /* Exclude packet types not supported by the peer */
1636 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
1638 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
1640 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
1642 return(BTM_NO_RESOURCES);
1645 p->pkt_types_mask = temp_pkt_types;
1647 return(BTM_CMD_STARTED);
1650 /*******************************************************************************
1652 ** Function btm_get_max_packet_size
1654 ** Returns Returns maximum packet size that can be used for current
1655 ** connection, 0 if connection is not established
1657 *******************************************************************************/
1658 UINT16 btm_get_max_packet_size (BD_ADDR addr)
1660 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1661 UINT16 pkt_types = 0;
1662 UINT16 pkt_size = 0;
1663 BTM_TRACE_DEBUG ("btm_get_max_packet_size");
1666 pkt_types = p->pkt_types_mask;
1670 /* Special case for when info for the local device is requested */
1671 if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0)
1673 pkt_types = btm_cb.btm_acl_pkt_types_supported;
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;
1708 /*******************************************************************************
1710 ** Function BTM_ReadRemoteVersion
1712 ** Returns If connected report peer device info
1714 *******************************************************************************/
1715 tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
1716 UINT16 *manufacturer, UINT16 *lmp_sub_version)
1718 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1719 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
1721 return(BTM_UNKNOWN_ADDR);
1724 *lmp_version = p->lmp_version;
1727 *manufacturer = p->manufacturer;
1729 if (lmp_sub_version)
1730 *lmp_sub_version = p->lmp_subversion;
1732 return(BTM_SUCCESS);
1735 /*******************************************************************************
1737 ** Function BTM_ReadRemoteFeatures
1739 ** Returns pointer to the remote supported features mask (8 bytes)
1741 *******************************************************************************/
1742 UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
1744 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1745 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
1751 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1754 /*******************************************************************************
1756 ** Function BTM_ReadRemoteExtendedFeatures
1758 ** Returns pointer to the remote extended features mask (8 bytes)
1759 ** or NULL if bad page
1761 *******************************************************************************/
1762 UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
1764 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1765 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
1771 if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
1773 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
1777 return(p->peer_lmp_features[page_number]);
1780 /*******************************************************************************
1782 ** Function BTM_ReadNumberRemoteFeaturesPages
1784 ** Returns number of features pages read from the remote device.
1786 *******************************************************************************/
1787 UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
1789 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1790 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
1796 return(p->num_read_pages);
1799 /*******************************************************************************
1801 ** Function BTM_ReadAllRemoteFeatures
1803 ** Returns pointer to all features of the remote (24 bytes).
1805 *******************************************************************************/
1806 UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
1808 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1809 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
1815 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1818 /*******************************************************************************
1820 ** Function BTM_RegBusyLevelNotif
1822 ** Description This function is called to register a callback to receive
1823 ** busy level change events.
1825 ** Returns BTM_SUCCESS if successfully registered, otherwise error
1827 *******************************************************************************/
1828 tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
1829 tBTM_BL_EVENT_MASK evt_mask)
1831 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
1833 *p_level = btm_cb.busy_level;
1835 btm_cb.bl_evt_mask = evt_mask;
1838 btm_cb.p_bl_changed_cb = NULL;
1839 else if (btm_cb.p_bl_changed_cb)
1842 btm_cb.p_bl_changed_cb = p_cb;
1844 return(BTM_SUCCESS);
1847 /*******************************************************************************
1849 ** Function BTM_SetQoS
1851 ** Description This function is called to setup QoS
1853 ** Returns status of the operation
1855 *******************************************************************************/
1856 tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
1858 tACL_CONN *p = &btm_cb.acl_db[0];
1860 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
1861 bd[0], bd[1], bd[2],
1862 bd[3], bd[4], bd[5]);
1864 /* If someone already waiting on the version, do not allow another */
1865 if (btm_cb.devcb.p_qossu_cmpl_cb)
1868 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
1870 btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1871 btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
1873 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
1874 p_flow->token_rate, p_flow->peak_bandwidth,
1875 p_flow->latency,p_flow->delay_variation))
1877 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1878 btu_stop_timer(&btm_cb.devcb.qossu_timer);
1879 return(BTM_NO_RESOURCES);
1882 return(BTM_CMD_STARTED);
1885 /* If here, no BD Addr found */
1886 return(BTM_UNKNOWN_ADDR);
1889 /*******************************************************************************
1891 ** Function btm_qos_setup_complete
1893 ** Description This function is called when the command complete message
1894 ** is received from the HCI for the qos setup request.
1898 *******************************************************************************/
1899 void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
1901 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
1902 tBTM_QOS_SETUP_CMPL qossu;
1903 BTM_TRACE_DEBUG ("btm_qos_setup_complete");
1904 btu_stop_timer (&btm_cb.devcb.qossu_timer);
1906 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1910 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
1911 qossu.status = status;
1912 qossu.handle = handle;
1915 qossu.flow.qos_flags = p_flow->qos_flags;
1916 qossu.flow.service_type = p_flow->service_type;
1917 qossu.flow.token_rate = p_flow->token_rate;
1918 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
1919 qossu.flow.latency = p_flow->latency;
1920 qossu.flow.delay_variation = p_flow->delay_variation;
1922 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
1923 qossu.flow.delay_variation);
1929 /*******************************************************************************
1931 ** Function BTM_ReadRSSI
1933 ** Description This function is called to read the link policy settings.
1934 ** The address of link policy results are returned in the callback.
1935 ** (tBTM_RSSI_RESULTS)
1937 ** Returns BTM_CMD_STARTED if successfully initiated or error code
1939 *******************************************************************************/
1940 tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
1943 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
1944 #if BLE_INCLUDED == TRUE
1945 tBT_DEVICE_TYPE dev_type;
1946 tBLE_ADDR_TYPE addr_type;
1948 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
1949 remote_bda[0], remote_bda[1], remote_bda[2],
1950 remote_bda[3], remote_bda[4], remote_bda[5]);
1952 /* If someone already waiting on the version, do not allow another */
1953 if (btm_cb.devcb.p_rssi_cmpl_cb)
1956 #if BLE_INCLUDED == TRUE
1957 BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
1958 if (dev_type == BT_DEVICE_TYPE_BLE)
1959 transport = BT_TRANSPORT_LE;
1962 p = btm_bda_to_acl(remote_bda, transport);
1963 if (p != (tACL_CONN *)NULL)
1965 btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
1966 BTM_DEV_REPLY_TIMEOUT);
1968 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
1970 if (!btsnd_hcic_read_rssi (p->hci_handle))
1972 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
1973 btu_stop_timer (&btm_cb.devcb.rssi_timer);
1974 return(BTM_NO_RESOURCES);
1977 return(BTM_CMD_STARTED);
1980 /* If here, no BD Addr found */
1981 return(BTM_UNKNOWN_ADDR);
1984 /*******************************************************************************
1986 ** Function BTM_ReadLinkQuality
1988 ** Description This function is called to read the link qulaity.
1989 ** The value of the link quality is returned in the callback.
1990 ** (tBTM_LINK_QUALITY_RESULTS)
1992 ** Returns BTM_CMD_STARTED if successfully initiated or error code
1994 *******************************************************************************/
1995 tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
1999 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2000 remote_bda[0], remote_bda[1], remote_bda[2],
2001 remote_bda[3], remote_bda[4], remote_bda[5]);
2003 /* If someone already waiting on the version, do not allow another */
2004 if (btm_cb.devcb.p_lnk_qual_cmpl_cb)
2007 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2008 if (p != (tACL_CONN *)NULL)
2010 btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
2011 BTM_DEV_REPLY_TIMEOUT);
2012 btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
2014 if (!btsnd_hcic_get_link_quality (p->hci_handle))
2016 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2017 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2018 return(BTM_NO_RESOURCES);
2021 return(BTM_CMD_STARTED);
2024 /* If here, no BD Addr found */
2025 return(BTM_UNKNOWN_ADDR);
2028 /*******************************************************************************
2030 ** Function BTM_ReadTxPower
2032 ** Description This function is called to read the current
2033 ** TX power of the connection. The tx power level results
2034 ** are returned in the callback.
2035 ** (tBTM_RSSI_RESULTS)
2037 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2039 *******************************************************************************/
2040 tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
2044 #define BTM_READ_RSSI_TYPE_CUR 0x00
2045 #define BTM_READ_RSSI_TYPE_MAX 0X01
2047 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2048 remote_bda[0], remote_bda[1], remote_bda[2],
2049 remote_bda[3], remote_bda[4], remote_bda[5]);
2051 /* If someone already waiting on the version, do not allow another */
2052 if (btm_cb.devcb.p_tx_power_cmpl_cb)
2055 p = btm_bda_to_acl(remote_bda, transport);
2056 if (p != (tACL_CONN *)NULL)
2058 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2059 BTM_DEV_REPLY_TIMEOUT);
2061 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2063 #if BLE_INCLUDED == TRUE
2064 if (p->transport == BT_TRANSPORT_LE)
2066 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2067 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2072 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2076 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2077 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2078 return(BTM_NO_RESOURCES);
2081 return(BTM_CMD_STARTED);
2084 /* If here, no BD Addr found */
2085 return (BTM_UNKNOWN_ADDR);
2087 /*******************************************************************************
2089 ** Function btm_read_tx_power_complete
2091 ** Description This function is called when the command complete message
2092 ** is received from the HCI for the read tx power request.
2096 *******************************************************************************/
2097 void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
2099 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2100 tBTM_TX_POWER_RESULTS results;
2102 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2104 BTM_TRACE_DEBUG ("btm_read_tx_power_complete");
2105 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2107 /* If there was a callback registered for read rssi, call it */
2108 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2112 STREAM_TO_UINT8 (results.hci_status, p);
2114 if (results.hci_status == HCI_SUCCESS)
2116 results.status = BTM_SUCCESS;
2120 STREAM_TO_UINT16 (handle, p);
2121 STREAM_TO_UINT8 (results.tx_power, p);
2123 /* Search through the list of active channels for the correct BD Addr */
2124 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2126 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2128 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2133 #if BLE_INCLUDED == TRUE
2136 STREAM_TO_UINT8 (results.tx_power, p);
2137 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
2140 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
2141 results.tx_power, results.hci_status);
2144 results.status = BTM_ERR_PROCESSING;
2150 /*******************************************************************************
2152 ** Function btm_read_rssi_complete
2154 ** Description This function is called when the command complete message
2155 ** is received from the HCI for the read rssi request.
2159 *******************************************************************************/
2160 void btm_read_rssi_complete (UINT8 *p)
2162 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2163 tBTM_RSSI_RESULTS results;
2165 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2167 BTM_TRACE_DEBUG ("btm_read_rssi_complete");
2168 btu_stop_timer (&btm_cb.devcb.rssi_timer);
2170 /* If there was a callback registered for read rssi, call it */
2171 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2175 STREAM_TO_UINT8 (results.hci_status, p);
2177 if (results.hci_status == HCI_SUCCESS)
2179 results.status = BTM_SUCCESS;
2181 STREAM_TO_UINT16 (handle, p);
2183 STREAM_TO_UINT8 (results.rssi, p);
2184 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
2185 results.rssi, results.hci_status);
2187 /* Search through the list of active channels for the correct BD Addr */
2188 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2190 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2192 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2198 results.status = BTM_ERR_PROCESSING;
2204 /*******************************************************************************
2206 ** Function btm_read_link_quality_complete
2208 ** Description This function is called when the command complete message
2209 ** is received from the HCI for the read link quality.
2213 *******************************************************************************/
2214 void btm_read_link_quality_complete (UINT8 *p)
2216 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
2217 tBTM_LINK_QUALITY_RESULTS results;
2219 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2221 BTM_TRACE_DEBUG ("btm_read_link_quality_complete");
2222 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2224 /* If there was a callback registered for read rssi, call it */
2225 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2229 STREAM_TO_UINT8 (results.hci_status, p);
2231 if (results.hci_status == HCI_SUCCESS)
2233 results.status = BTM_SUCCESS;
2235 STREAM_TO_UINT16 (handle, p);
2237 STREAM_TO_UINT8 (results.link_quality, p);
2238 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
2239 results.link_quality, results.hci_status);
2241 /* Search through the list of active channels for the correct BD Addr */
2242 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2244 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2246 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2252 results.status = BTM_ERR_PROCESSING;
2258 /*******************************************************************************
2260 ** Function btm_remove_acl
2262 ** Description This function is called to disconnect an ACL connection
2264 ** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
2266 *******************************************************************************/
2267 tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
2269 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
2270 tBTM_STATUS status = BTM_SUCCESS;
2272 BTM_TRACE_DEBUG ("btm_remove_acl");
2273 #if BTM_DISC_DURING_RS == TRUE
2274 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
2276 /* Role Switch is pending, postpone until completed */
2277 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
2279 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
2281 else /* otherwise can disconnect right away */
2284 if (hci_handle != 0xFFFF && p_dev_rec &&
2285 p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
2287 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
2288 status = BTM_NO_RESOURCES;
2291 status = BTM_UNKNOWN_ADDR;
2298 /*******************************************************************************
2300 ** Function BTM_SetTraceLevel
2302 ** Description This function sets the trace level for BTM. If called with
2303 ** a value of 0xFF, it simply returns the current trace level.
2305 ** Returns The new or current trace level
2307 *******************************************************************************/
2308 UINT8 BTM_SetTraceLevel (UINT8 new_level)
2310 BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
2311 if (new_level != 0xFF)
2312 btm_cb.trace_level = new_level;
2314 return(btm_cb.trace_level);
2317 /*******************************************************************************
2319 ** Function btm_cont_rswitch
2321 ** Description This function is called to continue processing an active
2322 ** role switch. It first disables encryption if enabled and
2323 ** EPR is not supported
2327 *******************************************************************************/
2328 void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
2331 BOOLEAN sw_ok = TRUE;
2332 BTM_TRACE_DEBUG ("btm_cont_rswitch");
2333 /* Check to see if encryption needs to be turned off if pending
2334 change of link key or role switch */
2335 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2337 /* Must turn off Encryption first if necessary */
2338 /* Some devices do not support switch or change of link key while encryption is on */
2339 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
2340 && !BTM_EPR_AVAILABLE(p))
2342 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
2344 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
2345 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2346 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
2350 /* Error occurred; set states back to Idle */
2351 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2355 else /* Encryption not used or EPR supported, continue with switch
2356 and/or change of link key */
2358 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2360 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
2361 #if BTM_DISC_DURING_RS == TRUE
2363 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
2365 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
2371 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2372 btm_acl_report_role_change(hci_status, p->remote_addr);
2377 /*******************************************************************************
2379 ** Function btm_acl_resubmit_page
2381 ** Description send pending page request
2383 *******************************************************************************/
2384 void btm_acl_resubmit_page (void)
2386 tBTM_SEC_DEV_REC *p_dev_rec;
2390 BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
2391 /* If there were other page request schedule can start the next one */
2392 if ((p_buf = (BT_HDR *)GKI_dequeue (&btm_cb.page_queue)) != NULL)
2394 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
2395 * for both create_conn and rmt_name */
2396 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
2398 STREAM_TO_BDADDR (bda, pp);
2400 p_dev_rec = btm_find_or_alloc_dev (bda);
2402 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2403 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2405 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
2408 btm_cb.paging = FALSE;
2411 /*******************************************************************************
2413 ** Function btm_acl_reset_paging
2415 ** Description set paging to FALSE and free the page queue - called at hci_reset
2417 *******************************************************************************/
2418 void btm_acl_reset_paging (void)
2421 BTM_TRACE_DEBUG ("btm_acl_reset_paging");
2422 /* If we sent reset we are definitely not paging any more */
2423 while ((p = (BT_HDR *)GKI_dequeue(&btm_cb.page_queue)) != NULL)
2426 btm_cb.paging = FALSE;
2429 /*******************************************************************************
2431 ** Function btm_acl_paging
2433 ** Description send a paging command or queue it in btm_cb
2435 *******************************************************************************/
2436 void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
2438 tBTM_SEC_DEV_REC *p_dev_rec;
2440 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
2441 btm_cb.discing, btm_cb.paging,
2442 (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
2445 btm_cb.paging = TRUE;
2446 GKI_enqueue (&btm_cb.page_queue, p);
2450 if (!BTM_ACL_IS_CONNECTED (bda))
2452 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
2453 (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
2454 btm_cb.connecting_bda[2],
2455 (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
2456 btm_cb.connecting_bda[5]);
2457 if (btm_cb.paging &&
2458 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
2460 GKI_enqueue (&btm_cb.page_queue, p);
2464 p_dev_rec = btm_find_or_alloc_dev (bda);
2465 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2466 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2468 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2471 btm_cb.paging = TRUE;
2473 else /* ACL is already up */
2475 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2480 /*******************************************************************************
2482 ** Function btm_acl_notif_conn_collision
2484 ** Description Send connection collision event to upper layer if registered
2486 ** Returns TRUE if sent out to upper layer,
2487 ** FALSE if no one needs the notification.
2489 *******************************************************************************/
2490 BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
2492 tBTM_BL_EVENT_DATA evt_data;
2494 /* Report possible collision to the upper layer. */
2495 if (btm_cb.p_bl_changed_cb)
2497 BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2498 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2500 evt_data.event = BTM_BL_COLLISION_EVT;
2501 evt_data.conn.p_bda = bda;
2503 #if BLE_INCLUDED == TRUE
2504 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
2505 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
2507 (*btm_cb.p_bl_changed_cb)(&evt_data);
2515 /*******************************************************************************
2517 ** Function btm_acl_chk_peer_pkt_type_support
2519 ** Description Check if peer supports requested packets
2521 *******************************************************************************/
2522 void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
2524 /* 3 and 5 slot packets? */
2525 if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2526 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
2528 if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2529 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
2531 /* 2 and 3 MPS support? */
2532 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2533 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
2534 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
2535 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
2537 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2538 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
2539 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
2540 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2542 /* EDR 3 and 5 slot support? */
2543 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
2544 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2546 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2547 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
2548 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
2550 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2551 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
2552 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);