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 | ******************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 26 | #include <assert.h> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <hardware/bluetooth.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 30 | #include <signal.h> |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 32 | #include <time.h> |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 33 | #include <utils/Log.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 34 | |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 35 | #include "alarm.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 36 | #include "bd.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 37 | #include "bta_api.h" |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 38 | #include "bt_hci_bdroid.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 39 | #include "bte.h" |
| 40 | #include "btu.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 41 | #include "bt_utils.h" |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 42 | #include "fixed_queue.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 43 | #include "gki.h" |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 44 | #include "hci_layer.h" |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 45 | #include "osi.h" |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 46 | #include "thread.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 47 | |
| 48 | /******************************************************************************* |
| 49 | ** Constants & Macros |
| 50 | *******************************************************************************/ |
| 51 | |
| 52 | /* Run-time configuration file */ |
| 53 | #ifndef BTE_STACK_CONF_FILE |
| 54 | #define BTE_STACK_CONF_FILE "/etc/bluetooth/bt_stack.conf" |
| 55 | #endif |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 56 | /* Run-time configuration file for BLE*/ |
| 57 | #ifndef BTE_BLE_STACK_CONF_FILE |
| 58 | #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf" |
| 59 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 60 | |
| 61 | /* if not specified in .txt file then use this as default */ |
| 62 | #ifndef HCI_LOGGING_FILENAME |
| 63 | #define HCI_LOGGING_FILENAME "/data/misc/bluedroid/btsnoop_hci.log" |
| 64 | #endif |
| 65 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 66 | /* Stack preload process maximum retry attempts */ |
| 67 | #ifndef PRELOAD_MAX_RETRY_ATTEMPTS |
| 68 | #define PRELOAD_MAX_RETRY_ATTEMPTS 0 |
| 69 | #endif |
| 70 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 71 | /******************************************************************************* |
| 72 | ** Local type definitions |
| 73 | *******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 74 | /* Preload retry control block */ |
| 75 | typedef struct |
| 76 | { |
| 77 | int retry_counts; |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 78 | alarm_t *alarm; |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 79 | } bt_preload_retry_cb_t; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 80 | |
| 81 | /****************************************************************************** |
| 82 | ** Variables |
| 83 | ******************************************************************************/ |
| 84 | BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */ |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 85 | BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */ |
Andre Eisenbach | 311e88d | 2014-09-17 16:40:53 -0700 | [diff] [blame] | 86 | BOOLEAN hci_save_log = FALSE; /* save a copy of the log before starting again */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 87 | char hci_logfile[256] = HCI_LOGGING_FILENAME; |
| 88 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | /******************************************************************************* |
| 90 | ** Static variables |
| 91 | *******************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 92 | static const hci_interface_t *hci; |
| 93 | static const hci_callbacks_t hci_callbacks; |
| 94 | static const allocator_t buffer_allocator; |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 95 | static bt_preload_retry_cb_t preload_retry_cb; |
Chris Manton | 6c25e79 | 2014-08-07 16:23:41 -0700 | [diff] [blame] | 96 | // Lock to serialize cleanup requests from upper layer. |
| 97 | static pthread_mutex_t cleanup_lock; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 98 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 99 | // These are temporary so we can run the new HCI code |
| 100 | // with the old upper stack. |
| 101 | static fixed_queue_t *upbound_data; |
| 102 | static thread_t *dispatch_thread; |
| 103 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 104 | /******************************************************************************* |
| 105 | ** Static functions |
| 106 | *******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 107 | static void bte_hci_enable(void); |
| 108 | static void bte_hci_disable(void); |
| 109 | static void preload_start_wait_timer(void); |
| 110 | static void preload_stop_wait_timer(void); |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 111 | static void dump_upbound_data_to_btu(fixed_queue_t *queue, void *context); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 112 | |
| 113 | /******************************************************************************* |
| 114 | ** Externs |
| 115 | *******************************************************************************/ |
| 116 | BTU_API extern UINT32 btu_task (UINT32 param); |
| 117 | BTU_API extern void BTE_Init (void); |
| 118 | BT_API extern void BTE_LoadStack(void); |
| 119 | BT_API void BTE_UnloadStack(void); |
| 120 | extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src); |
| 121 | extern void bte_load_conf(const char *p_path); |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 122 | extern void bte_load_ble_conf(const char *p_path); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 123 | extern bt_bdaddr_t btif_local_bd_addr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 124 | |
| 125 | |
| 126 | /******************************************************************************* |
| 127 | ** System Task Configuration |
| 128 | *******************************************************************************/ |
| 129 | |
| 130 | /* bluetooth protocol stack (BTU) task */ |
| 131 | #ifndef BTE_BTU_STACK_SIZE |
| 132 | #define BTE_BTU_STACK_SIZE 0//0x2000 /* In bytes */ |
| 133 | #endif |
| 134 | #define BTE_BTU_TASK_STR ((INT8 *) "BTU") |
| 135 | UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4]; |
| 136 | |
| 137 | /****************************************************************************** |
| 138 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 139 | ** Function bte_main_boot_entry |
| 140 | ** |
| 141 | ** Description BTE MAIN API - Entry point for BTE chip/stack initialization |
| 142 | ** |
| 143 | ** Returns None |
| 144 | ** |
| 145 | ******************************************************************************/ |
| 146 | void bte_main_boot_entry(void) |
| 147 | { |
| 148 | /* initialize OS */ |
| 149 | GKI_init(); |
| 150 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 151 | hci = hci_layer_get_interface(); |
| 152 | if (!hci) |
| 153 | ALOGE("%s could not get hci layer interface.", __func__); |
| 154 | |
| 155 | upbound_data = fixed_queue_new(SIZE_MAX); |
| 156 | dispatch_thread = thread_new("hci_dispatch"); |
| 157 | |
| 158 | fixed_queue_register_dequeue( |
| 159 | upbound_data, |
| 160 | thread_get_reactor(dispatch_thread), |
| 161 | dump_upbound_data_to_btu, |
| 162 | NULL |
| 163 | ); |
| 164 | |
| 165 | data_dispatcher_register_default(hci->upward_dispatcher, upbound_data); |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 166 | |
| 167 | memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t)); |
| 168 | preload_retry_cb.alarm = alarm_new(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 169 | |
| 170 | bte_load_conf(BTE_STACK_CONF_FILE); |
Prerepa Viswanadham | 4c94c5f | 2014-07-18 15:20:54 -0700 | [diff] [blame] | 171 | #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) |
| 172 | bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE); |
| 173 | #endif |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 174 | |
| 175 | #if (BTTRC_INCLUDED == TRUE) |
| 176 | /* Initialize trace feature */ |
| 177 | BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM); |
| 178 | #endif |
Chris Manton | 6c25e79 | 2014-08-07 16:23:41 -0700 | [diff] [blame] | 179 | |
| 180 | pthread_mutex_init(&cleanup_lock, NULL); |
| 181 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /****************************************************************************** |
| 185 | ** |
| 186 | ** Function bte_main_shutdown |
| 187 | ** |
| 188 | ** Description BTE MAIN API - Shutdown code for BTE chip/stack |
| 189 | ** |
| 190 | ** Returns None |
| 191 | ** |
| 192 | ******************************************************************************/ |
| 193 | void bte_main_shutdown() |
| 194 | { |
Chris Manton | 6c25e79 | 2014-08-07 16:23:41 -0700 | [diff] [blame] | 195 | pthread_mutex_destroy(&cleanup_lock); |
| 196 | |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 197 | alarm_free(preload_retry_cb.alarm); |
| 198 | preload_retry_cb.alarm = NULL; |
| 199 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 200 | GKI_shutdown(); |
| 201 | } |
| 202 | |
| 203 | /****************************************************************************** |
| 204 | ** |
| 205 | ** Function bte_main_enable |
| 206 | ** |
| 207 | ** Description BTE MAIN API - Creates all the BTE tasks. Should be called |
| 208 | ** part of the Bluetooth stack enable sequence |
| 209 | ** |
| 210 | ** Returns None |
| 211 | ** |
| 212 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 213 | void bte_main_enable() |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 214 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 215 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 216 | |
| 217 | /* Initialize BTE control block */ |
| 218 | BTE_Init(); |
| 219 | |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 220 | GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR, |
| 221 | (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE), |
| 222 | sizeof(bte_btu_stack)); |
| 223 | |
Zhihai Xu | 1a558ca | 2014-01-15 10:28:55 -0800 | [diff] [blame] | 224 | bte_hci_enable(); |
| 225 | |
Sharvil Nanavati | b538248 | 2014-06-29 18:10:15 -0700 | [diff] [blame] | 226 | GKI_run(); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /****************************************************************************** |
| 230 | ** |
| 231 | ** Function bte_main_disable |
| 232 | ** |
| 233 | ** Description BTE MAIN API - Destroys all the BTE tasks. Should be called |
| 234 | ** part of the Bluetooth stack disable sequence |
| 235 | ** |
| 236 | ** Returns None |
| 237 | ** |
| 238 | ******************************************************************************/ |
| 239 | void bte_main_disable(void) |
| 240 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 241 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 242 | |
| 243 | preload_stop_wait_timer(); |
| 244 | bte_hci_disable(); |
| 245 | GKI_destroy_task(BTU_TASK); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /****************************************************************************** |
| 249 | ** |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 250 | ** Function bte_main_config_hci_logging |
| 251 | ** |
| 252 | ** Description enable or disable HIC snoop logging |
| 253 | ** |
| 254 | ** Returns None |
| 255 | ** |
| 256 | ******************************************************************************/ |
| 257 | void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled) |
| 258 | { |
| 259 | int old = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE); |
| 260 | int new; |
| 261 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 262 | hci_logging_config = enable; |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 263 | |
| 264 | new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE); |
| 265 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 266 | if ((old == new) || bt_disabled) { |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 270 | if (new) |
| 271 | hci->turn_on_logging(hci_logfile); |
| 272 | else |
| 273 | hci->turn_off_logging(); |
Zhihai Xu | bad70b1 | 2013-06-04 18:21:25 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /****************************************************************************** |
| 277 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 278 | ** Function bte_hci_enable |
| 279 | ** |
| 280 | ** Description Enable HCI & Vendor modules |
| 281 | ** |
| 282 | ** Returns None |
| 283 | ** |
| 284 | ******************************************************************************/ |
| 285 | static void bte_hci_enable(void) |
| 286 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 287 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 288 | |
| 289 | preload_start_wait_timer(); |
| 290 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 291 | bool success = hci->init(btif_local_bd_addr.address, &buffer_allocator, &hci_callbacks); |
| 292 | APPL_TRACE_EVENT("libbt-hci init returns %d", success); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 293 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 294 | assert(success); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 295 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 296 | if (hci_logging_enabled == TRUE || hci_logging_config == TRUE) |
| 297 | hci->turn_on_logging(hci_logfile); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 298 | |
| 299 | #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE) |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 300 | APPL_TRACE_DEBUG("%s not turning off the chip before turning it on", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 301 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 302 | /* Do not power off the chip before powering on if BT_CLEAN_TURN_ON_DISABLED flag |
| 303 | is defined and set to TRUE to avoid below mentioned issue. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 304 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 305 | Wingray kernel driver maintains a combined counter to keep track of |
| 306 | BT-Wifi state. Invoking set_power(BT_HC_CHIP_PWR_OFF) when the BT is already |
| 307 | in OFF state causes this counter to be incorrectly decremented and results in undesired |
| 308 | behavior of the chip. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 309 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 310 | This is only a workaround and when the issue is fixed in the kernel this work around |
| 311 | should be removed. */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 312 | #else |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 313 | /* toggle chip power to ensure we will reset chip in case |
| 314 | a previous stack shutdown wasn't completed gracefully */ |
| 315 | hci->set_chip_power_on(false); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 316 | #endif |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 317 | hci->set_chip_power_on(true); |
| 318 | hci->do_preload(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /****************************************************************************** |
| 322 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 323 | ** Function bte_hci_disable |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 324 | ** |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 325 | ** Description Disable HCI & Vendor modules |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 326 | ** |
| 327 | ** Returns None |
| 328 | ** |
| 329 | ******************************************************************************/ |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 330 | static void bte_hci_disable(void) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 331 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 332 | APPL_TRACE_DEBUG("%s", __FUNCTION__); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 333 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 334 | if (!hci) |
Chris Manton | 6c25e79 | 2014-08-07 16:23:41 -0700 | [diff] [blame] | 335 | return; |
| 336 | |
| 337 | // Cleanup is not thread safe and must be protected. |
| 338 | pthread_mutex_lock(&cleanup_lock); |
| 339 | |
| 340 | if (hci_logging_enabled == TRUE || hci_logging_config == TRUE) |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 341 | hci->turn_off_logging(); |
| 342 | hci->cleanup(); |
Chris Manton | 6c25e79 | 2014-08-07 16:23:41 -0700 | [diff] [blame] | 343 | |
| 344 | pthread_mutex_unlock(&cleanup_lock); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /******************************************************************************* |
| 348 | ** |
| 349 | ** Function preload_wait_timeout |
| 350 | ** |
| 351 | ** Description Timeout thread of preload watchdog timer |
| 352 | ** |
| 353 | ** Returns None |
| 354 | ** |
| 355 | *******************************************************************************/ |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 356 | static void preload_wait_timeout(UNUSED_ATTR void *context) |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 357 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 358 | APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...", |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 359 | preload_retry_cb.retry_counts, |
| 360 | PRELOAD_MAX_RETRY_ATTEMPTS); |
| 361 | |
| 362 | if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS) |
| 363 | { |
| 364 | bte_hci_disable(); |
| 365 | GKI_delay(100); |
| 366 | bte_hci_enable(); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | /* Notify BTIF_TASK that the init procedure had failed*/ |
| 371 | GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /******************************************************************************* |
| 376 | ** |
| 377 | ** Function preload_start_wait_timer |
| 378 | ** |
| 379 | ** Description Launch startup watchdog timer |
| 380 | ** |
| 381 | ** Returns None |
| 382 | ** |
| 383 | *******************************************************************************/ |
| 384 | static void preload_start_wait_timer(void) |
| 385 | { |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 386 | uint32_t timeout_ms; |
Sharvil Nanavati | b67820c | 2014-01-20 18:20:28 -0800 | [diff] [blame] | 387 | char timeout_prop[PROPERTY_VALUE_MAX]; |
Sharvil Nanavati | b67820c | 2014-01-20 18:20:28 -0800 | [diff] [blame] | 388 | if (!property_get("bluetooth.enable_timeout_ms", timeout_prop, "3000") || (timeout_ms = atoi(timeout_prop)) < 100) |
| 389 | timeout_ms = 3000; |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 390 | |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 391 | alarm_set(preload_retry_cb.alarm, timeout_ms, preload_wait_timeout, NULL); |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /******************************************************************************* |
| 395 | ** |
| 396 | ** Function preload_stop_wait_timer |
| 397 | ** |
| 398 | ** Description Stop preload watchdog timer |
| 399 | ** |
| 400 | ** Returns None |
| 401 | ** |
| 402 | *******************************************************************************/ |
| 403 | static void preload_stop_wait_timer(void) |
| 404 | { |
Sharvil Nanavati | 14a559a | 2014-07-25 22:20:46 -0700 | [diff] [blame] | 405 | alarm_cancel(preload_retry_cb.alarm); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /****************************************************************************** |
| 409 | ** |
| 410 | ** Function bte_main_postload_cfg |
| 411 | ** |
| 412 | ** Description BTE MAIN API - Stack postload configuration |
| 413 | ** |
| 414 | ** Returns None |
| 415 | ** |
| 416 | ******************************************************************************/ |
| 417 | void bte_main_postload_cfg(void) |
| 418 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 419 | hci->do_postload(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE) |
| 423 | /****************************************************************************** |
| 424 | ** |
| 425 | ** Function bte_main_enable_lpm |
| 426 | ** |
| 427 | ** Description BTE MAIN API - Enable/Disable low power mode operation |
| 428 | ** |
| 429 | ** Returns None |
| 430 | ** |
| 431 | ******************************************************************************/ |
| 432 | void bte_main_enable_lpm(BOOLEAN enable) |
| 433 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 434 | hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /****************************************************************************** |
| 438 | ** |
| 439 | ** Function bte_main_lpm_allow_bt_device_sleep |
| 440 | ** |
| 441 | ** Description BTE MAIN API - Allow BT controller goest to sleep |
| 442 | ** |
| 443 | ** Returns None |
| 444 | ** |
| 445 | ******************************************************************************/ |
| 446 | void bte_main_lpm_allow_bt_device_sleep() |
| 447 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 448 | hci->send_low_power_command(LPM_WAKE_DEASSERT); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /****************************************************************************** |
| 452 | ** |
| 453 | ** Function bte_main_lpm_wake_bt_device |
| 454 | ** |
| 455 | ** Description BTE MAIN API - Wake BT controller up if it is in sleep mode |
| 456 | ** |
| 457 | ** Returns None |
| 458 | ** |
| 459 | ******************************************************************************/ |
| 460 | void bte_main_lpm_wake_bt_device() |
| 461 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 462 | hci->send_low_power_command(LPM_WAKE_ASSERT); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 463 | } |
| 464 | #endif // HCILP_INCLUDED |
| 465 | |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 466 | |
| 467 | /* NOTICE: |
| 468 | * Definitions for audio state structure, this type needs to match to |
| 469 | * the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h |
| 470 | */ |
| 471 | typedef struct { |
| 472 | UINT16 handle; |
| 473 | UINT16 peer_codec; |
| 474 | UINT16 state; |
| 475 | } bt_hc_audio_state_t; |
| 476 | |
| 477 | struct bt_audio_state_tag { |
| 478 | BT_HDR hdr; |
| 479 | bt_hc_audio_state_t audio; |
| 480 | }; |
| 481 | |
| 482 | /****************************************************************************** |
| 483 | ** |
| 484 | ** Function set_audio_state |
| 485 | ** |
| 486 | ** Description Sets audio state on controller state for SCO (PCM, WBS, FM) |
| 487 | ** |
| 488 | ** Parameters handle: codec related handle for SCO: sco cb idx, unused for |
| 489 | ** codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec |
| 490 | ** state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP |
| 491 | ** param: future extensions, e.g. call-in structure/event. |
| 492 | ** |
| 493 | ** Returns None |
| 494 | ** |
| 495 | ******************************************************************************/ |
| 496 | int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param) |
| 497 | { |
| 498 | struct bt_audio_state_tag *p_msg; |
| 499 | int result = -1; |
| 500 | |
| 501 | APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle, |
| 502 | codec, state); |
| 503 | if (NULL != param) |
| 504 | APPL_TRACE_WARNING("set_audio_state() non-null param not supported"); |
| 505 | p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg)); |
| 506 | if (!p_msg) |
| 507 | return result; |
| 508 | p_msg->audio.handle = handle; |
| 509 | p_msg->audio.peer_codec = codec; |
| 510 | p_msg->audio.state = state; |
| 511 | |
| 512 | p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE); |
| 513 | p_msg->hdr.len = sizeof(p_msg->audio); |
| 514 | p_msg->hdr.offset = 0; |
| 515 | /* layer_specific shall contain return path event! for BTA events! |
| 516 | * 0 means no return message is expected. */ |
| 517 | p_msg->hdr.layer_specific = 0; |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 518 | hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg); |
Matthew Xie | 66432dc | 2014-04-27 05:45:32 -0700 | [diff] [blame] | 519 | return result; |
| 520 | } |
| 521 | |
| 522 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 523 | /****************************************************************************** |
| 524 | ** |
| 525 | ** Function bte_main_hci_send |
| 526 | ** |
| 527 | ** Description BTE MAIN API - This function is called by the upper stack to |
| 528 | ** send an HCI message. The function displays a protocol trace |
| 529 | ** message (if enabled), and then calls the 'transmit' function |
| 530 | ** associated with the currently selected HCI transport |
| 531 | ** |
| 532 | ** Returns None |
| 533 | ** |
| 534 | ******************************************************************************/ |
| 535 | void bte_main_hci_send (BT_HDR *p_msg, UINT16 event) |
| 536 | { |
| 537 | UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */ |
| 538 | |
| 539 | p_msg->event = event; |
| 540 | |
| 541 | |
| 542 | if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \ |
| 543 | (sub_event == LOCAL_BLE_CONTROLLER_ID)) |
| 544 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 545 | hci->transmit_downward(event, p_msg); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 546 | } |
| 547 | else |
| 548 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 549 | APPL_TRACE_ERROR("Invalid Controller ID. Discarding message."); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 550 | GKI_freebuf(p_msg); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /****************************************************************************** |
| 555 | ** |
| 556 | ** Function bte_main_post_reset_init |
| 557 | ** |
| 558 | ** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT |
| 559 | ** and shall be automatically called from BTE after HCI_Reset |
| 560 | ** |
| 561 | ** Returns None |
| 562 | ** |
| 563 | ******************************************************************************/ |
| 564 | void bte_main_post_reset_init() |
| 565 | { |
| 566 | BTM_ContinueReset(); |
| 567 | } |
| 568 | |
| 569 | /***************************************************************************** |
| 570 | ** |
| 571 | ** libbt-hci Callback Functions |
| 572 | ** |
| 573 | *****************************************************************************/ |
| 574 | |
| 575 | /****************************************************************************** |
| 576 | ** |
| 577 | ** Function preload_cb |
| 578 | ** |
| 579 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 580 | ** when the libbt-hci completed stack preload process |
| 581 | ** |
| 582 | ** Returns None |
| 583 | ** |
| 584 | ******************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 585 | static void preload_cb(bool success) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 586 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 587 | APPL_TRACE_EVENT("HC preload_cb %d [1:SUCCESS 0:FAIL]", success); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 588 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 589 | if (success) |
YK Jeffrey Chao | 48ebe2c | 2013-04-24 11:38:06 -0700 | [diff] [blame] | 590 | { |
| 591 | preload_stop_wait_timer(); |
| 592 | |
| 593 | /* notify BTU task that libbt-hci is ready */ |
| 594 | GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL); |
| 595 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /****************************************************************************** |
| 599 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 600 | ** Function alloc |
| 601 | ** |
| 602 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 603 | ** from the libbt-hci to request for data buffer allocation |
| 604 | ** |
| 605 | ** Returns NULL / pointer to allocated buffer |
| 606 | ** |
| 607 | ******************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 608 | static void *alloc(size_t size) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 609 | { |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 610 | /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */ |
| 611 | if (size > GKI_MAX_BUF_SIZE) |
| 612 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 613 | APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d", |
Ganesh Ganapathi Batta | 8fe5887 | 2014-04-16 16:50:09 -0700 | [diff] [blame] | 614 | size, GKI_MAX_BUF_SIZE); |
| 615 | return NULL; |
| 616 | } |
| 617 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 618 | BT_HDR *p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 619 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 620 | if (!p_hdr) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 621 | { |
Sharvil Nanavati | e8c3d75 | 2014-05-04 10:12:26 -0700 | [diff] [blame] | 622 | 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] | 623 | } |
| 624 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 625 | return p_hdr; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /****************************************************************************** |
| 629 | ** |
| 630 | ** Function dealloc |
| 631 | ** |
| 632 | ** Description HOST/CONTROLLER LIB CALLOUT API - This function is called |
| 633 | ** from the libbt-hci to release the data buffer allocated |
| 634 | ** through the alloc call earlier |
| 635 | ** |
| 636 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 637 | ** pass data-path buffer/packet across bt_hci_lib interface |
Sharvil Nanavati | 75e8f41 | 2014-06-24 17:02:30 -0700 | [diff] [blame] | 638 | ** boundary. |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 639 | ** |
| 640 | ******************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 641 | static void dealloc(void *buffer) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 642 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 643 | GKI_freebuf(buffer); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 646 | static void dump_upbound_data_to_btu(fixed_queue_t *queue, UNUSED_ATTR void *context) { |
| 647 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, fixed_queue_dequeue(queue)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /****************************************************************************** |
| 651 | ** |
| 652 | ** Function tx_result |
| 653 | ** |
| 654 | ** Description HOST/CONTROLLER LIB CALLBACK API - This function is called |
| 655 | ** from the libbt-hci once it has processed/sent the prior data |
| 656 | ** buffer which core stack passed to it through transmit_buf |
| 657 | ** call earlier. |
| 658 | ** |
| 659 | ** The core stack is responsible for releasing the data buffer |
| 660 | ** if it has been completedly processed. |
| 661 | ** |
| 662 | ** Bluedroid libbt-hci library uses 'transac' parameter to |
| 663 | ** pass data-path buffer/packet across bt_hci_lib interface |
| 664 | ** boundary. The 'p_buf' is not intended to be used here |
| 665 | ** but might point to data portion in data-path buffer. |
| 666 | ** |
| 667 | ** Returns bt_hc_status_t |
| 668 | ** |
| 669 | ******************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 670 | static void tx_result(void *p_buf, bool all_fragments_sent) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 671 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 672 | if (!all_fragments_sent) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 673 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 674 | GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 675 | } |
| 676 | else |
| 677 | { |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 678 | GKI_freebuf(p_buf); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 679 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /***************************************************************************** |
| 683 | ** The libbt-hci Callback Functions Table |
| 684 | *****************************************************************************/ |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 685 | static const hci_callbacks_t hci_callbacks = { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 686 | preload_cb, |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 687 | tx_result |
| 688 | }; |
| 689 | |
Zach Johnson | fbbd42b | 2014-08-15 17:00:17 -0700 | [diff] [blame^] | 690 | static const allocator_t buffer_allocator = { |
| 691 | alloc, |
| 692 | dealloc |
| 693 | }; |
| 694 | |