blob: d5a3a980dfb1e0eb4c75b9ac24d5e5f092b229c8 [file] [log] [blame]
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -05001#include <cereal/types/string.hpp>
2#include <cereal/types/vector.hpp>
3#include <cereal/types/tuple.hpp>
4#include <cereal/archives/json.hpp>
5#include <fstream>
6#include "host_state_serialize.hpp"
7#include "host_state_manager.hpp"
8
9namespace phosphor
10{
11namespace state
12{
13namespace manager
14{
15/** @brief Function required by Cereal to perform serialization.
16 * @tparam Archive - Cereal archive type (binary in our case).
17 * @param[in] archive - reference to Cereal archive.
18 * @param[in] host - const reference to host.
19 */
20template<class Archive>
21void save(Archive& archive, const Host& host)
22{
23 archive(convertForMessage(host.sdbusplus::xyz::openbmc_project::
24 State::server::Host::requestedHostTransition()));
25}
26
27/** @brief Function required by Cereal to perform deserialization.
28 * @tparam Archive - Cereal archive type (binary in our case).
29 * @param[in] archive - reference to Cereal archive.
30 * @param[in] host - reference to host.
31 */
32template<class Archive>
33void load(Archive& archive, Host& host)
34{
35 using namespace
36 sdbusplus::xyz::openbmc_project::State::server;
37
38 Host::Transition requestedHostTransition{};
39
40 std::string str;
41 archive(str);
42 requestedHostTransition = Host::convertTransitionFromString(
43 str);
44 host.requestedHostTransition(requestedHostTransition);
45}
46
47fs::path serialize(const Host& host, const fs::path& dir)
48{
49 std::ofstream os(dir.c_str(), std::ios::binary);
50 cereal::JSONOutputArchive oarchive(os);
51 oarchive(host);
52 return dir;
53}
54
55bool deserialize(const fs::path& path, Host& host)
56{
57 if (fs::exists(path))
58 {
59 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
60 cereal::JSONInputArchive iarchive(is);
61 iarchive(host);
62 return true;
63 }
64 return false;
65}
66} //namespace manager
67} // namespace state
68} // namespace phosphor