blob: d1f65b24b2122d2fee4a01d4db0ebab40be0c92c [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 Project5738f832012-12-12 16:00:35 -080026#include <assert.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070027#include <cutils/properties.h>
28#include <fcntl.h>
29#include <hardware/bluetooth.h>
Chris Manton18023292014-08-29 09:12:06 -070030#include <pthread.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070031#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070032#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070033#include <time.h>
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070034#include <utils/Log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070036#include "alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "bd.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include "bta_api.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070039#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070040#include "bte.h"
41#include "btu.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070042#include "bt_utils.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070043#include "fixed_queue.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070044#include "gki.h"
Chris Manton18023292014-08-29 09:12:06 -070045#include "hash_functions.h"
46#include "hash_map.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070047#include "hci_layer.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070048#include "osi.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070049#include "thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
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 Viswanadham4c94c5f2014-07-18 15:20:54 -070059/* 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 Project5738f832012-12-12 16:00:35 -080063
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 Chao48ebe2c2013-04-24 11:38:06 -070069/* 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 Project5738f832012-12-12 16:00:35 -080074/*******************************************************************************
75** Local type definitions
76*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070077/* Preload retry control block */
78typedef struct
79{
80 int retry_counts;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070081 alarm_t *alarm;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070082} bt_preload_retry_cb_t;
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
84/******************************************************************************
85** Variables
86******************************************************************************/
87BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
Zhihai Xubad70b12013-06-04 18:21:25 -070088BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */
Andre Eisenbach311e88d2014-09-17 16:40:53 -070089BOOLEAN hci_save_log = FALSE; /* save a copy of the log before starting again */
The Android Open Source Project5738f832012-12-12 16:00:35 -080090char hci_logfile[256] = HCI_LOGGING_FILENAME;
91
Chris Manton860a9af2014-08-27 10:30:47 -070092// Communication queue between btu_task and bta.
93fixed_queue_t *btu_bta_msg_queue;
94
95// Communication queue between btu_task and hci.
96fixed_queue_t *btu_hci_msg_queue;
97
Chris Manton18023292014-08-29 09:12:06 -070098// General timer queue.
99fixed_queue_t *btu_general_alarm_queue;
100hash_map_t *btu_general_alarm_hash_map;
101pthread_mutex_t btu_general_alarm_lock;
102static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 17;
103
104// Oneshot timer queue.
105fixed_queue_t *btu_oneshot_alarm_queue;
106hash_map_t *btu_oneshot_alarm_hash_map;
107pthread_mutex_t btu_oneshot_alarm_lock;
108static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 17;
109
110// l2cap timer queue.
111fixed_queue_t *btu_l2cap_alarm_queue;
112hash_map_t *btu_l2cap_alarm_hash_map;
113pthread_mutex_t btu_l2cap_alarm_lock;
114static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 17;
115
The Android Open Source Project5738f832012-12-12 16:00:35 -0800116/*******************************************************************************
117** Static variables
118*******************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700119static const hci_interface_t *hci;
120static const hci_callbacks_t hci_callbacks;
121static const allocator_t buffer_allocator;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700122static bt_preload_retry_cb_t preload_retry_cb;
Zach Johnson7fa65f72014-08-29 18:20:48 -0700123// Lock to serialize shutdown requests from upper layer.
124static pthread_mutex_t shutdown_lock;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800125
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700126// These are temporary so we can run the new HCI code
127// with the old upper stack.
128static fixed_queue_t *upbound_data;
129static thread_t *dispatch_thread;
130
The Android Open Source Project5738f832012-12-12 16:00:35 -0800131/*******************************************************************************
132** Static functions
133*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700134static void bte_hci_enable(void);
135static void bte_hci_disable(void);
136static void preload_start_wait_timer(void);
137static void preload_stop_wait_timer(void);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700138static void dump_upbound_data_to_btu(fixed_queue_t *queue, void *context);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139
140/*******************************************************************************
141** Externs
142*******************************************************************************/
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700143UINT32 btu_task (UINT32 param);
144void BTE_StartUp(void);
145void BTE_ShutDown(void);
146void BTE_LoadStack(void);
147void BTE_UnloadStack(void);
148void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
149void bte_load_conf(const char *p_path);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700150extern void bte_load_ble_conf(const char *p_path);
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700151bt_bdaddr_t btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152
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")
163UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
164
165/******************************************************************************
166**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800167** Function bte_main_boot_entry
168**
169** Description BTE MAIN API - Entry point for BTE chip/stack initialization
170**
171** Returns None
172**
173******************************************************************************/
174void bte_main_boot_entry(void)
175{
176 /* initialize OS */
177 GKI_init();
178
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700179 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 Nanavati14a559a2014-07-25 22:20:46 -0700194
195 memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));
196 preload_retry_cb.alarm = alarm_new();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197
198 bte_load_conf(BTE_STACK_CONF_FILE);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700199#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
200 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
201#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800202
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 Manton6c25e792014-08-07 16:23:41 -0700207
Zach Johnson7fa65f72014-08-29 18:20:48 -0700208 pthread_mutex_init(&shutdown_lock, NULL);
Chris Manton6c25e792014-08-07 16:23:41 -0700209
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210}
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******************************************************************************/
221void bte_main_shutdown()
222{
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700223 alarm_free(preload_retry_cb.alarm);
224 preload_retry_cb.alarm = NULL;
225
Zach Johnson7fa65f72014-08-29 18:20:48 -0700226 pthread_mutex_destroy(&shutdown_lock);
227
The Android Open Source Project5738f832012-12-12 16:00:35 -0800228 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 Chao48ebe2c2013-04-24 11:38:06 -0700241void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700243 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244
Chris Manton860a9af2014-08-27 10:30:47 -0700245 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
246 btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
247
Chris Manton18023292014-08-29 09:12:06 -0700248 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 Project5738f832012-12-12 16:00:35 -0800263 /* Initialize BTE control block */
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700264 BTE_StartUp();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700266 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 Xu1a558ca2014-01-15 10:28:55 -0800270 bte_hci_enable();
271
Sharvil Nanavatib5382482014-06-29 18:10:15 -0700272 GKI_run();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700273}
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******************************************************************************/
285void bte_main_disable(void)
286{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700287 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700288
289 preload_stop_wait_timer();
290 bte_hci_disable();
291 GKI_destroy_task(BTU_TASK);
Sharvil Nanavatida0446b2014-08-29 18:28:19 -0700292
Chris Manton18023292014-08-29 09:12:06 -0700293 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 Nanavatida0446b2014-08-29 18:28:19 -0700308 BTE_ShutDown();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700309}
310
311/******************************************************************************
312**
Zhihai Xubad70b12013-06-04 18:21:25 -0700313** Function bte_main_config_hci_logging
314**
315** Description enable or disable HIC snoop logging
316**
317** Returns None
318**
319******************************************************************************/
320void 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700325 hci_logging_config = enable;
Zhihai Xubad70b12013-06-04 18:21:25 -0700326
327 new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
328
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700329 if ((old == new) || bt_disabled) {
Zhihai Xubad70b12013-06-04 18:21:25 -0700330 return;
331 }
332
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700333 if (new)
334 hci->turn_on_logging(hci_logfile);
335 else
336 hci->turn_off_logging();
Zhihai Xubad70b12013-06-04 18:21:25 -0700337}
338
339/******************************************************************************
340**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700341** Function bte_hci_enable
342**
343** Description Enable HCI & Vendor modules
344**
345** Returns None
346**
347******************************************************************************/
348static void bte_hci_enable(void)
349{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700350 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700351
352 preload_start_wait_timer();
353
Zach Johnson7fa65f72014-08-29 18:20:48 -0700354 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 Project5738f832012-12-12 16:00:35 -0800356
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700357 assert(success);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800358
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700359 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
360 hci->turn_on_logging(hci_logfile);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361
362#if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700363 APPL_TRACE_DEBUG("%s not turning off the chip before turning it on", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800364
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700365 /* 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 Project5738f832012-12-12 16:00:35 -0800367
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700368 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 Project5738f832012-12-12 16:00:35 -0800372
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700373 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 Project5738f832012-12-12 16:00:35 -0800375#else
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700376 /* 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 Project5738f832012-12-12 16:00:35 -0800379#endif
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700380 hci->set_chip_power_on(true);
381 hci->do_preload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800382}
383
384/******************************************************************************
385**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700386** Function bte_hci_disable
The Android Open Source Project5738f832012-12-12 16:00:35 -0800387**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700388** Description Disable HCI & Vendor modules
The Android Open Source Project5738f832012-12-12 16:00:35 -0800389**
390** Returns None
391**
392******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700393static void bte_hci_disable(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800394{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700395 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700397 if (!hci)
Chris Manton6c25e792014-08-07 16:23:41 -0700398 return;
399
Zach Johnson7fa65f72014-08-29 18:20:48 -0700400 // Shutdown is not thread safe and must be protected.
401 pthread_mutex_lock(&shutdown_lock);
Chris Manton6c25e792014-08-07 16:23:41 -0700402
403 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700404 hci->turn_off_logging();
Zach Johnson7fa65f72014-08-29 18:20:48 -0700405 hci->shut_down();
Chris Manton6c25e792014-08-07 16:23:41 -0700406
Zach Johnson7fa65f72014-08-29 18:20:48 -0700407 pthread_mutex_unlock(&shutdown_lock);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700408}
409
410/*******************************************************************************
411**
412** Function preload_wait_timeout
413**
414** Description Timeout thread of preload watchdog timer
415**
416** Returns None
417**
418*******************************************************************************/
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700419static void preload_wait_timeout(UNUSED_ATTR void *context)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700420{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700421 APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...",
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700422 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*******************************************************************************/
447static void preload_start_wait_timer(void)
448{
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700449 uint32_t timeout_ms;
Sharvil Nanavatib67820c2014-01-20 18:20:28 -0800450 char timeout_prop[PROPERTY_VALUE_MAX];
Sharvil Nanavatib67820c2014-01-20 18:20:28 -0800451 if (!property_get("bluetooth.enable_timeout_ms", timeout_prop, "3000") || (timeout_ms = atoi(timeout_prop)) < 100)
452 timeout_ms = 3000;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700453
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700454 alarm_set(preload_retry_cb.alarm, timeout_ms, preload_wait_timeout, NULL);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700455}
456
457/*******************************************************************************
458**
459** Function preload_stop_wait_timer
460**
461** Description Stop preload watchdog timer
462**
463** Returns None
464**
465*******************************************************************************/
466static void preload_stop_wait_timer(void)
467{
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700468 alarm_cancel(preload_retry_cb.alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800469}
470
471/******************************************************************************
472**
473** Function bte_main_postload_cfg
474**
475** Description BTE MAIN API - Stack postload configuration
476**
477** Returns None
478**
479******************************************************************************/
480void bte_main_postload_cfg(void)
481{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700482 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483}
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******************************************************************************/
495void bte_main_enable_lpm(BOOLEAN enable)
496{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700497 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800498}
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******************************************************************************/
509void bte_main_lpm_allow_bt_device_sleep()
510{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700511 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512}
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******************************************************************************/
523void bte_main_lpm_wake_bt_device()
524{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700525 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526}
527#endif // HCILP_INCLUDED
528
Matthew Xie66432dc2014-04-27 05:45:32 -0700529
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 */
534typedef struct {
535 UINT16 handle;
536 UINT16 peer_codec;
537 UINT16 state;
538} bt_hc_audio_state_t;
539
540struct 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******************************************************************************/
559int 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700581 hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
Matthew Xie66432dc2014-04-27 05:45:32 -0700582 return result;
583}
584
585
The Android Open Source Project5738f832012-12-12 16:00:35 -0800586/******************************************************************************
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******************************************************************************/
598void 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700608 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800609 }
610 else
611 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700612 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800613 GKI_freebuf(p_msg);
614 }
615}
616
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617/*****************************************************************************
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 Johnsonfbbd42b2014-08-15 17:00:17 -0700633static void preload_cb(bool success)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800634{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700635 APPL_TRACE_EVENT("HC preload_cb %d [1:SUCCESS 0:FAIL]", success);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700637 if (success)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700638 {
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 Project5738f832012-12-12 16:00:35 -0800644}
645
646/******************************************************************************
647**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648** 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700656static void *alloc(size_t size)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800657{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700658 /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */
659 if (size > GKI_MAX_BUF_SIZE)
660 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700661 APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700662 size, GKI_MAX_BUF_SIZE);
663 return NULL;
664 }
665
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700666 BT_HDR *p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800667
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700668 if (!p_hdr)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800669 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700670 APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671 }
672
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700673 return p_hdr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674}
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 Nanavati75e8f412014-06-24 17:02:30 -0700686** boundary.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687**
688******************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700689static void dealloc(void *buffer)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700691 GKI_freebuf(buffer);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800692}
693
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700694static void dump_upbound_data_to_btu(fixed_queue_t *queue, UNUSED_ATTR void *context) {
Chris Manton860a9af2014-08-27 10:30:47 -0700695 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 Project5738f832012-12-12 16:00:35 -0800699}
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 Johnsonfbbd42b2014-08-15 17:00:17 -0700721static void tx_result(void *p_buf, bool all_fragments_sent)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700723 if (!all_fragments_sent)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800724 {
Chris Manton860a9af2014-08-27 10:30:47 -0700725 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 Project5738f832012-12-12 16:00:35 -0800728 }
729 else
730 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700731 GKI_freebuf(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800733}
734
735/*****************************************************************************
736** The libbt-hci Callback Functions Table
737*****************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700738static const hci_callbacks_t hci_callbacks = {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800739 preload_cb,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740 tx_result
741};
742
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700743static const allocator_t buffer_allocator = {
744 alloc,
745 dealloc
746};
747