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 default:
718 break;
719 }
720 tBTM_VSC_CMPL vcs_cplt_params;
721
722 /* If there was a callback address for vcs complete, call it */
723 if (p_vsc_cplt_cback) {
724 /* Pass paramters to the callback function */
725 vcs_cplt_params.opcode = opcode; /* Number of bytes in return info */
726 vcs_cplt_params.param_len = evt_len; /* Number of bytes in return info */
727 vcs_cplt_params.p_param_buf = p;
728 (*p_vsc_cplt_cback)(&vcs_cplt_params); /* Call the VSC complete callback function */
729 }
730 #endif
731 }
732
733 /*******************************************************************************
734 **
735 ** Function BTM_RegisterForVSEvents
736 **
737 ** Description This function is called to register/deregister for vendor
738 ** specific HCI events.
739 **
740 ** If is_register=TRUE, then the function will be registered;
741 ** if is_register=FALSE, then the function will be deregistered.
742 **
743 ** Returns BTM_SUCCESS if successful,
744 ** BTM_BUSY if maximum number of callbacks have already been
745 ** registered.
746 **
747 *******************************************************************************/
BTM_RegisterForVSEvents(tBTM_VS_EVT_CB * p_cb,BOOLEAN is_register)748 tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
749 {
750 tBTM_STATUS retval = BTM_SUCCESS;
751 UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
752
753 /* See if callback is already registered */
754 for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
755 if (btm_cb.devcb.p_vend_spec_cb[i] == NULL) {
756 /* Found a free slot. Store index */
757 free_idx = i;
758 } else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb) {
759 /* Found callback in lookup table. If deregistering, clear the entry. */
760 if (is_register == FALSE) {
761 btm_cb.devcb.p_vend_spec_cb[i] = NULL;
762 BTM_TRACE_EVENT("BTM Deregister For VSEvents is successfully");
763 }
764 return (BTM_SUCCESS);
765 }
766 }
767
768 /* Didn't find callback. Add callback to free slot if registering */
769 if (is_register) {
770 if (free_idx < BTM_MAX_VSE_CALLBACKS) {
771 btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
772 BTM_TRACE_EVENT("BTM Register For VSEvents is successfully");
773 } else {
774 /* No free entries available */
775 BTM_TRACE_ERROR ("BTM_RegisterForVSEvents: too many callbacks registered");
776
777 retval = BTM_NO_RESOURCES;
778 }
779 }
780
781 return (retval);
782 }
783
784 /*******************************************************************************
785 **
786 ** Function btm_vendor_specific_evt
787 **
788 ** Description Process event HCI_VENDOR_SPECIFIC_EVT
789 **
790 ** Note: Some controllers do not send command complete, so
791 ** the callback and busy flag are cleared here also.
792 **
793 ** Returns void
794 **
795 *******************************************************************************/
btm_vendor_specific_evt(UINT8 * p,UINT8 evt_len)796 void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
797 {
798 UINT8 i;
799
800 #if (CLASSIC_BT_INCLUDED == TRUE)
801 UINT8 sub_event;
802 UINT8 *p_evt = p;
803
804 STREAM_TO_UINT8(sub_event, p_evt);
805 /* Check in subevent if authentication is through Legacy Authentication. */
806 if (sub_event == ESP_VS_REM_LEGACY_AUTH_CMP) {
807 UINT16 hci_handle;
808 STREAM_TO_UINT16(hci_handle, p_evt);
809 btm_sec_handle_remote_legacy_auth_cmp(hci_handle);
810 }
811 #endif /// (CLASSIC_BT_INCLUDED == TRUE)
812 for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
813 if (btm_cb.devcb.p_vend_spec_cb[i]) {
814 (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
815 }
816 }
817 BTM_TRACE_DEBUG ("BTM Event: Vendor Specific event from controller");
818 }
819
820
821 /*******************************************************************************
822 **
823 ** Function BTM_WritePageTimeout
824 **
825 ** Description Send HCI Write Page Timeout.
826 **
827 ** Returns
828 ** BTM_SUCCESS Command sent.
829 ** BTM_NO_RESOURCES If out of resources to send the command.
830 **
831 **
832 *******************************************************************************/
BTM_WritePageTimeout(UINT16 timeout)833 tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout)
834 {
835 BTM_TRACE_EVENT ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
836
837 /* Send the HCI command */
838 if (btsnd_hcic_write_page_tout (timeout)) {
839 return (BTM_SUCCESS);
840 } else {
841 return (BTM_NO_RESOURCES);
842 }
843 }
844
845 /*******************************************************************************
846 **
847 ** Function BTM_WriteVoiceSettings
848 **
849 ** Description Send HCI Write Voice Settings command.
850 ** See stack/hcidefs.h for settings bitmask values.
851 **
852 ** Returns
853 ** BTM_SUCCESS Command sent.
854 ** BTM_NO_RESOURCES If out of resources to send the command.
855 **
856 **
857 *******************************************************************************/
BTM_WriteVoiceSettings(UINT16 settings)858 tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
859 {
860 BTM_TRACE_EVENT ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
861
862 /* Send the HCI command */
863 if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff))) {
864 return (BTM_SUCCESS);
865 }
866
867 return (BTM_NO_RESOURCES);
868 }
869
870 /*******************************************************************************
871 **
872 ** Function BTM_EnableTestMode
873 **
874 ** Description Send HCI the enable device under test command.
875 **
876 ** Note: Controller can only be taken out of this mode by
877 ** resetting the controller.
878 **
879 ** Returns
880 ** BTM_SUCCESS Command sent.
881 ** BTM_NO_RESOURCES If out of resources to send the command.
882 **
883 **
884 *******************************************************************************/
BTM_EnableTestMode(void)885 tBTM_STATUS BTM_EnableTestMode(void)
886 {
887 UINT8 cond;
888
889 BTM_TRACE_EVENT ("BTM: BTM_EnableTestMode");
890
891 /* set auto accept connection as this is needed during test mode */
892 /* Allocate a buffer to hold HCI command */
893 cond = HCI_DO_AUTO_ACCEPT_CONNECT;
894 if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
895 HCI_FILTER_COND_NEW_DEVICE,
896 &cond, sizeof(cond))) {
897 return (BTM_NO_RESOURCES);
898 }
899
900 /* put device to connectable mode */
901 if (BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
902 BTM_DEFAULT_CONN_INTERVAL) != BTM_SUCCESS) {
903 return BTM_NO_RESOURCES;
904 }
905
906 /* put device to discoverable mode */
907 if (BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
908 BTM_DEFAULT_DISC_INTERVAL) != BTM_SUCCESS) {
909 return BTM_NO_RESOURCES;
910 }
911
912 /* mask off all of event from controller */
913 hci_layer_get_interface()->transmit_command(
914 hci_packet_factory_get_interface()->make_set_event_mask((const bt_event_mask_t *)("\x00\x00\x00\x00\x00\x00\x00\x00")),
915 NULL,
916 NULL,
917 NULL);
918
919 /* Send the HCI command */
920 if (btsnd_hcic_enable_test_mode ()) {
921 return (BTM_SUCCESS);
922 } else {
923 return (BTM_NO_RESOURCES);
924 }
925 }
926
927 /*******************************************************************************
928 **
929 ** Function BTM_DeleteStoredLinkKey
930 **
931 ** Description This function is called to delete link key for the specified
932 ** device addresses from the NVRAM storage attached to the Bluetooth
933 ** controller.
934 **
935 ** Parameters: bd_addr - Addresses of the devices
936 ** p_cb - Call back function to be called to return
937 ** the results
938 **
939 *******************************************************************************/
BTM_DeleteStoredLinkKey(BD_ADDR bd_addr,tBTM_CMPL_CB * p_cb)940 tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
941 {
942 BD_ADDR local_bd_addr = {0};
943 BOOLEAN delete_all_flag = FALSE;
944
945 /* Check if the previous command is completed */
946 if (btm_cb.devcb.p_stored_link_key_cmpl_cb) {
947 return (BTM_BUSY);
948 }
949
950 if (!bd_addr) {
951 /* This is to delete all link keys */
952 delete_all_flag = TRUE;
953
954 /* We don't care the BD address. Just pass a non zero pointer */
955 bd_addr = local_bd_addr;
956 }
957
958 BTM_TRACE_EVENT ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
959 delete_all_flag ? "TRUE" : "FALSE");
960
961 /* Send the HCI command */
962 btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
963 if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag)) {
964 return (BTM_NO_RESOURCES);
965 } else {
966 return (BTM_SUCCESS);
967 }
968 }
969
970 /*******************************************************************************
971 **
972 ** Function btm_delete_stored_link_key_complete
973 **
974 ** Description This function is called when the command complete message
975 ** is received from the HCI for the delete stored link key command.
976 **
977 ** Returns void
978 **
979 *******************************************************************************/
btm_delete_stored_link_key_complete(UINT8 * p)980 void btm_delete_stored_link_key_complete (UINT8 *p)
981 {
982 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
983 tBTM_DELETE_STORED_LINK_KEY_COMPLETE result;
984
985 /* If there was a callback registered for read stored link key, call it */
986 btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
987
988 if (p_cb) {
989 /* Set the call back event to indicate command complete */
990 result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
991
992 /* Extract the result fields from the HCI event */
993 STREAM_TO_UINT8 (result.status, p);
994 STREAM_TO_UINT16 (result.num_keys, p);
995
996 /* Call the call back and pass the result */
997 (*p_cb)(&result);
998 }
999 }
1000
1001 /*******************************************************************************
1002 **
1003 ** Function btm_report_device_status
1004 **
1005 ** Description This function is called when there is a change in the device
1006 ** status. This function will report the new device status to
1007 ** the application
1008 **
1009 ** Returns void
1010 **
1011 *******************************************************************************/
btm_report_device_status(tBTM_DEV_STATUS status)1012 void btm_report_device_status (tBTM_DEV_STATUS status)
1013 {
1014 tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
1015
1016 /* Call the call back to pass the device status to application */
1017 if (p_cb) {
1018 (*p_cb)(status);
1019 }
1020 }
1021
1022 #if (CLASSIC_BT_INCLUDED == TRUE)
1023 /*******************************************************************************
1024 **
1025 ** Function BTM_SetAfhChannels
1026 **
1027 ** Description This function is called to set AFH channels
1028 **
1029 ** Returns status of the operation
1030 **
1031 *******************************************************************************/
BTM_SetAfhChannels(AFH_CHANNELS channels,tBTM_CMPL_CB * p_afh_channels_cmpl_cback)1032 tBTM_STATUS BTM_SetAfhChannels (AFH_CHANNELS channels, tBTM_CMPL_CB *p_afh_channels_cmpl_cback)
1033 {
1034 if (!controller_get_interface()->get_is_ready()) {
1035 return (BTM_DEV_RESET);
1036 }
1037
1038 /* Check if set afh already in progress */
1039 if (btm_cb.devcb.p_afh_channels_cmpl_cb) {
1040 return (BTM_NO_RESOURCES);
1041 }
1042
1043 /* Save callback */
1044 btm_cb.devcb.p_afh_channels_cmpl_cb = p_afh_channels_cmpl_cback;
1045
1046 if (!btsnd_hcic_set_afh_channels (channels)) {
1047 return (BTM_NO_RESOURCES);
1048 }
1049
1050 btu_start_timer (&btm_cb.devcb.afh_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1051
1052 return BTM_CMD_STARTED;
1053 }
1054
1055 /*******************************************************************************
1056 **
1057 ** Function btm_set_afh_channels_complete
1058 **
1059 ** Description This function is called when setting AFH channels complete.
1060 ** message is received from the HCI.
1061 **
1062 ** Returns void
1063 **
1064 *******************************************************************************/
btm_set_afh_channels_complete(UINT8 * p)1065 void btm_set_afh_channels_complete (UINT8 *p)
1066 {
1067 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_afh_channels_cmpl_cb;
1068 tBTM_SET_AFH_CHANNELS_RESULTS results;
1069
1070 btu_free_timer (&btm_cb.devcb.afh_channels_timer);
1071
1072 /* If there is a callback address for setting AFH channels, call it */
1073 btm_cb.devcb.p_afh_channels_cmpl_cb = NULL;
1074
1075 if (p_cb) {
1076 STREAM_TO_UINT8 (results.hci_status, p);
1077
1078 switch (results.hci_status){
1079 case HCI_SUCCESS:
1080 results.status = BTM_SUCCESS;
1081 break;
1082 case HCI_ERR_UNSUPPORTED_VALUE:
1083 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
1084 results.status = BTM_ILLEGAL_VALUE;
1085 break;
1086 default:
1087 results.status = BTM_ERR_PROCESSING;
1088 break;
1089 }
1090 (*p_cb)(&results);
1091 }
1092 }
1093 #endif /// CLASSIC_BT_INCLUDED == TRUE
1094
1095 #if (BLE_INCLUDED == TRUE)
1096 /*******************************************************************************
1097 **
1098 ** Function BTM_BleSetChannels
1099 **
1100 ** Description This function is called to set BLE channels
1101 **
1102 ** Returns status of the operation
1103 **
1104 *******************************************************************************/
BTM_BleSetChannels(BLE_CHANNELS channels,tBTM_CMPL_CB * p_ble_channels_cmpl_cback)1105 tBTM_STATUS BTM_BleSetChannels (BLE_CHANNELS channels, tBTM_CMPL_CB *p_ble_channels_cmpl_cback)
1106 {
1107 if (!controller_get_interface()->get_is_ready()) {
1108 return (BTM_DEV_RESET);
1109 }
1110
1111 /* Check if set afh already in progress */
1112 if (btm_cb.devcb.p_ble_channels_cmpl_cb) {
1113 return (BTM_NO_RESOURCES);
1114 }
1115
1116 /* Save callback */
1117 btm_cb.devcb.p_ble_channels_cmpl_cb = p_ble_channels_cmpl_cback;
1118
1119 if (!btsnd_hcic_ble_set_channels (channels)) {
1120 return (BTM_NO_RESOURCES);
1121 }
1122
1123 btu_start_timer (&btm_cb.devcb.ble_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
1124
1125 return BTM_CMD_STARTED;
1126 }
1127
1128 /*******************************************************************************
1129 **
1130 ** Function btm_ble_set_channels_complete
1131 **
1132 ** Description This function is called when setting AFH channels complete.
1133 ** message is received from the HCI.
1134 **
1135 ** Returns void
1136 **
1137 *******************************************************************************/
btm_ble_set_channels_complete(UINT8 * p)1138 void btm_ble_set_channels_complete (UINT8 *p)
1139 {
1140 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_ble_channels_cmpl_cb;
1141 tBTM_BLE_SET_CHANNELS_RESULTS results;
1142
1143 btu_free_timer (&btm_cb.devcb.ble_channels_timer);
1144
1145 /* If there is a callback address for setting AFH channels, call it */
1146 btm_cb.devcb.p_ble_channels_cmpl_cb = NULL;
1147
1148 if (p_cb) {
1149 STREAM_TO_UINT8 (results.hci_status, p);
1150
1151 switch (results.hci_status){
1152 case HCI_SUCCESS:
1153 results.status = BTM_SUCCESS;
1154 break;
1155 case HCI_ERR_UNSUPPORTED_VALUE:
1156 case HCI_ERR_ILLEGAL_PARAMETER_FMT:
1157 results.status = BTM_ILLEGAL_VALUE;
1158 break;
1159 default:
1160 results.status = BTM_ERR_PROCESSING;
1161 break;
1162 }
1163 (*p_cb)(&results);
1164 }
1165 }
1166 #endif /// BLE_INCLUDED == TRUE
1167