host: power_restore: switch to new settings API

Change-Id: Ie44e421ad690527d1883f03dcde9d499282ecacc
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index e392086..a0d5d10 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -22,7 +22,8 @@
 	bmc_state_manager_main.cpp
 
 phosphor_discover_system_state_SOURCES = \
-	discover_system_state.cpp
+	discover_system_state.cpp \
+	settings.cpp
 
 phosphor_host_check_SOURCES = \
 	host_check_main.cpp
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index 3989b3f..c055f9f 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -5,8 +5,12 @@
 #include <systemd/sd-bus.h>
 #include <sdbusplus/server.hpp>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog-errors.hpp>
 #include "chassis_state_manager.hpp"
 #include "host_state_manager.hpp"
+#include "settings.hpp"
+#include "xyz/openbmc_project/Common/error.hpp"
+#include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
 
 namespace phosphor
 {
@@ -16,6 +20,8 @@
 {
 
 using namespace phosphor::logging;
+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";
@@ -25,9 +31,6 @@
 
 constexpr auto HOST_PATH = "/xyz/openbmc_project/state/host0";
 
-constexpr auto SETTINGS_PATH = "/org/openbmc/settings/host0";
-constexpr auto SETTINGS_INTERFACE = "org.openbmc.settings.Host";
-
 constexpr auto CHASSIS_PATH = "/xyz/openbmc_project/state/chassis0";
 
 std::string getService(sdbusplus::bus::bus& bus, std::string path,
@@ -118,8 +121,13 @@
 
 int main()
 {
+    using namespace phosphor::logging;
+
     auto bus = sdbusplus::bus::new_default();
 
+    using namespace settings;
+    Objects settings(bus);
+
     using namespace phosphor::state::manager;
     namespace server = sdbusplus::xyz::openbmc_project::State::server;
 
@@ -129,14 +137,30 @@
 
     if(currentPowerState == convertForMessage(server::Chassis::PowerState::Off))
     {
-        std::string power_policy = getProperty(bus, SETTINGS_PATH,
-                                               SETTINGS_INTERFACE,
-                                               "power_policy");
+        auto method =
+            bus.new_method_call(
+                    settings.service(settings.powerRestorePolicy,
+                        powerRestoreIntf).c_str(),
+                    settings.powerRestorePolicy.c_str(),
+                    "org.freedesktop.DBus.Properties",
+                    "Get");
+        method.append(powerRestoreIntf, "PowerRestorePolicy");
+        auto reply = bus.call(method);
+        if (reply.is_method_error())
+        {
+            log<level::ERR>("Error in PowerRestorePolicy Get");
+            elog<InternalFailure>();
+        }
+
+        sdbusplus::message::variant<std::string> result;
+        reply.read(result);
+        auto powerPolicy = result.get<std::string>();
 
         log<level::INFO>("Host power is off, checking power policy",
-                         entry("POWER_POLICY=%s", power_policy.c_str()));
+                         entry("POWER_POLICY=%s", powerPolicy.c_str()));
 
-        if (power_policy == "ALWAYS_POWER_ON")
+        if (RestorePolicy::Policy::AlwaysOn ==
+            RestorePolicy::convertPolicyFromString(powerPolicy))
         {
             log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on");
             setProperty(bus, HOST_PATH, HOST_BUSNAME,
diff --git a/host_state_manager.cpp b/host_state_manager.cpp
index 54321d2..6e2fd22 100644
--- a/host_state_manager.cpp
+++ b/host_state_manager.cpp
@@ -4,8 +4,10 @@
 #include <systemd/sd-bus.h>
 #include <sdbusplus/server.hpp>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog-errors.hpp>
 #include <experimental/filesystem>
 #include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
 #include "host_state_manager.hpp"
 #include "host_state_serialize.hpp"
 #include "config.h"
@@ -57,11 +59,6 @@
 constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
 constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
 
-constexpr auto SETTINGS_INTERFACE =
-               "xyz.openbmc_project.Control.Power.RestorePolicy";
-constexpr auto SETTINGS_SERVICE_ROOT = "/";
-constexpr auto SETTINGS_HOST_STATE_RESTORE = "PowerRestorePolicy";
-
 // TODO openbmc/openbmc#1646 - boot count needs to be defined in 1 place
 constexpr auto DEFAULT_BOOTCOUNT = 3;
 
@@ -116,58 +113,32 @@
 
 bool Host::getStateRestoreSetting() const
 {
-    using namespace phosphor::logging;
-    auto depth = 0;
-    auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
-                                          MAPPER_PATH,
-                                          MAPPER_INTERFACE,
-                                          "GetSubTree");
-    mapperCall.append(SETTINGS_SERVICE_ROOT);
-    mapperCall.append(depth);
-    mapperCall.append(std::vector<std::string>({SETTINGS_INTERFACE}));
+    using namespace settings;
+    using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+    using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
 
-    auto mapperResponseMsg = bus.call(mapperCall);
-    if (mapperResponseMsg.is_method_error())
+    auto method =
+        bus.new_method_call(
+                settings.service(settings.powerRestorePolicy,
+                    powerRestoreIntf).c_str(),
+                settings.powerRestorePolicy.c_str(),
+                "org.freedesktop.DBus.Properties",
+                "Get");
+
+    method.append(powerRestoreIntf, "PowerRestorePolicy");
+    auto reply = bus.call(method);
+    if (reply.is_method_error())
     {
-        log<level::ERR>("Error in mapper call");
-        return false;
-    }
-
-    using MapperResponseType = std::map<std::string,
-                               std::map<std::string, std::vector<std::string>>>;
-    MapperResponseType mapperResponse;
-    mapperResponseMsg.read(mapperResponse);
-    if (mapperResponse.empty())
-    {
-        log<level::ERR>("Invalid response from mapper");
-        return false;
-    }
-
-    auto& settingsPath = mapperResponse.begin()->first;
-    auto& service = mapperResponse.begin()->second.begin()->first;
-
-    auto cmdMsg  =  bus.new_method_call(service.c_str(),
-                                        settingsPath.c_str(),
-                                        SYSTEMD_PROPERTY_IFACE,
-                                        "Get");
-    cmdMsg.append(SETTINGS_INTERFACE);
-    cmdMsg.append(SETTINGS_HOST_STATE_RESTORE);
-
-    auto response = bus.call(cmdMsg);
-    if (response.is_method_error())
-    {
-        log<level::ERR>("Error in fetching host state restore settings");
-        return false;
+        log<level::ERR>("Error in PowerRestorePolicy Get");
+        elog<InternalFailure>();
     }
 
     sdbusplus::message::variant<std::string> result;
-    response.read(result);
+    reply.read(result);
+    auto powerPolicy = result.get<std::string>();
 
-    using RestorePolicy = sdbusplus::xyz::openbmc_project::Control::
-         Power::server::RestorePolicy;
-
-    if (RestorePolicy::convertPolicyFromString(result.get<std::string>()) ==
-        RestorePolicy::Policy::Restore)
+    if (RestorePolicy::Policy::Restore ==
+        RestorePolicy::convertPolicyFromString(powerPolicy))
     {
         return true;
     }