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