power_policy: switch to new settings API

Change-Id: I5a54c32fe13f7f0314f0d6a902193b16ca4556fd
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/chassishandler.cpp b/chassishandler.cpp
index 6c8b8e0..1a69cdb 100644
--- a/chassishandler.cpp
+++ b/chassishandler.cpp
@@ -29,6 +29,7 @@
 #include <sdbusplus/server/object.hpp>
 #include <xyz/openbmc_project/Control/Boot/Source/server.hpp>
 #include <xyz/openbmc_project/Control/Boot/Mode/server.hpp>
+#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
 
 #include "config.h"
 
@@ -120,13 +121,15 @@
 
 constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
 constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
+constexpr auto powerRestoreIntf =
+    "xyz.openbmc_project.Control.Power.RestorePolicy";
 sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
 
 namespace cache
 {
 
 settings::Objects objects(dbus,
-                          {bootModeIntf, bootSourceIntf});
+                          {bootModeIntf, bootSourceIntf, powerRestoreIntf});
 
 } // namespace cache
 } // namespace internal
@@ -858,33 +861,21 @@
     return rc;
 }
 
-struct hostPowerPolicyTypeMap_t
-{
-    uint8_t policyNum;
-    char    policyName[19];
-};
-
-hostPowerPolicyTypeMap_t g_hostPowerPolicyTypeMap_t[] = {
-
-        {0x00, "LEAVE_OFF"},
-        {0x01, "RESTORE_LAST_STATE"},
-        {0x02, "ALWAYS_POWER_ON"},
-        {0x03, "UNKNOWN"}
-};
-
-uint8_t get_host_power_policy(char *p)
+namespace power_policy
 {
 
-    hostPowerPolicyTypeMap_t *s = g_hostPowerPolicyTypeMap_t;
+using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
+using IpmiValue = uint8_t;
+using DbusValue = RestorePolicy::Policy;
 
-    while (s->policyNum != 0x03) {
-        if (!strcmp(s->policyName,p))
-            break;
-        s++;
-    }
+std::map<DbusValue, IpmiValue> dbusToIpmi =
+{
+    {RestorePolicy::Policy::AlwaysOff, 0x00},
+    {RestorePolicy::Policy::Restore, 0x01},
+    {RestorePolicy::Policy::AlwaysOn, 0x02}
+};
 
-    return s->policyNum;
-}
+} // namespace power_policy
 
 //----------------------------------------------------------------------
 // Get Chassis Status commands
@@ -906,11 +897,32 @@
     ipmi_ret_t rc = IPMI_CC_OK;
     ipmi_get_chassis_status_t chassis_status{};
 
-    char *p = NULL;
     uint8_t s = 0;
 
-    // Get the system bus where most system services are provided.
-    bus = ipmid_get_sd_bus_connection();
+    using namespace chassis::internal;
+    using namespace chassis::internal::cache;
+    using namespace power_policy;
+
+    const auto& powerRestoreSetting = objects.map.at(powerRestoreIntf);
+    auto method =
+        dbus.new_method_call(
+            objects.service(powerRestoreSetting, powerRestoreIntf).c_str(),
+            powerRestoreSetting.c_str(),
+            PROP_INTF,
+            "Get");
+    method.append(powerRestoreIntf, "PowerRestorePolicy");
+    auto resp = dbus.call(method);
+    if (resp.is_method_error())
+    {
+        log<level::ERR>("Error in PowerRestorePolicy Get");
+        report<InternalFailure>();
+        *data_len = 0;
+        return IPMI_CC_UNSPECIFIED_ERROR;
+    }
+    sdbusplus::message::variant<std::string> result;
+    resp.read(result);
+    auto powerRestore =
+        RestorePolicy::convertPolicyFromString(result.get<std::string>());
 
     *data_len = 4;
 
@@ -939,21 +951,7 @@
 
     printf("pgood is 0x%02x\n", pgood);
 
-    // Get Power Policy
-    r = dbus_get_property("power_policy",&p);
-
-    if (r < 0) {
-        fprintf(stderr, "Dbus get property(power_policy) failed for get_sys_boot_options.\n");
-        rc = IPMI_CC_UNSPECIFIED_ERROR;
-    } else {
-        s = get_host_power_policy(p);
-    }
-
-    if (p)
-    {
-        free(p);
-        p = NULL;
-    }
+    s = dbusToIpmi.at(powerRestore);
 
     // Current Power State
     // [7] reserved