util::dbus: Make common the hostRunningState code
Move the code for getting the host running state from attention handler
specific code to common utility code.
Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
Change-Id: I457662cc13aa4dc89b321f238cd060e7e7486d1b
diff --git a/util/dbus.cpp b/util/dbus.cpp
index e9526df..635a73c 100644
--- a/util/dbus.cpp
+++ b/util/dbus.cpp
@@ -1,5 +1,6 @@
#include <util/dbus.hpp>
#include <util/trace.hpp>
+#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
namespace util
{
@@ -225,6 +226,53 @@
return autoReboot;
}
+/** @brief Get the running state of the host */
+HostRunningState hostRunningState()
+{
+ // assume not able to get host running state
+ HostRunningState host = HostRunningState::Unknown;
+
+ constexpr auto interface = "xyz.openbmc_project.State.Boot.Progress";
+
+ DBusService service;
+ DBusPath path;
+
+ // find a dbus object and path that implements the interface
+ if (0 == find(interface, path, service))
+ {
+ DBusValue value;
+
+ // boot progress is implemented as a property
+ constexpr auto property = "BootProgress";
+
+ if (0 == getProperty(interface, path, service, property, value))
+ {
+ // return value is a variant, progress is in the vector of strings
+ std::string bootProgress(std::get<std::string>(value));
+
+ // convert boot progress to host state
+ using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::
+ server::Progress::ProgressStages;
+
+ BootProgress stage = sdbusplus::xyz::openbmc_project::State::Boot::
+ server::Progress::convertProgressStagesFromString(bootProgress);
+
+ if ((stage == BootProgress::SystemInitComplete) ||
+ (stage == BootProgress::OSStart) ||
+ (stage == BootProgress::OSRunning))
+ {
+ host = HostRunningState::Started;
+ }
+ else
+ {
+ host = HostRunningState::NotStarted;
+ }
+ }
+ }
+
+ return host;
+}
+
} // namespace dbus
} // namespace util
diff --git a/util/dbus.hpp b/util/dbus.hpp
index d1e152f..aaf3cdc 100644
--- a/util/dbus.hpp
+++ b/util/dbus.hpp
@@ -64,7 +64,7 @@
*/
std::vector<std::string> systemNames();
-/** @brief Host states for util::dbus host state operations */
+/** @brief Host transition states for host transition operations */
enum class HostState
{
Quiesce,
@@ -89,6 +89,24 @@
*/
bool autoRebootEnabled();
+/** @brief Host running states for host running operations */
+enum class HostRunningState
+{
+ Unknown,
+ NotStarted,
+ Started
+};
+
+/**
+ * Get the host running state
+ *
+ * Use host boot progress to determine if a host has been started. If host
+ * boot progress can not be determined then host state will be unknown.
+ *
+ * @return HostType == "Unknown", "Started or "NotStarted"
+ */
+HostRunningState hostRunningState();
+
} // namespace dbus
} // namespace util