xref: /openbmc/pldm/libpldmresponder/bios_config.hpp (revision d965934f04f9d9e0edd13f8bba1207fb2936712c)
1*d965934fSJohn Wang #pragma once
2*d965934fSJohn Wang 
3*d965934fSJohn Wang #include "bios_attribute.hpp"
4*d965934fSJohn Wang #include "bios_table.hpp"
5*d965934fSJohn Wang 
6*d965934fSJohn Wang #include <functional>
7*d965934fSJohn Wang #include <iostream>
8*d965934fSJohn Wang #include <memory>
9*d965934fSJohn Wang #include <nlohmann/json.hpp>
10*d965934fSJohn Wang #include <optional>
11*d965934fSJohn Wang #include <set>
12*d965934fSJohn Wang #include <string>
13*d965934fSJohn Wang #include <vector>
14*d965934fSJohn Wang 
15*d965934fSJohn Wang #include "bios_table.h"
16*d965934fSJohn Wang 
17*d965934fSJohn Wang namespace pldm
18*d965934fSJohn Wang {
19*d965934fSJohn Wang namespace responder
20*d965934fSJohn Wang {
21*d965934fSJohn Wang namespace bios
22*d965934fSJohn Wang {
23*d965934fSJohn Wang 
24*d965934fSJohn Wang /** @class BIOSConfig
25*d965934fSJohn Wang  *  @brief Manager BIOS Attributes
26*d965934fSJohn Wang  */
27*d965934fSJohn Wang class BIOSConfig
28*d965934fSJohn Wang {
29*d965934fSJohn Wang   public:
30*d965934fSJohn Wang     BIOSConfig() = delete;
31*d965934fSJohn Wang     BIOSConfig(const BIOSConfig&) = delete;
32*d965934fSJohn Wang     BIOSConfig(BIOSConfig&&) = delete;
33*d965934fSJohn Wang     BIOSConfig& operator=(const BIOSConfig&) = delete;
34*d965934fSJohn Wang     BIOSConfig& operator=(BIOSConfig&&) = delete;
35*d965934fSJohn Wang     ~BIOSConfig() = default;
36*d965934fSJohn Wang 
37*d965934fSJohn Wang     /** @brief Construct BIOSConfig
38*d965934fSJohn Wang      *  @param[in] jsonDir - The directory where json file exists
39*d965934fSJohn Wang      *  @param[in] tableDir - The directory where the persistent table is placed
40*d965934fSJohn Wang      *  @param[in] dbusHandler - Dbus Handler
41*d965934fSJohn Wang      */
42*d965934fSJohn Wang     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
43*d965934fSJohn Wang                         DBusHandler* const dbusHandler);
44*d965934fSJohn Wang 
45*d965934fSJohn Wang     /** @brief Set attribute value on dbus and attribute value table
46*d965934fSJohn Wang      *  @param[in] entry - attribute value entry
47*d965934fSJohn Wang      *  @param[in] size - size of the attribute value entry
48*d965934fSJohn Wang      *  @return pldm_completion_codes
49*d965934fSJohn Wang      */
50*d965934fSJohn Wang     int setAttrValue(const void* entry, size_t size);
51*d965934fSJohn Wang 
52*d965934fSJohn Wang     /** @brief Remove the persistent tables */
53*d965934fSJohn Wang     void removeTables();
54*d965934fSJohn Wang 
55*d965934fSJohn Wang     /** @brief Build bios tables(string,attribute,attribute value table)*/
56*d965934fSJohn Wang     void buildTables();
57*d965934fSJohn Wang 
58*d965934fSJohn Wang     /** @brief Get BIOS table of specified type
59*d965934fSJohn Wang      *  @param[in] tableType - The table type
60*d965934fSJohn Wang      *  @return The bios table, std::nullopt if the table is unaviliable
61*d965934fSJohn Wang      */
62*d965934fSJohn Wang     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
63*d965934fSJohn Wang 
64*d965934fSJohn Wang   private:
65*d965934fSJohn Wang     const fs::path jsonDir;
66*d965934fSJohn Wang     const fs::path tableDir;
67*d965934fSJohn Wang     DBusHandler* const dbusHandler;
68*d965934fSJohn Wang 
69*d965934fSJohn Wang     // vector persists all attributes
70*d965934fSJohn Wang     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
71*d965934fSJohn Wang     BIOSAttributes biosAttributes;
72*d965934fSJohn Wang 
73*d965934fSJohn Wang     /** @brief Construct an attribute and persist it
74*d965934fSJohn Wang      *  @tparam T - attribute type
75*d965934fSJohn Wang      *  @param[in] entry - json entry
76*d965934fSJohn Wang      */
77*d965934fSJohn Wang     template <typename T>
78*d965934fSJohn Wang     void constructAttribute(const Json& entry)
79*d965934fSJohn Wang     {
80*d965934fSJohn Wang         try
81*d965934fSJohn Wang         {
82*d965934fSJohn Wang             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
83*d965934fSJohn Wang         }
84*d965934fSJohn Wang         catch (const std::exception& e)
85*d965934fSJohn Wang         {
86*d965934fSJohn Wang             std::cerr << "Constructs Attribute Error, " << e.what()
87*d965934fSJohn Wang                       << std::endl;
88*d965934fSJohn Wang         }
89*d965934fSJohn Wang     }
90*d965934fSJohn Wang 
91*d965934fSJohn Wang     /** Construct attributes and persist them */
92*d965934fSJohn Wang     void constructAttributes();
93*d965934fSJohn Wang 
94*d965934fSJohn Wang     using ParseHandler = std::function<void(const Json& entry)>;
95*d965934fSJohn Wang 
96*d965934fSJohn Wang     /** @brief Helper function to parse json
97*d965934fSJohn Wang      *  @param[in] filePath - Path of json file
98*d965934fSJohn Wang      *  @param[in] handler - Handler to process each entry in the json
99*d965934fSJohn Wang      */
100*d965934fSJohn Wang     void load(const fs::path& filePath, ParseHandler handler);
101*d965934fSJohn Wang 
102*d965934fSJohn Wang     /** @brief Build String Table and persist it
103*d965934fSJohn Wang      *  @return The built string table, std::nullopt if it fails.
104*d965934fSJohn Wang      */
105*d965934fSJohn Wang     std::optional<Table> buildAndStoreStringTable();
106*d965934fSJohn Wang 
107*d965934fSJohn Wang     /** @brief Build attr table and attr value table and persist them
108*d965934fSJohn Wang      *  @param[in] stringTable - The string Table
109*d965934fSJohn Wang      */
110*d965934fSJohn Wang     void buildAndStoreAttrTables(const Table& stringTable);
111*d965934fSJohn Wang 
112*d965934fSJohn Wang     /** @brief Persist the table
113*d965934fSJohn Wang      *  @param[in] path - Path to persist the table
114*d965934fSJohn Wang      *  @param[in] table - The table
115*d965934fSJohn Wang      */
116*d965934fSJohn Wang     void storeTable(const fs::path& path, const Table& table);
117*d965934fSJohn Wang 
118*d965934fSJohn Wang     /** @brief Load bios table to ram
119*d965934fSJohn Wang      *  @param[in] path - Path of the table
120*d965934fSJohn Wang      *  @return The table, std::nullopt if loading fails
121*d965934fSJohn Wang      */
122*d965934fSJohn Wang     std::optional<Table> loadTable(const fs::path& path);
123*d965934fSJohn Wang };
124*d965934fSJohn Wang 
125*d965934fSJohn Wang } // namespace bios
126*d965934fSJohn Wang } // namespace responder
127*d965934fSJohn Wang } // namespace pldm