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 <stdlib.h>
13 #include "esp_compiler.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 typedef int esp_err_t;
20 
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 
25 #define ESP_ERR_NO_MEM              0x101   /*!< Out of memory */
26 #define ESP_ERR_INVALID_ARG         0x102   /*!< Invalid argument */
27 #define ESP_ERR_INVALID_STATE       0x103   /*!< Invalid state */
28 #define ESP_ERR_INVALID_SIZE        0x104   /*!< Invalid size */
29 #define ESP_ERR_NOT_FOUND           0x105   /*!< Requested resource not found */
30 #define ESP_ERR_NOT_SUPPORTED       0x106   /*!< Operation or feature not supported */
31 #define ESP_ERR_TIMEOUT             0x107   /*!< Operation timed out */
32 #define ESP_ERR_INVALID_RESPONSE    0x108   /*!< Received response was invalid */
33 #define ESP_ERR_INVALID_CRC         0x109   /*!< CRC or checksum was invalid */
34 #define ESP_ERR_INVALID_VERSION     0x10A   /*!< Version was invalid */
35 #define ESP_ERR_INVALID_MAC         0x10B   /*!< MAC address was invalid */
36 #define ESP_ERR_NOT_FINISHED        0x10C   /*!< There are items remained to retrieve */
37 #define ESP_ERR_NOT_ALLOWED         0x10D   /*!< Operation is not allowed */
38 #define ESP_ERR_ROC_IN_PROGRESS     0x10E   /*!< ROC Operation is in progress */
39 
40 
41 #define ESP_ERR_WIFI_BASE           0x3000  /*!< Starting number of WiFi error codes */
42 #define ESP_ERR_MESH_BASE           0x4000  /*!< Starting number of MESH error codes */
43 #define ESP_ERR_FLASH_BASE          0x6000  /*!< Starting number of flash error codes */
44 #define ESP_ERR_HW_CRYPTO_BASE      0xc000  /*!< Starting number of HW cryptography module error codes */
45 #define ESP_ERR_MEMPROT_BASE        0xd000  /*!< Starting number of Memory Protection API 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);
82 
83 void _esp_error_check_failed_without_abort(esp_err_t rc, const char *file, int line, const char *function, const char *expression);
84 
85 #ifndef __ASSERT_FUNC
86 /* This won't happen on IDF, which defines __ASSERT_FUNC in assert.h, but it does happen when building on the host which
87    uses /usr/include/assert.h or equivalent.
88 */
89 #ifdef __ASSERT_FUNCTION
90 #define __ASSERT_FUNC __ASSERT_FUNCTION /* used in glibc assert.h */
91 #else
92 #define __ASSERT_FUNC "??"
93 #endif
94 #endif
95 /** @endcond */
96 
97 /**
98  * Macro which can be used to check the error code,
99  * and terminate the program in case the code is not ESP_OK.
100  * Prints the error code, error location, and the failed statement to serial output.
101  *
102  * Disabled if assertions are disabled.
103  */
104 #ifdef NDEBUG
105 #define ESP_ERROR_CHECK(x) do {                                         \
106         esp_err_t err_rc_ = (x);                                        \
107         (void) sizeof(err_rc_);                                         \
108     } while(0)
109 #elif defined(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT)
110 #define ESP_ERROR_CHECK(x) do {                                         \
111         esp_err_t err_rc_ = (x);                                        \
112         if (unlikely(err_rc_ != ESP_OK)) {                              \
113             abort();                                                    \
114         }                                                               \
115     } while(0)
116 #else
117 #define ESP_ERROR_CHECK(x) do {                                         \
118         esp_err_t err_rc_ = (x);                                        \
119         if (unlikely(err_rc_ != ESP_OK)) {                              \
120             _esp_error_check_failed(err_rc_, __FILE__, __LINE__,        \
121                                     __ASSERT_FUNC, #x);                 \
122         }                                                               \
123     } while(0)
124 #endif
125 
126 /**
127  * Macro which can be used to check the error code. Prints the error code, error location, and the failed statement to
128  * serial output.
129  * In comparison with ESP_ERROR_CHECK(), this prints the same error message but isn't terminating the program.
130  */
131 #if defined NDEBUG || defined CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
132 #define ESP_ERROR_CHECK_WITHOUT_ABORT(x) ({                                         \
133         esp_err_t err_rc_ = (x);                                                    \
134         err_rc_;                                                                    \
135     })
136 #else
137 #define ESP_ERROR_CHECK_WITHOUT_ABORT(x) ({                                         \
138         esp_err_t err_rc_ = (x);                                                    \
139         if (unlikely(err_rc_ != ESP_OK)) {                                          \
140             _esp_error_check_failed_without_abort(err_rc_, __FILE__, __LINE__,      \
141                                                   __ASSERT_FUNC, #x);               \
142         }                                                                           \
143         err_rc_;                                                                    \
144     })
145 #endif //NDEBUG
146 
147 #ifdef __cplusplus
148 }
149 #endif
150