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