blob: 296263d9d09902776cfcf20c55ee7c052d9c580e [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>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070030#include <signal.h>
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070031#include <stdlib.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070032#include <time.h>
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070033#include <utils/Log.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080034
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070035#include "alarm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include "bd.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "bta_api.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070038#include "bt_hci_bdroid.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070039#include "bte.h"
40#include "btu.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070041#include "bt_utils.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070042#include "fixed_queue.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070043#include "gki.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070044#include "hci_layer.h"
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070045#include "osi.h"
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070046#include "thread.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047
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 Viswanadham4c94c5f2014-07-18 15:20:54 -070056/* 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 Project5738f832012-12-12 16:00:35 -080060
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 Chao48ebe2c2013-04-24 11:38:06 -070066/* 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 Project5738f832012-12-12 16:00:35 -080071/*******************************************************************************
72** Local type definitions
73*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070074/* Preload retry control block */
75typedef struct
76{
77 int retry_counts;
Sharvil Nanavati14a559a2014-07-25 22:20:46 -070078 alarm_t *alarm;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070079} bt_preload_retry_cb_t;
The Android Open Source Project5738f832012-12-12 16:00:35 -080080
81/******************************************************************************
82** Variables
83******************************************************************************/
84BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
Zhihai Xubad70b12013-06-04 18:21:25 -070085BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */
Andre Eisenbach311e88d2014-09-17 16:40:53 -070086BOOLEAN hci_save_log = FALSE; /* save a copy of the log before starting again */
The Android Open Source Project5738f832012-12-12 16:00:35 -080087char hci_logfile[256] = HCI_LOGGING_FILENAME;
88
The Android Open Source Project5738f832012-12-12 16:00:35 -080089/*******************************************************************************
90** Static variables
91*******************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070092static const hci_interface_t *hci;
93static const hci_callbacks_t hci_callbacks;
94static const allocator_t buffer_allocator;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070095static bt_preload_retry_cb_t preload_retry_cb;
Chris Manton6c25e792014-08-07 16:23:41 -070096// Lock to serialize cleanup requests from upper layer.
97static pthread_mutex_t cleanup_lock;
The Android Open Source Project5738f832012-12-12 16:00:35 -080098
Zach Johnsonfbbd42b2014-08-15 17:00:17 -070099// These are temporary so we can run the new HCI code
100// with the old upper stack.
101static fixed_queue_t *upbound_data;
102static thread_t *dispatch_thread;
103
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104/*******************************************************************************
105** Static functions
106*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700107static void bte_hci_enable(void);
108static void bte_hci_disable(void);
109static void preload_start_wait_timer(void);
110static void preload_stop_wait_timer(void);
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700111static void dump_upbound_data_to_btu(fixed_queue_t *queue, void *context);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112
113/*******************************************************************************
114** Externs
115*******************************************************************************/
116BTU_API extern UINT32 btu_task (UINT32 param);
117BTU_API extern void BTE_Init (void);
118BT_API extern void BTE_LoadStack(void);
119BT_API void BTE_UnloadStack(void);
120extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
121extern void bte_load_conf(const char *p_path);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700122extern void bte_load_ble_conf(const char *p_path);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700123extern bt_bdaddr_t btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124
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")
135UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
136
137/******************************************************************************
138**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139** Function bte_main_boot_entry
140**
141** Description BTE MAIN API - Entry point for BTE chip/stack initialization
142**
143** Returns None
144**
145******************************************************************************/
146void bte_main_boot_entry(void)
147{
148 /* initialize OS */
149 GKI_init();
150
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700151 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 Nanavati14a559a2014-07-25 22:20:46 -0700166
167 memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));
168 preload_retry_cb.alarm = alarm_new();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800169
170 bte_load_conf(BTE_STACK_CONF_FILE);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700171#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
172 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
173#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174
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 Manton6c25e792014-08-07 16:23:41 -0700179
180 pthread_mutex_init(&cleanup_lock, NULL);
181
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182}
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******************************************************************************/
193void bte_main_shutdown()
194{
Chris Manton6c25e792014-08-07 16:23:41 -0700195 pthread_mutex_destroy(&cleanup_lock);
196
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700197 alarm_free(preload_retry_cb.alarm);
198 preload_retry_cb.alarm = NULL;
199
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200 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 Chao48ebe2c2013-04-24 11:38:06 -0700213void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700215 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800216
217 /* Initialize BTE control block */
218 BTE_Init();
219
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700220 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 Xu1a558ca2014-01-15 10:28:55 -0800224 bte_hci_enable();
225
Sharvil Nanavatib5382482014-06-29 18:10:15 -0700226 GKI_run();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700227}
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******************************************************************************/
239void bte_main_disable(void)
240{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700241 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700242
243 preload_stop_wait_timer();
244 bte_hci_disable();
245 GKI_destroy_task(BTU_TASK);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700246}
247
248/******************************************************************************
249**
Zhihai Xubad70b12013-06-04 18:21:25 -0700250** Function bte_main_config_hci_logging
251**
252** Description enable or disable HIC snoop logging
253**
254** Returns None
255**
256******************************************************************************/
257void 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700262 hci_logging_config = enable;
Zhihai Xubad70b12013-06-04 18:21:25 -0700263
264 new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
265
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700266 if ((old == new) || bt_disabled) {
Zhihai Xubad70b12013-06-04 18:21:25 -0700267 return;
268 }
269
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700270 if (new)
271 hci->turn_on_logging(hci_logfile);
272 else
273 hci->turn_off_logging();
Zhihai Xubad70b12013-06-04 18:21:25 -0700274}
275
276/******************************************************************************
277**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700278** Function bte_hci_enable
279**
280** Description Enable HCI & Vendor modules
281**
282** Returns None
283**
284******************************************************************************/
285static void bte_hci_enable(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_start_wait_timer();
290
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700291 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 Project5738f832012-12-12 16:00:35 -0800293
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700294 assert(success);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800295
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700296 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
297 hci->turn_on_logging(hci_logfile);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298
299#if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700300 APPL_TRACE_DEBUG("%s not turning off the chip before turning it on", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800301
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700302 /* 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 Project5738f832012-12-12 16:00:35 -0800304
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700305 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 Project5738f832012-12-12 16:00:35 -0800309
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700310 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 Project5738f832012-12-12 16:00:35 -0800312#else
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700313 /* 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 Project5738f832012-12-12 16:00:35 -0800316#endif
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700317 hci->set_chip_power_on(true);
318 hci->do_preload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800319}
320
321/******************************************************************************
322**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700323** Function bte_hci_disable
The Android Open Source Project5738f832012-12-12 16:00:35 -0800324**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700325** Description Disable HCI & Vendor modules
The Android Open Source Project5738f832012-12-12 16:00:35 -0800326**
327** Returns None
328**
329******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700330static void bte_hci_disable(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800331{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700332 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800333
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700334 if (!hci)
Chris Manton6c25e792014-08-07 16:23:41 -0700335 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700341 hci->turn_off_logging();
342 hci->cleanup();
Chris Manton6c25e792014-08-07 16:23:41 -0700343
344 pthread_mutex_unlock(&cleanup_lock);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700345}
346
347/*******************************************************************************
348**
349** Function preload_wait_timeout
350**
351** Description Timeout thread of preload watchdog timer
352**
353** Returns None
354**
355*******************************************************************************/
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700356static void preload_wait_timeout(UNUSED_ATTR void *context)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700357{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700358 APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...",
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700359 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*******************************************************************************/
384static void preload_start_wait_timer(void)
385{
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700386 uint32_t timeout_ms;
Sharvil Nanavatib67820c2014-01-20 18:20:28 -0800387 char timeout_prop[PROPERTY_VALUE_MAX];
Sharvil Nanavatib67820c2014-01-20 18:20:28 -0800388 if (!property_get("bluetooth.enable_timeout_ms", timeout_prop, "3000") || (timeout_ms = atoi(timeout_prop)) < 100)
389 timeout_ms = 3000;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700390
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700391 alarm_set(preload_retry_cb.alarm, timeout_ms, preload_wait_timeout, NULL);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700392}
393
394/*******************************************************************************
395**
396** Function preload_stop_wait_timer
397**
398** Description Stop preload watchdog timer
399**
400** Returns None
401**
402*******************************************************************************/
403static void preload_stop_wait_timer(void)
404{
Sharvil Nanavati14a559a2014-07-25 22:20:46 -0700405 alarm_cancel(preload_retry_cb.alarm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406}
407
408/******************************************************************************
409**
410** Function bte_main_postload_cfg
411**
412** Description BTE MAIN API - Stack postload configuration
413**
414** Returns None
415**
416******************************************************************************/
417void bte_main_postload_cfg(void)
418{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700419 hci->do_postload();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800420}
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******************************************************************************/
432void bte_main_enable_lpm(BOOLEAN enable)
433{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700434 hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800435}
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******************************************************************************/
446void bte_main_lpm_allow_bt_device_sleep()
447{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700448 hci->send_low_power_command(LPM_WAKE_DEASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800449}
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******************************************************************************/
460void bte_main_lpm_wake_bt_device()
461{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700462 hci->send_low_power_command(LPM_WAKE_ASSERT);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463}
464#endif // HCILP_INCLUDED
465
Matthew Xie66432dc2014-04-27 05:45:32 -0700466
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 */
471typedef struct {
472 UINT16 handle;
473 UINT16 peer_codec;
474 UINT16 state;
475} bt_hc_audio_state_t;
476
477struct 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******************************************************************************/
496int 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700518 hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
Matthew Xie66432dc2014-04-27 05:45:32 -0700519 return result;
520}
521
522
The Android Open Source Project5738f832012-12-12 16:00:35 -0800523/******************************************************************************
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******************************************************************************/
535void 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700545 hci->transmit_downward(event, p_msg);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546 }
547 else
548 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700549 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550 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******************************************************************************/
564void 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700585static void preload_cb(bool success)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800586{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700587 APPL_TRACE_EVENT("HC preload_cb %d [1:SUCCESS 0:FAIL]", success);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700589 if (success)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700590 {
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 Project5738f832012-12-12 16:00:35 -0800596}
597
598/******************************************************************************
599**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800600** 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 Johnsonfbbd42b2014-08-15 17:00:17 -0700608static void *alloc(size_t size)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800609{
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700610 /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */
611 if (size > GKI_MAX_BUF_SIZE)
612 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700613 APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700614 size, GKI_MAX_BUF_SIZE);
615 return NULL;
616 }
617
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700618 BT_HDR *p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700620 if (!p_hdr)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800621 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700622 APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623 }
624
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700625 return p_hdr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626}
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 Nanavati75e8f412014-06-24 17:02:30 -0700638** boundary.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639**
640******************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700641static void dealloc(void *buffer)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800642{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700643 GKI_freebuf(buffer);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800644}
645
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700646static 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 Project5738f832012-12-12 16:00:35 -0800648}
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 Johnsonfbbd42b2014-08-15 17:00:17 -0700670static void tx_result(void *p_buf, bool all_fragments_sent)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671{
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700672 if (!all_fragments_sent)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800673 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700674 GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675 }
676 else
677 {
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700678 GKI_freebuf(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800680}
681
682/*****************************************************************************
683** The libbt-hci Callback Functions Table
684*****************************************************************************/
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700685static const hci_callbacks_t hci_callbacks = {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800686 preload_cb,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800687 tx_result
688};
689
Zach Johnsonfbbd42b2014-08-15 17:00:17 -0700690static const allocator_t buffer_allocator = {
691 alloc,
692 dealloc
693};
694