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 "esp_event.h" 19 #include "stdatomic.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 typedef SLIST_HEAD(base_nodes, base_node) base_nodes_t; 26 27 typedef struct esp_event_handler_context { 28 esp_event_handler_t handler; /**< event handler function*/ 29 void* arg; 30 } esp_event_handler_instance_context_t; /**< event handler argument */ 31 32 /// Event handler 33 typedef struct esp_event_handler_node { 34 esp_event_handler_instance_context_t* handler_ctx; /**< event handler context*/ 35 #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 36 uint32_t invoked; /**< number of times this handler has been invoked */ 37 int64_t time; /**< total runtime of this handler across all calls */ 38 #endif 39 SLIST_ENTRY(esp_event_handler_node) next; /**< next event handler in the list */ 40 } esp_event_handler_node_t; 41 42 typedef SLIST_HEAD(esp_event_handler_instances, esp_event_handler_node) esp_event_handler_nodes_t; 43 44 /// Event 45 typedef struct esp_event_id_node { 46 int32_t id; /**< id number of the event */ 47 esp_event_handler_nodes_t handlers; /**< list of handlers to be executed when 48 this event is raised */ 49 SLIST_ENTRY(esp_event_id_node) next; /**< pointer to the next event node on the linked list */ 50 } esp_event_id_node_t; 51 52 typedef SLIST_HEAD(esp_event_id_nodes, esp_event_id_node) esp_event_id_nodes_t; 53 54 typedef struct esp_event_base_node { 55 esp_event_base_t base; /**< base identifier of the event */ 56 esp_event_handler_nodes_t handlers; /**< event base level handlers, handlers for 57 all events with this base */ 58 esp_event_id_nodes_t id_nodes; /**< list of event ids with this base */ 59 SLIST_ENTRY(esp_event_base_node) next; /**< pointer to the next base node on the linked list */ 60 } esp_event_base_node_t; 61 62 typedef SLIST_HEAD(esp_event_base_nodes, esp_event_base_node) esp_event_base_nodes_t; 63 64 typedef struct esp_event_loop_node { 65 esp_event_handler_nodes_t handlers; /** event loop level handlers */ 66 esp_event_base_nodes_t base_nodes; /** list of event bases registered to the loop */ 67 SLIST_ENTRY(esp_event_loop_node) next; /** pointer to the next loop node containing 68 event loop level handlers and the rest of 69 event bases registered to the loop */ 70 } esp_event_loop_node_t; 71 72 typedef SLIST_HEAD(esp_event_loop_nodes, esp_event_loop_node) esp_event_loop_nodes_t; 73 74 /// Event loop 75 typedef struct esp_event_loop_instance { 76 const char* name; /**< name of this event loop */ 77 QueueHandle_t queue; /**< event queue */ 78 TaskHandle_t task; /**< task that consumes the event queue */ 79 TaskHandle_t running_task; /**< for loops with no dedicated task, the 80 task that consumes the queue */ 81 SemaphoreHandle_t mutex; /**< mutex for updating the events linked list */ 82 esp_event_loop_nodes_t loop_nodes; /**< set of linked lists containing the 83 registered handlers for the loop */ 84 #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING 85 atomic_uint_least32_t events_recieved; /**< number of events successfully posted to the loop */ 86 atomic_uint_least32_t events_dropped; /**< number of events dropped due to queue being full */ 87 SemaphoreHandle_t profiling_mutex; /**< mutex used for profiliing */ 88 SLIST_ENTRY(esp_event_loop_instance) next; /**< next event loop in the list */ 89 #endif 90 } esp_event_loop_instance_t; 91 92 #if CONFIG_ESP_EVENT_POST_FROM_ISR 93 typedef union esp_event_post_data { 94 uint32_t val; 95 void *ptr; 96 } esp_event_post_data_t; 97 #else 98 typedef void* esp_event_post_data_t; 99 #endif 100 101 /// Event posted to the event queue 102 typedef struct esp_event_post_instance { 103 #if CONFIG_ESP_EVENT_POST_FROM_ISR 104 bool data_allocated; /**< indicates whether data is allocated from heap */ 105 bool data_set; /**< indicates if data is null */ 106 #endif 107 esp_event_base_t base; /**< the event base */ 108 int32_t id; /**< the event id */ 109 esp_event_post_data_t data; /**< data associated with the event */ 110 } esp_event_post_instance_t; 111 112 #ifdef __cplusplus 113 } // extern "C" 114 #endif 115 116 #endif // #ifndef ESP_EVENT_INTERNAL_H_ 117