blob: 18163b724868d40b8d7b195bb27348da58ba9aa9 [file] [log] [blame]
Kuiying Wangce75d9e2020-12-15 16:17:05 +08001#include "manager_serialize.hpp"
2
3#include <cereal/archives/binary.hpp>
4#include <cereal/cereal.hpp>
5#include <cereal/types/map.hpp>
6#include <cereal/types/string.hpp>
7#include <cereal/types/tuple.hpp>
8#include <cereal/types/variant.hpp>
9#include <cereal/types/vector.hpp>
George Liu567a3cf2021-12-08 10:36:03 +080010#include <phosphor-logging/lg2.hpp>
Kuiying Wangce75d9e2020-12-15 16:17:05 +080011
12#include <fstream>
13
14namespace bios_config
15{
16
Kuiying Wangce75d9e2020-12-15 16:17:05 +080017/** @brief Function required by Cereal to perform serialization.
18 *
19 * @tparam Archive - Cereal archive type (binary in this case).
20 * @param[in] archive - reference to cereal archive.
21 * @param[in] entry- const reference to bios manager object
22 * @param[in] version - Class version that enables handling a serialized data
23 * across code levels
24 */
25template <class Archive>
26void save(Archive& archive, const Manager& entry,
27 const std::uint32_t /*version*/)
28{
29 archive(entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager::
30 baseBIOSTable(),
31 entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager::
32 pendingAttributes());
33}
34
35/** @brief Function required by Cereal to perform deserialization.
36 *
37 * @tparam Archive - Cereal archive type (binary in our case).
38 * @param[in] archive - reference to cereal archive.
39 * @param[out] entry - reference to bios manager object
40 * @param[in] version - Class version that enables handling a serialized data
41 * across code levels
42 */
43template <class Archive>
44void load(Archive& archive, Manager& entry, const std::uint32_t /*version*/)
45{
46 Manager::BaseTable baseTable;
47 Manager::PendingAttributes pendingAttrs;
48
49 archive(baseTable, pendingAttrs);
50 entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager::
51 baseBIOSTable(baseTable, true);
52 entry.sdbusplus::xyz::openbmc_project::BIOSConfig::server::Manager::
53 pendingAttributes(pendingAttrs, true);
54}
55
56void serialize(const Manager& obj, const fs::path& path)
57{
58 std::ofstream os(path.c_str(), std::ios::out | std::ios::binary);
59 cereal::BinaryOutputArchive oarchive(os);
60 oarchive(obj);
61}
62
63bool deserialize(const fs::path& path, Manager& entry)
64{
65 try
66 {
67 if (fs::exists(path))
68 {
69 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
70 cereal::BinaryInputArchive iarchive(is);
71 iarchive(entry);
72 return true;
73 }
74 return false;
75 }
76 catch (cereal::Exception& e)
77 {
George Liu567a3cf2021-12-08 10:36:03 +080078 lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
Kuiying Wangce75d9e2020-12-15 16:17:05 +080079 fs::remove(path);
80 return false;
81 }
82 catch (const std::length_error& e)
83 {
George Liu567a3cf2021-12-08 10:36:03 +080084 lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
Kuiying Wangce75d9e2020-12-15 16:17:05 +080085 fs::remove(path);
86 return false;
87 }
88}
89
Tom Josephf1101df2020-11-06 08:23:34 +053090} // namespace bios_config