sensordatahandler: clamp the values instead of removing the sensor

Clamping the sensor values to prevent sensors from going missing.

Based on
https://github.com/openbmc/phosphor-host-ipmid/blob/5aae092cab08279d45c7914c466314b356164b7c/dbus-sdr/sensorutils.cpp#L258-L304

Clamping the threshold value to correct the result of negative threshold
value.

Tested:
Set 'sensorUnits1: 0x80' in config for sensor cpu0_abcd_cur which has
low threshold -1 and high threshold 7.6.
Before:
~# ipmitool sensor | grep cpu0_abcd_cur
cpu0_abcd_cur | 0.022      | Amps       | ok    | na        | 4.020     | 4.020     | 7.587     | 7.587     | na
After:
~# ipmitool sensor | grep cpu0_abcd_cur
cpu0_abcd_cur | -0.135     | Amps       | ok    | na        | -0.998    | -0.998    | 7.587     | 7.587     | na

Change-Id: Ie5af79ccbe8ab9660755f1c9d281e49a08d01309
Signed-off-by: Willy Tu <wltu@google.com>
Signed-off-by: JeffLin <JeffLin2@quantatw.com>
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/sensordatahandler.hpp b/sensordatahandler.hpp
index 89d00d0..601f5cd 100644
--- a/sensordatahandler.hpp
+++ b/sensordatahandler.hpp
@@ -258,26 +258,20 @@
     constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
     constexpr uint8_t signedDataFormat = 0x80;
     // if sensorUnits1 [7:6] = 10b, sensor is signed
+    int32_t minClamp;
+    int32_t maxClamp;
     if ((sensorInfo.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
     {
-        if (rawData > std::numeric_limits<int8_t>::max() ||
-            rawData < std::numeric_limits<int8_t>::lowest())
-        {
-            log<level::ERR>("Value out of range");
-            throw std::out_of_range("Value out of range");
-        }
-        setReading(static_cast<int8_t>(rawData), &response);
+        minClamp = std::numeric_limits<int8_t>::lowest();
+        maxClamp = std::numeric_limits<int8_t>::max();
     }
     else
     {
-        if (rawData > std::numeric_limits<uint8_t>::max() ||
-            rawData < std::numeric_limits<uint8_t>::lowest())
-        {
-            log<level::ERR>("Value out of range");
-            throw std::out_of_range("Value out of range");
-        }
-        setReading(static_cast<uint8_t>(rawData), &response);
+        minClamp = std::numeric_limits<uint8_t>::lowest();
+        maxClamp = std::numeric_limits<uint8_t>::max();
     }
+    setReading(static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp)),
+               &response);
 
     if (!std::isfinite(value))
     {