4040#include < esp_openthread_border_router.h>
4141#endif
4242
43- static esp_netif_t * openthread_netif = NULL ;
44- static esp_openthread_platform_config_t * s_platform_config = NULL ;
45- static TaskHandle_t cli_transmit_task = NULL ;
46- static QueueHandle_t cli_transmit_task_queue = NULL ;
43+ static esp_netif_t * openthread_netif = nullptr ;
44+ static esp_openthread_platform_config_t * s_platform_config = nullptr ;
45+ static TaskHandle_t cli_transmit_task = nullptr ;
46+ static QueueHandle_t cli_transmit_task_queue = nullptr ;
47+ static TaskHandle_t openthread_task = nullptr ;
4748static constexpr uint16_t OTCLI_TRANSMIT_TASK_STACK_SIZE = 1024 ;
4849static constexpr UBaseType_t OTCLI_TRANSMIT_TASK_PRIORITY = 5 ;
50+ static const char * TAG = " OpenThread" ;
4951
5052CHIP_ERROR cli_transmit_task_post (std::unique_ptr<char []> && cli_str)
5153{
@@ -116,6 +118,14 @@ static esp_err_t cli_command_transmit_task(void)
116118 return ESP_OK;
117119}
118120
121+ static void cli_command_transmit_task_delete (void )
122+ {
123+ if (cli_transmit_task)
124+ {
125+ vTaskDelete (cli_transmit_task);
126+ }
127+ }
128+
119129static esp_netif_t * init_openthread_netif (const esp_openthread_platform_config_t * config)
120130{
121131 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD ();
@@ -141,7 +151,6 @@ static void ot_task_worker(void * context)
141151#if defined(CONFIG_OPENTHREAD_BORDER_ROUTER) && defined(CONFIG_AUTO_UPDATE_RCP)
142152
143153static constexpr size_t kRcpVersionMaxSize = 100 ;
144- static const char * TAG = " RCP_UPDATE" ;
145154
146155static void update_rcp (void )
147156{
@@ -230,7 +239,15 @@ esp_err_t openthread_init_stack(void)
230239 };
231240
232241 ESP_ERROR_CHECK (esp_netif_init ());
233- ESP_ERROR_CHECK (esp_vfs_eventfd_register (&eventfd_config));
242+ esp_err_t err = esp_vfs_eventfd_register (&eventfd_config);
243+ if (err == ESP_ERR_INVALID_STATE)
244+ {
245+ ESP_LOGW (TAG, " eventfd is already registered" );
246+ }
247+ else if (err != ESP_OK)
248+ {
249+ return err;
250+ }
234251 assert (s_platform_config);
235252 // Initialize the OpenThread stack
236253 ESP_ERROR_CHECK (esp_openthread_init (s_platform_config));
@@ -248,6 +265,24 @@ esp_err_t openthread_init_stack(void)
248265
249266esp_err_t openthread_launch_task (void )
250267{
251- xTaskCreate (ot_task_worker, " ot_task" , CONFIG_THREAD_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle (), 5 , NULL );
268+ xTaskCreate (ot_task_worker, " ot_task" , CONFIG_THREAD_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle (), 5 , &openthread_task );
252269 return ESP_OK;
253270}
271+
272+ esp_err_t openthread_deinit_stack (void )
273+ {
274+ esp_openthread_netif_glue_deinit ();
275+ esp_netif_destroy (openthread_netif);
276+ #ifdef CONFIG_OPENTHREAD_CLI
277+ cli_command_transmit_task_delete ();
278+ #endif
279+ return esp_openthread_deinit ();
280+ }
281+
282+ void openthread_delete_task (void )
283+ {
284+ if (openthread_task)
285+ {
286+ vTaskDelete (openthread_task);
287+ }
288+ }
0 commit comments