blob: 8f4f82560cc2f94c244d8e08d40a38116f6be408 [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#pragma once
2
Tom Josephb4268602020-04-17 17:20:45 +05303#include "types.hpp"
George Liu1ec85d42020-02-12 16:05:32 +08004#include "utils.hpp"
5
George Liue53193f2020-02-24 09:23:26 +08006#include <stdint.h>
7
8#include <filesystem>
9#include <fstream>
10#include <functional>
11#include <iostream>
12#include <nlohmann/json.hpp>
13#include <string>
14#include <xyz/openbmc_project/Common/error.hpp>
15
16#include "libpldm/pdr.h"
17
18using InternalFailure =
19 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20
21namespace fs = std::filesystem;
22
23namespace pldm
24{
25
26namespace responder
27{
28
29namespace pdr_utils
30{
31
32/** @struct PdrEntry
33 * PDR entry structure that acts as a PDR record structure in the PDR
34 * repository to handle PDR APIs.
35 */
36struct PdrEntry
37{
38 uint8_t* data;
39 uint32_t size;
40 union
41 {
42 uint32_t recordHandle;
43 uint32_t nextRecordHandle;
44 } handle;
45};
46using Type = uint8_t;
47using Json = nlohmann::json;
48using RecordHandle = uint32_t;
George Liu1ec85d42020-02-12 16:05:32 +080049using State = uint8_t;
50using PossibleValues = std::vector<uint8_t>;
51
52/** @brief Map of DBus property State to attribute value
53 */
54using StatestoDbusVal = std::map<State, pldm::utils::PropertyValue>;
George Liua2870722020-02-11 11:09:30 +080055using DbusMappings = std::vector<pldm::utils::DBusMapping>;
56using DbusValMaps = std::vector<StatestoDbusVal>;
George Liue53193f2020-02-24 09:23:26 +080057
58/** @brief Parse PDR JSON file and output Json object
59 *
60 * @param[in] path - path of PDR JSON file
61 *
62 * @return Json - Json object
63 */
64inline Json readJson(const std::string& path)
65{
66 fs::path dir(path);
67 if (!fs::exists(dir) || fs::is_empty(dir))
68 {
69 throw InternalFailure();
70 }
71
72 std::ifstream jsonFile(path);
73 if (!jsonFile.is_open())
74 {
75 std::cerr << "Error opening PDR JSON file, PATH=" << path << "\n";
76 return {};
77 }
78
79 return Json::parse(jsonFile);
80}
81
George Liu1ec85d42020-02-12 16:05:32 +080082/** @brief Populate the mapping between D-Bus property stateId and attribute
83 * value for the effecter PDR enumeration attribute.
84 *
85 * @param[in] type - type of the D-Bus property
86 * @param[in] dBusValues - json array of D-Bus property values
87 * @param[in] pv - Possible values for the effecter PDR enumeration attribute
88 *
89 * @return StatestoDbusVal - Map of D-Bus property stateId to attribute value
90 */
91StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
92 const PossibleValues& pv);
93
George Liue53193f2020-02-24 09:23:26 +080094/**
95 * @class RepoInterface
96 *
97 * @brief Abstract class describing the interface API to the PDR repository,
98 * this class wraps operations used to handle the new PDR APIs.
99 */
100class RepoInterface
101{
102 public:
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600103 RepoInterface(pldm_pdr* repo) : repo(repo)
104 {
105 }
106
George Liue53193f2020-02-24 09:23:26 +0800107 virtual ~RepoInterface() = default;
108
109 /** @brief Get an opaque pldm_pdr structure
110 *
111 * @return pldm_pdr - pldm_pdr structure
112 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600113 virtual pldm_pdr* getPdr() const = 0;
George Liue53193f2020-02-24 09:23:26 +0800114
115 /** @brief Add a PDR record to a PDR repository
116 *
117 * @param[in] pdrEntry - PDR records entry(data, size, recordHandle)
118 *
119 * @return uint32_t - record handle assigned to PDR record
120 */
121 virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0;
122
123 /** @brief Get the first PDR record from a PDR repository
124 *
125 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
126 *
127 * @return opaque pointer acting as PDR record handle, will be NULL if
128 * record was not found
129 */
130 virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0;
131
132 /** @brief Get the next PDR record from a PDR repository
133 *
134 * @param[in] currRecord - opaque pointer acting as a PDR record handle
135 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
136 *
137 * @return opaque pointer acting as PDR record handle, will be NULL if
138 * record was not found
139 */
140 virtual const pldm_pdr_record*
141 getNextRecord(const pldm_pdr_record* currRecord,
142 PdrEntry& pdrEntry) = 0;
143
144 /** @brief Get record handle of a PDR record
145 *
146 * @param[in] record - opaque pointer acting as a PDR record handle
147 *
148 * @return uint32_t - record handle assigned to PDR record; 0 if record is
149 * not found
150 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600151 virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0;
George Liue53193f2020-02-24 09:23:26 +0800152
153 /** @brief Get number of records in a PDR repository
154 *
155 * @return uint32_t - number of records
156 */
157 virtual uint32_t getRecordCount() = 0;
158
159 /** @brief Determine if records are empty in a PDR repository
160 *
161 * @return bool - true means empty and false means not empty
162 */
163 virtual bool empty() = 0;
164
165 protected:
166 pldm_pdr* repo;
167};
168
169/**
170 * @class Repo
171 *
172 * Wrapper class to handle the PDR APIs
173 *
174 * This class wraps operations used to handle PDR APIs.
175 */
176class Repo : public RepoInterface
177{
178 public:
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600179 Repo(pldm_pdr* repo) : RepoInterface(repo)
George Liue53193f2020-02-24 09:23:26 +0800180 {
George Liue53193f2020-02-24 09:23:26 +0800181 }
182
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600183 pldm_pdr* getPdr() const override;
George Liue53193f2020-02-24 09:23:26 +0800184
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600185 RecordHandle addRecord(const PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800186
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600187 const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800188
189 const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord,
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600190 PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800191
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600192 uint32_t getRecordHandle(const pldm_pdr_record* record) const override;
George Liue53193f2020-02-24 09:23:26 +0800193
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600194 uint32_t getRecordCount() override;
George Liue53193f2020-02-24 09:23:26 +0800195
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600196 bool empty() override;
George Liue53193f2020-02-24 09:23:26 +0800197};
198
Tom Josephb4268602020-04-17 17:20:45 +0530199using namespace pldm::pdr;
200/** @brief Parse the State Sensor PDR and return the parsed sensor info which
201 * will be used to lookup the sensor info in the PlatformEventMessage
202 * command of sensorEvent type.
203 *
204 * @param[in] stateSensorPdr - state sensor PDR
205 *
206 * @return terminus handle, sensor ID and parsed sensor info
207 */
208std::tuple<TerminusHandle, SensorID, SensorInfo>
209 parseStateSensorPDR(const std::vector<uint8_t>& stateSensorPdr);
210
George Liue53193f2020-02-24 09:23:26 +0800211} // namespace pdr_utils
212} // namespace responder
213} // namespace pldm