esp_event.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. * @note This function is obsolete and will be deprecated soon, please use esp_event_handler_instance_register()
  100. * instead.
  101. *
  102. * This function can be used to register a handler for either: (1) specific events,
  103. * (2) all events of a certain event base, or (3) all events known by the system event loop.
  104. *
  105. * - specific events: specify exact event_base and event_id
  106. * - all events of a certain base: specify exact event_base and use ESP_EVENT_ANY_ID as the event_id
  107. * - all events known by the loop: use ESP_EVENT_ANY_BASE for event_base and ESP_EVENT_ANY_ID as the event_id
  108. *
  109. * Registering multiple handlers to events is possible. Registering a single handler to multiple events is
  110. * also possible. However, registering the same handler to the same event multiple times would cause the
  111. * previous registrations to be overwritten.
  112. *
  113. * @param[in] event_base the base ID of the event to register the handler for
  114. * @param[in] event_id the ID of the event to register the handler for
  115. * @param[in] event_handler the handler function which gets called when the event is dispatched
  116. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  117. *
  118. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  119. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  120. *
  121. * @return
  122. * - ESP_OK: Success
  123. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  124. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  125. * - Others: Fail
  126. */
  127. esp_err_t esp_event_handler_register(esp_event_base_t event_base,
  128. int32_t event_id,
  129. esp_event_handler_t event_handler,
  130. void *event_handler_arg);
  131. /**
  132. * @brief Register an event handler to a specific loop (legacy).
  133. *
  134. * @note This function is obsolete and will be deprecated soon, please use esp_event_handler_instance_register_with()
  135. * instead.
  136. *
  137. * This function behaves in the same manner as esp_event_handler_register, except the additional
  138. * specification of the event loop to register the handler to.
  139. *
  140. * @param[in] event_loop the event loop to register this handler function to, must not be NULL
  141. * @param[in] event_base the base ID of the event to register the handler for
  142. * @param[in] event_id the ID of the event to register the handler for
  143. * @param[in] event_handler the handler function which gets called when the event is dispatched
  144. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  145. *
  146. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  147. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  148. *
  149. * @return
  150. * - ESP_OK: Success
  151. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  152. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  153. * - Others: Fail
  154. */
  155. esp_err_t esp_event_handler_register_with(esp_event_loop_handle_t event_loop,
  156. esp_event_base_t event_base,
  157. int32_t event_id,
  158. esp_event_handler_t event_handler,
  159. void *event_handler_arg);
  160. /**
  161. * @brief Register an instance of event handler to a specific loop.
  162. *
  163. * This function can be used to register a handler for either: (1) specific events,
  164. * (2) all events of a certain event base, 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. * Besides the error, the function returns an instance object as output parameter to identify each registration.
  171. * This is necessary to remove (unregister) the registration before the event loop is deleted.
  172. *
  173. * Registering multiple handlers to events, registering a single handler to multiple events as well as registering
  174. * the same handler to the same event multiple times is possible.
  175. * Each registration yields a distinct instance object which identifies it over the registration
  176. * lifetime.
  177. *
  178. * @param[in] event_loop the event loop to register this handler function to, must not be NULL
  179. * @param[in] event_base the base ID of the event to register the handler for
  180. * @param[in] event_id the ID of the event to register the handler for
  181. * @param[in] event_handler the handler function which gets called when the event is dispatched
  182. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  183. * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL.
  184. * This needs to be kept if the specific callback instance should be unregistered before deleting the whole
  185. * event loop. Registering the same event handler multiple times is possible and yields distinct instance
  186. * objects. The data can be the same for all registrations.
  187. * If no unregistration is needed, but the handler should be deleted when the event loop is deleted,
  188. * instance can be NULL.
  189. *
  190. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  191. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  192. *
  193. * @return
  194. * - ESP_OK: Success
  195. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  196. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL
  197. * - Others: Fail
  198. */
  199. esp_err_t esp_event_handler_instance_register_with(esp_event_loop_handle_t event_loop,
  200. esp_event_base_t event_base,
  201. int32_t event_id,
  202. esp_event_handler_t event_handler,
  203. void *event_handler_arg,
  204. esp_event_handler_instance_t *instance);
  205. /**
  206. * @brief Register an instance of event handler to the default loop.
  207. *
  208. * This function does the same as esp_event_handler_instance_register_with, except that it registers the
  209. * handler to the default event loop.
  210. *
  211. * @param[in] event_base the base ID of the event to register the handler for
  212. * @param[in] event_id the ID of the event to register the handler for
  213. * @param[in] event_handler the handler function which gets called when the event is dispatched
  214. * @param[in] event_handler_arg data, aside from event data, that is passed to the handler when it is called
  215. * @param[out] instance An event handler instance object related to the registered event handler and data, can be NULL.
  216. * This needs to be kept if the specific callback instance should be unregistered before deleting the whole
  217. * event loop. Registering the same event handler multiple times is possible and yields distinct instance
  218. * objects. The data can be the same for all registrations.
  219. * If no unregistration is needed, but the handler should be deleted when the event loop is deleted,
  220. * instance can be NULL.
  221. *
  222. * @note the event loop library does not maintain a copy of event_handler_arg, therefore the user should
  223. * ensure that event_handler_arg still points to a valid location by the time the handler gets called
  224. *
  225. * @return
  226. * - ESP_OK: Success
  227. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  228. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID or instance is NULL
  229. * - Others: Fail
  230. */
  231. esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base,
  232. int32_t event_id,
  233. esp_event_handler_t event_handler,
  234. void *event_handler_arg,
  235. esp_event_handler_instance_t *instance);
  236. /**
  237. * @brief Unregister a handler with the system event loop (legacy).
  238. *
  239. * @note This function is obsolete and will be deprecated soon, please use esp_event_handler_instance_unregister()
  240. * instead.
  241. *
  242. * Unregisters a handler, so it will no longer be called during dispatch.
  243. * Handlers can be unregistered for any combination of event_base and event_id which were previously registered.
  244. * To unregister a handler, the event_base and event_id arguments must match exactly the arguments passed to
  245. * esp_event_handler_register() when that handler was registered. Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID
  246. * will only unregister handlers that were registered with the same wildcard arguments.
  247. *
  248. * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be
  249. * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be
  250. * unregistered. This avoids accidental unregistration of handlers registered by other users or components.
  251. *
  252. * @param[in] event_base the base of the event with which to unregister the handler
  253. * @param[in] event_id the ID of the event with which to unregister the handler
  254. * @param[in] event_handler the handler to unregister
  255. *
  256. * @return ESP_OK success
  257. * @return ESP_ERR_INVALID_ARG invalid combination of event base and event ID
  258. * @return others fail
  259. */
  260. esp_err_t esp_event_handler_unregister(esp_event_base_t event_base,
  261. int32_t event_id,
  262. esp_event_handler_t event_handler);
  263. /**
  264. * @brief Unregister a handler from a specific event loop (legacy).
  265. *
  266. * @note This function is obsolete and will be deprecated soon, please use esp_event_handler_instance_unregister_with()
  267. * instead.
  268. *
  269. * This function behaves in the same manner as esp_event_handler_unregister, except the additional specification of
  270. * the event loop to unregister the handler with.
  271. *
  272. * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL
  273. * @param[in] event_base the base of the event with which to unregister the handler
  274. * @param[in] event_id the ID of the event with which to unregister the handler
  275. * @param[in] event_handler the handler to unregister
  276. *
  277. * @return
  278. * - ESP_OK: Success
  279. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  280. * - Others: Fail
  281. */
  282. esp_err_t esp_event_handler_unregister_with(esp_event_loop_handle_t event_loop,
  283. esp_event_base_t event_base,
  284. int32_t event_id,
  285. esp_event_handler_t event_handler);
  286. /**
  287. * @brief Unregister a handler instance from a specific event loop.
  288. *
  289. * Unregisters a handler instance, so it will no longer be called during dispatch.
  290. * Handler instances can be unregistered for any combination of event_base and event_id which were previously
  291. * registered. To unregister a handler instance, the event_base and event_id arguments must match exactly the
  292. * arguments passed to esp_event_handler_instance_register() when that handler instance was registered.
  293. * Passing ESP_EVENT_ANY_BASE and/or ESP_EVENT_ANY_ID will only unregister handler instances that were registered
  294. * with the same wildcard arguments.
  295. *
  296. * @note When using ESP_EVENT_ANY_ID, handlers registered to specific event IDs using the same base will not be
  297. * unregistered. When using ESP_EVENT_ANY_BASE, events registered to specific bases will also not be
  298. * unregistered. This avoids accidental unregistration of handlers registered by other users or components.
  299. *
  300. * @param[in] event_loop the event loop with which to unregister this handler function, must not be NULL
  301. * @param[in] event_base the base of the event with which to unregister the handler
  302. * @param[in] event_id the ID of the event with which to unregister the handler
  303. * @param[in] instance the instance object of the registration to be unregistered
  304. *
  305. * @return
  306. * - ESP_OK: Success
  307. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  308. * - Others: Fail
  309. */
  310. esp_err_t esp_event_handler_instance_unregister_with(esp_event_loop_handle_t event_loop,
  311. esp_event_base_t event_base,
  312. int32_t event_id,
  313. esp_event_handler_instance_t instance);
  314. /**
  315. * @brief Unregister a handler from the system event loop.
  316. *
  317. * This function does the same as esp_event_handler_instance_unregister_with, except that it unregisters the
  318. * handler instance from the default event loop.
  319. *
  320. * @param[in] event_base the base of the event with which to unregister the handler
  321. * @param[in] event_id the ID of the event with which to unregister the handler
  322. * @param[in] instance the instance object of the registration to be unregistered
  323. *
  324. * @return
  325. * - ESP_OK: Success
  326. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  327. * - Others: Fail
  328. */
  329. esp_err_t esp_event_handler_instance_unregister(esp_event_base_t event_base,
  330. int32_t event_id,
  331. esp_event_handler_instance_t instance);
  332. /**
  333. * @brief Posts an event to the system default event loop. The event loop library keeps a copy of event_data and manages
  334. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  335. * handler receives is always valid.
  336. *
  337. * @param[in] event_base the event base that identifies the event
  338. * @param[in] event_id the event ID that identifies the event
  339. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  340. * @param[in] event_data_size the size of the event data
  341. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  342. *
  343. * @return
  344. * - ESP_OK: Success
  345. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired,
  346. * queue full when posting from ISR
  347. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  348. * - Others: Fail
  349. */
  350. esp_err_t esp_event_post(esp_event_base_t event_base,
  351. int32_t event_id,
  352. const void *event_data,
  353. size_t event_data_size,
  354. TickType_t ticks_to_wait);
  355. /**
  356. * @brief Posts an event to the specified event loop. The event loop library keeps a copy of event_data and manages
  357. * the copy's lifetime automatically (allocation + deletion); this ensures that the data the
  358. * handler receives is always valid.
  359. *
  360. * This function behaves in the same manner as esp_event_post_to, except the additional specification of the event loop
  361. * to post the event to.
  362. *
  363. * @param[in] event_loop the event loop to post to, must not be NULL
  364. * @param[in] event_base the event base that identifies the event
  365. * @param[in] event_id the event ID that identifies the event
  366. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  367. * @param[in] event_data_size the size of the event data
  368. * @param[in] ticks_to_wait number of ticks to block on a full event queue
  369. *
  370. * @return
  371. * - ESP_OK: Success
  372. * - ESP_ERR_TIMEOUT: Time to wait for event queue to unblock expired,
  373. * queue full when posting from ISR
  374. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID
  375. * - Others: Fail
  376. */
  377. esp_err_t esp_event_post_to(esp_event_loop_handle_t event_loop,
  378. esp_event_base_t event_base,
  379. int32_t event_id,
  380. const void *event_data,
  381. size_t event_data_size,
  382. TickType_t ticks_to_wait);
  383. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  384. /**
  385. * @brief Special variant of esp_event_post for posting events from interrupt handlers.
  386. *
  387. * @param[in] event_base the event base that identifies the event
  388. * @param[in] event_id the event ID that identifies the event
  389. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  390. * @param[in] event_data_size the size of the event data; max is 4 bytes
  391. * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with
  392. * higher priority than currently running task has been unblocked by the posted event;
  393. * a context switch should be requested before the interrupt is existed.
  394. *
  395. * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled
  396. * @note when this function is called from an interrupt handler placed in IRAM, this function should
  397. * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR
  398. *
  399. * @return
  400. * - ESP_OK: Success
  401. * - ESP_FAIL: Event queue for the default event loop full
  402. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID,
  403. * data size of more than 4 bytes
  404. * - Others: Fail
  405. */
  406. esp_err_t esp_event_isr_post(esp_event_base_t event_base,
  407. int32_t event_id,
  408. const void *event_data,
  409. size_t event_data_size,
  410. BaseType_t *task_unblocked);
  411. /**
  412. * @brief Special variant of esp_event_post_to for posting events from interrupt handlers
  413. *
  414. * @param[in] event_loop the event loop to post to, must not be NULL
  415. * @param[in] event_base the event base that identifies the event
  416. * @param[in] event_id the event ID that identifies the event
  417. * @param[in] event_data the data, specific to the event occurrence, that gets passed to the handler
  418. * @param[in] event_data_size the size of the event data
  419. * @param[out] task_unblocked an optional parameter (can be NULL) which indicates that an event task with
  420. * higher priority than currently running task has been unblocked by the posted event;
  421. * a context switch should be requested before the interrupt is existed.
  422. *
  423. * @note this function is only available when CONFIG_ESP_EVENT_POST_FROM_ISR is enabled
  424. * @note when this function is called from an interrupt handler placed in IRAM, this function should
  425. * be placed in IRAM as well by enabling CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR
  426. *
  427. * @return
  428. * - ESP_OK: Success
  429. * - ESP_FAIL: Event queue for the loop full
  430. * - ESP_ERR_INVALID_ARG: Invalid combination of event base and event ID,
  431. * data size of more than 4 bytes
  432. * - Others: Fail
  433. */
  434. esp_err_t esp_event_isr_post_to(esp_event_loop_handle_t event_loop,
  435. esp_event_base_t event_base,
  436. int32_t event_id,
  437. const void *event_data,
  438. size_t event_data_size,
  439. BaseType_t *task_unblocked);
  440. #endif
  441. /**
  442. * @brief Dumps statistics of all event loops.
  443. *
  444. * Dumps event loop info in the format:
  445. *
  446. @verbatim
  447. event loop
  448. handler
  449. handler
  450. ...
  451. event loop
  452. handler
  453. handler
  454. ...
  455. where:
  456. event loop
  457. format: address,name rx:total_received dr:total_dropped
  458. where:
  459. address - memory address of the event loop
  460. name - name of the event loop, 'none' if no dedicated task
  461. total_received - number of successfully posted events
  462. total_dropped - number of events unsuccessfully posted due to queue being full
  463. handler
  464. format: address ev:base,id inv:total_invoked run:total_runtime
  465. where:
  466. address - address of the handler function
  467. base,id - the event specified by event base and ID this handler executes
  468. total_invoked - number of times this handler has been invoked
  469. total_runtime - total amount of time used for invoking this handler
  470. @endverbatim
  471. *
  472. * @param[in] file the file stream to output to
  473. *
  474. * @note this function is a noop when CONFIG_ESP_EVENT_LOOP_PROFILING is disabled
  475. *
  476. * @return
  477. * - ESP_OK: Success
  478. * - ESP_ERR_NO_MEM: Cannot allocate memory for event loops list
  479. * - Others: Fail
  480. */
  481. esp_err_t esp_event_dump(FILE *file);
  482. #ifdef __cplusplus
  483. } // extern "C"
  484. #endif
  485. #endif // #ifndef ESP_EVENT_H_