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 0 */
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 = 1;
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 == 1
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 == 1
111 && transport == p_acl_db->transport
112 #endif
113 ) {
114 ret = 0;
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 = 0;
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 == 1
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 1, otherwise 0.
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 == 1
192 BOOLEAN st = 1;
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 0;
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 = 0;
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 0;
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 == 1
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 = 1;
271 p->hci_handle = hci_handle;
272 p->link_role = link_role;
273 p->link_up_issued = 0;
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 == 1
280 p->transport = transport;
281 #if BLE_PRIVACY_SPT == 1
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 == 1
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 == 1)
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 == 1)
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 == 1)
334 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
335 #endif ///CLASSIC_BT_INCLUDED == 1
336 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
337 #if (SMP_INCLUDED == 1)
338 btm_sec_set_peer_sec_caps(p, p_dev_rec);
339 #endif ///SMP_INCLUDED == 1
340 #if (CLASSIC_BT_INCLUDED == 1)
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 == 1
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 == 1)
358 /* If here, features are not known yet */
359 if (p_dev_rec && transport == BT_TRANSPORT_LE) {
360 #if BLE_PRIVACY_SPT == 1
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 == 1)
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 = 0;
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 = 0;
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 == 1
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 == 1)
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 = 1;
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 = 0;
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 = 1;
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 = 0;
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 = 0;
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 == 1
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 == 1)
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 == 1)
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 == 1
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 == 1) {
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, 0)) {
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 == 1
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 == 1
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 == 1
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 == 1)
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 == 1
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 p_acl_cb->data_len_updating = true;
958 btsnd_hcic_ble_set_data_length(p_acl_cb->hci_handle, data_length, data_txtime);
959 }
960 l2cble_notify_le_connection (p_acl_cb->remote_addr);
961 } else {
962 //slave role, read remote feature
963 btsnd_hcic_ble_read_remote_feat(p_acl_cb->hci_handle);
964 }
965 }
966 #endif
967 }
968 }
969
970 /*******************************************************************************
971 **
972 ** Function btm_process_remote_ext_features
973 **
974 ** Description Local function called to process all extended features pages
975 ** read from a remote device.
976 **
977 ** Returns void
978 **
979 *******************************************************************************/
btm_process_remote_ext_features(tACL_CONN * p_acl_cb,UINT8 num_read_pages)980 void btm_process_remote_ext_features (tACL_CONN *p_acl_cb, UINT8 num_read_pages)
981 {
982 UINT16 handle = p_acl_cb->hci_handle;
983 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
984 UINT8 page_idx;
985
986 BTM_TRACE_DEBUG ("btm_process_remote_ext_features\n");
987
988 /* Make sure we have the record to save remote features information */
989 if (p_dev_rec == NULL) {
990 /* Get a new device; might be doing dedicated bonding */
991 p_dev_rec = btm_find_or_alloc_dev (p_acl_cb->remote_addr);
992 }
993
994 p_acl_cb->num_read_pages = num_read_pages;
995 p_dev_rec->num_read_pages = num_read_pages;
996
997 /* Move the pages to placeholder */
998 for (page_idx = 0; page_idx < num_read_pages; page_idx++) {
999 if (page_idx > HCI_EXT_FEATURES_PAGE_MAX) {
1000 BTM_TRACE_ERROR("%s: page=%d unexpected\n", __FUNCTION__, page_idx);
1001 break;
1002 }
1003 memcpy (p_dev_rec->features[page_idx], p_acl_cb->peer_lmp_features[page_idx],
1004 HCI_FEATURE_BYTES_PER_PAGE);
1005 }
1006
1007 const UINT8 req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
1008 #if (SMP_INCLUDED == 1)
1009 /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
1010 btm_sec_set_peer_sec_caps(p_acl_cb, p_dev_rec);
1011 #endif ///SMP_INCLUDED == 1
1012 BTM_TRACE_API("%s: pend:%d\n", __FUNCTION__, req_pend);
1013 if (req_pend) {
1014 #if (CLASSIC_BT_INCLUDED == 1)
1015 /* Request for remaining Security Features (if any) */
1016 l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
1017 #endif ///CLASSIC_BT_INCLUDED == 1
1018 }
1019 }
1020
1021
1022 /*******************************************************************************
1023 **
1024 ** Function btm_read_remote_features
1025 **
1026 ** Description Local function called to send a read remote supported features/
1027 ** remote extended features page[0].
1028 **
1029 ** Returns void
1030 **
1031 *******************************************************************************/
btm_read_remote_features(UINT16 handle)1032 void btm_read_remote_features (UINT16 handle)
1033 {
1034 tACL_CONN *p_acl_cb;
1035
1036 BTM_TRACE_DEBUG("btm_read_remote_features() handle: %d\n", handle);
1037
1038 p_acl_cb = btm_handle_to_acl(handle);
1039 if (p_acl_cb == NULL) {
1040 BTM_TRACE_ERROR("btm_read_remote_features handle=%d invalid\n", handle);
1041 return;
1042 }
1043
1044 p_acl_cb->num_read_pages = 0;
1045 memset (p_acl_cb->peer_lmp_features, 0, sizeof(p_acl_cb->peer_lmp_features));
1046
1047 /* first send read remote supported features HCI command */
1048 /* because we don't know whether the remote support extended feature command */
1049 btsnd_hcic_rmt_features_req (handle);
1050 }
1051
1052
1053 /*******************************************************************************
1054 **
1055 ** Function btm_read_remote_ext_features
1056 **
1057 ** Description Local function called to send a read remote extended features
1058 **
1059 ** Returns void
1060 **
1061 *******************************************************************************/
btm_read_remote_ext_features(UINT16 handle,UINT8 page_number)1062 void btm_read_remote_ext_features (UINT16 handle, UINT8 page_number)
1063 {
1064 BTM_TRACE_DEBUG("btm_read_remote_ext_features() handle: %d page: %d\n", handle, page_number);
1065
1066 btsnd_hcic_rmt_ext_features(handle, page_number);
1067 }
1068
1069
1070 /*******************************************************************************
1071 **
1072 ** Function btm_read_remote_features_complete
1073 **
1074 ** Description This function is called when the remote supported features
1075 ** complete event is received from the HCI.
1076 **
1077 ** Returns void
1078 **
1079 *******************************************************************************/
btm_read_remote_features_complete(UINT8 * p)1080 void btm_read_remote_features_complete (UINT8 *p)
1081 {
1082 tACL_CONN *p_acl_cb;
1083 UINT8 status;
1084 UINT16 handle;
1085
1086 BTM_TRACE_DEBUG ("btm_read_remote_features_complete\n");
1087 STREAM_TO_UINT8 (status, p);
1088
1089 if (status != HCI_SUCCESS) {
1090 BTM_TRACE_ERROR ("btm_read_remote_features_complete failed (status 0x%02x)\n", status);
1091 return;
1092 }
1093
1094 STREAM_TO_UINT16 (handle, p);
1095
1096 p_acl_cb = btm_handle_to_acl(handle);
1097 if (p_acl_cb == NULL) {
1098 BTM_TRACE_ERROR("btm_read_remote_features_complete handle=%d invalid\n", handle);
1099 return;
1100 }
1101
1102 /* Copy the received features page */
1103 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0], p,
1104 HCI_FEATURE_BYTES_PER_PAGE);
1105
1106 if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) &&
1107 (controller_get_interface()->supports_reading_remote_extended_features())) {
1108 /* if the remote controller has extended features and local controller supports
1109 ** HCI_Read_Remote_Extended_Features command then start reading these feature starting
1110 ** with extended features page 1 */
1111 BTM_TRACE_DEBUG ("Start reading remote extended features\n");
1112 btm_read_remote_ext_features(handle, HCI_EXT_FEATURES_PAGE_1);
1113 return;
1114 }
1115
1116 /* Remote controller has no extended features. Process remote controller supported features
1117 (features page HCI_EXT_FEATURES_PAGE_0). */
1118 btm_process_remote_ext_features (p_acl_cb, 1);
1119
1120 /* Continue with HCI connection establishment */
1121 btm_establish_continue (p_acl_cb);
1122 }
1123
1124 /*******************************************************************************
1125 **
1126 ** Function btm_read_remote_ext_features_complete
1127 **
1128 ** Description This function is called when the remote extended features
1129 ** complete event is received from the HCI.
1130 **
1131 ** Returns void
1132 **
1133 *******************************************************************************/
btm_read_remote_ext_features_complete(UINT8 * p)1134 void btm_read_remote_ext_features_complete (UINT8 *p)
1135 {
1136 tACL_CONN *p_acl_cb;
1137 UINT8 page_num, max_page;
1138 UINT16 handle;
1139
1140 BTM_TRACE_DEBUG ("btm_read_remote_ext_features_complete\n");
1141
1142 ++p;
1143 STREAM_TO_UINT16 (handle, p);
1144 STREAM_TO_UINT8 (page_num, p);
1145 STREAM_TO_UINT8 (max_page, p);
1146
1147 /* Validate parameters */
1148 p_acl_cb = btm_handle_to_acl(handle);
1149 if (p_acl_cb == NULL) {
1150 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete handle=%d invalid\n", handle);
1151 return;
1152 }
1153
1154 if (max_page > HCI_EXT_FEATURES_PAGE_MAX) {
1155 BTM_TRACE_ERROR("btm_read_remote_ext_features_complete page=%d unknown", max_page);
1156 }
1157
1158
1159 /* Copy the received features page */
1160 STREAM_TO_ARRAY(p_acl_cb->peer_lmp_features[page_num], p, HCI_FEATURE_BYTES_PER_PAGE);
1161
1162 /* If there is the next remote features page and
1163 * we have space to keep this page data - read this page */
1164 if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX)) {
1165 page_num++;
1166 BTM_TRACE_DEBUG("BTM reads next remote extended features page (%d)\n", page_num);
1167 btm_read_remote_ext_features (handle, page_num);
1168 return;
1169 }
1170
1171 /* Reading of remote feature pages is complete */
1172 BTM_TRACE_DEBUG("BTM reached last remote extended features page (%d)\n", page_num);
1173
1174 /* Process the pages */
1175 btm_process_remote_ext_features (p_acl_cb, (UINT8) (page_num + 1));
1176
1177 /* Continue with HCI connection establishment */
1178 btm_establish_continue (p_acl_cb);
1179 }
1180
1181 /*******************************************************************************
1182 **
1183 ** Function btm_read_remote_ext_features_failed
1184 **
1185 ** Description This function is called when the remote extended features
1186 ** complete event returns a failed status.
1187 **
1188 ** Returns void
1189 **
1190 *******************************************************************************/
btm_read_remote_ext_features_failed(UINT8 status,UINT16 handle)1191 void btm_read_remote_ext_features_failed (UINT8 status, UINT16 handle)
1192 {
1193 tACL_CONN *p_acl_cb;
1194
1195 BTM_TRACE_WARNING ("btm_read_remote_ext_features_failed (status 0x%02x) for handle %d\n",
1196 status, handle);
1197
1198 p_acl_cb = btm_handle_to_acl(handle);
1199 if (p_acl_cb == NULL) {
1200 BTM_TRACE_ERROR("btm_read_remote_ext_features_failed handle=%d invalid\n", handle);
1201 return;
1202 }
1203
1204 /* Process supported features only */
1205 btm_process_remote_ext_features (p_acl_cb, 1);
1206
1207 /* Continue HCI connection establishment */
1208 btm_establish_continue (p_acl_cb);
1209 }
1210
1211 /*******************************************************************************
1212 **
1213 ** Function btm_establish_continue
1214 **
1215 ** Description This function is called when the command complete message
1216 ** is received from the HCI for the read local link policy request.
1217 **
1218 ** Returns void
1219 **
1220 *******************************************************************************/
btm_establish_continue(tACL_CONN * p_acl_cb)1221 void btm_establish_continue (tACL_CONN *p_acl_cb)
1222 {
1223 tBTM_BL_EVENT_DATA evt_data;
1224 BTM_TRACE_DEBUG ("btm_establish_continue\n");
1225 #if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == 0)
1226 #if (defined BLE_INCLUDED && BLE_INCLUDED == 1)
1227 if (p_acl_cb->transport == BT_TRANSPORT_BR_EDR)
1228 #endif
1229 {
1230 /* For now there are a some devices that do not like sending */
1231 /* commands events and data at the same time. */
1232 /* Set the packet types to the default allowed by the device */
1233 btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
1234
1235 if (btm_cb.btm_def_link_policy) {
1236 BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
1237 }
1238 }
1239 #endif
1240 p_acl_cb->link_up_issued = 1;
1241
1242 /* If anyone cares, tell him database changed */
1243 if (btm_cb.p_bl_changed_cb) {
1244 evt_data.event = BTM_BL_CONN_EVT;
1245 evt_data.conn.p_bda = p_acl_cb->remote_addr;
1246 evt_data.conn.p_bdn = p_acl_cb->remote_name;
1247 evt_data.conn.p_dc = p_acl_cb->remote_dc;
1248 evt_data.conn.p_features = p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0];
1249 evt_data.conn.sc_downgrade = p_acl_cb->sc_downgrade;
1250 #if BLE_INCLUDED == 1
1251 evt_data.conn.handle = p_acl_cb->hci_handle;
1252 evt_data.conn.transport = p_acl_cb->transport;
1253 #endif
1254
1255 (*btm_cb.p_bl_changed_cb)(&evt_data);
1256 }
1257 btm_acl_update_busy_level (BTM_BLI_ACL_UP_EVT);
1258 }
1259
1260
1261 /*******************************************************************************
1262 **
1263 ** Function BTM_SetDefaultLinkSuperTout
1264 **
1265 ** Description Set the default value for HCI "Write Link Supervision Timeout"
1266 ** command to use when an ACL link is created.
1267 **
1268 ** Returns void
1269 **
1270 *******************************************************************************/
BTM_SetDefaultLinkSuperTout(UINT16 timeout)1271 void BTM_SetDefaultLinkSuperTout (UINT16 timeout)
1272 {
1273 BTM_TRACE_DEBUG ("BTM_SetDefaultLinkSuperTout\n");
1274 btm_cb.btm_def_link_super_tout = timeout;
1275 }
1276
1277 /*******************************************************************************
1278 **
1279 ** Function BTM_GetLinkSuperTout
1280 **
1281 ** Description Read the link supervision timeout value of the connection
1282 **
1283 ** Returns status of the operation
1284 **
1285 *******************************************************************************/
BTM_GetLinkSuperTout(BD_ADDR remote_bda,UINT16 * p_timeout)1286 tBTM_STATUS BTM_GetLinkSuperTout (BD_ADDR remote_bda, UINT16 *p_timeout)
1287 {
1288 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1289
1290 BTM_TRACE_DEBUG ("BTM_GetLinkSuperTout\n");
1291 if (p != (tACL_CONN *)NULL) {
1292 *p_timeout = p->link_super_tout;
1293 return (BTM_SUCCESS);
1294 }
1295 /* If here, no BD Addr found */
1296 return (BTM_UNKNOWN_ADDR);
1297 }
1298
1299
1300 /*******************************************************************************
1301 **
1302 ** Function BTM_SetLinkSuperTout
1303 **
1304 ** Description Create and send HCI "Write Link Supervision Timeout" command
1305 **
1306 ** Returns status of the operation
1307 **
1308 *******************************************************************************/
BTM_SetLinkSuperTout(BD_ADDR remote_bda,UINT16 timeout)1309 tBTM_STATUS BTM_SetLinkSuperTout (BD_ADDR remote_bda, UINT16 timeout)
1310 {
1311 tACL_CONN *p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
1312
1313 BTM_TRACE_DEBUG ("BTM_SetLinkSuperTout\n");
1314 if (p != (tACL_CONN *)NULL) {
1315 p->link_super_tout = timeout;
1316
1317 /* Only send if current role is Master; 2.0 spec requires this */
1318 if (p->link_role == BTM_ROLE_MASTER) {
1319 if (!btsnd_hcic_write_link_super_tout (LOCAL_BR_EDR_CONTROLLER_ID,
1320 p->hci_handle, timeout)) {
1321 return (BTM_NO_RESOURCES);
1322 }
1323
1324 return (BTM_CMD_STARTED);
1325 } else {
1326 return (BTM_SUCCESS);
1327 }
1328 }
1329
1330 /* If here, no BD Addr found */
1331 return (BTM_UNKNOWN_ADDR);
1332 }
1333
1334 /*******************************************************************************
1335 **
1336 ** Function BTM_IsAclConnectionUp
1337 **
1338 ** Description This function is called to check if an ACL connection exists
1339 ** to a specific remote BD Address.
1340 **
1341 ** Returns 1 if connection is up, else 0.
1342 **
1343 *******************************************************************************/
BTM_IsAclConnectionUp(BD_ADDR remote_bda,tBT_TRANSPORT transport)1344 BOOLEAN BTM_IsAclConnectionUp (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1345 {
1346 tACL_CONN *p;
1347
1348 BTM_TRACE_API ("BTM_IsAclConnectionUp: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
1349 remote_bda[0], remote_bda[1], remote_bda[2],
1350 remote_bda[3], remote_bda[4], remote_bda[5]);
1351
1352 p = btm_bda_to_acl(remote_bda, transport);
1353 if (p != (tACL_CONN *)NULL) {
1354 return (1);
1355 }
1356
1357 /* If here, no BD Addr found */
1358 return (0);
1359 }
1360
1361 /*******************************************************************************
1362 **
1363 ** Function BTM_GetNumAclLinks
1364 **
1365 ** Description This function is called to count the number of
1366 ** ACL links that are active.
1367 **
1368 ** Returns UINT16 Number of active ACL links
1369 **
1370 *******************************************************************************/
BTM_GetNumAclLinks(void)1371 UINT16 BTM_GetNumAclLinks (void)
1372 {
1373 uint16_t num_acl = 0;
1374
1375 num_acl = list_length(btm_cb.p_acl_db_list);
1376 return num_acl;
1377 }
1378
1379 /*******************************************************************************
1380 **
1381 ** Function btm_get_acl_disc_reason_code
1382 **
1383 ** Description This function is called to get the disconnection reason code
1384 ** returned by the HCI at disconnection complete event.
1385 **
1386 ** Returns 1 if connection is up, else 0.
1387 **
1388 *******************************************************************************/
btm_get_acl_disc_reason_code(void)1389 UINT16 btm_get_acl_disc_reason_code (void)
1390 {
1391 UINT8 res = btm_cb.acl_disc_reason;
1392 BTM_TRACE_DEBUG ("btm_get_acl_disc_reason_code\n");
1393 return (res);
1394 }
1395
1396
1397 /*******************************************************************************
1398 **
1399 ** Function BTM_GetHCIConnHandle
1400 **
1401 ** Description This function is called to get the handle for an ACL connection
1402 ** to a specific remote BD Address.
1403 **
1404 ** Returns the handle of the connection, or 0xFFFF if none.
1405 **
1406 *******************************************************************************/
BTM_GetHCIConnHandle(BD_ADDR remote_bda,tBT_TRANSPORT transport)1407 UINT16 BTM_GetHCIConnHandle (BD_ADDR remote_bda, tBT_TRANSPORT transport)
1408 {
1409 tACL_CONN *p;
1410 BTM_TRACE_DEBUG ("BTM_GetHCIConnHandle\n");
1411 p = btm_bda_to_acl(remote_bda, transport);
1412 if (p != (tACL_CONN *)NULL) {
1413 return (p->hci_handle);
1414 }
1415
1416 /* If here, no BD Addr found */
1417 return (0xFFFF);
1418 }
1419
1420 /*******************************************************************************
1421 **
1422 ** Function btm_process_clk_off_comp_evt
1423 **
1424 ** Description This function is called when clock offset command completes.
1425 **
1426 ** Input Parms hci_handle - connection handle associated with the change
1427 ** clock offset
1428 **
1429 ** Returns void
1430 **
1431 *******************************************************************************/
btm_process_clk_off_comp_evt(UINT16 hci_handle,UINT16 clock_offset)1432 void btm_process_clk_off_comp_evt (UINT16 hci_handle, UINT16 clock_offset)
1433 {
1434 tACL_CONN *p_acl_cb = NULL;
1435 BTM_TRACE_DEBUG ("btm_process_clk_off_comp_evt\n");
1436 /* Look up the connection by handle and set the current mode */
1437 p_acl_cb = btm_handle_to_acl(hci_handle);
1438 if (p_acl_cb) {
1439 p_acl_cb->clock_offset = clock_offset;
1440 }
1441
1442 }
1443
1444 /*******************************************************************************
1445 **
1446 ** Function btm_acl_role_changed
1447 **
1448 ** Description This function is called whan a link's master/slave role change
1449 ** event or command status event (with error) is received.
1450 ** It updates the link control block, and calls
1451 ** the registered callback with status and role (if registered).
1452 **
1453 ** Returns void
1454 **
1455 *******************************************************************************/
btm_acl_role_changed(UINT8 hci_status,BD_ADDR bd_addr,UINT8 new_role)1456 void btm_acl_role_changed (UINT8 hci_status, BD_ADDR bd_addr, UINT8 new_role)
1457 {
1458 UINT8 *p_bda = (bd_addr) ? bd_addr :
1459 btm_cb.devcb.switch_role_ref_data.remote_bd_addr;
1460 tACL_CONN *p = btm_bda_to_acl(p_bda, BT_TRANSPORT_BR_EDR);
1461 tBTM_ROLE_SWITCH_CMPL *p_data = &btm_cb.devcb.switch_role_ref_data;
1462 tBTM_SEC_DEV_REC *p_dev_rec;
1463 tBTM_BL_ROLE_CHG_DATA evt;
1464
1465 BTM_TRACE_DEBUG ("btm_acl_role_changed\n");
1466 /* Ignore any stray events */
1467 if (p == NULL) {
1468 /* it could be a failure */
1469 if (hci_status != HCI_SUCCESS) {
1470 btm_acl_report_role_change(hci_status, bd_addr);
1471 }
1472 return;
1473 }
1474
1475 p_data->hci_status = hci_status;
1476
1477 if (hci_status == HCI_SUCCESS) {
1478 p_data->role = new_role;
1479 memcpy(p_data->remote_bd_addr, p_bda, BD_ADDR_LEN);
1480
1481 /* Update cached value */
1482 p->link_role = new_role;
1483
1484 /* Reload LSTO: link supervision timeout is reset in the LM after a role switch */
1485 if (new_role == BTM_ROLE_MASTER) {
1486 BTM_SetLinkSuperTout (p->remote_addr, p->link_super_tout);
1487 }
1488 } else {
1489 /* so the BTM_BL_ROLE_CHG_EVT uses the old role */
1490 new_role = p->link_role;
1491 }
1492
1493 /* Check if any SCO req is pending for role change */
1494 btm_sco_chk_pend_rolechange (p->hci_handle);
1495
1496 /* if switching state is switching we need to turn encryption on */
1497 /* if idle, we did not change encryption */
1498 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
1499 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, 1)) {
1500 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
1501 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
1502 return;
1503 }
1504 }
1505
1506 /* Set the switch_role_state to IDLE since the reply received from HCI */
1507 /* regardless of its result either success or failed. */
1508 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
1509 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
1510 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
1511 }
1512
1513 /* if role switch complete is needed, report it now */
1514 btm_acl_report_role_change(hci_status, bd_addr);
1515
1516 /* if role change event is registered, report it now */
1517 if (btm_cb.p_bl_changed_cb && (btm_cb.bl_evt_mask & BTM_BL_ROLE_CHG_MASK)) {
1518 evt.event = BTM_BL_ROLE_CHG_EVT;
1519 evt.new_role = new_role;
1520 evt.p_bda = p_bda;
1521 evt.hci_status = hci_status;
1522 (*btm_cb.p_bl_changed_cb)((tBTM_BL_EVENT_DATA *)&evt);
1523 }
1524
1525 BTM_TRACE_DEBUG("Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, rs_st:%d\n",
1526 p_data->role, p_data->hci_status, p->switch_role_state);
1527
1528 #if BTM_DISC_DURING_RS == 1
1529 /* If a disconnect is pending, issue it now that role switch has completed */
1530 if ((p_dev_rec = btm_find_dev (p_bda)) != NULL) {
1531 if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
1532 BTM_TRACE_WARNING("btm_acl_role_changed -> Issuing delayed HCI_Disconnect!!!\n");
1533 btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
1534 }
1535 BTM_TRACE_ERROR("tBTM_SEC_DEV:0x%x rs_disc_pending=%d\n",
1536 (UINT32)p_dev_rec, p_dev_rec->rs_disc_pending);
1537 p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
1538 }
1539
1540 #endif
1541
1542 }
1543
1544 /*******************************************************************************
1545 **
1546 ** Function BTM_AllocateSCN
1547 **
1548 ** Description Look through the Server Channel Numbers for a free one.
1549 **
1550 ** Returns Allocated SCN number or 0 if none.
1551 **
1552 *******************************************************************************/
1553 #if (CLASSIC_BT_INCLUDED == 1)
BTM_AllocateSCN(void)1554 UINT8 BTM_AllocateSCN(void)
1555 {
1556 UINT8 x;
1557 BTM_TRACE_DEBUG ("BTM_AllocateSCN\n");
1558 for (x = 1; x < BTM_MAX_SCN; x++) {
1559 if (!btm_cb.btm_scn[x - 1]) {
1560 btm_cb.btm_scn[x - 1] = 1;
1561 return x;
1562 }
1563 }
1564 return (0); /* No free ports */
1565 }
1566 #endif ///CLASSIC_BT_INCLUDED == 1
1567
1568 /*******************************************************************************
1569 **
1570 ** Function BTM_TryAllocateSCN
1571 **
1572 ** Description Try to allocate a fixed server channel
1573 **
1574 ** Returns Returns 1 if server channel was available
1575 **
1576 *******************************************************************************/
1577 #if (CLASSIC_BT_INCLUDED == 1)
BTM_TryAllocateSCN(UINT8 scn)1578 BOOLEAN BTM_TryAllocateSCN(UINT8 scn)
1579 {
1580 if (scn >= BTM_MAX_SCN) {
1581 return 0;
1582 }
1583
1584 /* check if this port is available */
1585 if (!btm_cb.btm_scn[scn - 1]) {
1586 btm_cb.btm_scn[scn - 1] = 1;
1587 return 1;
1588 }
1589
1590 return (0); /* Port was busy */
1591 }
1592
1593
1594 /*******************************************************************************
1595 **
1596 ** Function BTM_FreeSCN
1597 **
1598 ** Description Free the specified SCN.
1599 **
1600 ** Returns 1 or 0
1601 **
1602 *******************************************************************************/
BTM_FreeSCN(UINT8 scn)1603 BOOLEAN BTM_FreeSCN(UINT8 scn)
1604 {
1605 BTM_TRACE_DEBUG ("BTM_FreeSCN \n");
1606 if (scn <= BTM_MAX_SCN) {
1607 btm_cb.btm_scn[scn - 1] = 0;
1608 return (1);
1609 } else {
1610 return (0); /* Illegal SCN passed in */
1611 }
1612 return (0);
1613 }
1614 #endif ///CLASSIC_BT_INCLUDED == 1
1615
1616 /*******************************************************************************
1617 **
1618 ** Function btm_set_packet_types
1619 **
1620 ** Description This function sets the packet types used for a specific
1621 ** ACL connection. It is called internally by btm_acl_created
1622 ** or by an application/profile by BTM_SetPacketTypes.
1623 **
1624 ** Returns status of the operation
1625 **
1626 *******************************************************************************/
btm_set_packet_types(tACL_CONN * p,UINT16 pkt_types)1627 tBTM_STATUS btm_set_packet_types (tACL_CONN *p, UINT16 pkt_types)
1628 {
1629 UINT16 temp_pkt_types;
1630 BTM_TRACE_DEBUG ("btm_set_packet_types\n");
1631 /* Save in the ACL control blocks, types that we support */
1632 temp_pkt_types = (pkt_types & BTM_ACL_SUPPORTED_PKTS_MASK &
1633 btm_cb.btm_acl_pkt_types_supported);
1634
1635 /* OR in any exception packet types if at least 2.0 version of spec */
1636 temp_pkt_types |= ((pkt_types & BTM_ACL_EXCEPTION_PKTS_MASK) |
1637 (btm_cb.btm_acl_pkt_types_supported & BTM_ACL_EXCEPTION_PKTS_MASK));
1638
1639 /* Exclude packet types not supported by the peer */
1640 btm_acl_chk_peer_pkt_type_support (p, &temp_pkt_types);
1641
1642 BTM_TRACE_DEBUG ("SetPacketType Mask -> 0x%04x\n", temp_pkt_types);
1643
1644 if (!btsnd_hcic_change_conn_type (p->hci_handle, temp_pkt_types)) {
1645 return (BTM_NO_RESOURCES);
1646 }
1647
1648 p->pkt_types_mask = temp_pkt_types;
1649
1650 return (BTM_CMD_STARTED);
1651 }
1652
1653 /*******************************************************************************
1654 **
1655 ** Function btm_get_max_packet_size
1656 **
1657 ** Returns Returns maximum packet size that can be used for current
1658 ** connection, 0 if connection is not established
1659 **
1660 *******************************************************************************/
btm_get_max_packet_size(BD_ADDR addr)1661 UINT16 btm_get_max_packet_size (BD_ADDR addr)
1662 {
1663 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1664 UINT16 pkt_types = 0;
1665 UINT16 pkt_size = 0;
1666 BTM_TRACE_DEBUG ("btm_get_max_packet_size\n");
1667 if (p != NULL) {
1668 pkt_types = p->pkt_types_mask;
1669 } else {
1670 /* Special case for when info for the local device is requested */
1671 if (memcmp (controller_get_interface()->get_address(), addr, BD_ADDR_LEN) == 0) {
1672 pkt_types = btm_cb.btm_acl_pkt_types_supported;
1673 }
1674 }
1675
1676 if (pkt_types) {
1677 if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH5)) {
1678 pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
1679 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH5)) {
1680 pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
1681 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH3)) {
1682 pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
1683 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH5) {
1684 pkt_size = HCI_DH5_PACKET_SIZE;
1685 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH3)) {
1686 pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
1687 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM5) {
1688 pkt_size = HCI_DM5_PACKET_SIZE;
1689 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH3) {
1690 pkt_size = HCI_DH3_PACKET_SIZE;
1691 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM3) {
1692 pkt_size = HCI_DM3_PACKET_SIZE;
1693 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_3_DH1)) {
1694 pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
1695 } else if (!(pkt_types & BTM_ACL_PKT_TYPES_MASK_NO_2_DH1)) {
1696 pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
1697 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DH1) {
1698 pkt_size = HCI_DH1_PACKET_SIZE;
1699 } else if (pkt_types & BTM_ACL_PKT_TYPES_MASK_DM1) {
1700 pkt_size = HCI_DM1_PACKET_SIZE;
1701 }
1702 }
1703
1704 return (pkt_size);
1705 }
1706
1707 /*******************************************************************************
1708 **
1709 ** Function BTM_ReadRemoteVersion
1710 **
1711 ** Returns If connected report peer device info
1712 **
1713 *******************************************************************************/
BTM_ReadRemoteVersion(BD_ADDR addr,UINT8 * lmp_version,UINT16 * manufacturer,UINT16 * lmp_sub_version)1714 tBTM_STATUS BTM_ReadRemoteVersion (BD_ADDR addr, UINT8 *lmp_version,
1715 UINT16 *manufacturer, UINT16 *lmp_sub_version)
1716 {
1717 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1718 BTM_TRACE_DEBUG ("BTM_ReadRemoteVersion\n");
1719 if (p == NULL) {
1720 return (BTM_UNKNOWN_ADDR);
1721 }
1722
1723 if (lmp_version) {
1724 *lmp_version = p->lmp_version;
1725 }
1726
1727 if (manufacturer) {
1728 *manufacturer = p->manufacturer;
1729 }
1730
1731 if (lmp_sub_version) {
1732 *lmp_sub_version = p->lmp_subversion;
1733 }
1734
1735 return (BTM_SUCCESS);
1736 }
1737
1738 /*******************************************************************************
1739 **
1740 ** Function BTM_ReadRemoteFeatures
1741 **
1742 ** Returns pointer to the remote supported features mask (8 bytes)
1743 **
1744 *******************************************************************************/
BTM_ReadRemoteFeatures(BD_ADDR addr)1745 UINT8 *BTM_ReadRemoteFeatures (BD_ADDR addr)
1746 {
1747 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1748 BTM_TRACE_DEBUG ("BTM_ReadRemoteFeatures\n");
1749 if (p == NULL) {
1750 return (NULL);
1751 }
1752
1753 return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1754 }
1755
1756 /*******************************************************************************
1757 **
1758 ** Function BTM_ReadRemoteExtendedFeatures
1759 **
1760 ** Returns pointer to the remote extended features mask (8 bytes)
1761 ** or NULL if bad page
1762 **
1763 *******************************************************************************/
BTM_ReadRemoteExtendedFeatures(BD_ADDR addr,UINT8 page_number)1764 UINT8 *BTM_ReadRemoteExtendedFeatures (BD_ADDR addr, UINT8 page_number)
1765 {
1766 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1767 BTM_TRACE_DEBUG ("BTM_ReadRemoteExtendedFeatures\n");
1768 if (p == NULL) {
1769 return (NULL);
1770 }
1771
1772 if (page_number > HCI_EXT_FEATURES_PAGE_MAX) {
1773 BTM_TRACE_ERROR("Warning: BTM_ReadRemoteExtendedFeatures page %d unknown\n", page_number);
1774 return NULL;
1775 }
1776
1777 return (p->peer_lmp_features[page_number]);
1778 }
1779
1780 /*******************************************************************************
1781 **
1782 ** Function BTM_ReadNumberRemoteFeaturesPages
1783 **
1784 ** Returns number of features pages read from the remote device.
1785 **
1786 *******************************************************************************/
BTM_ReadNumberRemoteFeaturesPages(BD_ADDR addr)1787 UINT8 BTM_ReadNumberRemoteFeaturesPages (BD_ADDR addr)
1788 {
1789 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1790 BTM_TRACE_DEBUG ("BTM_ReadNumberRemoteFeaturesPages\n");
1791 if (p == NULL) {
1792 return (0);
1793 }
1794
1795 return (p->num_read_pages);
1796 }
1797
1798 /*******************************************************************************
1799 **
1800 ** Function BTM_ReadAllRemoteFeatures
1801 **
1802 ** Returns pointer to all features of the remote (24 bytes).
1803 **
1804 *******************************************************************************/
BTM_ReadAllRemoteFeatures(BD_ADDR addr)1805 UINT8 *BTM_ReadAllRemoteFeatures (BD_ADDR addr)
1806 {
1807 tACL_CONN *p = btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
1808 BTM_TRACE_DEBUG ("BTM_ReadAllRemoteFeatures\n");
1809 if (p == NULL) {
1810 return (NULL);
1811 }
1812
1813 return (p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]);
1814 }
1815
1816 /*******************************************************************************
1817 **
1818 ** Function BTM_RegBusyLevelNotif
1819 **
1820 ** Description This function is called to register a callback to receive
1821 ** busy level change events.
1822 **
1823 ** Returns BTM_SUCCESS if successfully registered, otherwise error
1824 **
1825 *******************************************************************************/
BTM_RegBusyLevelNotif(tBTM_BL_CHANGE_CB * p_cb,UINT8 * p_level,tBTM_BL_EVENT_MASK evt_mask)1826 tBTM_STATUS BTM_RegBusyLevelNotif (tBTM_BL_CHANGE_CB *p_cb, UINT8 *p_level,
1827 tBTM_BL_EVENT_MASK evt_mask)
1828 {
1829 BTM_TRACE_DEBUG ("BTM_RegBusyLevelNotif\n");
1830 if (p_level) {
1831 *p_level = btm_cb.busy_level;
1832 }
1833
1834 btm_cb.bl_evt_mask = evt_mask;
1835
1836 if (!p_cb) {
1837 btm_cb.p_bl_changed_cb = NULL;
1838 } else if (btm_cb.p_bl_changed_cb) {
1839 return (BTM_BUSY);
1840 } else {
1841 btm_cb.p_bl_changed_cb = p_cb;
1842 }
1843
1844 return (BTM_SUCCESS);
1845 }
1846
1847 /*******************************************************************************
1848 **
1849 ** Function BTM_SetQoS
1850 **
1851 ** Description This function is called to setup QoS
1852 **
1853 ** Returns status of the operation
1854 **
1855 *******************************************************************************/
BTM_SetQoS(BD_ADDR bd,FLOW_SPEC * p_flow,tBTM_CMPL_CB * p_cb)1856 tBTM_STATUS BTM_SetQoS (BD_ADDR bd, FLOW_SPEC *p_flow, tBTM_CMPL_CB *p_cb)
1857 {
1858 tACL_CONN *p = NULL;
1859
1860 BTM_TRACE_API ("BTM_SetQoS: BdAddr: %02x%02x%02x%02x%02x%02x\n",
1861 bd[0], bd[1], bd[2],
1862 bd[3], bd[4], bd[5]);
1863
1864 /* If someone already waiting on the version, do not allow another */
1865 if (btm_cb.devcb.p_qossu_cmpl_cb) {
1866 return (BTM_BUSY);
1867 }
1868
1869 if ( (p = btm_bda_to_acl(bd, BT_TRANSPORT_BR_EDR)) != NULL) {
1870 btu_start_timer (&btm_cb.devcb.qossu_timer, BTU_TTYPE_BTM_QOS, BTM_DEV_REPLY_TIMEOUT);
1871 btm_cb.devcb.p_qossu_cmpl_cb = p_cb;
1872
1873 if (!btsnd_hcic_qos_setup (p->hci_handle, p_flow->qos_flags, p_flow->service_type,
1874 p_flow->token_rate, p_flow->peak_bandwidth,
1875 p_flow->latency, p_flow->delay_variation)) {
1876 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1877 btu_stop_timer(&btm_cb.devcb.qossu_timer);
1878 return (BTM_NO_RESOURCES);
1879 } else {
1880 return (BTM_CMD_STARTED);
1881 }
1882 }
1883
1884 /* If here, no BD Addr found */
1885 return (BTM_UNKNOWN_ADDR);
1886 }
1887
1888 /*******************************************************************************
1889 **
1890 ** Function btm_qos_setup_complete
1891 **
1892 ** Description This function is called when the command complete message
1893 ** is received from the HCI for the qos setup request.
1894 **
1895 ** Returns void
1896 **
1897 *******************************************************************************/
btm_qos_setup_complete(UINT8 status,UINT16 handle,FLOW_SPEC * p_flow)1898 void btm_qos_setup_complete (UINT8 status, UINT16 handle, FLOW_SPEC *p_flow)
1899 {
1900 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_qossu_cmpl_cb;
1901 tBTM_QOS_SETUP_CMPL qossu;
1902 BTM_TRACE_DEBUG ("btm_qos_setup_complete\n");
1903 btu_stop_timer (&btm_cb.devcb.qossu_timer);
1904
1905 btm_cb.devcb.p_qossu_cmpl_cb = NULL;
1906
1907 if (p_cb) {
1908 memset(&qossu, 0, sizeof(tBTM_QOS_SETUP_CMPL));
1909 qossu.status = status;
1910 qossu.handle = handle;
1911 tACL_CONN *p = btm_handle_to_acl(handle);
1912 if (p != NULL) {
1913 memcpy (qossu.rem_bda, p->remote_addr, BD_ADDR_LEN);
1914 }
1915 if (p_flow != NULL) {
1916 qossu.flow.qos_flags = p_flow->qos_flags;
1917 qossu.flow.service_type = p_flow->service_type;
1918 qossu.flow.token_rate = p_flow->token_rate;
1919 qossu.flow.peak_bandwidth = p_flow->peak_bandwidth;
1920 qossu.flow.latency = p_flow->latency;
1921 qossu.flow.delay_variation = p_flow->delay_variation;
1922 }
1923 BTM_TRACE_DEBUG ("BTM: p_flow->delay_variation: 0x%02x\n",
1924 qossu.flow.delay_variation);
1925 (*p_cb)(&qossu);
1926 }
1927 }
1928
1929 /*******************************************************************************
1930 **
1931 ** Function btm_qos_setup_timeout
1932 **
1933 ** Description This function processes a timeout.
1934 ** Currently, we just report an error log
1935 **
1936 ** Returns void
1937 **
1938 *******************************************************************************/
btm_qos_setup_timeout(void * p_tle)1939 void btm_qos_setup_timeout (void *p_tle)
1940 {
1941 BTM_TRACE_DEBUG ("%s\n", __func__);
1942
1943 btm_qos_setup_complete (HCI_ERR_HOST_TIMEOUT, 0, NULL);
1944 }
1945
1946 /*******************************************************************************
1947 **
1948 ** Function BTM_ReadRSSI
1949 **
1950 ** Description This function is called to read the link policy settings.
1951 ** The address of link policy results are returned in the callback.
1952 ** (tBTM_RSSI_RESULTS)
1953 **
1954 ** Returns BTM_CMD_STARTED if successfully initiated or error code
1955 **
1956 *******************************************************************************/
BTM_ReadRSSI(BD_ADDR remote_bda,tBT_TRANSPORT transport,tBTM_CMPL_CB * p_cb)1957 tBTM_STATUS BTM_ReadRSSI (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
1958 {
1959 tACL_CONN *p;
1960
1961 BTM_TRACE_API ("BTM_ReadRSSI: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
1962 remote_bda[0], remote_bda[1], remote_bda[2],
1963 remote_bda[3], remote_bda[4], remote_bda[5]);
1964 tBTM_RSSI_RESULTS result;
1965 /* If someone already waiting on the version, do not allow another */
1966 if (btm_cb.devcb.p_rssi_cmpl_cb) {
1967 result.status = BTM_BUSY;
1968 (*p_cb)(&result);
1969 return (BTM_BUSY);
1970 }
1971
1972 p = btm_bda_to_acl(remote_bda, transport);
1973 if (p != (tACL_CONN *)NULL) {
1974 btu_start_timer (&btm_cb.devcb.rssi_timer, BTU_TTYPE_BTM_ACL,
1975 BTM_DEV_REPLY_TIMEOUT);
1976
1977 btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
1978
1979 if (!btsnd_hcic_read_rssi (p->hci_handle)) {
1980 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
1981 btu_stop_timer (&btm_cb.devcb.rssi_timer);
1982 result.status = BTM_NO_RESOURCES;
1983 (*p_cb)(&result);
1984 return (BTM_NO_RESOURCES);
1985 } else {
1986 return (BTM_CMD_STARTED);
1987 }
1988 }
1989
1990 /* If here, no BD Addr found */
1991 return (BTM_UNKNOWN_ADDR);
1992 }
1993
1994 /*******************************************************************************
1995 **
1996 ** Function BTM_ReadLinkQuality
1997 **
1998 ** Description This function is called to read the link qulaity.
1999 ** The value of the link quality is returned in the callback.
2000 ** (tBTM_LINK_QUALITY_RESULTS)
2001 **
2002 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2003 **
2004 *******************************************************************************/
BTM_ReadLinkQuality(BD_ADDR remote_bda,tBTM_CMPL_CB * p_cb)2005 tBTM_STATUS BTM_ReadLinkQuality (BD_ADDR remote_bda, tBTM_CMPL_CB *p_cb)
2006 {
2007 tACL_CONN *p;
2008
2009 BTM_TRACE_API ("BTM_ReadLinkQuality: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2010 remote_bda[0], remote_bda[1], remote_bda[2],
2011 remote_bda[3], remote_bda[4], remote_bda[5]);
2012
2013 /* If someone already waiting on the version, do not allow another */
2014 if (btm_cb.devcb.p_lnk_qual_cmpl_cb) {
2015 return (BTM_BUSY);
2016 }
2017
2018 p = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
2019 if (p != (tACL_CONN *)NULL) {
2020 btu_start_timer (&btm_cb.devcb.lnk_quality_timer, BTU_TTYPE_BTM_ACL,
2021 BTM_DEV_REPLY_TIMEOUT);
2022 btm_cb.devcb.p_lnk_qual_cmpl_cb = p_cb;
2023
2024 if (!btsnd_hcic_get_link_quality (p->hci_handle)) {
2025 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2026 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2027 return (BTM_NO_RESOURCES);
2028 } else {
2029 return (BTM_CMD_STARTED);
2030 }
2031 }
2032
2033 /* If here, no BD Addr found */
2034 return (BTM_UNKNOWN_ADDR);
2035 }
2036
2037 /*******************************************************************************
2038 **
2039 ** Function BTM_ReadTxPower
2040 **
2041 ** Description This function is called to read the current
2042 ** TX power of the connection. The tx power level results
2043 ** are returned in the callback.
2044 ** (tBTM_RSSI_RESULTS)
2045 **
2046 ** Returns BTM_CMD_STARTED if successfully initiated or error code
2047 **
2048 *******************************************************************************/
BTM_ReadTxPower(BD_ADDR remote_bda,tBT_TRANSPORT transport,tBTM_CMPL_CB * p_cb)2049 tBTM_STATUS BTM_ReadTxPower (BD_ADDR remote_bda, tBT_TRANSPORT transport, tBTM_CMPL_CB *p_cb)
2050 {
2051 tACL_CONN *p;
2052 BOOLEAN ret;
2053 #define BTM_READ_RSSI_TYPE_CUR 0x00
2054 #define BTM_READ_RSSI_TYPE_MAX 0X01
2055
2056 BTM_TRACE_API ("BTM_ReadTxPower: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2057 remote_bda[0], remote_bda[1], remote_bda[2],
2058 remote_bda[3], remote_bda[4], remote_bda[5]);
2059
2060 /* If someone already waiting on the version, do not allow another */
2061 if (btm_cb.devcb.p_tx_power_cmpl_cb) {
2062 return (BTM_BUSY);
2063 }
2064
2065 p = btm_bda_to_acl(remote_bda, transport);
2066 if (p != (tACL_CONN *)NULL) {
2067 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2068 BTM_DEV_REPLY_TIMEOUT);
2069
2070 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2071
2072 #if BLE_INCLUDED == 1
2073 if (p->transport == BT_TRANSPORT_LE) {
2074 memcpy(btm_cb.devcb.read_tx_pwr_addr, remote_bda, BD_ADDR_LEN);
2075 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2076 } else
2077 #endif
2078 {
2079 ret = btsnd_hcic_read_tx_power (p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
2080 }
2081 if (!ret) {
2082 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2083 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2084 return (BTM_NO_RESOURCES);
2085 } else {
2086 return (BTM_CMD_STARTED);
2087 }
2088 }
2089
2090 /* If here, no BD Addr found */
2091 return (BTM_UNKNOWN_ADDR);
2092 }
2093
2094 #if (BLE_INCLUDED == 1)
BTM_BleReadAdvTxPower(tBTM_CMPL_CB * p_cb)2095 tBTM_STATUS BTM_BleReadAdvTxPower(tBTM_CMPL_CB *p_cb)
2096 {
2097 BOOLEAN ret;
2098 tBTM_TX_POWER_RESULTS result;
2099 /* If someone already waiting on the version, do not allow another */
2100 if (btm_cb.devcb.p_tx_power_cmpl_cb) {
2101 result.status = BTM_BUSY;
2102 (*p_cb)(&result);
2103 return (BTM_BUSY);
2104 }
2105
2106 btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
2107 btu_start_timer (&btm_cb.devcb.tx_power_timer, BTU_TTYPE_BTM_ACL,
2108 BTM_DEV_REPLY_TIMEOUT);
2109 ret = btsnd_hcic_ble_read_adv_chnl_tx_power();
2110
2111 if(!ret) {
2112 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2113 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2114 result.status = BTM_NO_RESOURCES;
2115 (*p_cb)(&result);
2116 return (BTM_NO_RESOURCES);
2117 } else {
2118 return BTM_CMD_STARTED;
2119 }
2120 }
2121
BTM_BleGetWhiteListSize(uint16_t * length)2122 void BTM_BleGetWhiteListSize(uint16_t *length)
2123 {
2124 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
2125 if (p_cb->white_list_avail_size == 0) {
2126 BTM_TRACE_WARNING("%s Whitelist full.", __func__);
2127 }
2128 *length = p_cb->white_list_avail_size;
2129 return;
2130 }
2131 #endif ///BLE_INCLUDED == 1
2132
2133 /*******************************************************************************
2134 **
2135 ** Function btm_read_tx_power_complete
2136 **
2137 ** Description This function is called when the command complete message
2138 ** is received from the HCI for the read tx power request.
2139 **
2140 ** Returns void
2141 **
2142 *******************************************************************************/
btm_read_tx_power_complete(UINT8 * p,BOOLEAN is_ble)2143 void btm_read_tx_power_complete (UINT8 *p, BOOLEAN is_ble)
2144 {
2145 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
2146 tBTM_TX_POWER_RESULTS results;
2147 UINT16 handle;
2148 tACL_CONN *p_acl_cb = NULL;
2149 BTM_TRACE_DEBUG ("btm_read_tx_power_complete\n");
2150 btu_stop_timer (&btm_cb.devcb.tx_power_timer);
2151
2152 /* If there was a callback registered for read rssi, call it */
2153 btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
2154
2155 if (p_cb) {
2156 STREAM_TO_UINT8 (results.hci_status, p);
2157
2158 if (results.hci_status == HCI_SUCCESS) {
2159 results.status = BTM_SUCCESS;
2160
2161 if (!is_ble) {
2162 STREAM_TO_UINT16 (handle, p);
2163 STREAM_TO_UINT8 (results.tx_power, p);
2164
2165 /* Search through the list of active channels for the correct BD Addr */
2166 p_acl_cb = btm_handle_to_acl(handle);
2167 if (p_acl_cb) {
2168 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2169 }
2170 }
2171 #if BLE_INCLUDED == 1
2172 else {
2173 STREAM_TO_UINT8 (results.tx_power, p);
2174 memcpy(results.rem_bda, btm_cb.devcb.read_tx_pwr_addr, BD_ADDR_LEN);
2175 }
2176 #endif
2177 BTM_TRACE_DEBUG ("BTM TX power Complete: tx_power %d, hci status 0x%02x\n",
2178 results.tx_power, results.hci_status);
2179 } else {
2180 results.status = BTM_ERR_PROCESSING;
2181 }
2182
2183 (*p_cb)(&results);
2184 }
2185 }
2186
2187 /*******************************************************************************
2188 **
2189 ** Function btm_read_rssi_complete
2190 **
2191 ** Description This function is called when the command complete message
2192 ** is received from the HCI for the read rssi request.
2193 **
2194 ** Returns void
2195 **
2196 *******************************************************************************/
btm_read_rssi_complete(UINT8 * p)2197 void btm_read_rssi_complete (UINT8 *p)
2198 {
2199 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
2200 tBTM_RSSI_RESULTS results;
2201 UINT16 handle;
2202 tACL_CONN *p_acl_cb = NULL;
2203 BTM_TRACE_DEBUG ("btm_read_rssi_complete\n");
2204 btu_stop_timer (&btm_cb.devcb.rssi_timer);
2205
2206 /* If there was a callback registered for read rssi, call it */
2207 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
2208
2209 if (p_cb) {
2210 STREAM_TO_UINT8 (results.hci_status, p);
2211
2212 if (results.hci_status == HCI_SUCCESS) {
2213 results.status = BTM_SUCCESS;
2214
2215 STREAM_TO_UINT16 (handle, p);
2216
2217 STREAM_TO_UINT8 (results.rssi, p);
2218 BTM_TRACE_DEBUG ("BTM RSSI Complete: rssi %d, hci status 0x%02x\n",
2219 results.rssi, results.hci_status);
2220
2221 /* Search through the list of active channels for the correct BD Addr */
2222 p_acl_cb = btm_handle_to_acl(handle);
2223 if (p_acl_cb) {
2224 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2225 }
2226 } else {
2227 results.status = BTM_ERR_PROCESSING;
2228 }
2229
2230 (*p_cb)(&results);
2231 }
2232 }
2233
2234 /*******************************************************************************
2235 **
2236 ** Function btm_read_link_quality_complete
2237 **
2238 ** Description This function is called when the command complete message
2239 ** is received from the HCI for the read link quality.
2240 **
2241 ** Returns void
2242 **
2243 *******************************************************************************/
btm_read_link_quality_complete(UINT8 * p)2244 void btm_read_link_quality_complete (UINT8 *p)
2245 {
2246 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_lnk_qual_cmpl_cb;
2247 tBTM_LINK_QUALITY_RESULTS results;
2248 UINT16 handle;
2249 tACL_CONN *p_acl_cb = NULL;
2250 BTM_TRACE_DEBUG ("btm_read_link_quality_complete\n");
2251 btu_stop_timer (&btm_cb.devcb.lnk_quality_timer);
2252
2253 /* If there was a callback registered for read rssi, call it */
2254 btm_cb.devcb.p_lnk_qual_cmpl_cb = NULL;
2255
2256 if (p_cb) {
2257 STREAM_TO_UINT8 (results.hci_status, p);
2258
2259 if (results.hci_status == HCI_SUCCESS) {
2260 results.status = BTM_SUCCESS;
2261
2262 STREAM_TO_UINT16 (handle, p);
2263
2264 STREAM_TO_UINT8 (results.link_quality, p);
2265 BTM_TRACE_DEBUG ("BTM Link Quality Complete: Link Quality %d, hci status 0x%02x\n",
2266 results.link_quality, results.hci_status);
2267
2268 /* Search through the list of active channels for the correct BD Addr */
2269 p_acl_cb = btm_handle_to_acl(handle);
2270 if (p_acl_cb) {
2271 memcpy (results.rem_bda, p_acl_cb->remote_addr, BD_ADDR_LEN);
2272 }
2273 } else {
2274 results.status = BTM_ERR_PROCESSING;
2275 }
2276
2277 (*p_cb)(&results);
2278 }
2279 }
2280
2281 /*******************************************************************************
2282 **
2283 ** Function btm_remove_acl
2284 **
2285 ** Description This function is called to disconnect an ACL connection
2286 **
2287 ** Returns BTM_SUCCESS if successfully initiated, otherwise BTM_NO_RESOURCES.
2288 **
2289 *******************************************************************************/
btm_remove_acl(BD_ADDR bd_addr,tBT_TRANSPORT transport)2290 tBTM_STATUS btm_remove_acl (BD_ADDR bd_addr, tBT_TRANSPORT transport)
2291 {
2292 UINT16 hci_handle = BTM_GetHCIConnHandle(bd_addr, transport);
2293 tBTM_STATUS status = BTM_SUCCESS;
2294
2295 BTM_TRACE_DEBUG ("btm_remove_acl\n");
2296 #if BTM_DISC_DURING_RS == 1
2297 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
2298
2299 /* Role Switch is pending, postpone until completed */
2300 if (p_dev_rec && (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING)) {
2301 p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
2302 } else /* otherwise can disconnect right away */
2303 #endif
2304 {
2305 if (hci_handle != 0xFFFF && p_dev_rec &&
2306 p_dev_rec->sec_state != BTM_SEC_STATE_DISCONNECTING) {
2307 if (!btsnd_hcic_disconnect (hci_handle, HCI_ERR_PEER_USER)) {
2308 status = BTM_NO_RESOURCES;
2309 }
2310 } else {
2311 status = BTM_UNKNOWN_ADDR;
2312 }
2313 }
2314
2315 return status;
2316 }
2317
2318
2319 /*******************************************************************************
2320 **
2321 ** Function BTM_SetTraceLevel
2322 **
2323 ** Description This function sets the trace level for BTM. If called with
2324 ** a value of 0xFF, it simply returns the current trace level.
2325 **
2326 ** Returns The new or current trace level
2327 **
2328 *******************************************************************************/
BTM_SetTraceLevel(UINT8 new_level)2329 UINT8 BTM_SetTraceLevel (UINT8 new_level)
2330 {
2331 BTM_TRACE_DEBUG ("BTM_SetTraceLevel\n");
2332 if (new_level != 0xFF) {
2333 btm_cb.trace_level = new_level;
2334 }
2335
2336 return (btm_cb.trace_level);
2337 }
2338
2339 /*******************************************************************************
2340 **
2341 ** Function btm_cont_rswitch
2342 **
2343 ** Description This function is called to continue processing an active
2344 ** role switch. It first disables encryption if enabled and
2345 ** EPR is not supported
2346 **
2347 ** Returns void
2348 **
2349 *******************************************************************************/
btm_cont_rswitch(tACL_CONN * p,tBTM_SEC_DEV_REC * p_dev_rec,UINT8 hci_status)2350 void btm_cont_rswitch (tACL_CONN *p, tBTM_SEC_DEV_REC *p_dev_rec,
2351 UINT8 hci_status)
2352 {
2353 BOOLEAN sw_ok = 1;
2354 BTM_TRACE_DEBUG ("btm_cont_rswitch\n");
2355 /* Check to see if encryption needs to be turned off if pending
2356 change of link key or role switch */
2357 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2358 /* Must turn off Encryption first if necessary */
2359 /* Some devices do not support switch or change of link key while encryption is on */
2360 if (p_dev_rec != NULL && ((p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) != 0)
2361 && !BTM_EPR_AVAILABLE(p)) {
2362 if (btsnd_hcic_set_conn_encrypt (p->hci_handle, 0)) {
2363 p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
2364 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2365 p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
2366 }
2367 } else {
2368 /* Error occurred; set states back to Idle */
2369 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2370 sw_ok = 0;
2371 }
2372 }
2373 } else /* Encryption not used or EPR supported, continue with switch
2374 and/or change of link key */
2375 {
2376 if (p->switch_role_state == BTM_ACL_SWKEY_STATE_MODE_CHANGE) {
2377 p->switch_role_state = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
2378 #if BTM_DISC_DURING_RS == 1
2379 if (p_dev_rec) {
2380 p_dev_rec->rs_disc_pending = BTM_SEC_RS_PENDING;
2381 }
2382 #endif
2383 sw_ok = btsnd_hcic_switch_role (p->remote_addr, (UINT8)!p->link_role);
2384 }
2385 }
2386
2387 if (!sw_ok) {
2388 p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
2389 btm_acl_report_role_change(hci_status, p->remote_addr);
2390 }
2391 }
2392 }
2393
2394 /*******************************************************************************
2395 **
2396 ** Function btm_acl_resubmit_page
2397 **
2398 ** Description send pending page request
2399 **
2400 *******************************************************************************/
btm_acl_resubmit_page(void)2401 void btm_acl_resubmit_page (void)
2402 {
2403 #if (SMP_INCLUDED == 1)
2404 tBTM_SEC_DEV_REC *p_dev_rec;
2405 BT_HDR *p_buf;
2406 UINT8 *pp;
2407 BD_ADDR bda;
2408 BTM_TRACE_DEBUG ("btm_acl_resubmit_page\n");
2409 /* If there were other page request schedule can start the next one */
2410 if ((p_buf = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
2411 /* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
2412 * for both create_conn and rmt_name */
2413 pp = (UINT8 *)(p_buf + 1) + p_buf->offset + 3;
2414
2415 STREAM_TO_BDADDR (bda, pp);
2416
2417 p_dev_rec = btm_find_or_alloc_dev (bda);
2418
2419 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2420 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2421
2422 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
2423 } else {
2424 btm_cb.paging = 0;
2425 }
2426 #endif ///SMP_INCLUDED == 1
2427 }
2428
2429 /*******************************************************************************
2430 **
2431 ** Function btm_acl_reset_paging
2432 **
2433 ** Description set paging to 0 and free the page queue - called at hci_reset
2434 **
2435 *******************************************************************************/
btm_acl_reset_paging(void)2436 void btm_acl_reset_paging (void)
2437 {
2438 BT_HDR *p;
2439 BTM_TRACE_DEBUG ("btm_acl_reset_paging\n");
2440 /* If we sent reset we are definitely not paging any more */
2441 while ((p = (BT_HDR *)fixed_queue_dequeue(btm_cb.page_queue, 0)) != NULL) {
2442 osi_free (p);
2443 }
2444
2445 btm_cb.paging = 0;
2446 }
2447
2448 /*******************************************************************************
2449 **
2450 ** Function btm_acl_paging
2451 **
2452 ** Description send a paging command or queue it in btm_cb
2453 **
2454 *******************************************************************************/
2455 #if (SMP_INCLUDED == 1 && CLASSIC_BT_INCLUDED == 1)
btm_acl_paging(BT_HDR * p,BD_ADDR bda)2456 void btm_acl_paging (BT_HDR *p, BD_ADDR bda)
2457 {
2458 tBTM_SEC_DEV_REC *p_dev_rec;
2459
2460 BTM_TRACE_DEBUG ("btm_acl_paging discing:%d, paging:%d BDA: %06x%06x\n",
2461 btm_cb.discing, btm_cb.paging,
2462 (bda[0] << 16) + (bda[1] << 8) + bda[2], (bda[3] << 16) + (bda[4] << 8) + bda[5]);
2463 if (btm_cb.discing) {
2464 btm_cb.paging = 1;
2465 fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
2466 } else {
2467 if (!BTM_ACL_IS_CONNECTED (bda)) {
2468 BTM_TRACE_DEBUG ("connecting_bda: %06x%06x\n",
2469 (btm_cb.connecting_bda[0] << 16) + (btm_cb.connecting_bda[1] << 8) +
2470 btm_cb.connecting_bda[2],
2471 (btm_cb.connecting_bda[3] << 16) + (btm_cb.connecting_bda[4] << 8) +
2472 btm_cb.connecting_bda[5]);
2473 if (btm_cb.paging &&
2474 memcmp (bda, btm_cb.connecting_bda, BD_ADDR_LEN) != 0) {
2475 fixed_queue_enqueue(btm_cb.page_queue, p, FIXED_QUEUE_MAX_TIMEOUT);
2476 } else {
2477 p_dev_rec = btm_find_or_alloc_dev (bda);
2478 memcpy (btm_cb.connecting_bda, p_dev_rec->bd_addr, BD_ADDR_LEN);
2479 memcpy (btm_cb.connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
2480
2481 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2482 }
2483
2484 btm_cb.paging = 1;
2485 } else { /* ACL is already up */
2486 btu_hcif_send_cmd (LOCAL_BR_EDR_CONTROLLER_ID, p);
2487 }
2488 }
2489 }
2490 #endif ///SMP_INCLUDED == 1
2491
2492 /*******************************************************************************
2493 **
2494 ** Function btm_acl_notif_conn_collision
2495 **
2496 ** Description Send connection collision event to upper layer if registered
2497 **
2498 ** Returns 1 if sent out to upper layer,
2499 ** 0 if no one needs the notification.
2500 **
2501 *******************************************************************************/
btm_acl_notif_conn_collision(BD_ADDR bda)2502 BOOLEAN btm_acl_notif_conn_collision (BD_ADDR bda)
2503 {
2504 tBTM_BL_EVENT_DATA evt_data;
2505
2506 /* Report possible collision to the upper layer. */
2507 if (btm_cb.p_bl_changed_cb) {
2508 BTM_TRACE_DEBUG ("btm_acl_notif_conn_collision: RemBdAddr: %02x%02x%02x%02x%02x%02x\n",
2509 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
2510
2511 evt_data.event = BTM_BL_COLLISION_EVT;
2512 evt_data.conn.p_bda = bda;
2513
2514 #if BLE_INCLUDED == 1
2515 evt_data.conn.transport = BT_TRANSPORT_BR_EDR;
2516 evt_data.conn.handle = BTM_INVALID_HCI_HANDLE;
2517 #endif
2518 (*btm_cb.p_bl_changed_cb)(&evt_data);
2519 return 1;
2520 } else {
2521 return 0;
2522 }
2523 }
2524
2525
2526 /*******************************************************************************
2527 **
2528 ** Function btm_acl_chk_peer_pkt_type_support
2529 **
2530 ** Description Check if peer supports requested packets
2531 **
2532 *******************************************************************************/
btm_acl_chk_peer_pkt_type_support(tACL_CONN * p,UINT16 * p_pkt_type)2533 void btm_acl_chk_peer_pkt_type_support (tACL_CONN *p, UINT16 *p_pkt_type)
2534 {
2535 /* 3 and 5 slot packets? */
2536 if (!HCI_3_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2537 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3);
2538 }
2539
2540 if (!HCI_5_SLOT_PACKETS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2541 *p_pkt_type &= ~(BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5);
2542 }
2543
2544 /* 2 and 3 MPS support? */
2545 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2546 /* Not supported. Add 'not_supported' mask for all 2MPS packet types */
2547 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
2548 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
2549 }
2550
2551 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2552 /* Not supported. Add 'not_supported' mask for all 3MPS packet types */
2553 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
2554 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2555 }
2556
2557 /* EDR 3 and 5 slot support? */
2558 if (HCI_EDR_ACL_2MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])
2559 || HCI_EDR_ACL_3MPS_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0])) {
2560 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2561 /* Not supported. Add 'not_supported' mask for all 3-slot EDR packet types */
2562 {
2563 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
2564 }
2565
2566 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
2567 /* Not supported. Add 'not_supported' mask for all 5-slot EDR packet types */
2568 {
2569 *p_pkt_type |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 + BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
2570 }
2571 }
2572 }
2573
2574 /*******************************************************************************
2575 **
2576 ** Function btm_acl_free
2577 **
2578 ** Description Free acl specific lists from btm control block
2579 **
2580 *******************************************************************************/
btm_acl_free(void)2581 void btm_acl_free(void)
2582 {
2583 list_free(btm_cb.p_acl_db_list);
2584 list_free(btm_cb.p_pm_mode_db_list);
2585 }
2586