Add watches for throttling reported by the OCC

Add Error objects to watch the sysfs entries provided by the OCC hwmon
driver that report various types of throttling. Also needed to add a
boolean input to the Error callback interface.

Resolves openbmc/openbmc#1821

Change-Id: I4425770a92ace0f73024b3dc4c577ce46957a62a
Signed-off-by: Eddie James <eajames@us.ibm.com>
diff --git a/occ_device.cpp b/occ_device.cpp
index 8129502..b100d46 100644
--- a/occ_device.cpp
+++ b/occ_device.cpp
@@ -1,5 +1,6 @@
 #include <iostream>
 #include "occ_device.hpp"
+#include "occ_status.hpp"
 
 namespace open_power
 {
@@ -25,5 +26,20 @@
     return (master != 0);
 }
 
+void Device::throttleProcTempCallback(bool error)
+{
+        statusObject.throttleProcTemp(error);
+}
+
+void Device::throttleProcPowerCallback(bool error)
+{
+        statusObject.throttleProcPower(error);
+}
+
+void Device::throttleMemTempCallback(bool error)
+{
+        statusObject.throttleMemTemp(error);
+}
+
 } // namespace occ
 } // namespace open_power
diff --git a/occ_device.hpp b/occ_device.hpp
index 7ffb0ef..fd30641 100644
--- a/occ_device.hpp
+++ b/occ_device.hpp
@@ -13,6 +13,7 @@
 {
 
 class Manager;
+class Status;
 namespace fs = std::experimental::filesystem;
 
 /** @class Device
@@ -38,18 +39,38 @@
         Device(EventPtr& event,
                const std::string& name,
                const Manager& manager,
-               std::function<void()> callBack = nullptr) :
+               Status& status,
+               std::function<void(bool)> callBack = nullptr) :
 #ifdef I2C_OCC
             config(name),
 #else
             config(name + '-' + "dev0"),
 #endif
             errorFile(fs::path(config) / "occ_error"),
+            statusObject(status),
             error(event, errorFile, callBack),
             presence(event,
                      fs::path(config) / "occs_present",
                      manager,
-                     callBack)
+                     callBack),
+            throttleProcTemp(
+                event,
+                fs::path(config) / "occ_dvfs_ot",
+                std::bind(std::mem_fn(&Device::throttleProcTempCallback),
+                          this,
+                          std::placeholders::_1)),
+            throttleProcPower(
+                event,
+                fs::path(config) / "occ_dvfs_power",
+                std::bind(std::mem_fn(&Device::throttleProcPowerCallback),
+                          this,
+                          std::placeholders::_1)),
+            throttleMemTemp(
+                event,
+                fs::path(config) / "occ_mem_throttle",
+                std::bind(std::mem_fn(&Device::throttleMemTempCallback),
+                          this,
+                          std::placeholders::_1))
         {
             // Nothing to do here
         }
@@ -89,6 +110,9 @@
                 presence.addWatch();
             }
 
+            throttleProcTemp.addWatch();
+            throttleProcPower.addWatch();
+            throttleMemTemp.addWatch();
             error.addWatch();
         }
 
@@ -98,6 +122,10 @@
             // we can always safely remove watch even if we don't add it
             presence.removeWatch();
             error.removeWatch();
+            error.removeWatch();
+            throttleMemTemp.removeWatch();
+            throttleProcPower.removeWatch();
+            throttleProcTemp.removeWatch();
         }
 
     private:
@@ -118,12 +146,20 @@
          */
         static fs::path unBindPath;
 
+        /**  Store the associated Status instance */
+        Status& statusObject;
+
         /** Abstraction of error monitoring */
         Error error;
 
         /** Abstraction of OCC presence monitoring */
         Presence presence;
 
+        /** Error instances for watching for throttling events */
+        Error throttleProcTemp;
+        Error throttleProcPower;
+        Error throttleMemTemp;
+
         /** @brief file writer to achieve bind and unbind
          *
          *  @param[in] filename - Name of file to be written
@@ -141,6 +177,24 @@
 
         /** @brief Returns if device represents the master OCC */
         bool master() const;
+
+        /** @brief callback for the proc temp throttle event
+         *
+         *  @param[in] error - True if an error is reported, false otherwise
+         */
+        void throttleProcTempCallback(bool error);
+
+        /** @brief callback for the proc power throttle event
+         *
+         *  @param[in] error - True if an error is reported, false otherwise
+         */
+        void throttleProcPowerCallback(bool error);
+
+        /** @brief callback for the proc temp throttle event
+         *
+         *  @param[in] error - True if an error is reported, false otherwise
+         */
+        void throttleMemTempCallback(bool error);
 };
 
 } // namespace occ
