sensor:monitor: Use a single PowerState object

Now that both monitor classes need to know about power state, change
them to keep a shared_ptr to a PowerState object and then pass the
object into their constructors.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ia83217e8af51204bbb3fc6e8e3f2eac5912b5913
diff --git a/sensor-monitor/main.cpp b/sensor-monitor/main.cpp
index 9b901f5..8f1cc56 100644
--- a/sensor-monitor/main.cpp
+++ b/sensor-monitor/main.cpp
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include "power_state.hpp"
 #include "shutdown_alarm_monitor.hpp"
 #include "threshold_alarm_logger.hpp"
 
@@ -27,9 +28,12 @@
     auto bus = sdbusplus::bus::new_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
 
-    ShutdownAlarmMonitor shutdownMonitor{bus, event};
+    std::shared_ptr<phosphor::fan::PowerState> powerState =
+        std::make_shared<phosphor::fan::PGoodState>();
 
-    ThresholdAlarmLogger logger{bus, event};
+    ShutdownAlarmMonitor shutdownMonitor{bus, event, powerState};
+
+    ThresholdAlarmLogger logger{bus, event, powerState};
 
     return event.loop();
 }
diff --git a/sensor-monitor/shutdown_alarm_monitor.cpp b/sensor-monitor/shutdown_alarm_monitor.cpp
index 73b9c4e..5286e94 100644
--- a/sensor-monitor/shutdown_alarm_monitor.cpp
+++ b/sensor-monitor/shutdown_alarm_monitor.cpp
@@ -84,10 +84,11 @@
 
 using namespace sdbusplus::bus::match;
 
-ShutdownAlarmMonitor::ShutdownAlarmMonitor(sdbusplus::bus::bus& bus,
-                                           sdeventplus::Event& event) :
+ShutdownAlarmMonitor::ShutdownAlarmMonitor(
+    sdbusplus::bus::bus& bus, sdeventplus::Event& event,
+    std::shared_ptr<PowerState> powerState) :
     bus(bus),
-    event(event),
+    event(event), _powerState(std::move(powerState)),
     hardShutdownMatch(bus,
                       "type='signal',member='PropertiesChanged',"
                       "path_namespace='/xyz/openbmc_project/sensors',"
@@ -101,11 +102,11 @@
                       "arg0='" +
                           shutdownInterfaces.at(ShutdownType::hard) + "'",
                       std::bind(&ShutdownAlarmMonitor::propertiesChanged, this,
-                                std::placeholders::_1)),
-    _powerState(std::make_unique<PGoodState>(
-        bus, std::bind(&ShutdownAlarmMonitor::powerStateChanged, this,
-                       std::placeholders::_1)))
+                                std::placeholders::_1))
 {
+    _powerState->addCallback("shutdownMon",
+                             std::bind(&ShutdownAlarmMonitor::powerStateChanged,
+                                       this, std::placeholders::_1));
     findAlarms();
 
     if (_powerState->isPowerOn())
diff --git a/sensor-monitor/shutdown_alarm_monitor.hpp b/sensor-monitor/shutdown_alarm_monitor.hpp
index 82f49a0..d500523 100644
--- a/sensor-monitor/shutdown_alarm_monitor.hpp
+++ b/sensor-monitor/shutdown_alarm_monitor.hpp
@@ -52,11 +52,6 @@
  * means that the host didn't do a soft shutdown in the time allowed and
  * now a hard shutdown is required.  This behavior could be modified with
  * compile flags if anyone needs a different behavior in the future.
- *
- * It currently uses the PGoodState class to check for power state.
- * If a different property is ever desired, a new class can be
- * derived from PowerState and a compile option can be used.
- *
  */
 class ShutdownAlarmMonitor
 {
@@ -73,8 +68,10 @@
      *
      * @param[in] bus - The sdbusplus bus object
      * @param[in] event - The sdeventplus event object
+     * @param[in] powerState - The PowerState object
      */
-    ShutdownAlarmMonitor(sdbusplus::bus::bus& bus, sdeventplus::Event& event);
+    ShutdownAlarmMonitor(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
+                         std::shared_ptr<phosphor::fan::PowerState> powerState);
 
   private:
     /**
@@ -175,6 +172,11 @@
     sdeventplus::Event& event;
 
     /**
+     * @brief The PowerState object to track power state changes.
+     */
+    std::shared_ptr<phosphor::fan::PowerState> _powerState;
+
+    /**
      * @brief The match for properties changing on the HardShutdown
      *        interface.
      */
@@ -187,11 +189,6 @@
     sdbusplus::bus::match::match softShutdownMatch;
 
     /**
-     * @brief The PowerState object to track power state changes.
-     */
-    std::unique_ptr<phosphor::fan::PowerState> _powerState;
-
-    /**
      * @brief The map of alarms.
      */
     std::map<AlarmKey, std::unique_ptr<sdeventplus::utility::Timer<
diff --git a/sensor-monitor/threshold_alarm_logger.cpp b/sensor-monitor/threshold_alarm_logger.cpp
index 50ccf5e..78dbc55 100644
--- a/sensor-monitor/threshold_alarm_logger.cpp
+++ b/sensor-monitor/threshold_alarm_logger.cpp
@@ -82,12 +82,11 @@
             {false,
              ErrorData{"PerfLossLowClear", Entry::Level::Informational}}}}}}};
 
-ThresholdAlarmLogger::ThresholdAlarmLogger(sdbusplus::bus::bus& bus,
-                                           sdeventplus::Event& event) :
+ThresholdAlarmLogger::ThresholdAlarmLogger(
+    sdbusplus::bus::bus& bus, sdeventplus::Event& event,
+    std::shared_ptr<PowerState> powerState) :
     bus(bus),
-    event(event), _powerState(std::make_unique<PGoodState>(
-                      bus, std::bind(&ThresholdAlarmLogger::powerStateChanged,
-                                     this, std::placeholders::_1))),
+    event(event), _powerState(std::move(powerState)),
     warningMatch(bus,
                  "type='signal',member='PropertiesChanged',"
                  "path_namespace='/xyz/openbmc_project/sensors',"
@@ -110,6 +109,10 @@
                   std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
                             std::placeholders::_1))
 {
+    _powerState->addCallback("thresholdMon",
+                             std::bind(&ThresholdAlarmLogger::powerStateChanged,
+                                       this, std::placeholders::_1));
+
     // check for any currently asserted threshold alarms
     std::for_each(
         thresholdData.begin(), thresholdData.end(),
diff --git a/sensor-monitor/threshold_alarm_logger.hpp b/sensor-monitor/threshold_alarm_logger.hpp
index 3e611d8..3380e49 100644
--- a/sensor-monitor/threshold_alarm_logger.hpp
+++ b/sensor-monitor/threshold_alarm_logger.hpp
@@ -64,8 +64,10 @@
      *
      * @param[in] bus - The sdbusplus bus object
      * @param[in] event - The sdeventplus event object
+     * @param[in] powerState - The PowerState object
      */
-    ThresholdAlarmLogger(sdbusplus::bus::bus& bus, sdeventplus::Event& event);
+    ThresholdAlarmLogger(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
+                         std::shared_ptr<phosphor::fan::PowerState> powerState);
 
   private:
     /**
@@ -167,7 +169,7 @@
     /**
      * @brief The PowerState object to track power state changes.
      */
-    std::unique_ptr<phosphor::fan::PowerState> _powerState;
+    std::shared_ptr<phosphor::fan::PowerState> _powerState;
 
     /**
      * @brief The Warning interface match object