|
| 1 | +// Copyright 2021 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 "esp_err.h" |
| 17 | +#include "esp_log.h" |
| 18 | + |
| 19 | +#ifdef __cplusplus |
| 20 | +extern "C" { |
| 21 | +#endif |
| 22 | + |
| 23 | +/** |
| 24 | + * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns. |
| 25 | + */ |
| 26 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 27 | +#define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \ |
| 28 | + esp_err_t err_rc_ = (x); \ |
| 29 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 30 | + return err_rc_; \ |
| 31 | + } \ |
| 32 | + } while(0) |
| 33 | +#else |
| 34 | +#define ESP_RETURN_ON_ERROR(x, log_tag, format, ...) do { \ |
| 35 | + esp_err_t err_rc_ = (x); \ |
| 36 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 37 | + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 38 | + return err_rc_; \ |
| 39 | + } \ |
| 40 | + } while(0) |
| 41 | +#endif |
| 42 | + |
| 43 | +/** |
| 44 | + * A version of ESP_RETURN_ON_ERROR() macro that can be called from ISR. |
| 45 | + */ |
| 46 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 47 | +#define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \ |
| 48 | + esp_err_t err_rc_ = (x); \ |
| 49 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 50 | + return err_rc_; \ |
| 51 | + } \ |
| 52 | + } while(0) |
| 53 | +#else |
| 54 | +#define ESP_RETURN_ON_ERROR_ISR(x, log_tag, format, ...) do { \ |
| 55 | + esp_err_t err_rc_ = (x); \ |
| 56 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 57 | + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 58 | + return err_rc_; \ |
| 59 | + } \ |
| 60 | + } while(0) |
| 61 | +#endif |
| 62 | + |
| 63 | +/** |
| 64 | + * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message, |
| 65 | + * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'. |
| 66 | + */ |
| 67 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 68 | +#define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \ |
| 69 | + esp_err_t err_rc_ = (x); \ |
| 70 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 71 | + ret = err_rc_; \ |
| 72 | + goto goto_tag; \ |
| 73 | + } \ |
| 74 | + } while(0) |
| 75 | +#else |
| 76 | +#define ESP_GOTO_ON_ERROR(x, goto_tag, log_tag, format, ...) do { \ |
| 77 | + esp_err_t err_rc_ = (x); \ |
| 78 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 79 | + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 80 | + ret = err_rc_; \ |
| 81 | + goto goto_tag; \ |
| 82 | + } \ |
| 83 | + } while(0) |
| 84 | +#endif |
| 85 | + |
| 86 | +/** |
| 87 | + * A version of ESP_GOTO_ON_ERROR() macro that can be called from ISR. |
| 88 | + */ |
| 89 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 90 | +#define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \ |
| 91 | + esp_err_t err_rc_ = (x); \ |
| 92 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 93 | + ret = err_rc_; \ |
| 94 | + goto goto_tag; \ |
| 95 | + } \ |
| 96 | + } while(0) |
| 97 | +#else |
| 98 | +#define ESP_GOTO_ON_ERROR_ISR(x, goto_tag, log_tag, format, ...) do { \ |
| 99 | + esp_err_t err_rc_ = (x); \ |
| 100 | + if (unlikely(err_rc_ != ESP_OK)) { \ |
| 101 | + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 102 | + ret = err_rc_; \ |
| 103 | + goto goto_tag; \ |
| 104 | + } \ |
| 105 | + } while(0) |
| 106 | +#endif |
| 107 | + |
| 108 | +/** |
| 109 | + * Macro which can be used to check the condition. If the condition is not 'true', it prints the message |
| 110 | + * and returns with the supplied 'err_code'. |
| 111 | + */ |
| 112 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 113 | +#define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \ |
| 114 | + if (unlikely(!(a))) { \ |
| 115 | + return err_code; \ |
| 116 | + } \ |
| 117 | + } while(0) |
| 118 | +#else |
| 119 | +#define ESP_RETURN_ON_FALSE(a, err_code, log_tag, format, ...) do { \ |
| 120 | + if (unlikely(!(a))) { \ |
| 121 | + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 122 | + return err_code; \ |
| 123 | + } \ |
| 124 | + } while(0) |
| 125 | +#endif |
| 126 | + |
| 127 | +/** |
| 128 | + * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR. |
| 129 | + */ |
| 130 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 131 | +#define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \ |
| 132 | + if (unlikely(!(a))) { \ |
| 133 | + return err_code; \ |
| 134 | + } \ |
| 135 | + } while(0) |
| 136 | +#else |
| 137 | +#define ESP_RETURN_ON_FALSE_ISR(a, err_code, log_tag, format, ...) do { \ |
| 138 | + if (unlikely(!(a))) { \ |
| 139 | + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 140 | + return err_code; \ |
| 141 | + } \ |
| 142 | + } while(0) |
| 143 | +#endif |
| 144 | + |
| 145 | +/** |
| 146 | + * Macro which can be used to check the condition. If the condition is not 'true', it prints the message, |
| 147 | + * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'. |
| 148 | + */ |
| 149 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 150 | +#define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \ |
| 151 | + if (unlikely(!(a))) { \ |
| 152 | + ret = err_code; \ |
| 153 | + goto goto_tag; \ |
| 154 | + } \ |
| 155 | + } while (0) |
| 156 | +#else |
| 157 | +#define ESP_GOTO_ON_FALSE(a, err_code, goto_tag, log_tag, format, ...) do { \ |
| 158 | + if (unlikely(!(a))) { \ |
| 159 | + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 160 | + ret = err_code; \ |
| 161 | + goto goto_tag; \ |
| 162 | + } \ |
| 163 | + } while (0) |
| 164 | +#endif |
| 165 | + |
| 166 | +/** |
| 167 | + * A version of ESP_GOTO_ON_FALSE() macro that can be called from ISR. |
| 168 | + */ |
| 169 | +#if defined(CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT) |
| 170 | +#define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \ |
| 171 | + if (unlikely(!(a))) { \ |
| 172 | + ret = err_code; \ |
| 173 | + goto goto_tag; \ |
| 174 | + } \ |
| 175 | + } while (0) |
| 176 | +#else |
| 177 | +#define ESP_GOTO_ON_FALSE_ISR(a, err_code, goto_tag, log_tag, format, ...) do { \ |
| 178 | + if (unlikely(!(a))) { \ |
| 179 | + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 180 | + ret = err_code; \ |
| 181 | + goto goto_tag; \ |
| 182 | + } \ |
| 183 | + } while (0) |
| 184 | +#endif |
| 185 | + |
| 186 | +#ifdef __cplusplus |
| 187 | +} |
| 188 | +#endif |
0 commit comments