OSDN Git Service

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