fsl_notifier.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_NOTIFIER_H_
  35. #define _FSL_NOTIFIER_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup notifier
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*!
  45. * @brief Notifier error codes.
  46. *
  47. * Used as return value of Notifier functions.
  48. */
  49. enum _notifier_status
  50. {
  51. kStatus_NOTIFIER_ErrorNotificationBefore =
  52. MAKE_STATUS(kStatusGroup_NOTIFIER, 0), /*!< An error occurs during send "BEFORE" notification. */
  53. kStatus_NOTIFIER_ErrorNotificationAfter =
  54. MAKE_STATUS(kStatusGroup_NOTIFIER, 1), /*!< An error occurs during send "AFTER" notification. */
  55. };
  56. /*!
  57. * @brief Notifier policies.
  58. *
  59. * Defines whether the user function execution is forced or not.
  60. * For kNOTIFIER_PolicyForcible, the user function is executed regardless of the callback results,
  61. * while kNOTIFIER_PolicyAgreement policy is used to exit NOTIFIER_SwitchConfig()
  62. * when any of the callbacks returns error code.
  63. * See also NOTIFIER_SwitchConfig() description.
  64. */
  65. typedef enum _notifier_policy
  66. {
  67. kNOTIFIER_PolicyAgreement, /*!< NOTIFIER_SwitchConfig() method is exited when any of the callbacks returns error
  68. code. */
  69. kNOTIFIER_PolicyForcible, /*!< The user function is executed regardless of the results. */
  70. } notifier_policy_t;
  71. /*! @brief Notification type. Used to notify registered callbacks */
  72. typedef enum _notifier_notification_type
  73. {
  74. kNOTIFIER_NotifyRecover = 0x00U, /*!< Notify IP to recover to previous work state. */
  75. kNOTIFIER_NotifyBefore = 0x01U, /*!< Notify IP that configuration setting is going to change. */
  76. kNOTIFIER_NotifyAfter = 0x02U, /*!< Notify IP that configuration setting has been changed. */
  77. } notifier_notification_type_t;
  78. /*!
  79. * @brief The callback type, which indicates kinds of notification the callback handles.
  80. *
  81. * Used in the callback configuration structure (notifier_callback_config_t)
  82. * to specify when the registered callback is called during configuration switch initiated by the
  83. * NOTIFIER_SwitchConfig().
  84. * Callback can be invoked in following situations.
  85. * - Before the configuration switch (Callback return value can affect NOTIFIER_SwitchConfig()
  86. * execution. See the NOTIFIER_SwitchConfig() and notifier_policy_t documentation).
  87. * - After an unsuccessful attempt to switch configuration
  88. * - After a successful configuration switch
  89. */
  90. typedef enum _notifier_callback_type
  91. {
  92. kNOTIFIER_CallbackBefore = 0x01U, /*!< Callback handles BEFORE notification. */
  93. kNOTIFIER_CallbackAfter = 0x02U, /*!< Callback handles AFTER notification. */
  94. kNOTIFIER_CallbackBeforeAfter = 0x03U, /*!< Callback handles BEFORE and AFTER notification. */
  95. } notifier_callback_type_t;
  96. /*! @brief Notifier user configuration type.
  97. *
  98. * Reference of the user defined configuration is stored in an array; the notifier switches between these configurations
  99. * based on this array.
  100. */
  101. typedef void notifier_user_config_t;
  102. /*! @brief Notifier user function prototype
  103. * Use this function to execute specific operations in configuration switch.
  104. * Before and after this function execution, different notification is sent to registered callbacks.
  105. * If this function returns any error code, NOTIFIER_SwitchConfig() exits.
  106. *
  107. * @param targetConfig target Configuration.
  108. * @param userData Refers to other specific data passed to user function.
  109. * @return An error code or kStatus_Success.
  110. */
  111. typedef status_t (*notifier_user_function_t)(notifier_user_config_t *targetConfig, void *userData);
  112. /*! @brief notification block passed to the registered callback function. */
  113. typedef struct _notifier_notification_block
  114. {
  115. notifier_user_config_t *targetConfig; /*!< Pointer to target configuration. */
  116. notifier_policy_t policy; /*!< Configure transition policy. */
  117. notifier_notification_type_t notifyType; /*!< Configure notification type. */
  118. } notifier_notification_block_t;
  119. /*!
  120. * @brief Callback prototype.
  121. *
  122. * Declaration of a callback. It is common for registered callbacks.
  123. * Reference to function of this type is part of the notifier_callback_config_t callback configuration structure.
  124. * Depending on callback type, function of this prototype is called (see NOTIFIER_SwitchConfig())
  125. * before configuration switch, after it or in both use cases to notify about
  126. * the switch progress (see notifier_callback_type_t). When called, the type of the notification
  127. * is passed as a parameter along with the reference to the target configuration structure (see notifier_notification_block_t)
  128. * and any data passed during the callback registration.
  129. * When notified before the configuration switch, depending on the configuration switch policy (see
  130. * notifier_policy_t), the callback may deny the execution of the user function by returning an error code different
  131. * than kStatus_Success (see NOTIFIER_SwitchConfig()).
  132. *
  133. * @param notify Notification block.
  134. * @param data Callback data. Refers to the data passed during callback registration. Intended to
  135. * pass any driver or application data such as internal state information.
  136. * @return An error code or kStatus_Success.
  137. */
  138. typedef status_t (*notifier_callback_t)(notifier_notification_block_t *notify, void *data);
  139. /*!
  140. * @brief Callback configuration structure.
  141. *
  142. * This structure holds the configuration of callbacks.
  143. * Callbacks of this type are expected to be statically allocated.
  144. * This structure contains the following application-defined data.
  145. * callback - pointer to the callback function
  146. * callbackType - specifies when the callback is called
  147. * callbackData - pointer to the data passed to the callback.
  148. */
  149. typedef struct _notifier_callback_config
  150. {
  151. notifier_callback_t callback; /*!< Pointer to the callback function. */
  152. notifier_callback_type_t callbackType; /*!< Callback type. */
  153. void *callbackData; /*!< Pointer to the data passed to the callback. */
  154. } notifier_callback_config_t;
  155. /*!
  156. * @brief Notifier handle structure.
  157. *
  158. * Notifier handle structure. Contains data necessary for the Notifier proper function.
  159. * Stores references to registered configurations, callbacks, information about their numbers,
  160. * user function, user data, and other internal data.
  161. * NOTIFIER_CreateHandle() must be called to initialize this handle.
  162. */
  163. typedef struct _notifier_handle
  164. {
  165. notifier_user_config_t **configsTable; /*!< Pointer to configure table. */
  166. uint8_t configsNumber; /*!< Number of configurations. */
  167. notifier_callback_config_t *callbacksTable; /*!< Pointer to callback table. */
  168. uint8_t callbacksNumber; /*!< Maximum number of callback configurations. */
  169. uint8_t errorCallbackIndex; /*!< Index of callback returns error. */
  170. uint8_t currentConfigIndex; /*!< Index of current configuration. */
  171. notifier_user_function_t userFunction; /*!< User function. */
  172. void *userData; /*!< User data passed to user function. */
  173. } notifier_handle_t;
  174. /*******************************************************************************
  175. * API
  176. ******************************************************************************/
  177. #if defined(__cplusplus)
  178. extern "C" {
  179. #endif
  180. /*!
  181. * @brief Creates a Notifier handle.
  182. *
  183. * @param notifierHandle A pointer to the notifier handle.
  184. * @param configs A pointer to an array with references to all configurations which is handled by the Notifier.
  185. * @param configsNumber Number of configurations. Size of the configuration array.
  186. * @param callbacks A pointer to an array of callback configurations.
  187. * If there are no callbacks to register during Notifier initialization, use NULL value.
  188. * @param callbacksNumber Number of registered callbacks. Size of the callbacks array.
  189. * @param userFunction User function.
  190. * @param userData User data passed to user function.
  191. * @return An error Code or kStatus_Success.
  192. */
  193. status_t NOTIFIER_CreateHandle(notifier_handle_t *notifierHandle,
  194. notifier_user_config_t **configs,
  195. uint8_t configsNumber,
  196. notifier_callback_config_t *callbacks,
  197. uint8_t callbacksNumber,
  198. notifier_user_function_t userFunction,
  199. void *userData);
  200. /*!
  201. * @brief Switches the configuration according to a pre-defined structure.
  202. *
  203. * This function sets the system to the target configuration. Before transition,
  204. * the Notifier sends notifications to all callbacks registered to the callback table.
  205. * Callbacks are invoked in the following order: All registered callbacks are notified
  206. * ordered by index in the callbacks array. The same order is used for before and after switch notifications.
  207. * The notifications before the configuration switch can be used to obtain confirmation about
  208. * the change from registered callbacks. If any registered callback denies the
  209. * configuration change, further execution of this function depends on the notifier policy: the
  210. * configuration change is either forced (kNOTIFIER_PolicyForcible) or exited (kNOTIFIER_PolicyAgreement).
  211. * When configuration change is forced, the result of the before switch notifications are ignored. If an
  212. * agreement is required, if any callback returns an error code, further notifications
  213. * before switch notifications are cancelled and all already notified callbacks are re-invoked.
  214. * The index of the callback which returned error code during pre-switch notifications is stored
  215. * (any error codes during callbacks re-invocation are ignored) and NOTIFIER_GetErrorCallback() can be used to get it.
  216. * Regardless of the policies, if any callback returns an error code, an error code indicating in which phase
  217. * the error occurred is returned when NOTIFIER_SwitchConfig() exits.
  218. * @param notifierHandle pointer to notifier handle
  219. * @param configIndex Index of the target configuration.
  220. * @param policy Transaction policy, kNOTIFIER_PolicyAgreement or kNOTIFIER_PolicyForcible.
  221. *
  222. * @return An error code or kStatus_Success.
  223. *
  224. */
  225. status_t NOTIFIER_SwitchConfig(notifier_handle_t *notifierHandle, uint8_t configIndex, notifier_policy_t policy);
  226. /*!
  227. * @brief This function returns the last failed notification callback.
  228. *
  229. * This function returns an index of the last callback that failed during the configuration switch while
  230. * the last NOTIFIER_SwitchConfig() was called. If the last NOTIFIER_SwitchConfig() call ended successfully
  231. * value equal to callbacks number is returned. The returned value represents an index in the array of
  232. * static call-backs.
  233. *
  234. * @param notifierHandle Pointer to the notifier handle
  235. * @return Callback Index of the last failed callback or value equal to callbacks count.
  236. */
  237. uint8_t NOTIFIER_GetErrorCallbackIndex(notifier_handle_t *notifierHandle);
  238. #if defined(__cplusplus)
  239. }
  240. #endif /* __cplusplus */
  241. /*! @}*/
  242. #endif /* _FSL_NOTIFIER_H_ */