1 /******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains functions that handle BTM interface functions for the
22 * Bluetooth device including Rest, HCI buffer size and others
23 *
24 ******************************************************************************/
25
26 #include <stdlib.h>
27 #include <string.h>
28 //#include <stdio.h>
29 #include <stddef.h>
30 #include "common/bt_trace.h"
31 #include "stack/bt_types.h"
32 //#include "bt_utils.h"
33 #include "btm_int.h"
34 #include "stack/btu.h"
35 #include "device/controller.h"
36 #include "hci/hci_layer.h"
37 #include "stack/hcimsgs.h"
38 #include "l2c_int.h"
39 //#include "btcore/include/module.h"
40 //#include "osi/include/osi/thread.h"
41
42 #if BLE_INCLUDED == TRUE
43 #include "gatt_int.h"
44 #endif /* BLE_INCLUDED */
45
46 //extern thread_t *bt_workqueue_thread;
47
48 /********************************************************************************/
49 /* L O C A L D A T A D E F I N I T I O N S */
50 /********************************************************************************/
51
52 #ifndef BTM_DEV_RESET_TIMEOUT
53 #define BTM_DEV_RESET_TIMEOUT 4
54 #endif
55
56 #define BTM_DEV_REPLY_TIMEOUT 2 /* 1 second expiration time is not good. Timer may start between 0 and 1 second. */
57 /* if it starts at the very end of the 0 second, timer will expire really easily. */
58
59 #define BTM_INFO_TIMEOUT 5 /* 5 seconds for info response */
60
61 /********************************************************************************/
62 /* L O C A L F U N C T I O N P R O T O T Y P E S */
63 /********************************************************************************/
64
65 static void btm_decode_ext_features_page (UINT8 page_number, const BD_FEATURES p_features);
66
67 /*******************************************************************************
68 **
69 ** Function btm_dev_init
70 **
71 ** Description This function is on the BTM startup
72 **
73 ** Returns void
74 **
75 *******************************************************************************/
btm_dev_init(void)76 void btm_dev_init (void)
77 {
78 #if 0 /* cleared in btm_init; put back in if called from anywhere else! */
79 memset (&btm_cb.devcb, 0, sizeof (tBTM_DEVCB));
80 #endif
81
82 /* Initialize nonzero defaults */
83 #if (BTM_MAX_LOC_BD_NAME_LEN > 0)
84 memset(btm_cb.cfg.bd_name, 0, sizeof(tBTM_LOC_BD_NAME));
85 #endif
86
87 btm_cb.devcb.reset_timer.param = (TIMER_PARAM_TYPE)TT_DEV_RESET;
88 btm_cb.devcb.rln_timer.param = (TIMER_PARAM_TYPE)TT_DEV_RLN;
89
90 btm_cb.btm_acl_pkt_types_supported = BTM_ACL_PKT_TYPES_MASK_DH1 + BTM_ACL_PKT_TYPES_MASK_DM1 +
91 BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3 +
92 BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5;
93
94 btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1 +
95 BTM_SCO_PKT_TYPES_MASK_HV2 +
96 BTM_SCO_PKT_TYPES_MASK_HV3 +
97 BTM_SCO_PKT_TYPES_MASK_EV3 +
98 BTM_SCO_PKT_TYPES_MASK_EV4 +
99 BTM_SCO_PKT_TYPES_MASK_EV5;
100 }
101
102
103 /*******************************************************************************
104 **
105 ** Function btm_db_reset
106 **
107 ** Description This function is called by BTM_DeviceReset and clears out any
108 ** pending callbacks for inquiries, discoveries, other pending
109 ** functions that may be in progress.
110 **
111 ** Returns void
112 **
113 *******************************************************************************/
btm_db_reset(void)114 static void btm_db_reset (void)
115 {
116 tBTM_CMPL_CB *p_cb;
117 tBTM_STATUS status = BTM_DEV_RESET;
118
119 btm_inq_db_reset();
120
121 if (btm_cb.devcb.p_rln_cmpl_cb) {
122 p_cb = btm_cb.devcb.p_rln_cmpl_cb;
123 btm_cb.devcb.p_rln_cmpl_cb = NULL;
124
125 if (p_cb) {
126 (*p_cb)((void *) NULL);
127 }
128 }
129
130 if (btm_cb.devcb.p_rssi_cmpl_cb) {
131 p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
132 btm_cb.devcb.p_rssi_cmpl_cb = NULL;
133
134 if (p_cb) {
135 (*p_cb)((tBTM_RSSI_RESULTS *) &status);
136 }
137 }
138 }
139
reset_complete(void)140 static void reset_complete(void)
141 {
142 const controller_t *controller = controller_get_interface();
143
144 /* Tell L2CAP that all connections are gone */
145 l2cu_device_reset ();
146 #if (SMP_INCLUDED == TRUE)
147 /* Clear current security state */
148 {
149 list_node_t *p_node = NULL;
150 for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
151 tBTM_SEC_DEV_REC *p_dev_rec = (tBTM_SEC_DEV_REC *) list_node(p_node);
152 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
153 }
154 }
155 #endif ///SMP_INCLUDED == TRUE
156 /* After the reset controller should restore all parameters to defaults. */
157 btm_cb.btm_inq_vars.inq_counter = 1;
158 btm_cb.btm_inq_vars.inq_scan_window = HCI_DEF_INQUIRYSCAN_WINDOW;
159 btm_cb.btm_inq_vars.inq_scan_period = HCI_DEF_INQUIRYSCAN_INTERVAL;
160 btm_cb.btm_inq_vars.inq_scan_type = HCI_DEF_SCAN_TYPE;
161
162 btm_cb.btm_inq_vars.page_scan_window = HCI_DEF_PAGESCAN_WINDOW;
163 btm_cb.btm_inq_vars.page_scan_period = HCI_DEF_PAGESCAN_INTERVAL;
164 btm_cb.btm_inq_vars.page_scan_type = HCI_DEF_SCAN_TYPE;
165
166 #if (BLE_INCLUDED == TRUE)
167 btm_cb.ble_ctr_cb.conn_state = BLE_CONN_IDLE;
168 btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_NONE;
169 btm_cb.ble_ctr_cb.p_select_cback = NULL;
170 gatt_reset_bgdev_list();
171 btm_ble_multi_adv_init();
172 #endif
173
174 btm_pm_reset();
175
176 l2c_link_processs_num_bufs(controller->get_acl_buffer_count_classic());
177 #if BTM_SCO_HCI_INCLUDED == TRUE
178 btm_sco_process_num_bufs(controller->get_sco_buffer_count());
179 #endif
180 #if (BLE_INCLUDED == TRUE)
181
182 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
183 /* Set up the BLE privacy settings */
184 if (controller->supports_ble() && controller->supports_ble_privacy() &&
185 controller->get_ble_resolving_list_max_size() > 0) {
186 btm_ble_resolving_list_init(controller->get_ble_resolving_list_max_size());
187 /* set the default random private address timeout */
188 btsnd_hcic_ble_set_rand_priv_addr_timeout(BTM_BLE_PRIVATE_ADDR_INT);
189 }
190 #endif
191
192 if (controller->supports_ble()) {
193 btm_ble_white_list_init(controller->get_ble_white_list_size());
194 l2c_link_processs_ble_num_bufs(controller->get_acl_buffer_count_ble());
195 }
196 #endif
197 #if (SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE)
198 BTM_SetPinType (btm_cb.cfg.pin_type, btm_cb.cfg.pin_code, btm_cb.cfg.pin_code_len);
199 #endif ///SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE
200 for (int i = 0; i <= controller->get_last_features_classic_index(); i++) {
201 btm_decode_ext_features_page(i, controller->get_features_classic(i)->as_array);
202 }
203
204 btm_report_device_status(BTM_DEV_STATUS_UP);
205 }
206
207 // TODO(zachoverflow): remove this function
BTM_DeviceReset(UNUSED_ATTR tBTM_CMPL_CB * p_cb)208 void BTM_DeviceReset (UNUSED_ATTR tBTM_CMPL_CB *p_cb)
209 {
210 /* Flush all ACL connections */
211 btm_acl_device_down();
212
213 /* Clear the callback, so application would not hang on reset */
214 btm_db_reset();
215
216 /* todo: review the below logic; start_up executes under another task context
217 * reset_complete runs in btu task */
218 controller_get_interface()->start_up();
219 reset_complete();
220 }
221
222 /*******************************************************************************
223 **
224 ** Function BTM_IsDeviceUp
225 **
226 ** Description This function is called to check if the device is up.
227 **
228 ** Returns TRUE if device is up, else FALSE
229 **
230 *******************************************************************************/
BTM_IsDeviceUp(void)231 BOOLEAN BTM_IsDeviceUp (void)
232 {
233 return controller_get_interface()->get_is_ready();
234 }
235
236 /*******************************************************************************
237 **
238 ** Function btm_dev_timeout
239 **
240 ** Description This function is called when a timer list entry expires.
241 **
242 ** Returns void
243 **
244 *******************************************************************************/
btm_dev_timeout(TIMER_LIST_ENT * p_tle)245 void btm_dev_timeout (TIMER_LIST_ENT *p_tle)
246 {
247 TIMER_PARAM_TYPE timer_type = (TIMER_PARAM_TYPE)p_tle->param;
248
249 if (timer_type == (TIMER_PARAM_TYPE)TT_DEV_RLN) {
250 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
251
252 btm_cb.devcb.p_rln_cmpl_cb = NULL;
253
254 if (p_cb) {
255 (*p_cb)((void *) NULL);
256 }
257 }
258 }
259
260 /*******************************************************************************
261 **
262 ** Function btm_decode_ext_features_page
263 **
264 ** Description This function is decodes a features page.
265 **
266 ** Returns void
267 **
268 *******************************************************************************/
btm_decode_ext_features_page(UINT8 page_number,const BD_FEATURES p_features)269 static void btm_decode_ext_features_page (UINT8 page_number, const BD_FEATURES p_features)
270 {
271 BTM_TRACE_DEBUG ("btm_decode_ext_features_page page: %d", page_number);
272 switch (page_number) {
273 /* Extended (Legacy) Page 0 */
274 case HCI_EXT_FEATURES_PAGE_0:
275
276 /* Create ACL supported packet types mask */
277 btm_cb.btm_acl_pkt_types_supported = (BTM_ACL_PKT_TYPES_MASK_DH1 +
278 BTM_ACL_PKT_TYPES_MASK_DM1);
279
280 if (HCI_3_SLOT_PACKETS_SUPPORTED(p_features)) {
281 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH3 +
282 BTM_ACL_PKT_TYPES_MASK_DM3);
283 }
284
285 if (HCI_5_SLOT_PACKETS_SUPPORTED(p_features)) {
286 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH5 +
287 BTM_ACL_PKT_TYPES_MASK_DM5);
288 }
289
290 /* Add in EDR related ACL types */
291 if (!HCI_EDR_ACL_2MPS_SUPPORTED(p_features)) {
292 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 +
293 BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
294 BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
295 }
296
297 if (!HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
298 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 +
299 BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
300 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
301 }
302
303 /* Check to see if 3 and 5 slot packets are available */
304 if (HCI_EDR_ACL_2MPS_SUPPORTED(p_features) ||
305 HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
306 if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p_features)) {
307 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
308 BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
309 }
310
311 if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p_features)) {
312 btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 +
313 BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
314 }
315 }
316
317 BTM_TRACE_DEBUG("Local supported ACL packet types: 0x%04x",
318 btm_cb.btm_acl_pkt_types_supported);
319
320 /* Create (e)SCO supported packet types mask */
321 btm_cb.btm_sco_pkt_types_supported = 0;
322 #if BTM_SCO_INCLUDED == TRUE
323 btm_cb.sco_cb.esco_supported = FALSE;
324 #endif
325 if (HCI_SCO_LINK_SUPPORTED(p_features)) {
326 btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1;
327
328 if (HCI_HV2_PACKETS_SUPPORTED(p_features)) {
329 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV2;
330 }
331
332 if (HCI_HV3_PACKETS_SUPPORTED(p_features)) {
333 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV3;
334 }
335 }
336
337 if (HCI_ESCO_EV3_SUPPORTED(p_features)) {
338 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV3;
339 }
340
341 if (HCI_ESCO_EV4_SUPPORTED(p_features)) {
342 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV4;
343 }
344
345 if (HCI_ESCO_EV5_SUPPORTED(p_features)) {
346 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV5;
347 }
348 #if BTM_SCO_INCLUDED == TRUE
349 if (btm_cb.btm_sco_pkt_types_supported & BTM_ESCO_LINK_ONLY_MASK) {
350 btm_cb.sco_cb.esco_supported = TRUE;
351
352 /* Add in EDR related eSCO types */
353 if (HCI_EDR_ESCO_2MPS_SUPPORTED(p_features)) {
354 if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
355 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_2_EV5;
356 }
357 } else {
358 btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_2_EV3 +
359 BTM_SCO_PKT_TYPES_MASK_NO_2_EV5);
360 }
361
362 if (HCI_EDR_ESCO_3MPS_SUPPORTED(p_features)) {
363 if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
364 btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_3_EV5;
365 }
366 } else {
367 btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_3_EV3 +
368 BTM_SCO_PKT_TYPES_MASK_NO_3_EV5);
369 }
370 }
371 #endif
372
373 BTM_TRACE_DEBUG("Local supported SCO packet types: 0x%04x",
374 btm_cb.btm_sco_pkt_types_supported);
375
376 /* Create Default Policy Settings */
377 if (HCI_SWITCH_SUPPORTED(p_features)) {
378 btm_cb.btm_def_link_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
379 } else {
380 btm_cb.btm_def_link_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
381 }
382
383 if (HCI_HOLD_MODE_SUPPORTED(p_features)) {
384 btm_cb.btm_def_link_policy |= HCI_ENABLE_HOLD_MODE;
385 } else {
386 btm_cb.btm_def_link_policy &= ~HCI_ENABLE_HOLD_MODE;
387 }
388
389 if (HCI_SNIFF_MODE_SUPPORTED(p_features)) {
390 btm_cb.btm_def_link_policy |= HCI_ENABLE_SNIFF_MODE;
391 } else {
392 btm_cb.btm_def_link_policy &= ~HCI_ENABLE_SNIFF_MODE;
393 }
394
395 if (HCI_PARK_MODE_SUPPORTED(p_features)) {
396 btm_cb.btm_def_link_policy |= HCI_ENABLE_PARK_MODE;
397 } else {
398 btm_cb.btm_def_link_policy &= ~HCI_ENABLE_PARK_MODE;
399 }
400
401 btm_sec_dev_reset ();
402
403 if (HCI_LMP_INQ_RSSI_SUPPORTED(p_features)) {
404 if (HCI_EXT_INQ_RSP_SUPPORTED(p_features)) {
405 BTM_SetInquiryMode (BTM_INQ_RESULT_EXTENDED);
406 } else {
407 BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
408 }
409 }
410
411 #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
412 if ( HCI_NON_FLUSHABLE_PB_SUPPORTED(p_features)) {
413 l2cu_set_non_flushable_pbf(TRUE);
414 } else {
415 l2cu_set_non_flushable_pbf(FALSE);
416 }
417 #endif
418 BTM_SetPageScanType (BTM_DEFAULT_SCAN_TYPE);
419 BTM_SetInquiryScanType (BTM_DEFAULT_SCAN_TYPE);
420
421 break;
422
423 /* Extended Page 1 */
424 case HCI_EXT_FEATURES_PAGE_1:
425 /* Nothing to do for page 1 */
426 break;
427
428 /* Extended Page 2 */
429 case HCI_EXT_FEATURES_PAGE_2:
430 /* Nothing to do for page 2 */
431 break;
432
433 default:
434 BTM_TRACE_ERROR("btm_decode_ext_features_page page=%d unknown", page_number);
435 break;
436 }
437 }
438
439 /*******************************************************************************
440 **
441 ** Function BTM_SetLocalDeviceName
442 **
443 ** Description This function is called to set the local device name.
444 **
445 ** Returns status of the operation
446 **
447 *******************************************************************************/
BTM_SetLocalDeviceName(char * p_name)448 tBTM_STATUS BTM_SetLocalDeviceName (char *p_name)
449 {
450 UINT8 *p;
451
452 if (!p_name || !p_name[0] || (strlen ((char *)p_name) > BD_NAME_LEN)) {
453 return (BTM_ILLEGAL_VALUE);
454 }
455
456 if (!controller_get_interface()->get_is_ready()) {
457 return (BTM_DEV_RESET);
458 }
459
460 #if BTM_MAX_LOC_BD_NAME_LEN > 0
461 /* Save the device name if local storage is enabled */
462 p = (UINT8 *)btm_cb.cfg.bd_name;
463 if (p != (UINT8 *)p_name) {
464 BCM_STRNCPY_S(btm_cb.cfg.bd_name, p_name, BTM_MAX_LOC_BD_NAME_LEN);
465 btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0';
466 }
467 #else
468 p = (UINT8 *)p_name;
469 #endif
470 #if CLASSIC_BT_INCLUDED
471 if (btsnd_hcic_change_name(p)) {
472 return (BTM_CMD_STARTED);
473 } else
474 #endif
475 {
476 return (BTM_NO_RESOURCES);
477 }
478 }
479
480
481
482 /*******************************************************************************
483 **
484 ** Function BTM_ReadLocalDeviceName
485 **
486 ** Description This function is called to read the local device name.
487 **
488 ** Returns status of the operation
489 ** If success, BTM_SUCCESS is returned and p_name points stored
490 ** local device name
491 ** If BTM doesn't store local device name, BTM_NO_RESOURCES is
492 ** is returned and p_name is set to NULL
493 **
494 *******************************************************************************/
BTM_ReadLocalDeviceName(char ** p_name)495 tBTM_STATUS BTM_ReadLocalDeviceName (char **p_name)
496 {
497 #if BTM_MAX_LOC_BD_NAME_LEN > 0
498 *p_name = btm_cb.cfg.bd_name;
499 return (BTM_SUCCESS);
500 #else
501 *p_name = NULL;
502 return (BTM_NO_RESOURCES);
503 #endif
504 }
505
506
507 /*******************************************************************************
508 **
509 ** Function BTM_ReadLocalDeviceNameFromController
510 **
511 ** Description Get local device name from controller. Do not use cached
512 ** name (used to get chip-id prior to btm reset complete).
513 **
514 ** Returns BTM_CMD_STARTED if successful, otherwise an error
515 **
516 *******************************************************************************/
BTM_ReadLocalDeviceNameFromController(tBTM_CMPL_CB * p_rln_cmpl_cback)517 tBTM_STATUS BTM_ReadLocalDeviceNameFromController (tBTM_CMPL_CB *p_rln_cmpl_cback)
518 {
519 /* Check if rln already in progress */
520 if (btm_cb.devcb.p_rln_cmpl_cb) {
521 return (BTM_NO_RESOURCES);
522 }
523
524 /* Save callback */
525 btm_cb.devcb.p_rln_cmpl_cb = p_rln_cmpl_cback;
526
527 btsnd_hcic_read_name();
528 btu_start_timer (&btm_cb.devcb.rln_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
529
530 return BTM_CMD_STARTED;
531 }
532
533 /*******************************************************************************
534 **
535 ** Function btm_read_local_name_complete
536 **
537 ** Description This function is called when local name read complete.
538 ** message is received from the HCI.
539 **
540 ** Returns void
541 **
542 *******************************************************************************/
btm_read_local_name_complete(UINT8 * p,UINT16 evt_len)543 void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len)
544 {
545 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
546 UINT8 status;
547 UNUSED(evt_len);
548
549 btu_free_timer (&btm_cb.devcb.rln_timer);
550
551 /* If there was a callback address for read local name, call it */
552 btm_cb.devcb.p_rln_cmpl_cb = NULL;
553
554 if (p_cb) {
555 STREAM_TO_UINT8 (status, p);
556
557 if (status == HCI_SUCCESS) {
558 (*p_cb)(p);
559 } else {
560 (*p_cb)(NULL);
561 }
562 }
563 }
564
565 /*******************************************************************************
566 **
567 ** Function BTM_SetDeviceClass
568 **
569 ** Description This function is called to set the local device class
570 **
571 ** Returns status of the operation
572 **
573 *******************************************************************************/
BTM_SetDeviceClass(DEV_CLASS dev_class)574 tBTM_STATUS BTM_SetDeviceClass (DEV_CLASS dev_class)
575 {
576 if (!memcmp (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN)) {
577 return (BTM_SUCCESS);
578 }
579
580 memcpy (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN);
581
582 if (!controller_get_interface()->get_is_ready()) {
583 return (BTM_DEV_RESET);
584 }
585
586 if (!btsnd_hcic_write_dev_class (dev_class)) {
587 return (BTM_NO_RESOURCES);
588 }
589
590 return (BTM_SUCCESS);
591 }
592
593
594 /*******************************************************************************
595 **
596 ** Function BTM_ReadDeviceClass
597 **
598 ** Description This function is called to read the local device class
599 **
600 ** Returns pointer to the device class
601 **
602 *******************************************************************************/
BTM_ReadDeviceClass(void)603 UINT8 *BTM_ReadDeviceClass (void)
604 {
605 return ((UINT8 *)btm_cb.devcb.dev_class);
606 }
607
608
609 /*******************************************************************************
610 **
611 ** Function BTM_ReadLocalFeatures
612 **
613 ** Description This function is called to read the local features
614 **
615 ** Returns pointer to the local features string
616 **
617 *******************************************************************************/
618 // TODO(zachoverflow): get rid of this function
BTM_ReadLocalFeatures(void)619 UINT8 *BTM_ReadLocalFeatures (void)
620 {
621 // Discarding const modifier for now, until this function dies
622 return (UINT8 *)controller_get_interface()->get_features_classic(0)->as_array;
623 }
624
625 /*******************************************************************************
626 **
627 ** Function BTM_RegisterForDeviceStatusNotif
628 **
629 ** Description This function is called to register for device status
630 ** change notifications.
631 **
632 ** If one registration is already there calling function should
633 ** save the pointer to the function that is return and
634 ** call it when processing of the event is complete
635 **
636 ** Returns status of the operation
637 **
638 *******************************************************************************/
BTM_RegisterForDeviceStatusNotif(tBTM_DEV_STATUS_CB * p_cb)639 tBTM_DEV_STATUS_CB *BTM_RegisterForDeviceStatusNotif (tBTM_DEV_STATUS_CB *p_cb)
640 {
641 tBTM_DEV_STATUS_CB *p_prev = btm_cb.devcb.p_dev_status_cb;
642
643 btm_cb.devcb.p_dev_status_cb = p_cb;
644 return (p_prev);
645 }
646
647 /*******************************************************************************
648 **
649 ** Function BTM_VendorSpecificCommand
650 **
651 ** Description Send a vendor specific HCI command to the controller.
652 **
653 ** Returns
654 ** BTM_SUCCESS Command sent. Does not expect command complete
655 ** event. (command cmpl callback param is NULL)
656 ** BTM_CMD_STARTED Command sent. Waiting for command cmpl event.
657 **
658 ** Notes
659 ** Opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC.
660 **
661 *******************************************************************************/
BTM_VendorSpecificCommand(UINT16 opcode,UINT8 param_len,UINT8 * p_param_buf,tBTM_VSC_CMPL_CB * p_cb)662 tBTM_STATUS BTM_VendorSpecificCommand(UINT16 opcode, UINT8 param_len,
663 UINT8 *p_param_buf, tBTM_VSC_CMPL_CB *p_cb)
664 {
665 BT_HDR *p_buf;
666
667 BTM_TRACE_EVENT ("BTM: BTM_VendorSpecificCommand: Opcode: 0x%04X, ParamLen: %i.",
668 opcode, param_len);
669
670 /* Allocate a buffer to hold HCI command plus the callback function */
671 if ((p_buf = HCI_GET_CMD_BUF(param_len)) != NULL) {
672 /* Send the HCI command (opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC) */
673 btsnd_hcic_vendor_spec_cmd (p_buf, opcode, param_len, p_param_buf, (void *)p_cb);
674
675 /* Return value */
676 if (p_cb != NULL) {
677 return (BTM_CMD_STARTED);
678 } else {
679 return (BTM_SUCCESS);
680 }
681 } else {
682 return (BTM_NO_RESOURCES);
683 }
684
685 }
686
687
688 /*******************************************************************************
689 **
690 ** Function btm_vsc_complete
691 **
692 ** Description This function is called when local HCI Vendor Specific
693 ** Command complete message is received from the HCI.
694 **
695 ** Returns void
696 **
697 *******************************************************************************/
btm_vsc_complete(UINT8 * p,UINT16 opcode,UINT16 evt_len,tBTM_CMPL_CB * p_vsc_cplt_cback)698 void btm_vsc_complete (UINT8 *p, UINT16 opcode, UINT16 evt_len,
699 tBTM_CMPL_CB *p_vsc_cplt_cback)
700 {
701 #if (BLE_INCLUDED == TRUE)
702 tBTM_BLE_CB *ble_cb = &btm_cb.ble_ctr_cb;
703 switch(opcode) {
704 case HCI_VENDOR_BLE_LONG_ADV_DATA:
705 BTM_TRACE_EVENT("Set long adv data complete\n");
706 break;
707 case HCI_VENDOR_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST: {
708 uint8_t subcode, status; uint32_t length;
709 STREAM_TO_UINT8(status, p);
710 STREAM_TO_UINT8(subcode, p);
711 STREAM_TO_UINT32(length, p);
712 if(ble_cb && ble_cb->update_exceptional_list_cmp_cb) {
713 (*ble_cb->update_exceptional_list_cmp_cb)(status, subcode, length, p);
714 }
715 break;
716 }
717 case HCI_VENDOR_BLE_CLEAR_ADV: {
718 uint8_t status;
719 STREAM_TO_UINT8(status, p);
720 if (ble_cb && ble_cb->inq_var.p_clear_adv_cb) {
721 ble_cb->inq_var.p_clear_adv_cb(status);
722 }
723 break;
724 }
725 case HCI_VENDOR_BLE_SET_CSA_SUPPORT: {
726 uint8_t status;
727 STREAM_TO_UINT8(status, p);
728 if (ble_cb && ble_cb->set_csa_support_cmpl_cb) {
729 ble_cb->set_csa_support_cmpl_cb(status);
730 }
731 break;
732 }
733 default:
734 break;
735 }
736 #endif // (BLE_INCLUDED == TRUE)
737 tBTM_VSC_CMPL vcs_cplt_params;
738
739 /* If there was a callback address for vcs complete, call it */
740 if (p_vsc_cplt_cback) {
741 /* Pass paramters to the callback function */
742 vcs_cplt_params.opcode = opcode; /* Number of bytes in return info */
743 vcs_cplt_params.param_len = evt_len; /* Number of bytes in return info */
744 vcs_cplt_params.p_param_buf = p;
745 (*p_vsc_cplt_cback)(&vcs_cplt_params); /* Call the VSC complete callback function */
746 }
747
748 }
749
750 /*******************************************************************************
751 **
752 ** Function BTM_RegisterForVSEvents
753 **
754 ** Description This function is called to register/deregister for vendor
755 ** specific HCI events.
756 **
757 ** If is_register=TRUE, then the function will be registered;
758 ** if is_register=FALSE, then the function will be deregistered.
759 **
760 ** Returns BTM_SUCCESS if successful,
761 ** BTM_BUSY if maximum number of callbacks have already been
762 ** registered.
763 **
764 *******************************************************************************/
BTM_RegisterForVSEvents(tBTM_VS_EVT_CB * p_cb,BOOLEAN is_register)765 tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
766 {
767 tBTM_STATUS retval = BTM_SUCCESS;
768 UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
769
770 /* See if callback is already registered */
771 for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
772 if (btm_cb.devcb.p_vend_spec_cb[i] == NULL) {
773 /* Found a free slot. Store index */
774 free_idx = i;
775 } else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb) {
776 /* Found callback in lookup table. If deregistering, clear the entry. */
777 if (is_register == FALSE) {
778 btm_cb.devcb.p_vend_spec_cb[i] = NULL;
779 BTM_TRACE_EVENT("BTM Deregister For VSEvents is successfully");
780 }
781 return (BTM_SUCCESS);
782 }
783 }
784
785 /* Didn't find callback. Add callback to free slot if registering */
786 if (is_register) {
787 if (free_idx < BTM_MAX_VSE_CALLBACKS) {
788 btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
789 BTM_TRACE_EVENT("BTM Register For VSEvents is successfully");
790 } else {
791 /* No free entries available */
792 BTM_TRACE_ERROR ("BTM_RegisterForVSEvents: too many callbacks registered");
793
794 retval = BTM_NO_RESOURCES;
795 }
796 }
797
798 return (retval);
799 }
800
801 /*******************************************************************************
802 **
803 ** Function btm_vendor_specific_evt
804 **
805 ** Description Process event HCI_VENDOR_SPECIFIC_EVT
806 **
807 ** Note: Some controllers do not send command complete, so
808 ** the callback and busy flag are cleared here also.
809 **
810 ** Returns void
811 **
812 *******************************************************************************/
btm_vendor_specific_evt(UINT8 * p,UINT8 evt_len)813 void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
814 {
815 UINT8 i;
816
817 #if (CLASSIC_BT_INCLUDED == TRUE)
818 UINT8 sub_event;
819 UINT8 *p_evt = p;
820
821 STREAM_TO_UINT8(sub_event, p_evt);
822 /* Check in subevent if authentication is through Legacy Authentication. */
823 if (sub_event == ESP_VS_REM_LEGACY_AUTH_CMP) {
824 UINT16 hci_handle;
825 STREAM_TO_UINT16(hci_handle, p_evt);
826 btm_sec_handle_remote_legacy_auth_cmp(hci_handle);
827 }
828 #endif /// (CLASSIC_BT_INCLUDED == TRUE)
829 for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
830 if (btm_cb.devcb.p_vend_spec_cb[i]) {
831 (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
832 }
833 }
834 BTM_TRACE_DEBUG ("BTM Event: Vendor Specific event from controller");
835 }
836
837
838 /*******************************************************************************
839 **
840 ** Function BTM_WritePageTimeout
841 **
842 ** Description Send HCI Write Page Timeout.
843 **
844 ** Returns
845 ** BTM_SUCCESS Command sent.
846 ** BTM_NO_RESOURCES If out of resources to send the command.
847 **
848 **
849 *******************************************************************************/
BTM_WritePageTimeout(UINT16 timeout)850 tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout)
851 {
852 BTM_TRACE_EVENT ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
853
854 /* Send the HCI command */
855 if (btsnd_hcic_write_page_tout (timeout)) {
856 return (BTM_SUCCESS);
857 } else {
858 return (BTM_NO_RESOURCES);
859 }
860 }
861
862 /*******************************************************************************
863 **
864 ** Function BTM_WriteVoiceSettings
865 **
866 ** Description Send HCI Write Voice Settings command.
867 ** See stack/hcidefs.h for settings bitmask values.
868 **
869 ** Returns
870 ** BTM_SUCCESS Command sent.
871 ** BTM_NO_RESOURCES If out of resources to send the command.
872 **
873 **
874 *******************************************************************************/
BTM_WriteVoiceSettings(UINT16 settings)875 tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
876 {
877 BTM_TRACE_EVENT ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
878
879 /* Send the HCI command */
880 if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff))) {
881 return (BTM_SUCCESS);
882 }
883
884 return (BTM_NO_RESOURCES);
885 }
886
887 /*******************************************************************************
888 **
889 ** Function BTM_EnableTestMode
890 **
891 ** Description Send HCI the enable device under test command.
892 **
893 ** Note: Controller can only be taken out of this mode by
894 ** resetting the controller.
895 **
896 ** Returns
897 ** BTM_SUCCESS Command sent.
898 ** BTM_NO_RESOURCES If out of resources to send the command.
899 **
900 **
901 *******************************************************************************/
BTM_EnableTestMode(void)902 tBTM_STATUS BTM_EnableTestMode(void)
903 {
904 UINT8 cond;
905
906 BTM_TRACE_EVENT ("BTM: BTM_EnableTestMode");
907
908 /* set auto accept connection as this is needed during test mode */
909 /* Allocate a buffer to hold HCI command */
910 cond = HCI_DO_AUTO_ACCEPT_CONNECT;
911 if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
912 HCI_FILTER_COND_NEW_DEVICE,
913 &cond, sizeof(cond))) {
914 return (BTM_NO_RESOURCES);
915 }
916
917 /* put device to connectable mode */
918 if (BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
919 BTM_DEFAULT_CONN_INTERVAL) != BTM_SUCCESS) {
920 return BTM_NO_RESOURCES;
921 }
922
923 /* put device to discoverable mode */
924 if (BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
925 BTM_DEFAULT_DISC_INTERVAL) != BTM_SUCCESS) {
926 return BTM_NO_RESOURCES;
927 }
928
929 /* mask off all of event from controller */
930 hci_layer_get_interface()->transmit_command(
931 hci_packet_factory_get_interface()->make_set_event_mask((const bt_event_mask_t *)("\x00\x00\x00\x00\x00\x00\x00\x00")),
932 NULL,
933 NULL,
934 NULL);
935
936 /* Send the HCI command */
937 if (btsnd_hcic_enable_test_mode ()) {
938 return (BTM_SUCCESS);
939 } else {
940 return (BTM_NO_RESOURCES);
941 }
942 }
943
944 /*******************************************************************************
945 **
946 ** Function BTM_DeleteStoredLinkKey
947 **
948 ** Description This function is called to delete link key for the specified
949 ** device addresses from the NVRAM storage attached to the Bluetooth
950 ** controller.
951 **
952 ** Parameters: bd_addr - Addresses of the devices
953 ** p_cb - Call back function to be called to return
954 ** the results
955 **
956 *******************************************************************************/
BTM_DeleteStoredLinkKey(BD_ADDR bd_addr,tBTM_CMPL_CB * p_cb)957 tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
958 {
959 BD_ADDR local_bd_addr = {0};
960 BOOLEAN delete_all_flag = FALSE;
961
962 /* Check if the previous command is completed */
963 if (btm_cb.devcb.p_stored_link_key_cmpl_cb) {
964 return (BTM_BUSY);
965 }
966
967 if (!bd_addr) {
968 /* This is to delete all link keys */
969 delete_all_flag = TRUE;
970
971 /* We don't care the BD address. Just pass a non zero pointer */
972 bd_addr = local_bd_addr;
973 }
974
975 BTM_TRACE_EVENT ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
976 delete_all_flag ? "TRUE" : "FALSE");
977
978 /* Send the HCI command */
979 btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
980 if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag)) {
981 return (BTM_NO_RESOURCES);
982 } else {
983 return (BTM_SUCCESS);
984 }
985 }
986
987 /*******************************************************************************
988 **
989 ** Function btm_delete_stored_link_key_complete
990 **
991 ** Description This function is called when the command complete message
992 ** is received from the HCI for the delete stored link key command.
993 **
994 ** Returns void
995 **
996 *******************************************************************************/
btm_delete_stored_link_key_complete(UINT8 * p)997 void btm_delete_stored_link_key_complete (UINT8 *p)
998 {
999 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
1000 tBTM_DELETE_STORED_LINK_KEY_COMPLETE result;
1001
1002 /* If there was a callback registered for read stored link key, call it */
1003 btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
1004
1005 if (p_cb) {
1006 /* Set the call back event to indicate command complete */
1007 result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
1008
1009 /* Extract the result fields from the HCI event */
1010 STREAM_TO_UINT8 (result.status, p);
1011 STREAM_TO_UINT16 (result.num_keys, p);
1012
1013 /* Call the call back and pass the result */
1014 (*p_cb)(&result);
1015 }
1016 }
1017
1018 /*******************************************************************************
1019 **
1020 ** Function btm_report_device_status
1021 **
1022 ** Description This function is called when there is a change in the device
1023 ** status. This function will report the new device status to
1024 ** the application
1025 **
1026 ** Returns void
1027 **
1028 *******************************************************************************/
btm_report_device_status(tBTM_DEV_STATUS status)1029 void btm_report_device_status (tBTM_DEV_STATUS status)
1030 {
1031 tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
1032
1033 /* Call the call back to pass the device status to application */
1034 if (p_cb) {
1035 (*p_cb)(status);
1036 }
1037 }
1038
1039 #if (CLASSIC_BT_INCLUDED == TRUE)
1040 /*******************************************************************************
1041 **
1042 ** Function BTM_SetAfhChannels
1043 **
1044 ** Description This function is called to set AFH channels
1045 **
1046 ** Returns status of the operation
1047 **
1048 *******************************************************************************/
BTM_SetAfhChannels(AFH_CHANNELS channels,tBTM_CMPL_CB * p_afh_channels_cmpl_cback)1049 tBTM_STATUS BTM_SetAfhChannels (AFH_CHANNELS channels, tBTM_CMPL_CB *p_afh_channels_cmpl_cback)
1050 {
1051 if (!controller_get_interface()->get_is_ready()) {
1052 return (BTM_DEV_RESET);
1053 }
1054
1055 /* Check if set afh already in progress */
1056 if (btm_cb.devcb.p_afh_channels_cmpl_cb) {
1057 return (BTM_NO_RESOURCES);
1058 }
1059
1060 /* Save callback */
1061 btm_cb.devcb.p_afh_channels_cmpl_cb = p_afh_channels_cmpl_cback;
1062
1063 if (!btsnd_hcic_set_afh_channels (channels)) {
1064 return (BTM_NO_RESOURCES);
1065 }
1066
1067 btu_start_timer (&btm_cb.devcb.afh_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1068
1069 return BTM_CMD_STARTED;
1070 }
1071
1072 /*******************************************************************************
1073 **
1074 ** Function btm_set_afh_channels_complete
1075 **
1076 ** Description This function is called when setting AFH channels complete.
1077 ** message is received from the HCI.
1078 **
1079 ** Returns void
1080 **
1081 *******************************************************************************/
btm_set_afh_channels_complete(UINT8 * p)1082 void btm_set_afh_channels_complete (UINT8 *p)
1083 {
1084 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_afh_channels_cmpl_cb;
1085 tBTM_SET_AFH_CHANNELS_RESULTS results;
1086
1087 btu_free_timer (&btm_cb.devcb.afh_channels_timer);
1088
1089 /* If there is a callback address for setting AFH channels, call it */
1090 btm_cb.devcb.p_afh_channels_cmpl_cb = NULL;
1091
1092 if (p_cb) {
1093 STREAM_TO_UINT8 (results.hci_status, p);
1094
1095 switch (results.hci_status){
1096 case HCI_SUCCESS:
1097 results.status = BTM_SUCCESS;
1098 break;
1099 case HCI_ERR_UNSUPPORTED_VALUE:
1100 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
1101 results.status = BTM_ILLEGAL_VALUE;
1102 break;
1103 default:
1104 results.status = BTM_ERR_PROCESSING;
1105 break;
1106 }
1107 (*p_cb)(&results);
1108 }
1109 }
1110 #endif /// CLASSIC_BT_INCLUDED == TRUE
1111
1112 #if (BLE_INCLUDED == TRUE)
1113 /*******************************************************************************
1114 **
1115 ** Function BTM_BleSetChannels
1116 **
1117 ** Description This function is called to set BLE channels
1118 **
1119 ** Returns status of the operation
1120 **
1121 *******************************************************************************/
BTM_BleSetChannels(BLE_CHANNELS channels,tBTM_CMPL_CB * p_ble_channels_cmpl_cback)1122 tBTM_STATUS BTM_BleSetChannels (BLE_CHANNELS channels, tBTM_CMPL_CB *p_ble_channels_cmpl_cback)
1123 {
1124 if (!controller_get_interface()->get_is_ready()) {
1125 return (BTM_DEV_RESET);
1126 }
1127
1128 /* Check if set afh already in progress */
1129 if (btm_cb.devcb.p_ble_channels_cmpl_cb) {
1130 return (BTM_NO_RESOURCES);
1131 }
1132
1133 /* Save callback */
1134 btm_cb.devcb.p_ble_channels_cmpl_cb = p_ble_channels_cmpl_cback;
1135
1136 if (!btsnd_hcic_ble_set_channels (channels)) {
1137 return (BTM_NO_RESOURCES);
1138 }
1139
1140 btu_start_timer (&btm_cb.devcb.ble_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1141
1142 return BTM_CMD_STARTED;
1143 }
1144
1145 /*******************************************************************************
1146 **
1147 ** Function btm_ble_set_channels_complete
1148 **
1149 ** Description This function is called when setting AFH channels complete.
1150 ** message is received from the HCI.
1151 **
1152 ** Returns void
1153 **
1154 *******************************************************************************/
btm_ble_set_channels_complete(UINT8 * p)1155 void btm_ble_set_channels_complete (UINT8 *p)
1156 {
1157 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_ble_channels_cmpl_cb;
1158 tBTM_BLE_SET_CHANNELS_RESULTS results;
1159
1160 btu_free_timer (&btm_cb.devcb.ble_channels_timer);
1161
1162 /* If there is a callback address for setting AFH channels, call it */
1163 btm_cb.devcb.p_ble_channels_cmpl_cb = NULL;
1164
1165 if (p_cb) {
1166 STREAM_TO_UINT8 (results.hci_status, p);
1167
1168 switch (results.hci_status){
1169 case HCI_SUCCESS:
1170 results.status = BTM_SUCCESS;
1171 break;
1172 case HCI_ERR_UNSUPPORTED_VALUE:
1173 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
1174 results.status = BTM_ILLEGAL_VALUE;
1175 break;
1176 default:
1177 results.status = BTM_ERR_PROCESSING;
1178 break;
1179 }
1180 (*p_cb)(&results);
1181 }
1182 }
1183 #endif /// BLE_INCLUDED == TRUE
1184