1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ESP_EVENT_INTERNAL_H_ 16 #define ESP_EVENT_INTERNAL_H_ 17 18 #include "sys/queue.h" 19 #include <stdbool.h> 20 #include "esp_event.h" 21 #include "stdatomic.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 typedef SLIST_HEAD(base_nodes, base_node) base_nodes_t; 28 29 typedef struct esp_event_handler_context { 30 esp_event_handler_t handler; /**< event handler function*/ 31 void* arg; 32 } esp_event_handler_instance_context_t; /**< event handler argument */ 33 34 /// Event handler 35 typedef struct esp_event_handler_node { 36 esp_event_handler_instance_context_t* handler_ctx; /**< event handler context*/ 37 #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 38 uint32_t invoked; /**< number of times this handler has been invoked */ 39 int64_t time; /**< total runtime of this handler across all calls */ 40 #endif 41 SLIST_ENTRY(esp_event_handler_node) next; /**< next event handler in the list */ 42 } esp_event_handler_node_t; 43 44 typedef SLIST_HEAD(esp_event_handler_instances, esp_event_handler_node) esp_event_handler_nodes_t; 45 46 /// Event 47 typedef struct esp_event_id_node { 48 int32_t id; /**< id number of the event */ 49 esp_event_handler_nodes_t handlers; /**< list of handlers to be executed when 50 this event is raised */ 51 SLIST_ENTRY(esp_event_id_node) next; /**< pointer to the next event node on the linked list */ 52 } esp_event_id_node_t; 53 54 typedef SLIST_HEAD(esp_event_id_nodes, esp_event_id_node) esp_event_id_nodes_t; 55 56 typedef struct esp_event_base_node { 57 esp_event_base_t base; /**< base identifier of the event */ 58 esp_event_handler_nodes_t handlers; /**< event base level handlers, handlers for 59 all events with this base */ 60 esp_event_id_nodes_t id_nodes; /**< list of event ids with this base */ 61 SLIST_ENTRY(esp_event_base_node) next; /**< pointer to the next base node on the linked list */ 62 } esp_event_base_node_t; 63 64 typedef SLIST_HEAD(esp_event_base_nodes, esp_event_base_node) esp_event_base_nodes_t; 65 66 typedef struct esp_event_loop_node { 67 esp_event_handler_nodes_t handlers; /** event loop level handlers */ 68 esp_event_base_nodes_t base_nodes; /** list of event bases registered to the loop */ 69 SLIST_ENTRY(esp_event_loop_node) next; /** pointer to the next loop node containing 70 event loop level handlers and the rest of 71 event bases registered to the loop */ 72 } esp_event_loop_node_t; 73 74 typedef SLIST_HEAD(esp_event_loop_nodes, esp_event_loop_node) esp_event_loop_nodes_t; 75 76 /// Event loop 77 typedef struct esp_event_loop_instance { 78 const char* name; /**< name of this event loop */ 79 QueueHandle_t queue; /**< event queue */ 80 TaskHandle_t task; /**< task that consumes the event queue */ 81 TaskHandle_t running_task; /**< for loops with no dedicated task, the 82 task that consumes the queue */ 83 SemaphoreHandle_t mutex; /**< mutex for updating the events linked list */ 84 esp_event_loop_nodes_t loop_nodes; /**< set of linked lists containing the 85 registered handlers for the loop */ 86 #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 87 atomic_uint_least32_t events_recieved; /**< number of events successfully posted to the loop */ 88 atomic_uint_least32_t events_dropped; /**< number of events dropped due to queue being full */ 89 SemaphoreHandle_t profiling_mutex; /**< mutex used for profiliing */ 90 SLIST_ENTRY(esp_event_loop_instance) next; /**< next event loop in the list */ 91 #endif 92 } esp_event_loop_instance_t; 93 94 #if CONFIG_ESP_EVENT_POST_FROM_ISR 95 typedef union esp_event_post_data { 96 uint32_t val; 97 void *ptr; 98 } esp_event_post_data_t; 99 #else 100 typedef void* esp_event_post_data_t; 101 #endif 102 103 /// Event posted to the event queue 104 typedef struct esp_event_post_instance { 105 #if CONFIG_ESP_EVENT_POST_FROM_ISR 106 bool data_allocated; /**< indicates whether data is allocated from heap */ 107 bool data_set; /**< indicates if data is null */ 108 #endif 109 esp_event_base_t base; /**< the event base */ 110 int32_t id; /**< the event id */ 111 esp_event_post_data_t data; /**< data associated with the event */ 112 } esp_event_post_instance_t; 113 114 #ifdef __cplusplus 115 } // extern "C" 116 #endif 117 118 #endif // #ifndef ESP_EVENT_INTERNAL_H_ 119