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