VSF Documented
esp_check.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*
19 * Clean-room re-implementation of ESP-IDF public API "esp_check.h".
20 *
21 * Error-checking convenience macros. Each macro evaluates an esp_err_t
22 * expression or boolean condition and, on failure, either returns the
23 * error code, returns void, or jumps to a goto label — always logging
24 * the failure site with ESP_LOGE / ESP_EARLY_LOGE.
25 *
26 * Dependency chain: esp_err.h → esp_log.h → esp_compiler.h → this file.
27 *
28 * Baseline: ESP-IDF v5.1 public API.
29 */
30
31#ifndef __VSF_ESPIDF_ESP_CHECK_H__
32#define __VSF_ESPIDF_ESP_CHECK_H__
33
34#include "esp_err.h"
35#include "esp_log.h"
36#include "esp_compiler.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* ------------------------------------------------------------------ */
43/* Helper: ISR-safe log alias. VSF has no early-log distinction; */
44/* ESP_EARLY_LOGE maps to the same trace service as ESP_LOGE. */
45/* ------------------------------------------------------------------ */
46#ifndef ESP_EARLY_LOGE
47# define ESP_EARLY_LOGE(tag, format, ...) \
48 ESP_LOGE(tag, format, ##__VA_ARGS__)
49#endif
50
51/* ------------------------------------------------------------------ */
52/* ESP_RETURN_ON_ERROR(x, tag, fmt, ...) */
53/* If x != ESP_OK → log + return err_rc_. */
54/* ------------------------------------------------------------------ */
55#define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \
56 esp_err_t err_rc_ = (x); \
57 if (unlikely(err_rc_ != ESP_OK)) { \
58 ESP_LOGE(log_tag, "%s(%d): " format, \
59 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
60 return err_rc_; \
61 } \
62 } while(0)
63
64#define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \
65 esp_err_t err_rc_ = (x); \
66 if (unlikely(err_rc_ != ESP_OK)) { \
67 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
68 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
69 return err_rc_; \
70 } \
71 } while(0)
72
73/* ------------------------------------------------------------------ */
74/* ESP_RETURN_VOID_ON_ERROR(x, tag, fmt, ...) */
75/* If x != ESP_OK → log + return (void). */
76/* ------------------------------------------------------------------ */
77#define ESP_RETURN_VOID_ON_ERROR(x, log_tag, format, ...) do { \
78 esp_err_t err_rc_ = (x); \
79 if (unlikely(err_rc_ != ESP_OK)) { \
80 ESP_LOGE(log_tag, "%s(%d): " format, \
81 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
82 return; \
83 } \
84 } while(0)
85
86#define ESP_RETURN_VOID_ON_ERROR_ISR(x, log_tag, format, ...) do { \
87 esp_err_t err_rc_ = (x); \
88 if (unlikely(err_rc_ != ESP_OK)) { \
89 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
90 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
91 return; \
92 } \
93 } while(0)
94
95/* ------------------------------------------------------------------ */
96/* ESP_GOTO_ON_ERROR(x, goto_tag, tag, fmt, ...) */
97/* If x != ESP_OK → log, ret = err_rc_, goto goto_tag. */
98/* ------------------------------------------------------------------ */
99#define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \
100 esp_err_t err_rc_ = (x); \
101 if (unlikely(err_rc_ != ESP_OK)) { \
102 ESP_LOGE(log_tag, "%s(%d): " format, \
103 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
104 ret = err_rc_; \
105 goto goto_tag; \
106 } \
107 } while(0)
108
109#define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \
110 esp_err_t err_rc_ = (x); \
111 if (unlikely(err_rc_ != ESP_OK)) { \
112 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
113 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
114 ret = err_rc_; \
115 goto goto_tag; \
116 } \
117 } while(0)
118
119/* ------------------------------------------------------------------ */
120/* ESP_RETURN_ON_FALSE(cond, err_code, tag, fmt, ...) */
121/* If !cond → log + return err_code. */
122/* ------------------------------------------------------------------ */
123#define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \
124 if (unlikely(!(a))) { \
125 ESP_LOGE(log_tag, "%s(%d): " format, \
126 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
127 return err_code; \
128 } \
129 } while(0)
130
131#define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \
132 if (unlikely(!(a))) { \
133 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
134 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
135 return err_code; \
136 } \
137 } while(0)
138
139/* ------------------------------------------------------------------ */
140/* ESP_RETURN_VOID_ON_FALSE(cond, tag, fmt, ...) */
141/* If !cond → log + return (void). */
142/* ------------------------------------------------------------------ */
143#define ESP_RETURN_VOID_ON_FALSE(a, log_tag, format, ...) do { \
144 if (unlikely(!(a))) { \
145 ESP_LOGE(log_tag, "%s(%d): " format, \
146 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
147 return; \
148 } \
149 } while(0)
150
151#define ESP_RETURN_VOID_ON_FALSE_ISR(a, log_tag, format, ...) do { \
152 if (unlikely(!(a))) { \
153 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
154 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
155 return; \
156 } \
157 } while(0)
158
159/* ------------------------------------------------------------------ */
160/* ESP_GOTO_ON_FALSE(cond, err_code, goto_tag, tag, fmt, ...) */
161/* If !cond → log, ret = err_code, goto goto_tag. */
162/* ------------------------------------------------------------------ */
163#define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \
164 if (unlikely(!(a))) { \
165 ESP_LOGE(log_tag, "%s(%d): " format, \
166 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
167 ret = err_code; \
168 goto goto_tag; \
169 } \
170 } while(0)
171
172#define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \
173 if (unlikely(!(a))) { \
174 ESP_EARLY_LOGE(log_tag, "%s(%d): " format, \
175 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
176 ret = err_code; \
177 goto goto_tag; \
178 } \
179 } while(0)
180
181/* ------------------------------------------------------------------ */
182/* ESP_RETURN_ON_ERROR_CLEANUP(x, cleanup...) */
183/* If x != ESP_OK → run cleanup code, then return err_rc_. */
184/* ------------------------------------------------------------------ */
185#define ESP_RETURN_ON_ERROR_CLEANUP(x, ...) do { \
186 esp_err_t err_rc_ = (x); \
187 if (unlikely(err_rc_ != ESP_OK)) { \
188 __VA_ARGS__; \
189 return err_rc_; \
190 } \
191 } while(0)
192
193#ifdef __cplusplus
194}
195#endif
196
197#endif // __VSF_ESPIDF_ESP_CHECK_H__
Generated from commit: vsfteam/vsf@c3767bf