blob: 1f03863832589993526b252d2743159f52a66d13 [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#pragma once
2
George Liu1ec85d42020-02-12 16:05:32 +08003#include "utils.hpp"
4
George Liue53193f2020-02-24 09:23:26 +08005#include <stdint.h>
6
7#include <filesystem>
8#include <fstream>
9#include <functional>
10#include <iostream>
11#include <nlohmann/json.hpp>
12#include <string>
13#include <xyz/openbmc_project/Common/error.hpp>
14
15#include "libpldm/pdr.h"
16
17using InternalFailure =
18 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19
20namespace fs = std::filesystem;
21
22namespace pldm
23{
24
25namespace responder
26{
27
28namespace pdr_utils
29{
30
31/** @struct PdrEntry
32 * PDR entry structure that acts as a PDR record structure in the PDR
33 * repository to handle PDR APIs.
34 */
35struct PdrEntry
36{
37 uint8_t* data;
38 uint32_t size;
39 union
40 {
41 uint32_t recordHandle;
42 uint32_t nextRecordHandle;
43 } handle;
44};
45using Type = uint8_t;
46using Json = nlohmann::json;
47using RecordHandle = uint32_t;
George Liu1ec85d42020-02-12 16:05:32 +080048using State = uint8_t;
49using PossibleValues = std::vector<uint8_t>;
50
51/** @brief Map of DBus property State to attribute value
52 */
53using StatestoDbusVal = std::map<State, pldm::utils::PropertyValue>;
George Liue53193f2020-02-24 09:23:26 +080054
55/** @brief Parse PDR JSON file and output Json object
56 *
57 * @param[in] path - path of PDR JSON file
58 *
59 * @return Json - Json object
60 */
61inline Json readJson(const std::string& path)
62{
63 fs::path dir(path);
64 if (!fs::exists(dir) || fs::is_empty(dir))
65 {
66 throw InternalFailure();
67 }
68
69 std::ifstream jsonFile(path);
70 if (!jsonFile.is_open())
71 {
72 std::cerr << "Error opening PDR JSON file, PATH=" << path << "\n";
73 return {};
74 }
75
76 return Json::parse(jsonFile);
77}
78
George Liu1ec85d42020-02-12 16:05:32 +080079/** @brief Populate the mapping between D-Bus property stateId and attribute
80 * value for the effecter PDR enumeration attribute.
81 *
82 * @param[in] type - type of the D-Bus property
83 * @param[in] dBusValues - json array of D-Bus property values
84 * @param[in] pv - Possible values for the effecter PDR enumeration attribute
85 *
86 * @return StatestoDbusVal - Map of D-Bus property stateId to attribute value
87 */
88StatestoDbusVal populateMapping(const std::string& type, const Json& dBusValues,
89 const PossibleValues& pv);
90
George Liue53193f2020-02-24 09:23:26 +080091/**
92 * @class RepoInterface
93 *
94 * @brief Abstract class describing the interface API to the PDR repository,
95 * this class wraps operations used to handle the new PDR APIs.
96 */
97class RepoInterface
98{
99 public:
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600100 RepoInterface(pldm_pdr* repo) : repo(repo)
101 {
102 }
103
George Liue53193f2020-02-24 09:23:26 +0800104 virtual ~RepoInterface() = default;
105
106 /** @brief Get an opaque pldm_pdr structure
107 *
108 * @return pldm_pdr - pldm_pdr structure
109 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600110 virtual pldm_pdr* getPdr() const = 0;
George Liue53193f2020-02-24 09:23:26 +0800111
112 /** @brief Add a PDR record to a PDR repository
113 *
114 * @param[in] pdrEntry - PDR records entry(data, size, recordHandle)
115 *
116 * @return uint32_t - record handle assigned to PDR record
117 */
118 virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0;
119
120 /** @brief Get the first PDR record from a PDR repository
121 *
122 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
123 *
124 * @return opaque pointer acting as PDR record handle, will be NULL if
125 * record was not found
126 */
127 virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0;
128
129 /** @brief Get the next PDR record from a PDR repository
130 *
131 * @param[in] currRecord - opaque pointer acting as a PDR record handle
132 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
133 *
134 * @return opaque pointer acting as PDR record handle, will be NULL if
135 * record was not found
136 */
137 virtual const pldm_pdr_record*
138 getNextRecord(const pldm_pdr_record* currRecord,
139 PdrEntry& pdrEntry) = 0;
140
141 /** @brief Get record handle of a PDR record
142 *
143 * @param[in] record - opaque pointer acting as a PDR record handle
144 *
145 * @return uint32_t - record handle assigned to PDR record; 0 if record is
146 * not found
147 */
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600148 virtual uint32_t getRecordHandle(const pldm_pdr_record* record) const = 0;
George Liue53193f2020-02-24 09:23:26 +0800149
150 /** @brief Get number of records in a PDR repository
151 *
152 * @return uint32_t - number of records
153 */
154 virtual uint32_t getRecordCount() = 0;
155
156 /** @brief Determine if records are empty in a PDR repository
157 *
158 * @return bool - true means empty and false means not empty
159 */
160 virtual bool empty() = 0;
161
162 protected:
163 pldm_pdr* repo;
164};
165
166/**
167 * @class Repo
168 *
169 * Wrapper class to handle the PDR APIs
170 *
171 * This class wraps operations used to handle PDR APIs.
172 */
173class Repo : public RepoInterface
174{
175 public:
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600176 Repo(pldm_pdr* repo) : RepoInterface(repo)
George Liue53193f2020-02-24 09:23:26 +0800177 {
George Liue53193f2020-02-24 09:23:26 +0800178 }
179
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600180 pldm_pdr* getPdr() const override;
George Liue53193f2020-02-24 09:23:26 +0800181
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600182 RecordHandle addRecord(const PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800183
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600184 const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800185
186 const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord,
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600187 PdrEntry& pdrEntry) override;
George Liue53193f2020-02-24 09:23:26 +0800188
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600189 uint32_t getRecordHandle(const pldm_pdr_record* record) const override;
George Liue53193f2020-02-24 09:23:26 +0800190
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600191 uint32_t getRecordCount() override;
George Liue53193f2020-02-24 09:23:26 +0800192
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600193 bool empty() override;
George Liue53193f2020-02-24 09:23:26 +0800194};
195
196} // namespace pdr_utils
197} // namespace responder
198} // namespace pldm