sensor-mon: Threshold alarm logger framework

Add the new class ThresholdAlarmLogger that will watch the Warning,
Critical, and PerformanceLoss sensor threshold D-Bus interfaces and
create event logs when their alarm properties set and clear.

This commit just creates the class with its three sdbusplus match
objects to watch those interfaces.

Change-Id: I67c629bcabe059952c2029dc034daace9b88f051
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/sensor-monitor/Makefile.am b/sensor-monitor/Makefile.am
index da19997..313c59f 100644
--- a/sensor-monitor/Makefile.am
+++ b/sensor-monitor/Makefile.am
@@ -9,6 +9,7 @@
 
 sensor_monitor_SOURCES = \
     shutdown_alarm_monitor.cpp \
+    threshold_alarm_logger.cpp \
 	main.cpp
 
 sensor_monitor_LDADD = \
diff --git a/sensor-monitor/README.md b/sensor-monitor/README.md
index 2887c53..ba09a08 100644
--- a/sensor-monitor/README.md
+++ b/sensor-monitor/README.md
@@ -16,4 +16,14 @@
 The configuration options `SHUTDOWN_ALARM_HARD_SHUTDOWN_DELAY_MS` and
 `SHUTDOWN_ALARM_SOFT_SHUTDOWN_DELAY_MS` can be used to change the delays.
 
-Currently, this monitor is enabled by default.
+### ThresholdAlarmLogger
+
+This monitor will watch the alarm properties on the following threshold
+D-Bus interfaces:
+
+- `xyz.openbmc_project.Sensor.Threshold.Warning`
+- `xyz.openbmc_project.Sensor.Threshold.Critical`
+- `xyz.openbmc_project.Sensor.Threshold.PerformanceLoss`
+
+When the alarm properties are asserted, event logs are created.  When they are
+deasserted, informational event logs are created.
diff --git a/sensor-monitor/main.cpp b/sensor-monitor/main.cpp
index bf1bf17..9b901f5 100644
--- a/sensor-monitor/main.cpp
+++ b/sensor-monitor/main.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "shutdown_alarm_monitor.hpp"
+#include "threshold_alarm_logger.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdeventplus/event.hpp>
@@ -28,5 +29,7 @@
 
     ShutdownAlarmMonitor shutdownMonitor{bus, event};
 
+    ThresholdAlarmLogger logger{bus, event};
+
     return event.loop();
 }
diff --git a/sensor-monitor/threshold_alarm_logger.cpp b/sensor-monitor/threshold_alarm_logger.cpp
new file mode 100644
index 0000000..0a057dc
--- /dev/null
+++ b/sensor-monitor/threshold_alarm_logger.cpp
@@ -0,0 +1,60 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "threshold_alarm_logger.hpp"
+
+namespace sensor::monitor
+{
+
+const std::string warningInterface =
+    "xyz.openbmc_project.Sensor.Threshold.Warning";
+const std::string criticalInterface =
+    "xyz.openbmc_project.Sensor.Threshold.Critical";
+const std::string perfLossInterface =
+    "xyz.openbmc_project.Sensor.Threshold.PerformanceLoss";
+
+ThresholdAlarmLogger::ThresholdAlarmLogger(sdbusplus::bus::bus& bus,
+                                           sdeventplus::Event& event) :
+    bus(bus),
+    event(event),
+    warningMatch(bus,
+                 "type='signal',member='PropertiesChanged',"
+                 "path_namespace='/xyz/openbmc_project/sensors',"
+                 "arg0='" +
+                     warningInterface + "'",
+                 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
+                           std::placeholders::_1)),
+    criticalMatch(bus,
+                  "type='signal',member='PropertiesChanged',"
+                  "path_namespace='/xyz/openbmc_project/sensors',"
+                  "arg0='" +
+                      criticalInterface + "'",
+                  std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
+                            std::placeholders::_1)),
+    perfLossMatch(bus,
+                  "type='signal',member='PropertiesChanged',"
+                  "path_namespace='/xyz/openbmc_project/sensors',"
+                  "arg0='" +
+                      perfLossInterface + "'",
+                  std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
+                            std::placeholders::_1))
+{}
+
+void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message::message& msg)
+{
+    // TODO
+}
+
+} // namespace sensor::monitor
diff --git a/sensor-monitor/threshold_alarm_logger.hpp b/sensor-monitor/threshold_alarm_logger.hpp
new file mode 100644
index 0000000..89e460d
--- /dev/null
+++ b/sensor-monitor/threshold_alarm_logger.hpp
@@ -0,0 +1,97 @@
+/**
+ * Copyright © 2021 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/bus/match.hpp>
+#include <sdeventplus/event.hpp>
+
+namespace sensor::monitor
+{
+
+/**
+ * @class ThresholdAlarmLogger
+ *
+ * This class watches the threshold interfaces
+ *    openbmc_project.Sensor.Threshold.Warning
+ *    openbmc_project.Sensor.Threshold.Critical
+ *    openbmc_project.Sensor.Threshold.PerformanceLoss
+ *
+ * and creates event logs when their high and low alarm
+ * properties set and clear.  The error names of the
+ * event logs are based on the sensor type and look like:
+ *
+ * xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHigh
+ * xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHighClear
+ */
+class ThresholdAlarmLogger
+{
+  public:
+    ThresholdAlarmLogger() = delete;
+    ~ThresholdAlarmLogger() = default;
+    ThresholdAlarmLogger(const ThresholdAlarmLogger&) = default;
+    ThresholdAlarmLogger& operator=(const ThresholdAlarmLogger&) = default;
+    ThresholdAlarmLogger(ThresholdAlarmLogger&&) = default;
+    ThresholdAlarmLogger& operator=(ThresholdAlarmLogger&&) = default;
+
+    /**
+     * @brief Constructor
+     *
+     * Looks for existing active threshold alarms.
+     *
+     * @param[in] bus - The sdbusplus bus object
+     * @param[in] event - The sdeventplus event object
+     */
+    ThresholdAlarmLogger(sdbusplus::bus::bus& bus, sdeventplus::Event& event);
+
+  private:
+    /**
+     * @brief The propertiesChanged handler for all of the thresholds
+     *        interfaces.
+     *
+     * Creates event logs for high/low alarm sets and clears.
+     *
+     * @param[in] msg - The signal message payload.
+     */
+    void propertiesChanged(sdbusplus::message::message& msg);
+
+    /**
+     * @brief The sdbusplus bus object
+     */
+    sdbusplus::bus::bus& bus;
+
+    /**
+     * @brief The sdeventplus Event object
+     */
+    sdeventplus::Event& event;
+
+    /**
+     * @brief The Warning interface match object
+     */
+    sdbusplus::bus::match::match warningMatch;
+
+    /**
+     * @brief The Critical interface match object
+     */
+    sdbusplus::bus::match::match criticalMatch;
+
+    /**
+     * @brief The PerformanceLoss interface match object
+     */
+    sdbusplus::bus::match::match perfLossMatch;
+};
+
+} // namespace sensor::monitor