Move PCH Thermtrip monitor to the new architecture

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

Change-Id: If285358b9997d0799a46e5a51f205a3421c82b24
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/include/error_monitors/pch_thermtrip_monitor.hpp b/include/error_monitors/pch_thermtrip_monitor.hpp
new file mode 100644
index 0000000..d439efe
--- /dev/null
+++ b/include/error_monitors/pch_thermtrip_monitor.hpp
@@ -0,0 +1,102 @@
+/*
+// 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::pch_thermtrip_monitor
+{
+class PCHThermtripMonitor :
+    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;
+
+    std::shared_ptr<sdbusplus::asio::dbus_interface> associationPCHThermtrip;
+    static const constexpr char* callbackMgrPath =
+        "/xyz/openbmc_project/CallbackManager";
+
+    void logEvent() override
+    {
+        sd_journal_send("MESSAGE=HostError: SSB thermal trip", "PRIORITY=%i",
+                        LOG_INFO, "REDFISH_MESSAGE_ID=%s",
+                        "OpenBMC.0.1.SsbThermalTrip", NULL);
+    }
+
+    void assertHandler() override
+    {
+        host_error_monitor::base_gpio_monitor::BaseGPIOMonitor::assertHandler();
+
+        setLED();
+    }
+
+    void deassertHandler() override
+    {
+        host_error_monitor::base_gpio_monitor::BaseGPIOMonitor::
+            deassertHandler();
+
+        unsetLED();
+    }
+
+    void setLED()
+    {
+        std::vector<Association> associations;
+
+        associations.emplace_back(
+            "", "critical",
+            "/xyz/openbmc_project/host_error_monitor/ssb_thermal_trip");
+        associations.emplace_back("", "critical", callbackMgrPath);
+
+        associationPCHThermtrip->set_property("Associations", associations);
+    }
+
+    void unsetLED()
+    {
+        std::vector<Association> associations;
+
+        associations.emplace_back("", "", "");
+
+        associationPCHThermtrip->set_property("Associations", associations);
+    }
+
+  public:
+    PCHThermtripMonitor(boost::asio::io_service& io,
+                        std::shared_ptr<sdbusplus::asio::connection> conn,
+                        const std::string& signalName) :
+        BaseGPIOMonitor(io, conn, signalName, assertValue)
+    {
+        // Associations interface for led status
+        std::vector<host_error_monitor::Association> associations;
+        associations.emplace_back("", "", "");
+
+        sdbusplus::asio::object_server server =
+            sdbusplus::asio::object_server(conn);
+        associationPCHThermtrip = server.add_interface(
+            "/xyz/openbmc_project/host_error_monitor/ssb_thermal_trip",
+            "xyz.openbmc_project.Association.Definitions");
+        associationPCHThermtrip->register_property("Associations",
+                                                   associations);
+        associationPCHThermtrip->initialize();
+
+        if (valid)
+        {
+            startMonitoring();
+        }
+    }
+};
+} // namespace host_error_monitor::pch_thermtrip_monitor
diff --git a/src/host_error_monitor.cpp b/src/host_error_monitor.cpp
index 61c9943..0fe5b30 100644
--- a/src/host_error_monitor.cpp
+++ b/src/host_error_monitor.cpp
@@ -33,30 +33,12 @@
 static boost::asio::io_service io;
 static std::shared_ptr<sdbusplus::asio::connection> conn;
 
-static std::shared_ptr<sdbusplus::asio::dbus_interface> associationSSBThermTrip;
-
-static const constexpr char* rootPath = "/xyz/openbmc_project/CallbackManager";
-
 static bool hostOff = true;
 bool hostIsOff()
 {
     return hostOff;
 }
 
-// GPIO Lines and Event Descriptors
-//----------------------------------
-// PCH_BMC_THERMTRIP function related definition
-//----------------------------------
-static gpiod::line pchThermtripLine;
-static boost::asio::posix::stream_descriptor pchThermtripEvent(io);
-
-static void ssbThermTripLog()
-{
-    sd_journal_send("MESSAGE=HostError: SSB thermal trip", "PRIORITY=%i",
-                    LOG_INFO, "REDFISH_MESSAGE_ID=%s",
-                    "OpenBMC.0.1.SsbThermalTrip", NULL);
-}
-
 static void initializeErrorState();
 static void init()
 {
@@ -207,57 +189,8 @@
     return true;
 }
 
-static void pchThermtripHandler()
-{
-    std::vector<Association> associations;
-
-    gpiod::line_event gpioLineEvent = pchThermtripLine.event_read();
-
-    bool pchThermtrip =
-        gpioLineEvent.event_type == gpiod::line_event::FALLING_EDGE;
-    if (pchThermtrip)
-    {
-        ssbThermTripLog();
-        associations.emplace_back(
-            "", "critical",
-            "/xyz/openbmc_project/host_error_monitor/ssb_thermal_trip");
-        associations.emplace_back("", "critical", host_error_monitor::rootPath);
-    }
-    else
-    {
-        associations.emplace_back("", "", "");
-    }
-    host_error_monitor::associationSSBThermTrip->set_property("Associations",
-                                                              associations);
-
-    pchThermtripEvent.async_wait(
-        boost::asio::posix::stream_descriptor::wait_read,
-        [](const boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "PCH Thermal trip handler error: " << ec.message()
-                          << "\n";
-                return;
-            }
-            pchThermtripHandler();
-        });
-}
-
 static void initializeErrorState()
-{
-    // Handle PCH_BMC_THERMTRIP if it's asserted now
-    if (pchThermtripLine.get_value() == 0)
-    {
-        ssbThermTripLog();
-        std::vector<Association> associations;
-        associations.emplace_back(
-            "", "critical",
-            "/xyz/openbmc_project/host_error_monitor/ssb_thermal_trip");
-        associations.emplace_back("", "critical", host_error_monitor::rootPath);
-        host_error_monitor::associationSSBThermTrip->set_property(
-            "Associations", associations);
-    }
-}
+{}
 } // namespace host_error_monitor
 
 int main(int argc, char* argv[])
@@ -272,29 +205,10 @@
     sdbusplus::asio::object_server server =
         sdbusplus::asio::object_server(host_error_monitor::conn);
 
-    // Associations interface for led status
-    std::vector<host_error_monitor::Association> associations;
-    associations.emplace_back("", "", "");
-    host_error_monitor::associationSSBThermTrip = server.add_interface(
-        "/xyz/openbmc_project/host_error_monitor/ssb_thermal_trip",
-        "xyz.openbmc_project.Association.Definitions");
-    host_error_monitor::associationSSBThermTrip->register_property(
-        "Associations", associations);
-    host_error_monitor::associationSSBThermTrip->initialize();
-
     // Start tracking host state
     std::shared_ptr<sdbusplus::bus::match::match> hostStateMonitor =
         host_error_monitor::startHostStateMonitor();
 
-    // Request PCH_BMC_THERMTRIP GPIO events
-    if (!host_error_monitor::requestGPIOEvents(
-            "PCH_BMC_THERMTRIP", host_error_monitor::pchThermtripHandler,
-            host_error_monitor::pchThermtripLine,
-            host_error_monitor::pchThermtripEvent))
-    {
-        return -1;
-    }
-
     // Initialize the signal monitors
     host_error_monitor::init();