Move common functions to host_error_monitor.hpp

As part of the new architecture, signals will be split out to
individual files, so move common functions into a shared
header file.

Change-Id: I17399fc818f6d8c07d60b5e910740fe923d93972
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/include/host_error_monitor.hpp b/include/host_error_monitor.hpp
new file mode 100644
index 0000000..eee1fab
--- /dev/null
+++ b/include/host_error_monitor.hpp
@@ -0,0 +1,95 @@
+/*
+// 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 <sdbusplus/asio/object_server.hpp>
+
+#include <iostream>
+
+namespace host_error_monitor
+{
+bool hostIsOff();
+
+void startPowerCycle(std::shared_ptr<sdbusplus::asio::connection> conn)
+{
+    conn->async_method_call(
+        [](boost::system::error_code ec) {
+            if (ec)
+            {
+                std::cerr << "failed to set Chassis State\n";
+            }
+        },
+        "xyz.openbmc_project.State.Chassis",
+        "/xyz/openbmc_project/state/chassis0",
+        "org.freedesktop.DBus.Properties", "Set",
+        "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
+        std::variant<std::string>{
+            "xyz.openbmc_project.State.Chassis.Transition.PowerCycle"});
+}
+
+void startWarmReset(std::shared_ptr<sdbusplus::asio::connection> conn)
+{
+    conn->async_method_call(
+        [](boost::system::error_code ec) {
+            if (ec)
+            {
+                std::cerr << "failed to set Host State\n";
+            }
+        },
+        "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
+        "org.freedesktop.DBus.Properties", "Set",
+        "xyz.openbmc_project.State.Host", "RequestedHostTransition",
+        std::variant<std::string>{
+            "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot"});
+}
+
+void startCrashdumpAndRecovery(
+    std::shared_ptr<sdbusplus::asio::connection> conn, bool recoverSystem,
+    const std::string& triggerType)
+{
+    static bool recover;
+    recover = recoverSystem;
+    std::cerr << "Starting crashdump\n";
+    static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch;
+
+    if (!crashdumpCompleteMatch)
+    {
+        crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>(
+            *conn,
+            "type='signal',interface='com.intel.crashdump.Stored',member='"
+            "CrashdumpComplete'",
+            [conn](sdbusplus::message::message& msg) {
+                std::cerr << "Crashdump completed\n";
+                if (recover)
+                {
+                    std::cerr << "Recovering the system\n";
+                    startWarmReset(conn);
+                }
+                crashdumpCompleteMatch.reset();
+            });
+    }
+
+    conn->async_method_call(
+        [](boost::system::error_code ec) {
+            if (ec)
+            {
+                std::cerr << "failed to start Crashdump\n";
+            }
+        },
+        "com.intel.crashdump", "/com/intel/crashdump",
+        "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType);
+}
+
+} // namespace host_error_monitor
diff --git a/src/host_error_monitor.cpp b/src/host_error_monitor.cpp
index d52a5dc..7937d9b 100644
--- a/src/host_error_monitor.cpp
+++ b/src/host_error_monitor.cpp
@@ -20,6 +20,7 @@
 #include <boost/asio/posix/stream_descriptor.hpp>
 #include <boost/asio/steady_timer.hpp>
 #include <gpiod.hpp>
+#include <host_error_monitor.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 
 #include <bitset>
@@ -39,6 +40,10 @@
 static const constexpr char* rootPath = "/xyz/openbmc_project/CallbackManager";
 
 static bool hostOff = true;
+bool hostIsOff()
+{
+    return hostOff;
+}
 
 static size_t caterrTimeoutMs = 2000;
 const static constexpr size_t caterrTimeoutMsMax = 600000; // 10 minutes maximum
@@ -398,70 +403,6 @@
     return true;
 }
 
-static void startPowerCycle()
-{
-    conn->async_method_call(
-        [](boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "failed to set Chassis State\n";
-            }
-        },
-        "xyz.openbmc_project.State.Chassis",
-        "/xyz/openbmc_project/state/chassis0",
-        "org.freedesktop.DBus.Properties", "Set",
-        "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
-        std::variant<std::string>{
-            "xyz.openbmc_project.State.Chassis.Transition.PowerCycle"});
-}
-
-static void startWarmReset()
-{
-    conn->async_method_call(
-        [](boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "failed to set Host State\n";
-            }
-        },
-        "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
-        "org.freedesktop.DBus.Properties", "Set",
-        "xyz.openbmc_project.State.Host", "RequestedHostTransition",
-        std::variant<std::string>{
-            "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot"});
-}
-
-static void startCrashdumpAndRecovery(bool recoverSystem,
-                                      const std::string& triggerType)
-{
-    std::cerr << "Starting crashdump\n";
-    static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch;
-
-    crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>(
-        *conn,
-        "type='signal',interface='com.intel.crashdump.Stored',member='"
-        "CrashdumpComplete'",
-        [recoverSystem](sdbusplus::message::message& msg) {
-            std::cerr << "Crashdump completed\n";
-            if (recoverSystem)
-            {
-                std::cerr << "Recovering the system\n";
-                startWarmReset();
-            }
-            crashdumpCompleteMatch.reset();
-        });
-
-    conn->async_method_call(
-        [](boost::system::error_code ec) {
-            if (ec)
-            {
-                std::cerr << "failed to start Crashdump\n";
-            }
-        },
-        "com.intel.crashdump", "/com/intel/crashdump",
-        "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType);
-}
-
 static void incrementCPUErrorCount(int cpuNum)
 {
     std::string propertyName = "ErrorCountCPU" + std::to_string(cpuNum + 1);
@@ -761,7 +702,7 @@
                     std::cerr << "Unable to read reset on CATERR value\n";
                     return;
                 }
-                startCrashdumpAndRecovery(*reset, "IERR");
+                startCrashdumpAndRecovery(conn, *reset, "IERR");
             },
             "xyz.openbmc_project.Settings",
             "/xyz/openbmc_project/control/processor_error_config",
@@ -1341,7 +1282,7 @@
                     std::cerr << "Unable to read reset on ERR2 value\n";
                     return;
                 }
-                startCrashdumpAndRecovery(*reset, "ERR2 Timeout");
+                startCrashdumpAndRecovery(conn, *reset, "ERR2 Timeout");
             },
             "xyz.openbmc_project.Settings",
             "/xyz/openbmc_project/control/processor_error_config",
@@ -1418,7 +1359,7 @@
                 if (*reset)
                 {
                     std::cerr << "Recovering the system\n";
-                    startWarmReset();
+                    startWarmReset(conn);
                 }
 #endif
             },