blob: 0719ebe2402868a13858153dfb6aedd3010d4603 [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 ******************************************************************************/
26#include <fcntl.h>
27#include <stdlib.h>
28#include <assert.h>
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070029#include <signal.h>
30#include <time.h>
31#include <hardware/bluetooth.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080032
33#include "gki.h"
34#include "bd.h"
35#include "btu.h"
36#include "bte.h"
37#include "bta_api.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080038#include "bt_utils.h"
Matthew Xie66432dc2014-04-27 05:45:32 -070039#include "bt_hci_bdroid.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040
41/*******************************************************************************
42** Constants & Macros
43*******************************************************************************/
44
45/* Run-time configuration file */
46#ifndef BTE_STACK_CONF_FILE
47#define BTE_STACK_CONF_FILE "/etc/bluetooth/bt_stack.conf"
48#endif
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -070049/* Run-time configuration file for BLE*/
50#ifndef BTE_BLE_STACK_CONF_FILE
51#define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
52#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080053
54/* if not specified in .txt file then use this as default */
55#ifndef HCI_LOGGING_FILENAME
56#define HCI_LOGGING_FILENAME "/data/misc/bluedroid/btsnoop_hci.log"
57#endif
58
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070059/* Stack preload process timeout period */
60#ifndef PRELOAD_START_TIMEOUT_MS
61#define PRELOAD_START_TIMEOUT_MS 3000 // 3 seconds
62#endif
63
64/* Stack preload process maximum retry attempts */
65#ifndef PRELOAD_MAX_RETRY_ATTEMPTS
66#define PRELOAD_MAX_RETRY_ATTEMPTS 0
67#endif
68
The Android Open Source Project5738f832012-12-12 16:00:35 -080069/*******************************************************************************
70** Local type definitions
71*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070072/* Preload retry control block */
73typedef struct
74{
75 int retry_counts;
76 BOOLEAN timer_created;
77 timer_t timer_id;
78} bt_preload_retry_cb_t;
The Android Open Source Project5738f832012-12-12 16:00:35 -080079
80/******************************************************************************
81** Variables
82******************************************************************************/
83BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
Zhihai Xubad70b12013-06-04 18:21:25 -070084BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */
The Android Open Source Project5738f832012-12-12 16:00:35 -080085char hci_logfile[256] = HCI_LOGGING_FILENAME;
86
87
88/*******************************************************************************
89** Static variables
90*******************************************************************************/
91static bt_hc_interface_t *bt_hc_if=NULL;
92static const bt_hc_callbacks_t hc_callbacks;
93static BOOLEAN lpm_enabled = FALSE;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070094static bt_preload_retry_cb_t preload_retry_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
96/*******************************************************************************
97** Static functions
98*******************************************************************************/
99static void bte_main_in_hw_init(void);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700100static void bte_hci_enable(void);
101static void bte_hci_disable(void);
102static void preload_start_wait_timer(void);
103static void preload_stop_wait_timer(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104
105/*******************************************************************************
106** Externs
107*******************************************************************************/
108BTU_API extern UINT32 btu_task (UINT32 param);
109BTU_API extern void BTE_Init (void);
110BT_API extern void BTE_LoadStack(void);
111BT_API void BTE_UnloadStack(void);
112extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
113extern void bte_load_conf(const char *p_path);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700114extern void bte_load_ble_conf(const char *p_path);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700115extern bt_bdaddr_t btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800116
117
118/*******************************************************************************
119** System Task Configuration
120*******************************************************************************/
121
122/* bluetooth protocol stack (BTU) task */
123#ifndef BTE_BTU_STACK_SIZE
124#define BTE_BTU_STACK_SIZE 0//0x2000 /* In bytes */
125#endif
126#define BTE_BTU_TASK_STR ((INT8 *) "BTU")
127UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
128
129/******************************************************************************
130**
131** Function bte_main_in_hw_init
132**
133** Description Internal helper function for chip hardware init
134**
135** Returns None
136**
137******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700138static void bte_main_in_hw_init(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139{
140 if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface()) \
141 == NULL)
142 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700143 APPL_TRACE_ERROR("!!! Failed to get BtHostControllerInterface !!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144 }
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700145
146 memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800147}
148
149/******************************************************************************
150**
151** Function bte_main_boot_entry
152**
153** Description BTE MAIN API - Entry point for BTE chip/stack initialization
154**
155** Returns None
156**
157******************************************************************************/
158void bte_main_boot_entry(void)
159{
160 /* initialize OS */
161 GKI_init();
162
163 bte_main_in_hw_init();
164
165 bte_load_conf(BTE_STACK_CONF_FILE);
Prerepa Viswanadham4c94c5f2014-07-18 15:20:54 -0700166#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
167 bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
168#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800169
170#if (BTTRC_INCLUDED == TRUE)
171 /* Initialize trace feature */
172 BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
173#endif
174}
175
176/******************************************************************************
177**
178** Function bte_main_shutdown
179**
180** Description BTE MAIN API - Shutdown code for BTE chip/stack
181**
182** Returns None
183**
184******************************************************************************/
185void bte_main_shutdown()
186{
187 GKI_shutdown();
188}
189
190/******************************************************************************
191**
192** Function bte_main_enable
193**
194** Description BTE MAIN API - Creates all the BTE tasks. Should be called
195** part of the Bluetooth stack enable sequence
196**
197** Returns None
198**
199******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700200void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800201{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700202 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203
204 /* Initialize BTE control block */
205 BTE_Init();
206
207 lpm_enabled = FALSE;
208
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700209 GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
210 (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
211 sizeof(bte_btu_stack));
212
Zhihai Xu1a558ca2014-01-15 10:28:55 -0800213 bte_hci_enable();
214
Sharvil Nanavatib5382482014-06-29 18:10:15 -0700215 GKI_run();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700216}
217
218/******************************************************************************
219**
220** Function bte_main_disable
221**
222** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
223** part of the Bluetooth stack disable sequence
224**
225** Returns None
226**
227******************************************************************************/
228void bte_main_disable(void)
229{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700230 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700231
232 preload_stop_wait_timer();
233 bte_hci_disable();
234 GKI_destroy_task(BTU_TASK);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700235}
236
237/******************************************************************************
238**
Zhihai Xubad70b12013-06-04 18:21:25 -0700239** Function bte_main_config_hci_logging
240**
241** Description enable or disable HIC snoop logging
242**
243** Returns None
244**
245******************************************************************************/
246void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled)
247{
248 int old = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
249 int new;
250
251 if (enable) {
252 hci_logging_config = TRUE;
253 } else {
254 hci_logging_config = FALSE;
255 }
256
257 new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
258
259 if ((old == new) || bt_disabled || (bt_hc_if == NULL)) {
260 return;
261 }
262
263 bt_hc_if->logging(new ? BT_HC_LOGGING_ON : BT_HC_LOGGING_OFF, hci_logfile);
264}
265
266/******************************************************************************
267**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700268** Function bte_hci_enable
269**
270** Description Enable HCI & Vendor modules
271**
272** Returns None
273**
274******************************************************************************/
275static void bte_hci_enable(void)
276{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700277 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700278
279 preload_start_wait_timer();
280
The Android Open Source Project5738f832012-12-12 16:00:35 -0800281 if (bt_hc_if)
282 {
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700283 int result = bt_hc_if->init(&hc_callbacks, btif_local_bd_addr.address);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700284 APPL_TRACE_EVENT("libbt-hci init returns %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285
286 assert(result == BT_HC_STATUS_SUCCESS);
287
Zhihai Xubad70b12013-06-04 18:21:25 -0700288 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800289 bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile);
290
291#if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700292 APPL_TRACE_DEBUG("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293
294 /* Do not power off the chip before powering on if BT_CLEAN_TURN_ON_DISABLED flag
295 is defined and set to TRUE to avoid below mentioned issue.
296
297 Wingray kernel driver maintains a combined counter to keep track of
298 BT-Wifi state. Invoking set_power(BT_HC_CHIP_PWR_OFF) when the BT is already
299 in OFF state causes this counter to be incorrectly decremented and results in undesired
300 behavior of the chip.
301
302 This is only a workaround and when the issue is fixed in the kernel this work around
303 should be removed. */
304#else
305 /* toggle chip power to ensure we will reset chip in case
306 a previous stack shutdown wasn't completed gracefully */
307 bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
308#endif
309 bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
310
311 bt_hc_if->preload(NULL);
312 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800313}
314
315/******************************************************************************
316**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700317** Function bte_hci_disable
The Android Open Source Project5738f832012-12-12 16:00:35 -0800318**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700319** Description Disable HCI & Vendor modules
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320**
321** Returns None
322**
323******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700324static void bte_hci_disable(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800325{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700326 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800327
328 if (bt_hc_if)
329 {
Zhihai Xubad70b12013-06-04 18:21:25 -0700330 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700331 bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile);
Sharvil Nanavatif3b23f22014-06-15 13:36:45 -0700332 bt_hc_if->cleanup();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700333 }
334}
335
336/*******************************************************************************
337**
338** Function preload_wait_timeout
339**
340** Description Timeout thread of preload watchdog timer
341**
342** Returns None
343**
344*******************************************************************************/
345static void preload_wait_timeout(union sigval arg)
346{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800347 UNUSED(arg);
348
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700349 APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...",
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700350 preload_retry_cb.retry_counts,
351 PRELOAD_MAX_RETRY_ATTEMPTS);
352
353 if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS)
354 {
355 bte_hci_disable();
356 GKI_delay(100);
357 bte_hci_enable();
358 }
359 else
360 {
361 /* Notify BTIF_TASK that the init procedure had failed*/
362 GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL);
363 }
364}
365
366/*******************************************************************************
367**
368** Function preload_start_wait_timer
369**
370** Description Launch startup watchdog timer
371**
372** Returns None
373**
374*******************************************************************************/
375static void preload_start_wait_timer(void)
376{
377 int status;
378 struct itimerspec ts;
379 struct sigevent se;
380 UINT32 timeout_ms = PRELOAD_START_TIMEOUT_MS;
381
382 if (preload_retry_cb.timer_created == FALSE)
383 {
384 se.sigev_notify = SIGEV_THREAD;
385 se.sigev_value.sival_ptr = &preload_retry_cb.timer_id;
386 se.sigev_notify_function = preload_wait_timeout;
387 se.sigev_notify_attributes = NULL;
388
389 status = timer_create(CLOCK_MONOTONIC, &se, &preload_retry_cb.timer_id);
390
391 if (status == 0)
392 preload_retry_cb.timer_created = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393 }
394
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700395 if (preload_retry_cb.timer_created == TRUE)
396 {
397 ts.it_value.tv_sec = timeout_ms/1000;
398 ts.it_value.tv_nsec = 1000000*(timeout_ms%1000);
399 ts.it_interval.tv_sec = 0;
400 ts.it_interval.tv_nsec = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700402 status = timer_settime(preload_retry_cb.timer_id, 0, &ts, 0);
403 if (status == -1)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700404 APPL_TRACE_ERROR("Failed to fire preload watchdog timer");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700405 }
406}
407
408/*******************************************************************************
409**
410** Function preload_stop_wait_timer
411**
412** Description Stop preload watchdog timer
413**
414** Returns None
415**
416*******************************************************************************/
417static void preload_stop_wait_timer(void)
418{
419 if (preload_retry_cb.timer_created == TRUE)
420 {
421 timer_delete(preload_retry_cb.timer_id);
422 preload_retry_cb.timer_created = FALSE;
423 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800424}
425
426/******************************************************************************
427**
428** Function bte_main_postload_cfg
429**
430** Description BTE MAIN API - Stack postload configuration
431**
432** Returns None
433**
434******************************************************************************/
435void bte_main_postload_cfg(void)
436{
437 if (bt_hc_if)
438 bt_hc_if->postload(NULL);
439}
440
441#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
442/******************************************************************************
443**
444** Function bte_main_enable_lpm
445**
446** Description BTE MAIN API - Enable/Disable low power mode operation
447**
448** Returns None
449**
450******************************************************************************/
451void bte_main_enable_lpm(BOOLEAN enable)
452{
453 int result = -1;
454
455 if (bt_hc_if)
456 result = bt_hc_if->lpm( \
457 (enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \
458 );
459
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700460 APPL_TRACE_EVENT("HC lib lpm enable=%d return %d", enable, result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800461}
462
463/******************************************************************************
464**
465** Function bte_main_lpm_allow_bt_device_sleep
466**
467** Description BTE MAIN API - Allow BT controller goest to sleep
468**
469** Returns None
470**
471******************************************************************************/
472void bte_main_lpm_allow_bt_device_sleep()
473{
474 int result = -1;
475
476 if ((bt_hc_if) && (lpm_enabled == TRUE))
477 result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT);
478
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700479 APPL_TRACE_DEBUG("HC lib lpm deassertion return %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480}
481
482/******************************************************************************
483**
484** Function bte_main_lpm_wake_bt_device
485**
486** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
487**
488** Returns None
489**
490******************************************************************************/
491void bte_main_lpm_wake_bt_device()
492{
493 int result = -1;
494
495 if ((bt_hc_if) && (lpm_enabled == TRUE))
496 result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT);
497
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700498 APPL_TRACE_DEBUG("HC lib lpm assertion return %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800499}
500#endif // HCILP_INCLUDED
501
Matthew Xie66432dc2014-04-27 05:45:32 -0700502
503/* NOTICE:
504 * Definitions for audio state structure, this type needs to match to
505 * the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
506 */
507typedef struct {
508 UINT16 handle;
509 UINT16 peer_codec;
510 UINT16 state;
511} bt_hc_audio_state_t;
512
513struct bt_audio_state_tag {
514 BT_HDR hdr;
515 bt_hc_audio_state_t audio;
516};
517
518/******************************************************************************
519**
520** Function set_audio_state
521**
522** Description Sets audio state on controller state for SCO (PCM, WBS, FM)
523**
524** Parameters handle: codec related handle for SCO: sco cb idx, unused for
525** codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
526** state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
527** param: future extensions, e.g. call-in structure/event.
528**
529** Returns None
530**
531******************************************************************************/
532int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
533{
534 struct bt_audio_state_tag *p_msg;
535 int result = -1;
536
537 APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
538 codec, state);
539 if (NULL != param)
540 APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
541 p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
542 if (!p_msg)
543 return result;
544 p_msg->audio.handle = handle;
545 p_msg->audio.peer_codec = codec;
546 p_msg->audio.state = state;
547
548 p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
549 p_msg->hdr.len = sizeof(p_msg->audio);
550 p_msg->hdr.offset = 0;
551 /* layer_specific shall contain return path event! for BTA events!
552 * 0 means no return message is expected. */
553 p_msg->hdr.layer_specific = 0;
554 if (bt_hc_if)
555 {
556 bt_hc_if->tx_cmd((TRANSAC)p_msg, (char *)(&p_msg->audio), sizeof(*p_msg));
557 }
558 return result;
559}
560
561
The Android Open Source Project5738f832012-12-12 16:00:35 -0800562/******************************************************************************
563**
564** Function bte_main_hci_send
565**
566** Description BTE MAIN API - This function is called by the upper stack to
567** send an HCI message. The function displays a protocol trace
568** message (if enabled), and then calls the 'transmit' function
569** associated with the currently selected HCI transport
570**
571** Returns None
572**
573******************************************************************************/
574void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
575{
576 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
577
578 p_msg->event = event;
579
580
581 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
582 (sub_event == LOCAL_BLE_CONTROLLER_ID))
583 {
584 if (bt_hc_if)
585 bt_hc_if->transmit_buf((TRANSAC)p_msg, \
586 (char *) (p_msg + 1), \
587 p_msg->len);
588 else
589 GKI_freebuf(p_msg);
590 }
591 else
592 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700593 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594 GKI_freebuf(p_msg);
595 }
596}
597
598/******************************************************************************
599**
600** Function bte_main_post_reset_init
601**
602** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT
603** and shall be automatically called from BTE after HCI_Reset
604**
605** Returns None
606**
607******************************************************************************/
608void bte_main_post_reset_init()
609{
610 BTM_ContinueReset();
611}
612
613/*****************************************************************************
614**
615** libbt-hci Callback Functions
616**
617*****************************************************************************/
618
619/******************************************************************************
620**
621** Function preload_cb
622**
623** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
624** when the libbt-hci completed stack preload process
625**
626** Returns None
627**
628******************************************************************************/
629static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result)
630{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800631 UNUSED(transac);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800632
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700633 APPL_TRACE_EVENT("HC preload_cb %d [0:SUCCESS 1:FAIL]", result);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700634
635 if (result == BT_HC_PRELOAD_SUCCESS)
636 {
637 preload_stop_wait_timer();
638
639 /* notify BTU task that libbt-hci is ready */
640 GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL);
641 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800642}
643
644/******************************************************************************
645**
646** Function postload_cb
647**
648** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
649** when the libbt-hci lib completed stack postload process
650**
651** Returns None
652**
653******************************************************************************/
654static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result)
655{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800656 UNUSED(transac);
657
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700658 APPL_TRACE_EVENT("HC postload_cb %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800659}
660
661/******************************************************************************
662**
663** Function lpm_cb
664**
665** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
666** back from the libbt-hci to indicate the current LPM state
667**
668** Returns None
669**
670******************************************************************************/
671static void lpm_cb(bt_hc_lpm_request_result_t result)
672{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700673 APPL_TRACE_EVENT("HC lpm_result_cb %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800674 lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE;
675}
676
677/******************************************************************************
678**
679** Function hostwake_ind
680**
681** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
682** from the libbt-hci to indicate the HostWake event
683**
684** Returns None
685**
686******************************************************************************/
687static void hostwake_ind(bt_hc_low_power_event_t event)
688{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700689 APPL_TRACE_EVENT("HC hostwake_ind %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690}
691
692/******************************************************************************
693**
694** Function alloc
695**
696** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
697** from the libbt-hci to request for data buffer allocation
698**
699** Returns NULL / pointer to allocated buffer
700**
701******************************************************************************/
702static char *alloc(int size)
703{
704 BT_HDR *p_hdr = NULL;
705
706 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700707 APPL_TRACE_DEBUG("HC alloc size=%d", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800708 */
709
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700710 /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */
711 if (size > GKI_MAX_BUF_SIZE)
712 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700713 APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700714 size, GKI_MAX_BUF_SIZE);
715 return NULL;
716 }
717
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718 p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
719
720 if (p_hdr == NULL)
721 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700722 APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800723 }
724
725 return ((char *) p_hdr);
726}
727
728/******************************************************************************
729**
730** Function dealloc
731**
732** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
733** from the libbt-hci to release the data buffer allocated
734** through the alloc call earlier
735**
736** Bluedroid libbt-hci library uses 'transac' parameter to
737** pass data-path buffer/packet across bt_hci_lib interface
Sharvil Nanavati75e8f412014-06-24 17:02:30 -0700738** boundary.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800739**
740******************************************************************************/
Sharvil Nanavati75e8f412014-06-24 17:02:30 -0700741static void dealloc(TRANSAC transac)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800742{
743 GKI_freebuf(transac);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800744}
745
746/******************************************************************************
747**
748** Function data_ind
749**
750** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
751** from the libbt-hci to pass in the received HCI packets
752**
753** The core stack is responsible for releasing the data buffer
754** passed in from the libbt-hci once the core stack has done
755** with it.
756**
757** Bluedroid libbt-hci library uses 'transac' parameter to
758** pass data-path buffer/packet across bt_hci_lib interface
759** boundary. The 'p_buf' and 'len' parameters are not intended
760** to be used here but might point to data portion in data-
761** path buffer and length of valid data respectively.
762**
763** Returns bt_hc_status_t
764**
765******************************************************************************/
766static int data_ind(TRANSAC transac, char *p_buf, int len)
767{
768 BT_HDR *p_msg = (BT_HDR *) transac;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800769 UNUSED(p_buf);
770 UNUSED(len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800771
772 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700773 APPL_TRACE_DEBUG("HC data_ind event=0x%04X (len=%d)", p_msg->event, len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774 */
775
776 GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
777 return BT_HC_STATUS_SUCCESS;
778}
779
780/******************************************************************************
781**
782** Function tx_result
783**
784** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
785** from the libbt-hci once it has processed/sent the prior data
786** buffer which core stack passed to it through transmit_buf
787** call earlier.
788**
789** The core stack is responsible for releasing the data buffer
790** if it has been completedly processed.
791**
792** Bluedroid libbt-hci library uses 'transac' parameter to
793** pass data-path buffer/packet across bt_hci_lib interface
794** boundary. The 'p_buf' is not intended to be used here
795** but might point to data portion in data-path buffer.
796**
797** Returns bt_hc_status_t
798**
799******************************************************************************/
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800800static int tx_result(TRANSAC transac, char *p_buf, bt_hc_transmit_result_t result)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800802 UNUSED(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800803 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700804 APPL_TRACE_DEBUG("HC tx_result %d (event=%04X)", result, \
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805 ((BT_HDR *)transac)->event);
806 */
807
808 if (result == BT_HC_TX_FRAGMENT)
809 {
810 GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
811 }
812 else
813 {
814 GKI_freebuf(transac);
815 }
816
817 return BT_HC_STATUS_SUCCESS;
818}
819
820/*****************************************************************************
821** The libbt-hci Callback Functions Table
822*****************************************************************************/
823static const bt_hc_callbacks_t hc_callbacks = {
824 sizeof(bt_hc_callbacks_t),
825 preload_cb,
826 postload_cb,
827 lpm_cb,
828 hostwake_ind,
829 alloc,
830 dealloc,
831 data_ind,
832 tx_result
833};
834