pinhole: move power policy service to utils

Move the getProperty() function to utils and use it for all utility
functions

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I3128d6006dc5f72a579daaf168b9976ee5bcb2e8
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index f638561..29abdc6 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -2,6 +2,7 @@
 
 #include "host_state_manager.hpp"
 #include "settings.hpp"
+#include "utils.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
 #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
 
@@ -30,94 +31,6 @@
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
 
-constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
-constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
-constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
-
-constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
-
-std::string getService(sdbusplus::bus::bus& bus, std::string path,
-                       std::string interface)
-{
-    auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
-                                      MAPPER_INTERFACE, "GetObject");
-
-    mapper.append(path, std::vector<std::string>({interface}));
-
-    std::map<std::string, std::vector<std::string>> mapperResponse;
-    try
-    {
-        auto mapperResponseMsg = bus.call(mapper);
-
-        mapperResponseMsg.read(mapperResponse);
-        if (mapperResponse.empty())
-        {
-            error("Mapper response empty, does not have path {PATH} and "
-                  "interface {INTERFACE}",
-                  "PATH", path, "INTERFACE", interface);
-            throw std::runtime_error("Error reading mapper response");
-        }
-    }
-    catch (const sdbusplus::exception::exception& e)
-    {
-        error("Error in mapper call for path {PATH} and interface {INTERFACE} "
-              "with error {ERROR}",
-              "PATH", path, "INTERFACE", interface, "ERROR", e);
-        throw;
-    }
-
-    return mapperResponse.begin()->first;
-}
-
-std::string getProperty(sdbusplus::bus::bus& bus, std::string path,
-                        std::string interface, std::string propertyName)
-{
-    std::variant<std::string> property;
-    std::string service = getService(bus, path, interface);
-
-    auto method = bus.new_method_call(service.c_str(), path.c_str(),
-                                      PROPERTY_INTERFACE, "Get");
-
-    method.append(interface, propertyName);
-
-    try
-    {
-        auto reply = bus.call(method);
-        reply.read(property);
-    }
-    catch (const sdbusplus::exception::exception& e)
-    {
-        error("Error in property Get, error {ERROR}, property {PROPERTY}",
-              "ERROR", e, "PROPERTY", propertyName);
-        throw;
-    }
-
-    if (std::get<std::string>(property).empty())
-    {
-        error("Error reading property response for {PROPERTY}", "PROPERTY",
-              propertyName);
-        throw std::runtime_error("Error reading property response");
-    }
-
-    return std::get<std::string>(property);
-}
-
-void setProperty(sdbusplus::bus::bus& bus, const std::string& path,
-                 const std::string& interface, const std::string& property,
-                 const std::string& value)
-{
-    std::variant<std::string> variantValue = value;
-    std::string service = getService(bus, path, interface);
-
-    auto method = bus.new_method_call(service.c_str(), path.c_str(),
-                                      PROPERTY_INTERFACE, "Set");
-
-    method.append(interface, property, variantValue);
-    bus.call_noreply(method);
-
-    return;
-}
-
 } // namespace manager
 } // namespace state
 } // namespace phosphor
@@ -194,9 +107,10 @@
             // one_time setting was set so we're going to use it. Reset it
             // to default for next time.
             info("One time set, use it and reset to default");
-            setProperty(bus, settings.powerRestorePolicyOneTime.c_str(),
-                        powerRestoreIntf, "PowerRestorePolicy",
-                        convertForMessage(RestorePolicy::Policy::None));
+            phosphor::state::manager::utils::setProperty(
+                bus, settings.powerRestorePolicyOneTime.c_str(),
+                powerRestoreIntf, "PowerRestorePolicy",
+                convertForMessage(RestorePolicy::Policy::None));
         }
 
         info("Host power is off, processing power policy {POWER_POLICY}",
@@ -206,11 +120,13 @@
             RestorePolicy::convertPolicyFromString(powerPolicy))
         {
             info("power_policy=ALWAYS_POWER_ON, powering host on");
-            setProperty(bus, hostPath, HOST_BUSNAME, "RestartCause",
-                        convertForMessage(
-                            server::Host::RestartCause::PowerPolicyAlwaysOn));
-            setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
-                        convertForMessage(server::Host::Transition::On));
+            phosphor::state::manager::utils::setProperty(
+                bus, hostPath, HOST_BUSNAME, "RestartCause",
+                convertForMessage(
+                    server::Host::RestartCause::PowerPolicyAlwaysOn));
+            phosphor::state::manager::utils::setProperty(
+                bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
+                convertForMessage(server::Host::Transition::On));
         }
         else if (RestorePolicy::Policy::AlwaysOff ==
                  RestorePolicy::convertPolicyFromString(powerPolicy))
