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"
42 #include "bt_common.h"
52 extern fixed_queue_t *btu_general_alarm_queue;
54 static void btm_read_remote_features (UINT16 handle);
55 static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
56 static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages);
58 /* 3 seconds timeout waiting for responses */
59 #define BTM_DEV_REPLY_TIMEOUT_MS (3 * 1000)
61 /*******************************************************************************
63 ** Function btm_acl_init
65 ** Description This function is called at BTM startup to initialize
69 *******************************************************************************/
70 void btm_acl_init (void)
72 BTM_TRACE_DEBUG ("btm_acl_init");
73 #if 0 /* cleared in btm_init; put back in if called from anywhere else! */
74 memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
75 memset (btm_cb.btm_scn, 0, BTM_MAX_SCN); /* Initialize the SCN usage to FALSE */
76 btm_cb.btm_def_link_policy = 0;
77 btm_cb.p_bl_changed_cb = NULL;
80 /* Initialize nonzero defaults */
81 btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
82 btm_cb.acl_disc_reason = 0xff ;
85 /*******************************************************************************
87 ** Function btm_bda_to_acl
89 ** Description This function returns the FIRST acl_db entry for the passed BDA.
91 ** Parameters bda : BD address of the remote device
92 ** transport : Physical transport used for ACL connection (BR/EDR or LE)
94 ** Returns Returns pointer to the ACL DB for the requested BDA if found.
97 *******************************************************************************/
98 tACL_CONN *btm_bda_to_acl (const BD_ADDR bda, tBT_TRANSPORT transport)
100 tACL_CONN *p = &btm_cb.acl_db[0];
104 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
106 if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
107 #if BLE_INCLUDED == TRUE
108 && p->transport == transport
112 BTM_TRACE_DEBUG ("btm_bda_to_acl found");
118 /* If here, no BD Addr found */
119 return((tACL_CONN *)NULL);
122 /*******************************************************************************
124 ** Function btm_handle_to_acl_index
126 ** Description This function returns the FIRST acl_db entry for the passed hci_handle.
128 ** Returns index to the acl_db or MAX_L2CAP_LINKS.
130 *******************************************************************************/
131 UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
133 tACL_CONN *p = &btm_cb.acl_db[0];
135 BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
136 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
138 if ((p->in_use) && (p->hci_handle == hci_handle))
144 /* If here, no BD Addr found */
148 #if BLE_PRIVACY_SPT == TRUE
149 /*******************************************************************************
151 ** Function btm_ble_get_acl_remote_addr
153 ** Description This function reads the active remote address used for the
156 ** Returns success return TRUE, otherwise FALSE.
158 *******************************************************************************/
159 BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
160 tBLE_ADDR_TYPE *p_addr_type)
162 #if BLE_INCLUDED == TRUE
165 if (p_dev_rec == NULL)
167 BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
171 switch (p_dev_rec->ble.active_addr_type)
173 case BTM_BLE_ADDR_PSEUDO:
174 memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
175 * p_addr_type = p_dev_rec->ble.ble_addr_type;
178 case BTM_BLE_ADDR_RRA:
179 memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
180 * p_addr_type = BLE_ADDR_RANDOM;
183 case BTM_BLE_ADDR_STATIC:
184 memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
185 * p_addr_type = p_dev_rec->ble.static_addr_type;
189 BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
203 /*******************************************************************************
205 ** Function btm_acl_created
207 ** Description This function is called by L2CAP when an ACL connection
212 *******************************************************************************/
213 void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
214 UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
216 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
220 BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d transport=%d",
221 hci_handle,link_role, transport);
222 /* Ensure we don't have duplicates */
223 p = btm_bda_to_acl(bda, transport);
224 if (p != (tACL_CONN *)NULL)
226 p->hci_handle = hci_handle;
227 p->link_role = link_role;
228 #if BLE_INCLUDED == TRUE
229 p->transport = transport;
231 BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
232 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
233 BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
237 /* Allocate acl_db entry */
238 for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
243 p->hci_handle = hci_handle;
244 p->link_role = link_role;
245 p->link_up_issued = FALSE;
246 memcpy (p->remote_addr, bda, BD_ADDR_LEN);
248 #if BLE_INCLUDED == TRUE
249 p->transport = transport;
250 #if BLE_PRIVACY_SPT == TRUE
251 if (transport == BT_TRANSPORT_LE)
252 btm_ble_refresh_local_resolvable_private_addr(bda,
253 btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
255 p->conn_addr_type = BLE_ADDR_PUBLIC;
256 memcpy(p->conn_addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
260 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
266 memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
269 memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
271 /* if BR/EDR do something more */
272 if (transport == BT_TRANSPORT_BR_EDR)
274 btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
275 btsnd_hcic_rmt_ver_req (p->hci_handle);
277 p_dev_rec = btm_find_dev_by_handle (hci_handle);
279 #if (BLE_INCLUDED == TRUE)
282 BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
286 if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
288 /* If remote features already known, copy them and continue connection setup */
289 if ((p_dev_rec->num_read_pages) &&
290 (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)))
292 memcpy (p->peer_lmp_features, p_dev_rec->features,
293 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
294 p->num_read_pages = p_dev_rec->num_read_pages;
296 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
298 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
299 btm_sec_set_peer_sec_caps(p, p_dev_rec);
301 BTM_TRACE_API("%s: pend:%d", __FUNCTION__, req_pend);
304 /* Request for remaining Security Features (if any) */
305 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
307 btm_establish_continue (p);
312 #if (BLE_INCLUDED == TRUE)
313 /* If here, features are not known yet */
314 if (p_dev_rec && transport == BT_TRANSPORT_LE)
316 #if BLE_PRIVACY_SPT == TRUE
317 btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
318 &p->active_remote_addr_type);
321 if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)
322 || link_role == HCI_ROLE_MASTER)
324 btsnd_hcic_ble_read_remote_feat(p->hci_handle);
328 btm_establish_continue(p);
334 btm_read_remote_features (p->hci_handle);
337 /* read page 1 - on rmt feature event for buffer reasons */
344 /*******************************************************************************
346 ** Function btm_acl_report_role_change
348 ** Description This function is called when the local device is deemed
349 ** to be down. It notifies L2CAP of the failure.
353 *******************************************************************************/
354 void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
356 tBTM_ROLE_SWITCH_CMPL ref_data;
357 BTM_TRACE_DEBUG ("btm_acl_report_role_change");
358 if (btm_cb.devcb.p_switch_role_cb
359 && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN))))
361 memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
362 ref_data.hci_status = hci_status;
363 (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
364 memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
365 btm_cb.devcb.p_switch_role_cb = NULL;
369 /*******************************************************************************
371 ** Function btm_acl_removed
373 ** Description This function is called by L2CAP when an ACL connection
374 ** is removed. Since only L2CAP creates ACL links, we use
375 ** the L2CAP link index as our index into the control blocks.
379 *******************************************************************************/
380 void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
383 tBTM_BL_EVENT_DATA evt_data;
384 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
385 tBTM_SEC_DEV_REC *p_dev_rec=NULL;
387 BTM_TRACE_DEBUG ("btm_acl_removed");
388 p = btm_bda_to_acl(bda, transport);
389 if (p != (tACL_CONN *)NULL)
393 /* if the disconnected channel has a pending role switch, clear it now */
394 btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
396 /* Only notify if link up has had a chance to be issued */
397 if (p->link_up_issued)
399 p->link_up_issued = FALSE;
401 /* If anyone cares, tell him database changed */
402 if (btm_cb.p_bl_changed_cb)
404 evt_data.event = BTM_BL_DISCN_EVT;
405 evt_data.discn.p_bda = bda;
406 #if BLE_INCLUDED == TRUE
407 evt_data.discn.handle = p->hci_handle;
408 evt_data.discn.transport = p->transport;
410 (*btm_cb.p_bl_changed_cb)(&evt_data);
413 btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
416 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
418 BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d",
421 btm_cb.ble_ctr_cb.inq_var.connectable_mode,
424 p_dev_rec = btm_find_dev(bda);
427 BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
428 if (p->transport == BT_TRANSPORT_LE)
430 BTM_TRACE_DEBUG("LE link down");
431 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
432 if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
434 BTM_TRACE_DEBUG("Not Bonded");
435 p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
439 BTM_TRACE_DEBUG("Bonded");
444 BTM_TRACE_DEBUG("Bletooth link down");
445 p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
446 | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
448 BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
452 BTM_TRACE_ERROR("Device not found");
457 /* Clear the ACL connection data */
458 memset(p, 0, sizeof(tACL_CONN));
463 /*******************************************************************************
465 ** Function btm_acl_device_down
467 ** Description This function is called when the local device is deemed
468 ** to be down. It notifies L2CAP of the failure.
472 *******************************************************************************/
473 void btm_acl_device_down (void)
475 tACL_CONN *p = &btm_cb.acl_db[0];
477 BTM_TRACE_DEBUG ("btm_acl_device_down");
478 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
482 BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
483 l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
488 /*******************************************************************************
490 ** Function btm_acl_update_busy_level
492 ** Description This function is called to update the busy level of the system
497 *******************************************************************************/
498 void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
500 tBTM_BL_UPDATE_DATA evt;
502 BTM_TRACE_DEBUG ("btm_acl_update_busy_level");
503 BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
506 case BTM_BLI_ACL_UP_EVT:
507 BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
509 case BTM_BLI_ACL_DOWN_EVT:
510 BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT");
512 case BTM_BLI_PAGE_EVT:
513 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
514 btm_cb.is_paging = TRUE;
515 evt.busy_level_flags= BTM_BL_PAGING_STARTED;
517 case BTM_BLI_PAGE_DONE_EVT:
518 BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
519 btm_cb.is_paging = FALSE;
520 evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
522 case BTM_BLI_INQ_EVT:
523 BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
524 btm_cb.is_inquiry = TRUE;
525 evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
527 case BTM_BLI_INQ_CANCEL_EVT:
528 BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
529 btm_cb.is_inquiry = FALSE;
530 evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
532 case BTM_BLI_INQ_DONE_EVT:
533 BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
534 btm_cb.is_inquiry = FALSE;
535 evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
539 if (btm_cb.is_paging || btm_cb.is_inquiry)
542 busy_level = BTM_GetNumAclLinks();
544 if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
546 evt.event = BTM_BL_UPDATE_EVT;
547 evt.busy_level = busy_level;
548 btm_cb.busy_level = busy_level;
549 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
551 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
556 /*******************************************************************************
558 ** Function BTM_GetRole
560 ** Description This function is called to get the role of the local device
561 ** for the ACL connection with the specified remote device
563 ** Returns BTM_SUCCESS if connection exists.
564 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
566 *******************************************************************************/
567 tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
570 BTM_TRACE_DEBUG ("BTM_GetRole");
571 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
573 *p_role = BTM_ROLE_UNDEFINED;
574 return(BTM_UNKNOWN_ADDR);
577 /* Get the current role */
578 *p_role = p->link_role;
583 /*******************************************************************************
585 ** Function BTM_SwitchRole
587 ** Description This function is called to switch role between master and
588 ** slave. If role is already set it will do nothing. If the
589 ** command was initiated, the callback function is called upon
592 ** Returns BTM_SUCCESS if already in specified role.
593 ** BTM_CMD_STARTED if command issued to controller.
594 ** BTM_NO_RESOURCES if couldn't allocate memory to issue command
595 ** BTM_UNKNOWN_ADDR if no active link with bd addr specified
596 ** BTM_MODE_UNSUPPORTED if local device does not support role switching
597 ** BTM_BUSY if the previous command is not completed
599 *******************************************************************************/
600 tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
603 tBTM_SEC_DEV_REC *p_dev_rec = NULL;
604 #if BTM_SCO_INCLUDED == TRUE
605 BOOLEAN is_sco_active;
608 tBTM_PM_MODE pwr_mode;
609 tBTM_PM_PWR_MD settings;
610 #if (BT_USE_TRACES == TRUE)
613 BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
614 remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
615 remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
617 /* Make sure the local device supports switching */
618 if (!controller_get_interface()->supports_master_slave_role_switch())
619 return(BTM_MODE_UNSUPPORTED);
621 if (btm_cb.devcb.p_switch_role_cb && p_cb)
623 #if (BT_USE_TRACES == TRUE)
624 p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
625 BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x",
626 p_bda[0], p_bda[1], p_bda[2],
627 p_bda[3], p_bda[4], p_bda[5]);
632 if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
633 return(BTM_UNKNOWN_ADDR);
635 /* Finished if already in desired role */
636 if (p->link_role == new_role)
639 #if BTM_SCO_INCLUDED == TRUE
640 /* Check if there is any SCO Active on this BD Address */
641 is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
643 if (is_sco_active == TRUE)
644 return(BTM_NO_RESOURCES);
647 /* Ignore role switch request if the previous request was not completed */
648 if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
650 BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
651 p->switch_role_state);
655 if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
658 /* Wake up the link if in sniff or park before attempting switch */
659 if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
661 memset( (void*)&settings, 0, sizeof(settings));
662 settings.mode = BTM_PM_MD_ACTIVE;
663 status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
664 if (status != BTM_CMD_STARTED)
665 return(BTM_WRONG_MODE);
667 p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
669 /* some devices do not support switch while encryption is on */
672 p_dev_rec = btm_find_dev (remote_bd_addr);
673 if ((p_dev_rec != NULL)
674 && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
675 && !BTM_EPR_AVAILABLE(p))
677 /* bypass turning off encryption if change link key is already doing it */
678 if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
680 if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
681 return(BTM_NO_RESOURCES);
683 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
686 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
690 if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
691 return(BTM_NO_RESOURCES);
693 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
695 #if BTM_DISC_DURING_RS == TRUE
697 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
702 /* Initialize return structure in case request fails */
705 memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
707 btm_cb.devcb.switch_role_ref_data.role = new_role;
708 /* initialized to an error code */
709 btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
710 btm_cb.devcb.p_switch_role_cb = p_cb;
712 return(BTM_CMD_STARTED);
715 /*******************************************************************************
717 ** Function btm_acl_encrypt_change
719 ** Description This function is when encryption of the connection is
720 ** completed by the LM. Checks to see if a role switch or
721 ** change of link key was active and initiates or continues
722 ** process if needed.
726 *******************************************************************************/
727 void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
731 tBTM_SEC_DEV_REC *p_dev_rec;
732 tBTM_BL_ROLE_CHG_DATA evt;
734 BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
735 handle, status, encr_enable);
736 xx = btm_handle_to_acl_index(handle);
737 /* don't assume that we can never get a bad hci_handle */
738 if (xx < MAX_L2CAP_LINKS)
739 p = &btm_cb.acl_db[xx];
743 /* Process Role Switch if active */
744 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
746 /* if encryption turn off failed we still will try to switch role */
749 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
750 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
754 p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
755 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
758 if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role))
760 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
761 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
762 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
764 #if BTM_DISC_DURING_RS == TRUE
767 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
768 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
773 /* Finished enabling Encryption after role switch */
774 else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
776 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
777 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
778 btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
780 /* if role change event is registered, report it now */
781 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
783 evt.event = BTM_BL_ROLE_CHG_EVT;
784 evt.new_role = btm_cb.devcb.switch_role_ref_data.role;
785 evt.p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
786 evt.hci_status = btm_cb.devcb.switch_role_ref_data.hci_status;
787 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
789 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
790 evt.new_role, evt.hci_status, p->switch_role_state);
793 #if BTM_DISC_DURING_RS == TRUE
794 /* If a disconnect is pending, issue it now that role switch has completed */
795 if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
797 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
799 BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
800 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
802 BTM_TRACE_ERROR("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
803 PTR_TO_UINT(p_dev_rec), p_dev_rec->rs_disc_pending);
804 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
809 /*******************************************************************************
811 ** Function BTM_SetLinkPolicy
813 ** Description Create and send HCI "Write Policy Set" command
815 ** Returns status of the operation
817 *******************************************************************************/
818 tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
821 UINT8 *localFeatures = BTM_ReadLocalFeatures();
822 BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
823 /* BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
825 /* First, check if hold mode is supported */
826 if (*settings != HCI_DISABLE_ALL_LM_MODES)
828 if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
830 *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
831 BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
833 if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
835 *settings &= (~HCI_ENABLE_HOLD_MODE);
836 BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
838 if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
840 *settings &= (~HCI_ENABLE_SNIFF_MODE);
841 BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
843 if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
845 *settings &= (~HCI_ENABLE_PARK_MODE);
846 BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
850 if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
851 return(btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
853 /* If here, no BD Addr found */
854 return(BTM_UNKNOWN_ADDR);
857 /*******************************************************************************
859 ** Function BTM_SetDefaultLinkPolicy
861 ** Description Set the default value for HCI "Write Policy Set" command
862 ** to use when an ACL link is created.
866 *******************************************************************************/
867 void BTM_SetDefaultLinkPolicy (UINT16 settings)
869 UINT8 *localFeatures = BTM_ReadLocalFeatures();
871 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
873 if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
875 settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
876 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
878 if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
880 settings &= ~HCI_ENABLE_HOLD_MODE;
881 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
883 if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
885 settings &= ~HCI_ENABLE_SNIFF_MODE;
886 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
888 if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
890 settings &= ~HCI_ENABLE_PARK_MODE;
891 BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
893 BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
895 btm_cb.btm_def_link_policy = settings;
897 /* Set the default Link Policy of the controller */
898 btsnd_hcic_write_def_policy_set(settings);
902 void btm_use_preferred_conn_params(BD_ADDR bda) {
903 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
904 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
906 /* If there are any preferred connection parameters, set them now */
907 if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
908 (p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
909 (p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
910 (p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
911 (p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
912 (p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
913 (p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
914 ((p_lcb->min_interval < p_dev_rec->conn_params.min_conn_int &&
915 p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
916 (p_lcb->min_interval > p_dev_rec->conn_params.max_conn_int) ||
917 (p_lcb->latency > p_dev_rec->conn_params.slave_latency) ||
918 (p_lcb->timeout > p_dev_rec->conn_params.supervision_tout)))
920 BTM_TRACE_DEBUG ("%s: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d", __func__,
921 p_lcb->handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
922 p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
924 p_lcb->min_interval = p_dev_rec->conn_params.min_conn_int;
925 p_lcb->max_interval = p_dev_rec->conn_params.max_conn_int;
926 p_lcb->timeout = p_dev_rec->conn_params.supervision_tout;
927 p_lcb->latency = p_dev_rec->conn_params.slave_latency;
929 btsnd_hcic_ble_upd_ll_conn_params (p_lcb->handle,
930 p_dev_rec->conn_params.min_conn_int,
931 p_dev_rec->conn_params.max_conn_int,
932 p_dev_rec->conn_params.slave_latency,
933 p_dev_rec->conn_params.supervision_tout,
938 /*******************************************************************************
940 ** Function btm_read_remote_version_complete
942 ** Description This function is called when the command complete message
943 ** is received from the HCI for the remote version info.
947 *******************************************************************************/
948 void btm_read_remote_version_complete (UINT8 *p)
950 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
954 BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
956 STREAM_TO_UINT8 (status, p);
957 STREAM_TO_UINT16 (handle, p);
959 /* Look up the connection by handle and copy features */
960 for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
962 if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
964 if (status == HCI_SUCCESS)
966 STREAM_TO_UINT8 (p_acl_cb->lmp_version, p);
967 STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
968 STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
971 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
972 if (p_acl_cb->transport == BT_TRANSPORT_LE) {
973 l2cble_notify_le_connection (p_acl_cb->remote_addr);
974 btm_use_preferred_conn_params(p_acl_cb->remote_addr);
976 #endif // (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
983 /*******************************************************************************
985 ** Function btm_process_remote_ext_features
987 ** Description Local function called to process all extended features pages
988 ** read from a remote device.
992 *******************************************************************************/
993 void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
995 UINT16 handle = p_acl_cb->hci_handle;
996 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
999 BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
1001 /* Make sure we have the record to save remote features information */
1002 if (p_dev_rec == NULL)
1004 /* Get a new device; might be doing dedicated bonding */
1005 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
1008 p_acl_cb->num_read_pages = num_read_pages;
1009 p_dev_rec->num_read_pages = num_read_pages;
1011 /* Move the pages to placeholder */
1012 for (page_idx = 0; page_idx < num_read_pages; page_idx++)
1014 if (page_idx > HCI_EXT_FEATURES_PAGE_MAX)
1016 BTM_TRACE_ERROR("%s: page=%d unexpected", __FUNCTION__, page_idx);
1019 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1020 HCI_FEATURE_BYTES_PER_PAGE);
1023 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1025 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
1026 btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
1028 BTM_TRACE_API("%s: pend:%d", __FUNCTION__, req_pend);
1031 /* Request for remaining Security Features (if any) */
1032 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1037 /*******************************************************************************
1039 ** Function btm_read_remote_features
1041 ** Description Local function called to send a read remote supported features/
1042 ** remote extended features page[0].
1046 *******************************************************************************/
1047 void btm_read_remote_features (UINT16 handle)
1050 tACL_CONN *p_acl_cb;
1052 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
1054 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1056 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
1060 p_acl_cb = &btm_cb.acl_db[acl_idx];
1061 p_acl_cb->num_read_pages = 0;
1062 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1064 /* first send read remote supported features HCI command */
1065 /* because we don't know whether the remote support extended feature command */
1066 btsnd_hcic_rmt_features_req (handle);
1070 /*******************************************************************************
1072 ** Function btm_read_remote_ext_features
1074 ** Description Local function called to send a read remote extended features
1078 *******************************************************************************/
1079 void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1081 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
1083 btsnd_hcic_rmt_ext_features(handle, page_number);
1087 /*******************************************************************************
1089 ** Function btm_read_remote_features_complete
1091 ** Description This function is called when the remote supported features
1092 ** complete event is received from the HCI.
1096 *******************************************************************************/
1097 void btm_read_remote_features_complete (UINT8 *p)
1099 tACL_CONN *p_acl_cb;
1104 BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
1105 STREAM_TO_UINT8 (status, p);
1107 if (status != HCI_SUCCESS)
1109 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
1113 STREAM_TO_UINT16 (handle, p);
1115 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1117 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
1121 p_acl_cb = &btm_cb.acl_db[acl_idx];
1123 /* Copy the received features page */
1124 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1125 HCI_FEATURE_BYTES_PER_PAGE);
1127 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1128 (controller_get_interface()->supports_reading_remote_extended_features()))
1130 /* if the remote controller has extended features and local controller supports
1131 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1132 ** with extended features page 1 */
1133 BTM_TRACE_DEBUG ("Start reading remote extended features");
1134 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1138 /* Remote controller has no extended features. Process remote controller supported features
1139 (features page HCI_EXT_FEATURES_PAGE_0). */
1140 btm_process_remote_ext_features (p_acl_cb, 1);
1142 /* Continue with HCI connection establishment */
1143 btm_establish_continue (p_acl_cb);
1146 /*******************************************************************************
1148 ** Function btm_read_remote_ext_features_complete
1150 ** Description This function is called when the remote extended features
1151 ** complete event is received from the HCI.
1155 *******************************************************************************/
1156 void btm_read_remote_ext_features_complete (UINT8 *p)
1158 tACL_CONN *p_acl_cb;
1159 UINT8 page_num, max_page;
1163 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
1166 STREAM_TO_UINT16 (handle, p);
1167 STREAM_TO_UINT8 (page_num, p);
1168 STREAM_TO_UINT8 (max_page, p);
1170 /* Validate parameters */
1171 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1173 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
1177 if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1179 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
1183 p_acl_cb = &btm_cb.acl_db[acl_idx];
1185 /* Copy the received features page */
1186 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1188 /* If there is the next remote features page and
1189 * we have space to keep this page data - read this page */
1190 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1193 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
1194 btm_read_remote_ext_features (handle, page_num);
1198 /* Reading of remote feature pages is complete */
1199 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
1201 /* Process the pages */
1202 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1204 /* Continue with HCI connection establishment */
1205 btm_establish_continue (p_acl_cb);
1208 /*******************************************************************************
1210 ** Function btm_read_remote_ext_features_failed
1212 ** Description This function is called when the remote extended features
1213 ** complete event returns a failed status.
1217 *******************************************************************************/
1218 void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
1220 tACL_CONN *p_acl_cb;
1223 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
1226 if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1228 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
1232 p_acl_cb = &btm_cb.acl_db[acl_idx];
1234 /* Process supported features only */
1235 btm_process_remote_ext_features (p_acl_cb, 1);
1237 /* Continue HCI connection establishment */
1238 btm_establish_continue (p_acl_cb);
1241 /*******************************************************************************
1243 ** Function btm_establish_continue
1245 ** Description This function is called when the command complete message
1246 ** is received from the HCI for the read local link policy request.
1250 *******************************************************************************/
1251 void btm_establish_continue (tACL_CONN *p_acl_cb)
1253 tBTM_BL_EVENT_DATA evt_data;
1254 BTM_TRACE_DEBUG ("btm_establish_continue");
1255 #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1256 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
1257 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
1260 /* For now there are a some devices that do not like sending */
1261 /* commands events and data at the same time. */
1262 /* Set the packet types to the default allowed by the device */
1263 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
1265 if (btm_cb.btm_def_link_policy)
1266 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1269 p_acl_cb->link_up_issued = TRUE;
1271 /* If anyone cares, tell him database changed */
1272 if (btm_cb.p_bl_changed_cb)
1274 evt_data.event = BTM_BL_CONN_EVT;
1275 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1276 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1277 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1278 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1279 #if BLE_INCLUDED == TRUE
1280 evt_data.conn.handle = p_acl_cb->hci_handle;
1281 evt_data.conn.transport = p_acl_cb->transport;
1284 (*btm_cb.p_bl_changed_cb)(&evt_data);
1286 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1290 /*******************************************************************************
1292 ** Function BTM_SetDefaultLinkSuperTout
1294 ** Description Set the default value for HCI "Write Link Supervision Timeout"
1295 ** command to use when an ACL link is created.
1299 *******************************************************************************/
1300 void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1302 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
1303 btm_cb.btm_def_link_super_tout = timeout;
1306 /*******************************************************************************
1308 ** Function BTM_GetLinkSuperTout
1310 ** Description Read the link supervision timeout value of the connection
1312 ** Returns status of the operation
1314 *******************************************************************************/
1315 tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1317 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1319 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
1320 if (p != (tACL_CONN *)NULL)
1322 *p_timeout = p->link_super_tout;
1323 return(BTM_SUCCESS);
1325 /* If here, no BD Addr found */
1326 return(BTM_UNKNOWN_ADDR);
1330 /*******************************************************************************
1332 ** Function BTM_SetLinkSuperTout
1334 ** Description Create and send HCI "Write Link Supervision Timeout" command
1336 ** Returns status of the operation
1338 *******************************************************************************/
1339 tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1341 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1343 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
1344 if (p != (tACL_CONN *)NULL)
1346 p->link_super_tout = timeout;
1348 /* Only send if current role is Master; 2.0 spec requires this */
1349 if (p->link_role == BTM_ROLE_MASTER)
1351 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1352 p->hci_handle, timeout))
1353 return(BTM_NO_RESOURCES);
1355 return(BTM_CMD_STARTED);
1358 return(BTM_SUCCESS);
1361 /* If here, no BD Addr found */
1362 return(BTM_UNKNOWN_ADDR);
1365 /*******************************************************************************
1367 ** Function BTM_IsAclConnectionUp
1369 ** Description This function is called to check if an ACL connection exists
1370 ** to a specific remote BD Address.
1372 ** Returns TRUE if connection is up, else FALSE.
1374 *******************************************************************************/
1375 BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1379 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
1380 remote_bda[0], remote_bda[1], remote_bda[2],
1381 remote_bda[3], remote_bda[4], remote_bda[5]);
1383 p = btm_bda_to_acl(remote_bda, transport);
1384 if (p != (tACL_CONN *)NULL)
1389 /* If here, no BD Addr found */
1393 /*******************************************************************************
1395 ** Function BTM_GetNumAclLinks
1397 ** Description This function is called to count the number of
1398 ** ACL links that are active.
1400 ** Returns UINT16 Number of active ACL links
1402 *******************************************************************************/
1403 UINT16 BTM_GetNumAclLinks (void)
1405 uint16_t num_acl = 0;
1407 for (uint16_t i = 0; i < MAX_L2CAP_LINKS; ++i)
1409 if (btm_cb.acl_db[i].in_use)
1416 /*******************************************************************************
1418 ** Function btm_get_acl_disc_reason_code
1420 ** Description This function is called to get the disconnection reason code
1421 ** returned by the HCI at disconnection complete event.
1423 ** Returns TRUE if connection is up, else FALSE.
1425 *******************************************************************************/
1426 UINT16 btm_get_acl_disc_reason_code (void)
1428 UINT8 res = btm_cb.acl_disc_reason;
1429 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
1434 /*******************************************************************************
1436 ** Function BTM_GetHCIConnHandle
1438 ** Description This function is called to get the handle for an ACL connection
1439 ** to a specific remote BD Address.
1441 ** Returns the handle of the connection, or 0xFFFF if none.
1443 *******************************************************************************/
1444 UINT16 BTM_GetHCIConnHandle (const BD_ADDR remote_bda, tBT_TRANSPORT transport)
1447 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
1448 p = btm_bda_to_acl(remote_bda, transport);
1449 if (p != (tACL_CONN *)NULL)
1451 return(p->hci_handle);
1454 /* If here, no BD Addr found */
1458 /*******************************************************************************
1460 ** Function btm_process_clk_off_comp_evt
1462 ** Description This function is called when clock offset command completes.
1464 ** Input Parms hci_handle - connection handle associated with the change
1469 *******************************************************************************/
1470 void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
1473 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
1474 /* Look up the connection by handle and set the current mode */
1475 if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
1476 btm_cb.acl_db[xx].clock_offset = clock_offset;
1479 /*******************************************************************************
1481 ** Function btm_acl_role_changed
1483 ** Description This function is called whan a link's master/slave role change
1484 ** event or command status event (with error) is received.
1485 ** It updates the link control block, and calls
1486 ** the registered callback with status and role (if registered).
1490 *******************************************************************************/
1491 void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
1493 UINT8 *p_bda = (bd_addr) ? bd_addr :
1494 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
1495 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
1496 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
1497 tBTM_SEC_DEV_REC *p_dev_rec;
1498 tBTM_BL_ROLE_CHG_DATA evt;
1500 BTM_TRACE_DEBUG ("btm_acl_role_changed");
1501 /* Ignore any stray events */
1504 /* it could be a failure */
1505 if (hci_status != HCI_SUCCESS)
1506 btm_acl_report_role_change(hci_status, bd_addr);
1510 p_data->hci_status = hci_status;
1512 if (hci_status == HCI_SUCCESS)
1514 p_data->role = new_role;
1515 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
1517 /* Update cached value */
1518 p->link_role = new_role;
1520 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
1521 if (new_role == BTM_ROLE_MASTER)
1523 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
1528 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
1529 new_role = p->link_role;
1532 /* Check if any SCO req is pending for role change */
1533 btm_sco_chk_pend_rolechange (p->hci_handle);
1535 /* if switching state is switching we need to turn encryption on */
1536 /* if idle, we did not change encryption */
1537 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
1539 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
1541 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
1542 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
1547 /* Set the switch_role_state to IDLE since the reply received from HCI */
1548 /* regardless of its result either success or failed. */
1549 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
1551 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1552 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1555 /* if role switch complete is needed, report it now */
1556 btm_acl_report_role_change(hci_status, bd_addr);
1558 /* if role change event is registered, report it now */
1559 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
1561 evt.event = BTM_BL_ROLE_CHG_EVT;
1562 evt.new_role = new_role;
1564 evt.hci_status = hci_status;
1565 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1568 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
1569 p_data->role, p_data->hci_status, p->switch_role_state);
1571 #if BTM_DISC_DURING_RS == TRUE
1572 /* If a disconnect is pending, issue it now that role switch has completed */
1573 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
1575 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1577 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
1578 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1580 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
1581 PTR_TO_UINT(p_dev_rec), p_dev_rec->rs_disc_pending);
1582 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1589 /*******************************************************************************
1591 ** Function BTM_AllocateSCN
1593 ** Description Look through the Server Channel Numbers for a free one.
1595 ** Returns Allocated SCN number or 0 if none.
1597 *******************************************************************************/
1599 UINT8 BTM_AllocateSCN(void)
1602 BTM_TRACE_DEBUG ("BTM_AllocateSCN");
1604 // stack reserves scn 1 for HFP, HSP we still do the correct way
1605 for (x = 1; x < BTM_MAX_SCN; x++)
1607 if (!btm_cb.btm_scn[x])
1609 btm_cb.btm_scn[x] = TRUE;
1614 return(0); /* No free ports */
1617 /*******************************************************************************
1619 ** Function BTM_TryAllocateSCN
1621 ** Description Try to allocate a fixed server channel
1623 ** Returns Returns TRUE if server channel was available
1625 *******************************************************************************/
1627 BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
1629 /* Make sure we don't exceed max port range.
1630 * Stack reserves scn 1 for HFP, HSP we still do the correct way.
1632 if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
1635 /* check if this port is available */
1636 if (!btm_cb.btm_scn[scn-1])
1638 btm_cb.btm_scn[scn-1] = TRUE;
1642 return (FALSE); /* Port was busy */
1645 /*******************************************************************************
1647 ** Function BTM_FreeSCN
1649 ** Description Free the specified SCN.
1651 ** Returns TRUE or FALSE
1653 *******************************************************************************/
1654 BOOLEAN BTM_FreeSCN(UINT8 scn)
1656 BTM_TRACE_DEBUG ("BTM_FreeSCN ");
1657 if (scn <= BTM_MAX_SCN)
1659 btm_cb.btm_scn[scn-1] = FALSE;
1663 return(FALSE); /* Illegal SCN passed in */
1666 /*******************************************************************************
1668 ** Function btm_set_packet_types
1670 ** Description This function sets the packet types used for a specific
1671 ** ACL connection. It is called internally by btm_acl_created
1672 ** or by an application/profile by BTM_SetPacketTypes.
1674 ** Returns status of the operation
1676 *******************************************************************************/
1677 tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
1679 UINT16 temp_pkt_types;
1680 BTM_TRACE_DEBUG ("btm_set_packet_types");
1681 /* Save in the ACL control blocks, types that we support */
1682 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
1683 btm_cb.btm_acl_pkt_types_supported);
1685 /* OR in any exception packet types if at least 2.0 version of spec */
1686 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
1687 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
1689 /* Exclude packet types not supported by the peer */
1690 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
1692 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
1694 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
1696 return(BTM_NO_RESOURCES);
1699 p->pkt_types_mask = temp_pkt_types;
1701 return(BTM_CMD_STARTED);
1704 /*******************************************************************************
1706 ** Function btm_get_max_packet_size
1708 ** Returns Returns maximum packet size that can be used for current
1709 ** connection, 0 if connection is not established
1711 *******************************************************************************/
1712 UINT16 btm_get_max_packet_size (BD_ADDR addr)
1714 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1715 UINT16 pkt_types = 0;
1716 UINT16 pkt_size = 0;
1717 BTM_TRACE_DEBUG ("btm_get_max_packet_size");
1720 pkt_types = p->pkt_types_mask;
1724 /* Special case for when info for the local device is requested */
1725 if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0)
1727 pkt_types = btm_cb.btm_acl_pkt_types_supported;
1733 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5))
1734 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
1735 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5))
1736 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
1737 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3))
1738 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
1739 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5)
1740 pkt_size = HCI_DH5_PACKET_SIZE;
1741 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3))
1742 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
1743 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5)
1744 pkt_size = HCI_DM5_PACKET_SIZE;
1745 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3)
1746 pkt_size = HCI_DH3_PACKET_SIZE;
1747 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3)
1748 pkt_size = HCI_DM3_PACKET_SIZE;
1749 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1))
1750 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
1751 else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1))
1752 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
1753 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1)
1754 pkt_size = HCI_DH1_PACKET_SIZE;
1755 else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1)
1756 pkt_size = HCI_DM1_PACKET_SIZE;
1762 /*******************************************************************************
1764 ** Function BTM_ReadRemoteVersion
1766 ** Returns If connected report peer device info
1768 *******************************************************************************/
1769 tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
1770 UINT16 *manufacturer, UINT16 *lmp_sub_version)
1772 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1773 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
1775 return(BTM_UNKNOWN_ADDR);
1778 *lmp_version = p->lmp_version;
1781 *manufacturer = p->manufacturer;
1783 if (lmp_sub_version)
1784 *lmp_sub_version = p->lmp_subversion;
1786 return(BTM_SUCCESS);
1789 /*******************************************************************************
1791 ** Function BTM_ReadRemoteFeatures
1793 ** Returns pointer to the remote supported features mask (8 bytes)
1795 *******************************************************************************/
1796 UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
1798 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1799 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
1805 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1808 /*******************************************************************************
1810 ** Function BTM_ReadRemoteExtendedFeatures
1812 ** Returns pointer to the remote extended features mask (8 bytes)
1813 ** or NULL if bad page
1815 *******************************************************************************/
1816 UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
1818 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1819 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
1825 if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
1827 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
1831 return(p->peer_lmp_features[page_number]);
1834 /*******************************************************************************
1836 ** Function BTM_ReadNumberRemoteFeaturesPages
1838 ** Returns number of features pages read from the remote device.
1840 *******************************************************************************/
1841 UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
1843 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1844 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
1850 return(p->num_read_pages);
1853 /*******************************************************************************
1855 ** Function BTM_ReadAllRemoteFeatures
1857 ** Returns pointer to all features of the remote (24 bytes).
1859 *******************************************************************************/
1860 UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
1862 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1863 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
1869 return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1872 /*******************************************************************************
1874 ** Function BTM_RegBusyLevelNotif
1876 ** Description This function is called to register a callback to receive
1877 ** busy level change events.
1879 ** Returns BTM_SUCCESS if successfully registered, otherwise error
1881 *******************************************************************************/
1882 tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
1883 tBTM_BL_EVENT_MASK evt_mask)
1885 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
1887 *p_level = btm_cb.busy_level;
1889 btm_cb.bl_evt_mask = evt_mask;
1892 btm_cb.p_bl_changed_cb = NULL;
1893 else if (btm_cb.p_bl_changed_cb)
1896 btm_cb.p_bl_changed_cb = p_cb;
1898 return(BTM_SUCCESS);
1901 /*******************************************************************************
1903 ** Function BTM_SetQoS
1905 ** Description This function is called to setup QoS
1907 ** Returns status of the operation
1909 *******************************************************************************/
1910 tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
1912 tACL_CONN *p = &btm_cb.acl_db[0];
1914 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
1915 bd[0], bd[1], bd[2],
1916 bd[3], bd[4], bd[5]);
1918 /* If someone already waiting on the version, do not allow another */
1919 if (btm_cb.devcb.p_qos_setup_cmpl_cb)
1922 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
1924 btm_cb.devcb.p_qos_setup_cmpl_cb = p_cb;
1925 alarm_set_on_queue(btm_cb.devcb.qos_setup_timer,
1926 BTM_DEV_REPLY_TIMEOUT_MS,
1927 btm_qos_setup_timeout, NULL,
1928 btu_general_alarm_queue);
1930 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
1931 p_flow->token_rate, p_flow->peak_bandwidth,
1932 p_flow->latency,p_flow->delay_variation))
1934 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
1935 alarm_cancel(btm_cb.devcb.qos_setup_timer);
1936 return(BTM_NO_RESOURCES);
1939 return(BTM_CMD_STARTED);
1942 /* If here, no BD Addr found */
1943 return(BTM_UNKNOWN_ADDR);
1946 /*******************************************************************************
1948 ** Function btm_qos_setup_timeout
1950 ** Description Callback when QoS setup times out.
1954 *******************************************************************************/
1955 void btm_qos_setup_timeout(UNUSED_ATTR void *data)
1957 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qos_setup_cmpl_cb;
1958 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
1960 (*p_cb)((void *) NULL);
1963 /*******************************************************************************
1965 ** Function btm_qos_setup_complete
1967 ** Description This function is called when the command complete message
1968 ** is received from the HCI for the qos setup request.
1972 *******************************************************************************/
1973 void btm_qos_setup_complete(UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
1975 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qos_setup_cmpl_cb;
1976 tBTM_QOS_SETUP_CMPL qossu;
1978 BTM_TRACE_DEBUG("%s", __func__);
1979 alarm_cancel(btm_cb.devcb.qos_setup_timer);
1980 btm_cb.devcb.p_qos_setup_cmpl_cb = NULL;
1982 /* If there was a registered callback, call it */
1985 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
1986 qossu.status = status;
1987 qossu.handle = handle;
1990 qossu.flow.qos_flags = p_flow->qos_flags;
1991 qossu.flow.service_type = p_flow->service_type;
1992 qossu.flow.token_rate = p_flow->token_rate;
1993 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
1994 qossu.flow.latency = p_flow->latency;
1995 qossu.flow.delay_variation = p_flow->delay_variation;
1997 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
1998 qossu.flow.delay_variation);
2003 /*******************************************************************************
2005 ** Function BTM_ReadRSSI
2007 ** Description This function is called to read the link policy settings.
2008 ** The address of link policy results are returned in the callback.
2009 ** (tBTM_RSSI_RESULTS)
2011 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2013 *******************************************************************************/
2014 tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2017 tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
2018 #if BLE_INCLUDED == TRUE
2019 tBT_DEVICE_TYPE dev_type;
2020 tBLE_ADDR_TYPE addr_type;
2022 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2023 remote_bda[0], remote_bda[1], remote_bda[2],
2024 remote_bda[3], remote_bda[4], remote_bda[5]);
2026 /* If someone already waiting on the version, do not allow another */
2027 if (btm_cb.devcb.p_rssi_cmpl_cb)
2030 #if BLE_INCLUDED == TRUE
2031 BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
2032 if (dev_type == BT_DEVICE_TYPE_BLE)
2033 transport = BT_TRANSPORT_LE;
2036 p = btm_bda_to_acl(remote_bda, transport);
2037 if (p != (tACL_CONN *)NULL)
2039 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
2040 alarm_set_on_queue(btm_cb.devcb.read_rssi_timer,
2041 BTM_DEV_REPLY_TIMEOUT_MS, btm_read_rssi_timeout,
2042 NULL, btu_general_alarm_queue);
2044 if (!btsnd_hcic_read_rssi (p->hci_handle))
2046 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2047 alarm_cancel(btm_cb.devcb.read_rssi_timer);
2048 return(BTM_NO_RESOURCES);
2051 return(BTM_CMD_STARTED);
2054 /* If here, no BD Addr found */
2055 return(BTM_UNKNOWN_ADDR);
2058 /*******************************************************************************
2060 ** Function BTM_ReadLinkQuality
2062 ** Description This function is called to read the link qulaity.
2063 ** The value of the link quality is returned in the callback.
2064 ** (tBTM_LINK_QUALITY_RESULTS)
2066 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2068 *******************************************************************************/
2069 tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2073 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2074 remote_bda[0], remote_bda[1], remote_bda[2],
2075 remote_bda[3], remote_bda[4], remote_bda[5]);
2077 /* If someone already waiting on the version, do not allow another */
2078 if (btm_cb.devcb.p_link_qual_cmpl_cb)
2081 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2082 if (p != (tACL_CONN *)NULL)
2084 btm_cb.devcb.p_link_qual_cmpl_cb = p_cb;
2085 alarm_set_on_queue(btm_cb.devcb.read_link_quality_timer,
2086 BTM_DEV_REPLY_TIMEOUT_MS,
2087 btm_read_link_quality_timeout, NULL,
2088 btu_general_alarm_queue);
2090 if (!btsnd_hcic_get_link_quality (p->hci_handle))
2092 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
2093 alarm_cancel(btm_cb.devcb.read_link_quality_timer);
2094 return(BTM_NO_RESOURCES);
2097 return(BTM_CMD_STARTED);
2100 /* If here, no BD Addr found */
2101 return(BTM_UNKNOWN_ADDR);
2104 /*******************************************************************************
2106 ** Function BTM_ReadTxPower
2108 ** Description This function is called to read the current
2109 ** TX power of the connection. The tx power level results
2110 ** are returned in the callback.
2111 ** (tBTM_RSSI_RESULTS)
2113 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2115 *******************************************************************************/
2116 tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
2120 #define BTM_READ_RSSI_TYPE_CUR 0x00
2121 #define BTM_READ_RSSI_TYPE_MAX 0X01
2123 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2124 remote_bda[0], remote_bda[1], remote_bda[2],
2125 remote_bda[3], remote_bda[4], remote_bda[5]);
2127 /* If someone already waiting on the version, do not allow another */
2128 if (btm_cb.devcb.p_tx_power_cmpl_cb)
2131 p = btm_bda_to_acl(remote_bda, transport);
2132 if (p != (tACL_CONN *)NULL)
2134 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2135 alarm_set_on_queue(btm_cb.devcb.read_tx_power_timer,
2136 BTM_DEV_REPLY_TIMEOUT_MS,
2137 btm_read_tx_power_timeout, NULL,
2138 btu_general_alarm_queue);
2140 #if BLE_INCLUDED == TRUE
2141 if (p->transport == BT_TRANSPORT_LE)
2143 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2144 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2149 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2153 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2154 alarm_cancel(btm_cb.devcb.read_tx_power_timer);
2155 return(BTM_NO_RESOURCES);
2158 return(BTM_CMD_STARTED);
2161 /* If here, no BD Addr found */
2162 return (BTM_UNKNOWN_ADDR);
2165 /*******************************************************************************
2167 ** Function btm_read_tx_power_timeout
2169 ** Description Callback when reading the tx power times out.
2173 *******************************************************************************/
2174 void btm_read_tx_power_timeout(UNUSED_ATTR void *data)
2176 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2177 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2179 (*p_cb)((void *) NULL);
2182 /*******************************************************************************
2184 ** Function btm_read_tx_power_complete
2186 ** Description This function is called when the command complete message
2187 ** is received from the HCI for the read tx power request.
2191 *******************************************************************************/
2192 void btm_read_tx_power_complete(UINT8 *p, BOOLEAN is_ble)
2194 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2195 tBTM_TX_POWER_RESULTS results;
2197 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2200 BTM_TRACE_DEBUG("%s", __func__);
2201 alarm_cancel(btm_cb.devcb.read_tx_power_timer);
2202 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2204 /* If there was a registered callback, call it */
2207 STREAM_TO_UINT8 (results.hci_status, p);
2209 if (results.hci_status == HCI_SUCCESS)
2211 results.status = BTM_SUCCESS;
2215 STREAM_TO_UINT16 (handle, p);
2216 STREAM_TO_UINT8 (results.tx_power, p);
2218 /* Search through the list of active channels for the correct BD Addr */
2219 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2221 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2223 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2228 #if BLE_INCLUDED == TRUE
2231 STREAM_TO_UINT8 (results.tx_power, p);
2232 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
2235 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
2236 results.tx_power, results.hci_status);
2239 results.status = BTM_ERR_PROCESSING;
2245 /*******************************************************************************
2247 ** Function btm_read_rssi_timeout
2249 ** Description Callback when reading the RSSI times out.
2253 *******************************************************************************/
2254 void btm_read_rssi_timeout(UNUSED_ATTR void *data)
2256 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2257 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2259 (*p_cb)((void *) NULL);
2262 /*******************************************************************************
2264 ** Function btm_read_rssi_complete
2266 ** Description This function is called when the command complete message
2267 ** is received from the HCI for the read rssi request.
2271 *******************************************************************************/
2272 void btm_read_rssi_complete (UINT8 *p)
2274 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2275 tBTM_RSSI_RESULTS results;
2277 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2280 BTM_TRACE_DEBUG("%s", __func__);
2281 alarm_cancel(btm_cb.devcb.read_rssi_timer);
2282 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2284 /* If there was a registered callback, call it */
2287 STREAM_TO_UINT8 (results.hci_status, p);
2289 if (results.hci_status == HCI_SUCCESS)
2291 results.status = BTM_SUCCESS;
2293 STREAM_TO_UINT16 (handle, p);
2295 STREAM_TO_UINT8 (results.rssi, p);
2296 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
2297 results.rssi, results.hci_status);
2299 /* Search through the list of active channels for the correct BD Addr */
2300 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2302 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2304 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2310 results.status = BTM_ERR_PROCESSING;
2316 /*******************************************************************************
2318 ** Function btm_read_link_quality_timeout
2320 ** Description Callback when reading the link quality times out.
2324 *******************************************************************************/
2325 void btm_read_link_quality_timeout(UNUSED_ATTR void *data)
2327 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
2328 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
2330 (*p_cb)((void *) NULL);
2333 /*******************************************************************************
2335 ** Function btm_read_link_quality_complete
2337 ** Description This function is called when the command complete message
2338 ** is received from the HCI for the read link quality.
2342 *******************************************************************************/
2343 void btm_read_link_quality_complete(UINT8 *p)
2345 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
2346 tBTM_LINK_QUALITY_RESULTS results;
2348 tACL_CONN *p_acl_cb = &btm_cb.acl_db[0];
2351 BTM_TRACE_DEBUG("%s", __func__);
2352 alarm_cancel(btm_cb.devcb.read_link_quality_timer);
2353 btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
2355 /* If there was a registered callback, call it */
2358 STREAM_TO_UINT8 (results.hci_status, p);
2360 if (results.hci_status == HCI_SUCCESS)
2362 results.status = BTM_SUCCESS;
2364 STREAM_TO_UINT16 (handle, p);
2366 STREAM_TO_UINT8 (results.link_quality, p);
2367 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
2368 results.link_quality, results.hci_status);
2370 /* Search through the list of active channels for the correct BD Addr */
2371 for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
2373 if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
2375 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2381 results.status = BTM_ERR_PROCESSING;
2387 /*******************************************************************************
2389 ** Function btm_remove_acl
2391 ** Description This function is called to disconnect an ACL connection
2393 ** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
2395 *******************************************************************************/
2396 tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
2398 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
2399 tBTM_STATUS status = BTM_SUCCESS;
2401 BTM_TRACE_DEBUG ("btm_remove_acl");
2402 #if BTM_DISC_DURING_RS == TRUE
2403 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
2405 /* Role Switch is pending, postpone until completed */
2406 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
2408 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
2410 else /* otherwise can disconnect right away */
2413 if (hci_handle != 0xFFFF && p_dev_rec &&
2414 p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
2416 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
2417 status = BTM_NO_RESOURCES;
2420 status = BTM_UNKNOWN_ADDR;
2427 /*******************************************************************************
2429 ** Function BTM_SetTraceLevel
2431 ** Description This function sets the trace level for BTM. If called with
2432 ** a value of 0xFF, it simply returns the current trace level.
2434 ** Returns The new or current trace level
2436 *******************************************************************************/
2437 UINT8 BTM_SetTraceLevel (UINT8 new_level)
2439 BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
2440 if (new_level != 0xFF)
2441 btm_cb.trace_level = new_level;
2443 return(btm_cb.trace_level);
2446 /*******************************************************************************
2448 ** Function btm_cont_rswitch
2450 ** Description This function is called to continue processing an active
2451 ** role switch. It first disables encryption if enabled and
2452 ** EPR is not supported
2456 *******************************************************************************/
2457 void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
2460 BOOLEAN sw_ok = TRUE;
2461 BTM_TRACE_DEBUG ("btm_cont_rswitch");
2462 /* Check to see if encryption needs to be turned off if pending
2463 change of link key or role switch */
2464 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2466 /* Must turn off Encryption first if necessary */
2467 /* Some devices do not support switch or change of link key while encryption is on */
2468 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
2469 && !BTM_EPR_AVAILABLE(p))
2471 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
2473 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
2474 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2475 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
2479 /* Error occurred; set states back to Idle */
2480 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2484 else /* Encryption not used or EPR supported, continue with switch
2485 and/or change of link key */
2487 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
2489 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
2490 #if BTM_DISC_DURING_RS == TRUE
2492 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
2494 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
2500 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2501 btm_acl_report_role_change(hci_status, p->remote_addr);
2506 /*******************************************************************************
2508 ** Function btm_acl_resubmit_page
2510 ** Description send pending page request
2512 *******************************************************************************/
2513 void btm_acl_resubmit_page (void)
2515 tBTM_SEC_DEV_REC *p_dev_rec;
2519 BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
2520 /* If there were other page request schedule can start the next one */
2521 if ((p_buf = (BT_HDR *)fixed_queue_try_dequeue(btm_cb.page_queue)) != NULL)
2523 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
2524 * for both create_conn and rmt_name */
2525 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
2527 STREAM_TO_BDADDR (bda, pp);
2529 p_dev_rec = btm_find_or_alloc_dev (bda);
2531 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2532 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2534 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
2537 btm_cb.paging = FALSE;
2540 /*******************************************************************************
2542 ** Function btm_acl_reset_paging
2544 ** Description set paging to FALSE and free the page queue - called at hci_reset
2546 *******************************************************************************/
2547 void btm_acl_reset_paging (void)
2550 BTM_TRACE_DEBUG ("btm_acl_reset_paging");
2551 /* If we sent reset we are definitely not paging any more */
2552 while ((p = (BT_HDR *)fixed_queue_try_dequeue(btm_cb.page_queue)) != NULL)
2555 btm_cb.paging = FALSE;
2558 /*******************************************************************************
2560 ** Function btm_acl_paging
2562 ** Description send a paging command or queue it in btm_cb
2564 *******************************************************************************/
2565 void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
2567 tBTM_SEC_DEV_REC *p_dev_rec;
2569 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
2570 btm_cb.discing, btm_cb.paging,
2571 (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
2574 btm_cb.paging = TRUE;
2575 fixed_queue_enqueue(btm_cb.page_queue, p);
2579 if (!BTM_ACL_IS_CONNECTED (bda))
2581 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
2582 (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
2583 btm_cb.connecting_bda[2],
2584 (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
2585 btm_cb.connecting_bda[5]);
2586 if (btm_cb.paging &&
2587 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
2589 fixed_queue_enqueue(btm_cb.page_queue, p);
2593 p_dev_rec = btm_find_or_alloc_dev (bda);
2594 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2595 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2597 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2600 btm_cb.paging = TRUE;
2602 else /* ACL is already up */
2604 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2609 /*******************************************************************************
2611 ** Function btm_acl_notif_conn_collision
2613 ** Description Send connection collision event to upper layer if registered
2615 ** Returns TRUE if sent out to upper layer,
2616 ** FALSE if no one needs the notification.
2618 *******************************************************************************/
2619 BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
2621 tBTM_BL_EVENT_DATA evt_data;
2623 /* Report possible collision to the upper layer. */
2624 if (btm_cb.p_bl_changed_cb)
2626 BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2627 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2629 evt_data.event = BTM_BL_COLLISION_EVT;
2630 evt_data.conn.p_bda = bda;
2632 #if BLE_INCLUDED == TRUE
2633 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
2634 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
2636 (*btm_cb.p_bl_changed_cb)(&evt_data);
2644 /*******************************************************************************
2646 ** Function btm_acl_chk_peer_pkt_type_support
2648 ** Description Check if peer supports requested packets
2650 *******************************************************************************/
2651 void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
2653 /* 3 and 5 slot packets? */
2654 if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2655 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
2657 if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2658 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
2660 /* 2 and 3 MPS support? */
2661 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2662 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
2663 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
2664 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
2666 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2667 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
2668 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
2669 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2671 /* EDR 3 and 5 slot support? */
2672 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
2673 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2675 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2676 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
2677 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
2679 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2680 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
2681 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);