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