Skip to content

Commit 9fcfe90

Browse files
committed
Fix the crash due to ble deinit race
1 parent ef804e0 commit 9fcfe90

File tree

1 file changed

+9
-36
lines changed

1 file changed

+9
-36
lines changed

components/esp_matter/esp_matter_core.cpp

+9-36
Original file line numberDiff line numberDiff line change
@@ -855,39 +855,13 @@ esp_err_t chip_stack_unlock()
855855
}
856856
} /* lock */
857857

858-
static void deinit_ble_if_commissioned(void)
858+
static void deinit_ble_if_commissioned(intptr_t unused)
859859
{
860-
#if CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
861-
if(chip::Server::GetInstance().GetFabricTable().FabricCount() > 0) {
862-
esp_err_t err = ESP_OK;
863-
#if CONFIG_BT_NIMBLE_ENABLED
864-
if (!ble_hs_is_enabled()) {
865-
ESP_LOGI(TAG, "BLE already deinited");
866-
return;
867-
}
868-
869-
if (nimble_port_stop() != 0) {
870-
ESP_LOGE(TAG, "nimble_port_stop() failed");
871-
return;
872-
}
873-
nimble_port_deinit();
874-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
875-
err = esp_nimble_hci_and_controller_deinit();
876-
#endif
877-
#endif /* CONFIG_BT_NIMBLE_ENABLED */
878-
#if CONFIG_IDF_TARGET_ESP32
879-
err |= esp_bt_mem_release(ESP_BT_MODE_BTDM);
880-
#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2
881-
err |= esp_bt_mem_release(ESP_BT_MODE_BLE);
882-
#endif
883-
if (err != ESP_OK) {
884-
ESP_LOGE(TAG, "BLE deinit failed");
885-
return;
886-
}
887-
ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed");
888-
PostEvent(chip::DeviceLayer::DeviceEventType::kBLEDeinitialized);
889-
}
890-
#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
860+
#if CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
861+
if (chip::Server::GetInstance().GetFabricTable().FabricCount() > 0) {
862+
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
863+
}
864+
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
891865
}
892866

893867
static void esp_matter_chip_init_task(intptr_t context)
@@ -937,7 +911,7 @@ static void esp_matter_chip_init_task(intptr_t context)
937911
sEthernetNetworkCommissioningInstance.Init();
938912
}
939913
#endif
940-
deinit_ble_if_commissioned();
914+
PlatformMgr().ScheduleWork(deinit_ble_if_commissioned, reinterpret_cast<intptr_t>(nullptr));
941915
xTaskNotifyGive(task_to_notify);
942916
}
943917

@@ -962,11 +936,11 @@ static void device_callback_internal(const ChipDeviceEvent * event, intptr_t arg
962936

963937
case chip::DeviceLayer::DeviceEventType::kCommissioningComplete:
964938
ESP_LOGI(TAG, "Commissioning Complete");
939+
PlatformMgr().ScheduleWork(deinit_ble_if_commissioned, reinterpret_cast<intptr_t>(nullptr));
965940
break;
966941

967942
case chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionClosed:
968943
ESP_LOGI(TAG, "BLE Disconnected");
969-
deinit_ble_if_commissioned();
970944
break;
971945
default:
972946
break;
@@ -985,8 +959,7 @@ static esp_err_t chip_init(event_callback_t callback, intptr_t callback_arg)
985959
}
986960

987961
setup_providers();
988-
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
989-
// ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
962+
990963
if (PlatformMgr().StartEventLoopTask() != CHIP_NO_ERROR) {
991964
chip::Platform::MemoryShutdown();
992965
ESP_LOGE(TAG, "Failed to launch Matter main task");

0 commit comments

Comments
 (0)