diff --git a/include/rc522/guards.h b/include/rc522/guards.h deleted file mode 100644 index e0c824d9..00000000 --- a/include/rc522/guards.h +++ /dev/null @@ -1,126 +0,0 @@ -#pragma once - -#include - -/** - * @brief Label for success guard gate in macros. - * Used as a goto label to handle successful execution paths in macros. - */ -#define SUCCESS_GUARD_GATE __success_guard_gate - -/** - * @brief Label for error guard gate in macros. - * Used as a goto label to handle error paths in macros. - */ -#define ERROR_GUARD_GATE __error_guard_gate - -/** - * @brief Label for exit guard gate in macros. - * Used as a goto label to mark the end of execution paths in macros. - */ -#define EXIT_GUARD_GATE __exit_guard_gate - -/** - * @brief Macro to create jump points for success and error handling. - * This macro defines labels for success and error handling, and a common exit point. - * @param on_error Code to execute on error. - * @param on_success Code to execute on success. - */ -#define JMP_GUARD_GATES(on_error, on_success) \ - goto SUCCESS_GUARD_GATE; \ - ERROR_GUARD_GATE: { \ - on_error; \ - goto EXIT_GUARD_GATE; \ - }; \ - SUCCESS_GUARD_GATE: { \ - on_success; \ - goto EXIT_GUARD_GATE; \ - }; \ - EXIT_GUARD_GATE: { \ - }; - -/** - * @brief Macro guard for jumping to an error handler on ESP error. - * Checks the result of EXP and if it's not ESP_OK, jumps to ERROR_GUARD_GATE. - * @param EXP Expression that returns an esp_err_t value. - */ -#define ESP_ERR_JMP_GUARD(EXP) \ - if ((err = (EXP)) != ESP_OK) { \ - goto ERROR_GUARD_GATE; \ - } - -/** - * @brief Macro for logging an error message and jumping to an error handler on ESP error. - * If EXP is not ESP_OK, logs the provided message and jumps to ERROR_GUARD_GATE. - * @param EXP Expression that returns an esp_err_t value. - * @param message The message to log on error. - */ -#define ESP_ERR_LOG_AND_JMP_GUARD(EXP, message) \ - if ((err = (EXP)) != ESP_OK) { \ - ESP_LOGE(TAG, message); \ - goto ERROR_GUARD_GATE; \ - } - -/** - * @brief Macro for conditionally logging an error message and jumping to an error handler. - * If EXP evaluates to true, logs the provided message and jumps to ERROR_GUARD_GATE. - * @param EXP Boolean expression to evaluate. - * @param message The message to log if EXP is true. - */ -#define CONDITION_LOG_AND_JMP_GUARD(EXP, message) \ - if (EXP) { \ - ESP_LOGE(TAG, message); \ - goto ERROR_GUARD_GATE; \ - } - -/** - * @brief Macro guard for memory allocation with jump to error handling. - * This macro checks if a memory allocation expression (EXP) fails (i.e., returns NULL). - * If the allocation fails, it jumps to the ERROR_GUARD_GATE label. - * This is typically used in functions where error handling is centralized - * at a specific label (ERROR_GUARD_GATE) for cleaner and more manageable code. - * It helps in avoiding deep nesting of error checks after each allocation. - * @param EXP Memory allocation expression (e.g., a call to malloc). - */ -#define ALLOC_JMP_GUARD(EXP) \ - if ((EXP) == NULL) { \ - err = ESP_ERR_NO_MEM; \ - goto ERROR_GUARD_GATE; \ - } - -/** - * @brief Macro guard for memory allocation functions. - * This macro ensures that if a memory allocation expression fails (returns NULL), - * the function in which this macro is used will immediately return with ESP_ERR_NO_MEM. - * It's useful for handling memory allocation failures gracefully in functions. - * @param EXP Memory allocation expression (e.g., a call to malloc). - * @return Exits the caller function with ESP_ERR_NO_MEM if EXP evaluates to NULL. - */ -#define ALLOC_RET_GUARD(EXP) \ - if ((EXP) == NULL) { \ - return ESP_ERR_NO_MEM; \ - } - -/** - * @brief Macro guard for ESP error handling. - * This macro checks if the result of EXP is not ESP_OK and, if so, returns the error code. - * @param EXP Expression that returns an esp_err_t value. - * @return Returns the error code if EXP is not ESP_OK. - */ -#define ESP_ERR_RET_GUARD(EXP) \ - if ((err = (EXP)) != ESP_OK) { \ - return err; \ - } - -/** - * @brief Macro for safely freeing memory. - * This macro checks if the pointer is not NULL before calling the free function. - * After freeing the memory, it sets the pointer to NULL to prevent dangling pointer issues. - * This helps in avoiding double free errors and improves the safety of memory management. - * @param ptr Pointer to the memory to be freed. - */ -#define FREE(ptr) \ - if (ptr) { \ - free(ptr); \ - ptr = NULL; \ - } diff --git a/src/rc522.c b/src/rc522.c index 71b5fc01..5ccb2db6 100644 --- a/src/rc522.c +++ b/src/rc522.c @@ -6,7 +6,6 @@ #include #include "rc522.h" -#include "rc522/guards.h" #include "rc522/registers.h" static const char *TAG = "RC522"; @@ -31,6 +30,12 @@ struct rc522 bool tag_was_present_last_time; }; +#define FREE(ptr) \ + if ((ptr) != NULL) { \ + free(ptr); \ + (ptr) = NULL; \ + } + static void rc522_task(void *arg); static esp_err_t rc522_write_n(rc522_handle_t rc522, uint8_t addr, uint8_t n, uint8_t *data)