1 #pragma once 2 3 #include "common/types.hpp" 4 #include "common/utils.hpp" 5 6 #include <libpldm/pdr.h> 7 8 #include <nlohmann/json.hpp> 9 #include <phosphor-logging/lg2.hpp> 10 #include <xyz/openbmc_project/Common/error.hpp> 11 12 #include <cstdint> 13 #include <filesystem> 14 #include <fstream> 15 #include <functional> 16 #include <iostream> 17 #include <string> 18 19 PHOSPHOR_LOG2_USING; 20 21 using InternalFailure = 22 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 23 24 namespace fs = std::filesystem; 25 26 namespace pldm 27 { 28 namespace responder 29 { 30 namespace pdr_utils 31 { 32 /** @struct Type ID associated with pdr 33 * 34 */ 35 enum class TypeId 36 { 37 PLDM_EFFECTER_ID, 38 PLDM_SENSOR_ID 39 }; 40 41 struct FruTLV 42 { 43 uint8_t fruFieldType; 44 uint8_t fruFieldLen; 45 std::vector<uint8_t> fruFieldValue; 46 }; 47 48 struct FruRecordDataFormat 49 { 50 uint16_t fruRSI; 51 uint8_t fruRecType; 52 uint8_t fruNum; 53 uint8_t fruEncodeType; 54 std::vector<FruTLV> fruTLV; 55 }; 56 57 /** @struct PdrEntry 58 * PDR entry structure that acts as a PDR record structure in the PDR 59 * repository to handle PDR APIs. 60 */ 61 struct PdrEntry 62 { 63 uint8_t* data; 64 uint32_t size; 65 union 66 { 67 uint32_t recordHandle; 68 uint32_t nextRecordHandle; 69 } handle; 70 }; 71 using Type = uint8_t; 72 using Json = nlohmann::json; 73 using RecordHandle = uint32_t; 74 using State = uint8_t; 75 using PossibleValues = std::vector<uint8_t>; 76 77 /** @brief Map of DBus property State to attribute value 78 */ 79 using StatestoDbusVal = std::map<State, pldm::utils::PropertyValue>; 80 using DbusMappings = std::vector<pldm::utils::DBusMapping>; 81 using DbusValMaps = std::vector<StatestoDbusVal>; 82 using EventStates = std::array<uint8_t, 8>; 83 84 /** @brief Parse PDR JSON file and output Json object 85 * 86 * @param[in] path - path of PDR JSON file 87 * 88 * @return Json - Json object 89 */ 90 inline Json readJson(const std::string& path) 91 { 92 fs::path dir(path); 93 if (!fs::exists(dir) || fs::is_empty(dir)) 94 { 95 throw InternalFailure(); 96 } 97 98 std::ifstream jsonFile(path); 99 if (!jsonFile.is_open()) 100 { 101 error("Error opening PDR JSON file at '{PATH}'", "PATH", path); 102 return {}; 103 } 104 105 return Json::parse(jsonFile); 106 } 107 108 /** @brief Populate the mapping between D-Bus property stateId and attribute 109 * value for the effecter PDR enumeration attribute. 110 * 111 * @param[in] type - type of the D-Bus property 112 * @param[in] dBusValues - json array of D-Bus property values 113 * @param[in] pv - Possible values for the effecter PDR enumeration attribute 114 * 115 * @return StatestoDbusVal - Map of D-Bus property stateId to attribute value 116 */ 117 StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues, 118 const PossibleValues& pv); 119 120 /** 121 * @class RepoInterface 122 * 123 * @brief Abstract class describing the interface API to the PDR repository, 124 * this class wraps operations used to handle the new PDR APIs. 125 */ 126 class RepoInterface 127 { 128 public: 129 RepoInterface(pldm_pdr* repo) : repo(repo) {} 130 131 virtual ~RepoInterface() = default; 132 133 /** @brief Get an opaque pldm_pdr structure 134 * 135 * @return pldm_pdr - pldm_pdr structure 136 */ 137 virtual pldm_pdr* getPdr() const = 0; 138 139 /** @brief Add a PDR record to a PDR repository 140 * 141 * @param[in] pdrEntry - PDR records entry(data, size, recordHandle) 142 * 143 * @return uint32_t - record handle assigned to PDR record 144 */ 145 virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0; 146 147 /** @brief Get the first PDR record from a PDR repository 148 * 149 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) 150 * 151 * @return opaque pointer acting as PDR record handle, will be NULL if 152 * record was not found 153 */ 154 virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0; 155 156 /** @brief Get the next PDR record from a PDR repository 157 * 158 * @param[in] currRecord - opaque pointer acting as a PDR record handle 159 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) 160 * 161 * @return opaque pointer acting as PDR record handle, will be NULL if 162 * record was not found 163 */ 164 virtual const pldm_pdr_record* getNextRecord( 165 const pldm_pdr_record* currRecord, PdrEntry& pdrEntry) = 0; 166 167 /** @brief Get record handle of a PDR record 168 * 169 * @param[in] record - opaque pointer acting as a PDR record handle 170 * 171 * @return uint32_t - record handle assigned to PDR record; 0 if record is 172 * not found 173 */ 174 virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0; 175 176 /** @brief Get number of records in a PDR repository 177 * 178 * @return uint32_t - number of records 179 */ 180 virtual uint32_t getRecordCount() = 0; 181 182 /** @brief Determine if records are empty in a PDR repository 183 * 184 * @return bool - true means empty and false means not empty 185 */ 186 virtual bool empty() = 0; 187 188 protected: 189 pldm_pdr* repo; 190 }; 191 192 /** 193 * @class Repo 194 * 195 * Wrapper class to handle the PDR APIs 196 * 197 * This class wraps operations used to handle PDR APIs. 198 */ 199 class Repo : public RepoInterface 200 { 201 public: 202 Repo(pldm_pdr* repo) : RepoInterface(repo) {} 203 204 pldm_pdr* getPdr() const override; 205 206 RecordHandle addRecord(const PdrEntry& pdrEntry) override; 207 208 const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override; 209 210 const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord, 211 PdrEntry& pdrEntry) override; 212 213 uint32_t getRecordHandle(const pldm_pdr_record* record) const override; 214 215 uint32_t getRecordCount() override; 216 217 bool empty() override; 218 }; 219 220 /** @brief Parse the State Sensor PDR and return the parsed sensor info which 221 * will be used to lookup the sensor info in the PlatformEventMessage 222 * command of sensorEvent type. 223 * 224 * @param[in] stateSensorPdr - state sensor PDR 225 * 226 * @return terminus handle, sensor ID and parsed sensor info 227 */ 228 std::tuple<pldm::pdr::TerminusHandle, pldm::pdr::SensorID, 229 pldm::pdr::SensorInfo> 230 parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr); 231 232 /** @brief Parse FRU record table and return the vector of the FRU record data 233 * format structure 234 * 235 * @param[in] fruData - fru data 236 * @param[in] fruLen - fru len 237 * 238 * @return std::vector<FruRecordDataFormat> - the vector of the FRU record data 239 * format structure 240 */ 241 std::vector<FruRecordDataFormat> 242 parseFruRecordTable(const uint8_t* fruData, size_t fruLen); 243 244 /** @brief Return the size of data type based on the effecterDataSize enum value 245 * 246 * @param[in] effecterDataSize - Bitwidth and format of setting effecter value 247 * @return[out] Map the effecterDataSize enum value to datatype and return the 248 * size of dataType 249 */ 250 size_t getEffecterDataSize(uint8_t effecterDataSize); 251 252 } // namespace pdr_utils 253 } // namespace responder 254 } // namespace pldm 255