Skip to content

Commit

Permalink
Merge branch 'feature/adds_esp_exit_macros' into 'master'
Browse files Browse the repository at this point in the history
feat(esp_common): Add ESP_RETURN_VOID_ON_ERROR and ESP_RETURN_VOID_ON_FALSE macros

Closes IDFGH-12532

See merge request espressif/esp-idf!30148
  • Loading branch information
KonstantinKondrashov committed Apr 11, 2024
2 parents 19700a5 + 69f6170 commit 0b5a6e3
Showing 1 changed file with 133 additions and 1 deletion.
134 changes: 133 additions & 1 deletion components/esp_common/include/esp_check.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -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'.
Expand Down

0 comments on commit 0b5a6e3

Please sign in to comment.