1 #pragma once
2 
3 #include "bios_attribute.hpp"
4 #include "bios_table.hpp"
5 
6 #include <functional>
7 #include <iostream>
8 #include <memory>
9 #include <nlohmann/json.hpp>
10 #include <optional>
11 #include <set>
12 #include <string>
13 #include <vector>
14 
15 #include "bios_table.h"
16 
17 namespace pldm
18 {
19 namespace responder
20 {
21 namespace bios
22 {
23 
24 /** @class BIOSConfig
25  *  @brief Manager BIOS Attributes
26  */
27 class BIOSConfig
28 {
29   public:
30     BIOSConfig() = delete;
31     BIOSConfig(const BIOSConfig&) = delete;
32     BIOSConfig(BIOSConfig&&) = delete;
33     BIOSConfig& operator=(const BIOSConfig&) = delete;
34     BIOSConfig& operator=(BIOSConfig&&) = delete;
35     ~BIOSConfig() = default;
36 
37     /** @brief Construct BIOSConfig
38      *  @param[in] jsonDir - The directory where json file exists
39      *  @param[in] tableDir - The directory where the persistent table is placed
40      *  @param[in] dbusHandler - Dbus Handler
41      */
42     explicit BIOSConfig(const char* jsonDir, const char* tableDir,
43                         DBusHandler* const dbusHandler);
44 
45     /** @brief Set attribute value on dbus and attribute value table
46      *  @param[in] entry - attribute value entry
47      *  @param[in] size - size of the attribute value entry
48      *  @return pldm_completion_codes
49      */
50     int setAttrValue(const void* entry, size_t size);
51 
52     /** @brief Remove the persistent tables */
53     void removeTables();
54 
55     /** @brief Build bios tables(string,attribute,attribute value table)*/
56     void buildTables();
57 
58     /** @brief Get BIOS table of specified type
59      *  @param[in] tableType - The table type
60      *  @return The bios table, std::nullopt if the table is unaviliable
61      */
62     std::optional<Table> getBIOSTable(pldm_bios_table_types tableType);
63 
64   private:
65     const fs::path jsonDir;
66     const fs::path tableDir;
67     DBusHandler* const dbusHandler;
68 
69     // vector persists all attributes
70     using BIOSAttributes = std::vector<std::unique_ptr<BIOSAttribute>>;
71     BIOSAttributes biosAttributes;
72 
73     using propName = std::string;
74     using DbusChObjProperties = std::map<propName, PropertyValue>;
75 
76     // vector to catch the D-Bus property change signals for BIOS attributes
77     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> biosAttrMatch;
78 
79     /** @brief Method to update a BIOS attribute when the corresponding Dbus
80      *  property is changed
81      *  @param[in] chProperties - list of properties which have changed
82      *  @param[in] biosAttrIndex - Index of BIOSAttribute pointer in
83      * biosAttributes
84      *  @return - none
85      */
86     void processBiosAttrChangeNotification(
87         const DbusChObjProperties& chProperties, uint32_t biosAttrIndex);
88 
89     /** @brief Construct an attribute and persist it
90      *  @tparam T - attribute type
91      *  @param[in] entry - json entry
92      */
93     template <typename T>
94     void constructAttribute(const Json& entry)
95     {
96         try
97         {
98             biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler));
99             auto biosAttrIndex = biosAttributes.size() - 1;
100             auto dBusMap = biosAttributes[biosAttrIndex]->getDBusMap();
101 
102             if (dBusMap.has_value())
103             {
104                 using namespace sdbusplus::bus::match::rules;
105                 biosAttrMatch.push_back(
106                     std::make_unique<sdbusplus::bus::match::match>(
107                         pldm::utils::DBusHandler::getBus(),
108                         propertiesChanged(dBusMap->objectPath,
109                                           dBusMap->interface),
110                         [this,
111                          biosAttrIndex](sdbusplus::message::message& msg) {
112                             DbusChObjProperties props;
113                             std::string iface;
114                             msg.read(iface, props);
115                             processBiosAttrChangeNotification(props,
116                                                               biosAttrIndex);
117                         }));
118             }
119         }
120         catch (const std::exception& e)
121         {
122             std::cerr << "Constructs Attribute Error, " << e.what()
123                       << std::endl;
124         }
125     }
126 
127     /** Construct attributes and persist them */
128     void constructAttributes();
129 
130     using ParseHandler = std::function<void(const Json& entry)>;
131 
132     /** @brief Helper function to parse json
133      *  @param[in] filePath - Path of json file
134      *  @param[in] handler - Handler to process each entry in the json
135      */
136     void load(const fs::path& filePath, ParseHandler handler);
137 
138     /** @brief Build String Table and persist it
139      *  @return The built string table, std::nullopt if it fails.
140      */
141     std::optional<Table> buildAndStoreStringTable();
142 
143     /** @brief Build attr table and attr value table and persist them
144      *  @param[in] stringTable - The string Table
145      */
146     void buildAndStoreAttrTables(const Table& stringTable);
147 
148     /** @brief Persist the table
149      *  @param[in] path - Path to persist the table
150      *  @param[in] table - The table
151      */
152     void storeTable(const fs::path& path, const Table& table);
153 
154     /** @brief Load bios table to ram
155      *  @param[in] path - Path of the table
156      *  @return The table, std::nullopt if loading fails
157      */
158     std::optional<Table> loadTable(const fs::path& path);
159 };
160 
161 } // namespace bios
162 } // namespace responder
163 } // namespace pldm
164