sdk_macros.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright (c) 2013 - 2019, Nordic Semiconductor ASA
  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. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA 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. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. *
  42. * @defgroup sdk_common_macros SDK Common Header
  43. * @ingroup app_common
  44. * @brief Macros for parameter checking and similar tasks
  45. * @{
  46. */
  47. #ifndef SDK_MACROS_H__
  48. #define SDK_MACROS_H__
  49. #include "nrf_assert.h"
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**@brief Macro for parameter checking.
  54. *
  55. * If @p _cond evaluates to true, does nothing. Otherwise,
  56. * if @p _module ## _PARAM_CHECK_DISABLED is @e not set (default), prints an error message
  57. * if @p _printfn is provided, and returns from the calling function context with code @p _err.
  58. * If @p _module ## _PARAM_CHECK_DISABLED is set, behaves like the ASSERT macro.
  59. *
  60. * Parameter checking implemented using this macro can be optionally turned off for release code.
  61. * Only disable runtime parameter checks if size if a major concern.
  62. *
  63. * @param _module The module name.
  64. * @param _cond The condition to be evaluated.
  65. * @param _err The error to be returned.
  66. * @param _printfn A printf-compatible function used to log the error.
  67. * Leave empty if no logging is needed.
  68. *
  69. * @hideinitializer
  70. */
  71. /*lint -esym(666, NRF_PARAM_CHECK*) : Expression with side effects passed to macro */
  72. #define NRF_PARAM_CHECK(_module, _cond, _err, _printfn) \
  73. do \
  74. { \
  75. if ((_cond)) \
  76. { \
  77. /* Do nothing. */ \
  78. } \
  79. else if (!(_module ## _PARAM_CHECK_DISABLED)) \
  80. { \
  81. _printfn("%s check failed in %s() with value 0x%x.", #_cond, __func__, _err); \
  82. return (_err); \
  83. } \
  84. else \
  85. { \
  86. ASSERT((_cond)); \
  87. } \
  88. } while (0);
  89. /**@brief Macro for verifying statement to be true. It will cause the exterior function to return
  90. * err_code if the statement is not true.
  91. *
  92. * @param[in] statement Statement to test.
  93. * @param[in] err_code Error value to return if test was invalid.
  94. *
  95. * @retval nothing, but will cause the exterior function to return @p err_code if @p statement
  96. * is false.
  97. */
  98. #define VERIFY_TRUE(statement, err_code) \
  99. do \
  100. { \
  101. if (!(statement)) \
  102. { \
  103. return err_code; \
  104. } \
  105. } while (0)
  106. /**@brief Macro for verifying statement to be true. It will cause the exterior function to return
  107. * if the statement is not true.
  108. *
  109. * @param[in] statement Statement to test.
  110. */
  111. #define VERIFY_TRUE_VOID(statement) VERIFY_TRUE((statement), )
  112. /**@brief Macro for verifying statement to be false. It will cause the exterior function to return
  113. * err_code if the statement is not false.
  114. *
  115. * @param[in] statement Statement to test.
  116. * @param[in] err_code Error value to return if test was invalid.
  117. *
  118. * @retval nothing, but will cause the exterior function to return @p err_code if @p statement
  119. * is true.
  120. */
  121. #define VERIFY_FALSE(statement, err_code) \
  122. do \
  123. { \
  124. if ((statement)) \
  125. { \
  126. return err_code; \
  127. } \
  128. } while (0)
  129. /**@brief Macro for verifying statement to be false. It will cause the exterior function to return
  130. * if the statement is not false.
  131. *
  132. * @param[in] statement Statement to test.
  133. */
  134. #define VERIFY_FALSE_VOID(statement) VERIFY_FALSE((statement), )
  135. /**@brief Macro for verifying that a function returned NRF_SUCCESS. It will cause the exterior
  136. * function to return error code of statement if it is not @ref NRF_SUCCESS.
  137. *
  138. * @param[in] statement Statement to check against NRF_SUCCESS.
  139. */
  140. #define VERIFY_SUCCESS(statement) \
  141. do \
  142. { \
  143. uint32_t _err_code = (uint32_t) (statement); \
  144. if (_err_code != NRF_SUCCESS) \
  145. { \
  146. return _err_code; \
  147. } \
  148. } while(0)
  149. /**@brief Macro for verifying that a function returned NRF_SUCCESS. It will cause the exterior
  150. * function to return if the err_code is not @ref NRF_SUCCESS.
  151. *
  152. * @param[in] err_code The error code to check.
  153. */
  154. #define VERIFY_SUCCESS_VOID(err_code) VERIFY_TRUE_VOID((err_code) == NRF_SUCCESS)
  155. /**@brief Macro for verifying that the module is initialized. It will cause the exterior function to
  156. * return @ref NRF_ERROR_INVALID_STATE if not.
  157. *
  158. * @note MODULE_INITIALIZED must be defined in each module using this macro. MODULE_INITIALIZED
  159. * should be true if the module is initialized, false if not.
  160. */
  161. #define VERIFY_MODULE_INITIALIZED() VERIFY_TRUE((MODULE_INITIALIZED), NRF_ERROR_INVALID_STATE)
  162. /**@brief Macro for verifying that the module is initialized. It will cause the exterior function to
  163. * return if not.
  164. *
  165. * @note MODULE_INITIALIZED must be defined in each module using this macro. MODULE_INITIALIZED
  166. * should be true if the module is initialized, false if not.
  167. */
  168. #define VERIFY_MODULE_INITIALIZED_VOID() VERIFY_TRUE_VOID((MODULE_INITIALIZED))
  169. /**@brief Macro for verifying that the module is initialized. It will cause the exterior function to
  170. * return if not.
  171. *
  172. * @param[in] param The variable to check if is NULL.
  173. */
  174. #define VERIFY_PARAM_NOT_NULL(param) VERIFY_FALSE(((param) == NULL), NRF_ERROR_NULL)
  175. /**@brief Macro for verifying that the module is initialized. It will cause the exterior function to
  176. * return if not.
  177. *
  178. * @param[in] param The variable to check if is NULL.
  179. */
  180. #define VERIFY_PARAM_NOT_NULL_VOID(param) VERIFY_FALSE_VOID(((param) == NULL))
  181. /** @} */
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185. #endif // SDK_MACROS_H__