blob: c209af2317e4c62eef57998d62a12df91172d853 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Hemant Gupta2dc99992014-04-18 12:54:08 +05303 * Copyright (C) 2014 The Android Open Source Project
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 * Copyright (C) 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20/************************************************************************************
21 *
22 * Filename: btif_core.c
23 *
24 * Description: Contains core functionality related to interfacing between
25 * Bluetooth HAL and BTE core stack.
26 *
27 ***********************************************************************************/
28
29#include <stdlib.h>
30#include <hardware/bluetooth.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <dirent.h>
36#include <ctype.h>
37#include <cutils/properties.h>
38
39#define LOG_TAG "BTIF_CORE"
40#include "btif_api.h"
Zhihai Xu6c65c2f2013-11-25 17:30:59 -080041#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include "bta_api.h"
43#include "gki.h"
44#include "btu.h"
45#include "bte.h"
46#include "bd.h"
47#include "btif_av.h"
48#include "btif_storage.h"
49#include "btif_util.h"
50#include "btif_sock.h"
51#include "btif_pan.h"
Hemant Gupta2dc99992014-04-18 12:54:08 +053052#include "btif_mce.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080053#include "btif_profile_queue.h"
54#include "btif_config.h"
Nitin Shivpure81ae1012013-06-25 16:34:00 +053055#include "btif_sock_util.h"
Kiran Kelageri99694f72014-09-15 14:44:55 -070056#include "btif_gatt_multi_adv_util.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080057/************************************************************************************
58** Constants & Macros
59************************************************************************************/
60
61#ifndef BTIF_TASK_STACK_SIZE
62#define BTIF_TASK_STACK_SIZE 0x2000 /* In bytes */
63#endif
64
65#ifndef BTE_DID_CONF_FILE
66#define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
67#endif
68
69#define BTIF_TASK_STR ((INT8 *) "BTIF")
70
71/************************************************************************************
72** Local type definitions
73************************************************************************************/
74
Nitin Shivpure81ae1012013-06-25 16:34:00 +053075static BOOLEAN bt_disabled = FALSE;
pramod kotreshappa020d5722015-02-11 15:36:30 -080076static BOOLEAN ssr_triggered = FALSE;
Nitin Shivpure81ae1012013-06-25 16:34:00 +053077pthread_mutex_t mutex_bt_disable;
78
The Android Open Source Project5738f832012-12-12 16:00:35 -080079/* These type definitions are used when passing data from the HAL to BTIF context
80* in the downstream path for the adapter and remote_device property APIs */
81
82typedef struct {
83 bt_bdaddr_t bd_addr;
84 bt_property_type_t type;
85} btif_storage_read_t;
86
87typedef struct {
88 bt_bdaddr_t bd_addr;
89 bt_property_t prop;
90} btif_storage_write_t;
91
92typedef union {
93 btif_storage_read_t read_req;
94 btif_storage_write_t write_req;
95} btif_storage_req_t;
96
97typedef enum {
98 BTIF_CORE_STATE_DISABLED = 0,
pramod kotreshappa401a3db2014-06-25 12:03:07 -070099 BTIF_CORE_STATE_INITIALIZED,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100 BTIF_CORE_STATE_ENABLING,
101 BTIF_CORE_STATE_ENABLED,
102 BTIF_CORE_STATE_DISABLING
103} btif_core_state_t;
104
105/************************************************************************************
106** Static variables
107************************************************************************************/
108
109bt_bdaddr_t btif_local_bd_addr;
110
111static UINT32 btif_task_stack[(BTIF_TASK_STACK_SIZE + 3) / 4];
112
113/* holds main adapter state */
114static btif_core_state_t btif_core_state = BTIF_CORE_STATE_DISABLED;
115
116static int btif_shutdown_pending = 0;
117static tBTA_SERVICE_MASK btif_enabled_services = 0;
Hemant Gupta9cd26bf2013-09-10 12:01:18 +0530118static int btif_data_profile_registered = 0;
119static int btif_pending_mode = BT_SCAN_MODE_NONE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120
121/*
122* This variable should be set to 1, if the Bluedroid+BTIF libraries are to
123* function in DUT mode.
124*
125* To set this, the btif_init_bluetooth needs to be called with argument as 1
126*/
127static UINT8 btif_dut_mode = 0;
128
129/************************************************************************************
130** Static functions
131************************************************************************************/
132static bt_status_t btif_associate_evt(void);
133static bt_status_t btif_disassociate_evt(void);
134
135/* sends message to btif task */
136static void btif_sendmsg(void *p_msg);
137
138/************************************************************************************
139** Externs
140************************************************************************************/
141extern void bte_load_did_conf(const char *p_path);
142
143/** TODO: Move these to _common.h */
144void bte_main_boot_entry(void);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700145void bte_main_enable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800146void bte_main_disable(void);
147void bte_main_shutdown(void);
148#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
149void bte_main_enable_lpm(BOOLEAN enable);
150#endif
151void bte_main_postload_cfg(void);
152void btif_dm_execute_service_request(UINT16 event, char *p_param);
153#ifdef BTIF_DM_OOB_TEST
154void btif_dm_load_local_oob(void);
155#endif
Zhihai Xubad70b12013-06-04 18:21:25 -0700156void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800157
158/************************************************************************************
159** Functions
160************************************************************************************/
161
162
163/*****************************************************************************
164** Context switching functions
165*****************************************************************************/
166
167
168/*******************************************************************************
169**
170** Function btif_context_switched
171**
172** Description Callback used to execute transferred context callback
173**
174** p_msg : message to be executed in btif context
175**
176** Returns void
177**
178*******************************************************************************/
179
180static void btif_context_switched(void *p_msg)
181{
182 tBTIF_CONTEXT_SWITCH_CBACK *p;
183
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700184 BTIF_TRACE_VERBOSE("btif_context_switched");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800185
186 p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
187
188 /* each callback knows how to parse the data */
189 if (p->p_cb)
190 p->p_cb(p->event, p->p_param);
191}
192
193
194/*******************************************************************************
195**
196** Function btif_transfer_context
197**
198** Description This function switches context to btif task
199**
200** p_cback : callback used to process message in btif context
201** event : event id of message
202** p_params : parameter area passed to callback (copied)
203** param_len : length of parameter area
204** p_copy_cback : If set this function will be invoked for deep copy
205**
206** Returns void
207**
208*******************************************************************************/
209
210bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
211{
212 tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
213
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700214 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215
216 /* allocate and send message that will be executed in btif context */
217 if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
218 {
219 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
220 p_msg->p_cb = p_cback;
221
222 p_msg->event = event; /* callback event */
223
224 /* check if caller has provided a copy callback to do the deep copy */
225 if (p_copy_cback)
226 {
227 p_copy_cback(event, p_msg->p_param, p_params);
228 }
229 else if (p_params)
230 {
231 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
232 }
233
234 btif_sendmsg(p_msg);
235 return BT_STATUS_SUCCESS;
236 }
237 else
238 {
239 /* let caller deal with a failed allocation */
240 return BT_STATUS_NOMEM;
241 }
242}
243
244/*******************************************************************************
245**
246** Function btif_is_dut_mode
247**
248** Description checks if BTIF is currently in DUT mode
249**
250** Returns 1 if test mode, otherwize 0
251**
252*******************************************************************************/
253
254UINT8 btif_is_dut_mode(void)
255{
256 return (btif_dut_mode == 1);
257}
258
259/*******************************************************************************
260**
261** Function btif_is_enabled
262**
263** Description checks if main adapter is fully enabled
264**
265** Returns 1 if fully enabled, otherwize 0
266**
267*******************************************************************************/
268
269int btif_is_enabled(void)
270{
271 return ((!btif_is_dut_mode()) && (btif_core_state == BTIF_CORE_STATE_ENABLED));
272}
273
274/*******************************************************************************
275**
276** Function btif_task
277**
278** Description BTIF task handler managing all messages being passed
279** Bluetooth HAL and BTA.
280**
281** Returns void
282**
283*******************************************************************************/
284
285static void btif_task(UINT32 params)
286{
287 UINT16 event;
288 BT_HDR *p_msg;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800289 UNUSED(params);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800290
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700291 BTIF_TRACE_DEBUG("btif task starting");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800292
293 btif_associate_evt();
294
295 for(;;)
296 {
297 /* wait for specified events */
298 event = GKI_wait(0xFFFF, 0);
299
300 /*
301 * Wait for the trigger to init chip and stack. This trigger will
302 * be received by btu_task once the UART is opened and ready
303 */
304 if (event == BT_EVT_TRIGGER_STACK_INIT)
305 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700306 BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800307 #if (BLE_INCLUDED == TRUE)
308 btif_dm_load_ble_local_keys();
309 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310 BTA_EnableBluetooth(bte_dm_evt);
311 }
312
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700313 /*
314 * Failed to initialize controller hardware, reset state and bring
315 * down all threads
316 */
317 if (event == BT_EVT_HARDWARE_INIT_FAIL)
318 {
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530319 lock_slot(&mutex_bt_disable);
320 BTIF_TRACE_DEBUG("btif_task: mutex_bt_disable lock");
321 if(bt_disabled == FALSE)
322 {
323 BTIF_TRACE_DEBUG("btif_task: hardware init failed");
324 bte_main_disable();
325 btif_queue_release();
326 GKI_task_self_cleanup(BTIF_TASK);
327 bte_main_shutdown();
328 btif_dut_mode = 0;
329 btif_core_state = BTIF_CORE_STATE_DISABLED;
330 /*variable to avoid the double cleanup*/
331 bt_disabled = TRUE;
332 HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
333 }
334 BTIF_TRACE_DEBUG("btif_task: mutex_bt_disable unlock");
335 unlock_slot(&mutex_bt_disable);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700336 break;
337 }
338
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339 if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
340 break;
341
342 if(event & TASK_MBOX_1_EVT_MASK)
343 {
344 while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL)
345 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700346 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800347
348 switch (p_msg->event)
349 {
350 case BT_EVT_CONTEXT_SWITCH_EVT:
351 btif_context_switched(p_msg);
352 break;
353 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700354 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355 break;
356 }
357
358 GKI_freebuf(p_msg);
359 }
360 }
361 }
362
363 btif_disassociate_evt();
364
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700365 BTIF_TRACE_DEBUG("btif task exiting");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366}
367
368
369/*******************************************************************************
370**
371** Function btif_sendmsg
372**
373** Description Sends msg to BTIF task
374**
375** Returns void
376**
377*******************************************************************************/
378
379void btif_sendmsg(void *p_msg)
380{
381 GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg);
382}
383
384static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
385{
386 char val[256];
387 uint8_t valid_bda = FALSE;
388 int val_size = 0;
389 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
390
391 /* Get local bdaddr storage path from property */
392 if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
393 {
394 int addr_fd;
395
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700396 BTIF_TRACE_DEBUG("local bdaddr is stored in %s", val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800397
Sharvil Nanavati405b5c92016-06-17 14:15:46 -0700398 if ((addr_fd = TEMP_FAILURE_RETRY(open(val, O_RDONLY))) != -1)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800399 {
400 memset(val, 0, sizeof(val));
Sharvil Nanavati405b5c92016-06-17 14:15:46 -0700401 TEMP_FAILURE_RETRY(read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 str2bd(val, local_addr);
403 /* If this is not a reserved/special bda, then use it */
404 if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)
405 {
406 valid_bda = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700407 BTIF_TRACE_DEBUG("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408 local_addr->address[0], local_addr->address[1], local_addr->address[2],
409 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
410 }
411
412 close(addr_fd);
413 }
414 }
415
416 if(!valid_bda)
417 {
418 val_size = sizeof(val);
419 if(btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
420 {
421 str2bd(val, local_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700422 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423 return;
424 }
425 }
426
427 /* No factory BDADDR found. Look for previously generated random BDA */
428 if ((!valid_bda) && \
429 (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
430 {
431 str2bd(val, local_addr);
432 valid_bda = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700433 BTIF_TRACE_DEBUG("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 local_addr->address[0], local_addr->address[1], local_addr->address[2],
435 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
436 }
437
438 /* Generate new BDA if necessary */
439 if (!valid_bda)
440 {
441 bdstr_t bdstr;
442 /* Seed the random number generator */
443 srand((unsigned int) (time(0)));
444
445 /* No autogen BDA. Generate one now. */
446 local_addr->address[0] = 0x22;
447 local_addr->address[1] = 0x22;
448 local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
449 local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
450 local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
451 local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
452
453 /* Convert to ascii, and store as a persistent property */
454 bd2str(local_addr, &bdstr);
455
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700456 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800457 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
458
459 if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700460 BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461 }
462
463 //save the bd address to config file
464 bdstr_t bdstr;
465 bd2str(local_addr, &bdstr);
466 val_size = sizeof(val);
467 if (btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
468 {
469 if (strcmp(bdstr, val) ==0)
470 {
471 // BDA is already present in the config file.
472 return;
473 }
474 }
475 btif_config_set_str("Local", "Adapter", "Address", bdstr);
476 btif_config_save();
477}
478
479/*****************************************************************************
480**
481** btif core api functions
482**
483*****************************************************************************/
484
485/*******************************************************************************
486**
487** Function btif_init_bluetooth
488**
489** Description Creates BTIF task and prepares BT scheduler for startup
490**
491** Returns bt_status_t
492**
493*******************************************************************************/
494
495bt_status_t btif_init_bluetooth()
496{
497 UINT8 status;
498 btif_config_init();
499 bte_main_boot_entry();
500
501 /* As part of the init, fetch the local BD ADDR */
502 memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
503 btif_fetch_local_bdaddr(&btif_local_bd_addr);
504
505 /* start btif task */
506 status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
507 (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
508 sizeof(btif_task_stack));
509
510 if (status != GKI_SUCCESS)
511 return BT_STATUS_FAIL;
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700512 btif_core_state = BTIF_CORE_STATE_INITIALIZED;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513 return BT_STATUS_SUCCESS;
514}
515
516/*******************************************************************************
517**
518** Function btif_associate_evt
519**
520** Description Event indicating btif_task is up
521** Attach btif_task to JVM
522**
523** Returns void
524**
525*******************************************************************************/
526
527static bt_status_t btif_associate_evt(void)
528{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700529 BTIF_TRACE_DEBUG("%s: notify ASSOCIATE_JVM", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800530 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
531
532 return BT_STATUS_SUCCESS;
533}
534
535
536/*******************************************************************************
537**
538** Function btif_enable_bluetooth
539**
540** Description Performs chip power on and kickstarts OS scheduler
541**
542** Returns bt_status_t
543**
544*******************************************************************************/
545
546bt_status_t btif_enable_bluetooth(void)
547{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700548 BTIF_TRACE_DEBUG("BTIF ENABLE BLUETOOTH");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800549
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700550 if (btif_core_state != BTIF_CORE_STATE_DISABLED &&
551 btif_core_state != BTIF_CORE_STATE_INITIALIZED)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552 {
553 ALOGD("not disabled\n");
554 return BT_STATUS_DONE;
555 }
556
557 btif_core_state = BTIF_CORE_STATE_ENABLING;
558
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530559 bt_disabled = FALSE;
pramod kotreshappa020d5722015-02-11 15:36:30 -0800560 ssr_triggered = FALSE;
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530561
562 init_slot_lock(&mutex_bt_disable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 /* Create the GKI tasks and run them */
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700564 bte_main_enable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
566 return BT_STATUS_SUCCESS;
567}
568
569
570/*******************************************************************************
571**
572** Function btif_enable_bluetooth_evt
573**
574** Description Event indicating bluetooth enable is completed
575** Notifies HAL user with updated adapter state
576**
577** Returns void
578**
579*******************************************************************************/
580
581void btif_enable_bluetooth_evt(tBTA_STATUS status, BD_ADDR local_bd)
582{
583 bt_bdaddr_t bd_addr;
584 bdstr_t bdstr;
585
586 bdcpy(bd_addr.address, local_bd);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700587 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588 bd2str(&bd_addr, &bdstr));
589
590 if (bdcmp(btif_local_bd_addr.address,local_bd))
591 {
592 bdstr_t buf;
593 bt_property_t prop;
594
595 /**
596 * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
597 * This could be because the factory BDADDR was stored separatley in
598 * the Controller's non-volatile memory rather than in device's file
599 * system.
600 **/
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700601 BTIF_TRACE_WARNING("***********************************************");
602 BTIF_TRACE_WARNING("BTIF init BDA was %02X:%02X:%02X:%02X:%02X:%02X",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800603 btif_local_bd_addr.address[0], btif_local_bd_addr.address[1],
604 btif_local_bd_addr.address[2], btif_local_bd_addr.address[3],
605 btif_local_bd_addr.address[4], btif_local_bd_addr.address[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700606 BTIF_TRACE_WARNING("Controller BDA is %02X:%02X:%02X:%02X:%02X:%02X",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607 local_bd[0], local_bd[1], local_bd[2],
608 local_bd[3], local_bd[4], local_bd[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700609 BTIF_TRACE_WARNING("***********************************************");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800610
611 bdcpy(btif_local_bd_addr.address, local_bd);
612
613 //save the bd address to config file
614 bd2str(&btif_local_bd_addr, &buf);
615 btif_config_set_str("Local", "Adapter", "Address", buf);
616 btif_config_save();
617
618 //fire HAL callback for property change
619 memcpy(buf, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
620 prop.type = BT_PROPERTY_BDADDR;
621 prop.val = (void*)buf;
622 prop.len = sizeof(bt_bdaddr_t);
623 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
624 }
625
626 bte_main_postload_cfg();
627#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
628 bte_main_enable_lpm(TRUE);
629#endif
630 /* add passing up bd address as well ? */
631
632 /* callback to HAL */
633 if (status == BTA_SUCCESS)
634 {
AnubhavGuptaed0a8b12014-08-13 14:26:14 +0530635 /* don't initialize a2dp service */
636 //btif_av_init(); // we need av_init only from init_sink, init_src
Andre Eisenbach2d754412013-11-20 17:23:06 -0800637
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638 /* init rfcomm & l2cap api */
639 btif_sock_init();
640
641 /* init pan */
642 btif_pan_init();
643
644 /* load did configuration */
645 bte_load_did_conf(BTE_DID_CONF_FILE);
646
647#ifdef BTIF_DM_OOB_TEST
648 btif_dm_load_local_oob();
649#endif
650 /* now fully enabled, update state */
651 btif_core_state = BTIF_CORE_STATE_ENABLED;
652
653 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON);
654 }
655 else
656 {
657 /* cleanup rfcomm & l2cap api */
658 btif_sock_cleanup();
659
660 btif_pan_cleanup();
661
662 /* we failed to enable, reset state */
663 btif_core_state = BTIF_CORE_STATE_DISABLED;
664
665 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
666 }
667}
668
669/*******************************************************************************
670**
671** Function btif_disable_bluetooth
672**
673** Description Inititates shutdown of Bluetooth system.
674** Any active links will be dropped and device entering
675** non connectable/discoverable mode
676**
677** Returns void
678**
679*******************************************************************************/
680bt_status_t btif_disable_bluetooth(void)
681{
682 tBTA_STATUS status;
683
684 if (!btif_is_enabled())
685 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700686 BTIF_TRACE_ERROR("btif_disable_bluetooth : not yet enabled");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687 return BT_STATUS_NOT_READY;
688 }
689
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700690 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800691
692 btif_dm_on_disable();
693 btif_core_state = BTIF_CORE_STATE_DISABLING;
694
695 /* cleanup rfcomm & l2cap api */
696 btif_sock_cleanup();
697
698 btif_pan_cleanup();
699
700 status = BTA_DisableBluetooth();
701
702 btif_config_flush();
703
Kiran Kelageri99694f72014-09-15 14:44:55 -0700704 /* clear the adv instances on bt turn off */
Satish kumar sugasi57a34ca2015-03-06 16:36:52 -0800705#if (BLE_INCLUDED == TRUE)
706 btif_gatt_adv_inst_cleanup();
707#endif
Kiran Kelageri99694f72014-09-15 14:44:55 -0700708
The Android Open Source Project5738f832012-12-12 16:00:35 -0800709 if (status != BTA_SUCCESS)
710 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700711 BTIF_TRACE_ERROR("disable bt failed (%d)", status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800712
713 /* reset the original state to allow attempting disable again */
714 btif_core_state = BTIF_CORE_STATE_ENABLED;
715
716 return BT_STATUS_FAIL;
717 }
718 return BT_STATUS_SUCCESS;
719}
720
721/*******************************************************************************
722**
723** Function btif_disable_bluetooth_evt
724**
725** Description Event notifying BT disable is now complete.
726** Terminates main stack tasks and notifies HAL
727** user with updated BT state.
728**
729** Returns void
730**
731*******************************************************************************/
732
733void btif_disable_bluetooth_evt(void)
734{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700735 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
pramod kotreshappa020d5722015-02-11 15:36:30 -0800736 if (ssr_triggered == TRUE)
737 {
738 BTIF_TRACE_DEBUG("%s SSR triggered,Ignore EVT",__FUNCTION__);
739 return;
740 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800741#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
742 bte_main_enable_lpm(FALSE);
743#endif
744
Satya Callojid5aa2472014-09-23 18:27:09 -0700745#if (BLE_INCLUDED == TRUE)
746 BTA_VendorCleanup();
747#endif
748
Srinu Jellafa9d8942015-03-05 17:53:44 +0530749 lock_slot(&mutex_bt_disable);
750 if(bt_disabled == FALSE)
751 {
752 BTIF_TRACE_ERROR("btif_task: btif_disable_bluetooth_evt");
753 bte_main_disable();
754 btif_core_state = BTIF_CORE_STATE_DISABLED;
755 HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
756 }
757 unlock_slot(&mutex_bt_disable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800758
759 if (btif_shutdown_pending)
760 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700761 BTIF_TRACE_DEBUG("%s: calling btif_shutdown_bluetooth", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800762 btif_shutdown_bluetooth();
763 }
764}
765
766
767/*******************************************************************************
768**
769** Function btif_shutdown_bluetooth
770**
771** Description Finalizes BT scheduler shutdown and terminates BTIF
772** task.
773**
774** Returns void
775**
776*******************************************************************************/
777
778bt_status_t btif_shutdown_bluetooth(void)
779{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700780 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800781
Zhihai Xu6c65c2f2013-11-25 17:30:59 -0800782 if (btif_core_state == BTIF_CORE_STATE_DISABLING)
783 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700784 BTIF_TRACE_WARNING("shutdown during disabling");
Zhihai Xu6c65c2f2013-11-25 17:30:59 -0800785 /* shutdown called before disabling is done */
786 btif_shutdown_pending = 1;
787 return BT_STATUS_NOT_READY;
788 }
789
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 if (btif_is_enabled())
791 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700792 BTIF_TRACE_WARNING("shutdown while still enabled, initiate disable");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793
794 /* shutdown called prior to disabling, initiate disable */
795 btif_disable_bluetooth();
796 btif_shutdown_pending = 1;
797 return BT_STATUS_NOT_READY;
798 }
799
800 btif_shutdown_pending = 0;
801
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530802 lock_slot(&mutex_bt_disable);
803 if(bt_disabled == FALSE)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700804 {
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530805 /*variable to avoid the double cleanup*/
806 bt_disabled = TRUE;
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530807 if (btif_core_state == BTIF_CORE_STATE_ENABLING)
808 {
809 // Java layer abort BT ENABLING, could be due to ENABLE TIMEOUT
810 // Direct call from cleanup()@bluetooth.c
811 // bring down HCI/Vendor lib
812 bte_main_disable();
813 btif_core_state = BTIF_CORE_STATE_DISABLED;
814 HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
815 }
Srinu Jella96c0c672015-03-24 19:55:15 +0530816 unlock_slot(&mutex_bt_disable);
Nitin Shivpure81ae1012013-06-25 16:34:00 +0530817
818 GKI_destroy_task(BTIF_TASK);
819 btif_queue_release();
820 bte_main_shutdown();
821
822 btif_dut_mode = 0;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700823 }
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700824 else if (btif_core_state == BTIF_CORE_STATE_INITIALIZED)
825 {
826 // Java Layer called cleanup before calling enable due to START TIMEOUT
827 // Cleanup GKI task to reset the hal callback handle
Srinu Jella96c0c672015-03-24 19:55:15 +0530828 btif_core_state = BTIF_CORE_STATE_DISABLED;
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700829 BTIF_TRACE_WARNING("shutdown...cleanup called before enable");
Srinu Jella96c0c672015-03-24 19:55:15 +0530830 unlock_slot(&mutex_bt_disable);
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700831 GKI_destroy_task(BTIF_TASK);
832 btif_queue_release();
833 bte_main_shutdown();
pramod kotreshappa401a3db2014-06-25 12:03:07 -0700834 btif_dut_mode = 0;
835 }
Srinu Jella96c0c672015-03-24 19:55:15 +0530836 else
837 {
838 unlock_slot(&mutex_bt_disable);
839 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840
Zhihai Xu6c65c2f2013-11-25 17:30:59 -0800841 bt_utils_cleanup();
842
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700843 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800844
845 return BT_STATUS_SUCCESS;
846}
847
pramod kotreshappa7107d9d2014-03-03 13:11:17 -0800848/*******************************************************************************
849Function btif_ssrcleanup
850Description Trigger SSR when Disable timeout occured
851
852*******************************************************************************/
853void btif_ssr_cleanup(void)
854{
pramod kotreshappa020d5722015-02-11 15:36:30 -0800855 ssr_triggered = TRUE;
pramod kotreshappa7107d9d2014-03-03 13:11:17 -0800856 bte_ssr_cleanup();
857}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800858
859/*******************************************************************************
860**
861** Function btif_disassociate_evt
862**
863** Description Event indicating btif_task is going down
864** Detach btif_task to JVM
865**
866** Returns void
867**
868*******************************************************************************/
869
870static bt_status_t btif_disassociate_evt(void)
871{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700872 BTIF_TRACE_DEBUG("%s: notify DISASSOCIATE_JVM", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873
874 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
875
876 /* shutdown complete, all events notified and we reset HAL callbacks */
877 bt_hal_cbacks = NULL;
878
879 return BT_STATUS_SUCCESS;
880}
881
882/****************************************************************************
883**
884** BTIF Test Mode APIs
885**
886*****************************************************************************/
887/*******************************************************************************
888**
889** Function btif_dut_mode_cback
890**
891** Description Callback invoked on completion of vendor specific test mode command
892**
893** Returns None
894**
895*******************************************************************************/
896static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
897{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800898 UNUSED(p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800899 /* For now nothing to be done. */
900}
901
902/*******************************************************************************
903**
904** Function btif_dut_mode_configure
905**
906** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
907** test mode
908**
909** Returns BT_STATUS_SUCCESS on success
910**
911*******************************************************************************/
912bt_status_t btif_dut_mode_configure(uint8_t enable)
913{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700914 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800915
916 if (btif_core_state != BTIF_CORE_STATE_ENABLED) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700917 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918 return BT_STATUS_NOT_READY;
919 }
920
921 btif_dut_mode = enable;
922 if (enable == 1) {
923 BTA_EnableTestMode();
924 } else {
925 BTA_DisableTestMode();
926 }
927 return BT_STATUS_SUCCESS;
928}
929
930/*******************************************************************************
931**
932** Function btif_dut_mode_send
933**
934** Description Sends a HCI Vendor specific command to the controller
935**
936** Returns BT_STATUS_SUCCESS on success
937**
938*******************************************************************************/
939bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
940{
941 /* TODO: Check that opcode is a vendor command group */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700942 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943 if (!btif_is_dut_mode()) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700944 BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945 return BT_STATUS_FAIL;
946 }
947 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
948 return BT_STATUS_SUCCESS;
949}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800950
The Android Open Source Project5738f832012-12-12 16:00:35 -0800951/*****************************************************************************
952**
953** btif api adapter property functions
954**
955*****************************************************************************/
956
957static bt_status_t btif_in_get_adapter_properties(void)
958{
959 bt_property_t properties[6];
960 uint32_t num_props;
961
962 bt_bdaddr_t addr;
963 bt_bdname_t name;
964 bt_scan_mode_t mode;
965 uint32_t disc_timeout;
966 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
967 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
968 num_props = 0;
969
970 /* BD_ADDR */
971 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
972 sizeof(addr), &addr);
973 btif_storage_get_adapter_property(&properties[num_props]);
974 num_props++;
975
976 /* BD_NAME */
977 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
978 sizeof(name), &name);
979 btif_storage_get_adapter_property(&properties[num_props]);
980 num_props++;
981
982 /* SCAN_MODE */
983 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
984 sizeof(mode), &mode);
985 btif_storage_get_adapter_property(&properties[num_props]);
986 num_props++;
987
988 /* DISC_TIMEOUT */
989 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
990 sizeof(disc_timeout), &disc_timeout);
991 btif_storage_get_adapter_property(&properties[num_props]);
992 num_props++;
993
994 /* BONDED_DEVICES */
995 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
996 sizeof(bonded_devices), bonded_devices);
997 btif_storage_get_adapter_property(&properties[num_props]);
998 num_props++;
999
1000 /* LOCAL UUIDs */
1001 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
1002 sizeof(local_uuids), local_uuids);
1003 btif_storage_get_adapter_property(&properties[num_props]);
1004 num_props++;
1005
1006 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1007 BT_STATUS_SUCCESS, num_props, properties);
1008
1009 return BT_STATUS_SUCCESS;
1010}
1011
1012static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
1013{
1014 bt_property_t remote_properties[8];
1015 uint32_t num_props = 0;
1016
1017 bt_bdname_t name, alias;
Pradeep Panigrahi82da3262013-08-14 11:45:56 +05301018 uint32_t cod, devtype, trustval;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
1020
1021 memset(remote_properties, 0, sizeof(remote_properties));
1022 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
1023 sizeof(name), &name);
1024 btif_storage_get_remote_device_property(bd_addr,
1025 &remote_properties[num_props]);
1026 num_props++;
1027
1028 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
1029 sizeof(alias), &alias);
1030 btif_storage_get_remote_device_property(bd_addr,
1031 &remote_properties[num_props]);
1032 num_props++;
1033
Pradeep Panigrahi82da3262013-08-14 11:45:56 +05301034 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_TRUST_VALUE,
1035 sizeof(trustval), &trustval);
1036 btif_storage_get_remote_device_property(bd_addr,
1037 &remote_properties[num_props]);
1038 num_props++;
1039
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
1041 sizeof(cod), &cod);
1042 btif_storage_get_remote_device_property(bd_addr,
1043 &remote_properties[num_props]);
1044 num_props++;
1045
1046 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
1047 sizeof(devtype), &devtype);
1048 btif_storage_get_remote_device_property(bd_addr,
1049 &remote_properties[num_props]);
1050 num_props++;
1051
1052 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
1053 sizeof(remote_uuids), remote_uuids);
1054 btif_storage_get_remote_device_property(bd_addr,
1055 &remote_properties[num_props]);
1056 num_props++;
1057
1058 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1059 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
1060
1061 return BT_STATUS_SUCCESS;
1062}
1063
1064
1065/*******************************************************************************
1066**
1067** Function execute_storage_request
1068**
1069** Description Executes adapter storage request in BTIF context
1070**
1071** Returns bt_status_t
1072**
1073*******************************************************************************/
1074
1075static void execute_storage_request(UINT16 event, char *p_param)
1076{
1077 uint8_t is_local;
1078 int num_entries = 0;
1079 bt_status_t status = BT_STATUS_SUCCESS;
1080
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001081 BTIF_TRACE_EVENT("execute storage request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001082
1083 switch(event)
1084 {
1085 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1086 {
1087 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1088 bt_property_t *p_prop = &(p_req->write_req.prop);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001089 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001090 p_prop->len, p_prop->val);
1091
1092 status = btif_storage_set_adapter_property(p_prop);
1093 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
1094 } break;
1095
1096 case BTIF_CORE_STORAGE_ADAPTER_READ:
1097 {
1098 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1099 char buf[512];
1100 bt_property_t prop;
1101 prop.type = p_req->read_req.type;
1102 prop.val = (void*)buf;
1103 prop.len = sizeof(buf);
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001104 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
1105 {
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -07001106 #if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001107 tBTM_BLE_VSC_CB cmn_vsc_cb;
1108 bt_local_le_features_t local_le_features;
1109
1110 /* LE features are not stored in storage. Should be retrived from stack */
1111 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1112 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1113
1114 prop.len = sizeof (bt_local_le_features_t);
1115 if (cmn_vsc_cb.filter_support == 1)
1116 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1117 else
1118 local_le_features.max_adv_filter_supported = 0;
1119 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1120 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1121 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001122 local_le_features.scan_result_storage_size_hibyte =
1123 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1124 local_le_features.scan_result_storage_size_lobyte =
1125 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojie5ba8842014-07-03 17:18:02 -07001126 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001127 memcpy(prop.val, &local_le_features, prop.len);
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -07001128 #endif
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001129 }
1130 else
1131 {
1132 status = btif_storage_get_adapter_property(&prop);
1133 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1135 } break;
1136
1137 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1138 {
1139 status = btif_in_get_adapter_properties();
1140 } break;
1141
1142 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1143 {
1144 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1145 } break;
1146
1147 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001148 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149 break;
1150 }
1151}
1152
1153static void execute_storage_remote_request(UINT16 event, char *p_param)
1154{
1155 bt_status_t status = BT_STATUS_FAIL;
1156 bt_property_t prop;
1157
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001158 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001159
1160 switch (event)
1161 {
1162 case BTIF_CORE_STORAGE_REMOTE_READ:
1163 {
1164 char buf[1024];
1165 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1166 prop.type = p_req->read_req.type;
1167 prop.val = (void*) buf;
1168 prop.len = sizeof(buf);
1169
1170 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1171 &prop);
1172 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1173 status, &(p_req->read_req.bd_addr), 1, &prop);
1174 }break;
1175 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1176 {
1177 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1178 status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1179 &(p_req->write_req.prop));
1180 }break;
1181 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1182 {
1183 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1184 btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1185 }break;
1186 }
1187}
1188
1189void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1190 bt_property_t *p_props)
1191{
1192 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1193 status, num_props, p_props);
1194
1195}
1196void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1197 uint32_t num_props, bt_property_t *p_props)
1198{
1199 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1200 status, remote_addr, num_props, p_props);
1201}
1202
1203/*******************************************************************************
1204**
1205** Function btif_in_storage_request_copy_cb
1206**
1207** Description Switch context callback function to perform the deep copy for
1208** both the adapter and remote_device property API
1209**
1210** Returns None
1211**
1212*******************************************************************************/
1213static void btif_in_storage_request_copy_cb(UINT16 event,
1214 char *p_new_buf, char *p_old_buf)
1215{
1216 btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1217 btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1218
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001219 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001220 switch (event)
1221 {
1222 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1223 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1224 {
1225 bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1226 /* Copy the member variables one at a time */
1227 new_req->write_req.prop.type = old_req->write_req.prop.type;
1228 new_req->write_req.prop.len = old_req->write_req.prop.len;
1229
1230 new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1231 memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1232 old_req->write_req.prop.len);
1233 }break;
1234 }
1235}
1236
1237/*******************************************************************************
1238**
1239** Function btif_get_adapter_properties
1240**
1241** Description Fetch all available properties (local & remote)
1242**
1243** Returns bt_status_t
1244**
1245*******************************************************************************/
1246
1247bt_status_t btif_get_adapter_properties(void)
1248{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001249 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250
1251 if (!btif_is_enabled())
1252 return BT_STATUS_NOT_READY;
1253
1254 return btif_transfer_context(execute_storage_request,
1255 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1256 NULL, 0, NULL);
1257}
1258
1259/*******************************************************************************
1260**
1261** Function btif_get_adapter_property
1262**
1263** Description Fetches property value from local cache
1264**
1265** Returns bt_status_t
1266**
1267*******************************************************************************/
1268
1269bt_status_t btif_get_adapter_property(bt_property_type_t type)
1270{
1271 btif_storage_req_t req;
1272
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001273 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001274
1275 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1276 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1277 return BT_STATUS_NOT_READY;
1278
1279 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1280 req.read_req.type = type;
1281
1282 return btif_transfer_context(execute_storage_request,
1283 BTIF_CORE_STORAGE_ADAPTER_READ,
1284 (char*)&req, sizeof(btif_storage_req_t), NULL);
1285}
1286
1287/*******************************************************************************
1288**
1289** Function btif_set_adapter_property
1290**
1291** Description Updates core stack with property value and stores it in
1292** local cache
1293**
1294** Returns bt_status_t
1295**
1296*******************************************************************************/
1297
1298bt_status_t btif_set_adapter_property(const bt_property_t *property)
1299{
1300 btif_storage_req_t req;
1301 bt_status_t status = BT_STATUS_SUCCESS;
1302 int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1303 char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1304 UINT16 name_len = 0;
1305
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001306 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001307 property->type, property->len, property->val);
1308
1309 if (!btif_is_enabled())
1310 return BT_STATUS_NOT_READY;
1311
1312 switch(property->type)
1313 {
1314 case BT_PROPERTY_BDNAME:
1315 {
1316 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1317 property->len;
1318 memcpy(bd_name,property->val, name_len);
1319 bd_name[name_len] = '\0';
1320
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001321 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001322
1323 BTA_DmSetDeviceName((char *)bd_name);
1324
1325 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1326 }
1327 break;
1328
1329 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1330 {
1331 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1332 tBTA_DM_DISC disc_mode;
1333 tBTA_DM_CONN conn_mode;
1334
1335 switch(mode)
1336 {
1337 case BT_SCAN_MODE_NONE:
1338 disc_mode = BTA_DM_NON_DISC;
1339 conn_mode = BTA_DM_NON_CONN;
1340 break;
1341
1342 case BT_SCAN_MODE_CONNECTABLE:
1343 disc_mode = BTA_DM_NON_DISC;
1344 conn_mode = BTA_DM_CONN;
1345 break;
1346
1347 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1348 disc_mode = BTA_DM_GENERAL_DISC;
1349 conn_mode = BTA_DM_CONN;
1350 break;
1351
1352 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001353 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001354 return BT_STATUS_PARM_INVALID;
1355 }
1356
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001357 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001358
Hemant Gupta9cd26bf2013-09-10 12:01:18 +05301359 if (!btif_data_profile_registered && mode != BT_SCAN_MODE_NONE)
1360 {
1361 btif_pending_mode = mode;
1362 BTIF_TRACE_DEBUG("btif_set_adapter_property: not setting connectable mode, "
1363 "as data profiles are not yet registered. Mode will be set when "
1364 "data profile(s) are registered");
1365 return BT_STATUS_SUCCESS;
1366 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1368
1369 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1370 }
1371 break;
1372 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1373 {
1374 /* Nothing to do beside store the value in NV. Java
1375 will change the SCAN_MODE property after setting timeout,
1376 if required */
1377 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1378 }
1379 break;
1380 case BT_PROPERTY_BDADDR:
1381 case BT_PROPERTY_UUIDS:
1382 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1383 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1384 /* no write support through HAL, these properties are only populated from BTA events */
1385 status = BT_STATUS_FAIL;
1386 break;
1387 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001388 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001389 property->type);
1390 status = BT_STATUS_FAIL;
1391 break;
1392 }
1393
1394 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1395 {
1396 int btif_status;
1397 /* pass on to storage for updating local database */
1398
1399 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1400 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1401
1402 return btif_transfer_context(execute_storage_request,
1403 storage_req_id,
1404 (char*)&req,
1405 sizeof(btif_storage_req_t)+property->len,
1406 btif_in_storage_request_copy_cb);
1407 }
1408
1409 return status;
1410
1411}
1412
1413/*******************************************************************************
1414**
1415** Function btif_get_remote_device_property
1416**
1417** Description Fetches the remote device property from the NVRAM
1418**
1419** Returns bt_status_t
1420**
1421*******************************************************************************/
1422bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1423 bt_property_type_t type)
1424{
1425 btif_storage_req_t req;
1426
1427 if (!btif_is_enabled())
1428 return BT_STATUS_NOT_READY;
1429
1430 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1431 req.read_req.type = type;
1432 return btif_transfer_context(execute_storage_remote_request,
1433 BTIF_CORE_STORAGE_REMOTE_READ,
1434 (char*)&req, sizeof(btif_storage_req_t),
1435 NULL);
1436}
1437
1438/*******************************************************************************
1439**
1440** Function btif_get_remote_device_properties
1441**
1442** Description Fetches all the remote device properties from NVRAM
1443**
1444** Returns bt_status_t
1445**
1446*******************************************************************************/
1447bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1448{
1449 btif_storage_req_t req;
1450
1451 if (!btif_is_enabled())
1452 return BT_STATUS_NOT_READY;
1453
1454 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1455 return btif_transfer_context(execute_storage_remote_request,
1456 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1457 (char*)&req, sizeof(btif_storage_req_t),
1458 NULL);
1459}
1460
1461/*******************************************************************************
1462**
1463** Function btif_set_remote_device_property
1464**
1465** Description Writes the remote device property to NVRAM.
1466** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1467** remote device property that can be set
1468**
1469** Returns bt_status_t
1470**
1471*******************************************************************************/
1472bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1473 const bt_property_t *property)
1474{
1475 btif_storage_req_t req;
1476
1477 if (!btif_is_enabled())
1478 return BT_STATUS_NOT_READY;
1479
1480 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1481 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1482
1483 return btif_transfer_context(execute_storage_remote_request,
1484 BTIF_CORE_STORAGE_REMOTE_WRITE,
1485 (char*)&req,
1486 sizeof(btif_storage_req_t)+property->len,
1487 btif_in_storage_request_copy_cb);
1488}
1489
1490
1491/*******************************************************************************
1492**
1493** Function btif_get_remote_service_record
1494**
1495** Description Looks up the service matching uuid on the remote device
1496** and fetches the SCN and service_name if the UUID is found
1497**
1498** Returns bt_status_t
1499**
1500*******************************************************************************/
1501bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1502 bt_uuid_t *uuid)
1503{
1504 if (!btif_is_enabled())
1505 return BT_STATUS_NOT_READY;
1506
1507 return btif_dm_get_remote_service_record(remote_addr, uuid);
1508}
1509
1510
1511/*******************************************************************************
1512**
1513** Function btif_get_enabled_services_mask
1514**
1515** Description Fetches currently enabled services
1516**
1517** Returns tBTA_SERVICE_MASK
1518**
1519*******************************************************************************/
1520
1521tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1522{
1523 return btif_enabled_services;
1524}
1525
1526/*******************************************************************************
1527**
1528** Function btif_enable_service
1529**
1530** Description Enables the service 'service_ID' to the service_mask.
1531** Upon BT enable, BTIF core shall invoke the BTA APIs to
1532** enable the profiles
1533**
1534** Returns bt_status_t
1535**
1536*******************************************************************************/
1537bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1538{
1539 tBTA_SERVICE_ID *p_id = &service_id;
1540
1541 /* If BT is enabled, we need to switch to BTIF context and trigger the
1542 * enable for that profile
1543 *
1544 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1545 * enable for the profiles that have been enabled */
1546
1547 btif_enabled_services |= (1 << service_id);
1548
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001549 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001550
1551 if (btif_is_enabled())
1552 {
1553 btif_transfer_context(btif_dm_execute_service_request,
1554 BTIF_DM_ENABLE_SERVICE,
1555 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1556 }
1557
1558 return BT_STATUS_SUCCESS;
1559}
1560/*******************************************************************************
1561**
1562** Function btif_disable_service
1563**
1564** Description Disables the service 'service_ID' to the service_mask.
1565** Upon BT disable, BTIF core shall invoke the BTA APIs to
1566** disable the profiles
1567**
1568** Returns bt_status_t
1569**
1570*******************************************************************************/
1571bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1572{
1573 tBTA_SERVICE_ID *p_id = &service_id;
1574
1575 /* If BT is enabled, we need to switch to BTIF context and trigger the
1576 * disable for that profile so that the appropriate uuid_property_changed will
1577 * be triggerred. Otherwise, we just need to clear the service_id in the mask
1578 */
1579
1580 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1581
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001582 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583
1584 if (btif_is_enabled())
1585 {
1586 btif_transfer_context(btif_dm_execute_service_request,
1587 BTIF_DM_DISABLE_SERVICE,
1588 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1589 }
1590
1591 return BT_STATUS_SUCCESS;
1592}
Zhihai Xubad70b12013-06-04 18:21:25 -07001593
1594/*******************************************************************************
1595**
1596** Function btif_config_hci_snoop_log
1597**
1598** Description enable or disable HCI snoop log
1599**
1600** Returns bt_status_t
1601**
1602*******************************************************************************/
1603bt_status_t btif_config_hci_snoop_log(uint8_t enable)
1604{
1605 bte_main_config_hci_logging(enable != 0,
1606 btif_core_state == BTIF_CORE_STATE_DISABLED);
1607 return BT_STATUS_SUCCESS;
1608}
Hemant Gupta9cd26bf2013-09-10 12:01:18 +05301609
1610/*******************************************************************************
1611**
1612** Function btif_data_profile_register
1613**
1614** Description Sets BT_PROPERTY_ADAPTER_SCAN_MODE property when data
1615** start registering.
1616**
1617** Returns bt_status_t
1618**
1619*******************************************************************************/
1620void btif_data_profile_register(int value)
1621{
1622 bt_property_t property;
1623 int val;
1624
1625 //update the global in any case
1626 btif_data_profile_registered = value;
1627
1628 if (btif_pending_mode == BT_SCAN_MODE_NONE)
1629 {
1630 BTIF_TRACE_EVENT("%s: pending mode is BT_SCAN_MODE_NONE.. returning", __FUNCTION__);
1631 return;
1632 }
1633
1634 BTIF_TRACE_EVENT("%s: Data profile registration = %d", __FUNCTION__, value);
1635 if (btif_data_profile_registered)
1636 {
1637 property.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
1638 val = btif_pending_mode;
1639 property.val = &val;;
1640 property.len = (sizeof(int));
1641 /* Reset pending mode to None */
1642 btif_pending_mode = BT_SCAN_MODE_NONE;
1643 btif_set_adapter_property(&property);
1644 }
1645}
pramod kotreshappa9b10be52014-10-13 16:41:32 -07001646
1647/*******************************************************************************
1648**
1649** Function btif_is_shutdown
1650**
1651** Description Check btif_core_state before freeing gki buffer, do not
1652** call gki free if the gki is already shutdown.
1653**
1654** Returns btif_core_state boolean
1655**
1656*******************************************************************************/
1657BOOLEAN btif_is_shutdown(void)
1658{
1659 return (btif_core_state == BTIF_CORE_STATE_DISABLED);
1660}