diff --git a/components/bt/bluedroid/api/esp_bt_main.c b/components/bt/bluedroid/api/esp_bt_main.c index 549865e743..f072359589 100644 --- a/components/bt/bluedroid/api/esp_bt_main.c +++ b/components/bt/bluedroid/api/esp_bt_main.c @@ -60,7 +60,11 @@ esp_err_t esp_bluedroid_enable(void) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_MAIN_INIT; msg.act = BTC_MAIN_ACT_ENABLE; - btc_transfer_context(&msg, NULL, 0, NULL); + + if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) { + LOG_ERROR("Bluedroid enable failed\n"); + return ESP_FAIL; + } if (future_await(*future_p) == FUTURE_FAIL) { LOG_ERROR("Bluedroid enable failed\n"); @@ -92,7 +96,11 @@ esp_err_t esp_bluedroid_disable(void) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_MAIN_INIT; msg.act = BTC_MAIN_ACT_DISABLE; - btc_transfer_context(&msg, NULL, 0, NULL); + + if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) { + LOG_ERROR("Bluedroid disable failed\n"); + return ESP_FAIL; + } if (future_await(*future_p) == FUTURE_FAIL) { LOG_ERROR("Bluedroid disable failed\n"); @@ -131,7 +139,11 @@ esp_err_t esp_bluedroid_init(void) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_MAIN_INIT; msg.act = BTC_MAIN_ACT_INIT; - btc_transfer_context(&msg, NULL, 0, NULL); + + if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) { + LOG_ERROR("Bluedroid initialise failed\n"); + return ESP_FAIL; + } if (future_await(*future_p) == FUTURE_FAIL) { LOG_ERROR("Bluedroid initialise failed\n"); @@ -169,7 +181,11 @@ esp_err_t esp_bluedroid_deinit(void) msg.sig = BTC_SIG_API_CALL; msg.pid = BTC_PID_MAIN_INIT; msg.act = BTC_MAIN_ACT_DEINIT; - btc_transfer_context(&msg, NULL, 0, NULL); + + if (btc_transfer_context(&msg, NULL, 0, NULL) != BT_STATUS_SUCCESS) { + LOG_ERROR("Bluedroid de-initialise failed\n"); + return ESP_FAIL; + } if (future_await(*future_p) == FUTURE_FAIL) { LOG_ERROR("Bluedroid de-initialise failed\n"); diff --git a/components/bt/bluedroid/bta/sys/bta_sys_main.c b/components/bt/bluedroid/bta/sys/bta_sys_main.c index 59b9ed4d37..c1d1c0018a 100644 --- a/components/bt/bluedroid/bta/sys/bta_sys_main.c +++ b/components/bt/bluedroid/bta/sys/bta_sys_main.c @@ -578,7 +578,7 @@ void bta_sys_sendmsg(void *p_msg) if (btu_bta_msg_queue) { fixed_queue_enqueue(btu_bta_msg_queue, p_msg); //ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } } @@ -599,7 +599,7 @@ void bta_alarm_cb(void *data) fixed_queue_enqueue(btu_bta_alarm_queue, p_tle); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms) diff --git a/components/bt/bluedroid/btc/core/btc_task.c b/components/bt/bluedroid/btc/core/btc_task.c index dcecf83c28..51b9ea247a 100644 --- a/components/bt/bluedroid/btc/core/btc_task.c +++ b/components/bt/bluedroid/btc/core/btc_task.c @@ -94,13 +94,13 @@ static void btc_task(void *arg) } } -static bt_status_t btc_task_post(btc_msg_t *msg) +static bt_status_t btc_task_post(btc_msg_t *msg, task_post_t timeout) { if (msg == NULL) { return BT_STATUS_PARM_INVALID; } - if (xQueueSend(xBtcQueue, msg, 10 / portTICK_PERIOD_MS) != pdTRUE) { + if (xQueueSend(xBtcQueue, msg, timeout) != pdTRUE) { LOG_ERROR("Btc Post failed\n"); return BT_STATUS_BUSY; } @@ -133,7 +133,7 @@ bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg lmsg.arg = NULL; } - return btc_task_post(&lmsg); + return btc_task_post(&lmsg, TASK_POST_BLOCKING); } diff --git a/components/bt/bluedroid/hci/hci_hal_h4.c b/components/bt/bluedroid/hci/hci_hal_h4.c index 021137511c..30e225e951 100644 --- a/components/bt/bluedroid/hci/hci_hal_h4.c +++ b/components/bt/bluedroid/hci/hci_hal_h4.c @@ -170,14 +170,14 @@ static void hci_hal_h4_rx_handler(void *arg) } } -void hci_hal_h4_task_post(void) +void hci_hal_h4_task_post(task_post_t timeout) { BtTaskEvt_t evt; evt.sig = 0xff; evt.par = 0; - if (xQueueSend(xHciH4Queue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) { + if (xQueueSend(xHciH4Queue, &evt, timeout) != pdTRUE) { LOG_ERROR("xHciH4Queue failed\n"); } } @@ -248,7 +248,7 @@ static void host_send_pkt_available_cb(void) { //Controller rx cache buffer is ready for receiving new host packet //Just Call Host main thread task to process pending packets. - hci_host_task_post(); + hci_host_task_post(TASK_POST_BLOCKING); } static int host_recv_pkt_cb(uint8_t *data, uint16_t len) @@ -268,7 +268,7 @@ static int host_recv_pkt_cb(uint8_t *data, uint16_t len) pkt->layer_specific = 0; memcpy(pkt->data, data, len); fixed_queue_enqueue(hci_hal_env.rx_q, pkt); - hci_hal_h4_task_post(); + hci_hal_h4_task_post(TASK_POST_BLOCKING); BTTRC_DUMP_BUFFER("Recv Pkt", pkt->data, len); diff --git a/components/bt/bluedroid/hci/hci_layer.c b/components/bt/bluedroid/hci/hci_layer.c index 14a36eac81..4273074217 100644 --- a/components/bt/bluedroid/hci/hci_layer.c +++ b/components/bt/bluedroid/hci/hci_layer.c @@ -135,7 +135,7 @@ void hci_shut_down(void) } -void hci_host_task_post(void) +void hci_host_task_post(task_post_t timeout) { BtTaskEvt_t evt; @@ -146,7 +146,7 @@ void hci_host_task_post(void) evt.sig = 0xff; evt.par = 0; - if (xQueueSend(xHciHostQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) { + if (xQueueSend(xHciHostQueue, &evt, timeout) != pdTRUE) { LOG_ERROR("xHciHostQueue failed\n"); } } @@ -279,7 +279,7 @@ static void transmit_command( BTTRC_DUMP_BUFFER(NULL, command->data + command->offset, command->len); fixed_queue_enqueue(hci_host_env.command_queue, wait_entry); - hci_host_task_post(); + hci_host_task_post(TASK_POST_BLOCKING); } static future_t *transmit_command_futured(BT_HDR *command) @@ -299,7 +299,7 @@ static future_t *transmit_command_futured(BT_HDR *command) command->event = MSG_STACK_TO_HC_HCI_CMD; fixed_queue_enqueue(hci_host_env.command_queue, wait_entry); - hci_host_task_post(); + hci_host_task_post(TASK_POST_BLOCKING); return future; } @@ -312,7 +312,7 @@ static void transmit_downward(uint16_t type, void *data) fixed_queue_enqueue(hci_host_env.packet_queue, data); } //ke_event_set(KE_EVENT_HCI_HOST_THREAD); - hci_host_task_post(); + hci_host_task_post(TASK_POST_BLOCKING); } @@ -493,7 +493,7 @@ intercepted: /*Tell HCI Host Task to continue TX Pending commands*/ if (hci_host_env.command_credits && !fixed_queue_is_empty(hci_host_env.command_queue)) { - hci_host_task_post(); + hci_host_task_post(TASK_POST_BLOCKING); } //ke_event_set(KE_EVENT_HCI_HOST_THREAD); @@ -524,7 +524,7 @@ static void dispatch_reassembled(BT_HDR *packet) if (hci_host_env.upwards_data_queue) { fixed_queue_enqueue(hci_host_env.upwards_data_queue, packet); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); //Tell Up-layer received packet. } else { LOG_DEBUG("%s had no queue to place upwards data packet in. Dropping it on the floor.", __func__); diff --git a/components/bt/bluedroid/osi/include/thread.h b/components/bt/bluedroid/osi/include/thread.h index c10f8c2afd..4d2b435962 100644 --- a/components/bt/bluedroid/osi/include/thread.h +++ b/components/bt/bluedroid/osi/include/thread.h @@ -62,11 +62,14 @@ enum { #define BTC_TASK_PRIO (configMAX_PRIORITIES - 6) #define BTC_TASK_QUEUE_NUM 60 -void btu_task_post(uint32_t sig); -void hci_host_task_post(void); -void hci_hal_h4_task_post(void); -void hci_drv_task_post(void); -void bt_alarm_task_post(void); + +#define TASK_POST_NON_BLOCKING (0) +#define TASK_POST_BLOCKING (portMAX_DELAY) +typedef uint32_t task_post_t; /* Timeout of task post return, unit TICK */ + +void btu_task_post(uint32_t sig, task_post_t timeout); +void hci_host_task_post(task_post_t timeout); +void hci_hal_h4_task_post(task_post_t timeout); #endif /* __THREAD_H__ */ diff --git a/components/bt/bluedroid/stack/btu/btu_hcif.c b/components/bt/bluedroid/stack/btu/btu_hcif.c index 9f28928075..7d00f0b76f 100644 --- a/components/bt/bluedroid/stack/btu/btu_hcif.c +++ b/components/bt/bluedroid/stack/btu/btu_hcif.c @@ -1011,7 +1011,7 @@ static void btu_hcif_command_complete_evt(BT_HDR *response, void *context) fixed_queue_enqueue(btu_hci_msg_queue, event); // ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } @@ -1211,7 +1211,7 @@ static void btu_hcif_command_status_evt(uint8_t status, BT_HDR *command, void *c fixed_queue_enqueue(btu_hci_msg_queue, event); //ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } /******************************************************************************* diff --git a/components/bt/bluedroid/stack/btu/btu_init.c b/components/bt/bluedroid/stack/btu/btu_init.c index 03a03f6e95..df387bceb3 100644 --- a/components/bt/bluedroid/stack/btu/btu_init.c +++ b/components/bt/bluedroid/stack/btu/btu_init.c @@ -202,7 +202,7 @@ void BTU_StartUp(void) xBtuQueue = xQueueCreate(BTU_QUEUE_NUM, sizeof(BtTaskEvt_t)); xTaskCreatePinnedToCore(btu_task_thread_handler, BTU_TASK_NAME, BTU_TASK_STACK_SIZE, NULL, BTU_TASK_PRIO, &xBtuTaskHandle, 0); - btu_task_post(SIG_BTU_START_UP); + btu_task_post(SIG_BTU_START_UP, TASK_POST_BLOCKING); /* // Continue startup on bt workqueue thread. thread_post(bt_workqueue_thread, btu_task_start_up, NULL); diff --git a/components/bt/bluedroid/stack/btu/btu_task.c b/components/bt/bluedroid/stack/btu/btu_task.c index 9e02627de2..d41e7bfd25 100644 --- a/components/bt/bluedroid/stack/btu/btu_task.c +++ b/components/bt/bluedroid/stack/btu/btu_task.c @@ -334,14 +334,14 @@ void btu_task_thread_handler(void *arg) } -void btu_task_post(uint32_t sig) +void btu_task_post(uint32_t sig, task_post_t timeout) { BtTaskEvt_t evt; evt.sig = sig; evt.par = 0; - if (xQueueSend(xBtuQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) { + if (xQueueSend(xBtuQueue, &evt, timeout) != pdTRUE) { LOG_ERROR("xBtuQueue failed\n"); } } @@ -516,7 +516,7 @@ void btu_general_alarm_cb(void *data) fixed_queue_enqueue(btu_general_alarm_queue, p_tle); //ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) @@ -606,7 +606,7 @@ static void btu_l2cap_alarm_cb(void *data) fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle); //ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) @@ -674,7 +674,7 @@ void btu_oneshot_alarm_cb(void *data) fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle); //ke_event_set(KE_EVENT_BTU_TASK_THREAD); - btu_task_post(SIG_BTU_WORK); + btu_task_post(SIG_BTU_WORK, TASK_POST_BLOCKING); } /* diff --git a/components/bt/bt.c b/components/bt/bt.c index bf933af08f..86f757ad2e 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -33,6 +33,8 @@ #if CONFIG_BT_ENABLED +#define BTDM_INIT_PERIOD (5000) /* ms */ + /* Bluetooth system and controller config */ #define BTDM_CFG_BT_EM_RELEASE (1<<0) #define BTDM_CFG_BT_DATA_RELEASE (1<<1) @@ -89,6 +91,7 @@ struct osi_funcs_t { /* Static variable declare */ static bool btdm_bb_init_flag = false; +static xSemaphoreHandle btdm_init_sem; static esp_bt_controller_status_t btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; static esp_bt_controller_config_t btdm_cfg_opts; static xTaskHandle btControllerTaskHandle; @@ -215,8 +218,13 @@ static void bt_controller_task(void *pvParam) btdm_controller_init(btdm_cfg_mask, &btdm_cfg_opts); btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED; + + xSemaphoreGive(btdm_init_sem); + /* Loop */ btdm_controller_schedule(); + + /* never run here */ } esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) @@ -231,6 +239,11 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) return ESP_ERR_INVALID_ARG; } + btdm_init_sem = xSemaphoreCreateBinary(); + if (btdm_init_sem == NULL) { + return ESP_ERR_NO_MEM; + } + memcpy(&btdm_cfg_opts, cfg, sizeof(esp_bt_controller_config_t)); ret = xTaskCreatePinnedToCore(bt_controller_task, "btController", @@ -239,9 +252,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) if (ret != pdPASS) { memset(&btdm_cfg_opts, 0x0, sizeof(esp_bt_controller_config_t)); + vSemaphoreDelete(btdm_init_sem); return ESP_ERR_NO_MEM; } + xSemaphoreTake(btdm_init_sem, BTDM_INIT_PERIOD/portTICK_PERIOD_MS); + vSemaphoreDelete(btdm_init_sem); + return ESP_OK; }