esp_event_internal.h 5.9 KB

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