Move CPU Thermtip monitor to the new architecture

Add an error monitor for CPU_THERMTRIP that checks the status on
start-up then waits for an interrupt to log the event.

Change-Id: I1fd34728cdd947ec7cad4194c06b5aa0d93e6f35
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/include/error_monitors/cpu_thermtrip_monitor.hpp b/include/error_monitors/cpu_thermtrip_monitor.hpp
new file mode 100644
index 0000000..0a1879b
--- /dev/null
+++ b/include/error_monitors/cpu_thermtrip_monitor.hpp
@@ -0,0 +1,108 @@
+/*
+// Copyright (c) 2021 Intel 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 <error_monitors/base_gpio_monitor.hpp>
+#include <host_error_monitor.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+namespace host_error_monitor::cpu_thermtrip_monitor
+{
+class CPUThermtripMonitor :
+    public host_error_monitor::base_gpio_monitor::BaseGPIOMonitor
+{
+    const static host_error_monitor::base_gpio_monitor::AssertValue
+        assertValue =
+            host_error_monitor::base_gpio_monitor::AssertValue::lowAssert;
+    size_t cpuNum;
+    gpiod::line cpuFIVRFaultLine;
+
+    void logEvent() override
+    {
+        if (cpuFIVRFaultLine.get_value() == 0)
+        {
+            cpuBootFIVRFaultLog();
+        }
+        else
+        {
+            cpuThermTripLog();
+        }
+    }
+
+    void cpuBootFIVRFaultLog()
+    {
+        std::string msg = "Boot FIVR Fault on CPU " + std::to_string(cpuNum);
+
+        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
+                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
+                        "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s",
+                        msg.c_str(), NULL);
+    }
+
+    void cpuThermTripLog()
+    {
+        std::string msg = "CPU " + std::to_string(cpuNum) + " thermal trip";
+
+        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
+                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
+                        "OpenBMC.0.1.CPUThermalTrip", "REDFISH_MESSAGE_ARGS=%d",
+                        cpuNum, NULL);
+    }
+
+    bool requestCPUFIVRFaultInput(const std::string& fivrSignalName)
+    {
+        // Find the GPIO line
+        cpuFIVRFaultLine = gpiod::find_line(fivrSignalName);
+        if (!cpuFIVRFaultLine)
+        {
+            std::cerr << "Failed to find the " << fivrSignalName << " line.\n";
+            return false;
+        }
+
+        // Request GPIO input
+        try
+        {
+            cpuFIVRFaultLine.request(
+                {"host-error-monitor", gpiod::line_request::DIRECTION_INPUT});
+        }
+        catch (std::exception&)
+        {
+            std::cerr << "Failed to request " << fivrSignalName << " input\n";
+            return false;
+        }
+
+        return true;
+    }
+
+  public:
+    CPUThermtripMonitor(boost::asio::io_service& io,
+                        std::shared_ptr<sdbusplus::asio::connection> conn,
+                        const std::string& signalName, const size_t cpuNum,
+                        const std::string& fivrSignalName) :
+        BaseGPIOMonitor(io, conn, signalName, assertValue),
+        cpuNum(cpuNum)
+    {
+        if (!requestCPUFIVRFaultInput(fivrSignalName))
+        {
+            valid = false;
+        }
+
+        if (valid)
+        {
+            startMonitoring();
+        }
+    }
+};
+} // namespace host_error_monitor::cpu_thermtrip_monitor
diff --git a/src/host_error_monitor.cpp b/src/host_error_monitor.cpp
index 02668fa..0087d82 100644
--- a/src/host_error_monitor.cpp
+++ b/src/host_error_monitor.cpp
@@ -44,12 +44,6 @@
 }
 
 // GPIO Lines and Event Descriptors
-static gpiod::line cpu1FIVRFaultLine;
-static gpiod::line cpu1ThermtripLine;
-static boost::asio::posix::stream_descriptor cpu1ThermtripEvent(io);
-static gpiod::line cpu2FIVRFaultLine;
-static gpiod::line cpu2ThermtripLine;
-static boost::asio::posix::stream_descriptor cpu2ThermtripEvent(io);
 static gpiod::line cpu1VRHotLine;
 static boost::asio::posix::stream_descriptor cpu1VRHotEvent(io);
 static gpiod::line cpu2VRHotLine;
@@ -75,25 +69,6 @@
 static gpiod::line cpu2MemtripLine;
 static boost::asio::posix::stream_descriptor cpu2MemtripEvent(io);
 
-static void cpuBootFIVRFaultLog(const int cpuNum)
-{
-    std::string msg = "Boot FIVR Fault on CPU " + std::to_string(cpuNum);
-
-    sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                    LOG_INFO, "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.CPUError",
-                    "REDFISH_MESSAGE_ARGS=%s", msg.c_str(), NULL);
-}
-
-static void cpuThermTripLog(const int cpuNum)
-{
-    std::string msg = "CPU " + std::to_string(cpuNum) + " thermal trip";
-
-    sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                    LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                    "OpenBMC.0.1.CPUThermalTrip", "REDFISH_MESSAGE_ARGS=%d",
-                    cpuNum, NULL);
-}
-
 static void memThermTripLog(const int cpuNum)
 {
     std::string cpuNumber = "CPU " + std::to_string(cpuNum);
@@ -272,42 +247,6 @@
     return true;
 }
 
-static void cpu1ThermtripAssertHandler()
-{
-    if (cpu1FIVRFaultLine.get_value() == 0)
-    {
-        cpuBootFIVRFaultLog(1);
-    }
-    else
-    {
-        cpuThermTripLog(1);
-    }
-}
-
-static void cpu1ThermtripHandler()
-{
-    gpiod::line_event gpioLineEvent = cpu1ThermtripLine.event_read();
-
-    bool cpu1Thermtrip =
-        gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
-    if (cpu1Thermtrip)
-    {
-        cpu1ThermtripAssertHandler();
-    }
-
-    cpu1ThermtripEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "CPU 1 Thermtrip handler error: " << ec.message()
-                          << "\n";
-                return;
-            }
-            cpu1ThermtripHandler();
-        });
-}
-
 static void cpu1MemtripHandler()
 {
     gpiod::line_event gpioLineEvent = cpu1MemtripLine.event_read();
@@ -332,42 +271,6 @@
         });
 }
 
-static void cpu2ThermtripAssertHandler()
-{
-    if (cpu2FIVRFaultLine.get_value() == 0)
-    {
-        cpuBootFIVRFaultLog(2);
-    }
-    else
-    {
-        cpuThermTripLog(2);
-    }
-}
-
-static void cpu2ThermtripHandler()
-{
-    gpiod::line_event gpioLineEvent = cpu2ThermtripLine.event_read();
-
-    bool cpu2Thermtrip =
-        gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
-    if (cpu2Thermtrip)
-    {
-        cpu2ThermtripAssertHandler();
-    }
-
-    cpu2ThermtripEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "CPU 2 Thermtrip handler error: " << ec.message()
-                          << "\n";
-                return;
-            }
-            cpu2ThermtripHandler();
-        });
-}
-
 static void cpu2MemtripHandler()
 {
     gpiod::line_event gpioLineEvent = cpu2MemtripLine.event_read();
@@ -602,18 +505,6 @@
 
 static void initializeErrorState()
 {
-    // Handle CPU1_THERMTRIP if it's asserted now
-    if (cpu1ThermtripLine.get_value() == 0)
-    {
-        cpu1ThermtripAssertHandler();
-    }
-
-    // Handle CPU2_THERMTRIP if it's asserted now
-    if (cpu2ThermtripLine.get_value() == 0)
-    {
-        cpu2ThermtripAssertHandler();
-    }
-
     // Handle CPU1_MEM_THERM_EVENT (CPU1 DIMM Thermal trip) if it's asserted now
     if (cpu1MemtripLine.get_value() == 0)
     {
@@ -703,38 +594,6 @@
     std::shared_ptr<sdbusplus::bus::match::match> hostStateMonitor =
         host_error_monitor::startHostStateMonitor();
 
-    // Request CPU1_FIVR_FAULT GPIO input
-    if (!host_error_monitor::requestGPIOInput(
-            "CPU1_FIVR_FAULT", host_error_monitor::cpu1FIVRFaultLine))
-    {
-        return -1;
-    }
-
-    // Request CPU1_THERMTRIP GPIO events
-    if (!host_error_monitor::requestGPIOEvents(
-            "CPU1_THERMTRIP", host_error_monitor::cpu1ThermtripHandler,
-            host_error_monitor::cpu1ThermtripLine,
-            host_error_monitor::cpu1ThermtripEvent))
-    {
-        return -1;
-    }
-
-    // Request CPU2_FIVR_FAULT GPIO input
-    if (!host_error_monitor::requestGPIOInput(
-            "CPU2_FIVR_FAULT", host_error_monitor::cpu2FIVRFaultLine))
-    {
-        return -1;
-    }
-
-    // Request CPU2_THERMTRIP GPIO events
-    if (!host_error_monitor::requestGPIOEvents(
-            "CPU2_THERMTRIP", host_error_monitor::cpu2ThermtripHandler,
-            host_error_monitor::cpu2ThermtripLine,
-            host_error_monitor::cpu2ThermtripEvent))
-    {
-        return -1;
-    }
-
     // Request CPU1_VRHOT GPIO events
     if (!host_error_monitor::requestGPIOEvents(
             "CPU1_VRHOT", host_error_monitor::cpu1VRHotHandler,