George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 3 | #include "utils.hpp" |
| 4 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
| 7 | #include <filesystem> |
| 8 | #include <fstream> |
| 9 | #include <functional> |
| 10 | #include <iostream> |
| 11 | #include <nlohmann/json.hpp> |
| 12 | #include <string> |
| 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | |
| 15 | #include "libpldm/pdr.h" |
| 16 | |
| 17 | using InternalFailure = |
| 18 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 19 | |
| 20 | namespace fs = std::filesystem; |
| 21 | |
| 22 | namespace pldm |
| 23 | { |
| 24 | |
| 25 | namespace responder |
| 26 | { |
| 27 | |
| 28 | namespace pdr_utils |
| 29 | { |
| 30 | |
| 31 | /** @struct PdrEntry |
| 32 | * PDR entry structure that acts as a PDR record structure in the PDR |
| 33 | * repository to handle PDR APIs. |
| 34 | */ |
| 35 | struct PdrEntry |
| 36 | { |
| 37 | uint8_t* data; |
| 38 | uint32_t size; |
| 39 | union |
| 40 | { |
| 41 | uint32_t recordHandle; |
| 42 | uint32_t nextRecordHandle; |
| 43 | } handle; |
| 44 | }; |
| 45 | using Type = uint8_t; |
| 46 | using Json = nlohmann::json; |
| 47 | using RecordHandle = uint32_t; |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 48 | using State = uint8_t; |
| 49 | using PossibleValues = std::vector<uint8_t>; |
| 50 | |
| 51 | /** @brief Map of DBus property State to attribute value |
| 52 | */ |
| 53 | using StatestoDbusVal = std::map<State, pldm::utils::PropertyValue>; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 54 | |
| 55 | /** @brief Parse PDR JSON file and output Json object |
| 56 | * |
| 57 | * @param[in] path - path of PDR JSON file |
| 58 | * |
| 59 | * @return Json - Json object |
| 60 | */ |
| 61 | inline Json readJson(const std::string& path) |
| 62 | { |
| 63 | fs::path dir(path); |
| 64 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 65 | { |
| 66 | throw InternalFailure(); |
| 67 | } |
| 68 | |
| 69 | std::ifstream jsonFile(path); |
| 70 | if (!jsonFile.is_open()) |
| 71 | { |
| 72 | std::cerr << "Error opening PDR JSON file, PATH=" << path << "\n"; |
| 73 | return {}; |
| 74 | } |
| 75 | |
| 76 | return Json::parse(jsonFile); |
| 77 | } |
| 78 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame^] | 79 | /** @brief Populate the mapping between D-Bus property stateId and attribute |
| 80 | * value for the effecter PDR enumeration attribute. |
| 81 | * |
| 82 | * @param[in] type - type of the D-Bus property |
| 83 | * @param[in] dBusValues - json array of D-Bus property values |
| 84 | * @param[in] pv - Possible values for the effecter PDR enumeration attribute |
| 85 | * |
| 86 | * @return StatestoDbusVal - Map of D-Bus property stateId to attribute value |
| 87 | */ |
| 88 | StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues, |
| 89 | const PossibleValues& pv); |
| 90 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 91 | /** |
| 92 | * @class RepoInterface |
| 93 | * |
| 94 | * @brief Abstract class describing the interface API to the PDR repository, |
| 95 | * this class wraps operations used to handle the new PDR APIs. |
| 96 | */ |
| 97 | class RepoInterface |
| 98 | { |
| 99 | public: |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 100 | RepoInterface(pldm_pdr* repo) : repo(repo) |
| 101 | { |
| 102 | } |
| 103 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 104 | virtual ~RepoInterface() = default; |
| 105 | |
| 106 | /** @brief Get an opaque pldm_pdr structure |
| 107 | * |
| 108 | * @return pldm_pdr - pldm_pdr structure |
| 109 | */ |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 110 | virtual pldm_pdr* getPdr() const = 0; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 111 | |
| 112 | /** @brief Add a PDR record to a PDR repository |
| 113 | * |
| 114 | * @param[in] pdrEntry - PDR records entry(data, size, recordHandle) |
| 115 | * |
| 116 | * @return uint32_t - record handle assigned to PDR record |
| 117 | */ |
| 118 | virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0; |
| 119 | |
| 120 | /** @brief Get the first PDR record from a PDR repository |
| 121 | * |
| 122 | * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) |
| 123 | * |
| 124 | * @return opaque pointer acting as PDR record handle, will be NULL if |
| 125 | * record was not found |
| 126 | */ |
| 127 | virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0; |
| 128 | |
| 129 | /** @brief Get the next PDR record from a PDR repository |
| 130 | * |
| 131 | * @param[in] currRecord - opaque pointer acting as a PDR record handle |
| 132 | * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) |
| 133 | * |
| 134 | * @return opaque pointer acting as PDR record handle, will be NULL if |
| 135 | * record was not found |
| 136 | */ |
| 137 | virtual const pldm_pdr_record* |
| 138 | getNextRecord(const pldm_pdr_record* currRecord, |
| 139 | PdrEntry& pdrEntry) = 0; |
| 140 | |
| 141 | /** @brief Get record handle of a PDR record |
| 142 | * |
| 143 | * @param[in] record - opaque pointer acting as a PDR record handle |
| 144 | * |
| 145 | * @return uint32_t - record handle assigned to PDR record; 0 if record is |
| 146 | * not found |
| 147 | */ |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 148 | virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 149 | |
| 150 | /** @brief Get number of records in a PDR repository |
| 151 | * |
| 152 | * @return uint32_t - number of records |
| 153 | */ |
| 154 | virtual uint32_t getRecordCount() = 0; |
| 155 | |
| 156 | /** @brief Determine if records are empty in a PDR repository |
| 157 | * |
| 158 | * @return bool - true means empty and false means not empty |
| 159 | */ |
| 160 | virtual bool empty() = 0; |
| 161 | |
| 162 | protected: |
| 163 | pldm_pdr* repo; |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * @class Repo |
| 168 | * |
| 169 | * Wrapper class to handle the PDR APIs |
| 170 | * |
| 171 | * This class wraps operations used to handle PDR APIs. |
| 172 | */ |
| 173 | class Repo : public RepoInterface |
| 174 | { |
| 175 | public: |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 176 | Repo(pldm_pdr* repo) : RepoInterface(repo) |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 177 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 180 | pldm_pdr* getPdr() const override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 181 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 182 | RecordHandle addRecord(const PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 183 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 184 | const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 185 | |
| 186 | const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord, |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 187 | PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 188 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 189 | uint32_t getRecordHandle(const pldm_pdr_record* record) const override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 190 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 191 | uint32_t getRecordCount() override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 192 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 193 | bool empty() override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace pdr_utils |
| 197 | } // namespace responder |
| 198 | } // namespace pldm |