Disable host watchdog during dump collection

Host watchdog to be disabled during hostboot and hardware dump
collection.

During a checkstop attention, the system is not functioning normally. So
a hardware or hostboot dump is collected and it could take a while to
get completed.  If the watchdog timer is active during that time, it may
get triggered.  As we already know the system is not functioning
normally and are collecting dump, disable the watchdog when the dump
collection is in progress.

Tested and ensured that one dump is created during a checkstop.

From the logs was able to identify that the host watchdog is disabled
just when the dump collection starts and host watchdog resumes after the
dump collection.

It is verified for both hardware dump and hostboot dump

Change-Id: I5f9df253594f9e858ca0477acb3ce1f3a4639785
Signed-off-by: deepakala-k <deepakala.karthikeyan@ibm.com>
diff --git a/attn/attn_dump.cpp b/attn/attn_dump.cpp
index 847eb8e..167835a 100644
--- a/attn/attn_dump.cpp
+++ b/attn/attn_dump.cpp
@@ -3,6 +3,7 @@
 #include <attn/attn_logging.hpp>
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/exception.hpp>
+#include <util/dbus.hpp>
 #include <util/trace.hpp>
 
 constexpr uint64_t dumpTimeout = 3600000000; // microseconds
@@ -99,6 +100,16 @@
     trace::inf("dump status: %s", dumpStatus.c_str());
 }
 
+/** Api used to enable or disable watchdog dbus property */
+void enableWatchdog(bool enable)
+{
+    constexpr auto service = "xyz.openbmc_project.Watchdog";
+    constexpr auto object = "/xyz/openbmc_project/watchdog/host0";
+    constexpr auto interface = "xyz.openbmc_project.State.Watchdog";
+    constexpr auto property = "Enabled";
+    util::dbus::setProperty<bool>(service, object, interface, property, enable);
+}
+
 /** Request a dump from the dump manager */
 void requestDump(uint32_t i_logId, const DumpParameters& i_dumpParameters)
 {
@@ -107,11 +118,22 @@
     constexpr auto function = "CreateDump";
 
     sdbusplus::message_t method;
+    bool watchdogDisabled = false;
 
     if (0 == dbusMethod(path, interface, function, method))
     {
         try
         {
+            // During a checkstop attention, the system is not functioning
+            // normally. So a hardware or hostboot dump is collected and it
+            // could take a while to get completed. So disable the watchdog when
+            // the dump collection is in progress.
+            if (DumpType::Hostboot == i_dumpParameters.dumpType ||
+                DumpType::Hardware == i_dumpParameters.dumpType)
+            {
+                watchdogDisabled = true;
+                enableWatchdog(false);
+            }
             // dbus call arguments
             std::map<std::string, std::variant<std::string, uint64_t>>
                 createParams;
@@ -156,6 +178,12 @@
             trace::err("requestDump exception");
             trace::err(e.what());
         }
+
+        if (watchdogDisabled)
+        {
+            // Dump collection is over, enable the watchdog
+            enableWatchdog(true);
+        }
     }
 }
 
diff --git a/attn/attn_dump.hpp b/attn/attn_dump.hpp
index 289aea5..2c014a2 100644
--- a/attn/attn_dump.hpp
+++ b/attn/attn_dump.hpp
@@ -31,4 +31,13 @@
  */
 void requestDump(uint32_t i_logId, const DumpParameters& dumpParameters);
 
+/**
+ * Enable or disable host watchdog dbus property
+ *
+ * This property is used in enabling/ disabling host watchdog.
+ *
+ * @param enable        Whether to enable (True) or disable (False) the watchdog
+ */
+void enableWatchdog(bool enable);
+
 } // namespace attn
diff --git a/util/dbus.hpp b/util/dbus.hpp
index 32d1b9c..c1dc252 100644
--- a/util/dbus.hpp
+++ b/util/dbus.hpp
@@ -2,6 +2,7 @@
 
 #include <sdbusplus/bus.hpp>
 #include <util/ffdc_file.hpp>
+#include <util/trace.hpp>
 
 #include <string>
 #include <variant>
@@ -56,6 +57,31 @@
                 const std::string& i_service, const std::string& i_property,
                 DBusValue& o_response);
 
+template <typename T>
+void setProperty(const std::string& service, const std::string& object,
+                 const std::string& interface, const std::string& propertyName,
+                 const std::variant<T>& propertyValue)
+{
+    try
+    {
+        auto bus = sdbusplus::bus::new_default();
+        auto method = bus.new_method_call(service.c_str(), object.c_str(),
+                                          "org.freedesktop.DBus.Properties",
+                                          "Set");
+        method.append(interface);
+        method.append(propertyName);
+        method.append(propertyValue);
+
+        bus.call(method);
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        trace::err("util::dbus::setProperty exception");
+        std::string traceMsg = std::string(e.what());
+        trace::err(traceMsg.c_str());
+    }
+}
+
 /**
  * Get the IBM compatible names defined for this system
  *