ESP32: Add functions to delete OpenThread task and deinit OpenThread stack (#36168)

diff --git a/src/platform/ESP32/OpenthreadLauncher.cpp b/src/platform/ESP32/OpenthreadLauncher.cpp
index 63f1bac..0ab3dad 100644
--- a/src/platform/ESP32/OpenthreadLauncher.cpp
+++ b/src/platform/ESP32/OpenthreadLauncher.cpp
@@ -40,12 +40,14 @@
 #include <esp_openthread_border_router.h>
 #endif
 
-static esp_netif_t * openthread_netif                       = NULL;
-static esp_openthread_platform_config_t * s_platform_config = NULL;
-static TaskHandle_t cli_transmit_task                       = NULL;
-static QueueHandle_t cli_transmit_task_queue                = NULL;
+static esp_netif_t * openthread_netif                       = nullptr;
+static esp_openthread_platform_config_t * s_platform_config = nullptr;
+static TaskHandle_t cli_transmit_task                       = nullptr;
+static QueueHandle_t cli_transmit_task_queue                = nullptr;
+static TaskHandle_t openthread_task                         = nullptr;
 static constexpr uint16_t OTCLI_TRANSMIT_TASK_STACK_SIZE    = 1024;
 static constexpr UBaseType_t OTCLI_TRANSMIT_TASK_PRIORITY   = 5;
+static const char * TAG                                     = "OpenThread";
 
 CHIP_ERROR cli_transmit_task_post(std::unique_ptr<char[]> && cli_str)
 {
@@ -116,6 +118,14 @@
     return ESP_OK;
 }
 
+static void cli_command_transmit_task_delete(void)
+{
+    if (cli_transmit_task)
+    {
+        vTaskDelete(cli_transmit_task);
+    }
+}
+
 static esp_netif_t * init_openthread_netif(const esp_openthread_platform_config_t * config)
 {
     esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
@@ -141,7 +151,6 @@
 #if defined(CONFIG_OPENTHREAD_BORDER_ROUTER) && defined(CONFIG_AUTO_UPDATE_RCP)
 
 static constexpr size_t kRcpVersionMaxSize = 100;
-static const char * TAG                    = "RCP_UPDATE";
 
 static void update_rcp(void)
 {
@@ -230,7 +239,15 @@
     };
 
     ESP_ERROR_CHECK(esp_netif_init());
-    ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
+    esp_err_t err = esp_vfs_eventfd_register(&eventfd_config);
+    if (err == ESP_ERR_INVALID_STATE)
+    {
+        ESP_LOGW(TAG, "eventfd is already registered");
+    }
+    else if (err != ESP_OK)
+    {
+        return err;
+    }
     assert(s_platform_config);
     // Initialize the OpenThread stack
     ESP_ERROR_CHECK(esp_openthread_init(s_platform_config));
@@ -248,6 +265,24 @@
 
 esp_err_t openthread_launch_task(void)
 {
-    xTaskCreate(ot_task_worker, "ot_task", CONFIG_THREAD_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle(), 5, NULL);
+    xTaskCreate(ot_task_worker, "ot_task", CONFIG_THREAD_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle(), 5, &openthread_task);
     return ESP_OK;
 }
+
+esp_err_t openthread_deinit_stack(void)
+{
+    esp_openthread_netif_glue_deinit();
+    esp_netif_destroy(openthread_netif);
+#ifdef CONFIG_OPENTHREAD_CLI
+    cli_command_transmit_task_delete();
+#endif
+    return esp_openthread_deinit();
+}
+
+void openthread_delete_task(void)
+{
+    if (openthread_task)
+    {
+        vTaskDelete(openthread_task);
+    }
+}
diff --git a/src/platform/ESP32/OpenthreadLauncher.h b/src/platform/ESP32/OpenthreadLauncher.h
index a15125f..daeebd1 100644
--- a/src/platform/ESP32/OpenthreadLauncher.h
+++ b/src/platform/ESP32/OpenthreadLauncher.h
@@ -29,4 +29,6 @@
 esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config);
 esp_err_t openthread_init_stack(void);
 esp_err_t openthread_launch_task(void);
+esp_err_t openthread_deinit_stack(void);
+void openthread_delete_task(void);
 CHIP_ERROR cli_transmit_task_post(std::unique_ptr<char[]> && cli_str);