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> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 10 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 13 | #include <filesystem> |
| 14 | #include <fstream> |
| 15 | #include <functional> |
| 16 | #include <iostream> |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 17 | #include <string> |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 18 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 19 | PHOSPHOR_LOG2_USING; |
| 20 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 21 | using InternalFailure = |
| 22 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 23 | |
| 24 | namespace fs = std::filesystem; |
| 25 | |
| 26 | namespace pldm |
| 27 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 28 | namespace responder |
| 29 | { |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 30 | namespace pdr_utils |
| 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 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame^] | 84 | error("Error opening PDR JSON file, PATH={JSON_PATH}", "JSON_PATH", |
| 85 | path); |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | return Json::parse(jsonFile); |
| 90 | } |
| 91 | |
George Liu | 1ec85d4 | 2020-02-12 16:05:32 +0800 | [diff] [blame] | 92 | /** @brief Populate the mapping between D-Bus property stateId and attribute |
| 93 | * value for the effecter PDR enumeration attribute. |
| 94 | * |
| 95 | * @param[in] type - type of the D-Bus property |
| 96 | * @param[in] dBusValues - json array of D-Bus property values |
| 97 | * @param[in] pv - Possible values for the effecter PDR enumeration attribute |
| 98 | * |
| 99 | * @return StatestoDbusVal - Map of D-Bus property stateId to attribute value |
| 100 | */ |
| 101 | StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues, |
| 102 | const PossibleValues& pv); |
| 103 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 104 | /** |
| 105 | * @class RepoInterface |
| 106 | * |
| 107 | * @brief Abstract class describing the interface API to the PDR repository, |
| 108 | * this class wraps operations used to handle the new PDR APIs. |
| 109 | */ |
| 110 | class RepoInterface |
| 111 | { |
| 112 | public: |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 113 | RepoInterface(pldm_pdr* repo) : repo(repo) |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 114 | {} |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 115 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 116 | virtual ~RepoInterface() = default; |
| 117 | |
| 118 | /** @brief Get an opaque pldm_pdr structure |
| 119 | * |
| 120 | * @return pldm_pdr - pldm_pdr structure |
| 121 | */ |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 122 | virtual pldm_pdr* getPdr() const = 0; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 123 | |
| 124 | /** @brief Add a PDR record to a PDR repository |
| 125 | * |
| 126 | * @param[in] pdrEntry - PDR records entry(data, size, recordHandle) |
| 127 | * |
| 128 | * @return uint32_t - record handle assigned to PDR record |
| 129 | */ |
| 130 | virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0; |
| 131 | |
| 132 | /** @brief Get the first PDR record from a PDR repository |
| 133 | * |
| 134 | * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) |
| 135 | * |
| 136 | * @return opaque pointer acting as PDR record handle, will be NULL if |
| 137 | * record was not found |
| 138 | */ |
| 139 | virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0; |
| 140 | |
| 141 | /** @brief Get the next PDR record from a PDR repository |
| 142 | * |
| 143 | * @param[in] currRecord - opaque pointer acting as a PDR record handle |
| 144 | * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle) |
| 145 | * |
| 146 | * @return opaque pointer acting as PDR record handle, will be NULL if |
| 147 | * record was not found |
| 148 | */ |
| 149 | virtual const pldm_pdr_record* |
| 150 | getNextRecord(const pldm_pdr_record* currRecord, |
| 151 | PdrEntry& pdrEntry) = 0; |
| 152 | |
| 153 | /** @brief Get record handle of a PDR record |
| 154 | * |
| 155 | * @param[in] record - opaque pointer acting as a PDR record handle |
| 156 | * |
| 157 | * @return uint32_t - record handle assigned to PDR record; 0 if record is |
| 158 | * not found |
| 159 | */ |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 160 | virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 161 | |
| 162 | /** @brief Get number of records in a PDR repository |
| 163 | * |
| 164 | * @return uint32_t - number of records |
| 165 | */ |
| 166 | virtual uint32_t getRecordCount() = 0; |
| 167 | |
| 168 | /** @brief Determine if records are empty in a PDR repository |
| 169 | * |
| 170 | * @return bool - true means empty and false means not empty |
| 171 | */ |
| 172 | virtual bool empty() = 0; |
| 173 | |
| 174 | protected: |
| 175 | pldm_pdr* repo; |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * @class Repo |
| 180 | * |
| 181 | * Wrapper class to handle the PDR APIs |
| 182 | * |
| 183 | * This class wraps operations used to handle PDR APIs. |
| 184 | */ |
| 185 | class Repo : public RepoInterface |
| 186 | { |
| 187 | public: |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 188 | Repo(pldm_pdr* repo) : RepoInterface(repo) |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 189 | {} |
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 | pldm_pdr* getPdr() const 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 | RecordHandle addRecord(const PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 194 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 195 | const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 196 | |
| 197 | const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord, |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 198 | PdrEntry& pdrEntry) override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 199 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 200 | uint32_t getRecordHandle(const pldm_pdr_record* record) const override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 201 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 202 | uint32_t getRecordCount() override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 203 | |
Deepak Kodihalli | c682fe2 | 2020-03-04 00:42:54 -0600 | [diff] [blame] | 204 | bool empty() override; |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 205 | }; |
| 206 | |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 207 | /** @brief Parse the State Sensor PDR and return the parsed sensor info which |
| 208 | * will be used to lookup the sensor info in the PlatformEventMessage |
| 209 | * command of sensorEvent type. |
| 210 | * |
| 211 | * @param[in] stateSensorPdr - state sensor PDR |
| 212 | * |
| 213 | * @return terminus handle, sensor ID and parsed sensor info |
| 214 | */ |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 215 | std::tuple<pldm::pdr::TerminusHandle, pldm::pdr::SensorID, |
| 216 | pldm::pdr::SensorInfo> |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 217 | parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr); |
| 218 | |
George Liu | e53193f | 2020-02-24 09:23:26 +0800 | [diff] [blame] | 219 | } // namespace pdr_utils |
| 220 | } // namespace responder |
| 221 | } // namespace pldm |