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