blob: b19519c90f1c88acc853b723f369c382d9b76537 [file] [log] [blame]
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05001#pragma once
2
3#include "config.h"
4
George Liu6492f522020-06-16 10:34:05 +08005#include "libpldm/fru.h"
6#include "libpldm/pdr.h"
7
Deepak Kodihalli70e8db02019-10-21 00:59:46 -05008#include "fru_parser.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05009#include "pldmd/handler.hpp"
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050010
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050011#include <sdbusplus/message.hpp>
George Liu6492f522020-06-16 10:34:05 +080012
13#include <map>
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050014#include <string>
15#include <variant>
16#include <vector>
17
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050018namespace pldm
19{
20
21namespace responder
22{
23
24namespace dbus
25{
26
27using Value =
28 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
29 uint64_t, double, std::string, std::vector<uint8_t>>;
30using PropertyMap = std::map<Property, Value>;
31using InterfaceMap = std::map<Interface, PropertyMap>;
32using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050033using ObjectPath = std::string;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050034
35} // namespace dbus
36
37/** @class FruImpl
38 *
39 * @brief Builds the PLDM FRU table containing the FRU records
40 */
41class FruImpl
42{
43 public:
44 /* @brief Header size for FRU record, it includes the FRU record set
45 * identifier, FRU record type, Number of FRU fields, Encoding type
46 * of FRU fields
47 */
48 static constexpr size_t recHeaderSize =
49 sizeof(struct pldm_fru_record_data_format) -
50 sizeof(struct pldm_fru_record_tlv);
51
Tom Joseph33e9c7e2020-06-11 22:09:52 +053052 /** @brief Constructor for FruImpl, the configPath is consumed to build the
53 * FruParser object.
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050054 *
55 * @param[in] configPath - path to the directory containing config files
Tom Joseph33e9c7e2020-06-11 22:09:52 +053056 * for PLDM FRU
57 * @param[in] pdrRepo - opaque pointer to PDR repository
58 * @param[in] entityTree - opaque pointer to the entity association tree
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050059 */
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050060 FruImpl(const std::string& configPath, pldm_pdr* pdrRepo,
Tom Joseph33e9c7e2020-06-11 22:09:52 +053061 pldm_entity_association_tree* entityTree) :
62 parser(configPath),
63 pdrRepo(pdrRepo), entityTree(entityTree)
64 {}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050065
66 /** @brief Total length of the FRU table in bytes, this excludes the pad
67 * bytes and the checksum.
68 *
69 * @return size of the FRU table
70 */
71 uint32_t size() const
72 {
73 return table.size() - padBytes;
74 }
75
76 /** @brief The checksum of the contents of the FRU table
77 *
78 * @return checksum
79 */
80 uint32_t checkSum() const
81 {
82 return checksum;
83 }
84
85 /** @brief Number of record set identifiers in the FRU tables
86 *
87 * @return number of record set identifiers
88 */
89 uint16_t numRSI() const
90 {
91 return rsi;
92 }
93
94 /** @brief The number of FRU records in the table
95 *
96 * @return number of FRU records
97 */
98 uint16_t numRecords() const
99 {
100 return numRecs;
101 }
102
103 /** @brief Get the FRU table
104 *
105 * @param[out] - Populate response with the FRU table
106 */
107 void getFRUTable(Response& response);
108
John Wang9e82ad12020-06-12 10:53:32 +0800109 /** @brief Get FRU Record Table By Option
110 * @param[out] response - Populate response with the FRU table got by
111 * options
112 * @param[in] fruTableHandle - The fru table handle
113 * @param[in] recordSetIdentifer - The record set identifier
114 * @param[in] recordType - The record type
115 * @param[in] fieldType - The field type
116 */
117 int getFRURecordByOption(Response& response, uint16_t fruTableHandle,
118 uint16_t recordSetIdentifer, uint8_t recordType,
119 uint8_t fieldType);
120
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530121 /** @brief FRU table is built by processing the D-Bus inventory namespace
122 * based on the config files for FRU. The table is populated based
123 * on the isBuilt flag.
124 */
125 void buildFRUTable();
126
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500127 private:
128 uint16_t nextRSI()
129 {
130 return ++rsi;
131 }
132
133 uint16_t rsi = 0;
134 uint16_t numRecs = 0;
135 uint8_t padBytes = 0;
136 std::vector<uint8_t> table;
137 uint32_t checksum = 0;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530138 bool isBuilt = false;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500139
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530140 fru_parser::FruParser parser;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500141 pldm_pdr* pdrRepo;
142 pldm_entity_association_tree* entityTree;
143
144 std::map<dbus::ObjectPath, pldm_entity_node*> objToEntityNode{};
145
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500146 /** @brief populateRecord builds the FRU records for an instance of FRU and
147 * updates the FRU table with the FRU records.
148 *
149 * @param[in] interfaces - D-Bus interfaces and the associated property
150 * values for the FRU
151 * @param[in] recordInfos - FRU record info to build the FRU records
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500152 * @param[in/out] entity - PLDM entity corresponding to FRU instance
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500153 */
154 void populateRecords(const pldm::responder::dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500155 const fru_parser::FruRecordInfos& recordInfos,
156 const pldm_entity& entity);
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500157};
158
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500159namespace fru
160{
161
162class Handler : public CmdHandler
163{
164
165 public:
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500166 Handler(const std::string& configPath, pldm_pdr* pdrRepo,
167 pldm_entity_association_tree* entityTree) :
168 impl(configPath, pdrRepo, entityTree)
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500169 {
170 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE_METADATA,
171 [this](const pldm_msg* request, size_t payloadLength) {
172 return this->getFRURecordTableMetadata(
173 request, payloadLength);
174 });
175
176 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE,
177 [this](const pldm_msg* request, size_t payloadLength) {
178 return this->getFRURecordTable(request,
179 payloadLength);
180 });
John Wang9e82ad12020-06-12 10:53:32 +0800181 handlers.emplace(PLDM_GET_FRU_RECORD_BY_OPTION,
182 [this](const pldm_msg* request, size_t payloadLength) {
183 return this->getFRURecordByOption(request,
184 payloadLength);
185 });
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500186 }
187
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500188 /** @brief Handler for Get FRURecordTableMetadata
189 *
190 * @param[in] request - Request message payload
191 * @param[in] payloadLength - Request payload length
192 *
193 * @return PLDM response message
194 */
195 Response getFRURecordTableMetadata(const pldm_msg* request,
196 size_t payloadLength);
197
198 /** @brief Handler for GetFRURecordTable
199 *
200 * @param[in] request - Request message payload
201 * @param[in] payloadLength - Request payload length
202 *
203 * @return PLDM response message
204 */
205 Response getFRURecordTable(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500206
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530207 /** @brief Build FRU table is bnot already built
208 *
209 */
210 void buildFRUTable()
211 {
212 impl.buildFRUTable();
213 }
214
John Wang9e82ad12020-06-12 10:53:32 +0800215 /** @brief Handler for GetFRURecordByOption
216 *
217 * @param[in] request - Request message payload
218 * @param[in] payloadLength - Request payload length
219 *
220 * @return PLDM response message
221 */
222 Response getFRURecordByOption(const pldm_msg* request,
223 size_t payloadLength);
224
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500225 private:
226 FruImpl impl;
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500227};
228
229} // namespace fru
230
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500231} // namespace responder
232
233} // namespace pldm