mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 04:02:27 +00:00
Event handling refactoring
This change separates definitions in esp_event.h and functions in event.c into several parts: - event structure definitions (esp_event.h) - default implementations of event handlers (event_default_handlers.c) - default implementation of event loop (event_loop.c, esp_event_loop.h) Purpose of this change is to allow applications choose their own poison: - full control of event loop at the expense of more bootstrap code - pre-defined event task firing event callbacks, but less code in app_main.c
This commit is contained in:
118
components/esp32/event_loop.c
Normal file
118
components/esp32/event_loop.c
Normal file
@@ -0,0 +1,118 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "esp_task.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#include "tcpip_adapter.h"
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
|
||||
static const char* TAG = "event";
|
||||
static bool s_event_init_flag = false;
|
||||
static QueueHandle_t s_event_queue = NULL;
|
||||
static system_event_cb_t s_event_handler_cb = NULL;
|
||||
static void *s_event_ctx = NULL;
|
||||
|
||||
static esp_err_t esp_wifi_post_event_to_user(system_event_t *event)
|
||||
{
|
||||
if (s_event_handler_cb) {
|
||||
return (*s_event_handler_cb)(s_event_ctx, event);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void esp_system_event_task(void *pvParameters)
|
||||
{
|
||||
system_event_t evt;
|
||||
esp_err_t ret;
|
||||
|
||||
while (1) {
|
||||
if (xQueueReceive(s_event_queue, &evt, portMAX_DELAY) == pdPASS) {
|
||||
ret = esp_event_process_default(&evt);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "default event handler failed!");
|
||||
}
|
||||
ret = esp_wifi_post_event_to_user(&evt);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "post event to user fail!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx)
|
||||
{
|
||||
system_event_cb_t old_cb = s_event_handler_cb;
|
||||
|
||||
s_event_handler_cb = cb;
|
||||
s_event_ctx = ctx;
|
||||
|
||||
return old_cb;
|
||||
}
|
||||
|
||||
esp_err_t esp_event_send(system_event_t *event)
|
||||
{
|
||||
portBASE_TYPE ret;
|
||||
|
||||
ret = xQueueSendToBack(s_event_queue, event, 0);
|
||||
|
||||
if (pdPASS != ret) {
|
||||
if (event) {
|
||||
ESP_LOGE(TAG, "e=%d f", event->event_id);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "e null");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
QueueHandle_t esp_event_loop_get_queue(void)
|
||||
{
|
||||
return s_event_queue;
|
||||
}
|
||||
|
||||
esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx)
|
||||
{
|
||||
if (s_event_init_flag) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
s_event_handler_cb = cb;
|
||||
s_event_ctx = ctx;
|
||||
|
||||
s_event_queue = xQueueCreate(CONFIG_SYSTEM_EVENT_QUEUE_SIZE, sizeof(system_event_t));
|
||||
|
||||
xTaskCreatePinnedToCore(esp_system_event_task, "eventTask",
|
||||
ESP_TASKD_EVENT_STACK, NULL, ESP_TASKD_EVENT_PRIO, NULL, 0);
|
||||
|
||||
s_event_init_flag = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
Reference in New Issue
Block a user