Skip to content

Commit

Permalink
Merge branch 'fix/make_ot_task_queue_non_permanent_blocking' into 'ma…
Browse files Browse the repository at this point in the history
…ster'

fix(openthread): make ot task queue sending non-permanent blocking

See merge request espressif/esp-idf!31979
  • Loading branch information
chshu committed Jul 22, 2024
2 parents 4703390 + 943033f commit 9f1f285
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
7 changes: 5 additions & 2 deletions components/openthread/src/esp_openthread_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ static void ot_cli_loop(void *context)
printf("Internal error: %s\n", esp_err_to_name(err));
}
} else {
esp_openthread_cli_input(line);
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
if (esp_openthread_cli_input(line) == ESP_OK) {
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
} else {
printf("Openthread task is busy, failed to run command: %s\n", line);
}
}
linenoiseHistoryAdd(line);
}
Expand Down
6 changes: 6 additions & 0 deletions components/openthread/src/esp_openthread_lwip_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

#define OPENTHREAD_IP6_MTU 1280

#if CONFIG_OPENTHREAD_BORDER_ROUTER
#if CONFIG_LWIP_IPV6_NUM_ADDRESSES != 12
#error CONFIG_LWIP_IPV6_NUM_ADDRESSES should be set to 12, please configure it using `idf.py menuconfig`
#endif
#endif

static err_t openthread_netif_init(struct netif *netif);
static void openthread_netif_input(void *h, void *buffer, size_t len, void *eb);

Expand Down
4 changes: 3 additions & 1 deletion components/openthread/src/esp_openthread_task_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static QueueHandle_t s_task_queue = NULL;
static int s_task_queue_event_fd = -1;
static const char *task_queue_workflow = "task_queue";

#define OT_TASK_QUEUE_SENDING_WAIT_TIME pdMS_TO_TICKS(100)

typedef struct {
esp_openthread_task_t task;
void *arg;
Expand Down Expand Up @@ -60,7 +62,7 @@ esp_err_t IRAM_ATTR esp_openthread_task_queue_post(esp_openthread_task_t task, v
ESP_RETURN_ON_FALSE_ISR(xQueueSendFromISR(s_task_queue, &task_storage, &task_woken), ESP_FAIL, OT_PLAT_LOG_TAG,
"Failed to post task to OpenThread task queue");
} else {
ESP_RETURN_ON_FALSE(xQueueSend(s_task_queue, &task_storage, portMAX_DELAY), ESP_FAIL, OT_PLAT_LOG_TAG,
ESP_RETURN_ON_FALSE(xQueueSend(s_task_queue, &task_storage, OT_TASK_QUEUE_SENDING_WAIT_TIME), ESP_FAIL, OT_PLAT_LOG_TAG,
"Failed to post task to OpenThread task queue");
}
ret = write(s_task_queue_event_fd, &val, sizeof(val));
Expand Down
84 changes: 44 additions & 40 deletions examples/openthread/ot_br/main/esp_ot_br.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,15 @@ static void ot_task_worker(void *aContext)

// Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config));

// Initialize border routing features
esp_openthread_lock_acquire(portMAX_DELAY);
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
#endif
ESP_ERROR_CHECK(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&config)));

esp_openthread_lock_acquire(portMAX_DELAY);
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
esp_openthread_cli_init();

#if CONFIG_OPENTHREAD_BR_AUTO_START
ESP_ERROR_CHECK(esp_openthread_border_router_init());
otOperationalDatasetTlvs dataset;
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
#endif // CONFIG_OPENTHREAD_BR_AUTO_START

esp_cli_custom_command_init();
esp_openthread_cli_create_task();
esp_openthread_lock_release();

// Run the main loop
esp_openthread_cli_create_task();
esp_openthread_launch_mainloop();

// Clean up
Expand All @@ -105,36 +91,15 @@ static void ot_task_worker(void *aContext)
vTaskDelete(NULL);
}

void app_main(void)
void ot_br_init(void *ctx)
{
// Used eventfds:
// * netif
// * task queue
// * border router
esp_vfs_eventfd_config_t eventfd_config = {
#if CONFIG_OPENTHREAD_RADIO_NATIVE || CONFIG_OPENTHREAD_RADIO_SPINEL_SPI
// * radio driver (A native radio device needs a eventfd for radio driver.)
// * SpiSpinelInterface (The Spi Spinel Interface needs a eventfd.)
// The above will not exist at the same time.
.max_fds = 4,
#else
.max_fds = 3,
#endif
};
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));

ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

#if CONFIG_EXAMPLE_CONNECT_WIFI
#if CONFIG_OPENTHREAD_BR_AUTO_START
ESP_ERROR_CHECK(example_connect());
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MAX_MODEM));
#if CONFIG_ESP_COEX_SW_COEXIST_ENABLE && CONFIG_OPENTHREAD_RADIO_NATIVE
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
ESP_ERROR_CHECK(esp_coex_wifi_i154_enable());
#else
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));

#if CONFIG_EXTERNAL_COEX_ENABLE
ot_br_external_coexist_init();
Expand All @@ -155,5 +120,44 @@ void app_main(void)

ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(mdns_hostname_set("esp-ot-br"));
xTaskCreate(ot_task_worker, "ot_br_main", 20480, xTaskGetCurrentTaskHandle(), 5, NULL);

// Initialize border routing features
esp_openthread_lock_acquire(portMAX_DELAY);
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
#endif

#if CONFIG_OPENTHREAD_BR_AUTO_START
ESP_ERROR_CHECK(esp_openthread_border_router_init());
otOperationalDatasetTlvs dataset;
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
#endif // CONFIG_OPENTHREAD_BR_AUTO_START

esp_openthread_lock_release();
vTaskDelete(NULL);
}

void app_main(void)
{
// Used eventfds:
// * netif
// * task queue
// * border router
esp_vfs_eventfd_config_t eventfd_config = {
#if CONFIG_OPENTHREAD_RADIO_NATIVE || CONFIG_OPENTHREAD_RADIO_SPINEL_SPI
// * radio driver (A native radio device needs a eventfd for radio driver.)
// * SpiSpinelInterface (The Spi Spinel Interface needs a eventfd.)
// The above will not exist at the same time.
.max_fds = 4,
#else
.max_fds = 3,
#endif
};
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
xTaskCreate(ot_task_worker, "ot_br_main", 8192, xTaskGetCurrentTaskHandle(), 5, NULL);
xTaskCreate(ot_br_init, "ot_br_init", 6144, NULL, 4, NULL);
}
2 changes: 1 addition & 1 deletion examples/openthread/ot_br/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CONFIG_OPENTHREAD_RADIO_SPINEL_UART=y
# lwIP
#
CONFIG_LWIP_IPV6_FORWARD=y
CONFIG_LWIP_IPV6_NUM_ADDRESSES=8
CONFIG_LWIP_IPV6_NUM_ADDRESSES=12
CONFIG_LWIP_MULTICAST_PING=y
CONFIG_LWIP_NETIF_STATUS_CALLBACK=y
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
Expand Down

0 comments on commit 9f1f285

Please sign in to comment.