blob: c890a870124f3da918eda79545cd8baea2a1113e [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
58 * @param[in] pdrRepo - opaque pointer to PDR repository
59 * @param[in] entityTree - opaque pointer to the entity association tree
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050060 */
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050061 FruImpl(const std::string& configPath, pldm_pdr* pdrRepo,
Tom Joseph33e9c7e2020-06-11 22:09:52 +053062 pldm_entity_association_tree* entityTree) :
63 parser(configPath),
64 pdrRepo(pdrRepo), entityTree(entityTree)
65 {}
Deepak Kodihalli70e8db02019-10-21 00:59:46 -050066
67 /** @brief Total length of the FRU table in bytes, this excludes the pad
68 * bytes and the checksum.
69 *
70 * @return size of the FRU table
71 */
72 uint32_t size() const
73 {
74 return table.size() - padBytes;
75 }
76
77 /** @brief The checksum of the contents of the FRU table
78 *
79 * @return checksum
80 */
81 uint32_t checkSum() const
82 {
83 return checksum;
84 }
85
86 /** @brief Number of record set identifiers in the FRU tables
87 *
88 * @return number of record set identifiers
89 */
90 uint16_t numRSI() const
91 {
92 return rsi;
93 }
94
95 /** @brief The number of FRU records in the table
96 *
97 * @return number of FRU records
98 */
99 uint16_t numRecords() const
100 {
101 return numRecs;
102 }
103
104 /** @brief Get the FRU table
105 *
106 * @param[out] - Populate response with the FRU table
107 */
108 void getFRUTable(Response& response);
109
John Wang9e82ad12020-06-12 10:53:32 +0800110 /** @brief Get FRU Record Table By Option
111 * @param[out] response - Populate response with the FRU table got by
112 * options
113 * @param[in] fruTableHandle - The fru table handle
114 * @param[in] recordSetIdentifer - The record set identifier
115 * @param[in] recordType - The record type
116 * @param[in] fieldType - The field type
117 */
118 int getFRURecordByOption(Response& response, uint16_t fruTableHandle,
119 uint16_t recordSetIdentifer, uint8_t recordType,
120 uint8_t fieldType);
121
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530122 /** @brief FRU table is built by processing the D-Bus inventory namespace
123 * based on the config files for FRU. The table is populated based
124 * on the isBuilt flag.
125 */
126 void buildFRUTable();
127
George Liuc4ea6a92020-07-14 15:48:44 +0800128 /** @brief Get std::map associated with the entity
129 * key: object path
130 * value: pldm_entity
131 *
132 * @return std::map<ObjectPath, pldm_entity>
133 */
134 inline const pldm::responder::dbus::AssociatedEntityMap&
135 getAssociateEntityMap() const
136 {
137 return associatedEntityMap;
138 }
139
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500140 private:
141 uint16_t nextRSI()
142 {
143 return ++rsi;
144 }
145
146 uint16_t rsi = 0;
147 uint16_t numRecs = 0;
148 uint8_t padBytes = 0;
149 std::vector<uint8_t> table;
150 uint32_t checksum = 0;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530151 bool isBuilt = false;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500152
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530153 fru_parser::FruParser parser;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500154 pldm_pdr* pdrRepo;
155 pldm_entity_association_tree* entityTree;
156
157 std::map<dbus::ObjectPath, pldm_entity_node*> objToEntityNode{};
158
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500159 /** @brief populateRecord builds the FRU records for an instance of FRU and
160 * updates the FRU table with the FRU records.
161 *
162 * @param[in] interfaces - D-Bus interfaces and the associated property
163 * values for the FRU
164 * @param[in] recordInfos - FRU record info to build the FRU records
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500165 * @param[in/out] entity - PLDM entity corresponding to FRU instance
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500166 */
George Liuc4ea6a92020-07-14 15:48:44 +0800167 void populateRecords(const dbus::InterfaceMap& interfaces,
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500168 const fru_parser::FruRecordInfos& recordInfos,
169 const pldm_entity& entity);
George Liuc4ea6a92020-07-14 15:48:44 +0800170
171 /** @brief Associate sensor/effecter to FRU entity
172 */
173 dbus::AssociatedEntityMap associatedEntityMap;
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500174};
175
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500176namespace fru
177{
178
179class Handler : public CmdHandler
180{
181
182 public:
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500183 Handler(const std::string& configPath, pldm_pdr* pdrRepo,
184 pldm_entity_association_tree* entityTree) :
185 impl(configPath, pdrRepo, entityTree)
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500186 {
187 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE_METADATA,
188 [this](const pldm_msg* request, size_t payloadLength) {
189 return this->getFRURecordTableMetadata(
190 request, payloadLength);
191 });
192
193 handlers.emplace(PLDM_GET_FRU_RECORD_TABLE,
194 [this](const pldm_msg* request, size_t payloadLength) {
195 return this->getFRURecordTable(request,
196 payloadLength);
197 });
John Wang9e82ad12020-06-12 10:53:32 +0800198 handlers.emplace(PLDM_GET_FRU_RECORD_BY_OPTION,
199 [this](const pldm_msg* request, size_t payloadLength) {
200 return this->getFRURecordByOption(request,
201 payloadLength);
202 });
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500203 }
204
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500205 /** @brief Handler for Get FRURecordTableMetadata
206 *
207 * @param[in] request - Request message payload
208 * @param[in] payloadLength - Request payload length
209 *
210 * @return PLDM response message
211 */
212 Response getFRURecordTableMetadata(const pldm_msg* request,
213 size_t payloadLength);
214
215 /** @brief Handler for GetFRURecordTable
216 *
217 * @param[in] request - Request message payload
218 * @param[in] payloadLength - Request payload length
219 *
220 * @return PLDM response message
221 */
222 Response getFRURecordTable(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500223
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530224 /** @brief Build FRU table is bnot already built
225 *
226 */
227 void buildFRUTable()
228 {
229 impl.buildFRUTable();
230 }
231
George Liuc4ea6a92020-07-14 15:48:44 +0800232 /** @brief Get std::map associated with the entity
233 * key: object path
234 * value: pldm_entity
235 *
236 * @return std::map<ObjectPath, pldm_entity>
237 */
238 const pldm::responder::dbus::AssociatedEntityMap&
239 getAssociateEntityMap() const
240 {
241 return impl.getAssociateEntityMap();
242 }
243
John Wang9e82ad12020-06-12 10:53:32 +0800244 /** @brief Handler for GetFRURecordByOption
245 *
246 * @param[in] request - Request message payload
247 * @param[in] payloadLength - Request payload length
248 *
249 * @return PLDM response message
250 */
251 Response getFRURecordByOption(const pldm_msg* request,
252 size_t payloadLength);
253
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500254 private:
255 FruImpl impl;
Deepak Kodihallie60c5822019-10-23 03:26:15 -0500256};
257
258} // namespace fru
259
Deepak Kodihalli70e8db02019-10-21 00:59:46 -0500260} // namespace responder
261
262} // namespace pldm