@@ -223,15 +139,16 @@
                  RestorePolicy::convertPolicyFromString(powerPolicy))
         {
             info("power_policy=RESTORE, restoring last state");
-            setProperty(
+            phosphor::state::manager::utils::setProperty(
                 bus, hostPath, HOST_BUSNAME, "RestartCause",
                 convertForMessage(
                     server::Host::RestartCause::PowerPolicyPreviousState));
             // Read last requested state and re-request it to execute it
-            auto hostReqState = getProperty(bus, hostPath, HOST_BUSNAME,
-                                            "RequestedHostTransition");
-            setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
-                        hostReqState);
+            auto hostReqState = phosphor::state::manager::utils::getProperty(
+                bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
+            phosphor::state::manager::utils::setProperty(
+                bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
+                hostReqState);
         }
     }
     catch (const sdbusplus::exception::exception& e)
diff --git a/meson.build b/meson.build
index 1ac11e8..9dad57f 100644
--- a/meson.build
+++ b/meson.build
@@ -124,8 +124,9 @@
 executable('phosphor-discover-system-state',
             'discover_system_state.cpp',
             'settings.cpp',
+            'utils.cpp',
             dependencies: [
-            sdbusplus, phosphorlogging
+            sdbusplus, phosphorlogging, libgpiod
             ],
     implicit_include_directories: true,
     install: true
diff --git a/utils.cpp b/utils.cpp
index a93fa39..1e5e9e2 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -55,6 +55,40 @@
     return mapperResponse.begin()->first;
 }
 
+std::string getProperty(sdbusplus::bus::bus& bus, const std::string& path,
+                        const std::string& interface,
+                        const std::string& propertyName)
+{
+    std::variant<std::string> property;
+    std::string service = getService(bus, path, interface);
+
+    auto method = bus.new_method_call(service.c_str(), path.c_str(),
+                                      PROPERTY_INTERFACE, "Get");
+
+    method.append(interface, propertyName);
+
+    try
+    {
+        auto reply = bus.call(method);
+        reply.read(property);
+    }
+    catch (const sdbusplus::exception::exception& e)
+    {
+        error("Error in property Get, error {ERROR}, property {PROPERTY}",
+              "ERROR", e, "PROPERTY", propertyName);
+        throw;
+    }
+
+    if (std::get<std::string>(property).empty())
+    {
+        error("Error reading property response for {PROPERTY}", "PROPERTY",
+              propertyName);
+        throw std::runtime_error("Error reading property response");
+    }
+
+    return std::get<std::string>(property);
+}
+
 void setProperty(sdbusplus::bus::bus& bus, const std::string& path,
                  const std::string& interface, const std::string& property,
                  const std::string& value)
diff --git a/utils.hpp b/utils.hpp
index 004d97a..410f972 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -23,6 +23,19 @@
 std::string getService(sdbusplus::bus::bus& bus, std::string path,
                        std::string interface);
 
+/** @brief Get the value of input property
+ *
+ * @param[in] bus          - The Dbus bus object
+ * @param[in] path         - The Dbus object path
+ * @param[in] interface    - The Dbus interface
+ * @param[in] property     - The property name to get
+ *
+ * @return The value of the property
+ */
+std::string getProperty(sdbusplus::bus::bus& bus, const std::string& path,
+                        const std::string& interface,
+                        const std::string& propertyName);
+
 /** @brief Set the value of property
  *
  * @param[in] bus          - The Dbus bus object