esp_check.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /**
  30. * A version of ESP_RETURN_ON_ERROR() macro that can be called from ISR.
  31. */
  32. #define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
  33. esp_err_t err_rc_ = (x); \
  34. if (unlikely(err_rc_ != ESP_OK)) { \
  35. return err_rc_; \
  36. } \
  37. } while(0)
  38. /**
  39. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message,
  40. * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'.
  41. */
  42. #define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
  43. esp_err_t err_rc_ = (x); \
  44. if (unlikely(err_rc_ != ESP_OK)) { \
  45. ret = err_rc_; \
  46. goto goto_tag; \
  47. } \
  48. } while(0)
  49. /**
  50. * A version of ESP_GOTO_ON_ERROR() macro that can be called from ISR.
  51. */
  52. #define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
  53. esp_err_t err_rc_ = (x); \
  54. if (unlikely(err_rc_ != ESP_OK)) { \
  55. ret = err_rc_; \
  56. goto goto_tag; \
  57. } \
  58. } while(0)
  59. /**
  60. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message
  61. * and returns with the supplied 'err_code'.
  62. */
  63. #define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
  64. if (unlikely(!(a))) { \
  65. return err_code; \
  66. } \
  67. } while(0)
  68. /**
  69. * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR.
  70. */
  71. #define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
  72. if (unlikely(!(a))) { \
  73. return err_code; \
  74. } \
  75. } while(0)
  76. /**
  77. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message,
  78. * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'.
  79. */
  80. #define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
  81. if (unlikely(!(a))) { \
  82. ret = err_code; \
  83. goto goto_tag; \
  84. } \
  85. } while (0)
  86. /**
  87. * A version of ESP_GOTO_ON_FALSE() macro that can be called from ISR.
  88. */
  89. #define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
  90. if (unlikely(!(a))) { \
  91. ret = err_code; \
  92. goto goto_tag; \
  93. } \
  94. } while (0)
  95. #else // !CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT
  96. /**
  97. * In the future, we want to switch to C++20. We also want to become compatible with clang.
  98. * Hence, we provide two versions of the following macros. The first one is using the GNU extension \#\#__VA_ARGS__.
  99. * The second one is using the C++20 feature __VA_OPT__(,). This allows users to compile their code with
  100. * standard C++20 enabled instead of the GNU extension. Below C++20, we haven't found any good alternative to
  101. * using \#\#__VA_ARGS__.
  102. */
  103. #if defined(__cplusplus) && (__cplusplus > 201703L)
  104. /**
  105. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns.
  106. */
  107. #define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \
  108. esp_err_t err_rc_ = (x); \
  109. if (unlikely(err_rc_ != ESP_OK)) { \
  110. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  111. return err_rc_; \
  112. } \
  113. } while(0)
  114. /**
  115. * A version of ESP_RETURN_ON_ERROR() macro that can be called from ISR.
  116. */
  117. #define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
  118. esp_err_t err_rc_ = (x); \
  119. if (unlikely(err_rc_ != ESP_OK)) { \
  120. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  121. return err_rc_; \
  122. } \
  123. } while(0)
  124. /**
  125. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message,
  126. * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'.
  127. */
  128. #define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
  129. esp_err_t err_rc_ = (x); \
  130. if (unlikely(err_rc_ != ESP_OK)) { \
  131. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  132. ret = err_rc_; \
  133. goto goto_tag; \
  134. } \
  135. } while(0)
  136. /**
  137. * A version of ESP_GOTO_ON_ERROR() macro that can be called from ISR.
  138. */
  139. #define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
  140. esp_err_t err_rc_ = (x); \
  141. if (unlikely(err_rc_ != ESP_OK)) { \
  142. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  143. ret = err_rc_; \
  144. goto goto_tag; \
  145. } \
  146. } while(0)
  147. /**
  148. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message
  149. * and returns with the supplied 'err_code'.
  150. */
  151. #define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
  152. if (unlikely(!(a))) { \
  153. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  154. return err_code; \
  155. } \
  156. } while(0)
  157. /**
  158. * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR.
  159. */
  160. #define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
  161. if (unlikely(!(a))) { \
  162. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  163. return err_code; \
  164. } \
  165. } while(0)
  166. /**
  167. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message,
  168. * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'.
  169. */
  170. #define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
  171. if (unlikely(!(a))) { \
  172. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  173. ret = err_code; \
  174. goto goto_tag; \
  175. } \
  176. } while (0)
  177. /**
  178. * A version of ESP_GOTO_ON_FALSE() macro that can be called from ISR.
  179. */
  180. #define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
  181. if (unlikely(!(a))) { \
  182. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \
  183. ret = err_code; \
  184. goto goto_tag; \
  185. } \
  186. } while (0)
  187. #else // !(defined(__cplusplus) && (__cplusplus > 201703L))
  188. /**
  189. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns.
  190. */
  191. #define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \
  192. esp_err_t err_rc_ = (x); \
  193. if (unlikely(err_rc_ != ESP_OK)) { \
  194. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  195. return err_rc_; \
  196. } \
  197. } while(0)
  198. /**
  199. * A version of ESP_RETURN_ON_ERROR() macro that can be called from ISR.
  200. */
  201. #define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
  202. esp_err_t err_rc_ = (x); \
  203. if (unlikely(err_rc_ != ESP_OK)) { \
  204. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  205. return err_rc_; \
  206. } \
  207. } while(0)
  208. /**
  209. * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message,
  210. * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'.
  211. */
  212. #define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
  213. esp_err_t err_rc_ = (x); \
  214. if (unlikely(err_rc_ != ESP_OK)) { \
  215. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  216. ret = err_rc_; \
  217. goto goto_tag; \
  218. } \
  219. } while(0)
  220. /**
  221. * A version of ESP_GOTO_ON_ERROR() macro that can be called from ISR.
  222. */
  223. #define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
  224. esp_err_t err_rc_ = (x); \
  225. if (unlikely(err_rc_ != ESP_OK)) { \
  226. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  227. ret = err_rc_; \
  228. goto goto_tag; \
  229. } \
  230. } while(0)
  231. /**
  232. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message
  233. * and returns with the supplied 'err_code'.
  234. */
  235. #define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
  236. if (unlikely(!(a))) { \
  237. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  238. return err_code; \
  239. } \
  240. } while(0)
  241. /**
  242. * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR.
  243. */
  244. #define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
  245. if (unlikely(!(a))) { \
  246. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  247. return err_code; \
  248. } \
  249. } while(0)
  250. /**
  251. * Macro which can be used to check the condition. If the condition is not 'true', it prints the message,
  252. * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'.
  253. */
  254. #define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
  255. if (unlikely(!(a))) { \
  256. ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  257. ret = err_code; \
  258. goto goto_tag; \
  259. } \
  260. } while (0)
  261. /**
  262. * A version of ESP_GOTO_ON_FALSE() macro that can be called from ISR.
  263. */
  264. #define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
  265. if (unlikely(!(a))) { \
  266. ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  267. ret = err_code; \
  268. goto goto_tag; \
  269. } \
  270. } while (0)
  271. #endif // !(defined(__cplusplus) && (__cplusplus > 201703L))
  272. #endif // !CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT
  273. #ifdef __cplusplus
  274. }
  275. #endif