esp_event_internal.h 6.0 KB

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