2022-02-16 10:15:20 -05:00
|
|
|
/*
|
2024-01-30 21:24:05 -05:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
|
2022-02-16 10:15:20 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-10-26 01:14:19 -04:00
|
|
|
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_event_internal.h"
|
2019-07-09 02:38:31 -04:00
|
|
|
#include "esp_task.h"
|
2018-10-26 01:14:19 -04:00
|
|
|
|
|
|
|
/* ------------------------- Static Variables ------------------------------- */
|
|
|
|
|
|
|
|
static esp_event_loop_handle_t s_default_loop = NULL;
|
|
|
|
|
|
|
|
/* ---------------------------- Public API ---------------------------------- */
|
|
|
|
|
|
|
|
esp_err_t esp_event_handler_register(esp_event_base_t event_base, int32_t event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
esp_event_handler_t event_handler, void* event_handler_arg)
|
2018-10-26 01:14:19 -04:00
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_event_handler_register_with(s_default_loop, event_base, event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
event_handler, event_handler_arg);
|
2018-10-26 01:14:19 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 23:37:41 -05:00
|
|
|
esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base,
|
|
|
|
int32_t event_id,
|
|
|
|
esp_event_handler_t event_handler,
|
|
|
|
void *event_handler_arg,
|
2020-11-10 02:40:01 -05:00
|
|
|
esp_event_handler_instance_t *context)
|
2020-01-16 23:37:41 -05:00
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_event_handler_instance_register_with(s_default_loop,
|
|
|
|
event_base,
|
|
|
|
event_id,
|
|
|
|
event_handler,
|
|
|
|
event_handler_arg,
|
|
|
|
context);
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:14:19 -04:00
|
|
|
esp_err_t esp_event_handler_unregister(esp_event_base_t event_base, int32_t event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
esp_event_handler_t event_handler)
|
2018-10-26 01:14:19 -04:00
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_event_handler_unregister_with(s_default_loop, event_base, event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
event_handler);
|
2018-10-26 01:14:19 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 23:37:41 -05:00
|
|
|
esp_err_t esp_event_handler_instance_unregister(esp_event_base_t event_base,
|
|
|
|
int32_t event_id,
|
|
|
|
esp_event_handler_instance_t context)
|
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2020-01-16 23:37:41 -05:00
|
|
|
return esp_event_handler_instance_unregister_with(s_default_loop, event_base, event_id, context);
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:14:19 -04:00
|
|
|
esp_err_t esp_event_post(esp_event_base_t event_base, int32_t event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
const void* event_data, size_t event_data_size, TickType_t ticks_to_wait)
|
2018-10-26 01:14:19 -04:00
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_event_post_to(s_default_loop, event_base, event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
event_data, event_data_size, ticks_to_wait);
|
2018-10-26 01:14:19 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 06:54:02 -04:00
|
|
|
#if CONFIG_ESP_EVENT_POST_FROM_ISR
|
2019-02-15 08:28:24 -05:00
|
|
|
esp_err_t esp_event_isr_post(esp_event_base_t event_base, int32_t event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
const void* event_data, size_t event_data_size, BaseType_t* task_unblocked)
|
2019-02-15 08:28:24 -05:00
|
|
|
{
|
|
|
|
if (s_default_loop == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return esp_event_isr_post_to(s_default_loop, event_base, event_id,
|
2024-01-30 21:24:05 -05:00
|
|
|
event_data, event_data_size, task_unblocked);
|
2019-02-15 08:28:24 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t esp_event_loop_create_default(void)
|
2018-10-26 01:14:19 -04:00
|
|
|
{
|
|
|
|
if (s_default_loop) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_event_loop_args_t loop_args = {
|
2019-04-30 06:51:55 -04:00
|
|
|
.queue_size = CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE,
|
2018-10-26 01:14:19 -04:00
|
|
|
.task_name = "sys_evt",
|
|
|
|
.task_stack_size = ESP_TASKD_EVENT_STACK,
|
|
|
|
.task_priority = ESP_TASKD_EVENT_PRIO,
|
|
|
|
.task_core_id = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
esp_err_t err;
|
|
|
|
|
|
|
|
err = esp_event_loop_create(&loop_args, &s_default_loop);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t esp_event_loop_delete_default(void)
|
2018-10-26 01:14:19 -04:00
|
|
|
{
|
|
|
|
if (!s_default_loop) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err;
|
2019-04-10 04:24:50 -04:00
|
|
|
|
2018-10-26 01:14:19 -04:00
|
|
|
err = esp_event_loop_delete(s_default_loop);
|
|
|
|
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_default_loop = NULL;
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|