blob: 3326ad6f1da1c5d5467d7661a23e4f337f18145c [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
Marie Janssen49120dc2015-07-07 16:47:20 -070029#define LOG_TAG "bt_btif_core"
30
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include <ctype.h>
Chris Manton0eefef02014-09-08 15:01:39 -070032#include <dirent.h>
33#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>
Arman Ugurayd30195c2015-05-29 15:27:58 -070039#include <unistd.h>
40
Chris Mantonf8027002015-03-12 09:22:48 -070041#include "bdaddr.h"
Zhihai Xu6c65c2f2013-11-25 17:30:59 -080042#include "bt_utils.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#include "bta_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080044#include "bte.h"
Chris Manton0eefef02014-09-08 15:01:39 -070045#include "btif_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#include "btif_av.h"
Chris Manton0eefef02014-09-08 15:01:39 -070047#include "btif_config.h"
Marie Janssendb554582015-06-26 14:53:46 -070048#include "btif_config.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080049#include "btif_pan.h"
50#include "btif_profile_queue.h"
Chris Manton0eefef02014-09-08 15:01:39 -070051#include "btif_sock.h"
52#include "btif_storage.h"
Adam Lesinski0620f972015-12-02 22:15:08 -080053#include "btif_uid.h"
Chris Manton0eefef02014-09-08 15:01:39 -070054#include "btif_util.h"
55#include "btu.h"
Chris Manton79ecab52014-10-31 14:54:51 -070056#include "device/include/controller.h"
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070057#include "bt_common.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070058#include "osi/include/fixed_queue.h"
59#include "osi/include/future.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080060#include "osi/include/log.h"
Marie Janssendb554582015-06-26 14:53:46 -070061#include "osi/include/osi.h"
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -080062#include "osi/include/properties.h"
Sharvil Nanavati0f9b91e2015-03-12 15:42:50 -070063#include "osi/include/thread.h"
Marie Janssendb554582015-06-26 14:53:46 -070064#include "stack_manager.h"
Chris Manton0eefef02014-09-08 15:01:39 -070065
Bryce Lee3d6accf2016-05-10 17:10:09 -070066#ifdef __cplusplus
67extern "C" {
68#endif
69
The Android Open Source Project5738f832012-12-12 16:00:35 -080070/************************************************************************************
71** Constants & Macros
72************************************************************************************/
73
The Android Open Source Project5738f832012-12-12 16:00:35 -080074#ifndef BTE_DID_CONF_FILE
Arman Ugurayf2d64342015-07-08 15:47:39 -070075// TODO(armansito): Find a better way than searching by a hardcoded path.
76#if defined(OS_GENERIC)
77#define BTE_DID_CONF_FILE "bt_did.conf"
78#else // !defined(OS_GENERIC)
The Android Open Source Project5738f832012-12-12 16:00:35 -080079#define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
Arman Ugurayf2d64342015-07-08 15:47:39 -070080#endif // defined(OS_GENERIC)
81#endif // BTE_DID_CONF_FILE
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
The Android Open Source Project5738f832012-12-12 16:00:35 -080083/************************************************************************************
84** Local type definitions
85************************************************************************************/
86
87/* These type definitions are used when passing data from the HAL to BTIF context
88* in the downstream path for the adapter and remote_device property APIs */
89
90typedef struct {
91 bt_bdaddr_t bd_addr;
92 bt_property_type_t type;
93} btif_storage_read_t;
94
95typedef struct {
96 bt_bdaddr_t bd_addr;
97 bt_property_t prop;
98} btif_storage_write_t;
99
100typedef union {
101 btif_storage_read_t read_req;
102 btif_storage_write_t write_req;
103} btif_storage_req_t;
104
105typedef enum {
106 BTIF_CORE_STATE_DISABLED = 0,
107 BTIF_CORE_STATE_ENABLING,
108 BTIF_CORE_STATE_ENABLED,
109 BTIF_CORE_STATE_DISABLING
110} btif_core_state_t;
111
112/************************************************************************************
113** Static variables
114************************************************************************************/
115
116bt_bdaddr_t btif_local_bd_addr;
117
The Android Open Source Project5738f832012-12-12 16:00:35 -0800118static tBTA_SERVICE_MASK btif_enabled_services = 0;
119
120/*
121* This variable should be set to 1, if the Bluedroid+BTIF libraries are to
122* function in DUT mode.
123*
124* To set this, the btif_init_bluetooth needs to be called with argument as 1
125*/
126static UINT8 btif_dut_mode = 0;
127
Chris Manton0eefef02014-09-08 15:01:39 -0700128static thread_t *bt_jni_workqueue_thread;
129static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue";
Adam Lesinski0620f972015-12-02 22:15:08 -0800130static uid_set_t* uid_set = NULL;
Chris Manton0eefef02014-09-08 15:01:39 -0700131
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132/************************************************************************************
133** Static functions
134************************************************************************************/
Chris Manton0eefef02014-09-08 15:01:39 -0700135static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700136static void btif_jni_disassociate();
Ajay Panicker28f294b2015-08-03 16:29:31 -0700137static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138
139/* sends message to btif task */
140static void btif_sendmsg(void *p_msg);
141
142/************************************************************************************
143** Externs
144************************************************************************************/
Chris Manton860a9af2014-08-27 10:30:47 -0700145extern fixed_queue_t *btu_hci_msg_queue;
146
The Android Open Source Project5738f832012-12-12 16:00:35 -0800147extern void bte_load_did_conf(const char *p_path);
148
149/** TODO: Move these to _common.h */
150void bte_main_boot_entry(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151void bte_main_disable(void);
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700152void bte_main_cleanup(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800153#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
154void bte_main_enable_lpm(BOOLEAN enable);
155#endif
156void bte_main_postload_cfg(void);
Jakub Pawlowski713993d2016-04-21 13:16:45 -0700157void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
158
159#ifdef __cplusplus
160}
161#endif
162
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163void btif_dm_execute_service_request(UINT16 event, char *p_param);
164#ifdef BTIF_DM_OOB_TEST
165void btif_dm_load_local_oob(void);
166#endif
167
The Android Open Source Project5738f832012-12-12 16:00:35 -0800168/*******************************************************************************
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{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700183 BTIF_TRACE_VERBOSE("btif_context_switched");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800184
Chris Manton0eefef02014-09-08 15:01:39 -0700185 tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186
187 /* each callback knows how to parse the data */
188 if (p->p_cb)
189 p->p_cb(p->event, p->p_param);
190}
191
192
193/*******************************************************************************
194**
195** Function btif_transfer_context
196**
197** Description This function switches context to btif task
198**
199** p_cback : callback used to process message in btif context
200** event : event id of message
201** p_params : parameter area passed to callback (copied)
202** param_len : length of parameter area
203** p_copy_cback : If set this function will be invoked for deep copy
204**
205** Returns void
206**
207*******************************************************************************/
208
209bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
210{
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800211 tBTIF_CONTEXT_SWITCH_CBACK *p_msg =
212 (tBTIF_CONTEXT_SWITCH_CBACK *)osi_malloc(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213
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 */
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800217 p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
218 p_msg->p_cb = p_cback;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800220 p_msg->event = event; /* callback event */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800221
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800222 /* check if caller has provided a copy callback to do the deep copy */
223 if (p_copy_cback) {
224 p_copy_cback(event, p_msg->p_param, p_params);
225 } else if (p_params) {
226 memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800227 }
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800228
229 btif_sendmsg(p_msg);
230
231 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800232}
233
234/*******************************************************************************
235**
236** Function btif_is_dut_mode
237**
238** Description checks if BTIF is currently in DUT mode
239**
240** Returns 1 if test mode, otherwize 0
241**
242*******************************************************************************/
243
244UINT8 btif_is_dut_mode(void)
245{
246 return (btif_dut_mode == 1);
247}
248
249/*******************************************************************************
250**
251** Function btif_is_enabled
252**
253** Description checks if main adapter is fully enabled
254**
255** Returns 1 if fully enabled, otherwize 0
256**
257*******************************************************************************/
258
259int btif_is_enabled(void)
260{
Zach Johnsonf450ba52014-09-18 18:46:39 -0700261 return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running()));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800262}
263
Chris Manton0eefef02014-09-08 15:01:39 -0700264void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
265 BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
266#if (BLE_INCLUDED == TRUE)
267 btif_dm_load_ble_local_keys();
268#endif
269 BTA_EnableBluetooth(bte_dm_evt);
270}
271
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272/*******************************************************************************
273**
274** Function btif_task
275**
276** Description BTIF task handler managing all messages being passed
277** Bluetooth HAL and BTA.
278**
279** Returns void
280**
281*******************************************************************************/
Zach Johnson4ada0412014-09-22 17:59:02 -0700282static void bt_jni_msg_ready(void *context) {
283 BT_HDR *p_msg = (BT_HDR *)context;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284
Chris Manton0eefef02014-09-08 15:01:39 -0700285 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286
Chris Manton0eefef02014-09-08 15:01:39 -0700287 switch (p_msg->event) {
288 case BT_EVT_CONTEXT_SWITCH_EVT:
289 btif_context_switched(p_msg);
290 break;
291 default:
292 BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
293 break;
294 }
Pavlin Radoslavovcceb4302016-02-05 13:54:43 -0800295 osi_free(p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800296}
297
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298/*******************************************************************************
299**
300** Function btif_sendmsg
301**
302** Description Sends msg to BTIF task
303**
304** Returns void
305**
306*******************************************************************************/
307
308void btif_sendmsg(void *p_msg)
309{
Zach Johnson4ada0412014-09-22 17:59:02 -0700310 thread_post(bt_jni_workqueue_thread, bt_jni_msg_ready, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800311}
312
Zach Johnsonefea7822014-09-25 11:58:50 -0700313void btif_thread_post(thread_fn func, void *context) {
314 thread_post(bt_jni_workqueue_thread, func, context);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315}
316
Ajay Panicker28f294b2015-08-03 16:29:31 -0700317static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr) {
318 char val[PROPERTY_VALUE_MAX] = {0};
319
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -0800320 if (osi_property_get(key, val, NULL)) {
Ajay Panicker28f294b2015-08-03 16:29:31 -0700321 if (string_to_bdaddr(val, addr)) {
322 BTIF_TRACE_DEBUG("%s: Got BDA %s", __func__, val);
323 return TRUE;
324 }
325 BTIF_TRACE_DEBUG("%s: System Property did not contain valid bdaddr", __func__);
326 }
327 return FALSE;
328}
329
The Android Open Source Project5738f832012-12-12 16:00:35 -0800330static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
331{
Ajay Panicker28f294b2015-08-03 16:29:31 -0700332 char val[PROPERTY_VALUE_MAX] = {0};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800333 uint8_t valid_bda = FALSE;
334 int val_size = 0;
Arman Ugurayd30195c2015-05-29 15:27:58 -0700335
The Android Open Source Project5738f832012-12-12 16:00:35 -0800336 const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
337
338 /* Get local bdaddr storage path from property */
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -0800339 if (osi_property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800340 {
341 int addr_fd;
342
Ajay Panicker28f294b2015-08-03 16:29:31 -0700343 BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800344
345 if ((addr_fd = open(val, O_RDONLY)) != -1)
346 {
347 memset(val, 0, sizeof(val));
348 read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349 /* If this is not a reserved/special bda, then use it */
Ajay Panicker28f294b2015-08-03 16:29:31 -0700350 if ((string_to_bdaddr(val, local_addr)) &&
351 (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800352 {
353 valid_bda = TRUE;
Ajay Panicker28f294b2015-08-03 16:29:31 -0700354 BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356 close(addr_fd);
357 }
358 }
359
360 if(!valid_bda)
361 {
362 val_size = sizeof(val);
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700363 if(btif_config_get_str("Adapter", "Address", val, &val_size))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364 {
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700365 string_to_bdaddr(val, local_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700366 BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800367 return;
368 }
369 }
370
371 /* No factory BDADDR found. Look for previously generated random BDA */
Ajay Panicker28f294b2015-08-03 16:29:31 -0700372 if (!valid_bda) {
373 valid_bda = btif_fetch_property(PERSIST_BDADDR_PROPERTY, local_addr);
374 }
375
376 /* No BDADDR found in file. Look for BDA in factory property */
377 if (!valid_bda) {
378 valid_bda = btif_fetch_property(FACTORY_BT_ADDR_PROPERTY, local_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800379 }
380
381 /* Generate new BDA if necessary */
382 if (!valid_bda)
383 {
384 bdstr_t bdstr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385
386 /* No autogen BDA. Generate one now. */
387 local_addr->address[0] = 0x22;
388 local_addr->address[1] = 0x22;
Marie Janssen093f0132016-03-10 11:27:53 -0800389 local_addr->address[2] = (uint8_t) osi_rand();
390 local_addr->address[3] = (uint8_t) osi_rand();
391 local_addr->address[4] = (uint8_t) osi_rand();
392 local_addr->address[5] = (uint8_t) osi_rand();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393
394 /* Convert to ascii, and store as a persistent property */
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700395 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700397 BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398 (char*)bdstr, PERSIST_BDADDR_PROPERTY);
399
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -0800400 if (osi_property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700401 BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800402 }
403
404 //save the bd address to config file
405 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700406 bdaddr_to_string(local_addr, bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800407 val_size = sizeof(val);
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700408 if (btif_config_get_str("Adapter", "Address", val, &val_size))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800409 {
410 if (strcmp(bdstr, val) ==0)
411 {
412 // BDA is already present in the config file.
413 return;
414 }
415 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700416 btif_config_set_str("Adapter", "Address", bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417}
418
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419/*******************************************************************************
420**
421** Function btif_init_bluetooth
422**
423** Description Creates BTIF task and prepares BT scheduler for startup
424**
425** Returns bt_status_t
426**
427*******************************************************************************/
Chris Manton0eefef02014-09-08 15:01:39 -0700428bt_status_t btif_init_bluetooth() {
Chris Manton0eefef02014-09-08 15:01:39 -0700429 bte_main_boot_entry();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800430
Chris Manton0eefef02014-09-08 15:01:39 -0700431 /* As part of the init, fetch the local BD ADDR */
432 memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
433 btif_fetch_local_bdaddr(&btif_local_bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434
Chris Manton0eefef02014-09-08 15:01:39 -0700435 bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME);
436 if (bt_jni_workqueue_thread == NULL) {
Marie Janssendb554582015-06-26 14:53:46 -0700437 LOG_ERROR(LOG_TAG, "%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME);
Chris Manton0eefef02014-09-08 15:01:39 -0700438 goto error_exit;
439 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440
Chris Manton0eefef02014-09-08 15:01:39 -0700441 // Associate this workqueue thread with jni.
442 btif_transfer_context(btif_jni_associate, 0, NULL, 0, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443
Chris Manton0eefef02014-09-08 15:01:39 -0700444 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800445
Chris Manton0eefef02014-09-08 15:01:39 -0700446error_exit:;
Chris Manton0eefef02014-09-08 15:01:39 -0700447 thread_free(bt_jni_workqueue_thread);
448
Zach Johnson4ada0412014-09-22 17:59:02 -0700449 bt_jni_workqueue_thread = NULL;
450
Chris Manton0eefef02014-09-08 15:01:39 -0700451 return BT_STATUS_FAIL;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800452}
453
454/*******************************************************************************
455**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456** Function btif_enable_bluetooth_evt
457**
458** Description Event indicating bluetooth enable is completed
459** Notifies HAL user with updated adapter state
460**
461** Returns void
462**
463*******************************************************************************/
464
Zach Johnson39110ec2014-10-06 13:15:00 -0700465void btif_enable_bluetooth_evt(tBTA_STATUS status)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800466{
Zach Johnson39110ec2014-10-06 13:15:00 -0700467 const controller_t *controller = controller_get_interface();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800468 bdstr_t bdstr;
Zach Johnson39110ec2014-10-06 13:15:00 -0700469 bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470
Zach Johnson39110ec2014-10-06 13:15:00 -0700471 BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status, bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800472
Zach Johnson39110ec2014-10-06 13:15:00 -0700473 if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474 {
Zach Johnson39110ec2014-10-06 13:15:00 -0700475 // TODO(zachoverflow): this whole code path seems like a bad time waiting to happen
476 // We open the vendor library using the old address.
477 bdstr_t old_address;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478 bt_property_t prop;
479
Zach Johnson39110ec2014-10-06 13:15:00 -0700480 bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address));
481
The Android Open Source Project5738f832012-12-12 16:00:35 -0800482 /**
483 * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
Zach Johnson39110ec2014-10-06 13:15:00 -0700484 * This could be because the factory BDADDR was stored separately in
The Android Open Source Project5738f832012-12-12 16:00:35 -0800485 * the Controller's non-volatile memory rather than in device's file
486 * system.
487 **/
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700488 BTIF_TRACE_WARNING("***********************************************");
Zach Johnson39110ec2014-10-06 13:15:00 -0700489 BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address);
490 BTIF_TRACE_WARNING("Controller BDA is %s", bdstr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700491 BTIF_TRACE_WARNING("***********************************************");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800492
Zach Johnson39110ec2014-10-06 13:15:00 -0700493 btif_local_bd_addr = *controller->get_address();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494
495 //save the bd address to config file
Zach Johnson39110ec2014-10-06 13:15:00 -0700496 btif_config_set_str("Adapter", "Address", bdstr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800497 btif_config_save();
498
499 //fire HAL callback for property change
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500 prop.type = BT_PROPERTY_BDADDR;
Zach Johnson39110ec2014-10-06 13:15:00 -0700501 prop.val = (void*)&btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800502 prop.len = sizeof(bt_bdaddr_t);
503 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
504 }
505
506 bte_main_postload_cfg();
507#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
508 bte_main_enable_lpm(TRUE);
509#endif
510 /* add passing up bd address as well ? */
511
512 /* callback to HAL */
513 if (status == BTA_SUCCESS)
514 {
Adam Lesinski0620f972015-12-02 22:15:08 -0800515 uid_set = uid_set_create();
516
517 btif_dm_init(uid_set);
518
The Android Open Source Project5738f832012-12-12 16:00:35 -0800519 /* init rfcomm & l2cap api */
Adam Lesinski0620f972015-12-02 22:15:08 -0800520 btif_sock_init(uid_set);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800521
522 /* init pan */
523 btif_pan_init();
524
525 /* load did configuration */
526 bte_load_did_conf(BTE_DID_CONF_FILE);
527
528#ifdef BTIF_DM_OOB_TEST
529 btif_dm_load_local_oob();
530#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800531
Zach Johnson8ea81882014-09-18 18:13:13 -0700532 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533 }
534 else
535 {
536 /* cleanup rfcomm & l2cap api */
537 btif_sock_cleanup();
538
539 btif_pan_cleanup();
540
Zach Johnson8ea81882014-09-18 18:13:13 -0700541 future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800542 }
543}
544
545/*******************************************************************************
546**
547** Function btif_disable_bluetooth
548**
549** Description Inititates shutdown of Bluetooth system.
550** Any active links will be dropped and device entering
551** non connectable/discoverable mode
552**
553** Returns void
554**
555*******************************************************************************/
556bt_status_t btif_disable_bluetooth(void)
557{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700558 BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559
560 btif_dm_on_disable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561 /* cleanup rfcomm & l2cap api */
562 btif_sock_cleanup();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 btif_pan_cleanup();
Zach Johnsonf450ba52014-09-18 18:46:39 -0700564 BTA_DisableBluetooth();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
The Android Open Source Project5738f832012-12-12 16:00:35 -0800566 return BT_STATUS_SUCCESS;
567}
568
569/*******************************************************************************
570**
571** Function btif_disable_bluetooth_evt
572**
573** Description Event notifying BT disable is now complete.
574** Terminates main stack tasks and notifies HAL
575** user with updated BT state.
576**
577** Returns void
578**
579*******************************************************************************/
580
581void btif_disable_bluetooth_evt(void)
582{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700583 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800584
585#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
586 bte_main_enable_lpm(FALSE);
587#endif
588
Satya Callojid5aa2472014-09-23 18:27:09 -0700589 bte_main_disable();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800590
The Android Open Source Project5738f832012-12-12 16:00:35 -0800591 /* callback to HAL */
Zach Johnson1924dac2014-09-24 16:01:29 -0700592 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800593}
594
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595/*******************************************************************************
596**
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700597** Function btif_cleanup_bluetooth
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598**
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700599** Description Cleanup BTIF state.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600**
601** Returns void
602**
603*******************************************************************************/
604
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700605bt_status_t btif_cleanup_bluetooth(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700607 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800608
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700609#if (BLE_INCLUDED == TRUE)
610 BTA_VendorCleanup();
611#endif
612
Pavlin Radoslavovfb5a3822016-03-25 18:55:30 -0700613 btif_dm_cleanup();
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700614 btif_jni_disassociate();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800615 btif_queue_release();
Chris Manton0eefef02014-09-08 15:01:39 -0700616
Chris Manton0eefef02014-09-08 15:01:39 -0700617 thread_free(bt_jni_workqueue_thread);
618 bt_jni_workqueue_thread = NULL;
619
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -0700620 bte_main_cleanup();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800621
622 btif_dut_mode = 0;
623
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700624 BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800625
626 return BT_STATUS_SUCCESS;
627}
628
The Android Open Source Project5738f832012-12-12 16:00:35 -0800629/*******************************************************************************
630**
631** Function btif_dut_mode_cback
632**
633** Description Callback invoked on completion of vendor specific test mode command
634**
635** Returns None
636**
637*******************************************************************************/
638static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
639{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800640 UNUSED(p);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641 /* For now nothing to be done. */
642}
643
644/*******************************************************************************
645**
646** Function btif_dut_mode_configure
647**
648** Description Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
649** test mode
650**
651** Returns BT_STATUS_SUCCESS on success
652**
653*******************************************************************************/
654bt_status_t btif_dut_mode_configure(uint8_t enable)
655{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700656 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800657
Zach Johnsonf450ba52014-09-18 18:46:39 -0700658 if (!stack_manager_get_interface()->get_stack_is_running()) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700659 BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800660 return BT_STATUS_NOT_READY;
661 }
662
663 btif_dut_mode = enable;
664 if (enable == 1) {
665 BTA_EnableTestMode();
666 } else {
667 BTA_DisableTestMode();
668 }
669 return BT_STATUS_SUCCESS;
670}
671
672/*******************************************************************************
673**
674** Function btif_dut_mode_send
675**
676** Description Sends a HCI Vendor specific command to the controller
677**
678** Returns BT_STATUS_SUCCESS on success
679**
680*******************************************************************************/
681bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
682{
683 /* TODO: Check that opcode is a vendor command group */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700684 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800685 if (!btif_is_dut_mode()) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700686 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 -0800687 return BT_STATUS_FAIL;
688 }
689 BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
690 return BT_STATUS_SUCCESS;
691}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800692
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693/*****************************************************************************
694**
695** btif api adapter property functions
696**
697*****************************************************************************/
698
699static bt_status_t btif_in_get_adapter_properties(void)
700{
701 bt_property_t properties[6];
702 uint32_t num_props;
703
704 bt_bdaddr_t addr;
705 bt_bdname_t name;
706 bt_scan_mode_t mode;
707 uint32_t disc_timeout;
708 bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
709 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
710 num_props = 0;
711
712 /* BD_ADDR */
713 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
714 sizeof(addr), &addr);
715 btif_storage_get_adapter_property(&properties[num_props]);
716 num_props++;
717
718 /* BD_NAME */
719 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
720 sizeof(name), &name);
721 btif_storage_get_adapter_property(&properties[num_props]);
722 num_props++;
723
724 /* SCAN_MODE */
725 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
726 sizeof(mode), &mode);
727 btif_storage_get_adapter_property(&properties[num_props]);
728 num_props++;
729
730 /* DISC_TIMEOUT */
731 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
732 sizeof(disc_timeout), &disc_timeout);
733 btif_storage_get_adapter_property(&properties[num_props]);
734 num_props++;
735
736 /* BONDED_DEVICES */
737 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
738 sizeof(bonded_devices), bonded_devices);
739 btif_storage_get_adapter_property(&properties[num_props]);
740 num_props++;
741
742 /* LOCAL UUIDs */
743 BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
744 sizeof(local_uuids), local_uuids);
745 btif_storage_get_adapter_property(&properties[num_props]);
746 num_props++;
747
748 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
749 BT_STATUS_SUCCESS, num_props, properties);
750
751 return BT_STATUS_SUCCESS;
752}
753
754static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
755{
756 bt_property_t remote_properties[8];
757 uint32_t num_props = 0;
758
759 bt_bdname_t name, alias;
760 uint32_t cod, devtype;
761 bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
762
763 memset(remote_properties, 0, sizeof(remote_properties));
764 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
765 sizeof(name), &name);
766 btif_storage_get_remote_device_property(bd_addr,
767 &remote_properties[num_props]);
768 num_props++;
769
770 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
771 sizeof(alias), &alias);
772 btif_storage_get_remote_device_property(bd_addr,
773 &remote_properties[num_props]);
774 num_props++;
775
776 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
777 sizeof(cod), &cod);
778 btif_storage_get_remote_device_property(bd_addr,
779 &remote_properties[num_props]);
780 num_props++;
781
782 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
783 sizeof(devtype), &devtype);
784 btif_storage_get_remote_device_property(bd_addr,
785 &remote_properties[num_props]);
786 num_props++;
787
788 BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
789 sizeof(remote_uuids), remote_uuids);
790 btif_storage_get_remote_device_property(bd_addr,
791 &remote_properties[num_props]);
792 num_props++;
793
794 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
795 BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
796
797 return BT_STATUS_SUCCESS;
798}
799
800
801/*******************************************************************************
802**
803** Function execute_storage_request
804**
805** Description Executes adapter storage request in BTIF context
806**
807** Returns bt_status_t
808**
809*******************************************************************************/
810
811static void execute_storage_request(UINT16 event, char *p_param)
812{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813 bt_status_t status = BT_STATUS_SUCCESS;
814
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700815 BTIF_TRACE_EVENT("execute storage request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816
817 switch(event)
818 {
819 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
820 {
821 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
822 bt_property_t *p_prop = &(p_req->write_req.prop);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700823 BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800824 p_prop->len, p_prop->val);
825
826 status = btif_storage_set_adapter_property(p_prop);
827 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
828 } break;
829
830 case BTIF_CORE_STORAGE_ADAPTER_READ:
831 {
832 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
833 char buf[512];
834 bt_property_t prop;
835 prop.type = p_req->read_req.type;
836 prop.val = (void*)buf;
837 prop.len = sizeof(buf);
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700838 if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
839 {
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -0700840 #if (BLE_INCLUDED == TRUE)
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700841 tBTM_BLE_VSC_CB cmn_vsc_cb;
842 bt_local_le_features_t local_le_features;
843
844 /* LE features are not stored in storage. Should be retrived from stack */
845 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
846 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
847
848 prop.len = sizeof (bt_local_le_features_t);
849 if (cmn_vsc_cb.filter_support == 1)
850 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
851 else
852 local_le_features.max_adv_filter_supported = 0;
853 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
854 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
855 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojif5387072015-02-09 17:40:52 -0800856 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
Satya Callojie5ba8842014-07-03 17:18:02 -0700857 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Satya Callojif5387072015-02-09 17:40:52 -0800858 local_le_features.version_supported = cmn_vsc_cb.version_supported;
859 local_le_features.total_trackable_advertisers =
860 cmn_vsc_cb.total_trackable_advertisers;
Andre Eisenbach2af0bb62015-05-21 17:07:21 -0700861
862 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
863 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700864 memcpy(prop.val, &local_le_features, prop.len);
Prerepa Viswanadham0c4ec0d2014-06-10 21:04:43 -0700865 #endif
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -0700866 }
867 else
868 {
869 status = btif_storage_get_adapter_property(&prop);
870 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
872 } break;
873
874 case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
875 {
876 status = btif_in_get_adapter_properties();
877 } break;
878
879 case BTIF_CORE_STORAGE_NOTIFY_STATUS:
880 {
881 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
882 } break;
883
884 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700885 BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886 break;
887 }
888}
889
890static void execute_storage_remote_request(UINT16 event, char *p_param)
891{
892 bt_status_t status = BT_STATUS_FAIL;
893 bt_property_t prop;
894
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700895 BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800896
897 switch (event)
898 {
899 case BTIF_CORE_STORAGE_REMOTE_READ:
900 {
901 char buf[1024];
902 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
903 prop.type = p_req->read_req.type;
904 prop.val = (void*) buf;
905 prop.len = sizeof(buf);
906
907 status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
908 &prop);
909 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
910 status, &(p_req->read_req.bd_addr), 1, &prop);
911 }break;
912 case BTIF_CORE_STORAGE_REMOTE_WRITE:
913 {
914 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
915 status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
916 &(p_req->write_req.prop));
917 }break;
918 case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
919 {
920 btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
921 btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
922 }break;
923 }
924}
925
926void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
927 bt_property_t *p_props)
928{
929 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
930 status, num_props, p_props);
931
932}
933void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
934 uint32_t num_props, bt_property_t *p_props)
935{
936 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
937 status, remote_addr, num_props, p_props);
938}
939
940/*******************************************************************************
941**
942** Function btif_in_storage_request_copy_cb
943**
944** Description Switch context callback function to perform the deep copy for
945** both the adapter and remote_device property API
946**
947** Returns None
948**
949*******************************************************************************/
950static void btif_in_storage_request_copy_cb(UINT16 event,
951 char *p_new_buf, char *p_old_buf)
952{
953 btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
954 btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
955
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700956 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800957 switch (event)
958 {
959 case BTIF_CORE_STORAGE_REMOTE_WRITE:
960 case BTIF_CORE_STORAGE_ADAPTER_WRITE:
961 {
962 bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
963 /* Copy the member variables one at a time */
964 new_req->write_req.prop.type = old_req->write_req.prop.type;
965 new_req->write_req.prop.len = old_req->write_req.prop.len;
966
967 new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
968 memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
969 old_req->write_req.prop.len);
970 }break;
971 }
972}
973
974/*******************************************************************************
975**
976** Function btif_get_adapter_properties
977**
978** Description Fetch all available properties (local & remote)
979**
980** Returns bt_status_t
981**
982*******************************************************************************/
983
984bt_status_t btif_get_adapter_properties(void)
985{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700986 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
988 if (!btif_is_enabled())
989 return BT_STATUS_NOT_READY;
990
991 return btif_transfer_context(execute_storage_request,
992 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
993 NULL, 0, NULL);
994}
995
996/*******************************************************************************
997**
998** Function btif_get_adapter_property
999**
1000** Description Fetches property value from local cache
1001**
1002** Returns bt_status_t
1003**
1004*******************************************************************************/
1005
1006bt_status_t btif_get_adapter_property(bt_property_type_t type)
1007{
1008 btif_storage_req_t req;
1009
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001010 BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011
1012 /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1013 if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1014 return BT_STATUS_NOT_READY;
1015
1016 memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1017 req.read_req.type = type;
1018
1019 return btif_transfer_context(execute_storage_request,
1020 BTIF_CORE_STORAGE_ADAPTER_READ,
1021 (char*)&req, sizeof(btif_storage_req_t), NULL);
1022}
1023
1024/*******************************************************************************
1025**
1026** Function btif_set_adapter_property
1027**
1028** Description Updates core stack with property value and stores it in
1029** local cache
1030**
1031** Returns bt_status_t
1032**
1033*******************************************************************************/
1034
1035bt_status_t btif_set_adapter_property(const bt_property_t *property)
1036{
1037 btif_storage_req_t req;
1038 bt_status_t status = BT_STATUS_SUCCESS;
1039 int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1040 char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1041 UINT16 name_len = 0;
1042
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001043 BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001044 property->type, property->len, property->val);
1045
1046 if (!btif_is_enabled())
1047 return BT_STATUS_NOT_READY;
1048
1049 switch(property->type)
1050 {
1051 case BT_PROPERTY_BDNAME:
1052 {
1053 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1054 property->len;
1055 memcpy(bd_name,property->val, name_len);
1056 bd_name[name_len] = '\0';
1057
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001058 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001059
1060 BTA_DmSetDeviceName((char *)bd_name);
1061
1062 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1063 }
1064 break;
1065
1066 case BT_PROPERTY_ADAPTER_SCAN_MODE:
1067 {
1068 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1069 tBTA_DM_DISC disc_mode;
1070 tBTA_DM_CONN conn_mode;
1071
1072 switch(mode)
1073 {
1074 case BT_SCAN_MODE_NONE:
1075 disc_mode = BTA_DM_NON_DISC;
1076 conn_mode = BTA_DM_NON_CONN;
1077 break;
1078
1079 case BT_SCAN_MODE_CONNECTABLE:
1080 disc_mode = BTA_DM_NON_DISC;
1081 conn_mode = BTA_DM_CONN;
1082 break;
1083
1084 case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1085 disc_mode = BTA_DM_GENERAL_DISC;
1086 conn_mode = BTA_DM_CONN;
1087 break;
1088
1089 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001090 BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001091 return BT_STATUS_PARM_INVALID;
1092 }
1093
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001094 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001095
1096 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1097
1098 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1099 }
1100 break;
1101 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1102 {
1103 /* Nothing to do beside store the value in NV. Java
1104 will change the SCAN_MODE property after setting timeout,
1105 if required */
1106 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1107 }
1108 break;
1109 case BT_PROPERTY_BDADDR:
1110 case BT_PROPERTY_UUIDS:
1111 case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1112 case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1113 /* no write support through HAL, these properties are only populated from BTA events */
1114 status = BT_STATUS_FAIL;
1115 break;
1116 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001117 BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118 property->type);
1119 status = BT_STATUS_FAIL;
1120 break;
1121 }
1122
1123 if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1124 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001125 /* pass on to storage for updating local database */
1126
1127 memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1128 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1129
1130 return btif_transfer_context(execute_storage_request,
1131 storage_req_id,
1132 (char*)&req,
1133 sizeof(btif_storage_req_t)+property->len,
1134 btif_in_storage_request_copy_cb);
1135 }
1136
1137 return status;
1138
1139}
1140
1141/*******************************************************************************
1142**
1143** Function btif_get_remote_device_property
1144**
1145** Description Fetches the remote device property from the NVRAM
1146**
1147** Returns bt_status_t
1148**
1149*******************************************************************************/
1150bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1151 bt_property_type_t type)
1152{
1153 btif_storage_req_t req;
1154
1155 if (!btif_is_enabled())
1156 return BT_STATUS_NOT_READY;
1157
1158 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1159 req.read_req.type = type;
1160 return btif_transfer_context(execute_storage_remote_request,
1161 BTIF_CORE_STORAGE_REMOTE_READ,
1162 (char*)&req, sizeof(btif_storage_req_t),
1163 NULL);
1164}
1165
1166/*******************************************************************************
1167**
1168** Function btif_get_remote_device_properties
1169**
1170** Description Fetches all the remote device properties from NVRAM
1171**
1172** Returns bt_status_t
1173**
1174*******************************************************************************/
1175bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1176{
1177 btif_storage_req_t req;
1178
1179 if (!btif_is_enabled())
1180 return BT_STATUS_NOT_READY;
1181
1182 memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1183 return btif_transfer_context(execute_storage_remote_request,
1184 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1185 (char*)&req, sizeof(btif_storage_req_t),
1186 NULL);
1187}
1188
1189/*******************************************************************************
1190**
1191** Function btif_set_remote_device_property
1192**
1193** Description Writes the remote device property to NVRAM.
1194** Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1195** remote device property that can be set
1196**
1197** Returns bt_status_t
1198**
1199*******************************************************************************/
1200bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1201 const bt_property_t *property)
1202{
1203 btif_storage_req_t req;
1204
1205 if (!btif_is_enabled())
1206 return BT_STATUS_NOT_READY;
1207
1208 memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1209 memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1210
1211 return btif_transfer_context(execute_storage_remote_request,
1212 BTIF_CORE_STORAGE_REMOTE_WRITE,
1213 (char*)&req,
1214 sizeof(btif_storage_req_t)+property->len,
1215 btif_in_storage_request_copy_cb);
1216}
1217
1218
1219/*******************************************************************************
1220**
1221** Function btif_get_remote_service_record
1222**
1223** Description Looks up the service matching uuid on the remote device
1224** and fetches the SCN and service_name if the UUID is found
1225**
1226** Returns bt_status_t
1227**
1228*******************************************************************************/
1229bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1230 bt_uuid_t *uuid)
1231{
1232 if (!btif_is_enabled())
1233 return BT_STATUS_NOT_READY;
1234
1235 return btif_dm_get_remote_service_record(remote_addr, uuid);
1236}
1237
1238
1239/*******************************************************************************
1240**
1241** Function btif_get_enabled_services_mask
1242**
1243** Description Fetches currently enabled services
1244**
1245** Returns tBTA_SERVICE_MASK
1246**
1247*******************************************************************************/
1248
1249tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1250{
1251 return btif_enabled_services;
1252}
1253
1254/*******************************************************************************
1255**
1256** Function btif_enable_service
1257**
1258** Description Enables the service 'service_ID' to the service_mask.
1259** Upon BT enable, BTIF core shall invoke the BTA APIs to
1260** enable the profiles
1261**
1262** Returns bt_status_t
1263**
1264*******************************************************************************/
1265bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1266{
1267 tBTA_SERVICE_ID *p_id = &service_id;
1268
1269 /* If BT is enabled, we need to switch to BTIF context and trigger the
1270 * enable for that profile
1271 *
1272 * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1273 * enable for the profiles that have been enabled */
1274
1275 btif_enabled_services |= (1 << service_id);
1276
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001277 BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001278
1279 if (btif_is_enabled())
1280 {
1281 btif_transfer_context(btif_dm_execute_service_request,
1282 BTIF_DM_ENABLE_SERVICE,
1283 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1284 }
1285
1286 return BT_STATUS_SUCCESS;
1287}
1288/*******************************************************************************
1289**
1290** Function btif_disable_service
1291**
1292** Description Disables the service 'service_ID' to the service_mask.
1293** Upon BT disable, BTIF core shall invoke the BTA APIs to
1294** disable the profiles
1295**
1296** Returns bt_status_t
1297**
1298*******************************************************************************/
1299bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1300{
1301 tBTA_SERVICE_ID *p_id = &service_id;
1302
1303 /* If BT is enabled, we need to switch to BTIF context and trigger the
1304 * disable for that profile so that the appropriate uuid_property_changed will
1305 * be triggerred. Otherwise, we just need to clear the service_id in the mask
1306 */
1307
1308 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
1309
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001310 BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001311
1312 if (btif_is_enabled())
1313 {
1314 btif_transfer_context(btif_dm_execute_service_request,
1315 BTIF_DM_DISABLE_SERVICE,
1316 (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1317 }
1318
1319 return BT_STATUS_SUCCESS;
1320}
Zhihai Xubad70b12013-06-04 18:21:25 -07001321
Chris Manton0eefef02014-09-08 15:01:39 -07001322static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
1323 BTIF_TRACE_DEBUG("%s Associating thread to JVM", __func__);
1324 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
Zhihai Xubad70b12013-06-04 18:21:25 -07001325}
Chris Manton0eefef02014-09-08 15:01:39 -07001326
Pavlin Radoslavov1bd691c2016-04-26 12:18:36 -07001327static void btif_jni_disassociate() {
Zach Johnson8ea81882014-09-18 18:13:13 -07001328 BTIF_TRACE_DEBUG("%s Disassociating thread from JVM", __func__);
1329 HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
1330 bt_hal_cbacks = NULL;
Chris Manton0eefef02014-09-08 15:01:39 -07001331}