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