From f9bcc9ddacb714027c4db590567e4a10cbab1e26 Mon Sep 17 00:00:00 2001 From: TheSomeMan Date: Tue, 11 Apr 2023 15:40:04 +0700 Subject: [PATCH] Add API wrap_esp_err_to_name_r --- CMakeLists.txt | 4 +++ include/wrap_esp_err_to_name_r.h | 24 +++++++++++++++++ src/wrap_esp_err_to_name_r.c | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 include/wrap_esp_err_to_name_r.h create mode 100644 src/wrap_esp_err_to_name_r.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d1efa5a..9c8b133 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(RUUVI_ESP_WRAPPERS_SRC include/os_wrapper_types.h include/time_units.h include/str_buf.h + include/wrap_esp_err_to_name_r.h src/log_dump.c src/mac_addr.c src/os_mkgmtime.c @@ -34,6 +35,7 @@ set(RUUVI_ESP_WRAPPERS_SRC src/os_timer.c src/os_timer_sig.c src/str_buf.c + src/wrap_esp_err_to_name_r.c ) set(RUUVI_ESP_WRAPPERS_INC @@ -47,6 +49,8 @@ if(${ESP_PLATFORM}) ${RUUVI_ESP_WRAPPERS_SRC} INCLUDE_DIRS ${RUUVI_ESP_WRAPPERS_INC} + PRIV_REQUIRES + esp-tls ) target_compile_options(__idf_ruuvi.esp_wrappers.c PRIVATE -Wall -Werror -Wextra -Wno-error=nonnull-compare) diff --git a/include/wrap_esp_err_to_name_r.h b/include/wrap_esp_err_to_name_r.h new file mode 100644 index 0000000..3c75b3a --- /dev/null +++ b/include/wrap_esp_err_to_name_r.h @@ -0,0 +1,24 @@ +/** + * @file wrap_esp_err_to_name_r.h + * @author TheSomeMan + * @date 2023-04-09 + * @copyright Ruuvi Innovations Ltd, license BSD-3-Clause. + */ + +#ifndef RUUVI_WRAP_ESP_ERR_TO_NAME_R_H +#define RUUVI_WRAP_ESP_ERR_TO_NAME_R_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const char* +wrap_esp_err_to_name_r(const esp_err_t code, char* const p_buf, const size_t buf_len); + +#ifdef __cplusplus +} +#endif + +#endif // RUUVI_WRAP_ESP_ERR_TO_NAME_R_H diff --git a/src/wrap_esp_err_to_name_r.c b/src/wrap_esp_err_to_name_r.c new file mode 100644 index 0000000..6358c10 --- /dev/null +++ b/src/wrap_esp_err_to_name_r.c @@ -0,0 +1,44 @@ +/** + * @file wrap_esp_err_to_name_r.c + * @author TheSomeMan + * @date 2023-04-09 + * @copyright Ruuvi Innovations Ltd, license BSD-3-Clause. + */ + +#include "wrap_esp_err_to_name_r.h" +#include +#include "mbedtls/error.h" +#include "esp_tls.h" + +const char* +wrap_esp_err_to_name_r(const esp_err_t code, char* const p_buf, const size_t buf_len) +{ + static const char* g_esp_unknown_msg = NULL; + if (NULL == g_esp_unknown_msg) + { + const esp_err_t unknown_esp_err_code = 1; + + g_esp_unknown_msg = esp_err_to_name(unknown_esp_err_code); + } + const char* p_err_desc = esp_err_to_name(code); + if (g_esp_unknown_msg != p_err_desc) + { + (void)snprintf(p_buf, buf_len, "%s", p_err_desc); + return p_buf; + } + + if ((strerror_r(code, p_buf, buf_len) != NULL) && ('\0' != p_buf[0])) + { + return p_buf; + } + + mbedtls_strerror(code, p_buf, buf_len); + + const char* const p_unknown_error_code_prefix = "UNKNOWN ERROR CODE ("; + if (0 == strncmp(p_unknown_error_code_prefix, p_buf, strlen(p_unknown_error_code_prefix))) + { + (void)snprintf(p_buf, buf_len, "%s 0x%x(%d)", g_esp_unknown_msg, code, code); + } + + return p_buf; +}