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 *  This file contains functions that handle ACL connections. This includes
22 *  operations such as hold and sniff modes, supported packet types.
23 *
24 ******************************************************************************/
25
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <stddef.h>
30
31#include "bt_types.h"
32#include "bt_target.h"
33#include "gki.h"
34#include "hcimsgs.h"
35#include "btu.h"
36#include "btm_api.h"
37#include "btm_int.h"
38#include "l2c_int.h"
39#include "hcidefs.h"
40#include "bd.h"
41#include "bt_utils.h"
42
43static void btm_read_remote_features (UINT16 handle);
44static void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number);
45static void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
46                                                  UINT8 page_idx);
47static void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages);
48
49#define BTM_DEV_REPLY_TIMEOUT   3       /* 3 second timeout waiting for responses */
50
51/*******************************************************************************
52**
53** Function         btm_acl_init
54**
55** Description      This function is called at BTM startup to initialize
56**
57** Returns          void
58**
59*******************************************************************************/
60void btm_acl_init (void)
61{
62    BTM_TRACE_DEBUG ("btm_acl_init");
63#if 0  /* cleared in btm_init; put back in if called from anywhere else! */
64    memset (&btm_cb.acl_db, 0, sizeof (btm_cb.acl_db));
65#if RFCOMM_INCLUDED == TRUE
66    memset (btm_cb.btm_scn, 0, BTM_MAX_SCN);          /* Initialize the SCN usage to FALSE */
67#endif
68    btm_cb.btm_def_link_policy     = 0;
69#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
70    btm_cb.p_bl_changed_cb         = NULL;
71#else
72    btm_cb.p_acl_changed_cb        = NULL;
73#endif
74#endif
75
76    /* Initialize nonzero defaults */
77    btm_cb.btm_def_link_super_tout = HCI_DEFAULT_INACT_TOUT;
78    btm_cb.acl_disc_reason         = 0xff ;
79}
80
81/*******************************************************************************
82**
83** Function         btm_bda_to_acl
84**
85** Description      This function returns the FIRST acl_db entry for the passed BDA.
86**
87** Parameters      bda : BD address of the remote device
88**                 transport : Physical transport used for ACL connection (BR/EDR or LE)
89**
90** Returns          Returns pointer to the ACL DB for the requested BDA if found.
91**                  NULL if not found.
92**
93*******************************************************************************/
94tACL_CONN *btm_bda_to_acl (BD_ADDR bda, tBT_TRANSPORT transport)
95{
96    tACL_CONN   *p = &btm_cb.acl_db[0];
97    UINT16       xx;
98    if (bda)
99    {
100        for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
101        {
102            if ((p->in_use) && (!memcmp (p->remote_addr, bda, BD_ADDR_LEN))
103#if BLE_INCLUDED == TRUE
104                && p->transport == transport
105#endif
106                )
107            {
108                BTM_TRACE_DEBUG ("btm_bda_to_acl found");
109                return(p);
110            }
111        }
112    }
113
114    /* If here, no BD Addr found */
115    return((tACL_CONN *)NULL);
116}
117
118/*******************************************************************************
119**
120** Function         btm_handle_to_acl_index
121**
122** Description      This function returns the FIRST acl_db entry for the passed hci_handle.
123**
124** Returns          index to the acl_db or MAX_L2CAP_LINKS.
125**
126*******************************************************************************/
127UINT8 btm_handle_to_acl_index (UINT16 hci_handle)
128{
129    tACL_CONN   *p = &btm_cb.acl_db[0];
130    UINT8       xx;
131    BTM_TRACE_DEBUG ("btm_handle_to_acl_index");
132    for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
133    {
134        if ((p->in_use) && (p->hci_handle == hci_handle))
135        {
136            break;
137        }
138    }
139
140    /* If here, no BD Addr found */
141    return(xx);
142}
143
144#if BLE_PRIVACY_SPT == TRUE
145/*******************************************************************************
146**
147** Function         btm_ble_get_acl_remote_addr
148**
149** Description      This function reads the active remote address used for the
150**                  connection.
151**
152** Returns          success return TRUE, otherwise FALSE.
153**
154*******************************************************************************/
155BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
156                                    tBLE_ADDR_TYPE *p_addr_type)
157{
158#if BLE_INCLUDED == TRUE
159    BOOLEAN         st = TRUE;
160
161    if (p_dev_rec == NULL)
162    {
163        BTM_TRACE_ERROR("btm_ble_get_acl_remote_addr can not find device with matching address");
164        return FALSE;
165    }
166
167    switch (p_dev_rec->ble.active_addr_type)
168    {
169    case BTM_BLE_ADDR_PSEUDO:
170        memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
171        * p_addr_type = p_dev_rec->ble.ble_addr_type;
172        break;
173
174    case BTM_BLE_ADDR_RRA:
175        memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
176        * p_addr_type = BLE_ADDR_RANDOM;
177        break;
178
179    case BTM_BLE_ADDR_STATIC:
180        memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
181        * p_addr_type = p_dev_rec->ble.static_addr_type;
182        break;
183
184    default:
185        BTM_TRACE_ERROR("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
186        st = FALSE;
187        break;
188    }
189
190    return st;
191#else
192    UNUSED(p_dev_rec);
193    UNUSED(conn_addr);
194    UNUSED(p_addr_type);
195    return FALSE;
196#endif
197}
198#endif
199/*******************************************************************************
200**
201** Function         btm_acl_created
202**
203** Description      This function is called by L2CAP when an ACL connection
204**                  is created.
205**
206** Returns          void
207**
208*******************************************************************************/
209void btm_acl_created (BD_ADDR bda, DEV_CLASS dc, BD_NAME bdn,
210                      UINT16 hci_handle, UINT8 link_role, tBT_TRANSPORT transport)
211{
212    tBTM_SEC_DEV_REC *p_dev_rec = NULL;
213    UINT8             yy;
214    tACL_CONN        *p;
215    UINT8             xx;
216
217    BTM_TRACE_DEBUG ("btm_acl_created hci_handle=%d link_role=%d  transport=%d",
218                      hci_handle,link_role, transport);
219    /* Ensure we don't have duplicates */
220    p = btm_bda_to_acl(bda, transport);
221    if (p != (tACL_CONN *)NULL)
222    {
223        p->hci_handle = hci_handle;
224        p->link_role  = link_role;
225#if BLE_INCLUDED == TRUE
226        p->transport = transport;
227#endif
228        BTM_TRACE_DEBUG ("Duplicate btm_acl_created: RemBdAddr: %02x%02x%02x%02x%02x%02x",
229                          bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
230        BTM_SetLinkPolicy(p->remote_addr, &btm_cb.btm_def_link_policy);
231        return;
232    }
233
234    /* Allocate acl_db entry */
235    for (xx = 0, p = &btm_cb.acl_db[0]; xx < MAX_L2CAP_LINKS; xx++, p++)
236    {
237        if (!p->in_use)
238        {
239            p->in_use            = TRUE;
240            p->hci_handle        = hci_handle;
241            p->link_role         = link_role;
242            p->link_up_issued    = FALSE;
243
244#if BLE_INCLUDED == TRUE
245            p->transport = transport;
246            if (transport == BT_TRANSPORT_LE)
247            {
248#if BLE_PRIVACY_SPT == TRUE
249                if (btm_cb.ble_ctr_cb.privacy)
250                {
251                    p->conn_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
252                    memcpy(p->conn_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, BD_ADDR_LEN);
253                }
254                else
255#endif
256                {
257                    p->conn_addr_type = BLE_ADDR_PUBLIC;
258                    BTM_GetLocalDeviceAddr(p->conn_addr);
259                }
260            }
261#endif
262            p->restore_pkt_types = 0;   /* Only exists while SCO is active */
263            p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
264
265#if BTM_PWR_MGR_INCLUDED == FALSE
266            p->mode             = BTM_ACL_MODE_NORMAL;
267#else
268            btm_pm_sm_alloc(xx);
269#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
270
271            memcpy (p->remote_addr, bda, BD_ADDR_LEN);
272
273            if (dc)
274                memcpy (p->remote_dc, dc, DEV_CLASS_LEN);
275
276            if (bdn)
277                memcpy (p->remote_name, bdn, BTM_MAX_REM_BD_NAME_LEN);
278
279            /* if BR/EDR do something more */
280            if (transport == BT_TRANSPORT_BR_EDR)
281            {
282                btsnd_hcic_read_rmt_clk_offset (p->hci_handle);
283                btsnd_hcic_rmt_ver_req (p->hci_handle);
284            }
285            p_dev_rec = btm_find_dev_by_handle (hci_handle);
286
287#if (BLE_INCLUDED == TRUE)
288            if (p_dev_rec )
289            {
290                BTM_TRACE_DEBUG ("device_type=0x%x", p_dev_rec->device_type);
291            }
292#endif
293
294            if (p_dev_rec && !(transport == BT_TRANSPORT_LE))
295            {
296                /* If remote features already known, copy them and continue connection setup */
297                if ((p_dev_rec->num_read_pages) &&
298                    (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1)) /* sanity check */)
299                {
300                    memcpy (p->peer_lmp_features, p_dev_rec->features,
301                        (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
302                    p->num_read_pages = p_dev_rec->num_read_pages;
303
304                    if (BTM_SEC_MODE_SP == btm_cb.security_mode
305                        && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
306                        && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
307                    {
308                        p_dev_rec->sm4 = BTM_SM4_TRUE;
309                    }
310                    else
311                    {
312                        p_dev_rec->sm4 |= BTM_SM4_KNOWN;
313                    }
314
315                    btm_establish_continue (p);
316                    return;
317                }
318            }
319
320#if (BLE_INCLUDED == TRUE)
321            /* If here, features are not known yet */
322            if (p_dev_rec && transport == BT_TRANSPORT_LE)
323            {
324#if BLE_PRIVACY_SPT == TRUE
325                btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
326                    &p->active_remote_addr_type);
327#endif
328
329                if (HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(btm_cb.devcb.local_le_features)
330                    || link_role == HCI_ROLE_MASTER)
331                {
332                    btsnd_hcic_ble_read_remote_feat(p->hci_handle);
333                }
334                else
335                {
336                    btm_establish_continue(p);
337                }
338            }
339            else
340#endif
341            {
342                btm_read_remote_features (p->hci_handle);
343            }
344
345            /* read page 1 - on rmt feature event for buffer reasons */
346            return;
347        }
348    }
349}
350
351
352/*******************************************************************************
353**
354** Function         btm_acl_report_role_change
355**
356** Description      This function is called when the local device is deemed
357**                  to be down. It notifies L2CAP of the failure.
358**
359** Returns          void
360**
361*******************************************************************************/
362void btm_acl_report_role_change (UINT8 hci_status, BD_ADDR bda)
363{
364    tBTM_ROLE_SWITCH_CMPL   ref_data;
365    BTM_TRACE_DEBUG ("btm_acl_report_role_change");
366    if (btm_cb.devcb.p_switch_role_cb
367        && (bda && (0 == memcmp(btm_cb.devcb.switch_role_ref_data.remote_bd_addr, bda, BD_ADDR_LEN))))
368    {
369        memcpy (&ref_data, &btm_cb.devcb.switch_role_ref_data, sizeof(tBTM_ROLE_SWITCH_CMPL));
370        ref_data.hci_status = hci_status;
371        (*btm_cb.devcb.p_switch_role_cb)(&ref_data);
372        memset (&btm_cb.devcb.switch_role_ref_data, 0, sizeof(tBTM_ROLE_SWITCH_CMPL));
373        btm_cb.devcb.p_switch_role_cb = NULL;
374    }
375}
376
377/*******************************************************************************
378**
379** Function         btm_acl_removed
380**
381** Description      This function is called by L2CAP when an ACL connection
382**                  is removed. Since only L2CAP creates ACL links, we use
383**                  the L2CAP link index as our index into the control blocks.
384**
385** Returns          void
386**
387*******************************************************************************/
388void btm_acl_removed (BD_ADDR bda, tBT_TRANSPORT transport)
389{
390    tACL_CONN   *p;
391#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
392    tBTM_BL_EVENT_DATA  evt_data;
393#endif
394#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
395    tBTM_SEC_DEV_REC *p_dev_rec=NULL;
396#endif
397
398    BTM_TRACE_DEBUG ("btm_acl_removed");
399    p = btm_bda_to_acl(bda, transport);
400    if (p != (tACL_CONN *)NULL)
401    {
402        p->in_use = FALSE;
403
404        /* if the disconnected channel has a pending role switch, clear it now */
405        btm_acl_report_role_change(HCI_ERR_NO_CONNECTION, bda);
406
407        /* Only notify if link up has had a chance to be issued */
408        if (p->link_up_issued)
409        {
410            p->link_up_issued = FALSE;
411
412            /* If anyone cares, tell him database changed */
413#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
414            if (btm_cb.p_bl_changed_cb)
415            {
416                evt_data.event = BTM_BL_DISCN_EVT;
417                evt_data.discn.p_bda = bda;
418#if BLE_INCLUDED == TRUE
419                evt_data.discn.handle = p->hci_handle;
420                evt_data.discn.transport = p->transport;
421#endif
422                (*btm_cb.p_bl_changed_cb)(&evt_data);
423            }
424
425            btm_acl_update_busy_level (BTM_BLI_ACL_DOWN_EVT);
426#else
427            if (btm_cb.p_acl_changed_cb)
428#if BLE_INCLUDED == TRUE
429                (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE, p->hci_handle, p->transport);
430#else
431                (*btm_cb.p_acl_changed_cb) (bda, NULL, NULL, NULL, FALSE);
432#endif
433#endif
434        }
435
436#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
437
438        BTM_TRACE_DEBUG ("acl hci_handle=%d transport=%d connectable_mode=0x%0x link_role=%d",
439                          p->hci_handle,
440                          p->transport,
441                          btm_cb.ble_ctr_cb.inq_var.connectable_mode,
442                          p->link_role);
443
444        p_dev_rec = btm_find_dev(bda);
445        if ( p_dev_rec)
446        {
447            BTM_TRACE_DEBUG("before update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
448            if (p->transport == BT_TRANSPORT_LE)
449            {
450                BTM_TRACE_DEBUG("LE link down");
451                p_dev_rec->sec_flags &= ~(BTM_SEC_LE_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
452                if ( (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) == 0)
453                {
454                    BTM_TRACE_DEBUG("Not Bonded");
455                    p_dev_rec->sec_flags &= ~(BTM_SEC_LE_LINK_KEY_AUTHED | BTM_SEC_LE_AUTHENTICATED);
456                }
457                else
458                {
459                    BTM_TRACE_DEBUG("Bonded");
460                }
461            }
462            else
463            {
464                BTM_TRACE_DEBUG("Bletooth link down");
465                p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
466                                        | BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED);
467            }
468            BTM_TRACE_DEBUG("after update p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
469        }
470        else
471        {
472            BTM_TRACE_ERROR("Device not found");
473
474        }
475#endif
476
477        return;
478    }
479}
480
481
482/*******************************************************************************
483**
484** Function         btm_acl_device_down
485**
486** Description      This function is called when the local device is deemed
487**                  to be down. It notifies L2CAP of the failure.
488**
489** Returns          void
490**
491*******************************************************************************/
492void btm_acl_device_down (void)
493{
494    tACL_CONN   *p = &btm_cb.acl_db[0];
495    UINT16      xx;
496    BTM_TRACE_DEBUG ("btm_acl_device_down");
497    for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
498    {
499        if (p->in_use)
500        {
501            BTM_TRACE_DEBUG ("hci_handle=%d HCI_ERR_HW_FAILURE ",p->hci_handle );
502            l2c_link_hci_disc_comp (p->hci_handle, HCI_ERR_HW_FAILURE);
503        }
504    }
505}
506
507#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
508/*******************************************************************************
509**
510** Function         btm_acl_update_busy_level
511**
512** Description      This function is called to update the busy level of the system
513**                  .
514**
515** Returns          void
516**
517*******************************************************************************/
518void btm_acl_update_busy_level (tBTM_BLI_EVENT event)
519{
520    tBTM_BL_UPDATE_DATA  evt;
521    UINT8 busy_level;
522    BTM_TRACE_DEBUG ("btm_acl_update_busy_level");
523    BOOLEAN old_inquiry_state = btm_cb.is_inquiry;
524    switch (event)
525    {
526        case BTM_BLI_ACL_UP_EVT:
527            BTM_TRACE_DEBUG ("BTM_BLI_ACL_UP_EVT");
528            btm_cb.num_acl++;
529            break;
530        case BTM_BLI_ACL_DOWN_EVT:
531            if (btm_cb.num_acl)
532            {
533                btm_cb.num_acl--;
534                BTM_TRACE_DEBUG ("BTM_BLI_ACL_DOWN_EVT", btm_cb.num_acl);
535            }
536            else
537            {
538                BTM_TRACE_ERROR ("BTM_BLI_ACL_DOWN_EVT issued, but num_acl already zero !!!");
539            }
540            break;
541        case BTM_BLI_PAGE_EVT:
542            BTM_TRACE_DEBUG ("BTM_BLI_PAGE_EVT");
543            btm_cb.is_paging = TRUE;
544            evt.busy_level_flags= BTM_BL_PAGING_STARTED;
545            break;
546        case BTM_BLI_PAGE_DONE_EVT:
547            BTM_TRACE_DEBUG ("BTM_BLI_PAGE_DONE_EVT");
548            btm_cb.is_paging = FALSE;
549            evt.busy_level_flags = BTM_BL_PAGING_COMPLETE;
550            break;
551        case BTM_BLI_INQ_EVT:
552            BTM_TRACE_DEBUG ("BTM_BLI_INQ_EVT");
553            btm_cb.is_inquiry = TRUE;
554            evt.busy_level_flags = BTM_BL_INQUIRY_STARTED;
555            break;
556        case BTM_BLI_INQ_CANCEL_EVT:
557            BTM_TRACE_DEBUG ("BTM_BLI_INQ_CANCEL_EVT");
558            btm_cb.is_inquiry = FALSE;
559            evt.busy_level_flags = BTM_BL_INQUIRY_CANCELLED;
560            break;
561        case BTM_BLI_INQ_DONE_EVT:
562            BTM_TRACE_DEBUG ("BTM_BLI_INQ_DONE_EVT");
563            btm_cb.is_inquiry = FALSE;
564            evt.busy_level_flags = BTM_BL_INQUIRY_COMPLETE;
565            break;
566    }
567
568    if (btm_cb.is_paging || btm_cb.is_inquiry)
569        busy_level = 10;
570    else
571        busy_level = (UINT8)btm_cb.num_acl;
572
573    if ((busy_level != btm_cb.busy_level) ||(old_inquiry_state != btm_cb.is_inquiry))
574    {
575        evt.event         = BTM_BL_UPDATE_EVT;
576        evt.busy_level    = busy_level;
577        btm_cb.busy_level = busy_level;
578        if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_UPDATE_MASK))
579        {
580            (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
581        }
582    }
583}
584#endif
585
586
587/*******************************************************************************
588**
589** Function         BTM_GetRole
590**
591** Description      This function is called to get the role of the local device
592**                  for the ACL connection with the specified remote device
593**
594** Returns          BTM_SUCCESS if connection exists.
595**                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
596**
597*******************************************************************************/
598tBTM_STATUS BTM_GetRole (BD_ADDR remote_bd_addr, UINT8 *p_role)
599{
600    tACL_CONN   *p;
601    BTM_TRACE_DEBUG ("BTM_GetRole");
602    if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
603    {
604        *p_role = BTM_ROLE_UNDEFINED;
605        return(BTM_UNKNOWN_ADDR);
606    }
607
608    /* Get the current role */
609    *p_role = p->link_role;
610    return(BTM_SUCCESS);
611}
612
613
614/*******************************************************************************
615**
616** Function         BTM_SwitchRole
617**
618** Description      This function is called to switch role between master and
619**                  slave.  If role is already set it will do nothing.  If the
620**                  command was initiated, the callback function is called upon
621**                  completion.
622**
623** Returns          BTM_SUCCESS if already in specified role.
624**                  BTM_CMD_STARTED if command issued to controller.
625**                  BTM_NO_RESOURCES if couldn't allocate memory to issue command
626**                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
627**                  BTM_MODE_UNSUPPORTED if local device does not support role switching
628**                  BTM_BUSY if the previous command is not completed
629**
630*******************************************************************************/
631tBTM_STATUS BTM_SwitchRole (BD_ADDR remote_bd_addr, UINT8 new_role, tBTM_CMPL_CB *p_cb)
632{
633    tACL_CONN   *p;
634    tBTM_SEC_DEV_REC  *p_dev_rec = NULL;
635#if BTM_SCO_INCLUDED == TRUE
636    BOOLEAN    is_sco_active;
637#endif
638#if BTM_PWR_MGR_INCLUDED == TRUE
639    tBTM_STATUS  status;
640    tBTM_PM_MODE pwr_mode;
641    tBTM_PM_PWR_MD settings;
642#endif
643#if (BT_USE_TRACES == TRUE)
644    BD_ADDR_PTR  p_bda;
645#endif
646    BTM_TRACE_API ("BTM_SwitchRole BDA: %02x-%02x-%02x-%02x-%02x-%02x",
647                    remote_bd_addr[0], remote_bd_addr[1], remote_bd_addr[2],
648                    remote_bd_addr[3], remote_bd_addr[4], remote_bd_addr[5]);
649
650    /* Make sure the local device supports switching */
651    if (!(HCI_SWITCH_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0])))
652        return(BTM_MODE_UNSUPPORTED);
653
654    if (btm_cb.devcb.p_switch_role_cb && p_cb)
655    {
656#if (BT_USE_TRACES == TRUE)
657        p_bda = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
658        BTM_TRACE_DEBUG ("Role switch on other device is in progress 0x%02x%02x%02x%02x%02x%02x",
659                          p_bda[0], p_bda[1], p_bda[2],
660                          p_bda[3], p_bda[4], p_bda[5]);
661#endif
662        return(BTM_BUSY);
663    }
664
665    if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
666        return(BTM_UNKNOWN_ADDR);
667
668    /* Finished if already in desired role */
669    if (p->link_role == new_role)
670        return(BTM_SUCCESS);
671
672#if BTM_SCO_INCLUDED == TRUE
673    /* Check if there is any SCO Active on this BD Address */
674    is_sco_active = btm_is_sco_active_by_bdaddr(remote_bd_addr);
675
676    if (is_sco_active == TRUE)
677        return(BTM_NO_RESOURCES);
678#endif
679
680    /* Ignore role switch request if the previous request was not completed */
681    if (p->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
682    {
683        BTM_TRACE_DEBUG ("BTM_SwitchRole busy: %d",
684                          p->switch_role_state);
685        return(BTM_BUSY);
686    }
687
688    /* Cannot switch role while parked or sniffing */
689#if BTM_PWR_MGR_INCLUDED == FALSE
690    if (p->mode == HCI_MODE_PARK)
691    {
692        if (!btsnd_hcic_exit_park_mode (p->hci_handle))
693            return(BTM_NO_RESOURCES);
694
695        p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
696    }
697    else if (p->mode == HCI_MODE_SNIFF)
698    {
699        if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
700            return(BTM_NO_RESOURCES);
701
702        p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
703    }
704#else   /* power manager is in use */
705
706    if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
707        return(status);
708
709    /* Wake up the link if in sniff or park before attempting switch */
710    if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF)
711    {
712        memset( (void*)&settings, 0, sizeof(settings));
713        settings.mode = BTM_PM_MD_ACTIVE;
714        status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
715        if (status != BTM_CMD_STARTED)
716            return(BTM_WRONG_MODE);
717
718        p->switch_role_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
719    }
720#endif
721    /* some devices do not support switch while encryption is on */
722    else
723    {
724        p_dev_rec = btm_find_dev (remote_bd_addr);
725        if ((p_dev_rec != NULL)
726            && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
727            && !BTM_EPR_AVAILABLE(p))
728        {
729            /* bypass turning off encryption if change link key is already doing it */
730            if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
731            {
732                if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
733                    return(BTM_NO_RESOURCES);
734                else
735                    p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
736            }
737
738            p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
739        }
740        else
741        {
742            if (!btsnd_hcic_switch_role (remote_bd_addr, new_role))
743                return(BTM_NO_RESOURCES);
744
745            p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
746
747#if BTM_DISC_DURING_RS == TRUE
748            if (p_dev_rec)
749                p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
750#endif
751        }
752    }
753
754    /* Initialize return structure in case request fails */
755    if (p_cb)
756    {
757        memcpy (btm_cb.devcb.switch_role_ref_data.remote_bd_addr, remote_bd_addr,
758                BD_ADDR_LEN);
759        btm_cb.devcb.switch_role_ref_data.role = new_role;
760        /* initialized to an error code */
761        btm_cb.devcb.switch_role_ref_data.hci_status = HCI_ERR_UNSUPPORTED_VALUE;
762        btm_cb.devcb.p_switch_role_cb = p_cb;
763    }
764    return(BTM_CMD_STARTED);
765}
766
767/*******************************************************************************
768**
769** Function         BTM_ChangeLinkKey
770**
771** Description      This function is called to change the link key of the
772**                  connection.
773**
774** Returns          BTM_CMD_STARTED if command issued to controller.
775**                  BTM_NO_RESOURCES if couldn't allocate memory to issue command
776**                  BTM_UNKNOWN_ADDR if no active link with bd addr specified
777**                  BTM_BUSY if the previous command is not completed
778**
779*******************************************************************************/
780tBTM_STATUS BTM_ChangeLinkKey (BD_ADDR remote_bd_addr, tBTM_CMPL_CB *p_cb)
781{
782    tACL_CONN   *p;
783    tBTM_SEC_DEV_REC  *p_dev_rec = NULL;
784#if BTM_PWR_MGR_INCLUDED == TRUE
785    tBTM_STATUS  status;
786    tBTM_PM_MODE pwr_mode;
787    tBTM_PM_PWR_MD settings;
788#endif
789    BTM_TRACE_DEBUG ("BTM_ChangeLinkKey");
790    if ((p = btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR)) == NULL)
791        return(BTM_UNKNOWN_ADDR);
792
793    /* Ignore change link key request if the previsous request has not completed */
794    if (p->change_key_state != BTM_ACL_SWKEY_STATE_IDLE)
795    {
796        BTM_TRACE_DEBUG ("Link key change request declined since the previous request"
797                          " for this device has not completed ");
798        return(BTM_BUSY);
799    }
800
801    memset (&btm_cb.devcb.chg_link_key_ref_data, 0, sizeof(tBTM_CHANGE_KEY_CMPL));
802
803    /* Cannot change key while parked */
804#if BTM_PWR_MGR_INCLUDED == FALSE
805    if (p->mode == HCI_MODE_PARK)
806    {
807        if (!btsnd_hcic_exit_park_mode (p->hci_handle))
808            return(BTM_NO_RESOURCES);
809
810        p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
811    }
812#else   /* power manager is in use */
813
814
815    if ((status = BTM_ReadPowerMode(p->remote_addr, &pwr_mode)) != BTM_SUCCESS)
816        return(status);
817
818    /* Wake up the link if in park before attempting to change link keys */
819    if (pwr_mode == BTM_PM_MD_PARK)
820    {
821        memset( (void*)&settings, 0, sizeof(settings));
822        settings.mode = BTM_PM_MD_ACTIVE;
823        status = BTM_SetPowerMode (BTM_PM_SET_ONLY_ID, p->remote_addr, &settings);
824        if (status != BTM_CMD_STARTED)
825            return(BTM_WRONG_MODE);
826
827        p->change_key_state = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
828    }
829#endif
830    /* some devices do not support change of link key while encryption is on */
831    else if (((p_dev_rec = btm_find_dev (remote_bd_addr)) != NULL)
832             && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0) && !BTM_EPR_AVAILABLE(p))
833    {
834        /* bypass turning off encryption if switch role is already doing it */
835        if (p->encrypt_state != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF)
836        {
837            if (!btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
838                return(BTM_NO_RESOURCES);
839            else
840                p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
841        }
842
843        p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
844    }
845    else    /* Ok to initiate change of link key */
846    {
847        if (!btsnd_hcic_change_link_key (p->hci_handle))
848            return(BTM_NO_RESOURCES);
849
850        p->change_key_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
851    }
852
853    /* Initialize return structure in case request fails */
854    memcpy (btm_cb.devcb.chg_link_key_ref_data.remote_bd_addr, remote_bd_addr,
855            BD_ADDR_LEN);
856    btm_cb.devcb.p_chg_link_key_cb = p_cb;
857    return(BTM_CMD_STARTED);
858}
859
860/*******************************************************************************
861**
862** Function         btm_acl_link_key_change
863**
864** Description      This function is called to when a change link key event
865**                  is received.
866**
867*******************************************************************************/
868void btm_acl_link_key_change (UINT16 handle, UINT8 status)
869{
870    tBTM_CHANGE_KEY_CMPL *p_data;
871    tACL_CONN            *p;
872    UINT8                xx;
873    BTM_TRACE_DEBUG ("btm_acl_link_key_change");
874    /* Look up the connection by handle and set the current mode */
875    xx = btm_handle_to_acl_index(handle);
876
877    /* don't assume that we can never get a bad hci_handle */
878    if (xx >= MAX_L2CAP_LINKS)
879        return;
880
881    p_data = &btm_cb.devcb.chg_link_key_ref_data;
882    p = &btm_cb.acl_db[xx];
883    p_data->hci_status = status;
884
885    /* if switching state is switching we need to turn encryption on */
886    /* if idle, we did not change encryption */
887    if (p->change_key_state == BTM_ACL_SWKEY_STATE_SWITCHING)
888    {
889        /* Make sure there's not also a role switch going on before re-enabling */
890        if (p->switch_role_state != BTM_ACL_SWKEY_STATE_SWITCHING)
891        {
892            if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
893            {
894                p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
895                p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
896                return;
897            }
898        }
899        else    /* Set the state and wait for change link key */
900        {
901            p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
902            return;
903        }
904    }
905
906    /* Set the switch_role_state to IDLE since the reply received from HCI */
907    /* regardless of its result either success or failed. */
908    if (p->change_key_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
909    {
910        p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
911        p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
912    }
913
914    if (btm_cb.devcb.p_chg_link_key_cb)
915    {
916        (*btm_cb.devcb.p_chg_link_key_cb)((void *)p_data);
917        btm_cb.devcb.p_chg_link_key_cb = NULL;
918    }
919
920    BTM_TRACE_ERROR("Change Link Key Complete Event: Handle 0x%02x, HCI Status 0x%02x",
921                     handle, p_data->hci_status);
922}
923
924/*******************************************************************************
925**
926** Function         btm_acl_encrypt_change
927**
928** Description      This function is when encryption of the connection is
929**                  completed by the LM.  Checks to see if a role switch or
930**                  change of link key was active and initiates or continues
931**                  process if needed.
932**
933** Returns          void
934**
935*******************************************************************************/
936void btm_acl_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
937{
938    tACL_CONN *p;
939    UINT8     xx;
940    tBTM_SEC_DEV_REC  *p_dev_rec;
941#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
942    tBTM_BL_ROLE_CHG_DATA   evt;
943#endif
944
945    BTM_TRACE_DEBUG ("btm_acl_encrypt_change handle=%d status=%d encr_enabl=%d",
946                      handle, status, encr_enable);
947    xx = btm_handle_to_acl_index(handle);
948    /* don't assume that we can never get a bad hci_handle */
949    if (xx < MAX_L2CAP_LINKS)
950        p = &btm_cb.acl_db[xx];
951    else
952        return;
953
954    /* Process Role Switch if active */
955    if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
956    {
957        /* if encryption turn off failed we still will try to switch role */
958        if (encr_enable)
959        {
960            p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
961            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
962        }
963        else
964        {
965            p->switch_role_state = BTM_ACL_SWKEY_STATE_SWITCHING;
966            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
967        }
968
969        if (!btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role))
970        {
971            p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
972            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
973            btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
974        }
975#if BTM_DISC_DURING_RS == TRUE
976        else
977        {
978            if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
979                p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
980        }
981#endif
982
983    }
984    /* Finished enabling Encryption after role switch */
985    else if (p->switch_role_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
986    {
987        p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
988        p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
989        btm_acl_report_role_change(btm_cb.devcb.switch_role_ref_data.hci_status, p->remote_addr);
990
991#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
992        /* if role change event is registered, report it now */
993        if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
994        {
995            evt.event       = BTM_BL_ROLE_CHG_EVT;
996            evt.new_role    = btm_cb.devcb.switch_role_ref_data.role;
997            evt.p_bda       = btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
998            evt.hci_status  = btm_cb.devcb.switch_role_ref_data.hci_status;
999            (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1000
1001            BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
1002                             evt.new_role, evt.hci_status, p->switch_role_state);
1003        }
1004#endif
1005
1006#if BTM_DISC_DURING_RS == TRUE
1007        /* If a disconnect is pending, issue it now that role switch has completed */
1008        if ((p_dev_rec = btm_find_dev (p->remote_addr)) != NULL)
1009        {
1010            if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
1011            {
1012                BTM_TRACE_WARNING("btm_acl_encrypt_change -> Issuing delayed HCI_Disconnect!!!");
1013                btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1014            }
1015            BTM_TRACE_ERROR("btm_acl_encrypt_change: tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
1016                (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1017            p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;     /* reset flag */
1018        }
1019#endif
1020    }
1021
1022
1023    /* Process Change Link Key if active */
1024    if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF)
1025    {
1026        /* if encryption turn off failed we still will try to change link key */
1027        if (encr_enable)
1028        {
1029            p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1030            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1031        }
1032        else
1033        {
1034            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
1035            p->change_key_state = BTM_ACL_SWKEY_STATE_SWITCHING;
1036        }
1037
1038        if (!btsnd_hcic_change_link_key (p->hci_handle))
1039        {
1040            p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1041            p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1042            if (btm_cb.devcb.p_chg_link_key_cb)
1043            {
1044                (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1045                btm_cb.devcb.p_chg_link_key_cb = NULL;
1046            }
1047        }
1048    }
1049    /* Finished enabling Encryption after changing link key */
1050    else if (p->change_key_state == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON)
1051    {
1052        p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1053        p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
1054        if (btm_cb.devcb.p_chg_link_key_cb)
1055        {
1056            (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
1057            btm_cb.devcb.p_chg_link_key_cb = NULL;
1058        }
1059    }
1060}
1061/*******************************************************************************
1062**
1063** Function         BTM_SetLinkPolicy
1064**
1065** Description      Create and send HCI "Write Policy Set" command
1066**
1067** Returns          status of the operation
1068**
1069*******************************************************************************/
1070tBTM_STATUS BTM_SetLinkPolicy (BD_ADDR remote_bda, UINT16 *settings)
1071{
1072    tACL_CONN   *p;
1073    UINT8       *localFeatures = BTM_ReadLocalFeatures();
1074    BTM_TRACE_DEBUG ("BTM_SetLinkPolicy");
1075/*    BTM_TRACE_API ("BTM_SetLinkPolicy: requested settings: 0x%04x", *settings ); */
1076
1077    /* First, check if hold mode is supported */
1078    if (*settings != HCI_DISABLE_ALL_LM_MODES)
1079    {
1080        if ( (*settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)) )
1081        {
1082            *settings &= (~HCI_ENABLE_MASTER_SLAVE_SWITCH);
1083            BTM_TRACE_API ("BTM_SetLinkPolicy switch not supported (settings: 0x%04x)", *settings );
1084        }
1085        if ( (*settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)) )
1086        {
1087            *settings &= (~HCI_ENABLE_HOLD_MODE);
1088            BTM_TRACE_API ("BTM_SetLinkPolicy hold not supported (settings: 0x%04x)", *settings );
1089        }
1090        if ( (*settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)) )
1091        {
1092            *settings &= (~HCI_ENABLE_SNIFF_MODE);
1093            BTM_TRACE_API ("BTM_SetLinkPolicy sniff not supported (settings: 0x%04x)", *settings );
1094        }
1095        if ( (*settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)) )
1096        {
1097            *settings &= (~HCI_ENABLE_PARK_MODE);
1098            BTM_TRACE_API ("BTM_SetLinkPolicy park not supported (settings: 0x%04x)", *settings );
1099        }
1100    }
1101
1102    if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
1103        return(btsnd_hcic_write_policy_set (p->hci_handle, *settings) ? BTM_CMD_STARTED : BTM_NO_RESOURCES);
1104
1105    /* If here, no BD Addr found */
1106    return(BTM_UNKNOWN_ADDR);
1107}
1108
1109/*******************************************************************************
1110**
1111** Function         BTM_SetDefaultLinkPolicy
1112**
1113** Description      Set the default value for HCI "Write Policy Set" command
1114**                  to use when an ACL link is created.
1115**
1116** Returns          void
1117**
1118*******************************************************************************/
1119void BTM_SetDefaultLinkPolicy (UINT16 settings)
1120{
1121    UINT8 *localFeatures = BTM_ReadLocalFeatures();
1122
1123    BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy setting:0x%04x", settings);
1124
1125    if((settings & HCI_ENABLE_MASTER_SLAVE_SWITCH) && (!HCI_SWITCH_SUPPORTED(localFeatures)))
1126    {
1127        settings &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1128        BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy switch not supported (settings: 0x%04x)", settings);
1129    }
1130    if ((settings & HCI_ENABLE_HOLD_MODE) && (!HCI_HOLD_MODE_SUPPORTED(localFeatures)))
1131    {
1132        settings &= ~HCI_ENABLE_HOLD_MODE;
1133        BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy hold not supported (settings: 0x%04x)", settings);
1134    }
1135    if ((settings & HCI_ENABLE_SNIFF_MODE) && (!HCI_SNIFF_MODE_SUPPORTED(localFeatures)))
1136    {
1137        settings &= ~HCI_ENABLE_SNIFF_MODE;
1138        BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy sniff not supported (settings: 0x%04x)", settings);
1139    }
1140    if ((settings & HCI_ENABLE_PARK_MODE) && (!HCI_PARK_MODE_SUPPORTED(localFeatures)))
1141    {
1142        settings &= ~HCI_ENABLE_PARK_MODE;
1143        BTM_TRACE_DEBUG("BTM_SetDefaultLinkPolicy park not supported (settings: 0x%04x)", settings);
1144    }
1145    BTM_TRACE_DEBUG("Set DefaultLinkPolicy:0x%04x", settings);
1146
1147    btm_cb.btm_def_link_policy = settings;
1148
1149    /* Set the default Link Policy of the controller */
1150    btsnd_hcic_write_def_policy_set(settings);
1151}
1152
1153
1154/*******************************************************************************
1155**
1156** Function         BTM_ReadLinkPolicy
1157**
1158** Description      This function is called to read the link policy settings.
1159**                  The address of link policy results are returned in the callback.
1160**                  (tBTM_LNK_POLICY_RESULTS)
1161**
1162** Returns          status of the operation
1163**
1164*******************************************************************************/
1165tBTM_STATUS BTM_ReadLinkPolicy (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
1166{
1167    tACL_CONN   *p;
1168
1169    BTM_TRACE_API ("BTM_ReadLinkPolicy: RemBdAddr: %02x%02x%02x%02x%02x%02x",
1170                    remote_bda[0], remote_bda[1], remote_bda[2],
1171                    remote_bda[3], remote_bda[4], remote_bda[5]);
1172
1173    /* If someone already waiting on the version, do not allow another */
1174    if (btm_cb.devcb.p_rlinkp_cmpl_cb)
1175        return(BTM_BUSY);
1176
1177    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1178    if (p != (tACL_CONN *)NULL)
1179    {
1180        btu_start_timer (&btm_cb.devcb.rlinkp_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1181        btm_cb.devcb.p_rlinkp_cmpl_cb = p_cb;
1182
1183        if (!btsnd_hcic_read_policy_set (p->hci_handle))
1184        {
1185            btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1186            btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1187            return(BTM_NO_RESOURCES);
1188        }
1189
1190        return(BTM_CMD_STARTED);
1191    }
1192
1193    /* If here, no BD Addr found */
1194    return(BTM_UNKNOWN_ADDR);
1195}
1196
1197
1198/*******************************************************************************
1199**
1200** Function         btm_read_link_policy_complete
1201**
1202** Description      This function is called when the command complete message
1203**                  is received from the HCI for the read local link policy request.
1204**
1205** Returns          void
1206**
1207*******************************************************************************/
1208void btm_read_link_policy_complete (UINT8 *p)
1209{
1210    tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
1211    tBTM_LNK_POLICY_RESULTS  lnkpol;
1212    UINT16                   handle;
1213    tACL_CONN               *p_acl_cb = &btm_cb.acl_db[0];
1214    UINT16                   index;
1215    BTM_TRACE_DEBUG ("btm_read_link_policy_complete");
1216    btu_stop_timer (&btm_cb.devcb.rlinkp_timer);
1217
1218    /* If there was a callback address for read local version, call it */
1219    btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
1220
1221    if (p_cb)
1222    {
1223        STREAM_TO_UINT8  (lnkpol.hci_status, p);
1224
1225        if (lnkpol.hci_status == HCI_SUCCESS)
1226        {
1227            lnkpol.status = BTM_SUCCESS;
1228
1229            STREAM_TO_UINT16 (handle, p);
1230
1231            STREAM_TO_UINT16 (lnkpol.settings, p);
1232
1233            /* Search through the list of active channels for the correct BD Addr */
1234            for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
1235            {
1236                if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
1237                {
1238                    memcpy (lnkpol.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
1239                    break;
1240                }
1241            }
1242        }
1243        else
1244            lnkpol.status = BTM_ERR_PROCESSING;
1245
1246        (*p_cb)(&lnkpol);
1247    }
1248}
1249
1250
1251/*******************************************************************************
1252**
1253** Function         btm_read_remote_version_complete
1254**
1255** Description      This function is called when the command complete message
1256**                  is received from the HCI for the remote version info.
1257**
1258** Returns          void
1259**
1260*******************************************************************************/
1261void btm_read_remote_version_complete (UINT8 *p)
1262{
1263    tACL_CONN        *p_acl_cb = &btm_cb.acl_db[0];
1264    UINT8             status;
1265    UINT16            handle;
1266    int               xx;
1267    BTM_TRACE_DEBUG ("btm_read_remote_version_complete");
1268    STREAM_TO_UINT8  (status, p);
1269    if (status == HCI_SUCCESS)
1270    {
1271        STREAM_TO_UINT16 (handle, p);
1272
1273        /* Look up the connection by handle and copy features */
1274        for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
1275        {
1276            if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
1277            {
1278                STREAM_TO_UINT8  (p_acl_cb->lmp_version, p);
1279                STREAM_TO_UINT16 (p_acl_cb->manufacturer, p);
1280                STREAM_TO_UINT16 (p_acl_cb->lmp_subversion, p);
1281                break;
1282            }
1283        }
1284    }
1285}
1286
1287
1288/*******************************************************************************
1289**
1290** Function         btm_process_remote_ext_features
1291**
1292** Description      Local function called to process all extended features pages
1293**                  read from a remote device.
1294**
1295** Returns          void
1296**
1297*******************************************************************************/
1298void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
1299{
1300    UINT16              handle = p_acl_cb->hci_handle;
1301    tBTM_SEC_DEV_REC    *p_dev_rec = btm_find_dev_by_handle (handle);
1302    UINT8               page_idx;
1303
1304    BTM_TRACE_DEBUG ("btm_process_remote_ext_features");
1305
1306    /* Make sure we have the record to save remote features information */
1307    if (p_dev_rec == NULL)
1308    {
1309        /* Get a new device; might be doing dedicated bonding */
1310        p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
1311    }
1312
1313    p_acl_cb->num_read_pages = num_read_pages;
1314    p_dev_rec->num_read_pages = num_read_pages;
1315
1316    /* Process the pages one by one */
1317    for (page_idx = 0; page_idx < num_read_pages; page_idx++)
1318    {
1319        btm_process_remote_ext_features_page (p_acl_cb, p_dev_rec, page_idx);
1320    }
1321}
1322
1323
1324/*******************************************************************************
1325**
1326** Function         btm_process_remote_ext_features_page
1327**
1328** Description      Local function called to process the information located
1329**                  in the specific extended features page read from a remote device.
1330**
1331** Returns          void
1332**
1333*******************************************************************************/
1334void btm_process_remote_ext_features_page (tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec,
1335                                           UINT8 page_idx)
1336{
1337    UINT16            handle;
1338    UINT8             req_pend;
1339
1340    handle = p_acl_cb->hci_handle;
1341
1342    memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1343            HCI_FEATURE_BYTES_PER_PAGE);
1344
1345    switch (page_idx)
1346    {
1347    /* Extended (Legacy) Page 0 */
1348    case HCI_EXT_FEATURES_PAGE_0:
1349        /* Page 0 indicates Controller support for SSP */
1350        if (btm_cb.security_mode < BTM_SEC_MODE_SP ||
1351            !HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1352        {
1353            req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1354            p_dev_rec->sm4 = BTM_SM4_KNOWN;
1355            if (req_pend)
1356            {
1357                l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1358            }
1359        }
1360        break;
1361
1362    /* Extended Page 1 */
1363    case HCI_EXT_FEATURES_PAGE_1:
1364        /* Page 1 indicates Host support for SSP and SC */
1365        req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1366
1367        if (btm_cb.security_mode == BTM_SEC_MODE_SP
1368            && HCI_SSP_HOST_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1])
1369            && HCI_SIMPLE_PAIRING_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
1370        {
1371            p_dev_rec->sm4 = BTM_SM4_TRUE;
1372        }
1373        else
1374        {
1375            p_dev_rec->sm4 = BTM_SM4_KNOWN;
1376        }
1377
1378        BTM_TRACE_API ("ext_features_complt page_num:%d f[0]:x%02x, sm4:%x, pend:%d",
1379                        HCI_EXT_FEATURES_PAGE_1, *(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_1]),
1380                        p_dev_rec->sm4, req_pend);
1381
1382        if (req_pend)
1383            l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1384
1385        break;
1386
1387    /* Extended Page 2 */
1388    case HCI_EXT_FEATURES_PAGE_2:
1389        /* Page 2 indicates Ping support*/
1390        break;
1391
1392    default:
1393        BTM_TRACE_ERROR("btm_process_remote_ext_features_page page=%d unexpected", page_idx);
1394        break;
1395    }
1396}
1397
1398
1399/*******************************************************************************
1400**
1401** Function         btm_read_remote_features
1402**
1403** Description      Local function called to send a read remote supported features/
1404**                  remote extended features page[0].
1405**
1406** Returns          void
1407**
1408*******************************************************************************/
1409void btm_read_remote_features (UINT16 handle)
1410{
1411    UINT8       acl_idx;
1412    tACL_CONN   *p_acl_cb;
1413
1414    BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d", handle);
1415
1416    if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1417    {
1418        BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid", handle);
1419        return;
1420    }
1421
1422    p_acl_cb = &btm_cb.acl_db[acl_idx];
1423    p_acl_cb->num_read_pages = 0;
1424    memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1425
1426    /* first send read remote supported features HCI command */
1427    /* because we don't know whether the remote support extended feature command */
1428    btsnd_hcic_rmt_features_req (handle);
1429}
1430
1431
1432/*******************************************************************************
1433**
1434** Function         btm_read_remote_ext_features
1435**
1436** Description      Local function called to send a read remote extended features
1437**
1438** Returns          void
1439**
1440*******************************************************************************/
1441void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1442{
1443    BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d", handle, page_number);
1444
1445    btsnd_hcic_rmt_ext_features(handle, page_number);
1446}
1447
1448
1449/*******************************************************************************
1450**
1451** Function         btm_read_remote_features_complete
1452**
1453** Description      This function is called when the remote supported features
1454**                  complete event is received from the HCI.
1455**
1456** Returns          void
1457**
1458*******************************************************************************/
1459void btm_read_remote_features_complete (UINT8 *p)
1460{
1461    tACL_CONN        *p_acl_cb;
1462    UINT8             status;
1463    UINT16            handle;
1464    UINT8            acl_idx;
1465
1466    BTM_TRACE_DEBUG ("btm_read_remote_features_complete");
1467    STREAM_TO_UINT8  (status, p);
1468
1469    if (status != HCI_SUCCESS)
1470    {
1471        BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)", status);
1472        return;
1473    }
1474
1475        STREAM_TO_UINT16 (handle, p);
1476
1477    if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1478        {
1479        BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid", handle);
1480        return;
1481                }
1482
1483    p_acl_cb = &btm_cb.acl_db[acl_idx];
1484
1485    /* Copy the received features page */
1486    STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1487                    HCI_FEATURE_BYTES_PER_PAGE);
1488
1489    if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1490        (HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(btm_cb.devcb.supported_cmds)))
1491    {
1492        /* if the remote controller has extended features and local controller supports
1493        ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1494        ** with extended features page 1 */
1495        BTM_TRACE_DEBUG ("Start reading remote extended features");
1496        btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1497        return;
1498    }
1499
1500    /* Remote controller has no extended features. Process remote controller supported features
1501       (features page HCI_EXT_FEATURES_PAGE_0). */
1502    btm_process_remote_ext_features (p_acl_cb, 1);
1503
1504    /* Continue with HCI connection establishment */
1505    btm_establish_continue (p_acl_cb);
1506}
1507
1508/*******************************************************************************
1509**
1510** Function         btm_read_remote_ext_features_complete
1511**
1512** Description      This function is called when the remote extended features
1513**                  complete event is received from the HCI.
1514**
1515** Returns          void
1516**
1517*******************************************************************************/
1518void btm_read_remote_ext_features_complete (UINT8 *p)
1519{
1520    tACL_CONN   *p_acl_cb;
1521    UINT8       status, page_num, max_page;
1522    UINT16      handle;
1523    UINT8       acl_idx;
1524
1525    BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete");
1526
1527    STREAM_TO_UINT8  (status, p);
1528    STREAM_TO_UINT16 (handle, p);
1529    STREAM_TO_UINT8  (page_num, p);
1530    STREAM_TO_UINT8  (max_page, p);
1531
1532    /* Validate parameters */
1533    if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1534    {
1535        BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid", handle);
1536        return;
1537    }
1538
1539    if (max_page > HCI_EXT_FEATURES_PAGE_MAX)
1540    {
1541        BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
1542        return;
1543    }
1544
1545    p_acl_cb = &btm_cb.acl_db[acl_idx];
1546
1547    /* Copy the received features page */
1548    STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1549
1550    /* If there is the next remote features page and
1551     * we have space to keep this page data - read this page */
1552    if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX))
1553    {
1554        page_num++;
1555        BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)", page_num);
1556        btm_read_remote_ext_features (handle, page_num);
1557        return;
1558    }
1559
1560    /* Reading of remote feature pages is complete */
1561    BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)", page_num);
1562
1563    /* Process the pages */
1564    btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1565
1566    /* Continue with HCI connection establishment */
1567    btm_establish_continue (p_acl_cb);
1568}
1569
1570/*******************************************************************************
1571**
1572** Function         btm_read_remote_ext_features_failed
1573**
1574** Description      This function is called when the remote extended features
1575**                  complete event returns a failed status.
1576**
1577** Returns          void
1578**
1579*******************************************************************************/
1580void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
1581{
1582    tACL_CONN   *p_acl_cb;
1583    UINT8       acl_idx;
1584
1585    BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d",
1586                         status, handle);
1587
1588    if ((acl_idx = btm_handle_to_acl_index(handle)) >= MAX_L2CAP_LINKS)
1589    {
1590        BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid", handle);
1591        return;
1592    }
1593
1594    p_acl_cb = &btm_cb.acl_db[acl_idx];
1595
1596    /* Process supported features only */
1597    btm_process_remote_ext_features (p_acl_cb, 1);
1598
1599    /* Continue HCI connection establishment */
1600    btm_establish_continue (p_acl_cb);
1601}
1602
1603/*******************************************************************************
1604**
1605** Function         btm_establish_continue
1606**
1607** Description      This function is called when the command complete message
1608**                  is received from the HCI for the read local link policy request.
1609**
1610** Returns          void
1611**
1612*******************************************************************************/
1613void btm_establish_continue (tACL_CONN *p_acl_cb)
1614{
1615#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
1616        tBTM_BL_EVENT_DATA  evt_data;
1617#endif
1618        BTM_TRACE_DEBUG ("btm_establish_continue");
1619#if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
1620#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
1621        if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
1622#endif
1623        {
1624            /* For now there are a some devices that do not like sending */
1625            /* commands events and data at the same time. */
1626            /* Set the packet types to the default allowed by the device */
1627            btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
1628
1629            if (btm_cb.btm_def_link_policy)
1630                BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1631        }
1632#endif
1633        p_acl_cb->link_up_issued = TRUE;
1634
1635        /* If anyone cares, tell him database changed */
1636#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
1637        if (btm_cb.p_bl_changed_cb)
1638        {
1639            evt_data.event = BTM_BL_CONN_EVT;
1640            evt_data.conn.p_bda = p_acl_cb->remote_addr;
1641            evt_data.conn.p_bdn = p_acl_cb->remote_name;
1642            evt_data.conn.p_dc  = p_acl_cb->remote_dc;
1643            evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1644#if BLE_INCLUDED == TRUE
1645            evt_data.conn.handle = p_acl_cb->hci_handle;
1646            evt_data.conn.transport = p_acl_cb->transport;
1647#endif
1648
1649            (*btm_cb.p_bl_changed_cb)(&evt_data);
1650        }
1651        btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1652#else
1653        if (btm_cb.p_acl_changed_cb)
1654#if BLE_INCLUDED == TRUE
1655            (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1656                                        p_acl_cb->remote_dc,
1657                                        p_acl_cb->remote_name,
1658                                        p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1659                                        TRUE,
1660                                        p_acl_cb->hci_handle,
1661                                        p_acl_cb->transport);
1662#else
1663            (*btm_cb.p_acl_changed_cb) (p_acl_cb->remote_addr,
1664                                        p_acl_cb->remote_dc,
1665                                        p_acl_cb->remote_name,
1666                                        p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0],
1667                                        TRUE);
1668#endif
1669
1670#endif
1671
1672}
1673
1674
1675/*******************************************************************************
1676**
1677** Function         BTM_SetDefaultLinkSuperTout
1678**
1679** Description      Set the default value for HCI "Write Link Supervision Timeout"
1680**                  command to use when an ACL link is created.
1681**
1682** Returns          void
1683**
1684*******************************************************************************/
1685void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1686{
1687    BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout");
1688    btm_cb.btm_def_link_super_tout = timeout;
1689}
1690
1691/*******************************************************************************
1692**
1693** Function         BTM_GetLinkSuperTout
1694**
1695** Description      Read the link supervision timeout value of the connection
1696**
1697** Returns          status of the operation
1698**
1699*******************************************************************************/
1700tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1701{
1702    tACL_CONN   *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1703
1704    BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout");
1705    if (p != (tACL_CONN *)NULL)
1706    {
1707        *p_timeout = p->link_super_tout;
1708        return(BTM_SUCCESS);
1709    }
1710    /* If here, no BD Addr found */
1711    return(BTM_UNKNOWN_ADDR);
1712}
1713
1714
1715/*******************************************************************************
1716**
1717** Function         BTM_SetLinkSuperTout
1718**
1719** Description      Create and send HCI "Write Link Supervision Timeout" command
1720**
1721** Returns          status of the operation
1722**
1723*******************************************************************************/
1724tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1725{
1726    tACL_CONN   *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1727
1728    BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout");
1729    if (p != (tACL_CONN *)NULL)
1730    {
1731        p->link_super_tout = timeout;
1732
1733        /* Only send if current role is Master; 2.0 spec requires this */
1734        if (p->link_role == BTM_ROLE_MASTER)
1735        {
1736            if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1737                                                   p->hci_handle, timeout))
1738                return(BTM_NO_RESOURCES);
1739
1740            return(BTM_CMD_STARTED);
1741        }
1742        else
1743            return(BTM_SUCCESS);
1744    }
1745
1746    /* If here, no BD Addr found */
1747    return(BTM_UNKNOWN_ADDR);
1748}
1749
1750/*******************************************************************************
1751**
1752** Function         BTM_RegForLstoEvt
1753**
1754** Description      register for the HCI "Link Supervision Timeout Change" event
1755**
1756** Returns          void
1757**
1758*******************************************************************************/
1759void BTM_RegForLstoEvt (tBTM_LSTO_CBACK *p_cback)
1760{
1761    BTM_TRACE_DEBUG ("BTM_RegForLstoEvt");
1762    btm_cb.p_lsto_cback = p_cback;
1763}
1764
1765/*******************************************************************************
1766**
1767** Function         btm_proc_lsto_evt
1768**
1769** Description      process the HCI "Link Supervision Timeout Change" event
1770**
1771** Returns          void
1772**
1773*******************************************************************************/
1774void btm_proc_lsto_evt(UINT16 handle, UINT16 timeout)
1775{
1776    UINT8 xx;
1777
1778    BTM_TRACE_DEBUG ("btm_proc_lsto_evt");
1779    if (btm_cb.p_lsto_cback)
1780    {
1781        /* Look up the connection by handle and set the current mode */
1782        xx = btm_handle_to_acl_index(handle);
1783
1784        /* don't assume that we can never get a bad hci_handle */
1785        if (xx < MAX_L2CAP_LINKS)
1786        {
1787            (*btm_cb.p_lsto_cback)(btm_cb.acl_db[xx].remote_addr, timeout);
1788        }
1789    }
1790}
1791
1792#if BTM_PWR_MGR_INCLUDED == FALSE
1793/*******************************************************************************
1794**
1795** Function         BTM_SetHoldMode
1796**
1797** Description      This function is called to set a connection into hold mode.
1798**                  A check is made if the connection is in sniff or park mode,
1799**                  and if yes, the hold mode is ignored.
1800**
1801** Returns          status of the operation
1802**
1803*******************************************************************************/
1804tBTM_STATUS BTM_SetHoldMode (BD_ADDR remote_bda, UINT16 min_interval, UINT16 max_interval)
1805{
1806    tACL_CONN   *p;
1807
1808    BTM_TRACE_DEBUG ("BTM_SetHoldMode");
1809    /* First, check if hold mode is supported */
1810    if (!HCI_HOLD_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1811        return(BTM_MODE_UNSUPPORTED);
1812
1813    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1814    if (p != (tACL_CONN *)NULL)
1815    {
1816        /* If the connection is in park or sniff mode, forget about holding it */
1817        if (p->mode != BTM_ACL_MODE_NORMAL)
1818            return(BTM_SUCCESS);
1819
1820        if (!btsnd_hcic_hold_mode (p->hci_handle, max_interval, min_interval))
1821            return(BTM_NO_RESOURCES);
1822
1823        return(BTM_CMD_STARTED);
1824    }
1825
1826    /* If here, no BD Addr found */
1827    return(BTM_UNKNOWN_ADDR);
1828}
1829
1830
1831/*******************************************************************************
1832**
1833** Function         BTM_SetSniffMode
1834**
1835** Description      This function is called to set a connection into sniff mode.
1836**                  A check is made if the connection is already in sniff or park
1837**                  mode, and if yes, the sniff mode is ignored.
1838**
1839** Returns          status of the operation
1840**
1841*******************************************************************************/
1842tBTM_STATUS BTM_SetSniffMode (BD_ADDR remote_bda, UINT16 min_period, UINT16 max_period,
1843                              UINT16 attempt, UINT16 timeout)
1844{
1845    tACL_CONN   *p;
1846    BTM_TRACE_DEBUG ("BTM_SetSniffMode");
1847    /* First, check if sniff mode is supported */
1848    if (!HCI_SNIFF_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1849        return(BTM_MODE_UNSUPPORTED);
1850
1851    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1852    if (p != (tACL_CONN *)NULL)
1853    {
1854        /* If the connection is in park mode, forget about sniffing it */
1855        if (p->mode != BTM_ACL_MODE_NORMAL)
1856            return(BTM_WRONG_MODE);
1857
1858        if (!btsnd_hcic_sniff_mode (p->hci_handle, max_period,
1859                                    min_period, attempt, timeout))
1860            return(BTM_NO_RESOURCES);
1861
1862        return(BTM_CMD_STARTED);
1863    }
1864
1865    /* If here, no BD Addr found */
1866    return(BTM_UNKNOWN_ADDR);
1867}
1868
1869
1870
1871
1872/*******************************************************************************
1873**
1874** Function         BTM_CancelSniffMode
1875**
1876** Description      This function is called to put a connection out of sniff mode.
1877**                  A check is made if the connection is already in sniff mode,
1878**                  and if not, the cancel sniff mode is ignored.
1879**
1880** Returns          status of the operation
1881**
1882*******************************************************************************/
1883tBTM_STATUS BTM_CancelSniffMode (BD_ADDR remote_bda)
1884{
1885    tACL_CONN   *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1886    BTM_TRACE_DEBUG ("BTM_CancelSniffMode ");
1887    if (p == (tACL_CONN *)NULL)
1888        return(BTM_UNKNOWN_ADDR);
1889
1890    /* If the connection is not in sniff mode, cannot cancel */
1891    if (p->mode != BTM_ACL_MODE_SNIFF)
1892        return(BTM_WRONG_MODE);
1893
1894    if (!btsnd_hcic_exit_sniff_mode (p->hci_handle))
1895        return(BTM_NO_RESOURCES);
1896
1897    return(BTM_CMD_STARTED);
1898}
1899
1900
1901/*******************************************************************************
1902**
1903** Function         BTM_SetParkMode
1904**
1905** Description      This function is called to set a connection into park mode.
1906**                  A check is made if the connection is already in sniff or park
1907**                  mode, and if yes, the park mode is ignored.
1908**
1909** Returns          status of the operation
1910**
1911*******************************************************************************/
1912tBTM_STATUS BTM_SetParkMode (BD_ADDR remote_bda, UINT16 beacon_min_period, UINT16 beacon_max_period)
1913{
1914    tACL_CONN   *p;
1915
1916    BTM_TRACE_DEBUG ("BTM_SetParkMode");
1917    /* First, check if park mode is supported */
1918    if (!HCI_PARK_MODE_SUPPORTED(BTM_ReadLocalFeatures()))
1919        return(BTM_MODE_UNSUPPORTED);
1920
1921    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1922    if (p != (tACL_CONN *)NULL)
1923    {
1924        /* If the connection is in sniff mode, forget about parking it */
1925        if (p->mode != BTM_ACL_MODE_NORMAL)
1926            return(BTM_WRONG_MODE);
1927
1928        /* no park mode if SCO exists -- CR#1982, 1.1 errata 1124
1929           command status event should be returned /w error code 0x0C "Command Disallowed"
1930           Let LM do this.
1931        */
1932        if (!btsnd_hcic_park_mode (p->hci_handle,
1933                                   beacon_max_period, beacon_min_period))
1934            return(BTM_NO_RESOURCES);
1935
1936        return(BTM_CMD_STARTED);
1937    }
1938
1939    /* If here, no BD Addr found */
1940    return(BTM_UNKNOWN_ADDR);
1941}
1942
1943/*******************************************************************************
1944**
1945** Function         BTM_CancelParkMode
1946**
1947** Description      This function is called to put a connection out of park mode.
1948**                  A check is made if the connection is already in park mode,
1949**                  and if not, the cancel sniff mode is ignored.
1950**
1951** Returns          status of the operation
1952**
1953*******************************************************************************/
1954tBTM_STATUS BTM_CancelParkMode (BD_ADDR remote_bda)
1955{
1956    tACL_CONN   *p;
1957
1958    BTM_TRACE_DEBUG ("BTM_CancelParkMode");
1959    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1960    if (p != (tACL_CONN *)NULL)
1961    {
1962        /* If the connection is not in park mode, cannot cancel */
1963        if (p->mode != BTM_ACL_MODE_PARK)
1964            return(BTM_WRONG_MODE);
1965
1966        if (!btsnd_hcic_exit_park_mode (p->hci_handle))
1967            return(BTM_NO_RESOURCES);
1968
1969        return(BTM_CMD_STARTED);
1970    }
1971
1972    /* If here, no BD Addr found */
1973    return(BTM_UNKNOWN_ADDR);
1974}
1975#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
1976
1977
1978/*******************************************************************************
1979**
1980** Function         BTM_SetPacketTypes
1981**
1982** Description      This function is set the packet types used for a specific
1983**                  ACL connection,
1984**
1985** Returns          status of the operation
1986**
1987*******************************************************************************/
1988tBTM_STATUS BTM_SetPacketTypes (BD_ADDR remote_bda, UINT16 pkt_types)
1989{
1990    tACL_CONN   *p;
1991    BTM_TRACE_DEBUG ("BTM_SetPacketTypes");
1992
1993    if ((p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
1994        return(btm_set_packet_types (p, pkt_types));
1995
1996    /* If here, no BD Addr found */
1997    return(BTM_UNKNOWN_ADDR);
1998}
1999
2000
2001/*******************************************************************************
2002**
2003** Function         BTM_ReadPacketTypes
2004**
2005** Description      This function is set the packet types used for a specific
2006**                  ACL connection,
2007**
2008** Returns          packet types supported for the connection, or 0 if no BD address
2009**
2010*******************************************************************************/
2011UINT16 BTM_ReadPacketTypes (BD_ADDR remote_bda)
2012{
2013    tACL_CONN   *p;
2014
2015    BTM_TRACE_DEBUG ("BTM_ReadPacketTypes");
2016    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2017    if (p != (tACL_CONN *)NULL)
2018    {
2019        return(p->pkt_types_mask);
2020    }
2021
2022    /* If here, no BD Addr found */
2023    return(0);
2024}
2025
2026
2027/*******************************************************************************
2028**
2029** Function         BTM_ReadAclMode
2030**
2031** Description      This returns the current mode for a specific
2032**                  ACL connection.
2033**
2034** Input Param      remote_bda - device address of desired ACL connection
2035**
2036** Output Param     p_mode - address where the current mode is copied into.
2037**                          BTM_ACL_MODE_NORMAL
2038**                          BTM_ACL_MODE_HOLD
2039**                          BTM_ACL_MODE_SNIFF
2040**                          BTM_ACL_MODE_PARK
2041**                          (valid only if return code is BTM_SUCCESS)
2042**
2043** Returns          BTM_SUCCESS if successful,
2044**                  BTM_UNKNOWN_ADDR if bd addr is not active or bad
2045**
2046*******************************************************************************/
2047#if BTM_PWR_MGR_INCLUDED == FALSE
2048tBTM_STATUS BTM_ReadAclMode (BD_ADDR remote_bda, UINT8 *p_mode)
2049{
2050    tACL_CONN   *p;
2051
2052    BTM_TRACE_API ("BTM_ReadAclMode: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2053                    remote_bda[0], remote_bda[1], remote_bda[2],
2054                    remote_bda[3], remote_bda[4], remote_bda[5]);
2055
2056    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2057    if (p != (tACL_CONN *)NULL)
2058    {
2059        *p_mode = p->mode;
2060        return(BTM_SUCCESS);
2061    }
2062
2063    /* If here, no BD Addr found */
2064    return(BTM_UNKNOWN_ADDR);
2065}
2066#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2067
2068/*******************************************************************************
2069**
2070** Function         BTM_ReadClockOffset
2071**
2072** Description      This returns the clock offset for a specific
2073**                  ACL connection.
2074**
2075** Input Param      remote_bda - device address of desired ACL connection
2076**
2077** Returns          clock-offset or 0 if unknown
2078**
2079*******************************************************************************/
2080UINT16 BTM_ReadClockOffset (BD_ADDR remote_bda)
2081{
2082    tACL_CONN   *p;
2083
2084    BTM_TRACE_API ("BTM_ReadClockOffset: 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 ( (p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR)) != NULL)
2089        return(p->clock_offset);
2090
2091    /* If here, no BD Addr found */
2092    return(0);
2093}
2094
2095/*******************************************************************************
2096**
2097** Function         BTM_IsAclConnectionUp
2098**
2099** Description      This function is called to check if an ACL connection exists
2100**                  to a specific remote BD Address.
2101**
2102** Returns          TRUE if connection is up, else FALSE.
2103**
2104*******************************************************************************/
2105BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
2106{
2107    tACL_CONN   *p;
2108
2109    BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2110                    remote_bda[0], remote_bda[1], remote_bda[2],
2111                    remote_bda[3], remote_bda[4], remote_bda[5]);
2112
2113    p = btm_bda_to_acl(remote_bda, transport);
2114    if (p != (tACL_CONN *)NULL)
2115    {
2116        return(TRUE);
2117    }
2118
2119    /* If here, no BD Addr found */
2120    return(FALSE);
2121}
2122
2123/*******************************************************************************
2124**
2125** Function         BTM_GetNumAclLinks
2126**
2127** Description      This function is called to count the number of
2128**                  ACL links that are active.
2129**
2130** Returns          UINT16  Number of active ACL links
2131**
2132*******************************************************************************/
2133UINT16 BTM_GetNumAclLinks (void)
2134{
2135#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2136    return(UINT16)btm_cb.num_acl;
2137#else
2138    tACL_CONN   *p = &btm_cb.acl_db[0];
2139    UINT16      xx, yy;
2140    BTM_TRACE_DEBUG ("BTM_GetNumAclLinks");
2141    for (xx = yy = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
2142    {
2143        if (p->in_use)
2144            yy++;
2145    }
2146
2147    return(yy);
2148#endif
2149}
2150
2151/*******************************************************************************
2152**
2153** Function         BTM_GetNumLeLinks
2154**
2155** Description      This function is called to count the number of
2156**                   LE ACL links that are active.
2157**
2158** Returns          UINT16  Number of active LE links
2159**
2160*******************************************************************************/
2161UINT16 BTM_GetNumLeLinks (void)
2162{
2163    UINT16 yy = 0;
2164
2165#if BLE_INCLUDED == TRUE
2166    tACL_CONN   *p = &btm_cb.acl_db[0];
2167    UINT16      xx;
2168    BTM_TRACE_DEBUG ("BTM_GetNumLeLinks");
2169    for (xx = yy = 0; xx < MAX_L2CAP_LINKS; xx++, p++)
2170    {
2171        if  ((p->in_use) &&(p->transport == BT_TRANSPORT_LE))
2172            yy++;
2173    }
2174#endif
2175
2176    return(yy);
2177}
2178
2179/*******************************************************************************
2180**
2181** Function         btm_get_acl_disc_reason_code
2182**
2183** Description      This function is called to get the disconnection reason code
2184**                  returned by the HCI at disconnection complete event.
2185**
2186** Returns          TRUE if connection is up, else FALSE.
2187**
2188*******************************************************************************/
2189UINT16 btm_get_acl_disc_reason_code (void)
2190{
2191    UINT8 res = btm_cb.acl_disc_reason;
2192    BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code");
2193    return(res);
2194}
2195
2196
2197/*******************************************************************************
2198**
2199** Function         BTM_GetHCIConnHandle
2200**
2201** Description      This function is called to get the handle for an ACL connection
2202**                  to a specific remote BD Address.
2203**
2204** Returns          the handle of the connection, or 0xFFFF if none.
2205**
2206*******************************************************************************/
2207UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
2208{
2209    tACL_CONN   *p;
2210    BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle");
2211    p = btm_bda_to_acl(remote_bda, transport);
2212    if (p != (tACL_CONN *)NULL)
2213    {
2214        return(p->hci_handle);
2215    }
2216
2217    /* If here, no BD Addr found */
2218    return(0xFFFF);
2219}
2220
2221#if BTM_PWR_MGR_INCLUDED == FALSE
2222/*******************************************************************************
2223**
2224** Function         btm_process_mode_change
2225**
2226** Description      This function is called when an HCI mode change event occurs.
2227**
2228** Input Parms      hci_status - status of the event (HCI_SUCCESS if no errors)
2229**                  hci_handle - connection handle associated with the change
2230**                  mode - HCI_MODE_ACTIVE, HCI_MODE_HOLD, HCI_MODE_SNIFF, or HCI_MODE_PARK
2231**                  interval - number of baseband slots (meaning depends on mode)
2232**
2233** Returns          void
2234**
2235*******************************************************************************/
2236void btm_process_mode_change (UINT8 hci_status, UINT16 hci_handle, UINT8 mode, UINT16 interval)
2237{
2238    tACL_CONN        *p;
2239    UINT8             xx;
2240    BTM_TRACE_DEBUG ("btm_process_mode_change");
2241    if (hci_status != HCI_SUCCESS)
2242    {
2243        BTM_TRACE_WARNING ("BTM: HCI Mode Change Error Status: 0x%02x", hci_status);
2244    }
2245
2246    /* Look up the connection by handle and set the current mode */
2247    xx = btm_handle_to_acl_index(hci_handle);
2248
2249    /* don't assume that we can never get a bad hci_handle */
2250    if (xx >= MAX_L2CAP_LINKS)
2251        return;
2252
2253    p = &btm_cb.acl_db[xx];
2254
2255    /* If status is not success mode does not mean anything */
2256    if (hci_status == HCI_SUCCESS)
2257        p->mode = mode;
2258
2259    /* If mode change was because of an active role switch or change link key */
2260    btm_cont_rswitch_or_chglinkkey(p, btm_find_dev(p->remote_addr), hci_status);
2261}
2262#endif /* BTM_PWR_MGR_INCLUDED == FALSE */
2263
2264/*******************************************************************************
2265**
2266** Function         btm_process_clk_off_comp_evt
2267**
2268** Description      This function is called when clock offset command completes.
2269**
2270** Input Parms      hci_handle - connection handle associated with the change
2271**                  clock offset
2272**
2273** Returns          void
2274**
2275*******************************************************************************/
2276void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
2277{
2278    UINT8      xx;
2279    BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt");
2280    /* Look up the connection by handle and set the current mode */
2281    if ((xx = btm_handle_to_acl_index(hci_handle)) < MAX_L2CAP_LINKS)
2282        btm_cb.acl_db[xx].clock_offset = clock_offset;
2283}
2284
2285/*******************************************************************************
2286**
2287** Function         btm_acl_role_changed
2288**
2289** Description      This function is called whan a link's master/slave role change
2290**                  event or command status event (with error) is received.
2291**                  It updates the link control block, and calls
2292**                  the registered callback with status and role (if registered).
2293**
2294** Returns          void
2295**
2296*******************************************************************************/
2297void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
2298{
2299    UINT8                   *p_bda = (bd_addr) ? bd_addr :
2300                                        btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
2301    tACL_CONN               *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
2302    tBTM_ROLE_SWITCH_CMPL   *p_data = &btm_cb.devcb.switch_role_ref_data;
2303    tBTM_SEC_DEV_REC        *p_dev_rec;
2304#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2305    tBTM_BL_ROLE_CHG_DATA   evt;
2306#endif
2307
2308    BTM_TRACE_DEBUG ("btm_acl_role_changed");
2309    /* Ignore any stray events */
2310    if (p == NULL)
2311    {
2312        /* it could be a failure */
2313        if (hci_status != HCI_SUCCESS)
2314            btm_acl_report_role_change(hci_status, bd_addr);
2315        return;
2316    }
2317
2318    p_data->hci_status = hci_status;
2319
2320    if (hci_status == HCI_SUCCESS)
2321    {
2322        p_data->role = new_role;
2323        memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
2324
2325        /* Update cached value */
2326        p->link_role = new_role;
2327
2328        /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
2329        if (new_role == BTM_ROLE_MASTER)
2330        {
2331            BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
2332        }
2333    }
2334    else
2335    {
2336        /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
2337        new_role = p->link_role;
2338    }
2339
2340    /* Check if any SCO req is pending for role change */
2341    btm_sco_chk_pend_rolechange (p->hci_handle);
2342
2343    /* if switching state is switching we need to turn encryption on */
2344    /* if idle, we did not change encryption */
2345    if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING)
2346    {
2347        /* Make sure there's not also a change link key going on before re-enabling */
2348        if (p->change_key_state != BTM_ACL_SWKEY_STATE_SWITCHING)
2349        {
2350            if (btsnd_hcic_set_conn_encrypt (p->hci_handle, TRUE))
2351            {
2352                p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
2353                p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2354                return;
2355            }
2356        }
2357        else    /* Set the state and wait for change link key */
2358        {
2359            p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
2360            return;
2361        }
2362    }
2363
2364    /* Set the switch_role_state to IDLE since the reply received from HCI */
2365    /* regardless of its result either success or failed. */
2366    if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS)
2367    {
2368        p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2369        p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
2370    }
2371
2372    /* if role switch complete is needed, report it now */
2373    btm_acl_report_role_change(hci_status, bd_addr);
2374
2375#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2376    /* if role change event is registered, report it now */
2377    if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK))
2378    {
2379        evt.event       = BTM_BL_ROLE_CHG_EVT;
2380        evt.new_role    = new_role;
2381        evt.p_bda       = p_bda;
2382        evt.hci_status  = hci_status;
2383        (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
2384    }
2385
2386    BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d",
2387                     p_data->role, p_data->hci_status, p->switch_role_state);
2388#endif
2389
2390#if BTM_DISC_DURING_RS == TRUE
2391    /* If a disconnect is pending, issue it now that role switch has completed */
2392    if ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
2393    {
2394        if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING)
2395        {
2396            BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!");
2397            btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
2398        }
2399        BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
2400                         (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
2401        p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;     /* reset flag */
2402    }
2403
2404#endif
2405
2406}
2407
2408#if (RFCOMM_INCLUDED==TRUE)
2409/*******************************************************************************
2410**
2411** Function         BTM_AllocateSCN
2412**
2413** Description      Look through the Server Channel Numbers for a free one.
2414**
2415** Returns          Allocated SCN number or 0 if none.
2416**
2417*******************************************************************************/
2418
2419UINT8 BTM_AllocateSCN(void)
2420{
2421    UINT8   x;
2422    BTM_TRACE_DEBUG ("BTM_AllocateSCN");
2423
2424    // stack reserves scn 1 for HFP, HSP we still do the correct way
2425    for (x = 1; x < BTM_MAX_SCN; x++)
2426    {
2427        if (!btm_cb.btm_scn[x])
2428        {
2429            btm_cb.btm_scn[x] = TRUE;
2430            return(x+1);
2431        }
2432    }
2433
2434    return(0);     /* No free ports */
2435}
2436
2437/*******************************************************************************
2438**
2439** Function         BTM_TryAllocateSCN
2440**
2441** Description      Try to allocate a fixed server channel
2442**
2443** Returns          Returns TRUE if server channel was available
2444**
2445*******************************************************************************/
2446
2447BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
2448{
2449    UINT8   x;
2450
2451    /* Make sure we don't exceed max port range.
2452     * Stack reserves scn 1 for HFP, HSP we still do the correct way.
2453     */
2454    if ( (scn>=BTM_MAX_SCN) || (scn == 1) )
2455        return FALSE;
2456
2457    /* check if this port is available */
2458    if (!btm_cb.btm_scn[scn-1])
2459    {
2460        btm_cb.btm_scn[scn-1] = TRUE;
2461        return TRUE;
2462    }
2463
2464    return (FALSE);     /* Port was busy */
2465}
2466
2467/*******************************************************************************
2468**
2469** Function         BTM_FreeSCN
2470**
2471** Description      Free the specified SCN.
2472**
2473** Returns          TRUE or FALSE
2474**
2475*******************************************************************************/
2476BOOLEAN BTM_FreeSCN(UINT8 scn)
2477{
2478    BTM_TRACE_DEBUG ("BTM_FreeSCN ");
2479    if (scn <= BTM_MAX_SCN)
2480    {
2481        btm_cb.btm_scn[scn-1] = FALSE;
2482        return(TRUE);
2483    }
2484    else
2485        return(FALSE);      /* Illegal SCN passed in */
2486}
2487
2488#else
2489
2490/* Make dummy functions for the RPC to link against */
2491UINT8 BTM_AllocateSCN(void)
2492{
2493    return(0);
2494}
2495
2496BOOLEAN BTM_FreeSCN(UINT8 scn)
2497{
2498    return(FALSE);
2499}
2500
2501#endif
2502
2503
2504/*******************************************************************************
2505**
2506** Function         btm_acl_timeout
2507**
2508** Description      This function is called when a timer list entry expires.
2509**
2510** Returns          void
2511**
2512*******************************************************************************/
2513void btm_acl_timeout (TIMER_LIST_ENT  *p_tle)
2514{
2515    UINT32 timer_type = p_tle->param;
2516
2517    BTM_TRACE_DEBUG ("btm_acl_timeout");
2518    if (timer_type == TT_DEV_RLNKP)
2519    {
2520        tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_rlinkp_cmpl_cb;
2521        tBTM_LNK_POLICY_RESULTS  lnkpol;
2522
2523        lnkpol.status = BTM_ERR_PROCESSING;
2524        lnkpol.settings = 0;
2525
2526        btm_cb.devcb.p_rlinkp_cmpl_cb = NULL;
2527
2528        if (p_cb)
2529            (*p_cb)(&lnkpol);
2530    }
2531}
2532
2533/*******************************************************************************
2534**
2535** Function         btm_set_packet_types
2536**
2537** Description      This function sets the packet types used for a specific
2538**                  ACL connection. It is called internally by btm_acl_created
2539**                  or by an application/profile by BTM_SetPacketTypes.
2540**
2541** Returns          status of the operation
2542**
2543*******************************************************************************/
2544tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
2545{
2546    UINT16 temp_pkt_types;
2547    BTM_TRACE_DEBUG ("btm_set_packet_types");
2548    /* Save in the ACL control blocks, types that we support */
2549    temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
2550                      btm_cb.btm_acl_pkt_types_supported);
2551
2552    /* OR in any exception packet types if at least 2.0 version of spec */
2553    if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
2554    {
2555        temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
2556                           (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
2557    }
2558    else
2559    {
2560        temp_pkt_types &= (~BTM_ACL_EXCEPTION_PKTS_MASK);
2561    }
2562
2563    /* Exclude packet types not supported by the peer */
2564    btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
2565
2566    BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x", temp_pkt_types);
2567
2568    if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types))
2569    {
2570        return(BTM_NO_RESOURCES);
2571    }
2572
2573    p->pkt_types_mask = temp_pkt_types;
2574
2575    return(BTM_CMD_STARTED);
2576}
2577
2578/*******************************************************************************
2579**
2580** Function         btm_get_max_packet_size
2581**
2582** Returns          Returns maximum packet size that can be used for current
2583**                  connection, 0 if connection is not established
2584**
2585*******************************************************************************/
2586UINT16 btm_get_max_packet_size (BD_ADDR addr)
2587{
2588    tACL_CONN   *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2589    UINT16      pkt_types = 0;
2590    UINT16      pkt_size = 0;
2591    BTM_TRACE_DEBUG ("btm_get_max_packet_size");
2592    if (p != NULL)
2593    {
2594        pkt_types = p->pkt_types_mask;
2595    }
2596    else
2597    {
2598        /* Special case for when info for the local device is requested */
2599        if (memcmp (btm_cb.devcb.local_addr, addr, BD_ADDR_LEN) == 0)
2600        {
2601            pkt_types = btm_cb.btm_acl_pkt_types_supported;
2602        }
2603    }
2604
2605    if (pkt_types)
2606    {
2607        if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5))
2608            pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
2609        else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5))
2610            pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
2611        else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3))
2612            pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
2613        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5)
2614            pkt_size = HCI_DH5_PACKET_SIZE;
2615        else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3))
2616            pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
2617        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5)
2618            pkt_size = HCI_DM5_PACKET_SIZE;
2619        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3)
2620            pkt_size = HCI_DH3_PACKET_SIZE;
2621        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3)
2622            pkt_size = HCI_DM3_PACKET_SIZE;
2623        else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1))
2624            pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
2625        else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1))
2626            pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
2627        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1)
2628            pkt_size = HCI_DH1_PACKET_SIZE;
2629        else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1)
2630            pkt_size = HCI_DM1_PACKET_SIZE;
2631    }
2632
2633   return(pkt_size);
2634}
2635
2636/*******************************************************************************
2637**
2638** Function         BTM_ReadRemoteVersion
2639**
2640** Returns          If connected report peer device info
2641**
2642*******************************************************************************/
2643tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
2644                                   UINT16 *manufacturer, UINT16 *lmp_sub_version)
2645{
2646    tACL_CONN        *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2647    BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion");
2648    if (p == NULL)
2649        return(BTM_UNKNOWN_ADDR);
2650
2651    if (lmp_version)
2652        *lmp_version = p->lmp_version;
2653
2654    if (manufacturer)
2655        *manufacturer = p->manufacturer;
2656
2657    if (lmp_sub_version)
2658        *lmp_sub_version = p->lmp_subversion;
2659
2660    return(BTM_SUCCESS);
2661}
2662
2663/*******************************************************************************
2664**
2665** Function         BTM_ReadRemoteFeatures
2666**
2667** Returns          pointer to the remote supported features mask (8 bytes)
2668**
2669*******************************************************************************/
2670UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
2671{
2672    tACL_CONN        *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2673    BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures");
2674    if (p == NULL)
2675    {
2676        return(NULL);
2677    }
2678
2679    return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
2680}
2681
2682/*******************************************************************************
2683**
2684** Function         BTM_ReadRemoteExtendedFeatures
2685**
2686** Returns          pointer to the remote extended features mask (8 bytes)
2687**                  or NULL if bad page
2688**
2689*******************************************************************************/
2690UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
2691{
2692    tACL_CONN        *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2693    BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures");
2694    if (p == NULL)
2695    {
2696        return(NULL);
2697    }
2698
2699    if (page_number > HCI_EXT_FEATURES_PAGE_MAX)
2700    {
2701        BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown", page_number);
2702        return NULL;
2703    }
2704
2705    return(p->peer_lmp_features[page_number]);
2706}
2707
2708/*******************************************************************************
2709**
2710** Function         BTM_ReadNumberRemoteFeaturesPages
2711**
2712** Returns          number of features pages read from the remote device.
2713**
2714*******************************************************************************/
2715UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
2716{
2717    tACL_CONN        *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2718    BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages");
2719    if (p == NULL)
2720    {
2721        return(0);
2722    }
2723
2724    return(p->num_read_pages);
2725}
2726
2727/*******************************************************************************
2728**
2729** Function         BTM_ReadAllRemoteFeatures
2730**
2731** Returns          pointer to all features of the remote (24 bytes).
2732**
2733*******************************************************************************/
2734UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
2735{
2736    tACL_CONN        *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
2737    BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures");
2738    if (p == NULL)
2739    {
2740        return(NULL);
2741    }
2742
2743    return(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
2744}
2745
2746/*******************************************************************************
2747**
2748** Function         BTM_RegBusyLevelNotif
2749**
2750** Description      This function is called to register a callback to receive
2751**                  busy level change events.
2752**
2753** Returns          BTM_SUCCESS if successfully registered, otherwise error
2754**
2755*******************************************************************************/
2756#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
2757tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
2758                                   tBTM_BL_EVENT_MASK evt_mask)
2759{
2760    BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif");
2761    if (p_level)
2762        *p_level = btm_cb.busy_level;
2763
2764    btm_cb.bl_evt_mask = evt_mask;
2765
2766    if (!p_cb)
2767        btm_cb.p_bl_changed_cb = NULL;
2768    else if (btm_cb.p_bl_changed_cb)
2769        return(BTM_BUSY);
2770    else
2771        btm_cb.p_bl_changed_cb = p_cb;
2772
2773    return(BTM_SUCCESS);
2774}
2775#else
2776/*******************************************************************************
2777**
2778** Function         BTM_AclRegisterForChanges
2779**
2780** Returns          This function is called to register a callback for when the
2781**                  ACL database changes, i.e. new entry or entry deleted.
2782**
2783*******************************************************************************/
2784tBTM_STATUS BTM_AclRegisterForChanges (tBTM_ACL_DB_CHANGE_CB *p_cb)
2785{
2786    BTM_TRACE_DEBUG ("BTM_AclRegisterForChanges");
2787    if (!p_cb)
2788        btm_cb.p_acl_changed_cb = NULL;
2789    else if (btm_cb.p_acl_changed_cb)
2790        return(BTM_BUSY);
2791    else
2792        btm_cb.p_acl_changed_cb = p_cb;
2793
2794    return(BTM_SUCCESS);
2795}
2796#endif
2797
2798/*******************************************************************************
2799**
2800** Function         BTM_SetQoS
2801**
2802** Description      This function is called to setup QoS
2803**
2804** Returns          status of the operation
2805**
2806*******************************************************************************/
2807tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
2808{
2809    tACL_CONN   *p = &btm_cb.acl_db[0];
2810
2811    BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x",
2812                    bd[0], bd[1], bd[2],
2813                    bd[3], bd[4], bd[5]);
2814
2815    /* If someone already waiting on the version, do not allow another */
2816    if (btm_cb.devcb.p_qossu_cmpl_cb)
2817        return(BTM_BUSY);
2818
2819    if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL)
2820    {
2821        btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
2822        btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
2823
2824        if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
2825                                   p_flow->token_rate, p_flow->peak_bandwidth,
2826                                   p_flow->latency,p_flow->delay_variation))
2827        {
2828            btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2829            btu_stop_timer(&btm_cb.devcb.qossu_timer);
2830            return(BTM_NO_RESOURCES);
2831        }
2832        else
2833            return(BTM_CMD_STARTED);
2834    }
2835
2836    /* If here, no BD Addr found */
2837    return(BTM_UNKNOWN_ADDR);
2838}
2839
2840/*******************************************************************************
2841**
2842** Function         btm_qos_setup_complete
2843**
2844** Description      This function is called when the command complete message
2845**                  is received from the HCI for the qos setup request.
2846**
2847** Returns          void
2848**
2849*******************************************************************************/
2850void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
2851{
2852    tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
2853    tBTM_QOS_SETUP_CMPL     qossu;
2854    BTM_TRACE_DEBUG ("btm_qos_setup_complete");
2855    btu_stop_timer (&btm_cb.devcb.qossu_timer);
2856
2857    btm_cb.devcb.p_qossu_cmpl_cb = NULL;
2858
2859    if (p_cb)
2860    {
2861        memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
2862        qossu.status = status;
2863        qossu.handle = handle;
2864        if (p_flow != NULL)
2865        {
2866            qossu.flow.qos_flags = p_flow->qos_flags;
2867            qossu.flow.service_type = p_flow->service_type;
2868            qossu.flow.token_rate = p_flow->token_rate;
2869            qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
2870            qossu.flow.latency = p_flow->latency;
2871            qossu.flow.delay_variation = p_flow->delay_variation;
2872        }
2873        BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x",
2874                          qossu.flow.delay_variation);
2875        (*p_cb)(&qossu);
2876    }
2877}
2878
2879
2880/*******************************************************************************
2881**
2882** Function         BTM_ReadRSSI
2883**
2884** Description      This function is called to read the link policy settings.
2885**                  The address of link policy results are returned in the callback.
2886**                  (tBTM_RSSI_RESULTS)
2887**
2888** Returns          BTM_CMD_STARTED if successfully initiated or error code
2889**
2890*******************************************************************************/
2891tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2892{
2893    tACL_CONN   *p;
2894    tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
2895#if BLE_INCLUDED == TRUE
2896    tBT_DEVICE_TYPE dev_type;
2897    tBLE_ADDR_TYPE  addr_type;
2898#endif
2899    BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2900                    remote_bda[0], remote_bda[1], remote_bda[2],
2901                    remote_bda[3], remote_bda[4], remote_bda[5]);
2902
2903    /* If someone already waiting on the version, do not allow another */
2904    if (btm_cb.devcb.p_rssi_cmpl_cb)
2905        return(BTM_BUSY);
2906
2907#if BLE_INCLUDED == TRUE
2908    BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
2909    if (dev_type == BT_DEVICE_TYPE_BLE)
2910        transport = BT_TRANSPORT_LE;
2911#endif
2912
2913    p = btm_bda_to_acl(remote_bda, transport);
2914    if (p != (tACL_CONN *)NULL)
2915    {
2916        btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
2917                         BTM_DEV_REPLY_TIMEOUT);
2918
2919        btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
2920
2921        if (!btsnd_hcic_read_rssi (p->hci_handle))
2922        {
2923            btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2924            btu_stop_timer (&btm_cb.devcb.rssi_timer);
2925            return(BTM_NO_RESOURCES);
2926        }
2927        else
2928            return(BTM_CMD_STARTED);
2929    }
2930
2931    /* If here, no BD Addr found */
2932    return(BTM_UNKNOWN_ADDR);
2933}
2934
2935/*******************************************************************************
2936**
2937** Function         BTM_ReadLinkQuality
2938**
2939** Description      This function is called to read the link qulaity.
2940**                  The value of the link quality is returned in the callback.
2941**                  (tBTM_LINK_QUALITY_RESULTS)
2942**
2943** Returns          BTM_CMD_STARTED if successfully initiated or error code
2944**
2945*******************************************************************************/
2946tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2947{
2948    tACL_CONN   *p;
2949
2950    BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2951                    remote_bda[0], remote_bda[1], remote_bda[2],
2952                    remote_bda[3], remote_bda[4], remote_bda[5]);
2953
2954    /* If someone already waiting on the version, do not allow another */
2955    if (btm_cb.devcb.p_lnk_qual_cmpl_cb)
2956        return(BTM_BUSY);
2957
2958    p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2959    if (p != (tACL_CONN *)NULL)
2960    {
2961        btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
2962                         BTM_DEV_REPLY_TIMEOUT);
2963        btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
2964
2965        if (!btsnd_hcic_get_link_quality (p->hci_handle))
2966        {
2967            btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2968            btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2969            return(BTM_NO_RESOURCES);
2970        }
2971        else
2972            return(BTM_CMD_STARTED);
2973    }
2974
2975    /* If here, no BD Addr found */
2976    return(BTM_UNKNOWN_ADDR);
2977}
2978
2979/*******************************************************************************
2980**
2981** Function         BTM_ReadTxPower
2982**
2983** Description      This function is called to read the current
2984**                  TX power of the connection. The tx power level results
2985**                  are returned in the callback.
2986**                  (tBTM_RSSI_RESULTS)
2987**
2988** Returns          BTM_CMD_STARTED if successfully initiated or error code
2989**
2990*******************************************************************************/
2991tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
2992{
2993    tACL_CONN   *p;
2994    BOOLEAN     ret;
2995#define BTM_READ_RSSI_TYPE_CUR  0x00
2996#define BTM_READ_RSSI_TYPE_MAX  0X01
2997
2998    BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x",
2999                    remote_bda[0], remote_bda[1], remote_bda[2],
3000                    remote_bda[3], remote_bda[4], remote_bda[5]);
3001
3002    /* If someone already waiting on the version, do not allow another */
3003    if (btm_cb.devcb.p_tx_power_cmpl_cb)
3004        return(BTM_BUSY);
3005
3006    p = btm_bda_to_acl(remote_bda, transport);
3007    if (p != (tACL_CONN *)NULL)
3008    {
3009        btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
3010                         BTM_DEV_REPLY_TIMEOUT);
3011
3012        btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
3013
3014#if BLE_INCLUDED == TRUE
3015        if (p->transport == BT_TRANSPORT_LE)
3016        {
3017            memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
3018            ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
3019        }
3020        else
3021#endif
3022        {
3023            ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
3024        }
3025        if (!ret)
3026        {
3027            btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
3028            btu_stop_timer (&btm_cb.devcb.tx_power_timer);
3029            return(BTM_NO_RESOURCES);
3030        }
3031        else
3032            return(BTM_CMD_STARTED);
3033    }
3034
3035    /* If here, no BD Addr found */
3036    return (BTM_UNKNOWN_ADDR);
3037}
3038/*******************************************************************************
3039**
3040** Function         btm_read_tx_power_complete
3041**
3042** Description      This function is called when the command complete message
3043**                  is received from the HCI for the read tx power request.
3044**
3045** Returns          void
3046**
3047*******************************************************************************/
3048void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
3049{
3050    tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
3051    tBTM_TX_POWER_RESULTS   results;
3052    UINT16                   handle;
3053    tACL_CONN               *p_acl_cb = &btm_cb.acl_db[0];
3054    UINT16                   index;
3055    BTM_TRACE_DEBUG ("btm_read_tx_power_complete");
3056    btu_stop_timer (&btm_cb.devcb.tx_power_timer);
3057
3058    /* If there was a callback registered for read rssi, call it */
3059    btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
3060
3061    if (p_cb)
3062    {
3063        STREAM_TO_UINT8  (results.hci_status, p);
3064
3065        if (results.hci_status == HCI_SUCCESS)
3066        {
3067            results.status = BTM_SUCCESS;
3068
3069            if (!is_ble)
3070            {
3071                STREAM_TO_UINT16 (handle, p);
3072                STREAM_TO_UINT8 (results.tx_power, p);
3073
3074                /* Search through the list of active channels for the correct BD Addr */
3075                for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3076                {
3077                    if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3078                    {
3079                        memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3080                        break;
3081                    }
3082                }
3083            }
3084#if BLE_INCLUDED == TRUE
3085            else
3086            {
3087                STREAM_TO_UINT8 (results.tx_power, p);
3088                memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
3089            }
3090#endif
3091            BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x",
3092                                  results.tx_power, results.hci_status);
3093        }
3094        else
3095            results.status = BTM_ERR_PROCESSING;
3096
3097        (*p_cb)(&results);
3098    }
3099}
3100
3101/*******************************************************************************
3102**
3103** Function         btm_read_rssi_complete
3104**
3105** Description      This function is called when the command complete message
3106**                  is received from the HCI for the read rssi request.
3107**
3108** Returns          void
3109**
3110*******************************************************************************/
3111void btm_read_rssi_complete (UINT8 *p)
3112{
3113    tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
3114    tBTM_RSSI_RESULTS        results;
3115    UINT16                   handle;
3116    tACL_CONN               *p_acl_cb = &btm_cb.acl_db[0];
3117    UINT16                   index;
3118    BTM_TRACE_DEBUG ("btm_read_rssi_complete");
3119    btu_stop_timer (&btm_cb.devcb.rssi_timer);
3120
3121    /* If there was a callback registered for read rssi, call it */
3122    btm_cb.devcb.p_rssi_cmpl_cb = NULL;
3123
3124    if (p_cb)
3125    {
3126        STREAM_TO_UINT8  (results.hci_status, p);
3127
3128        if (results.hci_status == HCI_SUCCESS)
3129        {
3130            results.status = BTM_SUCCESS;
3131
3132            STREAM_TO_UINT16 (handle, p);
3133
3134            STREAM_TO_UINT8 (results.rssi, p);
3135            BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x",
3136                              results.rssi, results.hci_status);
3137
3138            /* Search through the list of active channels for the correct BD Addr */
3139            for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3140            {
3141                if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3142                {
3143                    memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3144                    break;
3145                }
3146            }
3147        }
3148        else
3149            results.status = BTM_ERR_PROCESSING;
3150
3151        (*p_cb)(&results);
3152    }
3153}
3154
3155/*******************************************************************************
3156**
3157** Function         btm_read_link_quality_complete
3158**
3159** Description      This function is called when the command complete message
3160**                  is received from the HCI for the read link quality.
3161**
3162** Returns          void
3163**
3164*******************************************************************************/
3165void btm_read_link_quality_complete (UINT8 *p)
3166{
3167    tBTM_CMPL_CB            *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
3168    tBTM_LINK_QUALITY_RESULTS results;
3169    UINT16                   handle;
3170    tACL_CONN               *p_acl_cb = &btm_cb.acl_db[0];
3171    UINT16                   index;
3172    BTM_TRACE_DEBUG ("btm_read_link_quality_complete");
3173    btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
3174
3175    /* If there was a callback registered for read rssi, call it */
3176    btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
3177
3178    if (p_cb)
3179    {
3180        STREAM_TO_UINT8  (results.hci_status, p);
3181
3182        if (results.hci_status == HCI_SUCCESS)
3183        {
3184            results.status = BTM_SUCCESS;
3185
3186            STREAM_TO_UINT16 (handle, p);
3187
3188            STREAM_TO_UINT8 (results.link_quality, p);
3189            BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x",
3190                              results.link_quality, results.hci_status);
3191
3192            /* Search through the list of active channels for the correct BD Addr */
3193            for (index = 0; index < MAX_L2CAP_LINKS; index++, p_acl_cb++)
3194            {
3195                if ((p_acl_cb->in_use) && (handle == p_acl_cb->hci_handle))
3196                {
3197                    memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
3198                    break;
3199                }
3200            }
3201        }
3202        else
3203            results.status = BTM_ERR_PROCESSING;
3204
3205        (*p_cb)(&results);
3206    }
3207}
3208
3209/*******************************************************************************
3210**
3211** Function         btm_remove_acl
3212**
3213** Description      This function is called to disconnect an ACL connection
3214**
3215** Returns          BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
3216**
3217*******************************************************************************/
3218tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
3219{
3220    UINT16  hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
3221    tBTM_STATUS status = BTM_SUCCESS;
3222
3223    BTM_TRACE_DEBUG ("btm_remove_acl");
3224#if BTM_DISC_DURING_RS == TRUE
3225    tBTM_SEC_DEV_REC  *p_dev_rec = btm_find_dev (bd_addr);
3226
3227    /* Role Switch is pending, postpone until completed */
3228    if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING))
3229    {
3230        p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
3231    }
3232    else    /* otherwise can disconnect right away */
3233#endif
3234    {
3235        if (hci_handle != 0xFFFF && p_dev_rec &&
3236             p_dev_rec->sec_state!= BTM_SEC_STATE_DISCONNECTING)
3237        {
3238            if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER))
3239                status = BTM_NO_RESOURCES;
3240        }
3241        else
3242            status = BTM_UNKNOWN_ADDR;
3243    }
3244
3245    return status;
3246}
3247
3248
3249/*******************************************************************************
3250**
3251** Function         BTM_SetTraceLevel
3252**
3253** Description      This function sets the trace level for BTM.  If called with
3254**                  a value of 0xFF, it simply returns the current trace level.
3255**
3256** Returns          The new or current trace level
3257**
3258*******************************************************************************/
3259UINT8 BTM_SetTraceLevel (UINT8 new_level)
3260{
3261    BTM_TRACE_DEBUG ("BTM_SetTraceLevel");
3262    if (new_level != 0xFF)
3263        btm_cb.trace_level = new_level;
3264
3265    return(btm_cb.trace_level);
3266}
3267
3268/*******************************************************************************
3269**
3270** Function         btm_cont_rswitch_or_chglinkkey
3271**
3272** Description      This function is called to continue processing an active
3273**                  role switch or change of link key procedure.  It first
3274**                  disables encryption if enabled and EPR is not supported
3275**
3276** Returns          void
3277**
3278*******************************************************************************/
3279void btm_cont_rswitch_or_chglinkkey (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
3280                                     UINT8 hci_status)
3281{
3282    BOOLEAN sw_ok = TRUE;
3283    BOOLEAN chlk_ok = TRUE;
3284    BTM_TRACE_DEBUG ("btm_cont_rswitch_or_chglinkkey ");
3285    /* Check to see if encryption needs to be turned off if pending
3286       change of link key or role switch */
3287    if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE ||
3288        p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3289    {
3290        /* Must turn off Encryption first if necessary */
3291        /* Some devices do not support switch or change of link key while encryption is on */
3292        if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
3293            && !BTM_EPR_AVAILABLE(p))
3294        {
3295            if (btsnd_hcic_set_conn_encrypt (p->hci_handle, FALSE))
3296            {
3297                p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
3298                if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3299                    p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3300
3301                if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3302                    p->change_key_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
3303            }
3304            else
3305            {
3306                /* Error occurred; set states back to Idle */
3307                if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3308                    sw_ok = FALSE;
3309
3310                if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3311                    chlk_ok = FALSE;
3312            }
3313        }
3314        else    /* Encryption not used or EPR supported, continue with switch
3315                   and/or change of link key */
3316        {
3317            if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3318            {
3319                p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3320#if BTM_DISC_DURING_RS == TRUE
3321                if (p_dev_rec)
3322                    p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
3323#endif
3324                sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
3325            }
3326
3327            if (p->change_key_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE)
3328            {
3329                p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
3330                chlk_ok = btsnd_hcic_change_link_key (p->hci_handle);
3331            }
3332        }
3333
3334        if (!sw_ok)
3335        {
3336            p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
3337            btm_acl_report_role_change(hci_status, p->remote_addr);
3338        }
3339
3340        if (!chlk_ok)
3341        {
3342            p->change_key_state = BTM_ACL_SWKEY_STATE_IDLE;
3343            if (btm_cb.devcb.p_chg_link_key_cb)
3344            {
3345                btm_cb.devcb.chg_link_key_ref_data.hci_status = hci_status;
3346                (*btm_cb.devcb.p_chg_link_key_cb)(&btm_cb.devcb.chg_link_key_ref_data);
3347                btm_cb.devcb.p_chg_link_key_cb = NULL;
3348            }
3349        }
3350    }
3351}
3352
3353/*******************************************************************************
3354**
3355** Function         btm_acl_resubmit_page
3356**
3357** Description      send pending page request
3358**
3359*******************************************************************************/
3360void btm_acl_resubmit_page (void)
3361{
3362    tBTM_SEC_DEV_REC *p_dev_rec;
3363    BT_HDR  *p_buf;
3364    UINT8   *pp;
3365    BD_ADDR bda;
3366    BTM_TRACE_DEBUG ("btm_acl_resubmit_page");
3367    /* If there were other page request schedule can start the next one */
3368    if ((p_buf = (BT_HDR *)GKI_dequeue (&btm_cb.page_queue)) != NULL)
3369    {
3370        /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
3371         * for both create_conn and rmt_name */
3372        pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
3373
3374        STREAM_TO_BDADDR (bda, pp);
3375
3376        p_dev_rec = btm_find_or_alloc_dev (bda);
3377
3378        memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr,   BD_ADDR_LEN);
3379        memcpy (btm_cb.connecting_dc,  p_dev_rec->dev_class, DEV_CLASS_LEN);
3380
3381        btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
3382    }
3383    else
3384        btm_cb.paging = FALSE;
3385}
3386
3387/*******************************************************************************
3388**
3389** Function         btm_acl_reset_paging
3390**
3391** Description      set paging to FALSE and free the page queue - called at hci_reset
3392**
3393*******************************************************************************/
3394void  btm_acl_reset_paging (void)
3395{
3396    BT_HDR *p;
3397    BTM_TRACE_DEBUG ("btm_acl_reset_paging");
3398    /* If we sent reset we are definitely not paging any more */
3399    while ((p = (BT_HDR *)GKI_dequeue(&btm_cb.page_queue)) != NULL)
3400        GKI_freebuf (p);
3401
3402    btm_cb.paging = FALSE;
3403}
3404
3405/*******************************************************************************
3406**
3407** Function         btm_acl_set_discing
3408**
3409** Description      set discing to the given value
3410**
3411*******************************************************************************/
3412void  btm_acl_set_discing (BOOLEAN discing)
3413{
3414    BTM_TRACE_DEBUG ("btm_acl_set_discing");
3415    btm_cb.discing = discing;
3416}
3417
3418/*******************************************************************************
3419**
3420** Function         btm_acl_paging
3421**
3422** Description      send a paging command or queue it in btm_cb
3423**
3424*******************************************************************************/
3425void  btm_acl_paging (BT_HDR *p, BD_ADDR bda)
3426{
3427    tBTM_SEC_DEV_REC *p_dev_rec;
3428
3429    BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x",
3430                      btm_cb.discing, btm_cb.paging,
3431                      (bda[0]<<16) + (bda[1]<<8) + bda[2], (bda[3]<<16) + (bda[4] << 8) + bda[5]);
3432    if (btm_cb.discing)
3433    {
3434        btm_cb.paging = TRUE;
3435        GKI_enqueue (&btm_cb.page_queue, p);
3436    }
3437    else
3438    {
3439        if (!BTM_ACL_IS_CONNECTED (bda))
3440        {
3441            BTM_TRACE_DEBUG ("connecting_bda: %06x%06x",
3442                              (btm_cb.connecting_bda[0]<<16) + (btm_cb.connecting_bda[1]<<8) +
3443                               btm_cb.connecting_bda[2],
3444                              (btm_cb.connecting_bda[3]<<16) + (btm_cb.connecting_bda[4] << 8) +
3445                               btm_cb.connecting_bda[5]);
3446            if (btm_cb.paging &&
3447                memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0)
3448            {
3449                GKI_enqueue (&btm_cb.page_queue, p);
3450            }
3451            else
3452            {
3453                p_dev_rec = btm_find_or_alloc_dev (bda);
3454                memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr,   BD_ADDR_LEN);
3455                memcpy (btm_cb.connecting_dc,  p_dev_rec->dev_class, DEV_CLASS_LEN);
3456
3457                btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3458            }
3459
3460            btm_cb.paging = TRUE;
3461        }
3462        else /* ACL is already up */
3463        {
3464            btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
3465        }
3466    }
3467}
3468
3469/*******************************************************************************
3470**
3471** Function         btm_acl_notif_conn_collision
3472**
3473** Description      Send connection collision event to upper layer if registered
3474**
3475** Returns          TRUE if sent out to upper layer,
3476**                  FALSE if BTM_BUSY_LEVEL_CHANGE_INCLUDED == FALSE, or no one
3477**                  needs the notification.
3478**
3479**          Note: Function only used if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
3480**
3481*******************************************************************************/
3482BOOLEAN  btm_acl_notif_conn_collision (BD_ADDR bda)
3483{
3484#if (BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE)
3485    tBTM_BL_EVENT_DATA  evt_data;
3486
3487    /* Report possible collision to the upper layer. */
3488    if (btm_cb.p_bl_changed_cb)
3489    {
3490        BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x",
3491                          bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
3492
3493        evt_data.event = BTM_BL_COLLISION_EVT;
3494        evt_data.conn.p_bda = bda;
3495
3496#if BLE_INCLUDED == TRUE
3497        evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
3498        evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
3499#endif
3500        (*btm_cb.p_bl_changed_cb)(&evt_data);
3501        return TRUE;
3502    }
3503    else
3504        return FALSE;
3505#else
3506    return FALSE;
3507#endif
3508}
3509
3510
3511/*******************************************************************************
3512**
3513** Function         btm_acl_chk_peer_pkt_type_support
3514**
3515** Description      Check if peer supports requested packets
3516**
3517*******************************************************************************/
3518void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
3519{
3520    /* 3 and 5 slot packets? */
3521    if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3522        *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 +BTM_ACL_PKT_TYPES_MASK_DM3);
3523
3524    if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3525        *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
3526
3527    /* If HCI version > 2.0, then also check EDR packet types */
3528    if (btm_cb.devcb.local_version.hci_version >= HCI_PROTO_VERSION_2_0)
3529    {
3530        /* 2 and 3 MPS support? */
3531        if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3532            /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
3533            *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
3534                                BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
3535
3536        if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3537            /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
3538            *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
3539                                BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
3540
3541        /* EDR 3 and 5 slot support? */
3542        if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
3543         || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3544        {
3545            if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3546                /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
3547                *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
3548
3549            if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
3550                /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
3551                *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
3552        }
3553    }
3554}
3555