sys_events.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef SYS_EVENTS_H_INCLUDED
  43. #define SYS_EVENTS_H_INCLUDED
  44. #include <stddef.h>
  45. #include "sys_queue.h"
  46. /** @file
  47. * This file contains declarations of the Events API and necessary types. The Events feature is implemented
  48. * using the Queue functionality.
  49. *
  50. * @defgroup sys_events System events API
  51. * @ingroup sys_15_4
  52. * @{
  53. * @brief Module for declaring system events API.
  54. * @details The Events module defines some routines to subscribe/unsubscribe to/from system events. The events pool
  55. * can be extended by adding new events to the sys_event_id_t enumeration. The registered callbacks
  56. * can be called for an array of events. The callbacks can be called implicitly via posting the event by the
  57. * sys_event_post() routine.
  58. */
  59. /**@brief IDs of globally available events.
  60. *
  61. * @details Event IDs are system extension points that allow the user to implement
  62. * specific processing of predefined set of events, occurring in different modules.
  63. */
  64. typedef enum
  65. {
  66. SYS_EVENT_FALLING_ASLEEP, /**< Falling asleep event. */
  67. SYS_EVENT_WAKE_UP, /**< Waking up event. */
  68. SYS_EVENT_OUT_OF_MEMORY, /**< Out of memory event. */
  69. SYS_EVENT_MEMORY_FREED, /**< Memory was freed up event. */
  70. /** \note The list of system events can be extended during the implementation phase. */
  71. /* The following event IDs are used only for unit testing */
  72. TST_EVENT_0, /**< Test event #0. */
  73. TST_EVENT_1, /**< Test event #1. */
  74. TST_EVENT_2, /**< Test event #2. */
  75. #if (CONFIG_USE_SYS_TASK_NOTIFIER == 1)
  76. /** This event is posted when there are unhandled events available in
  77. * any of the schedulers.
  78. */
  79. SYS_EVENT_NEW_TASK,
  80. #endif
  81. SYS_EVENTS_AMOUNT
  82. } sys_event_id_t;
  83. /**@brief Prototype of user-implemented callback for processing an event.
  84. *
  85. * @details This callback is registered for the given event by a *_subscribe routine,
  86. * and is then called by the system events engine, when this event occurs.
  87. *
  88. * @param[in] p_data Pointer to the data, specific for this event.
  89. */
  90. typedef void (* sys_event_callback_t)(const void * p_data);
  91. /**@brief Event descriptor.
  92. *
  93. * @details This descriptor is used to subscribe/unsubscribe to/from the event.
  94. */
  95. typedef struct
  96. {
  97. /** Service field. */
  98. sys_queue_item_t queue_item;
  99. /** ID of the event to which this callback is to be subscribed. */
  100. sys_event_id_t event_id;
  101. /** Callback function which is to be called when this event occurs. */
  102. sys_event_callback_t callback;
  103. } sys_event_desc_t;
  104. /**@brief Function for initializing the global events infrastructure.
  105. */
  106. void sys_events_init(void);
  107. /**@brief Function for subscribing to a system event.
  108. *
  109. * @param[in] p_event_desc Pointer to the event descriptor.
  110. */
  111. void sys_event_subscribe(sys_event_desc_t * p_event_desc);
  112. /**@brief Function for unsubscribing from a system event event.
  113. *
  114. * @param[in] p_event_desc Pointer to the event descriptor.
  115. */
  116. void sys_event_unsubscribe(sys_event_desc_t * p_event_desc);
  117. /**@brief Function for subscribing to a group of events.
  118. *
  119. * @param[in] p_desc_array Pointer to the array of event descriptors.
  120. * @param[in] desc_amount Amount of event descriptors in the array.
  121. */
  122. void sys_events_array_subscribe(sys_event_desc_t * p_desc_array, size_t desc_amount);
  123. /**@brief Function for unsubscribing from the group of events.
  124. *
  125. *
  126. * @param[in] p_desc_array Pointer to the array of event descriptors.
  127. * @param[in] desc_amount Amount of the event descriptors in the array.
  128. */
  129. void sys_events_array_unsubscribe(sys_event_desc_t * p_desc_array, size_t desc_amount);
  130. /**@brief Function for posting an event.
  131. *
  132. * @details This function is used to notify all the subscribers of the given events via
  133. * their callbacks, when the given event occurs.
  134. *
  135. * @param[in] event_id ID of the event to be posted.
  136. * @param[in] p_data Pointer to be passed to the event handlers' callbacks.
  137. */
  138. void sys_event_post(sys_event_id_t event_id, const void * p_data);
  139. /** @} */
  140. #endif // SYS_EVENTS_H_INCLUDED