esp_check.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns.
  21. */
  22. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  23. #define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \
  24. esp_err_t err_rc_ = (x); \
  25. if (unlikely(err_rc_ != ESP_OK)) { \
  26. return err_rc_; \
  27. } \
  28. } while(0)
  29. #else
  30. #define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \
  31. esp_err_t err_rc_ = (x); \
  32. if (unlikely(err_rc_ != ESP_OK)) { \
  33. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  34. return err_rc_; \
  35. } \
  36. } while(0)
  37. #endif
  38. /**
  39. * A version of ESP_RETURN_ON_ERROR() macro that can be called from ISR.
  40. */
  41. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  42. #define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
  43. esp_err_t err_rc_ = (x); \
  44. if (unlikely(err_rc_ != ESP_OK)) { \
  45. return err_rc_; \
  46. } \
  47. } while(0)
  48. #else
  49. #define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
  50. esp_err_t err_rc_ = (x); \
  51. if (unlikely(err_rc_ != ESP_OK)) { \
  52. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  53. return err_rc_; \
  54. } \
  55. } while(0)
  56. #endif
  57. /**
  58. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message,
  59. * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'.
  60. */
  61. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  62. #define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
  63. esp_err_t err_rc_ = (x); \
  64. if (unlikely(err_rc_ != ESP_OK)) { \
  65. ret = err_rc_; \
  66. goto goto_tag; \
  67. } \
  68. } while(0)
  69. #else
  70. #define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
  71. esp_err_t err_rc_ = (x); \
  72. if (unlikely(err_rc_ != ESP_OK)) { \
  73. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  74. ret = err_rc_; \
  75. goto goto_tag; \
  76. } \
  77. } while(0)
  78. #endif
  79. /**
  80. * A version of ESP_GOTO_ON_ERROR() macro that can be called from ISR.
  81. */
  82. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  83. #define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
  84. esp_err_t err_rc_ = (x); \
  85. if (unlikely(err_rc_ != ESP_OK)) { \
  86. ret = err_rc_; \
  87. goto goto_tag; \
  88. } \
  89. } while(0)
  90. #else
  91. #define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
  92. esp_err_t err_rc_ = (x); \
  93. if (unlikely(err_rc_ != ESP_OK)) { \
  94. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  95. ret = err_rc_; \
  96. goto goto_tag; \
  97. } \
  98. } while(0)
  99. #endif
  100. /**
  101. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message
  102. * and returns with the supplied 'err_code'.
  103. */
  104. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  105. #define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
  106. if (unlikely(!(a))) { \
  107. return err_code; \
  108. } \
  109. } while(0)
  110. #else
  111. #define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
  112. if (unlikely(!(a))) { \
  113. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  114. return err_code; \
  115. } \
  116. } while(0)
  117. #endif
  118. /**
  119. * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR.
  120. */
  121. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  122. #define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
  123. if (unlikely(!(a))) { \
  124. return err_code; \
  125. } \
  126. } while(0)
  127. #else
  128. #define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
  129. if (unlikely(!(a))) { \
  130. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  131. return err_code; \
  132. } \
  133. } while(0)
  134. #endif
  135. /**
  136. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message,
  137. * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'.
  138. */
  139. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  140. #define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
  141. if (unlikely(!(a))) { \
  142. ret = err_code; \
  143. goto goto_tag; \
  144. } \
  145. } while (0)
  146. #else
  147. #define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
  148. if (unlikely(!(a))) { \
  149. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  150. ret = err_code; \
  151. goto goto_tag; \
  152. } \
  153. } while (0)
  154. #endif
  155. /**
  156. * A version of ESP_GOTO_ON_FALSE() macro that can be called from ISR.
  157. */
  158. #if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT)
  159. #define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
  160. if (unlikely(!(a))) { \
  161. ret = err_code; \
  162. goto goto_tag; \
  163. } \
  164. } while (0)
  165. #else
  166. #define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
  167. if (unlikely(!(a))) { \
  168. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  169. ret = err_code; \
  170. goto goto_tag; \
  171. } \
  172. } while (0)
  173. #endif
  174. #ifdef __cplusplus
  175. }
  176. #endif