Fix negative readings
We were making negative values 0. This fixes
the issue by converting using an int instead.
Tested: Tests pass
Change-Id: I389a90f52832f1a70b66fdddd8135c77011843a2
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/sensorutils.hpp b/include/sensorutils.hpp
index 98359fc..3b109ae 100644
--- a/include/sensorutils.hpp
+++ b/include/sensorutils.hpp
@@ -137,21 +137,26 @@
const int8_t rExp, const uint16_t bValue,
const int8_t bExp, const bool bSigned)
{
- uint32_t scaledValue =
+ int32_t scaledValue =
(value - (bValue * std::pow(10, bExp) * std::pow(10, rExp))) /
(mValue * std::pow(10, rExp));
- if (scaledValue > std::numeric_limits<uint8_t>::max() ||
- scaledValue < std::numeric_limits<uint8_t>::lowest())
- {
- throw std::out_of_range("Value out of range");
- }
if (bSigned)
{
+ if (scaledValue > std::numeric_limits<int8_t>::max() ||
+ scaledValue < std::numeric_limits<int8_t>::lowest())
+ {
+ throw std::out_of_range("Value out of range");
+ }
return static_cast<int8_t>(scaledValue);
}
else
{
+ if (scaledValue > std::numeric_limits<uint8_t>::max() ||
+ scaledValue < std::numeric_limits<uint8_t>::lowest())
+ {
+ throw std::out_of_range("Value out of range");
+ }
return static_cast<uint8_t>(scaledValue);
}
}
diff --git a/tests/test_sensorcommands.cpp b/tests/test_sensorcommands.cpp
index a0ca740..5513603 100644
--- a/tests/test_sensorcommands.cpp
+++ b/tests/test_sensorcommands.cpp
@@ -52,6 +52,12 @@
EXPECT_EQ(bExp, 0);
}
+ // check negative values
+ expected = 236; // 2s compliment -20
+ scaledVal = ipmi::scaleIPMIValueFromDouble(-20, mValue, rExp, bValue, bExp,
+ bSigned);
+ EXPECT_NEAR(scaledVal, expected, expected * 0.01);
+
// fan example
maxValue = 16000;
minValue = 0;