blob: 4c2af02df421a48c23b1d8dc299f73af8ed37ab6 [file] [log] [blame]
George Liue53193f2020-02-24 09:23:26 +08001#pragma once
2
3#include <stdint.h>
4
5#include <filesystem>
6#include <fstream>
7#include <functional>
8#include <iostream>
9#include <nlohmann/json.hpp>
10#include <string>
11#include <xyz/openbmc_project/Common/error.hpp>
12
13#include "libpldm/pdr.h"
14
15using InternalFailure =
16 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17
18namespace fs = std::filesystem;
19
20namespace pldm
21{
22
23namespace responder
24{
25
26namespace pdr_utils
27{
28
29/** @struct PdrEntry
30 * PDR entry structure that acts as a PDR record structure in the PDR
31 * repository to handle PDR APIs.
32 */
33struct PdrEntry
34{
35 uint8_t* data;
36 uint32_t size;
37 union
38 {
39 uint32_t recordHandle;
40 uint32_t nextRecordHandle;
41 } handle;
42};
43using Type = uint8_t;
44using Json = nlohmann::json;
45using RecordHandle = uint32_t;
46
47/** @brief Parse PDR JSON file and output Json object
48 *
49 * @param[in] path - path of PDR JSON file
50 *
51 * @return Json - Json object
52 */
53inline Json readJson(const std::string& path)
54{
55 fs::path dir(path);
56 if (!fs::exists(dir) || fs::is_empty(dir))
57 {
58 throw InternalFailure();
59 }
60
61 std::ifstream jsonFile(path);
62 if (!jsonFile.is_open())
63 {
64 std::cerr << "Error opening PDR JSON file, PATH=" << path << "\n";
65 return {};
66 }
67
68 return Json::parse(jsonFile);
69}
70
71/**
72 * @class RepoInterface
73 *
74 * @brief Abstract class describing the interface API to the PDR repository,
75 * this class wraps operations used to handle the new PDR APIs.
76 */
77class RepoInterface
78{
79 public:
80 virtual ~RepoInterface() = default;
81
82 /** @brief Get an opaque pldm_pdr structure
83 *
84 * @return pldm_pdr - pldm_pdr structure
85 */
86 virtual const pldm_pdr* getPdr() = 0;
87
88 /** @brief Add a PDR record to a PDR repository
89 *
90 * @param[in] pdrEntry - PDR records entry(data, size, recordHandle)
91 *
92 * @return uint32_t - record handle assigned to PDR record
93 */
94 virtual RecordHandle addRecord(const PdrEntry& pdrEntry) = 0;
95
96 /** @brief Get the first PDR record from a PDR repository
97 *
98 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
99 *
100 * @return opaque pointer acting as PDR record handle, will be NULL if
101 * record was not found
102 */
103 virtual const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry) = 0;
104
105 /** @brief Get the next PDR record from a PDR repository
106 *
107 * @param[in] currRecord - opaque pointer acting as a PDR record handle
108 * @param[in] pdrEntry - PDR records entry(data, size, nextRecordHandle)
109 *
110 * @return opaque pointer acting as PDR record handle, will be NULL if
111 * record was not found
112 */
113 virtual const pldm_pdr_record*
114 getNextRecord(const pldm_pdr_record* currRecord,
115 PdrEntry& pdrEntry) = 0;
116
117 /** @brief Get record handle of a PDR record
118 *
119 * @param[in] record - opaque pointer acting as a PDR record handle
120 *
121 * @return uint32_t - record handle assigned to PDR record; 0 if record is
122 * not found
123 */
124 virtual uint32_t getRecordHandle(const pldm_pdr_record* record) = 0;
125
126 /** @brief Get number of records in a PDR repository
127 *
128 * @return uint32_t - number of records
129 */
130 virtual uint32_t getRecordCount() = 0;
131
132 /** @brief Determine if records are empty in a PDR repository
133 *
134 * @return bool - true means empty and false means not empty
135 */
136 virtual bool empty() = 0;
137
138 protected:
139 pldm_pdr* repo;
140};
141
142/**
143 * @class Repo
144 *
145 * Wrapper class to handle the PDR APIs
146 *
147 * This class wraps operations used to handle PDR APIs.
148 */
149class Repo : public RepoInterface
150{
151 public:
152 Repo()
153 {
154 repo = pldm_pdr_init();
155 }
156
157 ~Repo()
158 {
159 pldm_pdr_destroy(repo);
160 }
161
162 const pldm_pdr* getPdr();
163
164 RecordHandle addRecord(const PdrEntry& pdrEntry);
165
166 const pldm_pdr_record* getFirstRecord(PdrEntry& pdrEntry);
167
168 const pldm_pdr_record* getNextRecord(const pldm_pdr_record* currRecord,
169 PdrEntry& pdrEntry);
170
171 uint32_t getRecordHandle(const pldm_pdr_record* record);
172
173 uint32_t getRecordCount();
174
175 bool empty();
176};
177
178} // namespace pdr_utils
179} // namespace responder
180} // namespace pldm