blob: 01fe223f6089e46763a5d7eaaebfcd19bce497f5 [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#pragma once
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/types.hpp"
4#include "common/utils.hpp"
George Liu1ec85d42020-02-12 16:05:32 +08005
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/pdr.h>
George Liue53193f2020-02-24 09:23:26 +08007#include <stdint.h>
8
George Liu6492f522020-06-16 10:34:05 +08009#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
George Liu6492f522020-06-16 10:34:05 +080011#include <xyz/openbmc_project/Common/error.hpp>
12
George Liue53193f2020-02-24 09:23:26 +080013#include <filesystem>
14#include <fstream>
15#include <functional>
16#include <iostream>
George Liue53193f2020-02-24 09:23:26 +080017#include <string>
George Liue53193f2020-02-24 09:23:26 +080018
Riya Dixit49cfb132023-03-02 04:26:53 -060019PHOSPHOR_LOG2_USING;
20
George Liue53193f2020-02-24 09:23:26 +080021using InternalFailure =
22 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
23
24namespace fs = std::filesystem;
25
26namespace pldm
27{
George Liue53193f2020-02-24 09:23:26 +080028namespace responder
29{
George Liue53193f2020-02-24 09:23:26 +080030namespace pdr_utils
31{
George Liuadbe1722020-05-09 19:20:19 +080032/** @struct Type ID associated with pdr
33 *
34 */
35enum class TypeId
36{
37 PLDM_EFFECTER_ID,
38 PLDM_SENSOR_ID
39};
40
George Liu682ee182020-12-25 15:24:33 +080041struct FruTLV
42{
43 uint8_t fruFieldType;
44 uint8_t fruFieldLen;
45 std::vector<uint8_t> fruFieldValue;
46};
47
48struct 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
George Liue53193f2020-02-24 09:23:26 +080057/** @struct PdrEntry
58 * PDR entry structure that acts as a PDR record structure in the PDR
59 * repository to handle PDR APIs.
60 */
61struct PdrEntry
62{
63 uint8_t* data;
64 uint32_t size;
65 union
66 {
67 uint32_t recordHandle;
68 uint32_t nextRecordHandle;
69 } handle;
70};
71using Type = uint8_t;
72using Json = nlohmann::json;
73using RecordHandle = uint32_t;
George Liu1ec85d42020-02-12 16:05:32 +080074using State = uint8_t;
75using PossibleValues = std::vector<uint8_t>;
76
77/** @brief Map of DBus property State to attribute value
78 */
79using StatestoDbusVal = std::map<State, pldm::utils::PropertyValue>;
George Liua2870722020-02-11 11:09:30 +080080using DbusMappings = std::vector<pldm::utils::DBusMapping>;
81using DbusValMaps = std::vector<StatestoDbusVal>;
Manojkiran Edaae933cc2024-02-21 17:19:21 +053082using EventStates = std::array<uint8_t, 8>;
George Liue53193f2020-02-24 09:23:26 +080083
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 */
90inline 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 {
Riya Dixit89644442024-03-31 05:39:59 -0500101 error("Error opening PDR JSON file at '{PATH}'", "PATH", path);
George Liue53193f2020-02-24 09:23:26 +0800102 return {};
103 }
104
105 return Json::parse(jsonFile);
106}
107
George Liu1ec85d42020-02-12 16:05:32 +0800108/** @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 */
117StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
118 const PossibleValues& pv);
119
George Liue53193f2020-02-24 09:23:26 +0800120/**
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 */
126class RepoInterface
127{
128 public:
Patrick Williams6da4f912023-05-10 07:50:53 -0500129 RepoInterface(pldm_pdr* repo) : repo(repo) {}
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600130
George Liue53193f2020-02-24 09:23:26 +0800131 virtual ~RepoInterface() = default;
132
133 /** @brief Get an opaque pldm_pdr structure
134 *
135 * @return pldm_pdr - pldm_pdr structure
136 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600137 virtual pldm_pdr* getPdr() const = 0;
George Liue53193f2020-02-24 09:23:26 +0800138
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*
165 getNextRecord(const pldm_pdr_record* currRecord,
166 PdrEntry& pdrEntry) = 0;
167
168 /** @brief Get record handle of a PDR record
169 *
170 * @param[in] record - opaque pointer acting as a PDR record handle
171 *
172 * @return uint32_t - record handle assigned to PDR record; 0 if record is
173 * not found
174 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600175 virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0;
George Liue53193f2020-02-24 09:23:26 +0800176
177 /** @brief Get number of records in a PDR repository
178 *
179 * @return uint32_t - number of records
180 */
181 virtual uint32_t getRecordCount() = 0;
182
183 /** @brief Determine if records are empty in a PDR repository
184 *
185 * @return bool - true means empty and false means not empty
186 */
187 virtual bool empty() = 0;
188
189 protected:
190 pldm_pdr* repo;
191};
192
193/**
194 * @class Repo
195 *
196 * Wrapper class to handle the PDR APIs
197 *
198 * This class wraps operations used to handle PDR APIs.
199 */
200class Repo : public RepoInterface
201{
202 public:
Patrick Williams6da4f912023-05-10 07:50:53 -0500203 Repo(pldm_pdr* repo) : RepoInterface(repo) {}
George Liue53193f2020-02-24 09:23:26 +0800204
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600205 pldm_pdr* getPdr() const override;
George Liue53193f2020-02-24 09:23:26 +0800206
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600207 RecordHandle addRecord(const PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800208
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600209 const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800210
211 const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord,
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600212 PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800213
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600214 uint32_t getRecordHandle(const pldm_pdr_record* record) const override;
George Liue53193f2020-02-24 09:23:26 +0800215
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600216 uint32_t getRecordCount() override;
George Liue53193f2020-02-24 09:23:26 +0800217
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600218 bool empty() override;
George Liue53193f2020-02-24 09:23:26 +0800219};
220
Tom Josephb4268602020-04-17 17:20:45 +0530221/** @brief Parse the State Sensor PDR and return the parsed sensor info which
222 * will be used to lookup the sensor info in the PlatformEventMessage
223 * command of sensorEvent type.
224 *
225 * @param[in] stateSensorPdr - state sensor PDR
226 *
227 * @return terminus handle, sensor ID and parsed sensor info
228 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400229std::tuple<pldm::pdr::TerminusHandle, pldm::pdr::SensorID,
230 pldm::pdr::SensorInfo>
Tom Josephb4268602020-04-17 17:20:45 +0530231 parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr);
232
George Liu682ee182020-12-25 15:24:33 +0800233/** @brief Parse FRU record table and return the vector of the FRU record data
234 * format structure
235 *
236 * @param[in] fruData - fru data
237 * @param[in] fruLen - fru len
238 *
239 * @return std::vector<FruRecordDataFormat> - the vector of the FRU record data
240 * format structure
241 */
242std::vector<FruRecordDataFormat> parseFruRecordTable(const uint8_t* fruData,
243 size_t fruLen);
244
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500245/** @brief Return the size of data type based on the effecterDataSize enum value
246 *
247 * @param[in] effecterDataSize - Bitwidth and format of setting effecter value
248 * @return[out] Map the effecterDataSize enum value to datatype and return the
249 * size of dataType
250 */
251size_t getEffecterDataSize(uint8_t effecterDataSize);
252
George Liue53193f2020-02-24 09:23:26 +0800253} // namespace pdr_utils
254} // namespace responder
255} // namespace pldm