feat(openthread): add a function to exit openthread main loop safely

This commit is contained in:
Xu Si Yu
2025-05-21 19:30:19 +08:00
committed by BOT
parent b002e50857
commit 49b5cc9e50
2 changed files with 24 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -79,6 +79,15 @@ esp_err_t esp_openthread_deinit(void);
*/ */
otInstance *esp_openthread_get_instance(void); otInstance *esp_openthread_get_instance(void);
/**
* @brief Signals the OpenThread main loop to exit.
*
* @return
* - ESP_OK on success
* - ESP_FAIL on failures
*
*/
esp_err_t esp_openthread_mainloop_exit(void);
#ifdef __cplusplus #ifdef __cplusplus
} // end of extern "C" } // end of extern "C"

View File

@ -26,6 +26,8 @@
#include "openthread/dataset_ftd.h" #include "openthread/dataset_ftd.h"
#endif #endif
static bool s_ot_mainloop_running = false;
static int hex_digit_to_int(char hex) static int hex_digit_to_int(char hex)
{ {
if ('A' <= hex && hex <= 'F') { if ('A' <= hex && hex <= 'F') {
@ -158,13 +160,24 @@ esp_err_t esp_openthread_auto_start(otOperationalDatasetTlvs *datasetTlvs)
return ESP_OK; return ESP_OK;
} }
static void mainloop_safe_exit(void *ctx)
{
s_ot_mainloop_running = false;
}
esp_err_t esp_openthread_mainloop_exit(void)
{
return esp_openthread_task_queue_post(mainloop_safe_exit, NULL);
}
esp_err_t esp_openthread_launch_mainloop(void) esp_err_t esp_openthread_launch_mainloop(void)
{ {
esp_openthread_mainloop_context_t mainloop; esp_openthread_mainloop_context_t mainloop;
otInstance *instance = esp_openthread_get_instance(); otInstance *instance = esp_openthread_get_instance();
esp_err_t error = ESP_OK; esp_err_t error = ESP_OK;
s_ot_mainloop_running = true;
while (true) { while (s_ot_mainloop_running) {
FD_ZERO(&mainloop.read_fds); FD_ZERO(&mainloop.read_fds);
FD_ZERO(&mainloop.write_fds); FD_ZERO(&mainloop.write_fds);
FD_ZERO(&mainloop.error_fds); FD_ZERO(&mainloop.error_fds);