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