Skip to content

Commit

Permalink
Add API wrap_esp_err_to_name_r
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Apr 12, 2023
1 parent ea0a006 commit f9bcc9d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions include/wrap_esp_err_to_name_r.h
Original file line number Diff line number Diff line change
@@ -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 <esp_err.h>

#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
44 changes: 44 additions & 0 deletions src/wrap_esp_err_to_name_r.c
Original file line number Diff line number Diff line change
@@ -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 <esp_err.h>
#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;
}

0 comments on commit f9bcc9d

Please sign in to comment.