sensor-mon: Watch for InterfacesAdded

Start handling the InterfacesAdded signal for the threshold interfaces.
This is so that if a sensor has a threshold alarm set, but then restarts
and then doesn't have it set, sensor-monitor will get the update to the
new good value.

Previously, sensor-monitor would still log a threshold alarm on every
power on after a sensor daemon restarted and then showed no alarm.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I691c8949165bc29ac2eab4190e5dc9a9283bd2e6
diff --git a/sensor-monitor/threshold_alarm_logger.cpp b/sensor-monitor/threshold_alarm_logger.cpp
index b2fd7e8..03b35ba 100644
--- a/sensor-monitor/threshold_alarm_logger.cpp
+++ b/sensor-monitor/threshold_alarm_logger.cpp
@@ -44,6 +44,9 @@
 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
 constexpr auto assocInterface = "xyz.openbmc_project.Association";
 
+const std::vector<std::string> thresholdIfaceNames{
+    warningInterface, criticalInterface, perfLossInterface};
+
 using ErrorData = std::tuple<ErrorName, Entry::Level>;
 
 /**
@@ -112,7 +115,12 @@
                        "type='signal',member='InterfacesRemoved',arg0path="
                        "'/xyz/openbmc_project/sensors/'",
                        std::bind(&ThresholdAlarmLogger::interfacesRemoved, this,
-                                 std::placeholders::_1))
+                                 std::placeholders::_1)),
+    ifacesAddedMatch(bus,
+                     "type='signal',member='InterfacesAdded',arg0path="
+                     "'/xyz/openbmc_project/sensors/'",
+                     std::bind(&ThresholdAlarmLogger::interfacesAdded, this,
+                               std::placeholders::_1))
 {
     _powerState->addCallback("thresholdMon",
                              std::bind(&ThresholdAlarmLogger::powerStateChanged,
@@ -143,6 +151,47 @@
 
     msg.read(interface, properties);
 
+    checkProperties(sensorPath, interface, properties);
+}
+
+void ThresholdAlarmLogger::interfacesRemoved(sdbusplus::message_t& msg)
+{
+    sdbusplus::message::object_path path;
+    std::vector<std::string> interfaces;
+
+    msg.read(path, interfaces);
+
+    for (const auto& interface : interfaces)
+    {
+        if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(),
+                      interface) != thresholdIfaceNames.end())
+        {
+            alarms.erase(InterfaceKey{path, interface});
+        }
+    }
+}
+
+void ThresholdAlarmLogger::interfacesAdded(sdbusplus::message_t& msg)
+{
+    sdbusplus::message::object_path path;
+    std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces;
+
+    msg.read(path, interfaces);
+
+    for (const auto& [interface, properties] : interfaces)
+    {
+        if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(),
+                      interface) != thresholdIfaceNames.end())
+        {
+            checkProperties(path, interface, properties);
+        }
+    }
+}
+
+void ThresholdAlarmLogger::checkProperties(
+    const std::string& sensorPath, const std::string& interface,
+    const std::map<std::string, std::variant<bool>>& properties)
+{
     auto alarmProperties = thresholdData.find(interface);
     if (alarmProperties == thresholdData.end())
     {
@@ -180,25 +229,6 @@
     }
 }
 
-void ThresholdAlarmLogger::interfacesRemoved(sdbusplus::message_t& msg)
-{
-    static const std::vector<std::string> thresholdNames{
-        warningInterface, criticalInterface, perfLossInterface};
-    sdbusplus::message::object_path path;
-    std::vector<std::string> interfaces;
-
-    msg.read(path, interfaces);
-
-    for (const auto& interface : interfaces)
-    {
-        if (std::find(thresholdNames.begin(), thresholdNames.end(),
-                      interface) != thresholdNames.end())
-        {
-            alarms.erase(InterfaceKey{path, interface});
-        }
-    }
-}
-
 void ThresholdAlarmLogger::checkThresholds(const std::string& interface,
                                            const std::string& sensorPath,
                                            const std::string& service)
diff --git a/sensor-monitor/threshold_alarm_logger.hpp b/sensor-monitor/threshold_alarm_logger.hpp
index 8659ea7..f7b00c5 100644
--- a/sensor-monitor/threshold_alarm_logger.hpp
+++ b/sensor-monitor/threshold_alarm_logger.hpp
@@ -91,6 +91,28 @@
     void interfacesRemoved(sdbusplus::message_t& msg);
 
     /**
+     * @brief The interfacesAdded handler for the threshold
+     *        interfaces.
+     *
+     * Checks the alarm when it shows up on D-Bus.
+     *
+     * @param[in] msg - The signal message payload.
+     */
+    void interfacesAdded(sdbusplus::message_t& msg);
+
+    /**
+     * @brief Checks for alarms in the D-Bus data passed in,
+     *        and creates an event log if necessary.
+     *
+     * @param[in] sensorPath - D-Bus path of the sensor
+     * @param[in] interface - The threshold interface name
+     * @param[in] properties - The map of property values on the interface
+     */
+    void checkProperties(
+        const std::string& sensorPath, const std::string& interface,
+        const std::map<std::string, std::variant<bool>>& properties);
+
+    /**
      * @brief Checks for active alarms on the path and threshold interface
      *        passed in and creates event logs if necessary.
      *
@@ -202,6 +224,11 @@
     sdbusplus::bus::match_t ifacesRemovedMatch;
 
     /**
+     * @brief The InterfacesAdded match object
+     */
+    sdbusplus::bus::match_t ifacesAddedMatch;
+
+    /**
      * @brief The current alarm values
      */
     std::map<InterfaceKey, std::map<PropertyName, bool>> alarms;