Clear flags when the host changes to powered off state

When the host is powered off, the OCCs will be stopped, so clear any
reset pending flags as well as any outstanding HRESET requests.

This will ensure the next boot will start clean and not react to
something that happened on prior boot.

Tested on Rainier for several error scenarios.

Change-Id: Ie4156975a844e823787f7162ee0542d7f099bd12
Signed-off-by: Chris Cain <cjcain@us.ibm.com>
diff --git a/occ_manager.cpp b/occ_manager.cpp
index ff9ff3f..699f4a8 100644
--- a/occ_manager.cpp
+++ b/occ_manager.cpp
@@ -66,7 +66,7 @@
                   std::placeholders::_1, std::placeholders::_2),
         std::bind(std::mem_fn(&Manager::updateOccSafeMode), this,
                   std::placeholders::_1),
-        event);
+        std::bind(std::mem_fn(&Manager::hostPoweredOff), this), event);
 #endif
 }
 
@@ -1784,5 +1784,24 @@
     }
 }
 
+// Clean up any variables since the OCC is no longer running.
+// Called when pldm receives an event indicating host is powered off.
+void Manager::hostPoweredOff()
+{
+    if (resetRequired)
+    {
+        lg2::info("hostPoweredOff: Clearing resetRequired for OCC{INST}",
+                  "INST", resetInstance);
+        resetRequired = false;
+    }
+    if (resetInProgress)
+    {
+        lg2::info("hostPoweredOff: Clearing resetInProgress for OCC{INST}",
+                  "INST", resetInstance);
+        resetInProgress = false;
+    }
+    resetInstance = 255;
+}
+
 } // namespace occ
 } // namespace open_power
diff --git a/occ_manager.hpp b/occ_manager.hpp
index fa34bf4..b11756d 100644
--- a/occ_manager.hpp
+++ b/occ_manager.hpp
@@ -162,6 +162,10 @@
      */
     void setSensorValueToNonFunctional(uint32_t id) const;
 
+    /** @brief Clear any state flags that need to be reset when the host state
+     * is off */
+    void hostPoweredOff();
+
   private:
     /** @brief Creates the OCC D-Bus objects.
      */
@@ -358,8 +362,8 @@
 #ifdef PLDM
     /**
      * @brief Timer used to throttle PLDM traces when there are problems
-              determining the OCC status via pldm. Used to prevent excessive
-              journal traces.
+     determining the OCC status via pldm. Used to prevent excessive
+     journal traces.
      */
     std::unique_ptr<
         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
diff --git a/pldm.cpp b/pldm.cpp
index 4b1582d..7c38971 100644
--- a/pldm.cpp
+++ b/pldm.cpp
@@ -272,6 +272,8 @@
         if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
         {
             clearData();
+            // Notify manager that host is now off
+            poweredOffCallBack();
         }
     }
 }
@@ -308,6 +310,12 @@
                    "NUM", sbeInstanceToEffecter.size());
         sbeInstanceToEffecter.clear();
     }
+    if (!outstandingHResets.empty())
+    {
+        lg2::info("clearData: clearing {NUM} outstanding HRESET requests",
+                  "NUM", outstandingHResets.size());
+        outstandingHResets.clear();
+    }
 }
 
 void Interface::fetchEffecterInfo(
diff --git a/pldm.hpp b/pldm.hpp
index 881061b..4d2873f 100644
--- a/pldm.hpp
+++ b/pldm.hpp
@@ -71,14 +71,18 @@
      *                                 state changes.
      *  @param[in] safeModeCallBack  - callBack handler to invoke when the
      *                                 system is in safe mode.
+     *  @param[in] poweredOffCallBack - callBack handler to invoke when the
+     *                                 host is powered off.
      */
     explicit Interface(
         std::function<bool(open_power::occ::instanceID, bool)>
             occActiveCallBack,
         std::function<void(open_power::occ::instanceID, bool)> sbeCallBack,
-        std::function<void(bool)> safeModeCallBack, EventPtr& event) :
+        std::function<void(bool)> safeModeCallBack,
+        std::function<void()> poweredOffCallBack, EventPtr& event) :
         occActiveCallBack(occActiveCallBack), sbeCallBack(sbeCallBack),
-        safeModeCallBack(safeModeCallBack), event(event),
+        safeModeCallBack(safeModeCallBack),
+        poweredOffCallBack(poweredOffCallBack), event(event),
         pldmEventSignal(
             open_power::occ::utils::getBus(),
             MatchRules::type::signal() +
@@ -205,6 +209,9 @@
      */
     std::function<void(bool)> safeModeCallBack = nullptr;
 
+    /** @brief Callback handler to be invoked when the host is powered off */
+    std::function<void()> poweredOffCallBack = nullptr;
+
     /** @brief reference to sd_event wrapped in unique_ptr */
     EventPtr& event;