blob: 57b8991e33e5ad3312aa908dcb6a73aabac3b73f [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{
Manojkiran Edaae2ed8d2024-10-15 10:42:17 +053058 try
59 {
60 std::ofstream os(path, std::ios::out | std::ios::binary);
61
62 if (!os.is_open())
63 {
64 lg2::error("Failed to open file for serialization: {FILE}", "FILE",
65 path);
66 return;
67 }
68
69 cereal::BinaryOutputArchive oarchive(os);
70 oarchive(obj);
71 }
72 catch (const std::exception& e)
73 {
74 lg2::error("Failed to Serialize : {ERROR} ", "ERROR", e);
75 }
Kuiying Wangce75d9e2020-12-15 16:17:05 +080076}
77
78bool deserialize(const fs::path& path, Manager& entry)
79{
80 try
81 {
82 if (fs::exists(path))
83 {
84 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
Manojkiran Edaae2ed8d2024-10-15 10:42:17 +053085 if (!is.is_open())
86 {
87 lg2::error("Failed to open file for deserialization: {FILE}",
88 "FILE", path);
89 return false;
90 }
Kuiying Wangce75d9e2020-12-15 16:17:05 +080091 cereal::BinaryInputArchive iarchive(is);
92 iarchive(entry);
93 return true;
94 }
95 return false;
96 }
97 catch (cereal::Exception& e)
98 {
George Liu567a3cf2021-12-08 10:36:03 +080099 lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
Kuiying Wangce75d9e2020-12-15 16:17:05 +0800100 fs::remove(path);
101 return false;
102 }
103 catch (const std::length_error& e)
104 {
George Liu567a3cf2021-12-08 10:36:03 +0800105 lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
Kuiying Wangce75d9e2020-12-15 16:17:05 +0800106 fs::remove(path);
107 return false;
108 }
109}
110
Patrick Williamsf19e2712023-05-10 07:51:20 -0500111} // namespace bios_config