esp_event_internal.h 6.0 KB

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