1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
23 #include "bt_target.h"
26 #include "allocator.h"
35 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
39 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
44 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
48 #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
52 #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
56 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
60 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
63 extern void avdt_rcv_sync_info (BT_HDR *p_buf);
66 #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
72 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
76 #if (BLE_INCLUDED == TRUE)
78 #if (SMP_INCLUDED == TRUE)
81 #include "btm_ble_int.h"
84 //#if (defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
85 //#include "bt_app_common.h"
88 extern void BTE_InitStack(void);
90 /* Define BTU storage area
92 #if BTU_DYNAMIC_MEMORY == FALSE
98 extern hash_map_t *btu_general_alarm_hash_map;
99 extern osi_mutex_t btu_general_alarm_lock;
101 // Oneshot timer queue.
102 extern hash_map_t *btu_oneshot_alarm_hash_map;
103 extern osi_mutex_t btu_oneshot_alarm_lock;
105 // l2cap timer queue.
106 extern hash_map_t *btu_l2cap_alarm_hash_map;
107 extern osi_mutex_t btu_l2cap_alarm_lock;
109 extern xTaskHandle xBtuTaskHandle;
110 extern xQueueHandle xBtuQueue;
111 extern bluedroid_init_done_cb_t bluedroid_init_done_cb;
113 /* Define a function prototype to allow a generic timeout handler */
114 typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
116 static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
117 static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
118 static void btu_hci_msg_process(BT_HDR *p_msg);
120 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
121 static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
124 static void btu_hci_msg_process(BT_HDR *p_msg)
126 /* Determine the input message type. */
127 switch (p_msg->event & BT_EVT_MASK) {
128 case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
130 post_to_task_hack_t *ph = (post_to_task_hack_t *) &p_msg->data[0];
134 case BT_EVT_TO_BTU_HCI_ACL:
135 /* All Acl Data goes to L2CAP */
136 l2c_rcv_acl_data (p_msg);
139 case BT_EVT_TO_BTU_L2C_SEG_XMIT:
140 /* L2CAP segment transmit complete */
141 l2c_link_segments_xmitted (p_msg);
144 case BT_EVT_TO_BTU_HCI_SCO:
145 #if BTM_SCO_INCLUDED == TRUE
146 btm_route_sco_data (p_msg);
150 case BT_EVT_TO_BTU_HCI_EVT:
151 btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
154 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
155 /* If host receives events which it doesn't response to, */
156 /* host should start idle timer to enter sleep mode. */
157 btu_check_bt_sleep ();
161 case BT_EVT_TO_BTU_HCI_CMD:
162 btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
167 uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
168 BOOLEAN handled = FALSE;
170 for (; !handled && i < BTU_MAX_REG_EVENT; i++) {
171 if (btu_cb.event_reg[i].event_cb == NULL) {
175 if (mask == btu_cb.event_reg[i].event_range) {
176 if (btu_cb.event_reg[i].event_cb) {
177 btu_cb.event_reg[i].event_cb(p_msg);
183 if (handled == FALSE) {
192 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
193 static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle)
195 // call timer callback
196 if (p_tle->p_cback) {
197 (*p_tle->p_cback)(p_tle);
198 } else if (p_tle->event) {
200 if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
201 p_msg->event = p_tle->event;
202 p_msg->layer_specific = 0;
204 bta_sys_sendmsg(p_msg);
210 /*****************************************************************************
212 ** Function btu_task_thread_handler
214 ** Description Process BTU Task Thread.
215 ******************************************************************************/
216 void btu_task_thread_handler(void *arg)
221 if (pdTRUE == xQueueReceive(xBtuQueue, &e, (portTickType)portMAX_DELAY)) {
224 case SIG_BTU_START_UP:
227 case SIG_BTU_HCI_MSG:
228 btu_hci_msg_process((BT_HDR *)e.par);
230 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
231 case SIG_BTU_BTA_MSG:
232 bta_sys_event((BT_HDR *)e.par);
234 case SIG_BTU_BTA_ALARM:
235 btu_bta_alarm_process((TIMER_LIST_ENT *)e.par);
238 case SIG_BTU_GENERAL_ALARM:
239 btu_general_alarm_process((TIMER_LIST_ENT *)e.par);
241 case SIG_BTU_ONESHOT_ALARM: {
242 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)e.par;
243 btu_general_alarm_process(p_tle);
245 switch (p_tle->event) {
246 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
247 case BTU_TTYPE_BLE_RANDOM_ADDR:
248 btm_ble_timeout(p_tle);
251 case BTU_TTYPE_USER_FUNC: {
252 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
258 LOG_ERROR("Received unexpected oneshot timer event:0x%x\n", p_tle->event);
263 case SIG_BTU_L2CAP_ALARM:
264 btu_l2cap_alarm_process((TIMER_LIST_ENT *)e.par);
274 task_post_status_t btu_task_post(uint32_t sig, void *param, task_post_t timeout)
281 if (xQueueSend(xBtuQueue, &evt, timeout) != pdTRUE) {
282 LOG_ERROR("xBtuQueue failed\n");
283 return TASK_POST_FAIL;
286 return TASK_POST_SUCCESS;
289 void btu_task_start_up(void)
291 /* Initialize the mandatory core stack control blocks
292 (BTU, BTM, L2CAP, and SDP)
296 /* Initialize any optional stack components */
299 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
303 // Inform the bt jni thread initialization is ok.
304 // btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
305 #if(defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
306 if (bluedroid_init_done_cb) {
307 bluedroid_init_done_cb();
312 void btu_task_shut_down(void)
314 #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
321 /*******************************************************************************
323 ** Function btu_start_timer
325 ** Description Start a timer for the specified amount of time.
326 ** NOTE: The timeout resolution is in SECONDS! (Even
327 ** though the timer structure field is ticks)
331 *******************************************************************************/
332 static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle)
334 assert(p_tle != NULL);
336 switch (p_tle->event) {
337 case BTU_TTYPE_BTM_DEV_CTL:
338 btm_dev_timeout(p_tle);
341 case BTU_TTYPE_L2CAP_LINK:
342 case BTU_TTYPE_L2CAP_CHNL:
343 case BTU_TTYPE_L2CAP_HOLD:
344 case BTU_TTYPE_L2CAP_INFO:
345 case BTU_TTYPE_L2CAP_FCR_ACK:
346 case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS:
347 l2c_process_timeout (p_tle);
349 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
351 sdp_conn_timeout ((tCONN_CB *)p_tle->param);
354 case BTU_TTYPE_BTM_RMT_NAME:
355 btm_inq_rmt_name_failed();
357 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
358 case BTU_TTYPE_RFCOMM_MFC:
359 case BTU_TTYPE_RFCOMM_PORT:
360 rfcomm_process_timeout (p_tle);
363 #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
365 bnep_process_timeout(p_tle);
370 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
371 case BTU_TTYPE_AVDT_CCB_RET:
372 case BTU_TTYPE_AVDT_CCB_RSP:
373 case BTU_TTYPE_AVDT_CCB_IDLE:
374 case BTU_TTYPE_AVDT_SCB_TC:
375 avdt_process_timeout(p_tle);
379 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
380 case BTU_TTYPE_HID_HOST_REPAGE_TO :
381 hidh_proc_repage_timeout(p_tle);
385 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
386 case BTU_TTYPE_BLE_INQUIRY:
387 case BTU_TTYPE_BLE_GAP_LIM_DISC:
388 case BTU_TTYPE_BLE_RANDOM_ADDR:
389 case BTU_TTYPE_BLE_GAP_FAST_ADV:
390 case BTU_TTYPE_BLE_SCAN:
391 case BTU_TTYPE_BLE_OBSERVE:
392 btm_ble_timeout(p_tle);
395 case BTU_TTYPE_ATT_WAIT_FOR_RSP:
396 gatt_rsp_timeout(p_tle);
399 case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
400 gatt_ind_ack_timeout(p_tle);
403 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
404 case BTU_TTYPE_SMP_PAIRING_CMD:
405 smp_rsp_timeout(p_tle);
411 #if (MCA_INCLUDED == TRUE)
412 case BTU_TTYPE_MCA_CCB_RSP:
413 mca_process_timeout(p_tle);
416 case BTU_TTYPE_USER_FUNC: {
417 tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
424 BOOLEAN handled = FALSE;
426 for (; !handled && i < BTU_MAX_REG_TIMER; i++) {
427 if (btu_cb.timer_reg[i].timer_cb == NULL) {
430 if (btu_cb.timer_reg[i].p_tle == p_tle) {
431 btu_cb.timer_reg[i].timer_cb(p_tle);
439 void btu_general_alarm_cb(void *data)
441 assert(data != NULL);
442 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
444 btu_task_post(SIG_BTU_GENERAL_ALARM, p_tle, TASK_POST_BLOCKING);
447 void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
449 osi_alarm_t *alarm = NULL;
451 assert(p_tle != NULL);
453 // Get the alarm for the timer list entry.
454 osi_mutex_lock(&btu_general_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
455 if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
456 alarm = osi_alarm_new("btu_gen", btu_general_alarm_cb, (void *)p_tle, 0);
457 hash_map_set(btu_general_alarm_hash_map, p_tle, alarm);
459 osi_mutex_unlock(&btu_general_alarm_lock);
461 alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
463 LOG_ERROR("%s Unable to create alarm", __func__);
466 osi_alarm_cancel(alarm);
469 // NOTE: This value is in seconds but stored in a ticks field.
470 p_tle->ticks = timeout_sec;
471 p_tle->in_use = TRUE;
472 osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
476 /*******************************************************************************
478 ** Function btu_stop_timer
480 ** Description Stop a timer.
484 *******************************************************************************/
485 void btu_stop_timer(TIMER_LIST_ENT *p_tle)
487 assert(p_tle != NULL);
489 if (p_tle->in_use == FALSE) {
492 p_tle->in_use = FALSE;
494 // Get the alarm for the timer list entry.
495 osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
497 LOG_WARN("%s Unable to find expected alarm in hashmap", __func__);
500 osi_alarm_cancel(alarm);
503 /*******************************************************************************
505 ** Function btu_free_timer
507 ** Description Stop and free a timer.
511 *******************************************************************************/
512 void btu_free_timer(TIMER_LIST_ENT *p_tle)
514 assert(p_tle != NULL);
516 if (p_tle->in_use == FALSE) {
519 p_tle->in_use = FALSE;
521 // Get the alarm for the timer list entry.
522 osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
524 LOG_WARN("%s Unable to find expected alarm in hashmap", __func__);
527 osi_alarm_cancel(alarm);
528 hash_map_erase(btu_general_alarm_hash_map, p_tle);
531 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
532 /*******************************************************************************
534 ** Function btu_start_quick_timer
536 ** Description Start a timer for the specified amount of time in ticks.
540 *******************************************************************************/
541 static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle)
543 assert(p_tle != NULL);
545 switch (p_tle->event) {
546 case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
547 case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
548 l2c_process_timeout (p_tle);
556 static void btu_l2cap_alarm_cb(void *data)
558 assert(data != NULL);
559 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
561 btu_task_post(SIG_BTU_L2CAP_ALARM, p_tle, TASK_POST_BLOCKING);
564 void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
566 osi_alarm_t *alarm = NULL;
568 assert(p_tle != NULL);
570 // Get the alarm for the timer list entry.
571 osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
572 if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
573 alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
574 hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
576 osi_mutex_unlock(&btu_l2cap_alarm_lock);
578 alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
580 LOG_ERROR("%s Unable to create alarm", __func__);
583 osi_alarm_cancel(alarm);
586 p_tle->ticks = timeout_ticks;
587 p_tle->in_use = TRUE;
588 // The quick timer ticks are 100ms long.
589 osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
592 /*******************************************************************************
594 ** Function btu_stop_quick_timer
596 ** Description Stop a timer.
600 *******************************************************************************/
601 void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle)
603 assert(p_tle != NULL);
605 if (p_tle->in_use == FALSE) {
608 p_tle->in_use = FALSE;
610 // Get the alarm for the timer list entry.
611 osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
613 LOG_WARN("%s Unable to find expected alarm in hashmap", __func__);
616 osi_alarm_cancel(alarm);
618 #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
620 void btu_oneshot_alarm_cb(void *data)
622 assert(data != NULL);
623 TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
625 btu_stop_timer_oneshot(p_tle);
627 btu_task_post(SIG_BTU_ONESHOT_ALARM, p_tle, TASK_POST_BLOCKING);
631 * Starts a oneshot timer with a timeout in seconds.
633 void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
635 osi_alarm_t *alarm = NULL;
637 assert(p_tle != NULL);
639 // Get the alarm for the timer list entry.
640 osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
641 if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
642 alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
643 hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
645 osi_mutex_unlock(&btu_oneshot_alarm_lock);
647 alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
649 LOG_ERROR("%s Unable to create alarm", __func__);
652 osi_alarm_cancel(alarm);
655 p_tle->in_use = TRUE;
656 // NOTE: This value is in seconds but stored in a ticks field.
657 p_tle->ticks = timeout_sec;
658 osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
661 void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle)
663 assert(p_tle != NULL);
665 if (p_tle->in_use == FALSE) {
668 p_tle->in_use = FALSE;
670 // Get the alarm for the timer list entry.
671 osi_alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
673 LOG_WARN("%s Unable to find expected alarm in hashmap", __func__);
676 osi_alarm_cancel(alarm);
679 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
680 /*******************************************************************************
682 ** Function btu_check_bt_sleep
684 ** Description This function is called to check if controller can go to sleep.
688 *******************************************************************************/
689 void btu_check_bt_sleep (void)
691 // TODO(zachoverflow) take pending commands into account?
692 if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) {
693 bte_main_lpm_allow_bt_device_sleep();