]> granicus.if.org Git - esp-idf/commitdiff
component/bt : fix task post bug && fix controller init protection
authorTian Hao <tianhao@espressif.com>
Tue, 13 Jun 2017 09:14:50 +0000 (17:14 +0800)
committerTian Hao <tianhao@espressif.com>
Tue, 13 Jun 2017 09:14:50 +0000 (17:14 +0800)
1. fix bluedroid task post to blocking.
2. use semphore protect btdm controller task when be called from higher priority task

components/bt/bluedroid/api/esp_bt_main.c
components/bt/bluedroid/bta/sys/bta_sys_main.c
components/bt/bluedroid/btc/core/btc_task.c
components/bt/bluedroid/hci/hci_hal_h4.c
components/bt/bluedroid/hci/hci_layer.c
components/bt/bluedroid/osi/include/thread.h
components/bt/bluedroid/stack/btu/btu_hcif.c
components/bt/bluedroid/stack/btu/btu_init.c
components/bt/bluedroid/stack/btu/btu_task.c
components/bt/bt.c

index 549865e7439db4e8dae576ddaf648f482e01d80d..f07235958907c0006b8117a3331334fcb9e07f3a 100644 (file)
@@ -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");
index 59b9ed4d37a157fdeab255aadd5f5ae5ef95ce6d..c1d1c0018abc84d0125a271132d970b6b992b633 100644 (file)
@@ -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)
index dcecf83c28c7868fc75f4a46eea13a5ce2350f44..51b9ea247a3c54facdf6cbde8afd8ac2661898a0 100644 (file)
@@ -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);
 }
 
 
index 021137511cb7750d1766542e662eaa98c5653b0c..30e225e951f9bd6ade8aa4d9a004fadaea388322 100644 (file)
@@ -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);
 
index 14a36eac813c75fce15583edfb85f0afa4a018d1..42730742176d44b8b0fae89c0b37471c44e463eb 100644 (file)
@@ -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__);
index c10f8c2afd91df4745c9b2ac51a5755c233f3a33..4d2b435962fb6d6e0cd3e0e0f7237a66b59778df 100644 (file)
@@ -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__ */
index 9f289280751552ce1569867d1bf9835ccd610c3c..7d00f0b76f9f1eff8c29dbf987edc479bd4de6e1 100644 (file)
@@ -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);
 }
 
 /*******************************************************************************
index 03a03f6e95c9646e664a1e187dfa5e0d7c8148ba..df387bceb3e164cfcef37667fb58e536c3c9f126 100644 (file)
@@ -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);
index 9e02627de260b8043fe2a778b264322d83666090..d41e7bfd25153bd76349475a2a30921b74116706 100644 (file)
@@ -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);
 }
 
 /*
index bf933af08ff1067519390a9d54be939a6f0c3f5f..86f757ad2e7264a41815f3e58dbbf98ee8bf3b2d 100644 (file)
@@ -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;
 }