-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API esp_err_to_name_with_alloc_str_buf and snprintf_with_esp_err_…
…desc
- Loading branch information
1 parent
f9bcc9d
commit 48b31cd
Showing
7 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.