Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/phydat: Remove deprecated print function #20529

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions sys/include/phydat.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,6 @@ typedef struct {
*/
void phydat_dump(phydat_t *data, uint8_t dim);

/**
* @brief Convert the given unit to a string
*
* @param[in] unit unit to convert
*
* @return string representation of given unit (e.g. V or m)
* @return NULL if unit was not recognized
*
* @deprecated Use @ref phydat_unit_print or @ref phydat_unit_write instead
*
* @warning For classic Harvard architectures a small buffer is used to store
* the string, so that subsequent (or concurrent!) calls will
* overwrite the output.
*/
const char *phydat_unit_to_str(uint8_t unit);

/**
* @brief Same as @ref phydat_unit_to_str
*
* In practise all users used the verbose function anyway. Hence,
* @ref phydat_unit_to_str just covers all units and this is just a backward
* compatibility wrapper.
*
* @deprecated Use @ref phydat_unit_print or @ref phydat_unit_write instead
*/
const char *phydat_unit_to_str_verbose(uint8_t unit);

/**
* @brief Print a unit
*
Expand Down Expand Up @@ -302,7 +275,7 @@ void phydat_fit(phydat_t *dat, const int32_t *values, unsigned int dim);
* factor.
*
* For encoding the unit, this function uses the extended
* phydat_unit_to_str_verbose() function to also print units for non-SI types,
* phydat_unit_write() function to also print units for non-SI types,
* e.g. it will produce `..."u":"date"}` for @ref UNIT_DATE or `..."u":"none"}`
* for @ref UNIT_NONE.
*
Expand Down
27 changes: 0 additions & 27 deletions sys/phydat/phydat_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,6 @@ ssize_t phydat_unit_write(char *dest, size_t max_size, uint8_t unit)
return len;
}

const char *phydat_unit_to_str(uint8_t unit)
{
#if IS_ACTIVE(HAS_FLASH_UTILS_ARCH)
/* Yeah, this is as bad as it looks... The function is deprecated for this
* reason and it will only affect AVR users, for whom this is a good
* trade-off. */
static char buf[8];
ssize_t pos = phydat_unit_write(buf, sizeof(buf) - 1, unit);
assert(pos >= 0);
if (pos < 0) {
pos = 0;
}
buf[pos] = '\0';
return buf;
#else
if ((unit < ARRAY_SIZE(_unit_to_str)) && (_unit_to_str[unit])) {
return _unit_to_str[unit];
}
return "";
#endif
}

const char *phydat_unit_to_str_verbose(uint8_t unit)
{
return phydat_unit_to_str(unit);
}

