blob: 986a3d2c299f82de02368a2a427ebd9958a3be1c [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;
George Liuc4ea6a92020-07-14 15:48:44 +080034using AssociatedEntityMap = std::map<ObjectPath, pldm_entity>;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050035
36} // namespace dbus
37
38/** @class FruImpl
39 *
40 * @brief Builds the PLDM FRU table containing the FRU records
41 */
42class FruImpl
43{
44 public:
45 /* @brief Header size for FRU record, it includes the FRU record set
46 * identifier, FRU record type, Number of FRU fields, Encoding type
47 * of FRU fields
48 */
49 static constexpr size_t recHeaderSize =
50 sizeof(struct pldm_fru_record_data_format) -
51 sizeof(struct pldm_fru_record_tlv);
52
Tom Joseph33e9c7e2020-06-11 22:09:52 +053053 /** @brief Constructor for FruImpl, the configPath is consumed to build the
54 * FruParser object.
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050055 *
56 * @param[in] configPath - path to the directory containing config files
Tom Joseph33e9c7e2020-06-11 22:09:52 +053057 * for PLDM FRU
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053058 * @param[in] fruMasterJsonPath - path to the file containing the FRU D-Bus
59 * Lookup Map
Tom Joseph33e9c7e2020-06-11 22:09:52 +053060 * @param[in] pdrRepo - opaque pointer to PDR repository
61 * @param[in] entityTree - opaque pointer to the entity association tree
Sampa Misrac073a202021-05-08 10:56:05 -050062 * @param[in] bmcEntityTree - opaque pointer to bmc's entity association
63 * tree
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050064 */
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053065 FruImpl(const std::string& configPath,
66 const std::filesystem::path& fruMasterJsonPath, pldm_pdr* pdrRepo,
Sampa Misrac073a202021-05-08 10:56:05 -050067 pldm_entity_association_tree* entityTree,
68 pldm_entity_association_tree* bmcEntityTree) :
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053069 parser(configPath, fruMasterJsonPath),
Sampa Misrac073a202021-05-08 10:56:05 -050070 pdrRepo(pdrRepo), entityTree(entityTree), bmcEntityTree(bmcEntityTree)
Tom Joseph33e9c7e2020-06-11 22:09:52 +053071 {}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050072
73 /** @brief Total length of the FRU table in bytes, this excludes the pad
74 * bytes and the checksum.
75 *
76 * @return size of the FRU table
77 */
78 uint32_t size() const
79 {
80 return table.size() - padBytes;
81 }
82
83 /** @brief The checksum of the contents of the FRU table
84 *
85 * @return checksum
86 */
87 uint32_t checkSum() const
88 {
89 return checksum;
90 }
91
92 /** @brief Number of record set identifiers in the FRU tables
93 *
94 * @return number of record set identifiers
95 */
96 uint16_t numRSI() const
97 {
98 return rsi;
99 }
100
101 /** @brief The number of FRU records in the table
102 *
103 * @return number of FRU records
104 */
105 uint16_t numRecords() const
106 {
107 return numRecs;
108 }
109
110 /** @brief Get the FRU table
111 *
112 * @param[out] - Populate response with the FRU table
113 */
114 void getFRUTable(Response& response);
115
John Wang9e82ad12020-06-12 10:53:32 +0800116 /** @brief Get FRU Record Table By Option
117 * @param[out] response - Populate response with the FRU table got by
118 * options
119 * @param[in] fruTableHandle - The fru table handle
120 * @param[in] recordSetIdentifer - The record set identifier
121 * @param[in] recordType - The record type
122 * @param[in] fieldType - The field type
123 */
124 int getFRURecordByOption(Response& response, uint16_t fruTableHandle,
125 uint16_t recordSetIdentifer, uint8_t recordType,
126 uint8_t fieldType);
127
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530128 /** @brief FRU table is built by processing the D-Bus inventory namespace
129 * based on the config files for FRU. The table is populated based
130 * on the isBuilt flag.
131 */
132 void buildFRUTable();
133
George Liuc4ea6a92020-07-14 15:48:44 +0800134 /** @brief Get std::map associated with the entity
135 * key: object path
136 * value: pldm_entity
137 *
138 * @return std::map<ObjectPath, pldm_entity>
139 */
140 inline const pldm::responder::dbus::AssociatedEntityMap&
141 getAssociateEntityMap() const
142 {
143 return associatedEntityMap;
144 }
145
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500146 /* @brief Method to populate the firmware version ID
147 *
148 * @return firmware version ID
149 */
150 std::string populatefwVersion();
151
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500152 private:
153 uint16_t nextRSI()
154 {
155 return ++rsi;
156 }
157
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600158 uint32_t nextRecordHandle()
159 {
160 return ++rh;
161 }
162
163 uint32_t rh = 0;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500164 uint16_t rsi = 0;
165 uint16_t numRecs = 0;
166 uint8_t padBytes = 0;
167 std::vector<uint8_t> table;
168 uint32_t checksum = 0;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530169 bool isBuilt = false;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500170
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530171 fru_parser::FruParser parser;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500172 pldm_pdr* pdrRepo;
173 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500174 pldm_entity_association_tree* bmcEntityTree;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500175
176 std::map<dbus::ObjectPath, pldm_entity_node*> objToEntityNode{};
177
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500178 /** @brief populateRecord builds the FRU records for an instance of FRU and
179 * updates the FRU table with the FRU records.
180 *
181 * @param[in] interfaces - D-Bus interfaces and the associated property
182 * values for the FRU
183 * @param[in] recordInfos - FRU record info to build the FRU records
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500184 * @param[in/out] entity - PLDM entity corresponding to FRU instance
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500185 */
George Liuc4ea6a92020-07-14 15:48:44 +0800186 void populateRecords(const dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500187 const fru_parser::FruRecordInfos& recordInfos,
188 const pldm_entity& entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800189
190 /** @brief Associate sensor/effecter to FRU entity
191 */
192 dbus::AssociatedEntityMap associatedEntityMap;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500193};
194
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500195namespace fru
196{
197
198class Handler : public CmdHandler
199{
200
201 public:
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530202 Handler(const std::string& configPath,
203 const std::filesystem::path& fruMasterJsonPath, pldm_pdr* pdrRepo,
Sampa Misrac073a202021-05-08 10:56:05 -0500204 pldm_entity_association_tree* entityTree,
205 pldm_entity_association_tree* bmcEntityTree) :
Manojkiran Eda03b01ca2021-06-29 08:55:09 +0530206 impl(configPath, fruMasterJsonPath, pdrRepo, entityTree, bmcEntityTree)
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500207 {
208 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE_METADATA,
209 [this](const pldm_msg* request, size_t payloadLength) {
210 return this->getFRURecordTableMetadata(
211 request, payloadLength);
212 });
213
214 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE,
215 [this](const pldm_msg* request, size_t payloadLength) {
216 return this->getFRURecordTable(request,
217 payloadLength);
218 });
John Wang9e82ad12020-06-12 10:53:32 +0800219 handlers.emplace(PLDM_GET_FRU_RECORD_BY_OPTION,
220 [this](const pldm_msg* request, size_t payloadLength) {
221 return this->getFRURecordByOption(request,
222 payloadLength);
223 });
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500224 }
225
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500226 /** @brief Handler for Get FRURecordTableMetadata
227 *
228 * @param[in] request - Request message payload
229 * @param[in] payloadLength - Request payload length
230 *
231 * @return PLDM response message
232 */
233 Response getFRURecordTableMetadata(const pldm_msg* request,
234 size_t payloadLength);
235
236 /** @brief Handler for GetFRURecordTable
237 *
238 * @param[in] request - Request message payload
239 * @param[in] payloadLength - Request payload length
240 *
241 * @return PLDM response message
242 */
243 Response getFRURecordTable(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500244
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530245 /** @brief Build FRU table is bnot already built
246 *
247 */
248 void buildFRUTable()
249 {
250 impl.buildFRUTable();
251 }
252
George Liuc4ea6a92020-07-14 15:48:44 +0800253 /** @brief Get std::map associated with the entity
254 * key: object path
255 * value: pldm_entity
256 *
257 * @return std::map<ObjectPath, pldm_entity>
258 */
259 const pldm::responder::dbus::AssociatedEntityMap&
260 getAssociateEntityMap() const
261 {
262 return impl.getAssociateEntityMap();
263 }
264
John Wang9e82ad12020-06-12 10:53:32 +0800265 /** @brief Handler for GetFRURecordByOption
266 *
267 * @param[in] request - Request message payload
268 * @param[in] payloadLength - Request payload length
269 *
270 * @return PLDM response message
271 */
272 Response getFRURecordByOption(const pldm_msg* request,
273 size_t payloadLength);
274
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500275 private:
276 FruImpl impl;
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500277};
278
279} // namespace fru
280
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500281} // namespace responder
282
283} // namespace pldm