esp_err.h 6.7 KB

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