void phydat_unit_print(uint8_t unit)
{
if ((unit < ARRAY_SIZE(_unit_to_str)) && (_unit_to_str[unit]) != NULL) {
Expand Down
13 changes: 11 additions & 2 deletions tests/sys/senml_phydat/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@
void test_phydat_to_senml_float(void)
{
senml_value_t res;
char unit_buf[10];
size_t size = 0;

for (size_t i = 0; i < ARRAY_SIZE(value_tests); i++) {
senml_value_t *exp = &(value_tests[i].senml1);

phydat_to_senml_float(&res, &(value_tests[i].phydat), value_tests[i].dim);

size = phydat_unit_write(unit_buf, ARRAY_SIZE(unit_buf), value_tests[i].phydat.unit);
unit_buf[size] = 0;
DEBUG("Float: %" PRIi16 "e%" PRIi16 " %" PRIsflash " -> %.f %s\n",
value_tests[i].phydat.val[value_tests[i].dim], value_tests[i].phydat.scale,
phydat_unit_to_str(value_tests[i].phydat.unit),
unit_buf,
res.value.value.f,
senml_unit_to_str(res.attr.unit));

Expand All @@ -132,15 +136,20 @@
void test_phydat_to_senml_decimal(void)
{
senml_value_t res;
char unit_buf[10];
size_t size = 0;

for (size_t i = 0; i < ARRAY_SIZE(value_tests); i++) {
senml_value_t *exp = &(value_tests[i].senml2);

phydat_to_senml_decimal(&res, &(value_tests[i].phydat), value_tests[i].dim);

size = phydat_unit_write(unit_buf, ARRAY_SIZE(unit_buf), value_tests[i].phydat.unit);
unit_buf[size] = 0;

DEBUG("Decimal: %" PRIi16 "e%" PRIi16 " %s -> %" PRIi32 "e%" PRIi32 " %" PRIsflash"\n",
value_tests[i].phydat.val[value_tests[i].dim], value_tests[i].phydat.scale,
phydat_unit_to_str(value_tests[i].phydat.unit),
unit_buf,
res.value.value.df.m, res.value.value.df.e,
senml_unit_to_str(res.attr.unit));

Expand All @@ -152,7 +161,7 @@
}
}


Check warning on line 164 in tests/sys/senml_phydat/main.c

View workflow job for this annotation

GitHub Actions / static-tests

too many consecutive empty lines
Test *tests_senml(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand Down
167 changes: 130 additions & 37 deletions tests/unittests/tests-phydat/tests-phydat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @}
*/

#include <errno.h>
#include <string.h>

#include "embUnit.h"
Expand Down Expand Up @@ -188,46 +189,137 @@ static void test_phydat_fit(void)
}
}

static void test_phydat_unit_write(void)
{
char buffer[] = {'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', '\0'};

/* Regular write, "none" is 4 bytes long */
TEST_ASSERT_EQUAL_INT(4, phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_NONE));

/* Buffer is too small, "none" is 4 bytes long */
TEST_ASSERT_EQUAL_INT(-EOVERFLOW, phydat_unit_write(buffer, 2, UNIT_NONE));

/* Write but no buffer, ignores buffer size, "time" is 4 bytes long */
TEST_ASSERT_EQUAL_INT(4, phydat_unit_write(NULL, 2, UNIT_TIME));

/* Invalid unit */
TEST_ASSERT_EQUAL_INT(-EINVAL, phydat_unit_write(buffer, ARRAY_SIZE(buffer), 0xff));

/* Should not write null terminator */
TEST_ASSERT_EQUAL_STRING("noneAAAAA", buffer);
}

static void test_unitstr__success(void)
{
/* test the verbose cases */
TEST_ASSERT_EQUAL_STRING("undefined", phydat_unit_to_str_verbose(UNIT_UNDEF));
TEST_ASSERT_EQUAL_STRING("none", phydat_unit_to_str_verbose(UNIT_NONE));
TEST_ASSERT_EQUAL_STRING("none", phydat_unit_to_str_verbose(UNIT_BOOL));
TEST_ASSERT_EQUAL_STRING("time", phydat_unit_to_str_verbose(UNIT_TIME));
TEST_ASSERT_EQUAL_STRING("date", phydat_unit_to_str_verbose(UNIT_DATE));

TEST_ASSERT_EQUAL_STRING("°C", phydat_unit_to_str_verbose(UNIT_TEMP_C));
TEST_ASSERT_EQUAL_STRING("°F", phydat_unit_to_str_verbose(UNIT_TEMP_F));
TEST_ASSERT_EQUAL_STRING("K", phydat_unit_to_str_verbose(UNIT_TEMP_K));
TEST_ASSERT_EQUAL_STRING("lx", phydat_unit_to_str_verbose(UNIT_LUX));
TEST_ASSERT_EQUAL_STRING("m", phydat_unit_to_str_verbose(UNIT_M));
TEST_ASSERT_EQUAL_STRING("m^2", phydat_unit_to_str_verbose(UNIT_M2));
TEST_ASSERT_EQUAL_STRING("m^3", phydat_unit_to_str_verbose(UNIT_M3));
TEST_ASSERT_EQUAL_STRING("gₙ", phydat_unit_to_str_verbose(UNIT_G_FORCE));
TEST_ASSERT_EQUAL_STRING("dps", phydat_unit_to_str_verbose(UNIT_DPS));
TEST_ASSERT_EQUAL_STRING("g", phydat_unit_to_str_verbose(UNIT_GRAM));
TEST_ASSERT_EQUAL_STRING("A", phydat_unit_to_str_verbose(UNIT_A));
TEST_ASSERT_EQUAL_STRING("V", phydat_unit_to_str_verbose(UNIT_V));
TEST_ASSERT_EQUAL_STRING("W", phydat_unit_to_str_verbose(UNIT_W));
TEST_ASSERT_EQUAL_STRING("dBm", phydat_unit_to_str_verbose(UNIT_DBM));
TEST_ASSERT_EQUAL_STRING("Gs", phydat_unit_to_str_verbose(UNIT_GAUSS));
TEST_ASSERT_EQUAL_STRING("T", phydat_unit_to_str_verbose(UNIT_T));
TEST_ASSERT_EQUAL_STRING("Bar", phydat_unit_to_str_verbose(UNIT_BAR));
TEST_ASSERT_EQUAL_STRING("Pa", phydat_unit_to_str_verbose(UNIT_PA));
TEST_ASSERT_EQUAL_STRING("permille", phydat_unit_to_str_verbose(UNIT_PERMILL));
TEST_ASSERT_EQUAL_STRING("ppm", phydat_unit_to_str_verbose(UNIT_PPM));
TEST_ASSERT_EQUAL_STRING("ppb", phydat_unit_to_str_verbose(UNIT_PPB));
TEST_ASSERT_EQUAL_STRING("cd", phydat_unit_to_str_verbose(UNIT_CD));
TEST_ASSERT_EQUAL_STRING("%", phydat_unit_to_str_verbose(UNIT_PERCENT));
TEST_ASSERT_EQUAL_STRING("cts", phydat_unit_to_str_verbose(UNIT_CTS));
TEST_ASSERT_EQUAL_STRING("C", phydat_unit_to_str_verbose(UNIT_COULOMB));
TEST_ASSERT_EQUAL_STRING("g/m^3", phydat_unit_to_str_verbose(UNIT_GPM3));
TEST_ASSERT_EQUAL_STRING("F", phydat_unit_to_str_verbose(UNIT_F));
TEST_ASSERT_EQUAL_STRING("pH", phydat_unit_to_str_verbose(UNIT_PH));
TEST_ASSERT_EQUAL_STRING("#/m^3", phydat_unit_to_str_verbose(UNIT_CPM3));
TEST_ASSERT_EQUAL_STRING("ohm", phydat_unit_to_str_verbose(UNIT_OHM));
char buffer[10];
size_t size = 0;

/* test the verbose cases */
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_UNDEF);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("undefined", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_NONE);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("none", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_BOOL);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("none", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_TIME);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("time", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_DATE);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("date", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_TEMP_C);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("°C", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_TEMP_F);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("°F", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_TEMP_K);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("K", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_LUX);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("lx", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_M);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("m", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_M2);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("m^2", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_M3);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("m^3", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_G_FORCE);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("gₙ", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_DPS);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("dps", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_GRAM);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("g", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_A);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("A", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_V);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("V", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_W);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("W", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_DBM);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("dBm", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_GAUSS);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("Gs", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_T);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("T", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_BAR);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("Bar", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PA);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("Pa", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PERMILL);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("permille", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PPM);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("ppm", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PPB);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("ppb", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_CD);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("cd", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PERCENT);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("%", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_CTS);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("cts", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_COULOMB);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("C", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_GPM3);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("g/m^3", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_F);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("F", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_PH);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("pH", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_CPM3);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("#/m^3", buffer);
size = phydat_unit_write(buffer, ARRAY_SIZE(buffer), UNIT_OHM);
buffer[size] = 0;
TEST_ASSERT_EQUAL_STRING("ohm", buffer);
}

static void test_json__success(void)
Expand Down Expand Up @@ -275,6 +367,7 @@ Test *tests_phydat_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_phydat_fit),
new_TestFixture(test_phydat_unit_write),
new_TestFixture(test_unitstr__success),
new_TestFixture(test_json__success),
};
Expand Down
Loading