From 8b817f05cfaad7ed462a3f50b3646af84fdacf86 Mon Sep 17 00:00:00 2001 From: TheSomeMan Date: Thu, 6 Jul 2023 15:48:57 +0700 Subject: [PATCH] Data format 6: Fix incorrect decoding of invalid temperature (#56) * Data format 6: Fix incorrect decoding of invalid temperature * Fix code formatting --- src/ruuvi_endpoint_6.c | 10 +++++----- test/test_ruuvi_endpoint_6.c | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ruuvi_endpoint_6.c b/src/ruuvi_endpoint_6.c index a6b5bf4..916dbc2 100644 --- a/src/ruuvi_endpoint_6.c +++ b/src/ruuvi_endpoint_6.c @@ -237,15 +237,15 @@ static re_float re_6_decode_temperature (const uint8_t * const p_slot) coded_val >>= RE_6_OFFSET_TEMPERATURE_OFS; coded_val &= RE_6_OFFSET_TEMPERATURE_MASK; - if (0 != (coded_val & ( (RE_6_OFFSET_TEMPERATURE_MASK + 1) >> 1U))) + if (RE_6_INVALID_TEMPERATURE == coded_val) { - // it's negative val, we need to set all most significant bits to '1' - coded_val |= ~RE_6_OFFSET_TEMPERATURE_MASK; + return NAN; } - if (RE_6_INVALID_TEMPERATURE == coded_val) + if (0 != (coded_val & ( (RE_6_OFFSET_TEMPERATURE_MASK + 1) >> 1U))) { - return NAN; + // it's negative val, we need to set all most significant bits to '1' + coded_val |= ~RE_6_OFFSET_TEMPERATURE_MASK; } return (re_float) (int16_t) coded_val / RE_6_TEMPERATURE_RATIO; diff --git a/test/test_ruuvi_endpoint_6.c b/test/test_ruuvi_endpoint_6.c index b912297..a70966d 100644 --- a/test/test_ruuvi_endpoint_6.c +++ b/test/test_ruuvi_endpoint_6.c @@ -258,6 +258,21 @@ void test_ruuvi_endpoint_6_get_invalid_data (void) (const re_6_data_t *) &m_re_6_data_invalid); TEST_ASSERT (RE_SUCCESS == err_code); TEST_ASSERT (! (memcmp (test_buffer, invalid_data, sizeof (invalid_data)))); + uint8_t raw_buf[31] = {0x02, 0x01, 0x04, 0x1B, 0xFF, 0x99, 0x04}; + memcpy (&raw_buf[7], test_buffer, sizeof (test_buffer)); + re_6_data_t decoded_data = {0}; + TEST_ASSERT_EQUAL (RE_SUCCESS, re_6_decode (raw_buf, &decoded_data)); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.pm1p0_ppm, decoded_data.pm1p0_ppm); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.pm2p5_ppm, decoded_data.pm1p0_ppm); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.pm4p0_ppm, decoded_data.pm1p0_ppm); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.pm10p0_ppm, decoded_data.pm1p0_ppm); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.co2, decoded_data.co2); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.humidity_rh, decoded_data.humidity_rh); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.voc_index, decoded_data.voc_index); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.nox_index, decoded_data.nox_index); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.temperature_c, decoded_data.temperature_c); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.measurement_count, decoded_data.measurement_count); + TEST_ASSERT_EQUAL (m_re_6_data_invalid.address, decoded_data.address); } void test_ruuvi_endpoint_6_underflow (void)