From 2ad908f71ee8da1e9211e691e989c3adae8347ae Mon Sep 17 00:00:00 2001 From: baohongde Date: Fri, 5 Mar 2021 21:54:20 +0800 Subject: [PATCH 1/2] components/bt: Shutdown Bluetooth before esp_restart. --- components/bt/bt.c | 22 ++++++++++++++++++++++ components/esp32/system_api.c | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/components/bt/bt.c b/components/bt/bt.c index b7b589373b..8d64e06841 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -1507,6 +1507,24 @@ esp_err_t esp_bt_controller_deinit(void) return ESP_OK; } + +static void bt_shutdown(void) +{ + esp_err_t ret = ESP_OK; + ESP_LOGD(BTDM_LOG_TAG, "stop/deinit bt"); + + ret = esp_bt_controller_disable(); + if (ESP_OK != ret) { + ESP_LOGW(BTDM_LOG_TAG, "controller disable ret=%d", ret); + } + ret = esp_bt_controller_deinit(); + if (ESP_OK != ret) { + ESP_LOGW(BTDM_LOG_TAG, "controller deinit ret=%d", ret); + } + return; +} + + esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) { int ret; @@ -1566,6 +1584,10 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) } btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED; + ret = esp_register_shutdown_handler(bt_shutdown); + if (ret != ESP_OK) { + ESP_LOGW(BTDM_LOG_TAG, "Register shutdown handler failed, ret = 0x%x", ret); + } return ESP_OK; } diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index 7616b4b00c..4de32887e2 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -44,7 +44,7 @@ static const char* TAG = "system_api"; static uint8_t base_mac_addr[6] = { 0 }; -#define SHUTDOWN_HANDLERS_NO 2 +#define SHUTDOWN_HANDLERS_NO 3 static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO]; void system_init() From a10dac936c49236a22a860ac61baaef357f75d37 Mon Sep 17 00:00:00 2001 From: baohongde Date: Mon, 1 Mar 2021 14:50:49 +0800 Subject: [PATCH 2/2] components/bt: Fix crash in Bluetooth when esp_restart --- components/bt/bt.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/components/bt/bt.c b/components/bt/bt.c index 8d64e06841..6bf353e63e 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -1511,16 +1511,13 @@ esp_err_t esp_bt_controller_deinit(void) static void bt_shutdown(void) { esp_err_t ret = ESP_OK; - ESP_LOGD(BTDM_LOG_TAG, "stop/deinit bt"); + ESP_LOGD(BTDM_LOG_TAG, "stop Bluetooth"); ret = esp_bt_controller_disable(); if (ESP_OK != ret) { ESP_LOGW(BTDM_LOG_TAG, "controller disable ret=%d", ret); } ret = esp_bt_controller_deinit(); - if (ESP_OK != ret) { - ESP_LOGW(BTDM_LOG_TAG, "controller deinit ret=%d", ret); - } return; }