blob: 469c8cf535f10626fedd1861c007000e85a38404 [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
The Android Open Source Project5738f832012-12-12 16:00:35 -080029#include <ctype.h>
30#include <cutils/properties.h>
Chris Manton0eefef02014-09-08 15:01:39 -070031#include <dirent.h>
Sharvil Nanavatiae973d52016-06-20 19:16:12 -070032#include <errno.h>
Chris Manton0eefef02014-09-08 15:01:39 -070033#include <fcntl.h>
34#include <hardware/bluetooth.h>
35#include <stdlib.h>
36#include <string.h>
37#include <sys/stat.h>
38#include <sys/types.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080039
Chris Mantonf8027002015-03-12 09:22:48 -070040#define LOG_TAG "bt_btif_core"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070041#include "btcore/include/bdaddr.h"
Chris Mantonf8027002015-03-12 09:22:48 -070042
43#include "bdaddr.h"
Zhihai Xu6c65c2f2013-11-25 17:30:59 -080044#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080045#include "bta_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#include "bte.h"
Chris Manton0eefef02014-09-08 15:01:39 -070047#include "btif_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080048#include "btif_av.h"
Chris Manton0eefef02014-09-08 15:01:39 -070049#include "btif_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080050#include "btif_pan.h"
51#include "btif_profile_queue.h"
52#include "btif_config.h"
Chris Manton0eefef02014-09-08 15:01:39 -070053#include "btif_sock.h"
54#include "btif_storage.h"
55#include "btif_util.h"
56#include "btu.h"
Chris Manton79ecab52014-10-31 14:54:51 -070057#include "device/include/controller.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070058#include "osi/include/fixed_queue.h"
59#include "osi/include/future.h"
Chris Manton0eefef02014-09-08 15:01:39 -070060#include "gki.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070061#include "osi/include/osi.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080062#include "osi/include/log.h"
Zach Johnson8ea81882014-09-18 18:13:13 -070063#include "stack_manager.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070064#include "osi/include/thread.h"
Chris Manton0eefef02014-09-08 15:01:39 -070065
The Android Open Source Project5738f832012-12-12 16:00:35 -080066/************************************************************************************
67** Constants & Macros
68************************************************************************************/
69
The Android Open Source Project5738f832012-12-12 16:00:35 -080070#ifndef BTE_DID_CONF_FILE
71#define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
72#endif
73
Srinu Jella6de08d62015-10-07 18:01:53 +053074#define VENDOR_PERSISTENCE_PATH "/persist"
75#define VENDOR_BT_NV_FILE_NAME ".bt_nv.bin"
76#define VENDOR_PAYLOAD_MAXLENGTH (260)
77#define VENDOR_MAX_CMD_HDR_SIZE (3)
78#define VENDOR_BD_ADDR_TYPE (1)
79
The Android Open Source Project5738f832012-12-12 16:00:35 -080080/************************************************************************************
81** Local type definitions
82************************************************************************************/
83
84/* These type definitions are used when passing data from the HAL to BTIF context
85* in the downstream path for the adapter and remote_device property APIs */
86
87typedef struct {
88 bt_bdaddr_t bd_addr;
89 bt_property_type_t type;
90} btif_storage_read_t;
91
92typedef struct {
93 bt_bdaddr_t bd_addr;
94 bt_property_t prop;
95} btif_storage_write_t;
96
97typedef union {
98 btif_storage_read_t read_req;
99 btif_storage_write_t write_req;
100} btif_storage_req_t;
101
102typedef enum {
103 BTIF_CORE_STATE_DISABLED = 0,
104 BTIF_CORE_STATE_ENABLING,
105 BTIF_CORE_STATE_ENABLED,
106 BTIF_CORE_STATE_DISABLING
107} btif_core_state_t;
108
109/************************************************************************************
110** Static variables
111************************************************************************************/
112
113bt_bdaddr_t btif_local_bd_addr;
114
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115static tBTA_SERVICE_MASK btif_enabled_services = 0;
116
117/*
118* This variable should be set to 1, if the Bluedroid+BTIF libraries are to
119* function in DUT mode.
120*
121* To set this, the btif_init_bluetooth needs to be called with argument as 1
122*/
123static UINT8 btif_dut_mode = 0;
124
Chris Manton0eefef02014-09-08 15:01:39 -0700125static thread_t *bt_jni_workqueue_thread;
126static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue";
127
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700128static BOOLEAN ssr_triggered = FALSE;
129
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130/************************************************************************************
131** Static functions
132************************************************************************************/
Chris Manton0eefef02014-09-08 15:01:39 -0700133static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
134static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
Ajay Panickerc1553d32015-08-03 16:29:31 -0700135static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136
137/* sends message to btif task */
138static void btif_sendmsg(void *p_msg);
139
140/************************************************************************************
141** Externs
142************************************************************************************/
Chris Manton860a9af2014-08-27 10:30:47 -0700143extern fixed_queue_t *btu_hci_msg_queue;
144
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145extern void bte_load_did_conf(const char *p_path);
146
147/** TODO: Move these to _common.h */
148void bte_main_boot_entry(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800149void bte_main_disable(void);
150void bte_main_shutdown(void);
151#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
152void bte_main_enable_lpm(BOOLEAN enable);
153#endif
154void bte_main_postload_cfg(void);
155void btif_dm_execute_service_request(UINT16 event, char *p_param);
156#ifdef BTIF_DM_OOB_TEST
157void btif_dm_load_local_oob(void);
158#endif
Zhihai Xubad70b12013-06-04 18:21:25 -0700159void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800160
The Android Open Source Project5738f832012-12-12 16:00:35 -0800161/*******************************************************************************
162**
163** Function btif_context_switched
164**
165** Description Callback used to execute transferred context callback
166**
167** p_msg : message to be executed in btif context
168**
169** Returns void
170**
171*******************************************************************************/
172
173static void btif_context_switched(void *p_msg)
174{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800175
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700176 BTIF_TRACE_VERBOSE("btif_context_switched");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800177
Chris Manton0eefef02014-09-08 15:01:39 -0700178 tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179
180 /* each callback knows how to parse the data */
181 if (p->p_cb)
182 p->p_cb(p->event, p->p_param);
183}
184
185
186/*******************************************************************************
187**
188** Function btif_transfer_context
189**
190** Description This function switches context to btif task
191**
192** p_cback : callback used to process message in btif context
193** event : event id of message
194** p_params : parameter area passed to callback (copied)
195** param_len : length of parameter area
196** p_copy_cback : If set this function will be invoked for deep copy
197**
198** Returns void
199**
200*******************************************************************************/
201
202bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
203{
204 tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
205
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700206 BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800207
208 /* allocate and send message that will be executed in btif context */
209 if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
210 {
211 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
212 p_msg->p_cb = p_cback;
213
214 p_msg->event = event; /* callback event */
215
216 /* check if caller has provided a copy callback to do the deep copy */
217 if (p_copy_cback)
218 {
219 p_copy_cback(event, p_msg->p_param, p_params);
220 }
221 else if (p_params)
222 {
223 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
224 }
225
226 btif_sendmsg(p_msg);
227 return BT_STATUS_SUCCESS;
228 }
229 else
230 {
231 /* let caller deal with a failed allocation */
232 return BT_STATUS_NOMEM;
233 }
234}
235
236/*******************************************************************************
237**
238** Function btif_is_dut_mode
239**
240** Description checks if BTIF is currently in DUT mode
241**
242** Returns 1 if test mode, otherwize 0
243**
244*******************************************************************************/
245
246UINT8 btif_is_dut_mode(void)
247{
248 return (btif_dut_mode == 1);
249}
250
251/*******************************************************************************
252**
253** Function btif_is_enabled
254**
255** Description checks if main adapter is fully enabled
256**
257** Returns 1 if fully enabled, otherwize 0
258**
259*******************************************************************************/
260
261int btif_is_enabled(void)
262{
Zach Johnsonf450ba52014-09-18 18:46:39 -0700263 return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running()));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264}
265
Chris Manton0eefef02014-09-08 15:01:39 -0700266void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
267 BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
268#if (BLE_INCLUDED == TRUE)
269 btif_dm_load_ble_local_keys();
270#endif
271 BTA_EnableBluetooth(bte_dm_evt);
272}
273
274void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
275 BTIF_TRACE_DEBUG("btif_task: hardware init failed");
276 bte_main_disable();
277 btif_queue_release();
278 bte_main_shutdown();
279 btif_dut_mode = 0;
Zach Johnson8ea81882014-09-18 18:13:13 -0700280
281 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800282}
283
284/*******************************************************************************
285**
286** Function btif_task
287**
288** Description BTIF task handler managing all messages being passed
289** Bluetooth HAL and BTA.
290**
291** Returns void
292**
293*******************************************************************************/
Zach Johnson4ada0412014-09-22 17:59:02 -0700294static void bt_jni_msg_ready(void *context) {
295 BT_HDR *p_msg = (BT_HDR *)context;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296
Chris Manton0eefef02014-09-08 15:01:39 -0700297 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298
Chris Manton0eefef02014-09-08 15:01:39 -0700299 switch (p_msg->event) {
300 case BT_EVT_CONTEXT_SWITCH_EVT:
301 btif_context_switched(p_msg);
302 break;
303 default:
304 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
305 break;
306 }
307 GKI_freebuf(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308}
309
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310/*******************************************************************************
311**
312** Function btif_sendmsg
313**
314** Description Sends msg to BTIF task
315**
316** Returns void
317**
318*******************************************************************************/
319
320void btif_sendmsg(void *p_msg)
321{
Pradeep Panigrahi5b1ce472016-01-22 14:30:26 +0530322 if(bt_jni_workqueue_thread != NULL)
323 thread_post(bt_jni_workqueue_thread, bt_jni_msg_ready, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324}
325
Zach Johnsonefea7822014-09-25 11:58:50 -0700326void btif_thread_post(thread_fn func, void *context) {
Pradeep Panigrahi5b1ce472016-01-22 14:30:26 +0530327 if(bt_jni_workqueue_thread != NULL)
328 thread_post(bt_jni_workqueue_thread, func, context);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800329}
330
Srinu Jella6de08d62015-10-07 18:01:53 +0530331static bool fetch_vendor_addr (bt_bdaddr_t *local_addr)
332{
333 int addr_fd, i;
334 int bytes_read = 0;
335 bool status = false;
336 unsigned char payload[VENDOR_PAYLOAD_MAXLENGTH];
337 unsigned char header[VENDOR_MAX_CMD_HDR_SIZE];
338 char filename[NAME_MAX];
339
340 snprintf(filename, NAME_MAX, "%s/%s",VENDOR_PERSISTENCE_PATH,VENDOR_BT_NV_FILE_NAME);
341 BTIF_TRACE_VERBOSE("Opening file '%s' for reading\n",filename);
342
343 /* Open the Vendor BD Addr file */
344 addr_fd = open(filename, O_RDONLY);
345 if(addr_fd < 0)
346 {
347 BTIF_TRACE_ERROR("Open of Vendor BD addr file failed\n");
348 return false;
349 }
350
351 while((bytes_read = read(addr_fd, header, VENDOR_MAX_CMD_HDR_SIZE)) &&
352 (bytes_read == 0 || bytes_read == VENDOR_MAX_CMD_HDR_SIZE))
353 {
354 if( VENDOR_BD_ADDR_TYPE == header[0])
355 {
356 if(read(addr_fd, local_addr, header[2]) == header[2])
357 {
358 BTIF_TRACE_WARNING("Read the Vendor BD addr from '%s'\n",filename);
359 status = true;
360 }
361 }
362 else
363 {
364 if(read(addr_fd, payload, header[2]) == header[2])
365 {
366 continue;
367 }
368 }
369 }
370
371 if (status) // swap bd address
372 {
373 char swap;
374
375 for (i = 0 ; i < 3; i++) {
376 swap = local_addr->address[i];
377 local_addr->address[i] = local_addr->address[5-i];
378 local_addr->address[5-i] = swap;
379 }
380 }
381
382 close(addr_fd);
383 return status;
384}
Ajay Panickerc1553d32015-08-03 16:29:31 -0700385static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr) {
386 char val[PROPERTY_VALUE_MAX] = {0};
387
388 if (property_get(key, val, NULL)) {
389 if (string_to_bdaddr(val, addr)) {
390 BTIF_TRACE_DEBUG("%s: Got BDA %s", __func__, val);
391 return TRUE;
392 }
393 BTIF_TRACE_DEBUG("%s: System Property did not contain valid bdaddr", __func__);
394 }
395 return FALSE;
396}
Srinu Jella6de08d62015-10-07 18:01:53 +0530397
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
399{
Ajay Panickerc1553d32015-08-03 16:29:31 -0700400 char val[PROPERTY_VALUE_MAX] = {0};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401 uint8_t valid_bda = FALSE;
402 int val_size = 0;
403 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
404
405 /* Get local bdaddr storage path from property */
406 if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
407 {
408 int addr_fd;
409
Ajay Panickerc1553d32015-08-03 16:29:31 -0700410 BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800411
Sharvil Nanavatiae973d52016-06-20 19:16:12 -0700412 if ((addr_fd = TEMP_FAILURE_RETRY(open(val, O_RDONLY))) != -1)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800413 {
414 memset(val, 0, sizeof(val));
Sharvil Nanavatiae973d52016-06-20 19:16:12 -0700415 TEMP_FAILURE_RETRY(read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416 /* If this is not a reserved/special bda, then use it */
Ajay Panickerc1553d32015-08-03 16:29:31 -0700417 if ((string_to_bdaddr(val, local_addr)) &&
418 (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419 {
420 valid_bda = TRUE;
Ajay Panickerc1553d32015-08-03 16:29:31 -0700421 BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800423 close(addr_fd);
424 }
425 }
426
427 if(!valid_bda)
428 {
429 val_size = sizeof(val);
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700430 if(btif_config_get_str("Adapter", "Address", val, &val_size))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800431 {
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700432 string_to_bdaddr(val, local_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700433 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 return;
435 }
436 }
437
438 /* No factory BDADDR found. Look for previously generated random BDA */
Ajay Panickerc1553d32015-08-03 16:29:31 -0700439 if (!valid_bda) {
440 valid_bda = btif_fetch_property(PERSIST_BDADDR_PROPERTY, local_addr);
441 }
442
443 /* No BDADDR found in file. Look for BDA in factory property */
444 if (!valid_bda) {
445 valid_bda = btif_fetch_property(FACTORY_BT_ADDR_PROPERTY, local_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446 }
447
Srinu Jella6de08d62015-10-07 18:01:53 +0530448 /* No factory BDADDR found. Look for BDA in ro.boot.btmacaddr */
449 if ((!valid_bda) && \
450 (property_get("ro.boot.btmacaddr", val, NULL)))
451 {
452 valid_bda = string_to_bdaddr(val, local_addr);
453 if (valid_bda) {
454 BTIF_TRACE_DEBUG("Got vendor BDA %02X:%02X:%02X:%02X:%02X:%02X",
455 local_addr->address[0], local_addr->address[1], local_addr->address[2],
456 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
457 }
458 }
459
460 if (!valid_bda && fetch_vendor_addr(local_addr))
461 {
462 valid_bda = TRUE;
463 BTIF_TRACE_DEBUG("Got Vendor BDA %02X:%02X:%02X:%02X:%02X:%02X",
464 local_addr->address[0], local_addr->address[1], local_addr->address[2],
465 local_addr->address[3], local_addr->address[4], local_addr->address[5]);
466 }
467
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468 /* Generate new BDA if necessary */
469 if (!valid_bda)
470 {
471 bdstr_t bdstr;
472 /* Seed the random number generator */
473 srand((unsigned int) (time(0)));
474
475 /* No autogen BDA. Generate one now. */
476 local_addr->address[0] = 0x22;
477 local_addr->address[1] = 0x22;
478 local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
479 local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
480 local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
481 local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
482
483 /* Convert to ascii, and store as a persistent property */
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700484 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700486 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800487 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
488
489 if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700490 BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491 }
492
493 //save the bd address to config file
494 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700495 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800496 val_size = sizeof(val);
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700497 if (btif_config_get_str("Adapter", "Address", val, &val_size))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800498 {
499 if (strcmp(bdstr, val) ==0)
500 {
501 // BDA is already present in the config file.
502 return;
503 }
504 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700505 btif_config_set_str("Adapter", "Address", bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800506}
507
The Android Open Source Project5738f832012-12-12 16:00:35 -0800508/*******************************************************************************
509**
510** Function btif_init_bluetooth
511**
512** Description Creates BTIF task and prepares BT scheduler for startup
513**
514** Returns bt_status_t
515**
516*******************************************************************************/
Chris Manton0eefef02014-09-08 15:01:39 -0700517bt_status_t btif_init_bluetooth() {
Chris Manton0eefef02014-09-08 15:01:39 -0700518 bte_main_boot_entry();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800519
Chris Manton0eefef02014-09-08 15:01:39 -0700520 /* As part of the init, fetch the local BD ADDR */
521 memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
522 btif_fetch_local_bdaddr(&btif_local_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523
Chris Manton0eefef02014-09-08 15:01:39 -0700524 bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME);
525 if (bt_jni_workqueue_thread == NULL) {
Sharvil Nanavati364a1e72014-12-25 22:17:09 -0800526 LOG_ERROR("%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME);
Chris Manton0eefef02014-09-08 15:01:39 -0700527 goto error_exit;
528 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529
Chris Manton0eefef02014-09-08 15:01:39 -0700530 // Associate this workqueue thread with jni.
531 btif_transfer_context(btif_jni_associate, 0, NULL, 0, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800532
Chris Manton0eefef02014-09-08 15:01:39 -0700533 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800534
Chris Manton0eefef02014-09-08 15:01:39 -0700535error_exit:;
Chris Manton0eefef02014-09-08 15:01:39 -0700536 thread_free(bt_jni_workqueue_thread);
537
Zach Johnson4ada0412014-09-22 17:59:02 -0700538 bt_jni_workqueue_thread = NULL;
539
Chris Manton0eefef02014-09-08 15:01:39 -0700540 return BT_STATUS_FAIL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800541}
542
543/*******************************************************************************
544**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545** Function btif_enable_bluetooth_evt
546**
547** Description Event indicating bluetooth enable is completed
548** Notifies HAL user with updated adapter state
549**
550** Returns void
551**
552*******************************************************************************/
553
Zach Johnson39110ec2014-10-06 13:15:00 -0700554void btif_enable_bluetooth_evt(tBTA_STATUS status)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555{
Zach Johnson39110ec2014-10-06 13:15:00 -0700556 const controller_t *controller = controller_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800557 bdstr_t bdstr;
Zach Johnson39110ec2014-10-06 13:15:00 -0700558 bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559
Zach Johnson39110ec2014-10-06 13:15:00 -0700560 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status, bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700562 ssr_triggered = FALSE;
563
Zach Johnson39110ec2014-10-06 13:15:00 -0700564 if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565 {
Zach Johnson39110ec2014-10-06 13:15:00 -0700566 // TODO(zachoverflow): this whole code path seems like a bad time waiting to happen
567 // We open the vendor library using the old address.
568 bdstr_t old_address;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569 bt_property_t prop;
570
Zach Johnson39110ec2014-10-06 13:15:00 -0700571 bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address));
572
The Android Open Source Project5738f832012-12-12 16:00:35 -0800573 /**
574 * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
Zach Johnson39110ec2014-10-06 13:15:00 -0700575 * This could be because the factory BDADDR was stored separately in
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576 * the Controller's non-volatile memory rather than in device's file
577 * system.
578 **/
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700579 BTIF_TRACE_WARNING("***********************************************");
Zach Johnson39110ec2014-10-06 13:15:00 -0700580 BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address);
581 BTIF_TRACE_WARNING("Controller BDA is %s", bdstr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700582 BTIF_TRACE_WARNING("***********************************************");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583
Zach Johnson39110ec2014-10-06 13:15:00 -0700584 btif_local_bd_addr = *controller->get_address();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800585
586 //save the bd address to config file
Zach Johnson39110ec2014-10-06 13:15:00 -0700587 btif_config_set_str("Adapter", "Address", bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588 btif_config_save();
589
590 //fire HAL callback for property change
The Android Open Source Project5738f832012-12-12 16:00:35 -0800591 prop.type = BT_PROPERTY_BDADDR;
Zach Johnson39110ec2014-10-06 13:15:00 -0700592 prop.val = (void*)&btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800593 prop.len = sizeof(bt_bdaddr_t);
594 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
595 }
596
597 bte_main_postload_cfg();
598#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
599 bte_main_enable_lpm(TRUE);
600#endif
601 /* add passing up bd address as well ? */
602
603 /* callback to HAL */
604 if (status == BTA_SUCCESS)
605 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606 /* init rfcomm & l2cap api */
607 btif_sock_init();
608
The Android Open Source Project5738f832012-12-12 16:00:35 -0800609 /* load did configuration */
610 bte_load_did_conf(BTE_DID_CONF_FILE);
611
venkata Jagadeesh2626f0a2015-06-16 15:29:04 +0530612 /* init pan */
613 btif_pan_init();
614
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615#ifdef BTIF_DM_OOB_TEST
616 btif_dm_load_local_oob();
617#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800618
Zach Johnson8ea81882014-09-18 18:13:13 -0700619 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800620 }
621 else
622 {
623 /* cleanup rfcomm & l2cap api */
624 btif_sock_cleanup();
625
626 btif_pan_cleanup();
627
Zach Johnson8ea81882014-09-18 18:13:13 -0700628 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800629 }
630}
631
632/*******************************************************************************
633**
634** Function btif_disable_bluetooth
635**
636** Description Inititates shutdown of Bluetooth system.
637** Any active links will be dropped and device entering
638** non connectable/discoverable mode
639**
640** Returns void
641**
642*******************************************************************************/
643bt_status_t btif_disable_bluetooth(void)
644{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700645 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800646
647 btif_dm_on_disable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648 /* cleanup rfcomm & l2cap api */
649 btif_sock_cleanup();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650 btif_pan_cleanup();
Zach Johnsonf450ba52014-09-18 18:46:39 -0700651 BTA_DisableBluetooth();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800652
Subramanian Srinivasan9233de62016-02-02 11:46:47 -0800653#if (BLE_INCLUDED == TRUE)
654 BTA_VendorCleanup();
655 BTA_StopBleTimers();
656#endif
657
The Android Open Source Project5738f832012-12-12 16:00:35 -0800658 return BT_STATUS_SUCCESS;
659}
660
661/*******************************************************************************
662**
663** Function btif_disable_bluetooth_evt
664**
665** Description Event notifying BT disable is now complete.
666** Terminates main stack tasks and notifies HAL
667** user with updated BT state.
668**
669** Returns void
670**
671*******************************************************************************/
672
673void btif_disable_bluetooth_evt(void)
674{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700675 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700676 if (ssr_triggered == TRUE)
677 {
678 BTIF_TRACE_DEBUG("%s SSR triggered,Ignore EVT",__FUNCTION__);
679 return;
680 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800681#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
682 bte_main_enable_lpm(FALSE);
683#endif
684
Satya Callojid5aa2472014-09-23 18:27:09 -0700685 bte_main_disable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800686
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687 /* callback to HAL */
Zach Johnson1924dac2014-09-24 16:01:29 -0700688 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800689}
690
The Android Open Source Project5738f832012-12-12 16:00:35 -0800691/*******************************************************************************
692**
693** Function btif_shutdown_bluetooth
694**
695** Description Finalizes BT scheduler shutdown and terminates BTIF
696** task.
697**
698** Returns void
699**
700*******************************************************************************/
701
702bt_status_t btif_shutdown_bluetooth(void)
703{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700704 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705
Chris Manton0eefef02014-09-08 15:01:39 -0700706 btif_transfer_context(btif_jni_disassociate, 0, NULL, 0, NULL);
Zhihai Xu6c65c2f2013-11-25 17:30:59 -0800707
The Android Open Source Project5738f832012-12-12 16:00:35 -0800708 btif_queue_release();
Chris Manton0eefef02014-09-08 15:01:39 -0700709
Chris Manton0eefef02014-09-08 15:01:39 -0700710 thread_free(bt_jni_workqueue_thread);
711 bt_jni_workqueue_thread = NULL;
712
The Android Open Source Project5738f832012-12-12 16:00:35 -0800713 bte_main_shutdown();
714
715 btif_dut_mode = 0;
716
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700717 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718
719 return BT_STATUS_SUCCESS;
720}
721
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722/*******************************************************************************
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700723Function btif_ssrcleanup
724Description Trigger SSR when Disable timeout occured
725
726*******************************************************************************/
727void btif_ssr_cleanup(void)
728{
729 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
730 ssr_triggered = TRUE;
pramod kotreshappa84f60eb2015-09-10 14:33:09 -0700731 bte_ssr_cleanup(0x11);//SSR reason 0x11 - ENABLE_TIMEOUT
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700732}
733
Srinu Jella51ca16f2013-12-06 15:25:26 +0530734/****************************************************************************
735**
736** BTIF Test Mode APIs
737**
738*****************************************************************************/
739#if HCI_RAW_CMD_INCLUDED == TRUE
740/*******************************************************************************
741**
742** Function btif_hci_event_cback
743**
744** Description Callback invoked on receiving HCI event
745**
746** Returns None
747**
748*******************************************************************************/
749static void btif_hci_event_cback ( tBTM_RAW_CMPL *p )
750{
751 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Balraj Selvarajf35ca6a2015-10-21 19:44:04 +0530752 if((p != NULL) && (bt_hal_cbacks != NULL)
753 && (bt_hal_cbacks->hci_event_recv_cb != NULL))
Srinu Jella51ca16f2013-12-06 15:25:26 +0530754 {
755 HAL_CBACK(bt_hal_cbacks, hci_event_recv_cb, p->event_code, p->p_param_buf,
756 p->param_len);
757 }
758}
759
760/*******************************************************************************
761**
762** Function btif_hci_cmd_send
763**
764** Description Sends a HCI raw command to the controller
765**
766** Returns BT_STATUS_SUCCESS on success
767**
768*******************************************************************************/
769bt_status_t btif_hci_cmd_send(uint16_t opcode, uint8_t *buf, uint8_t len)
770{
771 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
772
773 BTM_Hci_Raw_Command(opcode, len, buf, btif_hci_event_cback);
774 return BT_STATUS_SUCCESS;
775}
776#endif
777
Kiran Kelageri4a924fd2015-08-04 15:46:33 -0700778/*******************************************************************************
The Android Open Source Project5738f832012-12-12 16:00:35 -0800779**
780** Function btif_dut_mode_cback
781**
782** Description Callback invoked on completion of vendor specific test mode command
783**
784** Returns None
785**
786*******************************************************************************/
787static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
788{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800789 UNUSED(p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 /* For now nothing to be done. */
791}
792
793/*******************************************************************************
794**
795** Function btif_dut_mode_configure
796**
797** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
798** test mode
799**
800** Returns BT_STATUS_SUCCESS on success
801**
802*******************************************************************************/
803bt_status_t btif_dut_mode_configure(uint8_t enable)
804{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700805 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806
Zach Johnsonf450ba52014-09-18 18:46:39 -0700807 if (!stack_manager_get_interface()->get_stack_is_running()) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700808 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800809 return BT_STATUS_NOT_READY;
810 }
811
812 btif_dut_mode = enable;
813 if (enable == 1) {
814 BTA_EnableTestMode();
815 } else {
816 BTA_DisableTestMode();
817 }
818 return BT_STATUS_SUCCESS;
819}
820
821/*******************************************************************************
822**
823** Function btif_dut_mode_send
824**
825** Description Sends a HCI Vendor specific command to the controller
826**
827** Returns BT_STATUS_SUCCESS on success
828**
829*******************************************************************************/
830bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
831{
832 /* TODO: Check that opcode is a vendor command group */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700833 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800834 if (!btif_is_dut_mode()) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700835 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 -0800836 return BT_STATUS_FAIL;
837 }
838 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
839 return BT_STATUS_SUCCESS;
840}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800841
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842/*****************************************************************************
843**
844** btif api adapter property functions
845**
846*****************************************************************************/
847
848static bt_status_t btif_in_get_adapter_properties(void)
849{
850 bt_property_t properties[6];
851 uint32_t num_props;
852
853 bt_bdaddr_t addr;
854 bt_bdname_t name;
855 bt_scan_mode_t mode;
856 uint32_t disc_timeout;
857 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
858 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
859 num_props = 0;
860
861 /* BD_ADDR */
862 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
863 sizeof(addr), &addr);
864 btif_storage_get_adapter_property(&properties[num_props]);
865 num_props++;
866
867 /* BD_NAME */
868 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
869 sizeof(name), &name);
870 btif_storage_get_adapter_property(&properties[num_props]);
871 num_props++;
872
873 /* SCAN_MODE */
874 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
875 sizeof(mode), &mode);
876 btif_storage_get_adapter_property(&properties[num_props]);
877 num_props++;
878
879 /* DISC_TIMEOUT */
880 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
881 sizeof(disc_timeout), &disc_timeout);
882 btif_storage_get_adapter_property(&properties[num_props]);
883 num_props++;
884
885 /* BONDED_DEVICES */
886 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
887 sizeof(bonded_devices), bonded_devices);
888 btif_storage_get_adapter_property(&properties[num_props]);
889 num_props++;
890
891 /* LOCAL UUIDs */
892 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
893 sizeof(local_uuids), local_uuids);
894 btif_storage_get_adapter_property(&properties[num_props]);
895 num_props++;
896
897 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
898 BT_STATUS_SUCCESS, num_props, properties);
899
900 return BT_STATUS_SUCCESS;
901}
902
903static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
904{
905 bt_property_t remote_properties[8];
906 uint32_t num_props = 0;
907
908 bt_bdname_t name, alias;
909 uint32_t cod, devtype;
910 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
911
912 memset(remote_properties, 0, sizeof(remote_properties));
913 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
914 sizeof(name), &name);
915 btif_storage_get_remote_device_property(bd_addr,
916 &remote_properties[num_props]);
917 num_props++;
918
919 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
920 sizeof(alias), &alias);
921 btif_storage_get_remote_device_property(bd_addr,
922 &remote_properties[num_props]);
923 num_props++;
924
925 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
926 sizeof(cod), &cod);
927 btif_storage_get_remote_device_property(bd_addr,
928 &remote_properties[num_props]);
929 num_props++;
930
931 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
932 sizeof(devtype), &devtype);
933 btif_storage_get_remote_device_property(bd_addr,
934 &remote_properties[num_props]);
935 num_props++;
936
937 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
938 sizeof(remote_uuids), remote_uuids);
939 btif_storage_get_remote_device_property(bd_addr,
940 &remote_properties[num_props]);
941 num_props++;
942
943 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
944 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
945
946 return BT_STATUS_SUCCESS;
947}
948
949
950/*******************************************************************************
951**
952** Function execute_storage_request
953**
954** Description Executes adapter storage request in BTIF context
955**
956** Returns bt_status_t
957**
958*******************************************************************************/
959
960static void execute_storage_request(UINT16 event, char *p_param)
961{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962 bt_status_t status = BT_STATUS_SUCCESS;
963
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700964 BTIF_TRACE_EVENT("execute storage request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965
966 switch(event)
967 {
968 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
969 {
970 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
971 bt_property_t *p_prop = &(p_req->write_req.prop);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700972 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800973 p_prop->len, p_prop->val);
974
975 status = btif_storage_set_adapter_property(p_prop);
976 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
977 } break;
978
979 case BTIF_CORE_STORAGE_ADAPTER_READ:
980 {
981 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
982 char buf[512];
983 bt_property_t prop;
984 prop.type = p_req->read_req.type;
985 prop.val = (void*)buf;
986 prop.len = sizeof(buf);
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700987 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
988 {
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -0700989 #if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700990 tBTM_BLE_VSC_CB cmn_vsc_cb;
991 bt_local_le_features_t local_le_features;
992
993 /* LE features are not stored in storage. Should be retrived from stack */
994 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
995 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
996
997 prop.len = sizeof (bt_local_le_features_t);
998 if (cmn_vsc_cb.filter_support == 1)
999 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1000 else
1001 local_le_features.max_adv_filter_supported = 0;
1002 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1003 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1004 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojif5387072015-02-09 17:40:52 -08001005 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
Satya Callojie5ba8842014-07-03 17:18:02 -07001006 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Satya Callojif5387072015-02-09 17:40:52 -08001007 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1008 local_le_features.total_trackable_advertisers =
1009 cmn_vsc_cb.total_trackable_advertisers;
Satya Calloji5725fc62015-03-31 13:24:32 -07001010
1011 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1012 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001013 memcpy(prop.val, &local_le_features, prop.len);
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -07001014 #endif
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001015 }
1016 else
1017 {
1018 status = btif_storage_get_adapter_property(&prop);
1019 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1021 } break;
1022
1023 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1024 {
1025 status = btif_in_get_adapter_properties();
1026 } break;
1027
1028 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1029 {
1030 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1031 } break;
1032
1033 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001034 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035 break;
1036 }
1037}
1038
1039static void execute_storage_remote_request(UINT16 event, char *p_param)
1040{
1041 bt_status_t status = BT_STATUS_FAIL;
1042 bt_property_t prop;
1043
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001044 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045
1046 switch (event)
1047 {
1048 case BTIF_CORE_STORAGE_REMOTE_READ:
1049 {
1050 char buf[1024];
1051 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1052 prop.type = p_req->read_req.type;
1053 prop.val = (void*) buf;
1054 prop.len = sizeof(buf);
1055
1056 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1057 &prop);
1058 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1059 status, &(p_req->read_req.bd_addr), 1, &prop);
1060 }break;
1061 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1062 {
1063 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1064 status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1065 &(p_req->write_req.prop));
1066 }break;
1067 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1068 {
1069 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1070 btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1071 }break;
1072 }
1073}
1074
1075void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1076 bt_property_t *p_props)
1077{
1078 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1079 status, num_props, p_props);
1080
1081}
1082void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1083 uint32_t num_props, bt_property_t *p_props)
1084{
1085 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1086 status, remote_addr, num_props, p_props);
1087}
1088
1089/*******************************************************************************
1090**
1091** Function btif_in_storage_request_copy_cb
1092**
1093** Description Switch context callback function to perform the deep copy for
1094** both the adapter and remote_device property API
1095**
1096** Returns None
1097**
1098*******************************************************************************/
1099static void btif_in_storage_request_copy_cb(UINT16 event,
1100 char *p_new_buf, char *p_old_buf)
1101{
1102 btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1103 btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1104
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001105 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001106 switch (event)
1107 {
1108 case BTIF_CORE_STORAGE_REMOTE_WRITE:
1109 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1110 {
1111 bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1112 /* Copy the member variables one at a time */
1113 new_req->write_req.prop.type = old_req->write_req.prop.type;
1114 new_req->write_req.prop.len = old_req->write_req.prop.len;
1115
1116 new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1117 memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1118 old_req->write_req.prop.len);
1119 }break;
1120 }
1121}
1122
1123/*******************************************************************************
1124**
1125** Function btif_get_adapter_properties
1126**
1127** Description Fetch all available properties (local & remote)
1128**
1129** Returns bt_status_t
1130**
1131*******************************************************************************/
1132
1133bt_status_t btif_get_adapter_properties(void)
1134{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001135 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001136
1137 if (!btif_is_enabled())
1138 return BT_STATUS_NOT_READY;
1139
1140 return btif_transfer_context(execute_storage_request,
1141 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1142 NULL, 0, NULL);
1143}
1144
1145/*******************************************************************************
1146**
1147** Function btif_get_adapter_property
1148**
1149** Description Fetches property value from local cache
1150**
1151** Returns bt_status_t
1152**
1153*******************************************************************************/
1154
1155bt_status_t btif_get_adapter_property(bt_property_type_t type)
1156{
1157 btif_storage_req_t req;
1158
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001159 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001160
1161 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1162 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1163 return BT_STATUS_NOT_READY;
1164
1165 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1166 req.read_req.type = type;
1167
1168 return btif_transfer_context(execute_storage_request,
1169 BTIF_CORE_STORAGE_ADAPTER_READ,
1170 (char*)&req, sizeof(btif_storage_req_t), NULL);
1171}
1172
1173/*******************************************************************************
1174**
1175** Function btif_set_adapter_property
1176**
1177** Description Updates core stack with property value and stores it in
1178** local cache
1179**
1180** Returns bt_status_t
1181**
1182*******************************************************************************/
1183
1184bt_status_t btif_set_adapter_property(const bt_property_t *property)
1185{
1186 btif_storage_req_t req;
1187 bt_status_t status = BT_STATUS_SUCCESS;
1188 int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1189 char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1190 UINT16 name_len = 0;
1191
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001192 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001193 property->type, property->len, property->val);
1194
1195 if (!btif_is_enabled())
1196 return BT_STATUS_NOT_READY;
1197
1198 switch(property->type)
1199 {
1200 case BT_PROPERTY_BDNAME:
1201 {
1202 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1203 property->len;
1204 memcpy(bd_name,property->val, name_len);
1205 bd_name[name_len] = '\0';
1206
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001207 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001208
1209 BTA_DmSetDeviceName((char *)bd_name);
1210
1211 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1212 }
1213 break;
1214
1215 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1216 {
1217 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1218 tBTA_DM_DISC disc_mode;
1219 tBTA_DM_CONN conn_mode;
1220
1221 switch(mode)
1222 {
1223 case BT_SCAN_MODE_NONE:
1224 disc_mode = BTA_DM_NON_DISC;
1225 conn_mode = BTA_DM_NON_CONN;
1226 break;
1227
1228 case BT_SCAN_MODE_CONNECTABLE:
1229 disc_mode = BTA_DM_NON_DISC;
1230 conn_mode = BTA_DM_CONN;
1231 break;
1232
1233 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1234 disc_mode = BTA_DM_GENERAL_DISC;
1235 conn_mode = BTA_DM_CONN;
1236 break;
1237
1238 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001239 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001240 return BT_STATUS_PARM_INVALID;
1241 }
1242
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001243 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001244
1245 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1246
1247 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1248 }
1249 break;
1250 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1251 {
1252 /* Nothing to do beside store the value in NV. Java
1253 will change the SCAN_MODE property after setting timeout,
1254 if required */
1255 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1256 }
1257 break;
1258 case BT_PROPERTY_BDADDR:
1259 case BT_PROPERTY_UUIDS:
1260 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1261 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1262 /* no write support through HAL, these properties are only populated from BTA events */
1263 status = BT_STATUS_FAIL;
1264 break;
1265 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001266 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001267 property->type);
1268 status = BT_STATUS_FAIL;
1269 break;
1270 }
1271
1272 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1273 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001274 /* pass on to storage for updating local database */
1275
1276 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1277 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1278
1279 return btif_transfer_context(execute_storage_request,
1280 storage_req_id,
1281 (char*)&req,
1282 sizeof(btif_storage_req_t)+property->len,
1283 btif_in_storage_request_copy_cb);
1284 }
1285
1286 return status;
1287
1288}
1289
1290/*******************************************************************************
1291**
1292** Function btif_get_remote_device_property
1293**
1294** Description Fetches the remote device property from the NVRAM
1295**
1296** Returns bt_status_t
1297**
1298*******************************************************************************/
1299bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1300 bt_property_type_t type)
1301{
1302 btif_storage_req_t req;
1303
1304 if (!btif_is_enabled())
1305 return BT_STATUS_NOT_READY;
1306
1307 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1308 req.read_req.type = type;
1309 return btif_transfer_context(execute_storage_remote_request,
1310 BTIF_CORE_STORAGE_REMOTE_READ,
1311 (char*)&req, sizeof(btif_storage_req_t),
1312 NULL);
1313}
1314
1315/*******************************************************************************
1316**
1317** Function btif_get_remote_device_properties
1318**
1319** Description Fetches all the remote device properties from NVRAM
1320**
1321** Returns bt_status_t
1322**
1323*******************************************************************************/
1324bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1325{
1326 btif_storage_req_t req;
1327
1328 if (!btif_is_enabled())
1329 return BT_STATUS_NOT_READY;
1330
1331 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1332 return btif_transfer_context(execute_storage_remote_request,
1333 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1334 (char*)&req, sizeof(btif_storage_req_t),
1335 NULL);
1336}
1337
1338/*******************************************************************************
1339**
1340** Function btif_set_remote_device_property
1341**
1342** Description Writes the remote device property to NVRAM.
1343** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1344** remote device property that can be set
1345**
1346** Returns bt_status_t
1347**
1348*******************************************************************************/
1349bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1350 const bt_property_t *property)
1351{
1352 btif_storage_req_t req;
1353
1354 if (!btif_is_enabled())
1355 return BT_STATUS_NOT_READY;
1356
1357 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1358 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1359
1360 return btif_transfer_context(execute_storage_remote_request,
1361 BTIF_CORE_STORAGE_REMOTE_WRITE,
1362 (char*)&req,
1363 sizeof(btif_storage_req_t)+property->len,
1364 btif_in_storage_request_copy_cb);
1365}
1366
1367
1368/*******************************************************************************
1369**
1370** Function btif_get_remote_service_record
1371**
1372** Description Looks up the service matching uuid on the remote device
1373** and fetches the SCN and service_name if the UUID is found
1374**
1375** Returns bt_status_t
1376**
1377*******************************************************************************/
1378bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1379 bt_uuid_t *uuid)
1380{
1381 if (!btif_is_enabled())
1382 return BT_STATUS_NOT_READY;
1383
1384 return btif_dm_get_remote_service_record(remote_addr, uuid);
1385}
1386
1387
1388/*******************************************************************************
1389**
1390** Function btif_get_enabled_services_mask
1391**
1392** Description Fetches currently enabled services
1393**
1394** Returns tBTA_SERVICE_MASK
1395**
1396*******************************************************************************/
1397
1398tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1399{
1400 return btif_enabled_services;
1401}
1402
1403/*******************************************************************************
1404**
1405** Function btif_enable_service
1406**
1407** Description Enables the service 'service_ID' to the service_mask.
1408** Upon BT enable, BTIF core shall invoke the BTA APIs to
1409** enable the profiles
1410**
1411** Returns bt_status_t
1412**
1413*******************************************************************************/
1414bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1415{
1416 tBTA_SERVICE_ID *p_id = &service_id;
1417
1418 /* If BT is enabled, we need to switch to BTIF context and trigger the
1419 * enable for that profile
1420 *
1421 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1422 * enable for the profiles that have been enabled */
1423
1424 btif_enabled_services |= (1 << service_id);
1425
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001426 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001427
1428 if (btif_is_enabled())
1429 {
1430 btif_transfer_context(btif_dm_execute_service_request,
1431 BTIF_DM_ENABLE_SERVICE,
1432 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1433 }
1434
1435 return BT_STATUS_SUCCESS;
1436}
1437/*******************************************************************************
1438**
1439** Function btif_disable_service
1440**
1441** Description Disables the service 'service_ID' to the service_mask.
1442** Upon BT disable, BTIF core shall invoke the BTA APIs to
1443** disable the profiles
1444**
1445** Returns bt_status_t
1446**
1447*******************************************************************************/
1448bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1449{
1450 tBTA_SERVICE_ID *p_id = &service_id;
1451
1452 /* If BT is enabled, we need to switch to BTIF context and trigger the
1453 * disable for that profile so that the appropriate uuid_property_changed will
1454 * be triggerred. Otherwise, we just need to clear the service_id in the mask
1455 */
1456
1457 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1458
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001459 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001460
1461 if (btif_is_enabled())
1462 {
1463 btif_transfer_context(btif_dm_execute_service_request,
1464 BTIF_DM_DISABLE_SERVICE,
1465 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1466 }
1467
1468 return BT_STATUS_SUCCESS;
1469}
Zhihai Xubad70b12013-06-04 18:21:25 -07001470
Chris Manton0eefef02014-09-08 15:01:39 -07001471static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
1472 BTIF_TRACE_DEBUG("%s Associating thread to JVM", __func__);
1473 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
Zhihai Xubad70b12013-06-04 18:21:25 -07001474}
Chris Manton0eefef02014-09-08 15:01:39 -07001475
1476static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
Zach Johnson8ea81882014-09-18 18:13:13 -07001477 BTIF_TRACE_DEBUG("%s Disassociating thread from JVM", __func__);
1478 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
1479 bt_hal_cbacks = NULL;
1480 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
Chris Manton0eefef02014-09-08 15:01:39 -07001481}
1482