Filter ADC threshold crossings

When power goes down we get bad events because the
voltage rail spins down faster than pgood. Add in
a delay to check power 2 seconds after the event to
make sure power isn't going down.

Tested-by: power cycled and extra SEL went away

Change-Id: Ib820ccb50d1a949b8096f08e2711ad7a7c36087b
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/src/ADCSensor.cpp b/src/ADCSensor.cpp
index e06c8dd..01edc13 100644
--- a/src/ADCSensor.cpp
+++ b/src/ADCSensor.cpp
@@ -48,7 +48,7 @@
            "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
     objServer(objectServer), scaleFactor(scaleFactor),
     readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
-    waitTimer(io), errCount(0)
+    waitTimer(io), errCount(0), thresholdTimer(io, this)
 {
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/voltage/" + name,
@@ -168,5 +168,6 @@
     {
         return;
     }
-    thresholds::checkThresholds(this);
+
+    thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
 }
diff --git a/src/Thresholds.cpp b/src/Thresholds.cpp
index 4932bce..653b4e6 100644
--- a/src/Thresholds.cpp
+++ b/src/Thresholds.cpp
@@ -208,59 +208,80 @@
     }
 }
 
-bool checkThresholds(Sensor* sensor)
+static std::vector<std::pair<Threshold, bool>> checkThresholds(Sensor* sensor,
+                                                               double value)
 {
-    bool status = true;
-
+    std::vector<std::pair<Threshold, bool>> thresholdChanges;
     if (sensor->thresholds.empty())
     {
-        return true;
+        return thresholdChanges;
     }
+
     for (auto& threshold : sensor->thresholds)
     {
-        if (std::isnan(sensor->value))
+        if (threshold.direction == thresholds::Direction::HIGH)
         {
-            threshold.asserted = false;
-        }
-        else if (threshold.direction == thresholds::Direction::HIGH)
-        {
-            if (sensor->value > threshold.value && !threshold.asserted)
+            if (value > threshold.value)
             {
-                assertThresholds(sensor, threshold.level, threshold.direction,
-                                 true);
-                threshold.asserted = true;
+                thresholdChanges.push_back(std::make_pair(threshold, true));
             }
-            else if (sensor->value <= threshold.value && threshold.asserted)
+            else if (value <= threshold.value)
             {
-                assertThresholds(sensor, threshold.level, threshold.direction,
-                                 false);
-                threshold.asserted = false;
+                thresholdChanges.push_back(std::make_pair(threshold, false));
             }
         }
         else
         {
-            if (sensor->value < threshold.value && !threshold.asserted)
+            if (value < threshold.value)
             {
-                assertThresholds(sensor, threshold.level, threshold.direction,
-                                 true);
-                threshold.asserted = true;
+                thresholdChanges.push_back(std::make_pair(threshold, true));
             }
-            else if (sensor->value >= threshold.value && threshold.asserted)
+            else if (value >= threshold.value)
             {
-                assertThresholds(sensor, threshold.level, threshold.direction,
-                                 false);
-                threshold.asserted = false;
+                thresholdChanges.push_back(std::make_pair(threshold, false));
             }
         }
-        if (threshold.level == thresholds::Level::CRITICAL &&
-            threshold.asserted)
-        {
-            status = false;
-        }
     }
+    return thresholdChanges;
+}
+
+bool checkThresholds(Sensor* sensor)
+{
+    bool status = false;
+    std::vector<std::pair<Threshold, bool>> changes =
+        checkThresholds(sensor, sensor->value);
+    for (const auto& [threshold, asserted] : changes)
+    {
+        assertThresholds(sensor, threshold.level, threshold.direction,
+                         asserted);
+        if (threshold.level == thresholds::Level::CRITICAL && asserted)
+        {
+            status = true;
+        }
+    }
+
     return status;
 }
 
+void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer)
+{
+
+    std::vector<std::pair<Threshold, bool>> changes =
+        checkThresholds(sensor, sensor->value);
+    for (const auto& [threshold, asserted] : changes)
+    {
+        if (asserted)
+        {
+            thresholdTimer.startTimer(threshold);
+        }
+        else
+        {
+            assertThresholds(sensor, threshold.level, threshold.direction,
+                             false);
+        }
+    }
+}
+
 void assertThresholds(Sensor* sensor, thresholds::Level level,
                       thresholds::Direction direction, bool assert)
 {