blob: 36f12d0fea2facb8382f5c6d36bf36019cab5e64 [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"
38#include "bt_hci_lib.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080039#include "bt_utils.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
49
50/* if not specified in .txt file then use this as default */
51#ifndef HCI_LOGGING_FILENAME
52#define HCI_LOGGING_FILENAME "/data/misc/bluedroid/btsnoop_hci.log"
53#endif
54
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070055/* Stack preload process timeout period */
56#ifndef PRELOAD_START_TIMEOUT_MS
57#define PRELOAD_START_TIMEOUT_MS 3000 // 3 seconds
58#endif
59
60/* Stack preload process maximum retry attempts */
61#ifndef PRELOAD_MAX_RETRY_ATTEMPTS
62#define PRELOAD_MAX_RETRY_ATTEMPTS 0
63#endif
64
The Android Open Source Project5738f832012-12-12 16:00:35 -080065/*******************************************************************************
66** Local type definitions
67*******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070068/* Preload retry control block */
69typedef struct
70{
71 int retry_counts;
72 BOOLEAN timer_created;
73 timer_t timer_id;
74} bt_preload_retry_cb_t;
The Android Open Source Project5738f832012-12-12 16:00:35 -080075
76/******************************************************************************
77** Variables
78******************************************************************************/
79BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
Zhihai Xubad70b12013-06-04 18:21:25 -070080BOOLEAN hci_logging_config = FALSE; /* configured from bluetooth framework */
The Android Open Source Project5738f832012-12-12 16:00:35 -080081char hci_logfile[256] = HCI_LOGGING_FILENAME;
82
83
84/*******************************************************************************
85** Static variables
86*******************************************************************************/
87static bt_hc_interface_t *bt_hc_if=NULL;
88static const bt_hc_callbacks_t hc_callbacks;
89static BOOLEAN lpm_enabled = FALSE;
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070090static bt_preload_retry_cb_t preload_retry_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -080091
92/*******************************************************************************
93** Static functions
94*******************************************************************************/
95static void bte_main_in_hw_init(void);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -070096static void bte_hci_enable(void);
97static void bte_hci_disable(void);
98static void preload_start_wait_timer(void);
99static void preload_stop_wait_timer(void);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800100
101/*******************************************************************************
102** Externs
103*******************************************************************************/
104BTU_API extern UINT32 btu_task (UINT32 param);
105BTU_API extern void BTE_Init (void);
106BT_API extern void BTE_LoadStack(void);
107BT_API void BTE_UnloadStack(void);
108extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
109extern void bte_load_conf(const char *p_path);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700110extern bt_bdaddr_t btif_local_bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800111
112
113/*******************************************************************************
114** System Task Configuration
115*******************************************************************************/
116
117/* bluetooth protocol stack (BTU) task */
118#ifndef BTE_BTU_STACK_SIZE
119#define BTE_BTU_STACK_SIZE 0//0x2000 /* In bytes */
120#endif
121#define BTE_BTU_TASK_STR ((INT8 *) "BTU")
122UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
123
124/******************************************************************************
125**
126** Function bte_main_in_hw_init
127**
128** Description Internal helper function for chip hardware init
129**
130** Returns None
131**
132******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700133static void bte_main_in_hw_init(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800134{
135 if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface()) \
136 == NULL)
137 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700138 APPL_TRACE_ERROR("!!! Failed to get BtHostControllerInterface !!!");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139 }
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700140
141 memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142}
143
144/******************************************************************************
145**
146** Function bte_main_boot_entry
147**
148** Description BTE MAIN API - Entry point for BTE chip/stack initialization
149**
150** Returns None
151**
152******************************************************************************/
153void bte_main_boot_entry(void)
154{
155 /* initialize OS */
156 GKI_init();
157
158 bte_main_in_hw_init();
159
160 bte_load_conf(BTE_STACK_CONF_FILE);
161
162#if (BTTRC_INCLUDED == TRUE)
163 /* Initialize trace feature */
164 BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
165#endif
166}
167
168/******************************************************************************
169**
170** Function bte_main_shutdown
171**
172** Description BTE MAIN API - Shutdown code for BTE chip/stack
173**
174** Returns None
175**
176******************************************************************************/
177void bte_main_shutdown()
178{
179 GKI_shutdown();
180}
181
182/******************************************************************************
183**
184** Function bte_main_enable
185**
186** Description BTE MAIN API - Creates all the BTE tasks. Should be called
187** part of the Bluetooth stack enable sequence
188**
189** Returns None
190**
191******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700192void bte_main_enable()
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700194 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195
196 /* Initialize BTE control block */
197 BTE_Init();
198
199 lpm_enabled = FALSE;
200
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700201 GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
202 (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
203 sizeof(bte_btu_stack));
204
Zhihai Xu1a558ca2014-01-15 10:28:55 -0800205 bte_hci_enable();
206
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700207 GKI_run(0);
208}
209
210/******************************************************************************
211**
212** Function bte_main_disable
213**
214** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
215** part of the Bluetooth stack disable sequence
216**
217** Returns None
218**
219******************************************************************************/
220void bte_main_disable(void)
221{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700222 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700223
224 preload_stop_wait_timer();
225 bte_hci_disable();
226 GKI_destroy_task(BTU_TASK);
227 GKI_freeze();
228}
229
230/******************************************************************************
231**
Zhihai Xubad70b12013-06-04 18:21:25 -0700232** Function bte_main_config_hci_logging
233**
234** Description enable or disable HIC snoop logging
235**
236** Returns None
237**
238******************************************************************************/
239void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled)
240{
241 int old = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
242 int new;
243
244 if (enable) {
245 hci_logging_config = TRUE;
246 } else {
247 hci_logging_config = FALSE;
248 }
249
250 new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
251
252 if ((old == new) || bt_disabled || (bt_hc_if == NULL)) {
253 return;
254 }
255
256 bt_hc_if->logging(new ? BT_HC_LOGGING_ON : BT_HC_LOGGING_OFF, hci_logfile);
257}
258
259/******************************************************************************
260**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700261** Function bte_hci_enable
262**
263** Description Enable HCI & Vendor modules
264**
265** Returns None
266**
267******************************************************************************/
268static void bte_hci_enable(void)
269{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700270 APPL_TRACE_DEBUG("%s", __FUNCTION__);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700271
272 preload_start_wait_timer();
273
The Android Open Source Project5738f832012-12-12 16:00:35 -0800274 if (bt_hc_if)
275 {
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700276 int result = bt_hc_if->init(&hc_callbacks, btif_local_bd_addr.address);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700277 APPL_TRACE_EVENT("libbt-hci init returns %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800278
279 assert(result == BT_HC_STATUS_SUCCESS);
280
Zhihai Xubad70b12013-06-04 18:21:25 -0700281 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800282 bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile);
283
284#if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700285 APPL_TRACE_DEBUG("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286
287 /* Do not power off the chip before powering on if BT_CLEAN_TURN_ON_DISABLED flag
288 is defined and set to TRUE to avoid below mentioned issue.
289
290 Wingray kernel driver maintains a combined counter to keep track of
291 BT-Wifi state. Invoking set_power(BT_HC_CHIP_PWR_OFF) when the BT is already
292 in OFF state causes this counter to be incorrectly decremented and results in undesired
293 behavior of the chip.
294
295 This is only a workaround and when the issue is fixed in the kernel this work around
296 should be removed. */
297#else
298 /* toggle chip power to ensure we will reset chip in case
299 a previous stack shutdown wasn't completed gracefully */
300 bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
301#endif
302 bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
303
304 bt_hc_if->preload(NULL);
305 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800306}
307
308/******************************************************************************
309**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700310** Function bte_hci_disable
The Android Open Source Project5738f832012-12-12 16:00:35 -0800311**
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700312** Description Disable HCI & Vendor modules
The Android Open Source Project5738f832012-12-12 16:00:35 -0800313**
314** Returns None
315**
316******************************************************************************/
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700317static void bte_hci_disable(void)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800318{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700319 APPL_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800320
321 if (bt_hc_if)
322 {
Zhihai Xubad70b12013-06-04 18:21:25 -0700323 if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700324 bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile);
Sharvil Nanavatif3b23f22014-06-15 13:36:45 -0700325 bt_hc_if->cleanup();
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700326 }
327}
328
329/*******************************************************************************
330**
331** Function preload_wait_timeout
332**
333** Description Timeout thread of preload watchdog timer
334**
335** Returns None
336**
337*******************************************************************************/
338static void preload_wait_timeout(union sigval arg)
339{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800340 UNUSED(arg);
341
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700342 APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...",
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700343 preload_retry_cb.retry_counts,
344 PRELOAD_MAX_RETRY_ATTEMPTS);
345
346 if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS)
347 {
348 bte_hci_disable();
349 GKI_delay(100);
350 bte_hci_enable();
351 }
352 else
353 {
354 /* Notify BTIF_TASK that the init procedure had failed*/
355 GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL);
356 }
357}
358
359/*******************************************************************************
360**
361** Function preload_start_wait_timer
362**
363** Description Launch startup watchdog timer
364**
365** Returns None
366**
367*******************************************************************************/
368static void preload_start_wait_timer(void)
369{
370 int status;
371 struct itimerspec ts;
372 struct sigevent se;
373 UINT32 timeout_ms = PRELOAD_START_TIMEOUT_MS;
374
375 if (preload_retry_cb.timer_created == FALSE)
376 {
377 se.sigev_notify = SIGEV_THREAD;
378 se.sigev_value.sival_ptr = &preload_retry_cb.timer_id;
379 se.sigev_notify_function = preload_wait_timeout;
380 se.sigev_notify_attributes = NULL;
381
382 status = timer_create(CLOCK_MONOTONIC, &se, &preload_retry_cb.timer_id);
383
384 if (status == 0)
385 preload_retry_cb.timer_created = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800386 }
387
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700388 if (preload_retry_cb.timer_created == TRUE)
389 {
390 ts.it_value.tv_sec = timeout_ms/1000;
391 ts.it_value.tv_nsec = 1000000*(timeout_ms%1000);
392 ts.it_interval.tv_sec = 0;
393 ts.it_interval.tv_nsec = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800394
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700395 status = timer_settime(preload_retry_cb.timer_id, 0, &ts, 0);
396 if (status == -1)
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700397 APPL_TRACE_ERROR("Failed to fire preload watchdog timer");
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700398 }
399}
400
401/*******************************************************************************
402**
403** Function preload_stop_wait_timer
404**
405** Description Stop preload watchdog timer
406**
407** Returns None
408**
409*******************************************************************************/
410static void preload_stop_wait_timer(void)
411{
412 if (preload_retry_cb.timer_created == TRUE)
413 {
414 timer_delete(preload_retry_cb.timer_id);
415 preload_retry_cb.timer_created = FALSE;
416 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800417}
418
419/******************************************************************************
420**
421** Function bte_main_postload_cfg
422**
423** Description BTE MAIN API - Stack postload configuration
424**
425** Returns None
426**
427******************************************************************************/
428void bte_main_postload_cfg(void)
429{
430 if (bt_hc_if)
431 bt_hc_if->postload(NULL);
432}
433
434#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
435/******************************************************************************
436**
437** Function bte_main_enable_lpm
438**
439** Description BTE MAIN API - Enable/Disable low power mode operation
440**
441** Returns None
442**
443******************************************************************************/
444void bte_main_enable_lpm(BOOLEAN enable)
445{
446 int result = -1;
447
448 if (bt_hc_if)
449 result = bt_hc_if->lpm( \
450 (enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \
451 );
452
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700453 APPL_TRACE_EVENT("HC lib lpm enable=%d return %d", enable, result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800454}
455
456/******************************************************************************
457**
458** Function bte_main_lpm_allow_bt_device_sleep
459**
460** Description BTE MAIN API - Allow BT controller goest to sleep
461**
462** Returns None
463**
464******************************************************************************/
465void bte_main_lpm_allow_bt_device_sleep()
466{
467 int result = -1;
468
469 if ((bt_hc_if) && (lpm_enabled == TRUE))
470 result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT);
471
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700472 APPL_TRACE_DEBUG("HC lib lpm deassertion return %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800473}
474
475/******************************************************************************
476**
477** Function bte_main_lpm_wake_bt_device
478**
479** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
480**
481** Returns None
482**
483******************************************************************************/
484void bte_main_lpm_wake_bt_device()
485{
486 int result = -1;
487
488 if ((bt_hc_if) && (lpm_enabled == TRUE))
489 result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT);
490
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700491 APPL_TRACE_DEBUG("HC lib lpm assertion return %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800492}
493#endif // HCILP_INCLUDED
494
495/******************************************************************************
496**
497** Function bte_main_hci_send
498**
499** Description BTE MAIN API - This function is called by the upper stack to
500** send an HCI message. The function displays a protocol trace
501** message (if enabled), and then calls the 'transmit' function
502** associated with the currently selected HCI transport
503**
504** Returns None
505**
506******************************************************************************/
507void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
508{
509 UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
510
511 p_msg->event = event;
512
513
514 if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
515 (sub_event == LOCAL_BLE_CONTROLLER_ID))
516 {
517 if (bt_hc_if)
518 bt_hc_if->transmit_buf((TRANSAC)p_msg, \
519 (char *) (p_msg + 1), \
520 p_msg->len);
521 else
522 GKI_freebuf(p_msg);
523 }
524 else
525 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700526 APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800527 GKI_freebuf(p_msg);
528 }
529}
530
531/******************************************************************************
532**
533** Function bte_main_post_reset_init
534**
535** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT
536** and shall be automatically called from BTE after HCI_Reset
537**
538** Returns None
539**
540******************************************************************************/
541void bte_main_post_reset_init()
542{
543 BTM_ContinueReset();
544}
545
546/*****************************************************************************
547**
548** libbt-hci Callback Functions
549**
550*****************************************************************************/
551
552/******************************************************************************
553**
554** Function preload_cb
555**
556** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
557** when the libbt-hci completed stack preload process
558**
559** Returns None
560**
561******************************************************************************/
562static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result)
563{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800564 UNUSED(transac);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800565
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700566 APPL_TRACE_EVENT("HC preload_cb %d [0:SUCCESS 1:FAIL]", result);
YK Jeffrey Chao48ebe2c2013-04-24 11:38:06 -0700567
568 if (result == BT_HC_PRELOAD_SUCCESS)
569 {
570 preload_stop_wait_timer();
571
572 /* notify BTU task that libbt-hci is ready */
573 GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL);
574 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575}
576
577/******************************************************************************
578**
579** Function postload_cb
580**
581** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
582** when the libbt-hci lib completed stack postload process
583**
584** Returns None
585**
586******************************************************************************/
587static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result)
588{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800589 UNUSED(transac);
590
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700591 APPL_TRACE_EVENT("HC postload_cb %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592}
593
594/******************************************************************************
595**
596** Function lpm_cb
597**
598** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
599** back from the libbt-hci to indicate the current LPM state
600**
601** Returns None
602**
603******************************************************************************/
604static void lpm_cb(bt_hc_lpm_request_result_t result)
605{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700606 APPL_TRACE_EVENT("HC lpm_result_cb %d", result);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800607 lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE;
608}
609
610/******************************************************************************
611**
612** Function hostwake_ind
613**
614** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
615** from the libbt-hci to indicate the HostWake event
616**
617** Returns None
618**
619******************************************************************************/
620static void hostwake_ind(bt_hc_low_power_event_t event)
621{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700622 APPL_TRACE_EVENT("HC hostwake_ind %d", event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623}
624
625/******************************************************************************
626**
627** Function alloc
628**
629** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
630** from the libbt-hci to request for data buffer allocation
631**
632** Returns NULL / pointer to allocated buffer
633**
634******************************************************************************/
635static char *alloc(int size)
636{
637 BT_HDR *p_hdr = NULL;
638
639 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700640 APPL_TRACE_DEBUG("HC alloc size=%d", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641 */
642
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700643 /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */
644 if (size > GKI_MAX_BUF_SIZE)
645 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700646 APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d",
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700647 size, GKI_MAX_BUF_SIZE);
648 return NULL;
649 }
650
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651 p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
652
653 if (p_hdr == NULL)
654 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700655 APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800656 }
657
658 return ((char *) p_hdr);
659}
660
661/******************************************************************************
662**
663** Function dealloc
664**
665** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
666** from the libbt-hci to release the data buffer allocated
667** through the alloc call earlier
668**
669** Bluedroid libbt-hci library uses 'transac' parameter to
670** pass data-path buffer/packet across bt_hci_lib interface
Sharvil Nanavati75e8f412014-06-24 17:02:30 -0700671** boundary.
The Android Open Source Project5738f832012-12-12 16:00:35 -0800672**
673******************************************************************************/
Sharvil Nanavati75e8f412014-06-24 17:02:30 -0700674static void dealloc(TRANSAC transac)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675{
676 GKI_freebuf(transac);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800677}
678
679/******************************************************************************
680**
681** Function data_ind
682**
683** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
684** from the libbt-hci to pass in the received HCI packets
685**
686** The core stack is responsible for releasing the data buffer
687** passed in from the libbt-hci once the core stack has done
688** with it.
689**
690** Bluedroid libbt-hci library uses 'transac' parameter to
691** pass data-path buffer/packet across bt_hci_lib interface
692** boundary. The 'p_buf' and 'len' parameters are not intended
693** to be used here but might point to data portion in data-
694** path buffer and length of valid data respectively.
695**
696** Returns bt_hc_status_t
697**
698******************************************************************************/
699static int data_ind(TRANSAC transac, char *p_buf, int len)
700{
701 BT_HDR *p_msg = (BT_HDR *) transac;
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800702 UNUSED(p_buf);
703 UNUSED(len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800704
705 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700706 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 -0800707 */
708
709 GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
710 return BT_HC_STATUS_SUCCESS;
711}
712
713/******************************************************************************
714**
715** Function tx_result
716**
717** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
718** from the libbt-hci once it has processed/sent the prior data
719** buffer which core stack passed to it through transmit_buf
720** call earlier.
721**
722** The core stack is responsible for releasing the data buffer
723** if it has been completedly processed.
724**
725** Bluedroid libbt-hci library uses 'transac' parameter to
726** pass data-path buffer/packet across bt_hci_lib interface
727** boundary. The 'p_buf' is not intended to be used here
728** but might point to data portion in data-path buffer.
729**
730** Returns bt_hc_status_t
731**
732******************************************************************************/
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800733static int tx_result(TRANSAC transac, char *p_buf, bt_hc_transmit_result_t result)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800734{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -0800735 UNUSED(p_buf);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800736 /*
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700737 APPL_TRACE_DEBUG("HC tx_result %d (event=%04X)", result, \
The Android Open Source Project5738f832012-12-12 16:00:35 -0800738 ((BT_HDR *)transac)->event);
739 */
740
741 if (result == BT_HC_TX_FRAGMENT)
742 {
743 GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
744 }
745 else
746 {
747 GKI_freebuf(transac);
748 }
749
750 return BT_HC_STATUS_SUCCESS;
751}
752
753/*****************************************************************************
754** The libbt-hci Callback Functions Table
755*****************************************************************************/
756static const bt_hc_callbacks_t hc_callbacks = {
757 sizeof(bt_hc_callbacks_t),
758 preload_cb,
759 postload_cb,
760 lpm_cb,
761 hostwake_ind,
762 alloc,
763 dealloc,
764 data_ind,
765 tx_result
766};
767