summaryrefslogtreecommitdiffstats
path: root/host_state_serialize.cpp
blob: d5a3a980dfb1e0eb4c75b9ac24d5e5f092b229c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/tuple.hpp>
#include <cereal/archives/json.hpp>
#include <fstream>
#include "host_state_serialize.hpp"
#include "host_state_manager.hpp"

namespace phosphor
{
namespace state
{
namespace manager
{
/** @brief Function required by Cereal to perform serialization.
 *  @tparam Archive - Cereal archive type (binary in our case).
 *  @param[in] archive - reference to Cereal archive.
 *  @param[in] host - const reference to host.
 */
template<class Archive>
void save(Archive& archive, const Host& host)
{
    archive(convertForMessage(host.sdbusplus::xyz::openbmc_project::
           State::server::Host::requestedHostTransition()));
}

/** @brief Function required by Cereal to perform deserialization.
 *  @tparam Archive - Cereal archive type (binary in our case).
 *  @param[in] archive - reference to Cereal archive.
 *  @param[in] host - reference to host.
 */
template<class Archive>
void load(Archive& archive, Host& host)
{
    using namespace
        sdbusplus::xyz::openbmc_project::State::server;

    Host::Transition requestedHostTransition{};

    std::string str;
    archive(str);
    requestedHostTransition = Host::convertTransitionFromString(
                 str);
    host.requestedHostTransition(requestedHostTransition);
}

fs::path serialize(const Host& host, const fs::path& dir)
{
    std::ofstream os(dir.c_str(), std::ios::binary);
    cereal::JSONOutputArchive oarchive(os);
    oarchive(host);
    return dir;
}

bool deserialize(const fs::path& path, Host& host)
{
    if (fs::exists(path))
    {
        std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
        cereal::JSONInputArchive iarchive(is);
        iarchive(host);
        return true;
    }
    return false;
}
} //namespace manager
} // namespace state
} // namespace phosphor
OpenPOWER on IntegriCloud