OSDN Git Service

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