Move all restore state policy into discover_state

Doing this so the host state code can set it's requested
host transition state without calling the override function.
Need to initialize it to the persisted cereal value but
not act on it.

The phosphor_discover_state application will ensure the appropriate
action is taken based on the system state and the persisted
value of the last requested host state.

Resolves openbmc/openbmc#2210

Change-Id: I7bef12fe314c7dfe137494fd46ddb35309063fc5
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index c055f9f..303b4a4 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -6,7 +6,6 @@
 #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"
@@ -31,8 +30,6 @@
 
 constexpr auto HOST_PATH = "/xyz/openbmc_project/state/host0";
 
-constexpr auto CHASSIS_PATH = "/xyz/openbmc_project/state/chassis0";
-
 std::string getService(sdbusplus::bus::bus& bus, std::string path,
                        std::string interface)
 {
@@ -131,43 +128,50 @@
     using namespace phosphor::state::manager;
     namespace server = sdbusplus::xyz::openbmc_project::State::server;
 
-    std::string currentPowerState = getProperty(bus, CHASSIS_PATH,
-                                                CHASSIS_BUSNAME,
-                                                "CurrentPowerState");
+    // This application is only run if chassis power is off
 
-    if(currentPowerState == convertForMessage(server::Chassis::PowerState::Off))
+    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())
     {
-        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>();
-        }
+        log<level::ERR>("Error in PowerRestorePolicy Get");
+        elog<InternalFailure>();
+    }
 
-        sdbusplus::message::variant<std::string> result;
-        reply.read(result);
-        auto powerPolicy = result.get<std::string>();
+    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", powerPolicy.c_str()));
+    log<level::INFO>("Host power is off, checking power policy",
+                     entry("POWER_POLICY=%s", powerPolicy.c_str()));
 
-        if (RestorePolicy::Policy::AlwaysOn ==
+    if (RestorePolicy::Policy::AlwaysOn ==
+        RestorePolicy::convertPolicyFromString(powerPolicy))
+    {
+        log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on");
+        setProperty(bus, HOST_PATH, HOST_BUSNAME,
+                    "RequestedHostTransition",
+                    convertForMessage(server::Host::Transition::On));
+    }
+    else if(RestorePolicy::Policy::Restore ==
             RestorePolicy::convertPolicyFromString(powerPolicy))
-        {
-            log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on");
-            setProperty(bus, HOST_PATH, HOST_BUSNAME,
-                        "RequestedHostTransition",
-                        convertForMessage(server::Host::Transition::On));
-        }
+    {
+        log<level::INFO>("power_policy=RESTORE, restoring last state");
 
+        // Read last requested state and re-request it to execute it
+        auto hostReqState = getProperty(bus, HOST_PATH,
+                                        HOST_BUSNAME,
+                                        "RequestedHostTransition");
+        setProperty(bus, HOST_PATH, HOST_BUSNAME,
+                    "RequestedHostTransition",
+                    hostReqState);
     }
 
     return 0;