osi_event_hub.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_EVENT_HUB_H_
  13. #define _OSI_EVENT_HUB_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdarg.h>
  18. #include "kernel_config.h"
  19. #include "osi_api.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * opaque data structure for event dispatch
  25. */
  26. typedef struct osiEventDispatch osiEventDispatch_t;
  27. /**
  28. * opaque data structure for event hub
  29. */
  30. typedef struct osiEventHub osiEventHub_t;
  31. /**
  32. * function type of event handler
  33. */
  34. typedef void (*osiEventHandler_t)(const osiEvent_t *event);
  35. /**
  36. * function type of event callback
  37. *
  38. * Comparing to \p osiEventHandler_t, there is a callback
  39. * context pointer.
  40. */
  41. typedef void (*osiEventCallback_t)(void *ctx, const osiEvent_t *event);
  42. /**
  43. * @brief create an event hub
  44. *
  45. * To avoid dynamic memory complexity, static memory with specified
  46. * \p depth will be used.
  47. *
  48. * @param depth maximum registry count
  49. * @return
  50. * - event hub pointer
  51. * - NULL if out of memory
  52. */
  53. osiEventHub_t *osiEventHubCreate(size_t depth);
  54. /**
  55. * @brief delete the event hub
  56. *
  57. * When \p p is NULL, nothing will be done.
  58. *
  59. * @param p the event hub
  60. */
  61. void osiEventHubDelete(osiEventHub_t *p);
  62. /**
  63. * @brief register an event handler to event hub
  64. *
  65. * When the event ID is already registered, it will be replaced
  66. * silently.
  67. *
  68. * Though \p handler of NULL is not useful, it is permitted.
  69. *
  70. * @param p the event hub, must be valid
  71. * @param id registered event ID
  72. * @param handler event handler
  73. * @return
  74. * - true on success
  75. * - false on too many registrations
  76. */
  77. bool osiEventHubRegister(osiEventHub_t *p, uint32_t id, osiEventHandler_t handler);
  78. /**
  79. * @brief batch register events handler to event hub
  80. *
  81. * The variadic parameters are pairs of (id, handler), and ended
  82. * with id of zero.
  83. *
  84. * @param p the event hub, must be valid
  85. * @param id registered event ID
  86. * @return
  87. * - true on success
  88. * - false on too many registrations
  89. */
  90. bool osiEventHubBatchRegister(osiEventHub_t *p, uint32_t id, ...);
  91. /**
  92. * @brief batch register events handler to event hub
  93. *
  94. * It is the same as \p osiEventHubBatchRegister.
  95. *
  96. * @param p the event hub, must be valid
  97. * @param id registered event ID
  98. * @param args variadic parameters
  99. * @return
  100. * - true on success
  101. * - false on too many registrations
  102. */
  103. bool osiEventHubVBatchRegister(osiEventHub_t *p, uint32_t id, va_list args);
  104. /**
  105. * @brief dispatch an event
  106. *
  107. * Found the registered event handler, and invoke the handler.
  108. *
  109. * Handler of NULL is regarded as unregistered, and return false.
  110. *
  111. * @param p the event hub, must be valid
  112. * @param event event to be dispatched
  113. * @return
  114. * - true if the registered handler is invoked
  115. * - false if the event is not registered
  116. */
  117. bool osiEventHubRun(osiEventHub_t *p, const osiEvent_t *event);
  118. /**
  119. * @brief create an event hdispatch
  120. *
  121. * To avoid dynamic memory complexity, static memory with specified
  122. * \p depth will be used.
  123. *
  124. * @param depth maximum registry count
  125. * @return
  126. * - event dispatch pointer
  127. * - NULL if out of memory
  128. */
  129. osiEventDispatch_t *osiEventDispatchCreate(size_t depth);
  130. /**
  131. * @brief delete the event dispatch
  132. *
  133. * When \p p is NULL, nothing will be done.
  134. *
  135. * @param p the event dispatch
  136. */
  137. void osiEventDispatchDelete(osiEventDispatch_t *p);
  138. /**
  139. * @brief register an event handler to event hub
  140. *
  141. * When the event ID is already registered, it will return
  142. * false.
  143. *
  144. * @param p the event dispatch, must be valid
  145. * @param id registered event ID
  146. * @param cb event callback, can't be NULL
  147. * @param cb_ctx event callback context
  148. * @return
  149. * - true on success
  150. * - false on fail
  151. * - too many registrations
  152. * - event already registered
  153. * - invalid event callback
  154. */
  155. bool osiEventDispatchRegister(osiEventDispatch_t *p, uint32_t id, osiEventCallback_t cb, void *cb_ctx);
  156. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  157. /**
  158. * @brief unregister an event handler from event hub
  159. *
  160. * @param p the event dispatch, must be valid
  161. * @param id unregistered event ID
  162. * @return
  163. * - true on success
  164. * - false on fail
  165. */
  166. bool osiEventDispatchUnregister(osiEventDispatch_t *p, uint32_t id);
  167. #endif
  168. /**
  169. * @brief dispatch an event
  170. *
  171. * Found the registered event callback, and invoke the callback.
  172. *
  173. * @param p the event dispatch, must be valid
  174. * @param event event to be dispatched
  175. * @return
  176. * - true if the registered callback is invoked
  177. * - false if the event is not registered
  178. */
  179. bool osiEventDispatchRun(osiEventDispatch_t *p, const osiEvent_t *event);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif