esp_err.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "esp_compiler.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef int esp_err_t;
  15. /* Definitions for error constants. */
  16. #define ESP_OK 0 /*!< esp_err_t value indicating success (no error) */
  17. #define ESP_FAIL -1 /*!< Generic esp_err_t code indicating failure */
  18. #define ESP_ERR_NO_MEM 0x101 /*!< Out of memory */
  19. #define ESP_ERR_INVALID_ARG 0x102 /*!< Invalid argument */
  20. #define ESP_ERR_INVALID_STATE 0x103 /*!< Invalid state */
  21. #define ESP_ERR_INVALID_SIZE 0x104 /*!< Invalid size */
  22. #define ESP_ERR_NOT_FOUND 0x105 /*!< Requested resource not found */
  23. #define ESP_ERR_NOT_SUPPORTED 0x106 /*!< Operation or feature not supported */
  24. #define ESP_ERR_TIMEOUT 0x107 /*!< Operation timed out */
  25. #define ESP_ERR_INVALID_RESPONSE 0x108 /*!< Received response was invalid */
  26. #define ESP_ERR_INVALID_CRC 0x109 /*!< CRC or checksum was invalid */
  27. #define ESP_ERR_INVALID_VERSION 0x10A /*!< Version was invalid */
  28. #define ESP_ERR_INVALID_MAC 0x10B /*!< MAC address was invalid */
  29. #define ESP_ERR_NOT_FINISHED 0x10C /*!< There are items remained to retrieve */
  30. #define ESP_ERR_WIFI_BASE 0x3000 /*!< Starting number of WiFi error codes */
  31. #define ESP_ERR_MESH_BASE 0x4000 /*!< Starting number of MESH error codes */
  32. #define ESP_ERR_FLASH_BASE 0x6000 /*!< Starting number of flash error codes */
  33. #define ESP_ERR_HW_CRYPTO_BASE 0xc000 /*!< Starting number of HW cryptography module error codes */
  34. #define ESP_ERR_MEMPROT_BASE 0xd000 /*!< Starting number of Memory Protection API error codes */
  35. /**
  36. * @brief Returns string for esp_err_t error codes
  37. *
  38. * This function finds the error code in a pre-generated lookup-table and
  39. * returns its string representation.
  40. *
  41. * The function is generated by the Python script
  42. * tools/gen_esp_err_to_name.py which should be run each time an esp_err_t
  43. * error is modified, created or removed from the IDF project.
  44. *
  45. * @param code esp_err_t error code
  46. * @return string error message
  47. */
  48. const char *esp_err_to_name(esp_err_t code);
  49. /**
  50. * @brief Returns string for esp_err_t and system error codes
  51. *
  52. * This function finds the error code in a pre-generated lookup-table of
  53. * esp_err_t errors and returns its string representation. If the error code
  54. * is not found then it is attempted to be found among system errors.
  55. *
  56. * The function is generated by the Python script
  57. * tools/gen_esp_err_to_name.py which should be run each time an esp_err_t
  58. * error is modified, created or removed from the IDF project.
  59. *
  60. * @param code esp_err_t error code
  61. * @param[out] buf buffer where the error message should be written
  62. * @param buflen Size of buffer buf. At most buflen bytes are written into the buf buffer (including the terminating null byte).
  63. * @return buf containing the string error message
  64. */
  65. const char *esp_err_to_name_r(esp_err_t code, char *buf, size_t buflen);
  66. /** @cond */
  67. void _esp_error_check_failed(esp_err_t rc, const char *file, int line, const char *function, const char *expression) __attribute__((noreturn));
  68. /** @cond */
  69. void _esp_error_check_failed_without_abort(esp_err_t rc, const char *file, int line, const char *function, const char *expression);
  70. #ifndef __ASSERT_FUNC
  71. /* This won't happen on IDF, which defines __ASSERT_FUNC in assert.h, but it does happen when building on the host which
  72. uses /usr/include/assert.h or equivalent.
  73. */
  74. #ifdef __ASSERT_FUNCTION
  75. #define __ASSERT_FUNC __ASSERT_FUNCTION /* used in glibc assert.h */
  76. #else
  77. #define __ASSERT_FUNC "??"
  78. #endif
  79. #endif
  80. /** @endcond */
  81. /**
  82. * Macro which can be used to check the error code,
  83. * and terminate the program in case the code is not ESP_OK.
  84. * Prints the error code, error location, and the failed statement to serial output.
  85. *
  86. * Disabled if assertions are disabled.
  87. */
  88. #ifdef NDEBUG
  89. #define ESP_ERROR_CHECK(x) do { \
  90. esp_err_t err_rc_ = (x); \
  91. (void) sizeof(err_rc_); \
  92. } while(0)
  93. #elif defined(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT)
  94. #define ESP_ERROR_CHECK(x) do { \
  95. esp_err_t err_rc_ = (x); \
  96. if (unlikely(err_rc_ != ESP_OK)) { \
  97. abort(); \
  98. } \
  99. } while(0)
  100. #else
  101. #define ESP_ERROR_CHECK(x) do { \
  102. esp_err_t err_rc_ = (x); \
  103. if (unlikely(err_rc_ != ESP_OK)) { \
  104. _esp_error_check_failed(err_rc_, __FILE__, __LINE__, \
  105. __ASSERT_FUNC, #x); \
  106. } \
  107. } while(0)
  108. #endif
  109. /**
  110. * Macro which can be used to check the error code. Prints the error code, error location, and the failed statement to
  111. * serial output.
  112. * In comparison with ESP_ERROR_CHECK(), this prints the same error message but isn't terminating the program.
  113. */
  114. #if defined NDEBUG || defined CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  115. #define ESP_ERROR_CHECK_WITHOUT_ABORT(x) ({ \
  116. esp_err_t err_rc_ = (x); \
  117. err_rc_; \
  118. })
  119. #else
  120. #define ESP_ERROR_CHECK_WITHOUT_ABORT(x) ({ \
  121. esp_err_t err_rc_ = (x); \
  122. if (unlikely(err_rc_ != ESP_OK)) { \
  123. _esp_error_check_failed_without_abort(err_rc_, __FILE__, __LINE__, \
  124. __ASSERT_FUNC, #x); \
  125. } \
  126. err_rc_; \
  127. })
  128. #endif //NDEBUG
  129. #ifdef __cplusplus
  130. }
  131. #endif