esp_event.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. Event Loop Library
  2. ==================
  3. Overview
  4. --------
  5. The event loop library allows components to declare events to which other components can register handlers -- code which will execute when those events occur. This allows loosely coupled components to attach desired behavior to state changes of other components without application involvement. This also simplifies event processing by serializing and deferring code execution to another context.
  6. .. only:: SOC_WIFI_SUPPORTED
  7. One common use case is if a high level library is using the WiFi library: it may subscribe to :ref:`events produced by the Wi-Fi subsystem <wifi-programming-model>` directly and act on those events.
  8. .. only:: SOC_BT_SUPPORTED
  9. .. note::
  10. Various modules of the Bluetooth stack deliver events to applications via dedicated callback functions instead of via the Event Loop Library.
  11. Using ``esp_event`` APIs
  12. ------------------------
  13. There are two objects of concern for users of this library: events and event loops.
  14. Events are occurrences of note. For example, for Wi-Fi, a successful connection to the access point may be an event.
  15. Events are referenced using a two part identifier which are discussed more :ref:`here <esp-event-declaring-defining-events>`.
  16. Event loops are the vehicle by which events get posted by event sources and handled by event handler functions.
  17. These two appear prominently in the event loop library APIs.
  18. Using this library roughly entails the following flow:
  19. 1. A user defines a function that should run when an event is posted to a loop. This function is referred to as the event handler. It should have the same signature as :cpp:type:`esp_event_handler_t`.
  20. 2. An event loop is created using :cpp:func:`esp_event_loop_create`, which outputs a handle to the loop of type :cpp:type:`esp_event_loop_handle_t`. Event loops created using this API are referred to as user event loops. There is, however, a special type of event loop called the default event loop which are discussed :ref:`here <esp-event-default-loops>`.
  21. 3. Components register event handlers to the loop using :cpp:func:`esp_event_handler_register_with`. Handlers can be registered with multiple loops, more on that :ref:`here <esp-event-handler-registration>`.
  22. 4. Event sources post an event to the loop using :cpp:func:`esp_event_post_to`.
  23. 5. Components wanting to remove their handlers from being called can do so by unregistering from the loop using :cpp:func:`esp_event_handler_unregister_with`.
  24. 6. Event loops which are no longer needed can be deleted using :cpp:func:`esp_event_loop_delete`.
  25. In code, the flow above may look like as follows:
  26. .. code-block:: c
  27. // 1. Define the event handler
  28. void run_on_event(void* handler_arg, esp_event_base_t base, int32_t id, void* event_data)
  29. {
  30. // Event handler logic
  31. }
  32. void app_main()
  33. {
  34. // 2. A configuration structure of type esp_event_loop_args_t is needed to specify the properties of the loop to be
  35. // created. A handle of type esp_event_loop_handle_t is obtained, which is needed by the other APIs to reference the loop
  36. // to perform their operations on.
  37. esp_event_loop_args_t loop_args = {
  38. .queue_size = ...,
  39. .task_name = ...
  40. .task_priority = ...,
  41. .task_stack_size = ...,
  42. .task_core_id = ...
  43. };
  44. esp_event_loop_handle_t loop_handle;
  45. esp_event_loop_create(&loop_args, &loop_handle);
  46. // 3. Register event handler defined in (1). MY_EVENT_BASE and MY_EVENT_ID specifies a hypothetical
  47. // event that handler run_on_event should execute on when it gets posted to the loop.
  48. esp_event_handler_register_with(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_on_event, ...);
  49. ...
  50. // 4. Post events to the loop. This queues the event on the event loop. At some point in time
  51. // the event loop executes the event handler registered to the posted event, in this case run_on_event.
  52. // For simplicity sake this example calls esp_event_post_to from app_main, but posting can be done from
  53. // any other tasks (which is the more interesting use case).
  54. esp_event_post_to(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, ...);
  55. ...
  56. // 5. Unregistering an unneeded handler
  57. esp_event_handler_unregister_with(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_on_event);
  58. ...
  59. // 6. Deleting an unneeded event loop
  60. esp_event_loop_delete(loop_handle);
  61. }
  62. .. _esp-event-declaring-defining-events:
  63. Declaring and defining events
  64. -----------------------------
  65. As mentioned previously, events consists of two-part identifiers: the event base and the event ID. The event base identifies an independent group
  66. of events; the event ID identifies the event within that group. Think of the event base and event ID as a
  67. person's last name and first name, respectively. A last name identifies a family, and the first name identifies a person within that family.
  68. The event loop library provides macros to declare and define the event base easily.
  69. Event base declaration:
  70. .. code-block:: c
  71. ESP_EVENT_DECLARE_BASE(EVENT_BASE)
  72. Event base definition:
  73. .. code-block:: c
  74. ESP_EVENT_DEFINE_BASE(EVENT_BASE)
  75. .. note::
  76. In IDF, the base identifiers for system events are uppercase and are postfixed with ``_EVENT``. For example, the base for Wi-Fi events is declared and defined
  77. as ``WIFI_EVENT``, the Ethernet event base ``ETHERNET_EVENT``, and so on. The purpose is to have event bases look like constants (although
  78. they are global variables considering the definitions of macros ``ESP_EVENT_DECLARE_BASE`` and ``ESP_EVENT_DEFINE_BASE``).
  79. For event ID's, declaring them as enumerations is recommended. Once again, for visibility, these are typically placed in public header files.
  80. Event ID:
  81. .. code-block:: c
  82. enum {
  83. EVENT_ID_1,
  84. EVENT_ID_2,
  85. EVENT_ID_3,
  86. ...
  87. }
  88. .. _esp-event-default-loops:
  89. Default Event Loop
  90. ------------------
  91. The default event loop is a special type of loop used for system events (Wi-Fi events, for example). The handle for this
  92. loop is hidden from the user. The creation, deletion, handler registration/unregistration and posting of events is done
  93. through a variant of the APIs for user event loops. The table below enumerates those variants, and the user event
  94. loops equivalent.
  95. +---------------------------------------------------+---------------------------------------------------+
  96. | User Event Loops | Default Event Loops |
  97. +===================================================+===================================================+
  98. | :cpp:func:`esp_event_loop_create` | :cpp:func:`esp_event_loop_create_default` |
  99. +---------------------------------------------------+---------------------------------------------------+
  100. | :cpp:func:`esp_event_loop_delete` | :cpp:func:`esp_event_loop_delete_default` |
  101. +---------------------------------------------------+---------------------------------------------------+
  102. | :cpp:func:`esp_event_handler_register_with` | :cpp:func:`esp_event_handler_register` |
  103. +---------------------------------------------------+---------------------------------------------------+
  104. | :cpp:func:`esp_event_handler_unregister_with` | :cpp:func:`esp_event_handler_unregister` |
  105. +---------------------------------------------------+---------------------------------------------------+
  106. | :cpp:func:`esp_event_post_to` | :cpp:func:`esp_event_post` |
  107. +---------------------------------------------------+---------------------------------------------------+
  108. If you compare the signatures for both, they are mostly similar except the for the lack of loop handle
  109. specification for the default event loop APIs.
  110. Other than the API difference and the special designation to which system events are posted to, there is no difference
  111. to how default event loops and user event loops behave. It is even possible for users to post their own events
  112. to the default event loop, should the user opt to not create their own loops to save memory.
  113. .. _esp-event-handler-registration:
  114. Notes on Handler Registration
  115. -----------------------------
  116. It is possible to register a single handler to multiple events individually, i.e. using multiple calls to :cpp:func:`esp_event_handler_register_with`.
  117. For those multiple calls, the specific event base and event ID can be specified with which the handler should execute.
  118. However, in some cases it is desirable for a handler to execute on (1) all events that get posted to a loop or (2) all events
  119. of a particular base identifier. This is possible using the special event base identifier ``ESP_EVENT_ANY_BASE`` and
  120. special event ID ``ESP_EVENT_ANY_ID``. These special identifiers may be passed as the event base and event ID arguments
  121. for :cpp:func:`esp_event_handler_register_with`.
  122. Therefore, the valid arguments to :cpp:func:`esp_event_handler_register_with` are:
  123. 1. <event base>, <event ID> - handler executes when the event with base <event base> and event ID <event ID> gets posted to the loop
  124. 2. <event base>, ESP_EVENT_ANY_ID - handler executes when any event with base <event base> gets posted to the loop
  125. 3. ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID - handler executes when any event gets posted to the loop
  126. As an example, suppose the following handler registrations were performed:
  127. .. code-block:: c
  128. esp_event_handler_register_with(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_on_event_1, ...);
  129. esp_event_handler_register_with(loop_handle, MY_EVENT_BASE, ESP_EVENT_ANY_ID, run_on_event_2, ...);
  130. esp_event_handler_register_with(loop_handle, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, run_on_event_3, ...);
  131. If the hypothetical event ``MY_EVENT_BASE``, ``MY_EVENT_ID`` is posted, all three handlers ``run_on_event_1``, ``run_on_event_2``,
  132. and ``run_on_event_3`` would execute.
  133. If the hypothetical event ``MY_EVENT_BASE``, ``MY_OTHER_EVENT_ID`` is posted, only ``run_on_event_2`` and ``run_on_event_3`` would execute.
  134. If the hypothetical event ``MY_OTHER_EVENT_BASE``, ``MY_OTHER_EVENT_ID`` is posted, only ``run_on_event_3`` would execute.
  135. Handler Un-registering Itself
  136. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  137. In general, an event handler run by an event loop is *not allowed to do any (un)registering activity on that event loop*. There is one exception, though: un-registering itself is allowed for the handler. E.g., it is possible to do the following:
  138. .. code-block:: c
  139. void run_on_event(void* handler_arg, esp_event_base_t base, int32_t id, void* event_data)
  140. {
  141. esp_event_loop_handle_t *loop_handle = (esp_event_loop_handle_t*) handler_arg;
  142. esp_event_handler_unregister_with(*loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_on_event);
  143. }
  144. void app_main(void)
  145. {
  146. esp_event_loop_handle_t loop_handle;
  147. esp_event_loop_create(&loop_args, &loop_handle);
  148. esp_event_handler_register_with(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_on_event, &loop_handle);
  149. // ... post event MY_EVENT_BASE, MY_EVENT_ID and run loop at some point
  150. }
  151. Handler Registration and Handler Dispatch Order
  152. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  153. The general rule is that for handlers that match a certain posted event during dispatch, those which are registered first also gets executed first. The user can then control which handlers get executed first by
  154. registering them before other handlers, provided that all registrations are performed using a single task.
  155. If the user plans to take advantage of this behavior, caution must be exercised if there are multiple tasks registering handlers. While the 'first registered, first executed'
  156. behavior still holds true, the task which gets executed first will also get their handlers registered first. Handlers registered one after the other by a single task
  157. will still be dispatched in the order relative to each other, but if that task gets pre-empted in between registration by another task which also registers handlers; then during dispatch those
  158. handlers will also get executed in between.
  159. Event loop profiling
  160. --------------------
  161. A configuration option :ref:`CONFIG_ESP_EVENT_LOOP_PROFILING` can be enabled in order to activate statistics collection for all event loops created.
  162. The function :cpp:func:`esp_event_dump` can be used to output the collected statistics to a file stream. More details on the information included in the dump
  163. can be found in the :cpp:func:`esp_event_dump` API Reference.
  164. Application Example
  165. -------------------
  166. Examples on using the ``esp_event`` library can be found in :example:`system/esp_event`. The examples cover event declaration, loop creation, handler registration and unregistration and event posting.
  167. Other examples which also adopt esp_event library:
  168. * :example:`NMEA Parser <peripherals/uart/nmea0183_parser>`, which will decode the statements received from GPS.
  169. API Reference
  170. -------------
  171. .. include-build-file:: inc/esp_event.inc
  172. .. include-build-file:: inc/esp_event_base.inc
  173. Related Documents
  174. -----------------
  175. .. toctree::
  176. :maxdepth: 1