Move Memory Thermtip monitor to the new architecture

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

Change-Id: Iac2463808864fd9fb0c1ad921b758cd2758d8463
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/include/error_monitors/mem_thermtrip_monitor.hpp b/include/error_monitors/mem_thermtrip_monitor.hpp
new file mode 100644
index 0000000..7d87fe1
--- /dev/null
+++ b/include/error_monitors/mem_thermtrip_monitor.hpp
@@ -0,0 +1,55 @@
+/*
+// 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::mem_thermtrip_monitor
+{
+class MemThermtripMonitor :
+    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;
+
+    void logEvent() override
+    {
+        std::string cpuNumber = "CPU " + std::to_string(cpuNum);
+        std::string msg = cpuNumber + " Memory Thermal trip.";
+
+        sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
+                        LOG_ERR, "REDFISH_MESSAGE_ID=%s",
+                        "OpenBMC.0.1.MemoryThermTrip",
+                        "REDFISH_MESSAGE_ARGS=%s", cpuNumber.c_str(), NULL);
+    }
+
+  public:
+    MemThermtripMonitor(boost::asio::io_service& io,
+                        std::shared_ptr<sdbusplus::asio::connection> conn,
+                        const std::string& signalName, const size_t cpuNum) :
+        BaseGPIOMonitor(io, conn, signalName, assertValue),
+        cpuNum(cpuNum)
+    {
+        if (valid)
+        {
+            startMonitoring();
+        }
+    }
+};
+} // namespace host_error_monitor::mem_thermtrip_monitor
diff --git a/src/host_error_monitor.cpp b/src/host_error_monitor.cpp
index 0087d82..9f36d43 100644
--- a/src/host_error_monitor.cpp
+++ b/src/host_error_monitor.cpp
@@ -61,24 +61,6 @@
 //----------------------------------
 static gpiod::line pchThermtripLine;
 static boost::asio::posix::stream_descriptor pchThermtripEvent(io);
-//----------------------------------
-// CPU_MEM_THERM_EVENT function related definition
-//----------------------------------
-static gpiod::line cpu1MemtripLine;
-static boost::asio::posix::stream_descriptor cpu1MemtripEvent(io);
-static gpiod::line cpu2MemtripLine;
-static boost::asio::posix::stream_descriptor cpu2MemtripEvent(io);
-
-static void memThermTripLog(const int cpuNum)
-{
-    std::string cpuNumber = "CPU " + std::to_string(cpuNum);
-    std::string msg = cpuNumber + " Memory Thermal trip.";
-
-    sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i",
-                    LOG_ERR, "REDFISH_MESSAGE_ID=%s",
-                    "OpenBMC.0.1.MemoryThermTrip", "REDFISH_MESSAGE_ARGS=%s",
-                    cpuNumber.c_str(), NULL);
-}
 
 static void cpuVRHotLog(const std::string& vr)
 {
@@ -247,54 +229,6 @@
     return true;
 }
 
-static void cpu1MemtripHandler()
-{
-    gpiod::line_event gpioLineEvent = cpu1MemtripLine.event_read();
-
-    bool cpu1Memtrip =
-        gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
-    if (cpu1Memtrip)
-    {
-        memThermTripLog(1);
-    }
-
-    cpu1MemtripEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "CPU 1 Memory Thermaltrip handler error: "
-                          << ec.message() << "\n";
-                return;
-            }
-            cpu1MemtripHandler();
-        });
-}
-
-static void cpu2MemtripHandler()
-{
-    gpiod::line_event gpioLineEvent = cpu2MemtripLine.event_read();
-
-    bool cpu2Memtrip =
-        gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
-    if (cpu2Memtrip)
-    {
-        memThermTripLog(2);
-    }
-
-    cpu2MemtripEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "CPU 2 Memory Thermaltrip handler error: "
-                          << ec.message() << "\n";
-                return;
-            }
-            cpu2MemtripHandler();
-        });
-}
-
 static void cpu1VRHotAssertHandler()
 {
     cpuVRHotLog("CPU 1");
@@ -505,18 +439,6 @@
 
 static void initializeErrorState()
 {
-    // Handle CPU1_MEM_THERM_EVENT (CPU1 DIMM Thermal trip) if it's asserted now
-    if (cpu1MemtripLine.get_value() == 0)
-    {
-        memThermTripLog(1);
-    }
-
-    // Handle CPU2_MEM_THERM_EVENT (CPU2 DIMM Thermal trip) if it's asserted now
-    if (cpu2MemtripLine.get_value() == 0)
-    {
-        memThermTripLog(2);
-    }
-
     // Handle CPU1_VRHOT if it's asserted now
     if (cpu1VRHotLine.get_value() == 0)
     {
@@ -657,24 +579,6 @@
         return -1;
     }
 
-    // Request CPU1_MEM_THERM_EVENT GPIO events
-    if (!host_error_monitor::requestGPIOEvents(
-            "CPU1_MEM_THERM_EVENT", host_error_monitor::cpu1MemtripHandler,
-            host_error_monitor::cpu1MemtripLine,
-            host_error_monitor::cpu1MemtripEvent))
-    {
-        return -1;
-    }
-
-    // Request CPU2_MEM_THERM_EVENT GPIO events
-    if (!host_error_monitor::requestGPIOEvents(
-            "CPU2_MEM_THERM_EVENT", host_error_monitor::cpu2MemtripHandler,
-            host_error_monitor::cpu2MemtripLine,
-            host_error_monitor::cpu2MemtripEvent))
-    {
-        return -1;
-    }
-
     // Initialize the signal monitors
     host_error_monitor::init();