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