Home | History | Annotate | Download | only in main
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  Filename:      bte_main.c
     22  *
     23  *  Description:   Contains BTE core stack initialization and shutdown code
     24  *
     25  ******************************************************************************/
     26 #include <fcntl.h>
     27 #include <stdlib.h>
     28 #include <assert.h>
     29 #include <signal.h>
     30 #include <time.h>
     31 #include <hardware/bluetooth.h>
     32 
     33 #include "gki.h"
     34 #include "bd.h"
     35 #include "btu.h"
     36 #include "bte.h"
     37 #include "bta_api.h"
     38 #include "bt_utils.h"
     39 #include "bt_hci_bdroid.h"
     40 
     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 /* 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
     53 
     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 
     59 /* 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 
     69 /*******************************************************************************
     70 **  Local type definitions
     71 *******************************************************************************/
     72 /* Preload retry control block */
     73 typedef struct
     74 {
     75     int     retry_counts;
     76     BOOLEAN timer_created;
     77     timer_t timer_id;
     78 } bt_preload_retry_cb_t;
     79 
     80 /******************************************************************************
     81 **  Variables
     82 ******************************************************************************/
     83 BOOLEAN hci_logging_enabled = FALSE;    /* by default, turn hci log off */
     84 BOOLEAN hci_logging_config = FALSE;    /* configured from bluetooth framework */
     85 BOOLEAN hci_save_log = FALSE; /* save a copy of the log before starting again */
     86 char hci_logfile[256] = HCI_LOGGING_FILENAME;
     87 
     88 /*******************************************************************************
     89 **  Static variables
     90 *******************************************************************************/
     91 static bt_hc_interface_t *bt_hc_if=NULL;
     92 static const bt_hc_callbacks_t hc_callbacks;
     93 static BOOLEAN lpm_enabled = FALSE;
     94 static bt_preload_retry_cb_t preload_retry_cb;
     95 // Lock to serialize cleanup requests from upper layer.
     96 static pthread_mutex_t cleanup_lock;
     97 
     98 /*******************************************************************************
     99 **  Static functions
    100 *******************************************************************************/
    101 static void bte_main_in_hw_init(void);
    102 static void bte_hci_enable(void);
    103 static void bte_hci_disable(void);
    104 static void preload_start_wait_timer(void);
    105 static void preload_stop_wait_timer(void);
    106 
    107 /*******************************************************************************
    108 **  Externs
    109 *******************************************************************************/
    110 BTU_API extern UINT32 btu_task (UINT32 param);
    111 BTU_API extern void BTE_Init (void);
    112 BT_API extern void BTE_LoadStack(void);
    113 BT_API void BTE_UnloadStack(void);
    114 extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
    115 extern void bte_load_conf(const char *p_path);
    116 extern void bte_load_ble_conf(const char *p_path);
    117 extern bt_bdaddr_t btif_local_bd_addr;
    118 
    119 
    120 /*******************************************************************************
    121 **                        System Task Configuration
    122 *******************************************************************************/
    123 
    124 /* bluetooth protocol stack (BTU) task */
    125 #ifndef BTE_BTU_STACK_SIZE
    126 #define BTE_BTU_STACK_SIZE       0//0x2000         /* In bytes */
    127 #endif
    128 #define BTE_BTU_TASK_STR        ((INT8 *) "BTU")
    129 UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
    130 
    131 /******************************************************************************
    132 **
    133 ** Function         bte_main_in_hw_init
    134 **
    135 ** Description      Internal helper function for chip hardware init
    136 **
    137 ** Returns          None
    138 **
    139 ******************************************************************************/
    140 static void bte_main_in_hw_init(void)
    141 {
    142     if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface()) \
    143          == NULL)
    144     {
    145         APPL_TRACE_ERROR("!!! Failed to get BtHostControllerInterface !!!");
    146     }
    147 
    148     memset(&preload_retry_cb, 0, sizeof(bt_preload_retry_cb_t));
    149 }
    150 
    151 /******************************************************************************
    152 **
    153 ** Function         bte_main_boot_entry
    154 **
    155 ** Description      BTE MAIN API - Entry point for BTE chip/stack initialization
    156 **
    157 ** Returns          None
    158 **
    159 ******************************************************************************/
    160 void bte_main_boot_entry(void)
    161 {
    162     /* initialize OS */
    163     GKI_init();
    164 
    165     bte_main_in_hw_init();
    166 
    167     bte_load_conf(BTE_STACK_CONF_FILE);
    168 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
    169     bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
    170 #endif
    171 
    172 #if (BTTRC_INCLUDED == TRUE)
    173     /* Initialize trace feature */
    174     BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
    175 #endif
    176 
    177     pthread_mutex_init(&cleanup_lock, NULL);
    178 
    179 }
    180 
    181 /******************************************************************************
    182 **
    183 ** Function         bte_main_shutdown
    184 **
    185 ** Description      BTE MAIN API - Shutdown code for BTE chip/stack
    186 **
    187 ** Returns          None
    188 **
    189 ******************************************************************************/
    190 void bte_main_shutdown()
    191 {
    192     pthread_mutex_destroy(&cleanup_lock);
    193 
    194     GKI_shutdown();
    195 }
    196 
    197 /******************************************************************************
    198 **
    199 ** Function         bte_main_enable
    200 **
    201 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called
    202 **                  part of the Bluetooth stack enable sequence
    203 **
    204 ** Returns          None
    205 **
    206 ******************************************************************************/
    207 void bte_main_enable()
    208 {
    209     APPL_TRACE_DEBUG("%s", __FUNCTION__);
    210 
    211     /* Initialize BTE control block */
    212     BTE_Init();
    213 
    214     lpm_enabled = FALSE;
    215 
    216     GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
    217                     (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
    218                     sizeof(bte_btu_stack));
    219 
    220     bte_hci_enable();
    221 
    222     GKI_run();
    223 }
    224 
    225 /******************************************************************************
    226 **
    227 ** Function         bte_main_disable
    228 **
    229 ** Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
    230 **                  part of the Bluetooth stack disable sequence
    231 **
    232 ** Returns          None
    233 **
    234 ******************************************************************************/
    235 void bte_main_disable(void)
    236 {
    237     APPL_TRACE_DEBUG("%s", __FUNCTION__);
    238 
    239     preload_stop_wait_timer();
    240     bte_hci_disable();
    241     GKI_destroy_task(BTU_TASK);
    242 }
    243 
    244 /******************************************************************************
    245 **
    246 ** Function         bte_main_config_hci_logging
    247 **
    248 ** Description      enable or disable HIC snoop logging
    249 **
    250 ** Returns          None
    251 **
    252 ******************************************************************************/
    253 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled)
    254 {
    255     int old = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
    256     int new;
    257 
    258     if (enable) {
    259         hci_logging_config = TRUE;
    260     } else {
    261         hci_logging_config = FALSE;
    262     }
    263 
    264     new = (hci_logging_enabled == TRUE) || (hci_logging_config == TRUE);
    265 
    266     if ((old == new) || bt_disabled || (bt_hc_if == NULL)) {
    267         return;
    268     }
    269 
    270     bt_hc_if->logging(new ? BT_HC_LOGGING_ON : BT_HC_LOGGING_OFF, hci_logfile, hci_save_log);
    271 }
    272 
    273 /******************************************************************************
    274 **
    275 ** Function         bte_hci_enable
    276 **
    277 ** Description      Enable HCI & Vendor modules
    278 **
    279 ** Returns          None
    280 **
    281 ******************************************************************************/
    282 static void bte_hci_enable(void)
    283 {
    284     APPL_TRACE_DEBUG("%s", __FUNCTION__);
    285 
    286     preload_start_wait_timer();
    287 
    288     if (bt_hc_if)
    289     {
    290         int result = bt_hc_if->init(&hc_callbacks, btif_local_bd_addr.address);
    291         APPL_TRACE_EVENT("libbt-hci init returns %d", result);
    292 
    293         assert(result == BT_HC_STATUS_SUCCESS);
    294 
    295         if (hci_logging_enabled == TRUE || hci_logging_config == TRUE)
    296             bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile, hci_save_log);
    297 
    298 #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
    299         APPL_TRACE_DEBUG("%s  Not Turninig Off the BT before Turninig ON", __FUNCTION__);
    300 
    301         /* Do not power off the chip before powering on  if BT_CLEAN_TURN_ON_DISABLED flag
    302          is defined and set to TRUE to avoid below mentioned issue.
    303 
    304          Wingray kernel driver maintains a combined  counter to keep track of
    305          BT-Wifi state. Invoking  set_power(BT_HC_CHIP_PWR_OFF) when the BT is already
    306          in OFF state causes this counter to be incorrectly decremented and results in undesired
    307          behavior of the chip.
    308 
    309          This is only a workaround and when the issue is fixed in the kernel this work around
    310          should be removed. */
    311 #else
    312         /* toggle chip power to ensure we will reset chip in case
    313            a previous stack shutdown wasn't completed gracefully */
    314         bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
    315 #endif
    316         bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
    317 
    318         bt_hc_if->preload(NULL);
    319     }
    320 }
    321 
    322 /******************************************************************************
    323 **
    324 ** Function         bte_hci_disable
    325 **
    326 ** Description      Disable HCI & Vendor modules
    327 **
    328 ** Returns          None
    329 **
    330 ******************************************************************************/
    331 static void bte_hci_disable(void)
    332 {
    333     APPL_TRACE_DEBUG("%s", __FUNCTION__);
    334 
    335     if (!bt_hc_if)
    336         return;
    337 
    338     // Cleanup is not thread safe and must be protected.
    339     pthread_mutex_lock(&cleanup_lock);
    340 
    341     if (hci_logging_enabled == TRUE ||  hci_logging_config == TRUE)
    342         bt_hc_if->logging(BT_HC_LOGGING_OFF, hci_logfile, hci_save_log);
    343     bt_hc_if->cleanup();
    344 
    345     pthread_mutex_unlock(&cleanup_lock);
    346 }
    347 
    348 /*******************************************************************************
    349 **
    350 ** Function        preload_wait_timeout
    351 **
    352 ** Description     Timeout thread of preload watchdog timer
    353 **
    354 ** Returns         None
    355 **
    356 *******************************************************************************/
    357 static void preload_wait_timeout(union sigval arg)
    358 {
    359     UNUSED(arg);
    360 
    361     APPL_TRACE_ERROR("...preload_wait_timeout (retried:%d/max-retry:%d)...",
    362                         preload_retry_cb.retry_counts,
    363                         PRELOAD_MAX_RETRY_ATTEMPTS);
    364 
    365     if (preload_retry_cb.retry_counts++ < PRELOAD_MAX_RETRY_ATTEMPTS)
    366     {
    367         bte_hci_disable();
    368         GKI_delay(100);
    369         bte_hci_enable();
    370     }
    371     else
    372     {
    373         /* Notify BTIF_TASK that the init procedure had failed*/
    374         GKI_send_event(BTIF_TASK, BT_EVT_HARDWARE_INIT_FAIL);
    375     }
    376 }
    377 
    378 /*******************************************************************************
    379 **
    380 ** Function        preload_start_wait_timer
    381 **
    382 ** Description     Launch startup watchdog timer
    383 **
    384 ** Returns         None
    385 **
    386 *******************************************************************************/
    387 static void preload_start_wait_timer(void)
    388 {
    389     int status;
    390     struct itimerspec ts;
    391     struct sigevent se;
    392     UINT32 timeout_ms = PRELOAD_START_TIMEOUT_MS;
    393 
    394     if (preload_retry_cb.timer_created == FALSE)
    395     {
    396         se.sigev_notify = SIGEV_THREAD;
    397         se.sigev_value.sival_ptr = &preload_retry_cb.timer_id;
    398         se.sigev_notify_function = preload_wait_timeout;
    399         se.sigev_notify_attributes = NULL;
    400 
    401         status = timer_create(CLOCK_MONOTONIC, &se, &preload_retry_cb.timer_id);
    402 
    403         if (status == 0)
    404             preload_retry_cb.timer_created = TRUE;
    405     }
    406 
    407     if (preload_retry_cb.timer_created == TRUE)
    408     {
    409         ts.it_value.tv_sec = timeout_ms/1000;
    410         ts.it_value.tv_nsec = 1000000*(timeout_ms%1000);
    411         ts.it_interval.tv_sec = 0;
    412         ts.it_interval.tv_nsec = 0;
    413 
    414         status = timer_settime(preload_retry_cb.timer_id, 0, &ts, 0);
    415         if (status == -1)
    416             APPL_TRACE_ERROR("Failed to fire preload watchdog timer");
    417     }
    418 }
    419 
    420 /*******************************************************************************
    421 **
    422 ** Function        preload_stop_wait_timer
    423 **
    424 ** Description     Stop preload watchdog timer
    425 **
    426 ** Returns         None
    427 **
    428 *******************************************************************************/
    429 static void preload_stop_wait_timer(void)
    430 {
    431     if (preload_retry_cb.timer_created == TRUE)
    432     {
    433         timer_delete(preload_retry_cb.timer_id);
    434         preload_retry_cb.timer_created = FALSE;
    435     }
    436 }
    437 
    438 /******************************************************************************
    439 **
    440 ** Function         bte_main_postload_cfg
    441 **
    442 ** Description      BTE MAIN API - Stack postload configuration
    443 **
    444 ** Returns          None
    445 **
    446 ******************************************************************************/
    447 void bte_main_postload_cfg(void)
    448 {
    449     if (bt_hc_if)
    450         bt_hc_if->postload(NULL);
    451 }
    452 
    453 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    454 /******************************************************************************
    455 **
    456 ** Function         bte_main_enable_lpm
    457 **
    458 ** Description      BTE MAIN API - Enable/Disable low power mode operation
    459 **
    460 ** Returns          None
    461 **
    462 ******************************************************************************/
    463 void bte_main_enable_lpm(BOOLEAN enable)
    464 {
    465     int result = -1;
    466 
    467     if (bt_hc_if)
    468         result = bt_hc_if->lpm( \
    469         (enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \
    470         );
    471 
    472     APPL_TRACE_EVENT("HC lib lpm enable=%d return %d", enable, result);
    473 }
    474 
    475 /******************************************************************************
    476 **
    477 ** Function         bte_main_lpm_allow_bt_device_sleep
    478 **
    479 ** Description      BTE MAIN API - Allow BT controller goest to sleep
    480 **
    481 ** Returns          None
    482 **
    483 ******************************************************************************/
    484 void bte_main_lpm_allow_bt_device_sleep()
    485 {
    486     int result = -1;
    487 
    488     if ((bt_hc_if) && (lpm_enabled == TRUE))
    489         result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT);
    490 
    491     APPL_TRACE_DEBUG("HC lib lpm deassertion return %d", result);
    492 }
    493 
    494 /******************************************************************************
    495 **
    496 ** Function         bte_main_lpm_wake_bt_device
    497 **
    498 ** Description      BTE MAIN API - Wake BT controller up if it is in sleep mode
    499 **
    500 ** Returns          None
    501 **
    502 ******************************************************************************/
    503 void bte_main_lpm_wake_bt_device()
    504 {
    505     int result = -1;
    506 
    507     if ((bt_hc_if) && (lpm_enabled == TRUE))
    508         result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT);
    509 
    510     APPL_TRACE_DEBUG("HC lib lpm assertion return %d", result);
    511 }
    512 #endif  // HCILP_INCLUDED
    513 
    514 
    515 /* NOTICE:
    516  *  Definitions for audio state structure, this type needs to match to
    517  *  the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
    518  */
    519 typedef struct {
    520     UINT16  handle;
    521     UINT16  peer_codec;
    522     UINT16  state;
    523 } bt_hc_audio_state_t;
    524 
    525 struct bt_audio_state_tag {
    526     BT_HDR hdr;
    527     bt_hc_audio_state_t audio;
    528 };
    529 
    530 /******************************************************************************
    531 **
    532 ** Function         set_audio_state
    533 **
    534 ** Description      Sets audio state on controller state for SCO (PCM, WBS, FM)
    535 **
    536 ** Parameters       handle: codec related handle for SCO: sco cb idx, unused for
    537 **                  codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
    538 **                  state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
    539 **                  param: future extensions, e.g. call-in structure/event.
    540 **
    541 ** Returns          None
    542 **
    543 ******************************************************************************/
    544 int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
    545 {
    546     struct bt_audio_state_tag *p_msg;
    547     int result = -1;
    548 
    549     APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
    550                     codec, state);
    551     if (NULL != param)
    552         APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
    553     p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
    554     if (!p_msg)
    555         return result;
    556     p_msg->audio.handle = handle;
    557     p_msg->audio.peer_codec = codec;
    558     p_msg->audio.state = state;
    559 
    560     p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
    561     p_msg->hdr.len = sizeof(p_msg->audio);
    562     p_msg->hdr.offset = 0;
    563     /* layer_specific shall contain return path event! for BTA events!
    564      * 0 means no return message is expected. */
    565     p_msg->hdr.layer_specific = 0;
    566        if (bt_hc_if)
    567        {
    568         bt_hc_if->tx_cmd((TRANSAC)p_msg, (char *)(&p_msg->audio), sizeof(*p_msg));
    569         }
    570     return result;
    571 }
    572 
    573 
    574 /******************************************************************************
    575 **
    576 ** Function         bte_main_hci_send
    577 **
    578 ** Description      BTE MAIN API - This function is called by the upper stack to
    579 **                  send an HCI message. The function displays a protocol trace
    580 **                  message (if enabled), and then calls the 'transmit' function
    581 **                  associated with the currently selected HCI transport
    582 **
    583 ** Returns          None
    584 **
    585 ******************************************************************************/
    586 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
    587 {
    588     UINT16 sub_event = event & BT_SUB_EVT_MASK;  /* local controller ID */
    589 
    590     p_msg->event = event;
    591 
    592 
    593     if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
    594        (sub_event == LOCAL_BLE_CONTROLLER_ID))
    595     {
    596         if (bt_hc_if)
    597             bt_hc_if->transmit_buf((TRANSAC)p_msg, \
    598                                        (char *) (p_msg + 1), \
    599                                         p_msg->len);
    600         else
    601             GKI_freebuf(p_msg);
    602     }
    603     else
    604     {
    605         APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
    606         GKI_freebuf(p_msg);
    607     }
    608 }
    609 
    610 /******************************************************************************
    611 **
    612 ** Function         bte_main_post_reset_init
    613 **
    614 ** Description      BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT
    615 **                  and shall be automatically called from BTE after HCI_Reset
    616 **
    617 ** Returns          None
    618 **
    619 ******************************************************************************/
    620 void bte_main_post_reset_init()
    621 {
    622     BTM_ContinueReset();
    623 }
    624 
    625 /*****************************************************************************
    626 **
    627 **   libbt-hci Callback Functions
    628 **
    629 *****************************************************************************/
    630 
    631 /******************************************************************************
    632 **
    633 ** Function         preload_cb
    634 **
    635 ** Description      HOST/CONTROLLER LIB CALLBACK API - This function is called
    636 **                  when the libbt-hci completed stack preload process
    637 **
    638 ** Returns          None
    639 **
    640 ******************************************************************************/
    641 static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result)
    642 {
    643     UNUSED(transac);
    644 
    645     APPL_TRACE_EVENT("HC preload_cb %d [0:SUCCESS 1:FAIL]", result);
    646 
    647     if (result == BT_HC_PRELOAD_SUCCESS)
    648     {
    649         preload_stop_wait_timer();
    650 
    651         /* notify BTU task that libbt-hci is ready */
    652         GKI_send_event(BTU_TASK, BT_EVT_PRELOAD_CMPL);
    653     }
    654 }
    655 
    656 /******************************************************************************
    657 **
    658 ** Function         postload_cb
    659 **
    660 ** Description      HOST/CONTROLLER LIB CALLBACK API - This function is called
    661 **                  when the libbt-hci lib completed stack postload process
    662 **
    663 ** Returns          None
    664 **
    665 ******************************************************************************/
    666 static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result)
    667 {
    668     UNUSED(transac);
    669 
    670     APPL_TRACE_EVENT("HC postload_cb %d", result);
    671 }
    672 
    673 /******************************************************************************
    674 **
    675 ** Function         lpm_cb
    676 **
    677 ** Description      HOST/CONTROLLER LIB CALLBACK API - This function is called
    678 **                  back from the libbt-hci to indicate the current LPM state
    679 **
    680 ** Returns          None
    681 **
    682 ******************************************************************************/
    683 static void lpm_cb(bt_hc_lpm_request_result_t result)
    684 {
    685     APPL_TRACE_EVENT("HC lpm_result_cb %d", result);
    686     lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE;
    687 }
    688 
    689 /******************************************************************************
    690 **
    691 ** Function         hostwake_ind
    692 **
    693 ** Description      HOST/CONTROLLER LIB CALLOUT API - This function is called
    694 **                  from the libbt-hci to indicate the HostWake event
    695 **
    696 ** Returns          None
    697 **
    698 ******************************************************************************/
    699 static void hostwake_ind(bt_hc_low_power_event_t event)
    700 {
    701     APPL_TRACE_EVENT("HC hostwake_ind %d", event);
    702 }
    703 
    704 /******************************************************************************
    705 **
    706 ** Function         alloc
    707 **
    708 ** Description      HOST/CONTROLLER LIB CALLOUT API - This function is called
    709 **                  from the libbt-hci to request for data buffer allocation
    710 **
    711 ** Returns          NULL / pointer to allocated buffer
    712 **
    713 ******************************************************************************/
    714 static char *alloc(int size)
    715 {
    716     BT_HDR *p_hdr = NULL;
    717 
    718     /*
    719     APPL_TRACE_DEBUG("HC alloc size=%d", size);
    720     */
    721 
    722     /* Requested buffer size cannot exceed GKI_MAX_BUF_SIZE. */
    723     if (size > GKI_MAX_BUF_SIZE)
    724     {
    725          APPL_TRACE_ERROR("HCI DATA SIZE %d greater than MAX %d",
    726                            size, GKI_MAX_BUF_SIZE);
    727          return NULL;
    728     }
    729 
    730     p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
    731 
    732     if (p_hdr == NULL)
    733     {
    734         APPL_TRACE_WARNING("alloc returns NO BUFFER! (sz %d)", size);
    735     }
    736 
    737     return ((char *) p_hdr);
    738 }
    739 
    740 /******************************************************************************
    741 **
    742 ** Function         dealloc
    743 **
    744 ** Description      HOST/CONTROLLER LIB CALLOUT API - This function is called
    745 **                  from the libbt-hci to release the data buffer allocated
    746 **                  through the alloc call earlier
    747 **
    748 **                  Bluedroid libbt-hci library uses 'transac' parameter to
    749 **                  pass data-path buffer/packet across bt_hci_lib interface
    750 **                  boundary.
    751 **
    752 ******************************************************************************/
    753 static void dealloc(TRANSAC transac)
    754 {
    755     GKI_freebuf(transac);
    756 }
    757 
    758 /******************************************************************************
    759 **
    760 ** Function         data_ind
    761 **
    762 ** Description      HOST/CONTROLLER LIB CALLOUT API - This function is called
    763 **                  from the libbt-hci to pass in the received HCI packets
    764 **
    765 **                  The core stack is responsible for releasing the data buffer
    766 **                  passed in from the libbt-hci once the core stack has done
    767 **                  with it.
    768 **
    769 **                  Bluedroid libbt-hci library uses 'transac' parameter to
    770 **                  pass data-path buffer/packet across bt_hci_lib interface
    771 **                  boundary. The 'p_buf' and 'len' parameters are not intended
    772 **                  to be used here but might point to data portion in data-
    773 **                  path buffer and length of valid data respectively.
    774 **
    775 ** Returns          bt_hc_status_t
    776 **
    777 ******************************************************************************/
    778 static int data_ind(TRANSAC transac, char *p_buf, int len)
    779 {
    780     BT_HDR *p_msg = (BT_HDR *) transac;
    781     UNUSED(p_buf);
    782     UNUSED(len);
    783 
    784     /*
    785     APPL_TRACE_DEBUG("HC data_ind event=0x%04X (len=%d)", p_msg->event, len);
    786     */
    787 
    788     GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
    789     return BT_HC_STATUS_SUCCESS;
    790 }
    791 
    792 /******************************************************************************
    793 **
    794 ** Function         tx_result
    795 **
    796 ** Description      HOST/CONTROLLER LIB CALLBACK API - This function is called
    797 **                  from the libbt-hci once it has processed/sent the prior data
    798 **                  buffer which core stack passed to it through transmit_buf
    799 **                  call earlier.
    800 **
    801 **                  The core stack is responsible for releasing the data buffer
    802 **                  if it has been completedly processed.
    803 **
    804 **                  Bluedroid libbt-hci library uses 'transac' parameter to
    805 **                  pass data-path buffer/packet across bt_hci_lib interface
    806 **                  boundary. The 'p_buf' is not intended to be used here
    807 **                  but might point to data portion in data-path buffer.
    808 **
    809 ** Returns          bt_hc_status_t
    810 **
    811 ******************************************************************************/
    812 static int tx_result(TRANSAC transac, char *p_buf, bt_hc_transmit_result_t result)
    813 {
    814     UNUSED(p_buf);
    815     /*
    816     APPL_TRACE_DEBUG("HC tx_result %d (event=%04X)", result, \
    817                       ((BT_HDR *)transac)->event);
    818     */
    819 
    820     if (result == BT_HC_TX_FRAGMENT)
    821     {
    822         GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
    823     }
    824     else
    825     {
    826         GKI_freebuf(transac);
    827     }
    828 
    829     return BT_HC_STATUS_SUCCESS;
    830 }
    831 
    832 /*****************************************************************************
    833 **   The libbt-hci Callback Functions Table
    834 *****************************************************************************/
    835 static const bt_hc_callbacks_t hc_callbacks = {
    836     sizeof(bt_hc_callbacks_t),
    837     preload_cb,
    838     postload_cb,
    839     lpm_cb,
    840     hostwake_ind,
    841     alloc,
    842     dealloc,
    843     data_ind,
    844     tx_result
    845 };
    846 
    847