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 | |
Marie Janssen | 49120dc | 2015-07-07 16:47:20 -0700 | [diff] [blame] | 29 | #define LOG_TAG "bt_btif_core" |
| 30 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 31 | #include <ctype.h> |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 32 | #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 Uguray | d30195c | 2015-05-29 15:27:58 -0700 | [diff] [blame] | 39 | #include <unistd.h> |
| 40 | |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 41 | #include "bdaddr.h" |
Zhihai Xu | 6c65c2f | 2013-11-25 17:30:59 -0800 | [diff] [blame] | 42 | #include "bt_utils.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | #include "bta_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 44 | #include "bte.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 45 | #include "btif_api.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 46 | #include "btif_av.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 47 | #include "btif_config.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 48 | #include "btif_config.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 49 | #include "btif_pan.h" |
| 50 | #include "btif_profile_queue.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 51 | #include "btif_sock.h" |
| 52 | #include "btif_storage.h" |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 53 | #include "btif_uid.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 54 | #include "btif_util.h" |
| 55 | #include "btu.h" |
Chris Manton | 79ecab5 | 2014-10-31 14:54:51 -0700 | [diff] [blame] | 56 | #include "device/include/controller.h" |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 57 | #include "bt_common.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" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 60 | #include "osi/include/log.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 61 | #include "osi/include/osi.h" |
Jakub Pawlowski | 3eb4a48 | 2016-02-24 10:39:46 -0800 | [diff] [blame] | 62 | #include "osi/include/properties.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 63 | #include "osi/include/thread.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 64 | #include "stack_manager.h" |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 65 | |
Bryce Lee | 3d6accf | 2016-05-10 17:10:09 -0700 | [diff] [blame^] | 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 70 | /************************************************************************************ |
| 71 | ** Constants & Macros |
| 72 | ************************************************************************************/ |
| 73 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 74 | #ifndef BTE_DID_CONF_FILE |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 75 | // 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 79 | #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf" |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 80 | #endif // defined(OS_GENERIC) |
| 81 | #endif // BTE_DID_CONF_FILE |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 83 | /************************************************************************************ |
| 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 | |
| 90 | typedef struct { |
| 91 | bt_bdaddr_t bd_addr; |
| 92 | bt_property_type_t type; |
| 93 | } btif_storage_read_t; |
| 94 | |
| 95 | typedef struct { |
| 96 | bt_bdaddr_t bd_addr; |
| 97 | bt_property_t prop; |
| 98 | } btif_storage_write_t; |
| 99 | |
| 100 | typedef union { |
| 101 | btif_storage_read_t read_req; |
| 102 | btif_storage_write_t write_req; |
| 103 | } btif_storage_req_t; |
| 104 | |
| 105 | typedef 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 | |
| 116 | bt_bdaddr_t btif_local_bd_addr; |
| 117 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 118 | static 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 | */ |
| 126 | static UINT8 btif_dut_mode = 0; |
| 127 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 128 | static thread_t *bt_jni_workqueue_thread; |
| 129 | static const char *BT_JNI_WORKQUEUE_NAME = "bt_jni_workqueue"; |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 130 | static uid_set_t* uid_set = NULL; |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 131 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 132 | /************************************************************************************ |
| 133 | ** Static functions |
| 134 | ************************************************************************************/ |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 135 | static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param); |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 136 | static void btif_jni_disassociate(); |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 137 | 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] | 138 | |
| 139 | /* sends message to btif task */ |
| 140 | static void btif_sendmsg(void *p_msg); |
| 141 | |
| 142 | /************************************************************************************ |
| 143 | ** Externs |
| 144 | ************************************************************************************/ |
Chris Manton | 860a9af | 2014-08-27 10:30:47 -0700 | [diff] [blame] | 145 | extern fixed_queue_t *btu_hci_msg_queue; |
| 146 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 147 | extern void bte_load_did_conf(const char *p_path); |
| 148 | |
| 149 | /** TODO: Move these to _common.h */ |
| 150 | void bte_main_boot_entry(void); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 151 | void bte_main_disable(void); |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 152 | void bte_main_cleanup(void); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 153 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 154 | void bte_main_enable_lpm(BOOLEAN enable); |
| 155 | #endif |
| 156 | void bte_main_postload_cfg(void); |
Jakub Pawlowski | 713993d | 2016-04-21 13:16:45 -0700 | [diff] [blame] | 157 | void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled); |
| 158 | |
| 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 163 | void btif_dm_execute_service_request(UINT16 event, char *p_param); |
| 164 | #ifdef BTIF_DM_OOB_TEST |
| 165 | void btif_dm_load_local_oob(void); |
| 166 | #endif |
| 167 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 168 | /******************************************************************************* |
| 169 | ** |
| 170 | ** Function btif_context_switched |
| 171 | ** |
| 172 | ** Description Callback used to execute transferred context callback |
| 173 | ** |
| 174 | ** p_msg : message to be executed in btif context |
| 175 | ** |
| 176 | ** Returns void |
| 177 | ** |
| 178 | *******************************************************************************/ |
| 179 | |
| 180 | static void btif_context_switched(void *p_msg) |
| 181 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 182 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 183 | BTIF_TRACE_VERBOSE("btif_context_switched"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 184 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 185 | 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] | 186 | |
| 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 | |
| 209 | bt_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 Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 211 | tBTIF_CONTEXT_SWITCH_CBACK *p_msg = |
| 212 | (tBTIF_CONTEXT_SWITCH_CBACK *)osi_malloc(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 213 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 214 | 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] | 215 | |
| 216 | /* allocate and send message that will be executed in btif context */ |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 217 | p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */ |
| 218 | p_msg->p_cb = p_cback; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 219 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 220 | p_msg->event = event; /* callback event */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 221 | |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 222 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 227 | } |
Pavlin Radoslavov | 717a4a9 | 2016-02-06 08:36:06 -0800 | [diff] [blame] | 228 | |
| 229 | btif_sendmsg(p_msg); |
| 230 | |
| 231 | return BT_STATUS_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 232 | } |
| 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 | |
| 244 | UINT8 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 | |
| 259 | int btif_is_enabled(void) |
| 260 | { |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 261 | 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] | 262 | } |
| 263 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 264 | void 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 272 | /******************************************************************************* |
| 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 Johnson | 4ada041 | 2014-09-22 17:59:02 -0700 | [diff] [blame] | 282 | static void bt_jni_msg_ready(void *context) { |
| 283 | BT_HDR *p_msg = (BT_HDR *)context; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 284 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 285 | 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] | 286 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 287 | 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 Radoslavov | cceb430 | 2016-02-05 13:54:43 -0800 | [diff] [blame] | 295 | osi_free(p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 296 | } |
| 297 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 298 | /******************************************************************************* |
| 299 | ** |
| 300 | ** Function btif_sendmsg |
| 301 | ** |
| 302 | ** Description Sends msg to BTIF task |
| 303 | ** |
| 304 | ** Returns void |
| 305 | ** |
| 306 | *******************************************************************************/ |
| 307 | |
| 308 | void btif_sendmsg(void *p_msg) |
| 309 | { |
Zach Johnson | 4ada041 | 2014-09-22 17:59:02 -0700 | [diff] [blame] | 310 | 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] | 311 | } |
| 312 | |
Zach Johnson | efea782 | 2014-09-25 11:58:50 -0700 | [diff] [blame] | 313 | void btif_thread_post(thread_fn func, void *context) { |
| 314 | thread_post(bt_jni_workqueue_thread, func, context); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 317 | static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr) { |
| 318 | char val[PROPERTY_VALUE_MAX] = {0}; |
| 319 | |
Jakub Pawlowski | 3eb4a48 | 2016-02-24 10:39:46 -0800 | [diff] [blame] | 320 | if (osi_property_get(key, val, NULL)) { |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 321 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 330 | static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr) |
| 331 | { |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 332 | char val[PROPERTY_VALUE_MAX] = {0}; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 333 | uint8_t valid_bda = FALSE; |
| 334 | int val_size = 0; |
Arman Uguray | d30195c | 2015-05-29 15:27:58 -0700 | [diff] [blame] | 335 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 336 | const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0}; |
| 337 | |
| 338 | /* Get local bdaddr storage path from property */ |
Jakub Pawlowski | 3eb4a48 | 2016-02-24 10:39:46 -0800 | [diff] [blame] | 339 | if (osi_property_get(PROPERTY_BT_BDADDR_PATH, val, NULL)) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 340 | { |
| 341 | int addr_fd; |
| 342 | |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 343 | 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] | 344 | |
| 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 349 | /* If this is not a reserved/special bda, then use it */ |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 350 | if ((string_to_bdaddr(val, local_addr)) && |
| 351 | (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] | 352 | { |
| 353 | valid_bda = TRUE; |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 354 | 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] | 355 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 356 | close(addr_fd); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if(!valid_bda) |
| 361 | { |
| 362 | val_size = sizeof(val); |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 363 | 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] | 364 | { |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 365 | string_to_bdaddr(val, local_addr); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 366 | 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] | 367 | return; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* No factory BDADDR found. Look for previously generated random BDA */ |
Ajay Panicker | 28f294b | 2015-08-03 16:29:31 -0700 | [diff] [blame] | 372 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* Generate new BDA if necessary */ |
| 382 | if (!valid_bda) |
| 383 | { |
| 384 | bdstr_t bdstr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 385 | |
| 386 | /* No autogen BDA. Generate one now. */ |
| 387 | local_addr->address[0] = 0x22; |
| 388 | local_addr->address[1] = 0x22; |
Marie Janssen | 093f013 | 2016-03-10 11:27:53 -0800 | [diff] [blame] | 389 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 393 | |
| 394 | /* Convert to ascii, and store as a persistent property */ |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 395 | bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 396 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 397 | 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] | 398 | (char*)bdstr, PERSIST_BDADDR_PROPERTY); |
| 399 | |
Jakub Pawlowski | 3eb4a48 | 2016-02-24 10:39:46 -0800 | [diff] [blame] | 400 | if (osi_property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 401 | 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] | 402 | } |
| 403 | |
| 404 | //save the bd address to config file |
| 405 | bdstr_t bdstr; |
Sharvil Nanavati | 8a6a89f | 2014-08-20 09:39:25 -0700 | [diff] [blame] | 406 | bdaddr_to_string(local_addr, bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 407 | val_size = sizeof(val); |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | if (strcmp(bdstr, val) ==0) |
| 411 | { |
| 412 | // BDA is already present in the config file. |
| 413 | return; |
| 414 | } |
| 415 | } |
Sharvil Nanavati | 9d52f88 | 2014-08-19 09:50:18 -0700 | [diff] [blame] | 416 | btif_config_set_str("Adapter", "Address", bdstr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 417 | } |
| 418 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 419 | /******************************************************************************* |
| 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 Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 428 | bt_status_t btif_init_bluetooth() { |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 429 | bte_main_boot_entry(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 430 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 431 | /* 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 434 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 435 | bt_jni_workqueue_thread = thread_new(BT_JNI_WORKQUEUE_NAME); |
| 436 | if (bt_jni_workqueue_thread == NULL) { |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 437 | LOG_ERROR(LOG_TAG, "%s Unable to create thread %s", __func__, BT_JNI_WORKQUEUE_NAME); |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 438 | goto error_exit; |
| 439 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 440 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 441 | // Associate this workqueue thread with jni. |
| 442 | 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] | 443 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 444 | return BT_STATUS_SUCCESS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 445 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 446 | error_exit:; |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 447 | thread_free(bt_jni_workqueue_thread); |
| 448 | |
Zach Johnson | 4ada041 | 2014-09-22 17:59:02 -0700 | [diff] [blame] | 449 | bt_jni_workqueue_thread = NULL; |
| 450 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 451 | return BT_STATUS_FAIL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /******************************************************************************* |
| 455 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 456 | ** 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 Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 465 | void btif_enable_bluetooth_evt(tBTA_STATUS status) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 466 | { |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 467 | const controller_t *controller = controller_get_interface(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 468 | bdstr_t bdstr; |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 469 | bdaddr_to_string(controller->get_address(), bdstr, sizeof(bdstr)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 470 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 471 | 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] | 472 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 473 | 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] | 474 | { |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 475 | // 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 478 | bt_property_t prop; |
| 479 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 480 | bdaddr_to_string(&btif_local_bd_addr, old_address, sizeof(old_address)); |
| 481 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 482 | /** |
| 483 | * 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] | 484 | * 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] | 485 | * the Controller's non-volatile memory rather than in device's file |
| 486 | * system. |
| 487 | **/ |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 488 | BTIF_TRACE_WARNING("***********************************************"); |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 489 | BTIF_TRACE_WARNING("BTIF init BDA was %s", old_address); |
| 490 | BTIF_TRACE_WARNING("Controller BDA is %s", bdstr); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 491 | BTIF_TRACE_WARNING("***********************************************"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 492 | |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 493 | btif_local_bd_addr = *controller->get_address(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 494 | |
| 495 | //save the bd address to config file |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 496 | btif_config_set_str("Adapter", "Address", bdstr); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 497 | btif_config_save(); |
| 498 | |
| 499 | //fire HAL callback for property change |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 500 | prop.type = BT_PROPERTY_BDADDR; |
Zach Johnson | 39110ec | 2014-10-06 13:15:00 -0700 | [diff] [blame] | 501 | prop.val = (void*)&btif_local_bd_addr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 502 | 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 Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 515 | uid_set = uid_set_create(); |
| 516 | |
| 517 | btif_dm_init(uid_set); |
| 518 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 519 | /* init rfcomm & l2cap api */ |
Adam Lesinski | 0620f97 | 2015-12-02 22:15:08 -0800 | [diff] [blame] | 520 | btif_sock_init(uid_set); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 521 | |
| 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 531 | |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 532 | future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 533 | } |
| 534 | else |
| 535 | { |
| 536 | /* cleanup rfcomm & l2cap api */ |
| 537 | btif_sock_cleanup(); |
| 538 | |
| 539 | btif_pan_cleanup(); |
| 540 | |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 541 | future_ready(stack_manager_get_hack_future(), FUTURE_FAIL); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 542 | } |
| 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 | *******************************************************************************/ |
| 556 | bt_status_t btif_disable_bluetooth(void) |
| 557 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 558 | BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 559 | |
| 560 | btif_dm_on_disable(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 561 | /* cleanup rfcomm & l2cap api */ |
| 562 | btif_sock_cleanup(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 563 | btif_pan_cleanup(); |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 564 | BTA_DisableBluetooth(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 565 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 566 | 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 | |
| 581 | void btif_disable_bluetooth_evt(void) |
| 582 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 583 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 584 | |
| 585 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 586 | bte_main_enable_lpm(FALSE); |
| 587 | #endif |
| 588 | |
Satya Calloji | d5aa247 | 2014-09-23 18:27:09 -0700 | [diff] [blame] | 589 | bte_main_disable(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 590 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 591 | /* callback to HAL */ |
Zach Johnson | 1924dac | 2014-09-24 16:01:29 -0700 | [diff] [blame] | 592 | future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 593 | } |
| 594 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 595 | /******************************************************************************* |
| 596 | ** |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 597 | ** Function btif_cleanup_bluetooth |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 598 | ** |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 599 | ** Description Cleanup BTIF state. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 600 | ** |
| 601 | ** Returns void |
| 602 | ** |
| 603 | *******************************************************************************/ |
| 604 | |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 605 | bt_status_t btif_cleanup_bluetooth(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 606 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 607 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 608 | |
Pavlin Radoslavov | 58318f4 | 2016-05-05 16:20:15 -0700 | [diff] [blame] | 609 | #if (BLE_INCLUDED == TRUE) |
| 610 | BTA_VendorCleanup(); |
| 611 | #endif |
| 612 | |
Pavlin Radoslavov | fb5a382 | 2016-03-25 18:55:30 -0700 | [diff] [blame] | 613 | btif_dm_cleanup(); |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 614 | btif_jni_disassociate(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 615 | btif_queue_release(); |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 616 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 617 | thread_free(bt_jni_workqueue_thread); |
| 618 | bt_jni_workqueue_thread = NULL; |
| 619 | |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 620 | bte_main_cleanup(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 621 | |
| 622 | btif_dut_mode = 0; |
| 623 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 624 | BTIF_TRACE_DEBUG("%s done", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 625 | |
| 626 | return BT_STATUS_SUCCESS; |
| 627 | } |
| 628 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 629 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 638 | static void btif_dut_mode_cback( tBTM_VSC_CMPL *p ) |
| 639 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 640 | UNUSED(p); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 641 | /* 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 | *******************************************************************************/ |
| 654 | bt_status_t btif_dut_mode_configure(uint8_t enable) |
| 655 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 656 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 657 | |
Zach Johnson | f450ba5 | 2014-09-18 18:46:39 -0700 | [diff] [blame] | 658 | if (!stack_manager_get_interface()->get_stack_is_running()) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 659 | 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] | 660 | 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 | *******************************************************************************/ |
| 681 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 684 | BTIF_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 685 | if (!btif_is_dut_mode()) { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 686 | 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] | 687 | return BT_STATUS_FAIL; |
| 688 | } |
| 689 | BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback); |
| 690 | return BT_STATUS_SUCCESS; |
| 691 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 692 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 693 | /***************************************************************************** |
| 694 | ** |
| 695 | ** btif api adapter property functions |
| 696 | ** |
| 697 | *****************************************************************************/ |
| 698 | |
| 699 | static 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 | |
| 754 | static 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 | |
| 811 | static void execute_storage_request(UINT16 event, char *p_param) |
| 812 | { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 813 | bt_status_t status = BT_STATUS_SUCCESS; |
| 814 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 815 | BTIF_TRACE_EVENT("execute storage request event : %d", event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 816 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 823 | 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] | 824 | 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 Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 838 | if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES) |
| 839 | { |
Prerepa Viswanadham | 0c4ec0d | 2014-06-10 21:04:43 -0700 | [diff] [blame] | 840 | #if (BLE_INCLUDED == TRUE) |
Ganesh Ganapathi Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 841 | 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 Calloji | f538707 | 2015-02-09 17:40:52 -0800 | [diff] [blame] | 856 | 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] | 857 | local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support; |
Satya Calloji | f538707 | 2015-02-09 17:40:52 -0800 | [diff] [blame] | 858 | 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 Eisenbach | 2af0bb6 | 2015-05-21 17:07:21 -0700 | [diff] [blame] | 861 | |
| 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 Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 864 | memcpy(prop.val, &local_le_features, prop.len); |
Prerepa Viswanadham | 0c4ec0d | 2014-06-10 21:04:43 -0700 | [diff] [blame] | 865 | #endif |
Ganesh Ganapathi Batta | 9546abf | 2014-05-30 16:28:00 -0700 | [diff] [blame] | 866 | } |
| 867 | else |
| 868 | { |
| 869 | status = btif_storage_get_adapter_property(&prop); |
| 870 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 871 | 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 885 | 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] | 886 | break; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | static 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 895 | 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] | 896 | |
| 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 | |
| 926 | void 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 | } |
| 933 | void 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 | *******************************************************************************/ |
| 950 | static 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 956 | BTIF_TRACE_EVENT("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 957 | 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 | |
| 984 | bt_status_t btif_get_adapter_properties(void) |
| 985 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 986 | BTIF_TRACE_EVENT("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 987 | |
| 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 | |
| 1006 | bt_status_t btif_get_adapter_property(bt_property_type_t type) |
| 1007 | { |
| 1008 | btif_storage_req_t req; |
| 1009 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1010 | BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1011 | |
| 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 | |
| 1035 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1043 | 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] | 1044 | 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1058 | 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] | 1059 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1090 | BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1091 | return BT_STATUS_PARM_INVALID; |
| 1092 | } |
| 1093 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1094 | BTIF_TRACE_EVENT("set property scan mode : %x", mode); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1095 | |
| 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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1117 | 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] | 1118 | 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 Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1125 | /* 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 | *******************************************************************************/ |
| 1150 | bt_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 | *******************************************************************************/ |
| 1175 | bt_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 | *******************************************************************************/ |
| 1200 | bt_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 | *******************************************************************************/ |
| 1229 | bt_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 | |
| 1249 | tBTA_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 | *******************************************************************************/ |
| 1265 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1277 | 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] | 1278 | |
| 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 | *******************************************************************************/ |
| 1299 | bt_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 Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 1310 | 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] | 1311 | |
| 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 Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 1321 | |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1322 | static 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 Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 1325 | } |
Chris Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1326 | |
Pavlin Radoslavov | 1bd691c | 2016-04-26 12:18:36 -0700 | [diff] [blame] | 1327 | static void btif_jni_disassociate() { |
Zach Johnson | 8ea8188 | 2014-09-18 18:13:13 -0700 | [diff] [blame] | 1328 | 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 Manton | 0eefef0 | 2014-09-08 15:01:39 -0700 | [diff] [blame] | 1331 | } |