1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #20529 from Teufelchen1/deprecate/phydat_str

sys/phydat: Remove deprecated print function
This commit is contained in:
Marian Buschsieweke 2024-04-04 11:24:04 +00:00 committed by GitHub
commit be0d276caa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 142 additions and 94 deletions

View File

@ -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
*
@ -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.
*

View File

@ -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) {

View File

@ -109,15 +109,19 @@ static value_test_t value_tests[] = {
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));
@ -132,15 +136,20 @@ void test_phydat_to_senml_float(void)
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));

View File

@ -20,6 +20,7 @@
* @}
*/
#include <errno.h>
#include <string.h>
#include "embUnit.h"
@ -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)
{
char buffer[10];
size_t size = 0;
/* 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));
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)
@ -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),
};