John Wang | d965934 | 2020-02-27 16:46:05 +0800 | [diff] [blame] | 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 | /** @brief Construct an attribute and persist it |
| 74 | * @tparam T - attribute type |
| 75 | * @param[in] entry - json entry |
| 76 | */ |
| 77 | template <typename T> |
| 78 | void constructAttribute(const Json& entry) |
| 79 | { |
| 80 | try |
| 81 | { |
| 82 | biosAttributes.push_back(std::make_unique<T>(entry, dbusHandler)); |
| 83 | } |
| 84 | catch (const std::exception& e) |
| 85 | { |
| 86 | std::cerr << "Constructs Attribute Error, " << e.what() |
| 87 | << std::endl; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** Construct attributes and persist them */ |
| 92 | void constructAttributes(); |
| 93 | |
| 94 | using ParseHandler = std::function<void(const Json& entry)>; |
| 95 | |
| 96 | /** @brief Helper function to parse json |
| 97 | * @param[in] filePath - Path of json file |
| 98 | * @param[in] handler - Handler to process each entry in the json |
| 99 | */ |
| 100 | void load(const fs::path& filePath, ParseHandler handler); |
| 101 | |
| 102 | /** @brief Build String Table and persist it |
| 103 | * @return The built string table, std::nullopt if it fails. |
| 104 | */ |
| 105 | std::optional<Table> buildAndStoreStringTable(); |
| 106 | |
| 107 | /** @brief Build attr table and attr value table and persist them |
| 108 | * @param[in] stringTable - The string Table |
| 109 | */ |
| 110 | void buildAndStoreAttrTables(const Table& stringTable); |
| 111 | |
| 112 | /** @brief Persist the table |
| 113 | * @param[in] path - Path to persist the table |
| 114 | * @param[in] table - The table |
| 115 | */ |
| 116 | void storeTable(const fs::path& path, const Table& table); |
| 117 | |
| 118 | /** @brief Load bios table to ram |
| 119 | * @param[in] path - Path of the table |
| 120 | * @return The table, std::nullopt if loading fails |
| 121 | */ |
| 122 | std::optional<Table> loadTable(const fs::path& path); |
| 123 | }; |
| 124 | |
| 125 | } // namespace bios |
| 126 | } // namespace responder |
| 127 | } // namespace pldm |