The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Hemant Gupta | 2dc9999 | 2014-04-18 12:54:08 +0530 | [diff] [blame] | 3 | * Copyright (C) 2014 The Android Open Source Project |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 4 | * 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 29 | #include <ctype.h> |
| 30 | #include <cutils/properties.h> |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 31 | #include <dirent.h> |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 32 | #include <errno.h> |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 39 | |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 40 | #define LOG_TAG "bt_btif_core" |
Sharvil Nanavati | 95b74f2 | 2015-03-12 15:55:21 -0700 | [diff] [blame] | 41 | #include "btcore/include/bdaddr.h" |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 42 | |
| 43 | #include "bdaddr.h" |
Zhihai Xu | 6c65c2f | 2013-11-25 17:30:59 -0800 | [diff] [blame] | 44 | #include "bt_utils.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | #include "bta_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 46 | #include "bte.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 47 | #include "btif_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 48 | #include "btif_av.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 49 | #include "btif_config.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | #include "btif_pan.h" |
| 51 | #include "btif_profile_queue.h" |
| 52 | #include "btif_config.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 53 | #include "btif_sock.h" |
| 54 | #include "btif_storage.h" |
| 55 | #include "btif_util.h" |
| 56 | #include "btu.h" |
Chris Manton | 79ecab5 | 2014-10-31 14:54:51 -0700 | [diff] [blame] | 57 | #include "device/include/controller.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 58 | #include "osi/include/fixed_queue.h" |
| 59 | #include "osi/include/future.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 60 | #include "gki.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 61 | #include "osi/include/osi.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 62 | #include "osi/include/log.h" |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 63 | #include "stack_manager.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 64 | #include "osi/include/thread.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 65 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 66 | /************************************************************************************ |
| 67 | ** Constants & Macros |
| 68 | ************************************************************************************/ |
| 69 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 70 | #ifndef BTE_DID_CONF_FILE |
| 71 | #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf" |
| 72 | #endif |
| 73 | |
Srinu Jella | 6de08d6 | 2015-10-07 18:01:53 +0530 | [diff] [blame] | 74 | #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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | /************************************************************************************ |
| 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 | |
| 87 | typedef struct { |
| 88 | bt_bdaddr_t bd_addr; |
| 89 | bt_property_type_t type; |
| 90 | } btif_storage_read_t; |
| 91 | |
| 92 | typedef struct { |
| 93 | bt_bdaddr_t bd_addr; |
| 94 | bt_property_t prop; |
| 95 | } btif_storage_write_t; |
| 96 | |
| 97 | typedef union { |
| 98 | btif_storage_read_t read_req; |
| 99 | btif_storage_write_t write_req; |
| 100 | } btif_storage_req_t; |
| 101 | |
| 102 | typedef 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 | |
| 113 | bt_bdaddr_t btif_local_bd_addr; |
| 114 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 115 | static 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 | */ |
| 123 | static UINT8 btif_dut_mode = 0; |
| 124 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 125 | static thread_t *bt_jni_workqueue_thread; |
| 126 | static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue"; |
| 127 | |
Kiran Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 128 | static BOOLEAN ssr_triggered = FALSE; |
| 129 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 130 | /************************************************************************************ |
| 131 | ** Static functions |
| 132 | ************************************************************************************/ |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 133 | static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param); |
| 134 | static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param); |
Ajay Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 135 | static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 136 | |
| 137 | /* sends message to btif task */ |
| 138 | static void btif_sendmsg(void *p_msg); |
| 139 | |
| 140 | /************************************************************************************ |
| 141 | ** Externs |
| 142 | ************************************************************************************/ |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 143 | extern fixed_queue_t *btu_hci_msg_queue; |
| 144 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 145 | extern void bte_load_did_conf(const char *p_path); |
| 146 | |
| 147 | /** TODO: Move these to _common.h */ |
| 148 | void bte_main_boot_entry(void); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 149 | void bte_main_disable(void); |
| 150 | void bte_main_shutdown(void); |
| 151 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 152 | void bte_main_enable_lpm(BOOLEAN enable); |
| 153 | #endif |
| 154 | void bte_main_postload_cfg(void); |
| 155 | void btif_dm_execute_service_request(UINT16 event, char *p_param); |
| 156 | #ifdef BTIF_DM_OOB_TEST |
| 157 | void btif_dm_load_local_oob(void); |
| 158 | #endif |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 159 | void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 160 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 161 | /******************************************************************************* |
| 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 | |
| 173 | static void btif_context_switched(void *p_msg) |
| 174 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 175 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 176 | BTIF_TRACE_VERBOSE("btif_context_switched"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 177 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 178 | tBTIF_CONTEXT_SWITCH_CBACK *p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 179 | |
| 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 | |
| 202 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 206 | BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 207 | |
| 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 | |
| 246 | UINT8 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 | |
| 261 | int btif_is_enabled(void) |
| 262 | { |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 263 | return ((!btif_is_dut_mode()) && (stack_manager_get_interface()->get_stack_is_running())); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 266 | void 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 | |
| 274 | void 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 Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 280 | |
| 281 | future_ready(stack_manager_get_hack_future(), FUTURE_FAIL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 282 | } |
| 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 Johnson | 4ada041 | 2014-09-22 17:59:02 -0700 | [diff] [blame] | 294 | static void bt_jni_msg_ready(void *context) { |
| 295 | BT_HDR *p_msg = (BT_HDR *)context; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 296 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 297 | BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 298 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 299 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 308 | } |
| 309 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 310 | /******************************************************************************* |
| 311 | ** |
| 312 | ** Function btif_sendmsg |
| 313 | ** |
| 314 | ** Description Sends msg to BTIF task |
| 315 | ** |
| 316 | ** Returns void |
| 317 | ** |
| 318 | *******************************************************************************/ |
| 319 | |
| 320 | void btif_sendmsg(void *p_msg) |
| 321 | { |
Pradeep Panigrahi | 5b1ce47 | 2016-01-22 14:30:26 +0530 | [diff] [blame] | 322 | if(bt_jni_workqueue_thread != NULL) |
| 323 | thread_post(bt_jni_workqueue_thread, bt_jni_msg_ready, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Zach Johnson | efea782 | 2014-09-25 11:58:50 -0700 | [diff] [blame] | 326 | void btif_thread_post(thread_fn func, void *context) { |
Pradeep Panigrahi | 5b1ce47 | 2016-01-22 14:30:26 +0530 | [diff] [blame] | 327 | if(bt_jni_workqueue_thread != NULL) |
| 328 | thread_post(bt_jni_workqueue_thread, func, context); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Srinu Jella | 6de08d6 | 2015-10-07 18:01:53 +0530 | [diff] [blame] | 331 | static 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 Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 385 | static 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 Jella | 6de08d6 | 2015-10-07 18:01:53 +0530 | [diff] [blame] | 397 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 398 | static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr) |
| 399 | { |
Ajay Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 400 | char val[PROPERTY_VALUE_MAX] = {0}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 401 | 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 Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 410 | BTIF_TRACE_DEBUG("%s, local bdaddr is stored in %s", __func__, val); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 411 | |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 412 | if ((addr_fd = TEMP_FAILURE_RETRY(open(val, O_RDONLY))) != -1) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 413 | { |
| 414 | memset(val, 0, sizeof(val)); |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 415 | TEMP_FAILURE_RETRY(read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 416 | /* If this is not a reserved/special bda, then use it */ |
Ajay Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 417 | if ((string_to_bdaddr(val, local_addr)) && |
| 418 | (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 419 | { |
| 420 | valid_bda = TRUE; |
Ajay Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 421 | BTIF_TRACE_DEBUG("%s: Got Factory BDA %s", __func__, val); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 422 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 423 | close(addr_fd); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if(!valid_bda) |
| 428 | { |
| 429 | val_size = sizeof(val); |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 430 | if(btif_config_get_str("Adapter", "Address", val, &val_size)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 431 | { |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 432 | string_to_bdaddr(val, local_addr); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 433 | BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is %s", val); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* No factory BDADDR found. Look for previously generated random BDA */ |
Ajay Panicker | c1553d3 | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 439 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Srinu Jella | 6de08d6 | 2015-10-07 18:01:53 +0530 | [diff] [blame] | 448 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 468 | /* 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 Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 484 | bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 485 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 486 | BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 487 | (char*)bdstr, PERSIST_BDADDR_PROPERTY); |
| 488 | |
| 489 | if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 490 | BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | //save the bd address to config file |
| 494 | bdstr_t bdstr; |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 495 | bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 496 | val_size = sizeof(val); |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 497 | if (btif_config_get_str("Adapter", "Address", val, &val_size)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 498 | { |
| 499 | if (strcmp(bdstr, val) ==0) |
| 500 | { |
| 501 | // BDA is already present in the config file. |
| 502 | return; |
| 503 | } |
| 504 | } |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 505 | btif_config_set_str("Adapter", "Address", bdstr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 506 | } |
| 507 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 508 | /******************************************************************************* |
| 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 Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 517 | bt_status_t btif_init_bluetooth() { |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 518 | bte_main_boot_entry(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 519 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 520 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 523 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 524 | bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME); |
| 525 | if (bt_jni_workqueue_thread == NULL) { |
Sharvil Nanavati | 364a1e7 | 2014-12-25 22:17:09 -0800 | [diff] [blame] | 526 | LOG_ERROR("%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME); |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 527 | goto error_exit; |
| 528 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 529 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 530 | // Associate this workqueue thread with jni. |
| 531 | btif_transfer_context(btif_jni_associate, 0, NULL, 0, NULL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 532 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 533 | return BT_STATUS_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 534 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 535 | error_exit:; |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 536 | thread_free(bt_jni_workqueue_thread); |
| 537 | |
Zach Johnson | 4ada041 | 2014-09-22 17:59:02 -0700 | [diff] [blame] | 538 | bt_jni_workqueue_thread = NULL; |
| 539 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 540 | return BT_STATUS_FAIL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /******************************************************************************* |
| 544 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 545 | ** 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 Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 554 | void btif_enable_bluetooth_evt(tBTA_STATUS status) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 555 | { |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 556 | const controller_t *controller = controller_get_interface(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 557 | bdstr_t bdstr; |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 558 | bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 559 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 560 | BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status, bdstr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 561 | |
Kiran Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 562 | ssr_triggered = FALSE; |
| 563 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 564 | if (bdcmp(btif_local_bd_addr.address, controller->get_address()->address)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 565 | { |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 566 | // 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 569 | bt_property_t prop; |
| 570 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 571 | bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address)); |
| 572 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 573 | /** |
| 574 | * The Controller's BDADDR does not match to the BTIF's initial BDADDR! |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 575 | * This could be because the factory BDADDR was stored separately in |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 576 | * the Controller's non-volatile memory rather than in device's file |
| 577 | * system. |
| 578 | **/ |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 579 | BTIF_TRACE_WARNING("***********************************************"); |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 580 | BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address); |
| 581 | BTIF_TRACE_WARNING("Controller BDA is %s", bdstr); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 582 | BTIF_TRACE_WARNING("***********************************************"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 583 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 584 | btif_local_bd_addr = *controller->get_address(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 585 | |
| 586 | //save the bd address to config file |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 587 | btif_config_set_str("Adapter", "Address", bdstr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 588 | btif_config_save(); |
| 589 | |
| 590 | //fire HAL callback for property change |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 591 | prop.type = BT_PROPERTY_BDADDR; |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 592 | prop.val = (void*)&btif_local_bd_addr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 593 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 606 | /* init rfcomm & l2cap api */ |
| 607 | btif_sock_init(); |
| 608 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 609 | /* load did configuration */ |
| 610 | bte_load_did_conf(BTE_DID_CONF_FILE); |
| 611 | |
venkata Jagadeesh | 2626f0a | 2015-06-16 15:29:04 +0530 | [diff] [blame] | 612 | /* init pan */ |
| 613 | btif_pan_init(); |
| 614 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 615 | #ifdef BTIF_DM_OOB_TEST |
| 616 | btif_dm_load_local_oob(); |
| 617 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 618 | |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 619 | future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 620 | } |
| 621 | else |
| 622 | { |
| 623 | /* cleanup rfcomm & l2cap api */ |
| 624 | btif_sock_cleanup(); |
| 625 | |
| 626 | btif_pan_cleanup(); |
| 627 | |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 628 | future_ready(stack_manager_get_hack_future(), FUTURE_FAIL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 629 | } |
| 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 | *******************************************************************************/ |
| 643 | bt_status_t btif_disable_bluetooth(void) |
| 644 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 645 | BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 646 | |
| 647 | btif_dm_on_disable(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 648 | /* cleanup rfcomm & l2cap api */ |
| 649 | btif_sock_cleanup(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 650 | btif_pan_cleanup(); |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 651 | BTA_DisableBluetooth(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 652 | |
Subramanian Srinivasan | 9233de6 | 2016-02-02 11:46:47 -0800 | [diff] [blame] | 653 | #if (BLE_INCLUDED == TRUE) |
| 654 | BTA_VendorCleanup(); |
| 655 | BTA_StopBleTimers(); |
| 656 | #endif |
| 657 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 658 | 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 | |
| 673 | void btif_disable_bluetooth_evt(void) |
| 674 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 675 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
Kiran Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 676 | if (ssr_triggered == TRUE) |
| 677 | { |
| 678 | BTIF_TRACE_DEBUG("%s SSR triggered,Ignore EVT",__FUNCTION__); |
| 679 | return; |
| 680 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 681 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 682 | bte_main_enable_lpm(FALSE); |
| 683 | #endif |
| 684 | |
Satya Calloji | d5aa247 | 2014-09-23 18:27:09 -0700 | [diff] [blame] | 685 | bte_main_disable(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 686 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 687 | /* callback to HAL */ |
Zach Johnson | 1924dac | 2014-09-24 16:01:29 -0700 | [diff] [blame] | 688 | future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 689 | } |
| 690 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 691 | /******************************************************************************* |
| 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 | |
| 702 | bt_status_t btif_shutdown_bluetooth(void) |
| 703 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 704 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 705 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 706 | btif_transfer_context(btif_jni_disassociate, 0, NULL, 0, NULL); |
Zhihai Xu | 6c65c2f | 2013-11-25 17:30:59 -0800 | [diff] [blame] | 707 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 708 | btif_queue_release(); |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 709 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 710 | thread_free(bt_jni_workqueue_thread); |
| 711 | bt_jni_workqueue_thread = NULL; |
| 712 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 713 | bte_main_shutdown(); |
| 714 | |
| 715 | btif_dut_mode = 0; |
| 716 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 717 | BTIF_TRACE_DEBUG("%s done", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 718 | |
| 719 | return BT_STATUS_SUCCESS; |
| 720 | } |
| 721 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 722 | /******************************************************************************* |
Kiran Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 723 | Function btif_ssrcleanup |
| 724 | Description Trigger SSR when Disable timeout occured |
| 725 | |
| 726 | *******************************************************************************/ |
| 727 | void btif_ssr_cleanup(void) |
| 728 | { |
| 729 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
| 730 | ssr_triggered = TRUE; |
pramod kotreshappa | 84f60eb | 2015-09-10 14:33:09 -0700 | [diff] [blame] | 731 | bte_ssr_cleanup(0x11);//SSR reason 0x11 - ENABLE_TIMEOUT |
Kiran Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Srinu Jella | 51ca16f | 2013-12-06 15:25:26 +0530 | [diff] [blame] | 734 | /**************************************************************************** |
| 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 | *******************************************************************************/ |
| 749 | static void btif_hci_event_cback ( tBTM_RAW_CMPL *p ) |
| 750 | { |
| 751 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
Balraj Selvaraj | f35ca6a | 2015-10-21 19:44:04 +0530 | [diff] [blame] | 752 | if((p != NULL) && (bt_hal_cbacks != NULL) |
| 753 | && (bt_hal_cbacks->hci_event_recv_cb != NULL)) |
Srinu Jella | 51ca16f | 2013-12-06 15:25:26 +0530 | [diff] [blame] | 754 | { |
| 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 | *******************************************************************************/ |
| 769 | bt_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 Kelageri | 4a924fd | 2015-08-04 15:46:33 -0700 | [diff] [blame] | 778 | /******************************************************************************* |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 779 | ** |
| 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 | *******************************************************************************/ |
| 787 | static void btif_dut_mode_cback( tBTM_VSC_CMPL *p ) |
| 788 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 789 | UNUSED(p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 790 | /* 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 | *******************************************************************************/ |
| 803 | bt_status_t btif_dut_mode_configure(uint8_t enable) |
| 804 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 805 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 806 | |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 807 | if (!stack_manager_get_interface()->get_stack_is_running()) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 808 | BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 809 | 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 | *******************************************************************************/ |
| 830 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 833 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 834 | if (!btif_is_dut_mode()) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 835 | BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1."); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 836 | return BT_STATUS_FAIL; |
| 837 | } |
| 838 | BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback); |
| 839 | return BT_STATUS_SUCCESS; |
| 840 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 841 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 842 | /***************************************************************************** |
| 843 | ** |
| 844 | ** btif api adapter property functions |
| 845 | ** |
| 846 | *****************************************************************************/ |
| 847 | |
| 848 | static 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 | |
| 903 | static 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 | |
| 960 | static void execute_storage_request(UINT16 event, char *p_param) |
| 961 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 962 | bt_status_t status = BT_STATUS_SUCCESS; |
| 963 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 964 | BTIF_TRACE_EVENT("execute storage request event : %d", event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 965 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 972 | BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type, |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 973 | 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 Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 987 | if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) |
| 988 | { |
Prerepa Viswanadham | 0c4ec0d | 2014-06-10 21:04:43 -0700 | [diff] [blame] | 989 | #if (BLE_INCLUDED == TRUE) |
Ganesh Ganapathi Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 990 | 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 Calloji | f538707 | 2015-02-09 17:40:52 -0800 | [diff] [blame] | 1005 | local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg; |
Satya Calloji | e5ba884 | 2014-07-03 17:18:02 -0700 | [diff] [blame] | 1006 | local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support; |
Satya Calloji | f538707 | 2015-02-09 17:40:52 -0800 | [diff] [blame] | 1007 | 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 Calloji | 5725fc6 | 2015-03-31 13:24:32 -0700 | [diff] [blame] | 1010 | |
| 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 Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 1013 | memcpy(prop.val, &local_le_features, prop.len); |
Prerepa Viswanadham | 0c4ec0d | 2014-06-10 21:04:43 -0700 | [diff] [blame] | 1014 | #endif |
Ganesh Ganapathi Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | status = btif_storage_get_adapter_property(&prop); |
| 1019 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1020 | 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1034 | BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | static 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1044 | BTIF_TRACE_EVENT("execute storage remote request event : %d", event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1045 | |
| 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 | |
| 1075 | void 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 | } |
| 1082 | void 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 | *******************************************************************************/ |
| 1099 | static 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1105 | BTIF_TRACE_EVENT("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1106 | 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 | |
| 1133 | bt_status_t btif_get_adapter_properties(void) |
| 1134 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1135 | BTIF_TRACE_EVENT("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1136 | |
| 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 | |
| 1155 | bt_status_t btif_get_adapter_property(bt_property_type_t type) |
| 1156 | { |
| 1157 | btif_storage_req_t req; |
| 1158 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1159 | BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1160 | |
| 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 | |
| 1184 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1192 | BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1193 | 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1207 | BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1208 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1239 | BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1240 | return BT_STATUS_PARM_INVALID; |
| 1241 | } |
| 1242 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1243 | BTIF_TRACE_EVENT("set property scan mode : %x", mode); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1244 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1266 | BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d", |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1267 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1274 | /* 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 | *******************************************************************************/ |
| 1299 | bt_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 | *******************************************************************************/ |
| 1324 | bt_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 | *******************************************************************************/ |
| 1349 | bt_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 | *******************************************************************************/ |
| 1378 | bt_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 | |
| 1398 | tBTA_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 | *******************************************************************************/ |
| 1414 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1426 | BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1427 | |
| 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 | *******************************************************************************/ |
| 1448 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1459 | BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1460 | |
| 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 Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 1470 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1471 | static 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 Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 1474 | } |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1475 | |
| 1476 | static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) { |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 1477 | 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 Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |