The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2009-2012 Broadcom Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | /****************************************************************************** |
| 20 | * |
| 21 | * Filename: bte_main.c |
| 22 | * |
| 23 | * Description: Contains BTE core stack initialization and shutdown code |
| 24 | * |
| 25 | ******************************************************************************/ |
| 26 | #include <fcntl.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <time.h> |
| 31 | #include <hardware/bluetooth.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 32 | |
| 33 | #include "gki.h" |
| 34 | #include "bd.h" |
| 35 | #include "btu.h" |
| 36 | #include "bte.h" |
| 37 | #include "bta_api.h" |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 38 | #include "bt_utils.h" |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 39 | #include "bt_hci_bdroid.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 40 | |
| 41 | /******************************************************************************* |
| 42 | ** Constants & Macros |
| 43 | *******************************************************************************/ |
| 44 | |
| 45 | /* Run-time configuration file */ |
| 46 | #ifndef BTE_STACK_CONF_FILE |
| 47 | #define BTE_STACK_CONF_FILE "/etc/bluetooth/bt_stack.conf" |
| 48 | #endif |
| 49 | |
| 50 | /* if not specified in .txt file then use this as default */ |
| 51 | #ifndef HCI_LOGGING_FILENAME |
| 52 | #define HCI_LOGGING_FILENAME "/data/misc/bluedroid/btsnoop_hci.log" |
| 53 | #endif |
| 54 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 55 | /* Stack preload process timeout period */ |
| 56 | #ifndef PRELOAD_START_TIMEOUT_MS |
| 57 | #define PRELOAD_START_TIMEOUT_MS 3000 // 3 seconds |
| 58 | #endif |
| 59 | |
| 60 | /* Stack preload process maximum retry attempts */ |
| 61 | #ifndef PRELOAD_MAX_RETRY_ATTEMPTS |
| 62 | #define PRELOAD_MAX_RETRY_ATTEMPTS 0 |
| 63 | #endif |
| 64 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 65 | /******************************************************************************* |
| 66 | ** Local type definitions |
| 67 | *******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 68 | /* Preload retry control block */ |
| 69 | typedef struct |
| 70 | { |
| 71 | int retry_counts; |
| 72 | BOOLEAN timer_created; |
| 73 | timer_t timer_id; |
| 74 | } bt_preload_retry_cb_t; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 75 | |
| 76 | /****************************************************************************** |
| 77 | ** Variables |
| 78 | ******************************************************************************/ |
| 79 | BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */ |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 80 | BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 81 | char hci_logfile[256] = HCI_LOGGING_FILENAME; |
| 82 | |
| 83 | |
| 84 | /******************************************************************************* |
| 85 | ** Static variables |
| 86 | *******************************************************************************/ |
| 87 | static bt_hc_interface_t *bt_hc_if=NULL; |
| 88 | static const bt_hc_callbacks_t hc_callbacks; |
| 89 | static BOOLEAN lpm_enabled = FALSE; |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 90 | static bt_preload_retry_cb_t preload_retry_cb; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 91 | |
| 92 | /******************************************************************************* |
| 93 | ** Static functions |
| 94 | *******************************************************************************/ |
| 95 | static void bte_main_in_hw_init(void); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 96 | static void bte_hci_enable(void); |
| 97 | static void bte_hci_disable(void); |
| 98 | static void preload_start_wait_timer(void); |
| 99 | static void preload_stop_wait_timer(void); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 100 | |
| 101 | /******************************************************************************* |
| 102 | ** Externs |
| 103 | *******************************************************************************/ |
| 104 | BTU_API extern UINT32 btu_task (UINT32 param); |
| 105 | BTU_API extern void BTE_Init (void); |
| 106 | BT_API extern void BTE_LoadStack(void); |
| 107 | BT_API void BTE_UnloadStack(void); |
| 108 | extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src); |
| 109 | extern void bte_load_conf(const char *p_path); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 110 | extern bt_bdaddr_t btif_local_bd_addr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 111 | |
| 112 | |
| 113 | /******************************************************************************* |
| 114 | ** System Task Configuration |
| 115 | *******************************************************************************/ |
| 116 | |
| 117 | /* bluetooth protocol stack (BTU) task */ |
| 118 | #ifndef BTE_BTU_STACK_SIZE |
| 119 | #define BTE_BTU_STACK_SIZE 0//0x2000 /* In bytes */ |
| 120 | #endif |
| 121 | #define BTE_BTU_TASK_STR ((INT8 *) "BTU") |
| 122 | UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4]; |
| 123 | |
| 124 | /****************************************************************************** |
| 125 | ** |
| 126 | ** Function bte_main_in_hw_init |
| 127 | ** |
| 128 | ** Description Internal helper function for chip hardware init |
| 129 | ** |
| 130 | ** Returns None |
| 131 | ** |
| 132 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 133 | static void bte_main_in_hw_init(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 134 | { |
| 135 | if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface()) \ |
| 136 | == NULL) |
| 137 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 138 | APPL_TRACE_ERROR("!!! Failed to get BtHostControllerInterface !!!"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 139 | } |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 140 | |
| 141 | memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /****************************************************************************** |
| 145 | ** |
| 146 | ** Function bte_main_boot_entry |
| 147 | ** |
| 148 | ** Description BTE MAIN API - Entry point for BTE chip/stack initialization |
| 149 | ** |
| 150 | ** Returns None |
| 151 | ** |
| 152 | ******************************************************************************/ |
| 153 | void bte_main_boot_entry(void) |
| 154 | { |
| 155 | /* initialize OS */ |
| 156 | GKI_init(); |
| 157 | |
| 158 | bte_main_in_hw_init(); |
| 159 | |
| 160 | bte_load_conf(BTE_STACK_CONF_FILE); |
| 161 | |
| 162 | #if (BTTRC_INCLUDED == TRUE) |
| 163 | /* Initialize trace feature */ |
| 164 | BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM); |
| 165 | #endif |
| 166 | } |
| 167 | |
| 168 | /****************************************************************************** |
| 169 | ** |
| 170 | ** Function bte_main_shutdown |
| 171 | ** |
| 172 | ** Description BTE MAIN API - Shutdown code for BTE chip/stack |
| 173 | ** |
| 174 | ** Returns None |
| 175 | ** |
| 176 | ******************************************************************************/ |
| 177 | void bte_main_shutdown() |
| 178 | { |
| 179 | GKI_shutdown(); |
| 180 | } |
| 181 | |
| 182 | /****************************************************************************** |
| 183 | ** |
| 184 | ** Function bte_main_enable |
| 185 | ** |
| 186 | ** Description BTE MAIN API - Creates all the BTE tasks. Should be called |
| 187 | ** part of the Bluetooth stack enable sequence |
| 188 | ** |
| 189 | ** Returns None |
| 190 | ** |
| 191 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 192 | void bte_main_enable() |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 193 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 194 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 195 | |
| 196 | /* Initialize BTE control block */ |
| 197 | BTE_Init(); |
| 198 | |
| 199 | lpm_enabled = FALSE; |
| 200 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 201 | GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR, |
| 202 | (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE), |
| 203 | sizeof(bte_btu_stack)); |
| 204 | |
Zhihai Xu | 1a558ca | 2014-01-15 10:28:55 -0800 | [diff] [blame] | 205 | bte_hci_enable(); |
| 206 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 207 | GKI_run(0); |
| 208 | } |
| 209 | |
| 210 | /****************************************************************************** |
| 211 | ** |
| 212 | ** Function bte_main_disable |
| 213 | ** |
| 214 | ** Description BTE MAIN API - Destroys all the BTE tasks. Should be called |
| 215 | ** part of the Bluetooth stack disable sequence |
| 216 | ** |
| 217 | ** Returns None |
| 218 | ** |
| 219 | ******************************************************************************/ |
| 220 | void bte_main_disable(void) |
| 221 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 222 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 223 | |
| 224 | preload_stop_wait_timer(); |
| 225 | bte_hci_disable(); |
| 226 | GKI_destroy_task(BTU_TASK); |
| 227 | GKI_freeze(); |
| 228 | } |
| 229 | |
| 230 | /****************************************************************************** |
| 231 | ** |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 232 | ** Function bte_main_config_hci_logging |
| 233 | ** |
| 234 | ** Description enable or disable HIC snoop logging |
| 235 | ** |
| 236 | ** Returns None |
| 237 | ** |
| 238 | ******************************************************************************/ |
| 239 | void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled) |
| 240 | { |
| 241 | int old = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE); |
| 242 | int new; |
| 243 | |
| 244 | if (enable) { |
| 245 | hci_logging_config = TRUE; |
| 246 | } else { |
| 247 | hci_logging_config = FALSE; |
| 248 | } |
| 249 | |
| 250 | new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE); |
| 251 | |
| 252 | if ((old == new) || bt_disabled || (bt_hc_if == NULL)) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | bt_hc_if->logging(new ? BT_HC_LOGGING_ON : BT_HC_LOGGING_OFF, hci_logfile); |
| 257 | } |
| 258 | |
| 259 | /****************************************************************************** |
| 260 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 261 | ** Function bte_hci_enable |
| 262 | ** |
| 263 | ** Description Enable HCI & Vendor modules |
| 264 | ** |
| 265 | ** Returns None |
| 266 | ** |
| 267 | ******************************************************************************/ |
| 268 | static void bte_hci_enable(void) |
| 269 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 270 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 271 | |
| 272 | preload_start_wait_timer(); |
| 273 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 274 | if (bt_hc_if) |
| 275 | { |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 276 | int result = bt_hc_if->init(&hc_callbacks, btif_local_bd_addr.address); |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 277 | APPL_TRACE_EVENT("libbt-hci init returns %d", result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 278 | |
| 279 | assert(result == BT_HC_STATUS_SUCCESS); |
| 280 | |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 281 | if (hci_logging_enabled == TRUE || hci_logging_config == TRUE) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 282 | bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile); |
| 283 | |
| 284 | #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 285 | APPL_TRACE_DEBUG("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 286 | |
| 287 | /* Do not power off the chip before powering on if BT_CLEAN_TURN_ON_DISABLED flag |
| 288 | is defined and set to TRUE to avoid below mentioned issue. |
| 289 | |
| 290 | Wingray kernel driver maintains a combined counter to keep track of |
| 291 | BT-Wifi state. Invoking set_power(BT_HC_CHIP_PWR_OFF) when the BT is already |
| 292 | in OFF state causes this counter to be incorrectly decremented and results in undesired |
| 293 | behavior of the chip. |
| 294 | |
| 295 | This is only a workaround and when the issue is fixed in the kernel this work around |
| 296 | should be removed. */ |
| 297 | #else |
| 298 | /* toggle chip power to ensure we will reset chip in case |
| 299 | a previous stack shutdown wasn't completed gracefully */ |
| 300 | bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF); |
| 301 | #endif |
| 302 | bt_hc_if->set_power(BT_HC_CHIP_PWR_ON); |
| 303 | |
| 304 | bt_hc_if->preload(NULL); |
| 305 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /****************************************************************************** |
| 309 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 310 | ** Function bte_hci_disable |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 311 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 312 | ** Description Disable HCI & Vendor modules |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 313 | ** |
| 314 | ** Returns None |
| 315 | ** |
| 316 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 317 | static void bte_hci_disable(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 318 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 319 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 320 | |
| 321 | if (bt_hc_if) |
| 322 | { |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 323 | if (hci_logging_enabled == TRUE || hci_logging_config == TRUE) |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 324 | bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile); |
Sharvil Nanavati | f3b23f2 | 2014-06-15 13:36:45 -0700 | [diff] [blame] | 325 | bt_hc_if->cleanup(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /******************************************************************************* |
| 330 | ** |
| 331 | ** Function preload_wait_timeout |
| 332 | ** |
| 333 | ** Description Timeout thread of preload watchdog timer |
| 334 | ** |
| 335 | ** Returns None |
| 336 | ** |
| 337 | *******************************************************************************/ |
| 338 | static void preload_wait_timeout(union sigval arg) |
| 339 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 340 | UNUSED(arg); |
| 341 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 342 | APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...", |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 343 | preload_retry_cb.retry_counts, |
| 344 | PRELOAD_MAX_RETRY_ATTEMPTS); |
| 345 | |
| 346 | if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS) |
| 347 | { |
| 348 | bte_hci_disable(); |
| 349 | GKI_delay(100); |
| 350 | bte_hci_enable(); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | /* Notify BTIF_TASK that the init procedure had failed*/ |
| 355 | GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /******************************************************************************* |
| 360 | ** |
| 361 | ** Function preload_start_wait_timer |
| 362 | ** |
| 363 | ** Description Launch startup watchdog timer |
| 364 | ** |
| 365 | ** Returns None |
| 366 | ** |
| 367 | *******************************************************************************/ |
| 368 | static void preload_start_wait_timer(void) |
| 369 | { |
| 370 | int status; |
| 371 | struct itimerspec ts; |
| 372 | struct sigevent se; |
| 373 | UINT32 timeout_ms = PRELOAD_START_TIMEOUT_MS; |
| 374 | |
| 375 | if (preload_retry_cb.timer_created == FALSE) |
| 376 | { |
| 377 | se.sigev_notify = SIGEV_THREAD; |
| 378 | se.sigev_value.sival_ptr = &preload_retry_cb.timer_id; |
| 379 | se.sigev_notify_function = preload_wait_timeout; |
| 380 | se.sigev_notify_attributes = NULL; |
| 381 | |
| 382 | status = timer_create(CLOCK_MONOTONIC, &se, &preload_retry_cb.timer_id); |
| 383 | |
| 384 | if (status == 0) |
| 385 | preload_retry_cb.timer_created = TRUE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 386 | } |
| 387 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 388 | if (preload_retry_cb.timer_created == TRUE) |
| 389 | { |
| 390 | ts.it_value.tv_sec = timeout_ms/1000; |
| 391 | ts.it_value.tv_nsec = 1000000*(timeout_ms%1000); |
| 392 | ts.it_interval.tv_sec = 0; |
| 393 | ts.it_interval.tv_nsec = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 394 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 395 | status = timer_settime(preload_retry_cb.timer_id, 0, &ts, 0); |
| 396 | if (status == -1) |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 397 | APPL_TRACE_ERROR("Failed to fire preload watchdog timer"); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | /******************************************************************************* |
| 402 | ** |
| 403 | ** Function preload_stop_wait_timer |
| 404 | ** |
| 405 | ** Description Stop preload watchdog timer |
| 406 | ** |
| 407 | ** Returns None |
| 408 | ** |
| 409 | *******************************************************************************/ |
| 410 | static void preload_stop_wait_timer(void) |
| 411 | { |
| 412 | if (preload_retry_cb.timer_created == TRUE) |
| 413 | { |
| 414 | timer_delete(preload_retry_cb.timer_id); |
| 415 | preload_retry_cb.timer_created = FALSE; |
| 416 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /****************************************************************************** |
| 420 | ** |
| 421 | ** Function bte_main_postload_cfg |
| 422 | ** |
| 423 | ** Description BTE MAIN API - Stack postload configuration |
| 424 | ** |
| 425 | ** Returns None |
| 426 | ** |
| 427 | ******************************************************************************/ |
| 428 | void bte_main_postload_cfg(void) |
| 429 | { |
| 430 | if (bt_hc_if) |
| 431 | bt_hc_if->postload(NULL); |
| 432 | } |
| 433 | |
| 434 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 435 | /****************************************************************************** |
| 436 | ** |
| 437 | ** Function bte_main_enable_lpm |
| 438 | ** |
| 439 | ** Description BTE MAIN API - Enable/Disable low power mode operation |
| 440 | ** |
| 441 | ** Returns None |
| 442 | ** |
| 443 | ******************************************************************************/ |
| 444 | void bte_main_enable_lpm(BOOLEAN enable) |
| 445 | { |
| 446 | int result = -1; |
| 447 | |
| 448 | if (bt_hc_if) |
| 449 | result = bt_hc_if->lpm( \ |
| 450 | (enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \ |
| 451 | ); |
| 452 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 453 | APPL_TRACE_EVENT("HC lib lpm enable=%d return %d", enable, result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /****************************************************************************** |
| 457 | ** |
| 458 | ** Function bte_main_lpm_allow_bt_device_sleep |
| 459 | ** |
| 460 | ** Description BTE MAIN API - Allow BT controller goest to sleep |
| 461 | ** |
| 462 | ** Returns None |
| 463 | ** |
| 464 | ******************************************************************************/ |
| 465 | void bte_main_lpm_allow_bt_device_sleep() |
| 466 | { |
| 467 | int result = -1; |
| 468 | |
| 469 | if ((bt_hc_if) && (lpm_enabled == TRUE)) |
| 470 | result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT); |
| 471 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 472 | APPL_TRACE_DEBUG("HC lib lpm deassertion return %d", result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /****************************************************************************** |
| 476 | ** |
| 477 | ** Function bte_main_lpm_wake_bt_device |
| 478 | ** |
| 479 | ** Description BTE MAIN API - Wake BT controller up if it is in sleep mode |
| 480 | ** |
| 481 | ** Returns None |
| 482 | ** |
| 483 | ******************************************************************************/ |
| 484 | void bte_main_lpm_wake_bt_device() |
| 485 | { |
| 486 | int result = -1; |
| 487 | |
| 488 | if ((bt_hc_if) && (lpm_enabled == TRUE)) |
| 489 | result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT); |
| 490 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 491 | APPL_TRACE_DEBUG("HC lib lpm assertion return %d", result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 492 | } |
| 493 | #endif // HCILP_INCLUDED |
| 494 | |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 495 | |
| 496 | /* NOTICE: |
| 497 | * Definitions for audio state structure, this type needs to match to |
| 498 | * the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h |
| 499 | */ |
| 500 | typedef struct { |
| 501 | UINT16 handle; |
| 502 | UINT16 peer_codec; |
| 503 | UINT16 state; |
| 504 | } bt_hc_audio_state_t; |
| 505 | |
| 506 | struct bt_audio_state_tag { |
| 507 | BT_HDR hdr; |
| 508 | bt_hc_audio_state_t audio; |
| 509 | }; |
| 510 | |
| 511 | /****************************************************************************** |
| 512 | ** |
| 513 | ** Function set_audio_state |
| 514 | ** |
| 515 | ** Description Sets audio state on controller state for SCO (PCM, WBS, FM) |
| 516 | ** |
| 517 | ** Parameters handle: codec related handle for SCO: sco cb idx, unused for |
| 518 | ** codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec |
| 519 | ** state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP |
| 520 | ** param: future extensions, e.g. call-in structure/event. |
| 521 | ** |
| 522 | ** Returns None |
| 523 | ** |
| 524 | ******************************************************************************/ |
| 525 | int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param) |
| 526 | { |
| 527 | struct bt_audio_state_tag *p_msg; |
| 528 | int result = -1; |
| 529 | |
| 530 | APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle, |
| 531 | codec, state); |
| 532 | if (NULL != param) |
| 533 | APPL_TRACE_WARNING("set_audio_state() non-null param not supported"); |
| 534 | p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg)); |
| 535 | if (!p_msg) |
| 536 | return result; |
| 537 | p_msg->audio.handle = handle; |
| 538 | p_msg->audio.peer_codec = codec; |
| 539 | p_msg->audio.state = state; |
| 540 | |
| 541 | p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE); |
| 542 | p_msg->hdr.len = sizeof(p_msg->audio); |
| 543 | p_msg->hdr.offset = 0; |
| 544 | /* layer_specific shall contain return path event! for BTA events! |
| 545 | * 0 means no return message is expected. */ |
| 546 | p_msg->hdr.layer_specific = 0; |
| 547 | if (bt_hc_if) |
| 548 | { |
| 549 | bt_hc_if->tx_cmd((TRANSAC)p_msg, (char *)(&p_msg->audio), sizeof(*p_msg)); |
| 550 | } |
| 551 | return result; |
| 552 | } |
| 553 | |
| 554 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 555 | /****************************************************************************** |
| 556 | ** |
| 557 | ** Function bte_main_hci_send |
| 558 | ** |
| 559 | ** Description BTE MAIN API - This function is called by the upper stack to |
| 560 | ** send an HCI message. The function displays a protocol trace |
| 561 | ** message (if enabled), and then calls the 'transmit' function |
| 562 | ** associated with the currently selected HCI transport |
| 563 | ** |
| 564 | ** Returns None |
| 565 | ** |
| 566 | ******************************************************************************/ |
| 567 | void bte_main_hci_send (BT_HDR *p_msg, UINT16 event) |
| 568 | { |
| 569 | UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ |
| 570 | |
| 571 | p_msg->event = event; |
| 572 | |
| 573 | |
| 574 | if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \ |
| 575 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) |
| 576 | { |
| 577 | if (bt_hc_if) |
| 578 | bt_hc_if->transmit_buf((TRANSAC)p_msg, \ |
| 579 | (char *) (p_msg + 1), \ |
| 580 | p_msg->len); |
| 581 | else |
| 582 | GKI_freebuf(p_msg); |
| 583 | } |
| 584 | else |
| 585 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 586 | APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 587 | GKI_freebuf(p_msg); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /****************************************************************************** |
| 592 | ** |
| 593 | ** Function bte_main_post_reset_init |
| 594 | ** |
| 595 | ** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT |
| 596 | ** and shall be automatically called from BTE after HCI_Reset |
| 597 | ** |
| 598 | ** Returns None |
| 599 | ** |
| 600 | ******************************************************************************/ |
| 601 | void bte_main_post_reset_init() |
| 602 | { |
| 603 | BTM_ContinueReset(); |
| 604 | } |
| 605 | |
| 606 | /***************************************************************************** |
| 607 | ** |
| 608 | ** libbt-hci Callback Functions |
| 609 | ** |
| 610 | *****************************************************************************/ |
| 611 | |
| 612 | /****************************************************************************** |
| 613 | ** |
| 614 | ** Function preload_cb |
| 615 | ** |
| 616 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 617 | ** when the libbt-hci completed stack preload process |
| 618 | ** |
| 619 | ** Returns None |
| 620 | ** |
| 621 | ******************************************************************************/ |
| 622 | static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result) |
| 623 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 624 | UNUSED(transac); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 625 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 626 | APPL_TRACE_EVENT("HC preload_cb %d [0:SUCCESS 1:FAIL]", result); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 627 | |
| 628 | if (result == BT_HC_PRELOAD_SUCCESS) |
| 629 | { |
| 630 | preload_stop_wait_timer(); |
| 631 | |
| 632 | /* notify BTU task that libbt-hci is ready */ |
| 633 | GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL); |
| 634 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /****************************************************************************** |
| 638 | ** |
| 639 | ** Function postload_cb |
| 640 | ** |
| 641 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 642 | ** when the libbt-hci lib completed stack postload process |
| 643 | ** |
| 644 | ** Returns None |
| 645 | ** |
| 646 | ******************************************************************************/ |
| 647 | static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result) |
| 648 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 649 | UNUSED(transac); |
| 650 | |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 651 | APPL_TRACE_EVENT("HC postload_cb %d", result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /****************************************************************************** |
| 655 | ** |
| 656 | ** Function lpm_cb |
| 657 | ** |
| 658 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 659 | ** back from the libbt-hci to indicate the current LPM state |
| 660 | ** |
| 661 | ** Returns None |
| 662 | ** |
| 663 | ******************************************************************************/ |
| 664 | static void lpm_cb(bt_hc_lpm_request_result_t result) |
| 665 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 666 | APPL_TRACE_EVENT("HC lpm_result_cb %d", result); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 667 | lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE; |
| 668 | } |
| 669 | |
| 670 | /****************************************************************************** |
| 671 | ** |
| 672 | ** Function hostwake_ind |
| 673 | ** |
| 674 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 675 | ** from the libbt-hci to indicate the HostWake event |
| 676 | ** |
| 677 | ** Returns None |
| 678 | ** |
| 679 | ******************************************************************************/ |
| 680 | static void hostwake_ind(bt_hc_low_power_event_t event) |
| 681 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 682 | APPL_TRACE_EVENT("HC hostwake_ind %d", event); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /****************************************************************************** |
| 686 | ** |
| 687 | ** Function alloc |
| 688 | ** |
| 689 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 690 | ** from the libbt-hci to request for data buffer allocation |
| 691 | ** |
| 692 | ** Returns NULL / pointer to allocated buffer |
| 693 | ** |
| 694 | ******************************************************************************/ |
| 695 | static char *alloc(int size) |
| 696 | { |
| 697 | BT_HDR *p_hdr = NULL; |
| 698 | |
| 699 | /* |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 700 | APPL_TRACE_DEBUG("HC alloc size=%d", size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 701 | */ |
| 702 | |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 703 | /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */ |
| 704 | if (size > GKI_MAX_BUF_SIZE) |
| 705 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 706 | APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d", |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 707 | size, GKI_MAX_BUF_SIZE); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 711 | p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size); |
| 712 | |
| 713 | if (p_hdr == NULL) |
| 714 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 715 | APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return ((char *) p_hdr); |
| 719 | } |
| 720 | |
| 721 | /****************************************************************************** |
| 722 | ** |
| 723 | ** Function dealloc |
| 724 | ** |
| 725 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 726 | ** from the libbt-hci to release the data buffer allocated |
| 727 | ** through the alloc call earlier |
| 728 | ** |
| 729 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 730 | ** pass data-path buffer/packet across bt_hci_lib interface |
Sharvil Nanavati | 75e8f41 | 2014-06-24 17:02:30 -0700 | [diff] [blame] | 731 | ** boundary. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 732 | ** |
| 733 | ******************************************************************************/ |
Sharvil Nanavati | 75e8f41 | 2014-06-24 17:02:30 -0700 | [diff] [blame] | 734 | static void dealloc(TRANSAC transac) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 735 | { |
| 736 | GKI_freebuf(transac); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /****************************************************************************** |
| 740 | ** |
| 741 | ** Function data_ind |
| 742 | ** |
| 743 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 744 | ** from the libbt-hci to pass in the received HCI packets |
| 745 | ** |
| 746 | ** The core stack is responsible for releasing the data buffer |
| 747 | ** passed in from the libbt-hci once the core stack has done |
| 748 | ** with it. |
| 749 | ** |
| 750 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 751 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 752 | ** boundary. The 'p_buf' and 'len' parameters are not intended |
| 753 | ** to be used here but might point to data portion in data- |
| 754 | ** path buffer and length of valid data respectively. |
| 755 | ** |
| 756 | ** Returns bt_hc_status_t |
| 757 | ** |
| 758 | ******************************************************************************/ |
| 759 | static int data_ind(TRANSAC transac, char *p_buf, int len) |
| 760 | { |
| 761 | BT_HDR *p_msg = (BT_HDR *) transac; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 762 | UNUSED(p_buf); |
| 763 | UNUSED(len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 764 | |
| 765 | /* |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 766 | APPL_TRACE_DEBUG("HC data_ind event=0x%04X (len=%d)", p_msg->event, len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 767 | */ |
| 768 | |
| 769 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac); |
| 770 | return BT_HC_STATUS_SUCCESS; |
| 771 | } |
| 772 | |
| 773 | /****************************************************************************** |
| 774 | ** |
| 775 | ** Function tx_result |
| 776 | ** |
| 777 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 778 | ** from the libbt-hci once it has processed/sent the prior data |
| 779 | ** buffer which core stack passed to it through transmit_buf |
| 780 | ** call earlier. |
| 781 | ** |
| 782 | ** The core stack is responsible for releasing the data buffer |
| 783 | ** if it has been completedly processed. |
| 784 | ** |
| 785 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 786 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 787 | ** boundary. The 'p_buf' is not intended to be used here |
| 788 | ** but might point to data portion in data-path buffer. |
| 789 | ** |
| 790 | ** Returns bt_hc_status_t |
| 791 | ** |
| 792 | ******************************************************************************/ |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 793 | static int tx_result(TRANSAC transac, char *p_buf, bt_hc_transmit_result_t result) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 794 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 795 | UNUSED(p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 796 | /* |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 797 | APPL_TRACE_DEBUG("HC tx_result %d (event=%04X)", result, \ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 798 | ((BT_HDR *)transac)->event); |
| 799 | */ |
| 800 | |
| 801 | if (result == BT_HC_TX_FRAGMENT) |
| 802 | { |
| 803 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac); |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | GKI_freebuf(transac); |
| 808 | } |
| 809 | |
| 810 | return BT_HC_STATUS_SUCCESS; |
| 811 | } |
| 812 | |
| 813 | /***************************************************************************** |
| 814 | ** The libbt-hci Callback Functions Table |
| 815 | *****************************************************************************/ |
| 816 | static const bt_hc_callbacks_t hc_callbacks = { |
| 817 | sizeof(bt_hc_callbacks_t), |
| 818 | preload_cb, |
| 819 | postload_cb, |
| 820 | lpm_cb, |
| 821 | hostwake_ind, |
| 822 | alloc, |
| 823 | dealloc, |
| 824 | data_ind, |
| 825 | tx_result |
| 826 | }; |
| 827 | |