esp_event.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ESP_EVENT_H_
  7. #define ESP_EVENT_H_
  8. #include "esp_err.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/semphr.h"
  13. #include "esp_event_base.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /// Configuration for creating event loops
  18. typedef struct {
  19. int32_t queue_size; /**< size of the event loop queue */
  20. const char *task_name; /**< name of the event loop task; if NULL,
  21. a dedicated task is not created for event loop*/
  22. UBaseType_t task_priority; /**< priority of the event loop task, ignored if task name is NULL */
  23. uint32_t task_stack_size; /**< stack size of the event loop task, ignored if task name is NULL */
  24. BaseType_t task_core_id; /**< core to which the event loop task is pinned to,
  25. ignored if task name is NULL */
  26. } esp_event_loop_args_t;
  27. /**
  28. * @brief Create a new event loop.
  29. *
  30. * @param[in] event_loop_args configuration structure for the event loop to create
  31. * @param[out] event_loop handle to the created event loop
  32. *
  33. * @return
  34. * - ESP_OK: Success
  35. * - ESP_ERR_INVALID_ARG: event_loop_args or event_loop was NULL
  36. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  37. * - ESP_FAIL: Failed to create task loop
  38. * - Others: Fail
  39. */
  40. esp_err_t esp_event_loop_create(const esp_event_loop_args_t *event_loop_args, esp_event_loop_handle_t *event_loop);
  41. /**
  42. * @brief Delete an existing event loop.
  43. *
  44. * @param[in] event_loop event loop to delete, must not be NULL
  45. *
  46. * @return
  47. * - ESP_OK: Success
  48. * - Others: Fail
  49. */
  50. esp_err_t esp_event_loop_delete(esp_event_loop_handle_t event_loop);
  51. /**
  52. * @brief Create default event loop
  53. *
  54. * @return
  55. * - ESP_OK: Success
  56. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  57. * - ESP_FAIL: Failed to create task loop
  58. * - Others: Fail
  59. */
  60. esp_err_t esp_event_loop_create_default(void);
  61. /**
  62. * @brief Delete the default event loop
  63. *
  64. * @return
  65. * - ESP_OK: Success
  66. * - Others: Fail
  67. */
  68. esp_err_t esp_event_loop_delete_default(void);
  69. /**
  70. * @brief Dispatch events posted to an event loop.
  71. *
  72. * This function is used to dispatch events posted to a loop with no dedicated task, i.e. task name was set to NULL
  73. * in event_loop_args argument during loop creation. This function includes an argument to limit the amount of time
  74. * it runs, returning control to the caller when that time expires (or some time afterwards). There is no guarantee
  75. * that a call to this function will exit at exactly the time of expiry. There is also no guarantee that events have
  76. * been dispatched during the call, as the function might have spent all the allotted time waiting on the event queue.
  77. * Once an event has been dequeued, however, it is guaranteed to be dispatched. This guarantee contributes to not being
  78. * able to exit exactly at time of expiry as (1) blocking on internal mutexes is necessary for dispatching the dequeued
  79. * event, and (2) during dispatch of the dequeued event there is no way to control the time occupied by handler code
  80. * execution. The guaranteed time of exit is therefore the allotted time + amount of time required to dispatch
  81. * the last dequeued event.
  82. *
  83. * In cases where waiting on the queue times out, ESP_OK is returned and not ESP_ERR_TIMEOUT, since it is
  84. * normal behavior.
  85. *
  86. * @param[in] event_loop event loop to dispatch posted events from, must not be NULL
  87. * @param[in] ticks_to_run number of ticks to run the loop
  88. *
  89. * @note encountering an unknown event that has been posted to the loop will only generate a warning, not an error.
  90. *
  91. * @return
  92. * - ESP_OK: Success
  93. * - Others: Fail
  94. */
  95. esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t ticks_to_run);
  96. /**
  97. * @brief Register an event handler to the system event loop (legacy).
  98. *
  99. * This function can be used to register a handler for either: (1) specific events,
  100. * (2) all events of a certain event base, or (3) all events known by the system event loop.
  101. *
  102. * - specific events: specify exact event_base and event_id
  103. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  104. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  105. *
  106. * Registering multiple handlers to events is possible. Registering a single handler to multiple events is
  107. * also possible. However, registering the same handler to the same event multiple times would cause the
  108. * previous registrations to be overwritten.
  109. *
  110. * @param[in] event_base the base ID of the event to register the handler for
  111. * @param[in] event_id the ID of the event to register the handler for
  112. * @param[in] event_handler the handler function which gets called when the event is dispatched
  113. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  114. *
  115. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  116. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  117. *
  118. * @return
  119. * - ESP_OK: Success
  120. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  121. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  122. * - Others: Fail
  123. */
  124. esp_err_t esp_event_handler_register(esp_event_base_t event_base,
  125. int32_t event_id,
  126. esp_event_handler_t event_handler,
  127. void *event_handler_arg);
  128. /**
  129. * @brief Register an event handler to a specific loop (legacy).
  130. *
  131. * This function behaves in the same manner as esp_event_handler_register, except the additional
  132. * specification of the event loop to register the handler to.
  133. *
  134. * @param[in] event_loop the event loop to register this handler function to, must not be NULL
  135. * @param[in] event_base the base ID of the event to register the handler for
  136. * @param[in] event_id the ID of the event to register the handler for
  137. * @param[in] event_handler the handler function which gets called when the event is dispatched
  138. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  139. *
  140. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  141. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  142. *
  143. * @return
  144. * - ESP_OK: Success
  145. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  146. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  147. * - Others: Fail
  148. */
  149. esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop,
  150. esp_event_base_t event_base,
  151. int32_t event_id,
  152. esp_event_handler_t event_handler,
  153. void *event_handler_arg);
  154. /**
  155. * @brief Register an instance of event handler to a specific loop.
  156. *
  157. * This function can be used to register a handler for either: (1) specific events,
  158. * (2) all events of a certain event base, or (3) all events known by the system event loop.
  159. *
  160. * - specific events: specify exact event_base and event_id
  161. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  162. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  163. *
  164. * Besides the error, the function returns an instance object as output parameter to identify each registration.
  165. * This is necessary to remove (unregister) the registration before the event loop is deleted.
  166. *
  167. * Registering multiple handlers to events, registering a single handler to multiple events as well as registering
  168. * the same handler to the same event multiple times is possible.
  169. * Each registration yields a distinct instance object which identifies it over the registration
  170. * lifetime.
  171. *
  172. * @param[in] event_loop the event loop to register this handler function to, must not be NULL
  173. * @param[in] event_base the base ID of the event to register the handler for
  174. * @param[in] event_id the ID of the event to register the handler for
  175. * @param[in] event_handler the handler function which gets called when the event is dispatched
  176. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  177. * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL.
  178. * This needs to be kept if the specific callback instance should be unregistered before deleting the whole
  179. * event loop. Registering the same event handler multiple times is possible and yields distinct instance
  180. * objects. The data can be the same for all registrations.
  181. * If no unregistration is needed, but the handler should be deleted when the event loop is deleted,
  182. * instance can be NULL.
  183. *
  184. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  185. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  186. *
  187. * @return
  188. * - ESP_OK: Success
  189. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  190. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL
  191. * - Others: Fail
  192. */
  193. esp_err_t esp_event_handler_instance_register_with(esp_event_loop_handle_t event_loop,
  194. esp_event_base_t event_base,
  195. int32_t event_id,
  196. esp_event_handler_t event_handler,
  197. void *event_handler_arg,
  198. esp_event_handler_instance_t *instance);
  199. /**
  200. * @brief Register an instance of event handler to the default loop.
  201. *
  202. * This function does the same as esp_event_handler_instance_register_with, except that it registers the
  203. * handler to the default event loop.
  204. *
  205. * @param[in] event_base the base ID of the event to register the handler for
  206. * @param[in] event_id the ID of the event to register the handler for
  207. * @param[in] event_handler the handler function which gets called when the event is dispatched
  208. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  209. * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL.
  210. * This needs to be kept if the specific callback instance should be unregistered before deleting the whole
  211. * event loop. Registering the same event handler multiple times is possible and yields distinct instance
  212. * objects. The data can be the same for all registrations.
  213. * If no unregistration is needed, but the handler should be deleted when the event loop is deleted,
  214. * instance can be NULL.
  215. *
  216. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  217. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  218. *
  219. * @return
  220. * - ESP_OK: Success
  221. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  222. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL
  223. * - Others: Fail
  224. */
  225. esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base,
  226. int32_t event_id,
  227. esp_event_handler_t event_handler,
  228. void *event_handler_arg,
  229. esp_event_handler_instance_t *instance);
  230. /**
  231. * @brief Unregister a handler with the system event loop (legacy).
  232. *
  233. * Unregisters a handler, so it will no longer be called during dispatch.
  234. * Handlers can be unregistered for any combination of event_base and event_id which were previously registered.
  235. * To unregister a handler, the event_base and event_id arguments must match exactly the arguments passed to
  236. * esp_event_handler_register() when that handler was registered. Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID
  237. * will only unregister handlers that were registered with the same wildcard arguments.
  238. *
  239. * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be
  240. * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be
  241. * unregistered. This avoids accidental unregistration of handlers registered by other users or components.
  242. *
  243. * @param[in] event_base the base of the event with which to unregister the handler
  244. * @param[in] event_id the ID of the event with which to unregister the handler
  245. * @param[in] event_handler the handler to unregister
  246. *
  247. * @return ESP_OK success
  248. * @return ESP_ERR_INVALID_ARG invalid combination of event base and event ID
  249. * @return others fail
  250. */
  251. esp_err_t esp_event_handler_unregister(esp_event_base_t event_base,
  252. int32_t event_id,
  253. esp_event_handler_t event_handler);
  254. /**
  255. * @brief Unregister a handler from a specific event loop (legacy).
  256. *
  257. * This function behaves in the same manner as esp_event_handler_unregister, except the additional specification of
  258. * the event loop to unregister the handler with.
  259. *
  260. * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL
  261. * @param[in] event_base the base of the event with which to unregister the handler
  262. * @param[in] event_id the ID of the event with which to unregister the handler
  263. * @param[in] event_handler the handler to unregister
  264. *
  265. * @return
  266. * - ESP_OK: Success
  267. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  268. * - Others: Fail
  269. */
  270. esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop,
  271. esp_event_base_t event_base,
  272. int32_t event_id,
  273. esp_event_handler_t event_handler);
  274. /**
  275. * @brief Unregister a handler instance from a specific event loop.
  276. *
  277. * Unregisters a handler instance, so it will no longer be called during dispatch.
  278. * Handler instances can be unregistered for any combination of event_base and event_id which were previously
  279. * registered. To unregister a handler instance, the event_base and event_id arguments must match exactly the
  280. * arguments passed to esp_event_handler_instance_register() when that handler instance was registered.
  281. * Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID will only unregister handler instances that were registered
  282. * with the same wildcard arguments.
  283. *
  284. * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be
  285. * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be
  286. * unregistered. This avoids accidental unregistration of handlers registered by other users or components.
  287. *
  288. * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL
  289. * @param[in] event_base the base of the event with which to unregister the handler
  290. * @param[in] event_id the ID of the event with which to unregister the handler
  291. * @param[in] instance the instance object of the registration to be unregistered
  292. *
  293. * @return
  294. * - ESP_OK: Success
  295. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  296. * - Others: Fail
  297. */
  298. esp_err_t esp_event_handler_instance_unregister_with(esp_event_loop_handle_t event_loop,
  299. esp_event_base_t event_base,
  300. int32_t event_id,
  301. esp_event_handler_instance_t instance);
  302. /**
  303. * @brief Unregister a handler from the system event loop.
  304. *
  305. * This function does the same as esp_event_handler_instance_unregister_with, except that it unregisters the
  306. * handler instance from the default event loop.
  307. *
  308. * @param[in] event_base the base of the event with which to unregister the handler
  309. * @param[in] event_id the ID of the event with which to unregister the handler
  310. * @param[in] instance the instance object of the registration to be unregistered
  311. *
  312. * @return
  313. * - ESP_OK: Success
  314. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  315. * - Others: Fail
  316. */
  317. esp_err_t esp_event_handler_instance_unregister(esp_event_base_t event_base,
  318. int32_t event_id,
  319. esp_event_handler_instance_t instance);
  320. /**
  321. * @brief Posts an event to the system default event loop. The event loop library keeps a copy of event_data and manages
  322. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  323. * handler receives is always valid.
  324. *
  325. * @param[in] event_base the event base that identifies the event
  326. * @param[in] event_id the event ID that identifies the event
  327. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  328. * @param[in] event_data_size the size of the event data
  329. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  330. *
  331. * @return
  332. * - ESP_OK: Success
  333. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired,
  334. * queue full when posting from ISR
  335. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  336. * - Others: Fail
  337. */
  338. esp_err_t esp_event_post(esp_event_base_t event_base,
  339. int32_t event_id,
  340. const void *event_data,
  341. size_t event_data_size,
  342. TickType_t ticks_to_wait);
  343. /**
  344. * @brief Posts an event to the specified event loop. The event loop library keeps a copy of event_data and manages
  345. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  346. * handler receives is always valid.
  347. *
  348. * This function behaves in the same manner as esp_event_post_to, except the additional specification of the event loop
  349. * to post the event to.
  350. *
  351. * @param[in] event_loop the event loop to post to, must not be NULL
  352. * @param[in] event_base the event base that identifies the event
  353. * @param[in] event_id the event ID that identifies the event
  354. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  355. * @param[in] event_data_size the size of the event data
  356. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  357. *
  358. * @return
  359. * - ESP_OK: Success
  360. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired,
  361. * queue full when posting from ISR
  362. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  363. * - Others: Fail
  364. */
  365. esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop,
  366. esp_event_base_t event_base,
  367. int32_t event_id,
  368. const void *event_data,
  369. size_t event_data_size,
  370. TickType_t ticks_to_wait);
  371. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  372. /**
  373. * @brief Special variant of esp_event_post for posting events from interrupt handlers.
  374. *
  375. * @param[in] event_base the event base that identifies the event
  376. * @param[in] event_id the event ID that identifies the event
  377. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  378. * @param[in] event_data_size the size of the event data; max is 4 bytes
  379. * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with
  380. * higher priority than currently running task has been unblocked by the posted event;
  381. * a context switch should be requested before the interrupt is existed.
  382. *
  383. * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled
  384. * @note when this function is called from an interrupt handler placed in IRAM, this function should
  385. * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR
  386. *
  387. * @return
  388. * - ESP_OK: Success
  389. * - ESP_FAIL: Event queue for the default event loop full
  390. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID,
  391. * data size of more than 4 bytes
  392. * - Others: Fail
  393. */
  394. esp_err_t esp_event_isr_post(esp_event_base_t event_base,
  395. int32_t event_id,
  396. const void *event_data,
  397. size_t event_data_size,
  398. BaseType_t *task_unblocked);
  399. /**
  400. * @brief Special variant of esp_event_post_to for posting events from interrupt handlers
  401. *
  402. * @param[in] event_loop the event loop to post to, must not be NULL
  403. * @param[in] event_base the event base that identifies the event
  404. * @param[in] event_id the event ID that identifies the event
  405. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  406. * @param[in] event_data_size the size of the event data
  407. * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with
  408. * higher priority than currently running task has been unblocked by the posted event;
  409. * a context switch should be requested before the interrupt is existed.
  410. *
  411. * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled
  412. * @note when this function is called from an interrupt handler placed in IRAM, this function should
  413. * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR
  414. *
  415. * @return
  416. * - ESP_OK: Success
  417. * - ESP_FAIL: Event queue for the loop full
  418. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID,
  419. * data size of more than 4 bytes
  420. * - Others: Fail
  421. */
  422. esp_err_t esp_event_isr_post_to(esp_event_loop_handle_t event_loop,
  423. esp_event_base_t event_base,
  424. int32_t event_id,
  425. const void *event_data,
  426. size_t event_data_size,
  427. BaseType_t *task_unblocked);
  428. #endif
  429. /**
  430. * @brief Dumps statistics of all event loops.
  431. *
  432. * Dumps event loop info in the format:
  433. *
  434. @verbatim
  435. event loop
  436. handler
  437. handler
  438. ...
  439. event loop
  440. handler
  441. handler
  442. ...
  443. where:
  444. event loop
  445. format: address,name rx:total_received dr:total_dropped
  446. where:
  447. address - memory address of the event loop
  448. name - name of the event loop, 'none' if no dedicated task
  449. total_received - number of successfully posted events
  450. total_dropped - number of events unsuccessfully posted due to queue being full
  451. handler
  452. format: address ev:base,id inv:total_invoked run:total_runtime
  453. where:
  454. address - address of the handler function
  455. base,id - the event specified by event base and ID this handler executes
  456. total_invoked - number of times this handler has been invoked
  457. total_runtime - total amount of time used for invoking this handler
  458. @endverbatim
  459. *
  460. * @param[in] file the file stream to output to
  461. *
  462. * @note this function is a noop when CONFIG_ESP_EVENT_LOOP_PROFILING is disabled
  463. *
  464. * @return
  465. * - ESP_OK: Success
  466. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  467. * - Others: Fail
  468. */
  469. esp_err_t esp_event_dump(FILE *file);
  470. #ifdef __cplusplus
  471. } // extern "C"
  472. #endif
  473. #endif // #ifndef ESP_EVENT_H_