esp_event.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifndef ESP_EVENT_H_
  15. #define ESP_EVENT_H_
  16. #include "esp_err.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/queue.h"
  20. #include "freertos/semphr.h"
  21. #include "esp_event_base.h"
  22. #include "esp_event_legacy.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /// Configuration for creating event loops
  27. typedef struct {
  28. int32_t queue_size; /**< size of the event loop queue */
  29. const char* task_name; /**< name of the event loop task; if NULL,
  30. a dedicated task is not created for event loop*/
  31. UBaseType_t task_priority; /**< priority of the event loop task, ignored if task name is NULL */
  32. uint32_t task_stack_size; /**< stack size of the event loop task, ignored if task name is NULL */
  33. BaseType_t task_core_id; /**< core to which the event loop task is pinned to,
  34. ignored if task name is NULL */
  35. } esp_event_loop_args_t;
  36. /**
  37. * @brief Create a new event loop.
  38. *
  39. * @param[in] event_loop_args configuration structure for the event loop to create
  40. * @param[out] event_loop handle to the created event loop
  41. *
  42. * @return
  43. * - ESP_OK: Success
  44. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  45. * - ESP_FAIL: Failed to create task loop
  46. * - Others: Fail
  47. */
  48. esp_err_t esp_event_loop_create(const esp_event_loop_args_t* event_loop_args, esp_event_loop_handle_t* event_loop);
  49. /**
  50. * @brief Delete an existing event loop.
  51. *
  52. * @param[in] event_loop event loop to delete
  53. *
  54. * @return
  55. * - ESP_OK: Success
  56. * - Others: Fail
  57. */
  58. esp_err_t esp_event_loop_delete(esp_event_loop_handle_t event_loop);
  59. /**
  60. * @brief Create default event loop
  61. *
  62. * @return
  63. * - ESP_OK: Success
  64. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  65. * - ESP_FAIL: Failed to create task loop
  66. * - Others: Fail
  67. */
  68. esp_err_t esp_event_loop_create_default();
  69. /**
  70. * @brief Delete the default event loop
  71. *
  72. * @return
  73. * - ESP_OK: Success
  74. * - Others: Fail
  75. */
  76. esp_err_t esp_event_loop_delete_default();
  77. /**
  78. * @brief Dispatch events posted to an event loop.
  79. *
  80. * This function is used to dispatch events posted to a loop with no dedicated task, i.e task name was set to NULL
  81. * in event_loop_args argument during loop creation. This function includes an argument to limit the amount of time
  82. * it runs, returning control to the caller when that time expires (or some time afterwards). There is no guarantee
  83. * that a call to this function will exit at exactly the time of expiry. There is also no guarantee that events have
  84. * been dispatched during the call, as the function might have spent all of the alloted time waiting on the event queue.
  85. * Once an event has been unqueued, however, it is guaranteed to be dispatched. This guarantee contributes to not being
  86. * able to exit exactly at time of expiry as (1) blocking on internal mutexes is necessary for dispatching the unqueued
  87. * event, and (2) during dispatch of the unqueued event there is no way to control the time occupied by handler code
  88. * execution. The guaranteed time of exit is therefore the alloted time + amount of time required to dispatch
  89. * the last unqueued event.
  90. *
  91. * In cases where waiting on the queue times out, ESP_OK is returned and not ESP_ERR_TIMEOUT, since it is
  92. * normal behavior.
  93. *
  94. * @param[in] event_loop event loop to dispatch posted events from
  95. * @param[in] ticks_to_run number of ticks to run the loop
  96. *
  97. * @note encountering an unknown event that has been posted to the loop will only generate a warning, not an error.
  98. *
  99. * @return
  100. * - ESP_OK: Success
  101. * - Others: Fail
  102. */
  103. esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t ticks_to_run);
  104. /**
  105. * @brief Register an event handler to the system event loop.
  106. *
  107. * This function can be used to register a handler for either: (1) specific events,
  108. * (2) all events of a certain event base, or (3) all events known by the system event loop.
  109. *
  110. * - specific events: specify exact event_base and event_id
  111. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  112. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  113. *
  114. * Registering multiple handlers to events is possible. Registering a single handler to multiple events is
  115. * also possible. However, registering the same handler to the same event multiple times would cause the
  116. * previous registrations to be overwritten.
  117. *
  118. * @param[in] event_base the base id of the event to register the handler for
  119. * @param[in] event_id the id of the event to register the handler for
  120. * @param[in] event_handler the handler function which gets called when the event is dispatched
  121. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  122. *
  123. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  124. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  125. *
  126. * @return
  127. * - ESP_OK: Success
  128. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  129. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event id
  130. * - Others: Fail
  131. */
  132. esp_err_t esp_event_handler_register(esp_event_base_t event_base,
  133. int32_t event_id,
  134. esp_event_handler_t event_handler,
  135. void* event_handler_arg);
  136. /**
  137. * @brief Register an event handler to a specific loop.
  138. *
  139. * This function behaves in the same manner as esp_event_handler_register, except the additional
  140. * specification of the event loop to register the handler to.
  141. *
  142. * @param[in] event_loop the event loop to register this handler function to
  143. * @param[in] event_base the base id of the event to register the handler for
  144. * @param[in] event_id the id of the event to register the handler for
  145. * @param[in] event_handler the handler function which gets called when the event is dispatched
  146. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  147. *
  148. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  149. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  150. *
  151. * @return
  152. * - ESP_OK: Success
  153. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  154. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event id
  155. * - Others: Fail
  156. */
  157. esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop,
  158. esp_event_base_t event_base,
  159. int32_t event_id,
  160. esp_event_handler_t event_handler,
  161. void* event_handler_arg);
  162. /**
  163. * @brief Unregister a handler with the system event loop.
  164. *
  165. * This function can be used to unregister a handler so that it no longer gets called during dispatch.
  166. * Handlers can be unregistered for either: (1) specific events, (2) all events of a certain event base,
  167. * or (3) all events known by the system event loop
  168. *
  169. * - specific events: specify exact event_base and event_id
  170. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  171. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  172. *
  173. * This function ignores unregistration of handlers that has not been previously registered.
  174. *
  175. * @param[in] event_base the base of the event with which to unregister the handler
  176. * @param[in] event_id the id of the event with which to unregister the handler
  177. * @param[in] event_handler the handler to unregister
  178. *
  179. * @return ESP_OK success
  180. * @return ESP_ERR_INVALID_ARG invalid combination of event base and event id
  181. * @return others fail
  182. */
  183. esp_err_t esp_event_handler_unregister(esp_event_base_t event_base, int32_t event_id, esp_event_handler_t event_handler);
  184. /**
  185. * @brief Unregister a handler with the system event loop.
  186. *
  187. * This function behaves in the same manner as esp_event_handler_unregister, except the additional specification of
  188. * the event loop to unregister the handler with.
  189. *
  190. * @param[in] event_loop the event loop with which to unregister this handler function
  191. * @param[in] event_base the base of the event with which to unregister the handler
  192. * @param[in] event_id the id of the event with which to unregister the handler
  193. * @param[in] event_handler the handler to unregister
  194. *
  195. * @return
  196. * - ESP_OK: Success
  197. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event id
  198. * - Others: Fail
  199. */
  200. esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop,
  201. esp_event_base_t event_base,
  202. int32_t event_id,
  203. esp_event_handler_t event_handler);
  204. /**
  205. * @brief Posts an event to the system default event loop. The event loop library keeps a copy of event_data and manages
  206. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  207. * handler recieves is always valid.
  208. *
  209. * @param[in] event_base the event base that identifies the event
  210. * @param[in] event_id the the event id that identifies the event
  211. * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler
  212. * @param[in] event_data_size the size of the event data
  213. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  214. *
  215. * @note posting events from an ISR is not supported
  216. *
  217. * @return
  218. * - ESP_OK: Success
  219. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired
  220. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event id
  221. * - Others: Fail
  222. */
  223. esp_err_t esp_event_post(esp_event_base_t event_base,
  224. int32_t event_id,
  225. void* event_data,
  226. size_t event_data_size,
  227. TickType_t ticks_to_wait);
  228. /**
  229. * @brief Posts an event to the specified event loop. The event loop library keeps a copy of event_data and manages
  230. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  231. * handler recieves is always valid.
  232. *
  233. * This function behaves in the same manner as esp_event_post_to, except the additional specification of the event loop
  234. * to post the event to.
  235. *
  236. * @param[in] event_loop the event loop to post to
  237. * @param[in] event_base the event base that identifies the event
  238. * @param[in] event_id the the event id that identifies the event
  239. * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler
  240. * @param[in] event_data_size the size of the event data
  241. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  242. *
  243. * @note posting events from an ISR is not supported
  244. *
  245. * @return
  246. * - ESP_OK: Success
  247. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired
  248. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event id
  249. * - Others: Fail
  250. */
  251. esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop,
  252. esp_event_base_t event_base,
  253. int32_t event_id,
  254. void* event_data,
  255. size_t event_data_size,
  256. TickType_t ticks_to_wait);
  257. /**
  258. * @brief Dumps statistics of all event loops.
  259. *
  260. * Dumps event loop info in the format:
  261. *
  262. @verbatim
  263. event loop
  264. handler
  265. handler
  266. ...
  267. event loop
  268. handler
  269. handler
  270. ...
  271. where:
  272. event loop
  273. format: address,name rx:total_recieved dr:total_dropped
  274. where:
  275. address - memory address of the event loop
  276. name - name of the event loop, 'none' if no dedicated task
  277. total_recieved - number of successfully posted events
  278. total_dropped - number of events unsucessfully posted due to queue being full
  279. handler
  280. format: address ev:base,id inv:total_invoked run:total_runtime
  281. where:
  282. address - address of the handler function
  283. base,id - the event specified by event base and id this handler executes
  284. total_invoked - number of times this handler has been invoked
  285. total_runtime - total amount of time used for invoking this handler
  286. @endverbatim
  287. *
  288. * @param[in] file the file stream to output to
  289. *
  290. * @note this function is a noop when CONFIG_EVENT_LOOP_PROFILING is disabled
  291. *
  292. * @return
  293. * - ESP_OK: Success
  294. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  295. * - Others: Fail
  296. */
  297. esp_err_t esp_event_dump(FILE* file);
  298. #ifdef __cplusplus
  299. } // extern "C"
  300. #endif
  301. #endif // #ifndef ESP_EVENT_H_