diff --git a/occ_errors.cpp b/occ_errors.cpp
index fc24310..ee41db9 100644
--- a/occ_errors.cpp
+++ b/occ_errors.cpp
@@ -117,18 +117,10 @@
     // A non-zero data indicates an error condition
     // Let the caller take appropriate action on this
     auto data = readFile(len);
-    log<level::INFO>("Error file updated",
-                     entry("ERROR=%s", data.c_str()));
-    if (data.empty() ||
-            data.front() == NO_ERROR)
-    {
-        return;
-    }
-
-    // This must be an error
+    bool error = !(data.empty() || data.front() == NO_ERROR);
     if (callBack)
     {
-        callBack();
+        callBack(error);
     }
     return;
 }
diff --git a/occ_errors.hpp b/occ_errors.hpp
index 070e72c..12ae925 100644
--- a/occ_errors.hpp
+++ b/occ_errors.hpp
@@ -32,7 +32,7 @@
          */
         Error(EventPtr& event,
               const fs::path& file,
-              std::function<void()> callBack = nullptr) :
+              std::function<void(bool)> callBack = nullptr) :
             event(event),
             file(fs::path(DEV_PATH) / file),
             callBack(callBack)
@@ -97,7 +97,7 @@
         const fs::path file;
 
         /** @brief Optional function to call on error scenario */
-        std::function<void()> callBack;
+        std::function<void(bool)> callBack;
 
         /** @brief Reads file data
          *
diff --git a/occ_presence.cpp b/occ_presence.cpp
index f91521a..23fece0 100644
--- a/occ_presence.cpp
+++ b/occ_presence.cpp
@@ -48,7 +48,7 @@
                          entry("OCC_OCCS=%d", occsPresent)));
         if (callBack)
         {
-            callBack();
+            callBack(true);
         }
     }
 }
diff --git a/occ_presence.hpp b/occ_presence.hpp
index 321dacb..e0bd3ad 100644
--- a/occ_presence.hpp
+++ b/occ_presence.hpp
@@ -30,7 +30,7 @@
         Presence(EventPtr& event,
               const fs::path& file,
               const Manager& mgr,
-              std::function<void()> callBack = nullptr) :
+              std::function<void(bool)> callBack = nullptr) :
             Error(event, file, callBack),
             manager(mgr)
         {
diff --git a/occ_status.cpp b/occ_status.cpp
index 36520b5..8867276 100644
--- a/occ_status.cpp
+++ b/occ_status.cpp
@@ -63,13 +63,17 @@
 }
 
 // Callback handler when a device error is reported.
-void Status::deviceErrorHandler()
+void Status::deviceErrorHandler(bool error)
 {
-    // This would deem OCC inactive
-    this->occActive(false);
+    // Make sure we have an error
+    if (error)
+    {
+        // This would deem OCC inactive
+        this->occActive(false);
 
-    // Reset the OCC
-    this->resetOCC();
+        // Reset the OCC
+        this->resetOCC();
+    }
 }
 
 // Sends message to host control command handler to reset OCC
diff --git a/occ_status.hpp b/occ_status.hpp
index 55ee539..60134fb 100644
--- a/occ_status.hpp
+++ b/occ_status.hpp
@@ -70,7 +70,9 @@
                      name + std::to_string(instance + 1),
 #endif
                      manager,
-                     std::bind(&Status::deviceErrorHandler, this)),
+                     *this,
+                     std::bind(std::mem_fn(&Status::deviceErrorHandler), this,
+                               std::placeholders::_1)),
               hostControlSignal(
                      bus,
                      sdbusRule::type::signal() +
@@ -151,8 +153,11 @@
          **/
         sdbusplus::bus::match_t hostControlSignal;
 
-        /** @brief Callback handler when device errors are detected */
-        void deviceErrorHandler();
+        /** @brief Callback handler when device errors are detected
+         *
+         *  @param[in]  error - True if an error is reported, false otherwise
+         */
+        void deviceErrorHandler(bool error);
 
         /** @brief Callback function on host control signals
          *