1 #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>
10 #include <phosphor-logging/lg2.hpp>
11 
12 #include <fstream>
13 
14 namespace bios_config
15 {
16 
17 /** @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  */
25 template <class Archive>
save(Archive & archive,const Manager & entry,const std::uint32_t)26 void 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  */
43 template <class Archive>
load(Archive & archive,Manager & entry,const std::uint32_t)44 void 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 
serialize(const Manager & obj,const fs::path & path)56 void serialize(const Manager& obj, const fs::path& path)
57 {
58     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     }
76 }
77 
deserialize(const fs::path & path,Manager & entry)78 bool 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);
85             if (!is.is_open())
86             {
87                 lg2::error("Failed to open file for deserialization: {FILE}",
88                            "FILE", path);
89                 return false;
90             }
91             cereal::BinaryInputArchive iarchive(is);
92             iarchive(entry);
93             return true;
94         }
95         return false;
96     }
97     catch (const std::exception& e)
98     {
99         lg2::error("Failed to serialize: {ERROR}", "ERROR", e);
100         fs::remove(path);
101         return false;
102     }
103 }
104 
105 } // namespace bios_config
106