OSDN Git Service

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