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/host_state_manager.cpp b/host_state_manager.cpp
index 6b13276..0991cf7 100644
--- a/host_state_manager.cpp
+++ b/host_state_manager.cpp
@@ -2,14 +2,18 @@
#include <map>
#include <string>
#include <systemd/sd-bus.h>
+#include <cereal/cereal.hpp>
+#include <cereal/types/string.hpp>
+#include <cereal/types/vector.hpp>
+#include <cereal/types/tuple.hpp>
+#include <cereal/archives/json.hpp>
+#include <fstream>
#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"
@@ -98,9 +102,7 @@
server::Host::requestedHostTransition(Transition::Off);
}
- auto restore = getStateRestoreSetting();
-
- if ((!restore) || (!deserialize(HOST_STATE_PERSIST_PATH, *this)))
+ if (!deserialize(HOST_STATE_PERSIST_PATH))
{
//set to default value.
server::Host::requestedHostTransition(Transition::Off);
@@ -109,40 +111,6 @@
return;
}
-bool Host::getStateRestoreSetting() const
-{
- using namespace settings;
- using namespace sdbusplus::xyz::openbmc_project::Common::Error;
- using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
-
- 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>();
-
- if (RestorePolicy::Policy::Restore ==
- RestorePolicy::convertPolicyFromString(powerPolicy))
- {
- return true;
- }
- return false;
-}
-
void Host::executeTransition(Transition tranReq)
{
auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second;
@@ -319,6 +287,26 @@
return rebootCount;
}
+fs::path Host::serialize(const fs::path& dir)
+{
+ std::ofstream os(dir.c_str(), std::ios::binary);
+ cereal::JSONOutputArchive oarchive(os);
+ oarchive(*this);
+ return dir;
+}
+
+bool Host::deserialize(const fs::path& path)
+{
+ if (fs::exists(path))
+ {
+ std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+ cereal::JSONInputArchive iarchive(is);
+ iarchive(*this);
+ return true;
+ }
+ return false;
+}
+
Host::Transition Host::requestedHostTransition(Transition value)
{
log<level::INFO>(
@@ -339,7 +327,7 @@
executeTransition(value);
auto retVal = server::Host::requestedHostTransition(value);
- serialize(*this);
+ serialize();
return retVal;
}