watchdog: Collect hostboot dump when watchdog times out

The hostboot dump collection to be initiated by watchdog_timeout
is disabled by default. When watchdog times out, only error
message corresponding to watchdog timeout is logged. To enable
hostboot dump collection whenever watchdog times out, the meson
option 'hostboot-dump-collection' must be enabled.

Testing - with meson option 'hostboot-dump-collection' enabled:
Ran watchdog_timeout:
case-1: CurrentHostState - off, AutoReboot - false
- Verified PEL object was not created
- Verified hostboot dump was not created
- Verified the Host State changed to Quiesce

case-2: CurrentHostState - off, AutoReboot - true
- Verified PEL object was created
- Verified hostboot dump was not created
- Verified the Host State changed to Running

case-3: CurrentHostState - Running, AutoBoot - false
- Verified PEL object was not created
- Verified hostboot dump was not created
- Verified the Host State changed to Quiesce

case-4: CurrentHostState - Running, AutoBoot - true, default timeout = 300s
- Verified PEL object was created
- Verified hostboot dump was created
- Observed Host state moving to either Running or Quiesce

case-5: CurrentHostState - Running, AutoBoot - true, specified timeout = 5s
- Verified PEL object was created
- Verified hostboot dump was created
- Observed Host state moving to either Running or Quiesce

Docker Unit test: passed

Signed-off-by: Shantappa Teekappanavar <sbteeks@yahoo.com>
Change-Id: Ib92d0c2f282816fb742cf07c1cb876b2cc093c12
diff --git a/watchdog/watchdog_common.cpp b/watchdog/watchdog_common.cpp
new file mode 100644
index 0000000..f5b52ff
--- /dev/null
+++ b/watchdog/watchdog_common.cpp
@@ -0,0 +1,66 @@
+#include <libpdbg.h>
+
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+#include <watchdog_common.hpp>
+#include <watchdog_logging.hpp>
+
+#include <map>
+
+namespace watchdog
+{
+namespace dump
+{
+
+using namespace phosphor::logging;
+
+void transitionHost(const std::string& target)
+{
+    constexpr auto systemdService = "org.freedesktop.systemd1";
+    constexpr auto systemdObjPath = "/org/freedesktop/systemd1";
+    constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
+
+    auto bus = sdbusplus::bus::new_system();
+    auto method = bus.new_method_call(systemdService, systemdObjPath,
+                                      systemdInterface, "StartUnit");
+
+    method.append(target); // target unit to start
+    method.append("replace");
+
+    bus.call_noreply(method); // start the service
+}
+
+bool isAutoRebootEnabled()
+{
+    constexpr auto settingsService = "xyz.openbmc_project.Settings";
+    constexpr auto settingsPath =
+        "/xyz/openbmc_project/control/host0/auto_reboot";
+    constexpr auto settingsIntf = "org.freedesktop.DBus.Properties";
+    constexpr auto rebootPolicy =
+        "xyz.openbmc_project.Control.Boot.RebootPolicy";
+
+    auto bus = sdbusplus::bus::new_system();
+    auto method =
+        bus.new_method_call(settingsService, settingsPath, settingsIntf, "Get");
+
+    method.append(rebootPolicy);
+    method.append("AutoReboot");
+
+    bool autoReboot = false;
+    try
+    {
+        auto reply = bus.call(method);
+        std::variant<bool> result;
+        reply.read(result);
+        autoReboot = std::get<bool>(result);
+    }
+    catch (const sdbusplus::exception::SdBusError& e)
+    {
+        log<level::ERR>("Error in AutoReboot Get", entry("ERROR=%s", e.what()));
+    }
+
+    return autoReboot;
+}
+
+} // namespace dump
+} // namespace watchdog