esp_event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_INVALIG_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. * @return
  149. * - ESP_OK: Success
  150. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  151. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  152. * - Others: Fail
  153. */
  154. esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop,
  155. esp_event_base_t event_base,
  156. int32_t event_id,
  157. esp_event_handler_t event_handler,
  158. void* event_handler_arg);
  159. /**
  160. * @brief Unregister a handler with the system event loop.
  161. *
  162. * This function can be used to unregister a handler so that it no longer gets called during dispatch.
  163. * Handlers can be unregistered for either: (1) specific events, (2) all events of a certain event base,
  164. * or (3) all events known by the system event loop
  165. *
  166. * - specific events: specify exact event_base and event_id
  167. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  168. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  169. *
  170. * This function ignores unregistration of handlers that has not been previously registered.
  171. *
  172. * @param[in] event_base the base of the event with which to unregister the handler
  173. * @param[in] event_id the id of the event with which to unregister the handler
  174. * @param[in] event_handler the handler to unregister
  175. *
  176. * @return ESP_OK success
  177. * @return ESP_ERR_INVALIG_ARG invalid combination of event base and event id
  178. * @return others fail
  179. */
  180. esp_err_t esp_event_handler_unregister(esp_event_base_t event_base, int32_t event_id, esp_event_handler_t event_handler);
  181. /**
  182. * @brief Unregister a handler with the system event loop.
  183. *
  184. * This function behaves in the same manner as esp_event_handler_unregister, except the additional specification of
  185. * the event loop to unregister the handler with.
  186. *
  187. * @param[in] event_loop the event loop with which to unregister this handler function
  188. * @param[in] event_base the base of the event with which to unregister the handler
  189. * @param[in] event_id the id of the event with which to unregister the handler
  190. * @param[in] event_handler the handler to unregister
  191. *
  192. * @return
  193. * - ESP_OK: Success
  194. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  195. * - Others: Fail
  196. */
  197. esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop,
  198. esp_event_base_t event_base,
  199. int32_t event_id,
  200. esp_event_handler_t event_handler);
  201. /**
  202. * @brief Posts an event to the system default event loop. The event loop library keeps a copy of event_data and manages
  203. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  204. * handler recieves is always valid.
  205. *
  206. * @param[in] event_base the event base that identifies the event
  207. * @param[in] event_id the the event id that identifies the event
  208. * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler
  209. * @param[in] event_data_size the size of the event data
  210. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  211. *
  212. * @note posting events from an ISR is not supported
  213. *
  214. * @return
  215. * - ESP_OK: Success
  216. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired
  217. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  218. * - Others: Fail
  219. */
  220. esp_err_t esp_event_post(esp_event_base_t event_base,
  221. int32_t event_id,
  222. void* event_data,
  223. size_t event_data_size,
  224. TickType_t ticks_to_wait);
  225. /**
  226. * @brief Posts an event to the specified event loop. The event loop library keeps a copy of event_data and manages
  227. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  228. * handler recieves is always valid.
  229. *
  230. * This function behaves in the same manner as esp_event_post_to, except the additional specification of the event loop
  231. * to post the event to.
  232. *
  233. * @param[in] event_loop the event loop to post to
  234. * @param[in] event_base the event base that identifies the event
  235. * @param[in] event_id the the event id that identifies the event
  236. * @param[in] event_data the data, specific to the event occurence, that gets passed to the handler
  237. * @param[in] event_data_size the size of the event data
  238. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  239. *
  240. * @note posting events from an ISR is not supported
  241. *
  242. * @return
  243. * - ESP_OK: Success
  244. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired
  245. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  246. * - Others: Fail
  247. */
  248. esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop,
  249. esp_event_base_t event_base,
  250. int32_t event_id,
  251. void* event_data,
  252. size_t event_data_size,
  253. TickType_t ticks_to_wait);
  254. /**
  255. * @brief Dumps statistics of all event loops.
  256. *
  257. * Dumps event loop info in the format:
  258. *
  259. @verbatim
  260. event loop
  261. event
  262. handler
  263. handler
  264. event
  265. handler
  266. handler
  267. event loop
  268. event
  269. handler
  270. ...
  271. ...
  272. ...
  273. where:
  274. event loop
  275. format: address,name rx:total_recieved dr:total_dropped inv:total_number_of_invocations run:total_runtime
  276. where:
  277. address - memory address of the event loop
  278. name - name of the event loop
  279. total_recieved - number of successfully posted events
  280. total_number_of_invocations - total number of handler invocations performed so far
  281. total_runtime - total runtime of all invocations so far
  282. event
  283. format: base:id proc:total_processed run:total_runtime
  284. where:
  285. base - event base
  286. id - event id
  287. total_processed - number of instances of this event that has been processed
  288. total_runtime - total amount of time in microseconds used for invoking handlers of this event
  289. handler
  290. format: address inv:total_invoked run:total_runtime
  291. where:
  292. address - address of the handler function
  293. total_invoked - number of times this handler has been invoked
  294. total_runtime - total amount of time used for invoking this handler
  295. @endverbatim
  296. *
  297. * @param[in] file the file stream to output to
  298. *
  299. * @note this function is a noop when CONFIG_EVENT_LOOP_PROFILING is disabled
  300. *
  301. * @return
  302. * - ESP_OK: Success
  303. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  304. * - Others: Fail
  305. */
  306. esp_err_t esp_event_dump(FILE* file);
  307. #ifdef __cplusplus
  308. } // extern "C"
  309. #endif
  310. #endif // #ifndef ESP_EVENT_H_