Refactor: Move isPoweredOn to utility

The check of power on will be used by other components, so move it to
utility.
There are several constexpr definitions are used by multiple components,
move them into types.hpp so that we do not have duplicated constexprs.

Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: I42ad142d6e7ae8da9c0cf6d8f5cb21dee229eba2
diff --git a/power-supply/power_supply.cpp b/power-supply/power_supply.cpp
index 62067ef..981435f 100644
--- a/power-supply/power_supply.cpp
+++ b/power-supply/power_supply.cpp
@@ -21,6 +21,7 @@
 #include "gpio.hpp"
 #include "names_values.hpp"
 #include "pmbus.hpp"
+#include "types.hpp"
 #include "utility.hpp"
 
 #include <functional>
@@ -41,25 +42,6 @@
 using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
 namespace version = sdbusplus::xyz::openbmc_project::Software::server;
 
-constexpr auto ASSOCIATION_IFACE = "xyz.openbmc_project.Association";
-constexpr auto LOGGING_IFACE = "xyz.openbmc_project.Logging.Entry";
-constexpr auto INVENTORY_IFACE = "xyz.openbmc_project.Inventory.Item";
-constexpr auto POWER_IFACE = "org.openbmc.control.Power";
-constexpr auto INVENTORY_MGR_IFACE = "xyz.openbmc_project.Inventory.Manager";
-constexpr auto ASSET_IFACE = "xyz.openbmc_project.Inventory.Decorator.Asset";
-constexpr auto VERSION_IFACE = "xyz.openbmc_project.Software.Version";
-
-constexpr auto ENDPOINTS_PROP = "endpoints";
-constexpr auto MESSAGE_PROP = "Message";
-constexpr auto RESOLVED_PROP = "Resolved";
-constexpr auto PRESENT_PROP = "Present";
-constexpr auto VERSION_PURPOSE_PROP = "Purpose";
-
-constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
-constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
-
-constexpr auto INPUT_HISTORY = "input_history";
-
 PowerSupply::PowerSupply(const std::string& name, size_t inst,
                          const std::string& objpath, const std::string& invpath,
                          sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
@@ -261,31 +243,7 @@
 
 void PowerSupply::updatePowerState()
 {
-    // When state = 1, system is powered on
-    int32_t state = 0;
-
-    try
-    {
-        auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
-
-        // Use getProperty utility function to get power state.
-        util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
-                                   service, bus, state);
-
-        if (state)
-        {
-            powerOn = true;
-        }
-        else
-        {
-            powerOn = false;
-        }
-    }
-    catch (std::exception& e)
-    {
-        log<level::INFO>("Failed to get power state. Assuming it is off.");
-        powerOn = false;
-    }
+    powerOn = util::isPoweredOn(bus);
 }
 
 void PowerSupply::checkInputFault(const uint16_t statusWord)
diff --git a/tools/power-utils/updater.cpp b/tools/power-utils/updater.cpp
index 41aa3eb..024fabc 100644
--- a/tools/power-utils/updater.cpp
+++ b/tools/power-utils/updater.cpp
@@ -17,6 +17,7 @@
 
 #include "updater.hpp"
 
+#include "types.hpp"
 #include "utility.hpp"
 
 #include <fstream>
@@ -28,9 +29,6 @@
 namespace updater
 {
 
-constexpr auto INVENTORY_IFACE = "xyz.openbmc_project.Inventory.Item";
-constexpr auto PRESENT_PROP = "Present";
-
 namespace internal
 {
 
diff --git a/types.hpp b/types.hpp
new file mode 100644
index 0000000..90b5c6a
--- /dev/null
+++ b/types.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+/* const expressions shared in this repository */
+
+constexpr auto ASSOCIATION_IFACE = "xyz.openbmc_project.Association";
+constexpr auto LOGGING_IFACE = "xyz.openbmc_project.Logging.Entry";
+constexpr auto INVENTORY_IFACE = "xyz.openbmc_project.Inventory.Item";
+constexpr auto POWER_IFACE = "org.openbmc.control.Power";
+constexpr auto INVENTORY_MGR_IFACE = "xyz.openbmc_project.Inventory.Manager";
+constexpr auto ASSET_IFACE = "xyz.openbmc_project.Inventory.Decorator.Asset";
+constexpr auto VERSION_IFACE = "xyz.openbmc_project.Software.Version";
+
+constexpr auto ENDPOINTS_PROP = "endpoints";
+constexpr auto MESSAGE_PROP = "Message";
+constexpr auto RESOLVED_PROP = "Resolved";
+constexpr auto PRESENT_PROP = "Present";
+constexpr auto VERSION_PURPOSE_PROP = "Purpose";
+
+constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
+constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
+
+constexpr auto INPUT_HISTORY = "input_history";
diff --git a/utility.cpp b/utility.cpp
index bd84249..680b89e 100644
--- a/utility.cpp
+++ b/utility.cpp
@@ -15,6 +15,8 @@
  */
 #include "utility.hpp"
 
+#include "types.hpp"
+
 #include <fstream>
 
 namespace phosphor
@@ -103,6 +105,24 @@
     return type;
 }
 
+bool isPoweredOn(sdbusplus::bus::bus& bus)
+{
+    // When state = 1, system is powered on
+    int32_t state = 0;
+
+    try
+    {
+        auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
+        getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
+                             state);
+    }
+    catch (std::exception& e)
+    {
+        log<level::INFO>("Failed to get power state. Assuming it is off.");
+    }
+    return state != 0;
+}
+
 } // namespace util
 } // namespace power
 } // namespace phosphor
diff --git a/utility.hpp b/utility.hpp
index f6c40d8..ffc489c 100644
--- a/utility.hpp
+++ b/utility.hpp
@@ -125,6 +125,13 @@
  */
 phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
 
+/**
+ * Check if power is on
+ *
+ * @return true if power is on, otherwise false
+ */
+bool isPoweredOn(sdbusplus::bus::bus& bus);
+
 } // namespace util
 } // namespace power
 } // namespace phosphor