From c6b756b2d72390d6ed437bbbb8833e25d2cf882c Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Mon, 19 Jul 2021 15:54:05 +0530 Subject: [PATCH] Blufi [Nimble] Add fix to crash while running blufi on ESP32C3 A race condition is occuring while creating task on ESP32C3. Task is getting created, but the function returns with a delay. Since task was created, events start getting posted, but existing application initializes certain threads / callbacks after stack initialization. The same application works in different ways for bluedroid and nimble. Hence modified the order during initialization accordingly. --- examples/bluetooth/blufi/main/blufi_example.h | 1 + .../bluetooth/blufi/main/blufi_example_main.c | 16 +---- examples/bluetooth/blufi/main/blufi_init.c | 67 +++++++++++++++++-- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/examples/bluetooth/blufi/main/blufi_example.h b/examples/bluetooth/blufi/main/blufi_example.h index d8b92f5d9e..a80e273e67 100644 --- a/examples/bluetooth/blufi/main/blufi_example.h +++ b/examples/bluetooth/blufi/main/blufi_example.h @@ -13,3 +13,4 @@ int blufi_security_init(void); void blufi_security_deinit(void); int esp_blufi_gap_register_callback(void); esp_err_t esp_blufi_host_init(void); +esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *callbacks); diff --git a/examples/bluetooth/blufi/main/blufi_example_main.c b/examples/bluetooth/blufi/main/blufi_example_main.c index 67ff42cb29..4256d00de6 100644 --- a/examples/bluetooth/blufi/main/blufi_example_main.c +++ b/examples/bluetooth/blufi/main/blufi_example_main.c @@ -385,23 +385,11 @@ void app_main(void) return; } - ret = esp_blufi_host_init(); + ret = esp_blufi_host_and_cb_init(&example_callbacks); if (ret) { - BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); + BLUFI_ERROR("%s initialise failed: %s\n", __func__, esp_err_to_name(ret)); return; } BLUFI_INFO("BLUFI VERSION %04x\n", esp_blufi_get_version()); - - ret = esp_blufi_register_callbacks(&example_callbacks); - if(ret){ - BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); - return; - } - - ret = esp_blufi_gap_register_callback(); - if(ret){ - BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); - return; - } } diff --git a/examples/bluetooth/blufi/main/blufi_init.c b/examples/bluetooth/blufi/main/blufi_init.c index 052739b055..04dc13f4eb 100644 --- a/examples/bluetooth/blufi/main/blufi_init.c +++ b/examples/bluetooth/blufi/main/blufi_init.c @@ -3,10 +3,10 @@ #include "esp_blufi_api.h" #include "esp_log.h" #include "esp_blufi.h" +#include "blufi_example.h" #ifdef CONFIG_BT_BLUEDROID_ENABLED #include "esp_bt.h" #include "esp_bt_main.h" -#include "blufi_example.h" #include "esp_bt_device.h" #endif @@ -42,15 +42,40 @@ esp_err_t esp_blufi_host_init(void) } -int esp_blufi_gap_register_callback(void) +esp_err_t esp_blufi_gap_register_callback(void) { int rc; rc = esp_ble_gap_register_callback(esp_blufi_gap_event_handler); if(rc){ return rc; } - esp_blufi_profile_init(); - return 0; + return esp_blufi_profile_init(); +} + +esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks) +{ + esp_err_t ret = ESP_OK; + + ret = esp_blufi_host_init(); + if (ret) { + BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); + return ret; + } + + ret = esp_blufi_register_callbacks(example_callbacks); + if(ret){ + BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); + return ret; + } + + ret = esp_blufi_gap_register_callback(); + if(ret){ + BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); + return ret; + } + + return ESP_OK; + } #endif /* CONFIG_BT_BLUEDROID_ENABLED */ @@ -115,14 +140,42 @@ esp_err_t esp_blufi_host_init(void) /* XXX Need to have template for store */ ble_store_config_init(); - nimble_port_freertos_init(bleprph_host_task); esp_blufi_btc_init(); + + nimble_port_freertos_init(bleprph_host_task); + return ESP_OK; } -int esp_blufi_gap_register_callback(void) +esp_err_t esp_blufi_gap_register_callback(void) { - return 0; + return ESP_OK; } +esp_err_t esp_blufi_host_and_cb_init(esp_blufi_callbacks_t *example_callbacks) +{ + esp_err_t ret = ESP_OK; + + ret = esp_blufi_register_callbacks(example_callbacks); + if(ret){ + BLUFI_ERROR("%s blufi register failed, error code = %x\n", __func__, ret); + return ret; + } + + ret = esp_blufi_gap_register_callback(); + if(ret){ + BLUFI_ERROR("%s gap register failed, error code = %x\n", __func__, ret); + return ret; + } + + ret = esp_blufi_host_init(); + if (ret) { + BLUFI_ERROR("%s initialise host failed: %s\n", __func__, esp_err_to_name(ret)); + return ret; + } + + return ret; +} + + #endif /* CONFIG_BT_NIMBLE_ENABLED */