Move ERR2 monitor to the new architecture

Add an error monitor for CPU_ERR2 that polls to check if it is
asserted through the timeout and logs the event.  If it is ever
not asserted, it will wait for an interrupt to start polling
again.

Change-Id: Ia6ed6717b40e1bdba4ae79add3c93abcc019c71f
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/include/error_monitors/err2_monitor.hpp b/include/error_monitors/err2_monitor.hpp
new file mode 100644
index 0000000..f213e6c
--- /dev/null
+++ b/include/error_monitors/err2_monitor.hpp
@@ -0,0 +1,66 @@
+/*
+// 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/err_pin_monitor.hpp>
+#include <host_error_monitor.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+#include <iostream>
+
+namespace host_error_monitor::err2_monitor
+{
+static constexpr bool debug = false;
+
+class Err2Monitor : public host_error_monitor::err_pin_monitor::ErrPinMonitor
+{
+    const static constexpr uint8_t beepCPUErr2 = 5;
+
+    void assertHandler() override
+    {
+        host_error_monitor::err_pin_monitor::ErrPinMonitor::assertHandler();
+
+        beep(conn, beepCPUErr2);
+
+        conn->async_method_call(
+            [this](boost::system::error_code ec,
+                   const std::variant<bool>& property) {
+                if (ec)
+                {
+                    return;
+                }
+                const bool* reset = std::get_if<bool>(&property);
+                if (reset == nullptr)
+                {
+                    std::cerr << "Unable to read reset on ERR2 value\n";
+                    return;
+                }
+                startCrashdumpAndRecovery(conn, *reset, "ERR2 Timeout");
+            },
+            "xyz.openbmc_project.Settings",
+            "/xyz/openbmc_project/control/processor_error_config",
+            "org.freedesktop.DBus.Properties", "Get",
+            "xyz.openbmc_project.Control.Processor.ErrConfig", "ResetOnERR2");
+    }
+
+  public:
+    Err2Monitor(boost::asio::io_service& io,
+                std::shared_ptr<sdbusplus::asio::connection> conn,
+                const std::string& signalName) :
+        host_error_monitor::err_pin_monitor::ErrPinMonitor(io, conn, signalName,
+                                                           2)
+    {}
+};
+} // namespace host_error_monitor::err2_monitor
diff --git a/include/host_error_monitor.hpp b/include/host_error_monitor.hpp
index 90a946e..e8498b1 100644
--- a/include/host_error_monitor.hpp
+++ b/include/host_error_monitor.hpp
@@ -107,4 +107,21 @@
               << "\n";
 }
 
+static void beep(std::shared_ptr<sdbusplus::asio::connection> conn,
+                 const uint8_t& beepPriority)
+{
+    conn->async_method_call(
+        [](boost::system::error_code ec) {
+            if (ec)
+            {
+                std::cerr << "beep returned error with "
+                             "async_method_call (ec = "
+                          << ec << ")\n";
+                return;
+            }
+        },
+        "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode",
+        "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority));
+}
+
 } // namespace host_error_monitor