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