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" |
| 38 | #include "bt_hci_lib.h" |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 39 | #include "bt_utils.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 | { |
| 138 | APPL_TRACE_ERROR0("!!! Failed to get BtHostControllerInterface !!!"); |
| 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 | { |
| 194 | APPL_TRACE_DEBUG1("%s", __FUNCTION__); |
| 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 | { |
| 222 | APPL_TRACE_DEBUG1("%s", __FUNCTION__); |
| 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 | { |
| 270 | APPL_TRACE_DEBUG1("%s", __FUNCTION__); |
| 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); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 277 | APPL_TRACE_EVENT1("libbt-hci init returns %d", result); |
| 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) |
| 285 | APPL_TRACE_DEBUG1("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__); |
| 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 | { |
| 319 | APPL_TRACE_DEBUG1("%s", __FUNCTION__); |
| 320 | |
| 321 | if (bt_hc_if) |
| 322 | { |
| 323 | bt_hc_if->cleanup(); |
| 324 | bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF); |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 325 | if (hci_logging_enabled == TRUE || hci_logging_config == TRUE) |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 326 | bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /******************************************************************************* |
| 331 | ** |
| 332 | ** Function preload_wait_timeout |
| 333 | ** |
| 334 | ** Description Timeout thread of preload watchdog timer |
| 335 | ** |
| 336 | ** Returns None |
| 337 | ** |
| 338 | *******************************************************************************/ |
| 339 | static void preload_wait_timeout(union sigval arg) |
| 340 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 341 | UNUSED(arg); |
| 342 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 343 | APPL_TRACE_ERROR2("...preload_wait_timeout (retried:%d/max-retry:%d)...", |
| 344 | preload_retry_cb.retry_counts, |
| 345 | PRELOAD_MAX_RETRY_ATTEMPTS); |
| 346 | |
| 347 | if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS) |
| 348 | { |
| 349 | bte_hci_disable(); |
| 350 | GKI_delay(100); |
| 351 | bte_hci_enable(); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | /* Notify BTIF_TASK that the init procedure had failed*/ |
| 356 | GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /******************************************************************************* |
| 361 | ** |
| 362 | ** Function preload_start_wait_timer |
| 363 | ** |
| 364 | ** Description Launch startup watchdog timer |
| 365 | ** |
| 366 | ** Returns None |
| 367 | ** |
| 368 | *******************************************************************************/ |
| 369 | static void preload_start_wait_timer(void) |
| 370 | { |
| 371 | int status; |
| 372 | struct itimerspec ts; |
| 373 | struct sigevent se; |
| 374 | UINT32 timeout_ms = PRELOAD_START_TIMEOUT_MS; |
| 375 | |
| 376 | if (preload_retry_cb.timer_created == FALSE) |
| 377 | { |
| 378 | se.sigev_notify = SIGEV_THREAD; |
| 379 | se.sigev_value.sival_ptr = &preload_retry_cb.timer_id; |
| 380 | se.sigev_notify_function = preload_wait_timeout; |
| 381 | se.sigev_notify_attributes = NULL; |
| 382 | |
| 383 | status = timer_create(CLOCK_MONOTONIC, &se, &preload_retry_cb.timer_id); |
| 384 | |
| 385 | if (status == 0) |
| 386 | preload_retry_cb.timer_created = TRUE; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 387 | } |
| 388 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 389 | if (preload_retry_cb.timer_created == TRUE) |
| 390 | { |
| 391 | ts.it_value.tv_sec = timeout_ms/1000; |
| 392 | ts.it_value.tv_nsec = 1000000*(timeout_ms%1000); |
| 393 | ts.it_interval.tv_sec = 0; |
| 394 | ts.it_interval.tv_nsec = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 395 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 396 | status = timer_settime(preload_retry_cb.timer_id, 0, &ts, 0); |
| 397 | if (status == -1) |
| 398 | APPL_TRACE_ERROR0("Failed to fire preload watchdog timer"); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /******************************************************************************* |
| 403 | ** |
| 404 | ** Function preload_stop_wait_timer |
| 405 | ** |
| 406 | ** Description Stop preload watchdog timer |
| 407 | ** |
| 408 | ** Returns None |
| 409 | ** |
| 410 | *******************************************************************************/ |
| 411 | static void preload_stop_wait_timer(void) |
| 412 | { |
| 413 | if (preload_retry_cb.timer_created == TRUE) |
| 414 | { |
| 415 | timer_delete(preload_retry_cb.timer_id); |
| 416 | preload_retry_cb.timer_created = FALSE; |
| 417 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /****************************************************************************** |
| 421 | ** |
| 422 | ** Function bte_main_postload_cfg |
| 423 | ** |
| 424 | ** Description BTE MAIN API - Stack postload configuration |
| 425 | ** |
| 426 | ** Returns None |
| 427 | ** |
| 428 | ******************************************************************************/ |
| 429 | void bte_main_postload_cfg(void) |
| 430 | { |
| 431 | if (bt_hc_if) |
| 432 | bt_hc_if->postload(NULL); |
| 433 | } |
| 434 | |
| 435 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 436 | /****************************************************************************** |
| 437 | ** |
| 438 | ** Function bte_main_enable_lpm |
| 439 | ** |
| 440 | ** Description BTE MAIN API - Enable/Disable low power mode operation |
| 441 | ** |
| 442 | ** Returns None |
| 443 | ** |
| 444 | ******************************************************************************/ |
| 445 | void bte_main_enable_lpm(BOOLEAN enable) |
| 446 | { |
| 447 | int result = -1; |
| 448 | |
| 449 | if (bt_hc_if) |
| 450 | result = bt_hc_if->lpm( \ |
| 451 | (enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \ |
| 452 | ); |
| 453 | |
| 454 | APPL_TRACE_EVENT2("HC lib lpm enable=%d return %d", enable, result); |
| 455 | } |
| 456 | |
| 457 | /****************************************************************************** |
| 458 | ** |
| 459 | ** Function bte_main_lpm_allow_bt_device_sleep |
| 460 | ** |
| 461 | ** Description BTE MAIN API - Allow BT controller goest to sleep |
| 462 | ** |
| 463 | ** Returns None |
| 464 | ** |
| 465 | ******************************************************************************/ |
| 466 | void bte_main_lpm_allow_bt_device_sleep() |
| 467 | { |
| 468 | int result = -1; |
| 469 | |
| 470 | if ((bt_hc_if) && (lpm_enabled == TRUE)) |
| 471 | result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT); |
| 472 | |
| 473 | APPL_TRACE_DEBUG1("HC lib lpm deassertion return %d", result); |
| 474 | } |
| 475 | |
| 476 | /****************************************************************************** |
| 477 | ** |
| 478 | ** Function bte_main_lpm_wake_bt_device |
| 479 | ** |
| 480 | ** Description BTE MAIN API - Wake BT controller up if it is in sleep mode |
| 481 | ** |
| 482 | ** Returns None |
| 483 | ** |
| 484 | ******************************************************************************/ |
| 485 | void bte_main_lpm_wake_bt_device() |
| 486 | { |
| 487 | int result = -1; |
| 488 | |
| 489 | if ((bt_hc_if) && (lpm_enabled == TRUE)) |
| 490 | result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT); |
| 491 | |
| 492 | APPL_TRACE_DEBUG1("HC lib lpm assertion return %d", result); |
| 493 | } |
| 494 | #endif // HCILP_INCLUDED |
| 495 | |
| 496 | /****************************************************************************** |
| 497 | ** |
| 498 | ** Function bte_main_hci_send |
| 499 | ** |
| 500 | ** Description BTE MAIN API - This function is called by the upper stack to |
| 501 | ** send an HCI message. The function displays a protocol trace |
| 502 | ** message (if enabled), and then calls the 'transmit' function |
| 503 | ** associated with the currently selected HCI transport |
| 504 | ** |
| 505 | ** Returns None |
| 506 | ** |
| 507 | ******************************************************************************/ |
| 508 | void bte_main_hci_send (BT_HDR *p_msg, UINT16 event) |
| 509 | { |
| 510 | UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ |
| 511 | |
| 512 | p_msg->event = event; |
| 513 | |
| 514 | |
| 515 | if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \ |
| 516 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) |
| 517 | { |
| 518 | if (bt_hc_if) |
| 519 | bt_hc_if->transmit_buf((TRANSAC)p_msg, \ |
| 520 | (char *) (p_msg + 1), \ |
| 521 | p_msg->len); |
| 522 | else |
| 523 | GKI_freebuf(p_msg); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | APPL_TRACE_ERROR0("Invalid Controller ID. Discarding message."); |
| 528 | GKI_freebuf(p_msg); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /****************************************************************************** |
| 533 | ** |
| 534 | ** Function bte_main_post_reset_init |
| 535 | ** |
| 536 | ** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT |
| 537 | ** and shall be automatically called from BTE after HCI_Reset |
| 538 | ** |
| 539 | ** Returns None |
| 540 | ** |
| 541 | ******************************************************************************/ |
| 542 | void bte_main_post_reset_init() |
| 543 | { |
| 544 | BTM_ContinueReset(); |
| 545 | } |
| 546 | |
| 547 | /***************************************************************************** |
| 548 | ** |
| 549 | ** libbt-hci Callback Functions |
| 550 | ** |
| 551 | *****************************************************************************/ |
| 552 | |
| 553 | /****************************************************************************** |
| 554 | ** |
| 555 | ** Function preload_cb |
| 556 | ** |
| 557 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 558 | ** when the libbt-hci completed stack preload process |
| 559 | ** |
| 560 | ** Returns None |
| 561 | ** |
| 562 | ******************************************************************************/ |
| 563 | static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result) |
| 564 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 565 | UNUSED(transac); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 566 | |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 567 | APPL_TRACE_EVENT1("HC preload_cb %d [0:SUCCESS 1:FAIL]", result); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 568 | |
| 569 | if (result == BT_HC_PRELOAD_SUCCESS) |
| 570 | { |
| 571 | preload_stop_wait_timer(); |
| 572 | |
| 573 | /* notify BTU task that libbt-hci is ready */ |
| 574 | GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL); |
| 575 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /****************************************************************************** |
| 579 | ** |
| 580 | ** Function postload_cb |
| 581 | ** |
| 582 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 583 | ** when the libbt-hci lib completed stack postload process |
| 584 | ** |
| 585 | ** Returns None |
| 586 | ** |
| 587 | ******************************************************************************/ |
| 588 | static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result) |
| 589 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 590 | UNUSED(transac); |
| 591 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 592 | APPL_TRACE_EVENT1("HC postload_cb %d", result); |
| 593 | } |
| 594 | |
| 595 | /****************************************************************************** |
| 596 | ** |
| 597 | ** Function lpm_cb |
| 598 | ** |
| 599 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 600 | ** back from the libbt-hci to indicate the current LPM state |
| 601 | ** |
| 602 | ** Returns None |
| 603 | ** |
| 604 | ******************************************************************************/ |
| 605 | static void lpm_cb(bt_hc_lpm_request_result_t result) |
| 606 | { |
| 607 | APPL_TRACE_EVENT1("HC lpm_result_cb %d", result); |
| 608 | lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE; |
| 609 | } |
| 610 | |
| 611 | /****************************************************************************** |
| 612 | ** |
| 613 | ** Function hostwake_ind |
| 614 | ** |
| 615 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 616 | ** from the libbt-hci to indicate the HostWake event |
| 617 | ** |
| 618 | ** Returns None |
| 619 | ** |
| 620 | ******************************************************************************/ |
| 621 | static void hostwake_ind(bt_hc_low_power_event_t event) |
| 622 | { |
| 623 | APPL_TRACE_EVENT1("HC hostwake_ind %d", event); |
| 624 | } |
| 625 | |
| 626 | /****************************************************************************** |
| 627 | ** |
| 628 | ** Function alloc |
| 629 | ** |
| 630 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 631 | ** from the libbt-hci to request for data buffer allocation |
| 632 | ** |
| 633 | ** Returns NULL / pointer to allocated buffer |
| 634 | ** |
| 635 | ******************************************************************************/ |
| 636 | static char *alloc(int size) |
| 637 | { |
| 638 | BT_HDR *p_hdr = NULL; |
| 639 | |
| 640 | /* |
| 641 | APPL_TRACE_DEBUG1("HC alloc size=%d", size); |
| 642 | */ |
| 643 | |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 644 | /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */ |
| 645 | if (size > GKI_MAX_BUF_SIZE) |
| 646 | { |
| 647 | APPL_TRACE_ERROR2("HCI DATA SIZE %d greater than MAX %d", |
| 648 | size, GKI_MAX_BUF_SIZE); |
| 649 | return NULL; |
| 650 | } |
| 651 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 652 | p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size); |
| 653 | |
| 654 | if (p_hdr == NULL) |
| 655 | { |
Ganesh Ganapathi Batta | 7fa4fba | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 656 | APPL_TRACE_WARNING1("alloc returns NO BUFFER! (sz %d)", size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return ((char *) p_hdr); |
| 660 | } |
| 661 | |
| 662 | /****************************************************************************** |
| 663 | ** |
| 664 | ** Function dealloc |
| 665 | ** |
| 666 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 667 | ** from the libbt-hci to release the data buffer allocated |
| 668 | ** through the alloc call earlier |
| 669 | ** |
| 670 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 671 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 672 | ** boundary. The 'p_buf' is not intended to be used here |
| 673 | ** but might point to data portion of data-path buffer. |
| 674 | ** |
| 675 | ** Returns bt_hc_status_t |
| 676 | ** |
| 677 | ******************************************************************************/ |
| 678 | static int dealloc(TRANSAC transac, char *p_buf) |
| 679 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 680 | UNUSED(p_buf); |
| 681 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 682 | GKI_freebuf(transac); |
| 683 | return BT_HC_STATUS_SUCCESS; |
| 684 | } |
| 685 | |
| 686 | /****************************************************************************** |
| 687 | ** |
| 688 | ** Function data_ind |
| 689 | ** |
| 690 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 691 | ** from the libbt-hci to pass in the received HCI packets |
| 692 | ** |
| 693 | ** The core stack is responsible for releasing the data buffer |
| 694 | ** passed in from the libbt-hci once the core stack has done |
| 695 | ** with it. |
| 696 | ** |
| 697 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 698 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 699 | ** boundary. The 'p_buf' and 'len' parameters are not intended |
| 700 | ** to be used here but might point to data portion in data- |
| 701 | ** path buffer and length of valid data respectively. |
| 702 | ** |
| 703 | ** Returns bt_hc_status_t |
| 704 | ** |
| 705 | ******************************************************************************/ |
| 706 | static int data_ind(TRANSAC transac, char *p_buf, int len) |
| 707 | { |
| 708 | BT_HDR *p_msg = (BT_HDR *) transac; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 709 | UNUSED(p_buf); |
| 710 | UNUSED(len); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | APPL_TRACE_DEBUG2("HC data_ind event=0x%04X (len=%d)", p_msg->event, len); |
| 714 | */ |
| 715 | |
| 716 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac); |
| 717 | return BT_HC_STATUS_SUCCESS; |
| 718 | } |
| 719 | |
| 720 | /****************************************************************************** |
| 721 | ** |
| 722 | ** Function tx_result |
| 723 | ** |
| 724 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 725 | ** from the libbt-hci once it has processed/sent the prior data |
| 726 | ** buffer which core stack passed to it through transmit_buf |
| 727 | ** call earlier. |
| 728 | ** |
| 729 | ** The core stack is responsible for releasing the data buffer |
| 730 | ** if it has been completedly processed. |
| 731 | ** |
| 732 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 733 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 734 | ** boundary. The 'p_buf' is not intended to be used here |
| 735 | ** but might point to data portion in data-path buffer. |
| 736 | ** |
| 737 | ** Returns bt_hc_status_t |
| 738 | ** |
| 739 | ******************************************************************************/ |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 740 | 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] | 741 | { |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 742 | UNUSED(p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 743 | /* |
| 744 | APPL_TRACE_DEBUG2("HC tx_result %d (event=%04X)", result, \ |
| 745 | ((BT_HDR *)transac)->event); |
| 746 | */ |
| 747 | |
| 748 | if (result == BT_HC_TX_FRAGMENT) |
| 749 | { |
| 750 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | GKI_freebuf(transac); |
| 755 | } |
| 756 | |
| 757 | return BT_HC_STATUS_SUCCESS; |
| 758 | } |
| 759 | |
| 760 | /***************************************************************************** |
| 761 | ** The libbt-hci Callback Functions Table |
| 762 | *****************************************************************************/ |
| 763 | static const bt_hc_callbacks_t hc_callbacks = { |
| 764 | sizeof(bt_hc_callbacks_t), |
| 765 | preload_cb, |
| 766 | postload_cb, |
| 767 | lpm_cb, |
| 768 | hostwake_ind, |
| 769 | alloc, |
| 770 | dealloc, |
| 771 | data_ind, |
| 772 | tx_result |
| 773 | }; |
| 774 | |