Skip to content

Commit

Permalink
Add API esp_err_to_name_with_alloc_str_buf and snprintf_with_esp_err_…
Browse files Browse the repository at this point in the history
…desc
  • Loading branch information
TheSomeMan committed Apr 12, 2023
1 parent f9bcc9d commit 48b31cd
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(RUUVI_ESP_WRAPPERS_SRC
include/os_timer_sig.h
include/os_wrapper_types.h
include/time_units.h
include/snprintf_with_esp_err_desc.h
include/str_buf.h
include/wrap_esp_err_to_name_r.h
src/log_dump.c
Expand All @@ -34,6 +35,7 @@ set(RUUVI_ESP_WRAPPERS_SRC
src/os_time.c
src/os_timer.c
src/os_timer_sig.c
src/snprintf_with_esp_err_desc.c
src/str_buf.c
src/wrap_esp_err_to_name_r.c
)
Expand Down
34 changes: 34 additions & 0 deletions include/snprintf_with_esp_err_desc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @file snprintf_with_esp_err_desc.h
* @author TheSomeMan
* @date 2023-04-09
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/

#ifndef RUUVI_SNPRINTF_WITH_ESP_ERR_DESC_H
#define RUUVI_SNPRINTF_WITH_ESP_ERR_DESC_H

#include <esp_err.h>
#include "str_buf.h"

#ifdef __cplusplus
extern "C" {
#endif

str_buf_t
esp_err_to_name_with_alloc_str_buf(const esp_err_t esp_err_code);

ATTR_PRINTF(4, 5)
int
snprintf_with_esp_err_desc(
const esp_err_t esp_err_code,
char* const p_buf,
const size_t buf_size,
const char* const p_fmt,
...);

#ifdef __cplusplus
}
#endif

#endif // RUUVI_SNPRINTF_WITH_ESP_ERR_DESC_H
68 changes: 68 additions & 0 deletions src/snprintf_with_esp_err_desc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @file snprintf_with_esp_err_desc.c
* @author TheSomeMan
* @date 2023-04-09
* @copyright Ruuvi Innovations Ltd, license BSD-3-Clause.
*/

#include "snprintf_with_esp_err_desc.h"
#include <string.h>
#include <esp_err.h>
#include "os_malloc.h"
#include "str_buf.h"
#include "wrap_esp_err_to_name_r.h"

#define ERR_DESC_SIZE (120)

str_buf_t
esp_err_to_name_with_alloc_str_buf(const esp_err_t esp_err_code)
{
char* p_err_desc_buf = os_malloc(ERR_DESC_SIZE);
if (NULL == p_err_desc_buf)
{
return str_buf_init_null();
}
str_buf_t str_buf = STR_BUF_INIT(p_err_desc_buf, ERR_DESC_SIZE);
wrap_esp_err_to_name_r(esp_err_code, p_err_desc_buf, ERR_DESC_SIZE);
str_buf.idx = strlen(p_err_desc_buf);
return str_buf;
}

int
snprintf_with_esp_err_desc(
const esp_err_t esp_err_code,
char* const p_buf,
const size_t buf_size,
const char* const p_fmt,
...)
{
int idx = 0;
if (NULL != p_fmt)
{
va_list args;
va_start(args, p_fmt);
idx = vsnprintf(p_buf, buf_size, p_fmt, args);
va_end(args);
}
if (idx < 0)
{
return idx;
}
char* const p_extra_buf = (NULL != p_buf) ? &p_buf[idx] : NULL;
const size_t remain_len = ((size_t)idx < buf_size) ? buf_size - (size_t)idx : 0;
const char* const p_delimiter = (0 != idx) ? ", " : "";
str_buf_t str_buf_err_desc = esp_err_to_name_with_alloc_str_buf(esp_err_code);
if (NULL != str_buf_err_desc.buf)
{
idx += snprintf(p_extra_buf, remain_len, "%serror %d (%s)", p_delimiter, esp_err_code, str_buf_err_desc.buf);
}
else
{
idx += snprintf(p_extra_buf, remain_len, "%serror %d", p_delimiter, esp_err_code);
}
if (NULL != str_buf_err_desc.buf)
{
str_buf_free_buf(&str_buf_err_desc);
}
return idx;
}
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_subdirectory(test_os_task)
#add_subdirectory(test_os_task_freertos)
add_subdirectory(test_os_timer_freertos)
add_subdirectory(test_os_timer_sig_freertos)
add_subdirectory(test_snprintf_with_esp_err_desc)
add_subdirectory(test_str_buf)
add_subdirectory(test_time_units)

Expand Down Expand Up @@ -113,6 +114,11 @@ add_test(NAME test_os_timer_sig_freertos
--gtest_output=xml:$<TARGET_FILE_DIR:ruuvi_esp_wrappers-test-os_timer_sig_freertos>/gtestresults.xml
)

add_test(NAME test_snprintf_with_esp_err_desc
COMMAND ruuvi_esp_wrappers-test-snprintf_with_esp_err_desc
--gtest_output=xml:$<TARGET_FILE_DIR:ruuvi_esp_wrappers-test-snprintf_with_esp_err_desc>/gtestresults.xml
)

add_test(NAME test_str_buf
COMMAND ruuvi_esp_wrappers-test-str_buf
--gtest_output=xml:$<TARGET_FILE_DIR:ruuvi_esp_wrappers-test-str_buf>/gtestresults.xml
Expand Down
20 changes: 20 additions & 0 deletions tests/common/include/esp_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ typedef int32_t esp_err_t;
const char*
esp_err_to_name(esp_err_t code);

/**
* @brief Returns string for esp_err_t and system error codes
*
* This function finds the error code in a pre-generated lookup-table of
* esp_err_t errors and returns its string representation. If the error code
* is not found then it is attempted to be found among system errors.
*
* The function is generated by the Python script
* tools/gen_esp_err_to_name.py which should be run each time an esp_err_t
* error is modified, created or removed from the IDF project.
*
* @param code esp_err_t error code
* @param[out] buf buffer where the error message should be written
* @param buflen Size of buffer buf. At most buflen bytes are written into the buf buffer (including the terminating
* null byte).
* @return buf containing the string error message
*/
const char*
esp_err_to_name_r(esp_err_t code, char* buf, size_t buflen);

static inline void
strlcpy(char* dst, const char* src, const size_t len)
{
Expand Down
50 changes: 50 additions & 0 deletions tests/test_snprintf_with_esp_err_desc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
cmake_minimum_required(VERSION 3.7)

project(ruuvi_esp_wrappers-test-snprintf_with_esp_err_desc)
set(ProjectId ruuvi_esp_wrappers-test-snprintf_with_esp_err_desc)

add_executable(${ProjectId}
test_snprintf_with_esp_err_desc.cpp
../../src/snprintf_with_esp_err_desc.c
../../include/snprintf_with_esp_err_desc.h
../../src/str_buf.c
../../include/str_buf.h
)

set_target_properties(${ProjectId} PROPERTIES
C_STANDARD 11
CXX_STANDARD 14
)

target_include_directories(${ProjectId} PUBLIC
${gtest_SOURCE_DIR}/include
${gtest_SOURCE_DIR}
../../include
include
${CMAKE_CURRENT_SOURCE_DIR}
)

target_compile_definitions(${ProjectId} PUBLIC
RUUVI_TESTS_SNPRINTF_WITH_ESP_ERR_DESC=1
)

target_compile_options(${ProjectId} PUBLIC
-g3
-ggdb
-fprofile-arcs
-ftest-coverage
--coverage
)

# CMake has a target_link_options starting from version 3.13
#target_link_options(${ProjectId} PUBLIC
# --coverage
#)

target_link_libraries(${ProjectId}
gtest
gtest_main
gcov
ruuvi_esp_wrappers-common_test_funcs
--coverage
)
Loading

0 comments on commit 48b31cd

Please sign in to comment.