diff --git a/components/esp_common/include/esp_check.h b/components/esp_common/include/esp_check.h index 1a8bc44480..7042f29d57 100644 --- a/components/esp_common/include/esp_check.h +++ b/components/esp_common/include/esp_check.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -35,6 +35,29 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns. + * This macro is used when the function returns void. + */ +#define ESP_RETURN_VOID_ON_ERROR(x, log_tag, format, ...) do { \ + (void)log_tag; \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_VOID_ON_ERROR() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_ERROR_ISR(x, log_tag, format, ...) do { \ + (void)log_tag; \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + return; \ + } \ + } while(0) + /** * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message, * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'. @@ -71,6 +94,27 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the condition. If the condition is not 'true', it prints the message + * and returns without a value. + */ +#define ESP_RETURN_VOID_ON_FALSE(a, log_tag, format, ...) do { \ + (void)log_tag; \ + if (unlikely(!(a))) { \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_FALSE_ISR(a, log_tag, format, ...) do { \ + (void)log_tag; \ + if (unlikely(!(a))) { \ + return; \ + } \ + } while(0) + /** * A version of ESP_RETURN_ON_FALSE() macro that can be called from ISR. */ @@ -137,6 +181,29 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns. + * This macro is used when the function returns void. + */ +#define ESP_RETURN_VOID_ON_ERROR(x, log_tag, format, ...) do { \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_VOID_ON_ERROR() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_ERROR_ISR(x, log_tag, format, ...) do { \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \ + return; \ + } \ + } while(0) + /** * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message, * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'. @@ -183,6 +250,27 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the condition. If the condition is not 'true', it prints the message + * and returns without a value. + */ +#define ESP_RETURN_VOID_ON_FALSE(a, err_code, log_tag, format, ...) do { \ + if (unlikely(!(a))) { \ + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_VOID_ON_FALSE() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_FALSE_ISR(a, log_tag, format, ...) do { \ + if (unlikely(!(a))) { \ + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__ __VA_OPT__(,) __VA_ARGS__); \ + return; \ + } \ + } while(0) + /** * Macro which can be used to check the condition. If the condition is not 'true', it prints the message, * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'. @@ -230,6 +318,29 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message and returns. + * This macro is used when the function returns void. + */ +#define ESP_RETURN_VOID_ON_ERROR(x, log_tag, format, ...) do { \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_VOID_ON_ERROR() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_ERROR_ISR(x, log_tag, format, ...) do { \ + esp_err_t err_rc_ = (x); \ + if (unlikely(err_rc_ != ESP_OK)) { \ + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + return; \ + } \ + } while(0) + /** * Macro which can be used to check the error code. If the code is not ESP_OK, it prints the message, * sets the local variable 'ret' to the code, and then exits by jumping to 'goto_tag'. @@ -276,6 +387,27 @@ extern "C" { } \ } while(0) +/** + * Macro which can be used to check the condition. If the condition is not 'true', it prints the message + * and returns without a value. + */ +#define ESP_RETURN_VOID_ON_FALSE(a, log_tag, format, ...) do { \ + if (unlikely(!(a))) { \ + ESP_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + return; \ + } \ + } while(0) + +/** + * A version of ESP_RETURN_VOID_ON_FALSE() macro that can be called from ISR. + */ +#define ESP_RETURN_VOID_ON_FALSE_ISR(a, log_tag, format, ...) do { \ + if (unlikely(!(a))) { \ + ESP_EARLY_LOGE(log_tag, "%s(%d): " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ + return; \ + } \ + } while(0) + /** * Macro which can be used to check the condition. If the condition is not 'true', it prints the message, * sets the local variable 'ret' to the supplied 'err_code', and then exits by jumping to 'goto_tag'.