Skip to content

Commit

Permalink
Data format 6: Fix incorrect decoding of invalid temperature (#56)
Browse files Browse the repository at this point in the history
* Data format 6: Fix incorrect decoding of invalid temperature

* Fix code formatting
  • Loading branch information
TheSomeMan authored Jul 6, 2023
1 parent 35c9c06 commit 8b817f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ruuvi_endpoint_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/test_ruuvi_endpoint_6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8b817f0

Please sign in to comment.