blob: 45bd0410275bab8024b6f0560d5647b0fa19699c [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301
2#include "platform.hpp"
3
George Liu83409572019-12-24 18:42:54 +08004#include "utils.hpp"
5
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05306namespace pldm
7{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05308namespace responder
9{
Sampa Misraa2fa0702019-05-31 01:28:55 -050010namespace platform
11{
12
Deepak Kodihallic682fe22020-03-04 00:42:54 -060013using InternalFailure =
14 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
15
George Liu1ec85d42020-02-12 16:05:32 +080016static const Json empty{};
17
18void Handler::addDbusObjMaps(uint16_t effecterId,
19 std::tuple<DbusMappings, DbusValMaps> dbusObj)
20{
21 dbusObjMaps.emplace(effecterId, dbusObj);
22}
23
24const std::tuple<DbusMappings, DbusValMaps>&
25 Handler::getDbusObjMaps(uint16_t effecterId) const
26{
27 return dbusObjMaps.at(effecterId);
28}
29
Deepak Kodihallic682fe22020-03-04 00:42:54 -060030void Handler::generateStateEffecterRepo(const Json& json, Repo& repo)
31{
32 static const std::vector<Json> emptyList{};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060033 auto entries = json.value("entries", emptyList);
34 for (const auto& e : entries)
35 {
36 size_t pdrSize = 0;
37 auto effecters = e.value("effecters", emptyList);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060038 for (const auto& effecter : effecters)
39 {
40 auto set = effecter.value("set", empty);
41 auto statesSize = set.value("size", 0);
42 if (!statesSize)
43 {
44 std::cerr << "Malformed PDR JSON return "
45 "pdrEntry;- no state set "
46 "info, TYPE="
47 << PLDM_STATE_EFFECTER_PDR << "\n";
48 throw InternalFailure();
49 }
50 pdrSize += sizeof(state_effecter_possible_states) -
51 sizeof(bitfield8_t) + (sizeof(bitfield8_t) * statesSize);
52 }
53 pdrSize += sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
54
55 std::vector<uint8_t> entry{};
56 entry.resize(pdrSize);
57
58 pldm_state_effecter_pdr* pdr =
59 reinterpret_cast<pldm_state_effecter_pdr*>(entry.data());
60 pdr->hdr.record_handle = 0;
61 pdr->hdr.version = 1;
62 pdr->hdr.type = PLDM_STATE_EFFECTER_PDR;
63 pdr->hdr.record_change_num = 0;
64 pdr->hdr.length = pdrSize - sizeof(pldm_pdr_hdr);
65
66 pdr->terminus_handle = 0;
67 pdr->effecter_id = this->getNextEffecterId();
68 pdr->entity_type = e.value("type", 0);
69 pdr->entity_instance = e.value("instance", 0);
70 pdr->container_id = e.value("container", 0);
71 pdr->effecter_semantic_id = 0;
72 pdr->effecter_init = PLDM_NO_INIT;
73 pdr->has_description_pdr = false;
74 pdr->composite_effecter_count = effecters.size();
75
George Liu1ec85d42020-02-12 16:05:32 +080076 DbusMappings dbusMappings{};
77 DbusValMaps dbusValMaps{};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060078 uint8_t* start =
79 entry.data() + sizeof(pldm_state_effecter_pdr) - sizeof(uint8_t);
80 for (const auto& effecter : effecters)
81 {
82 auto set = effecter.value("set", empty);
83 state_effecter_possible_states* possibleStates =
84 reinterpret_cast<state_effecter_possible_states*>(start);
85 possibleStates->state_set_id = set.value("id", 0);
86 possibleStates->possible_states_size = set.value("size", 0);
87
88 start += sizeof(possibleStates->state_set_id) +
89 sizeof(possibleStates->possible_states_size);
90 static const std::vector<uint8_t> emptyStates{};
George Liu1ec85d42020-02-12 16:05:32 +080091 PossibleValues stateValues;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060092 auto states = set.value("states", emptyStates);
93 for (const auto& state : states)
94 {
95 auto index = state / 8;
96 auto bit = state - (index * 8);
97 bitfield8_t* bf = reinterpret_cast<bitfield8_t*>(start + index);
98 bf->byte |= 1 << bit;
George Liu1ec85d42020-02-12 16:05:32 +080099 stateValues.emplace_back(std::move(state));
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600100 }
101 start += possibleStates->possible_states_size;
102
George Liu1ec85d42020-02-12 16:05:32 +0800103 auto dbusEntry = effecter.value("dbus", empty);
104 auto objectPath = dbusEntry.value("path", "");
105 auto interface = dbusEntry.value("interface", "");
106 auto propertyName = dbusEntry.value("property_name", "");
107 auto propertyType = dbusEntry.value("property_type", "");
108 pldm::utils::DBusMapping dbusMapping{objectPath, interface,
109 propertyName, propertyType};
110 dbusMappings.emplace_back(std::move(dbusMapping));
111
112 Json propValues = dbusEntry["property_values"];
113 StatestoDbusVal dbusIdToValMap =
114 populateMapping(propertyType, propValues, stateValues);
115 if (!dbusIdToValMap.empty())
116 {
117 dbusValMaps.emplace_back(std::move(dbusIdToValMap));
118 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600119 }
George Liu1ec85d42020-02-12 16:05:32 +0800120
121 addDbusObjMaps(
122 pdr->effecter_id,
123 std::make_tuple(std::move(dbusMappings), std::move(dbusValMaps)));
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600124 PdrEntry pdrEntry{};
125 pdrEntry.data = entry.data();
126 pdrEntry.size = pdrSize;
127 repo.addRecord(pdrEntry);
128 }
129}
130
131void Handler::generate(const std::string& dir, Repo& repo)
132{
133 // A map of PDR type to a lambda that handles creation of that PDR type.
134 // The lambda essentially would parse the platform specific PDR JSONs to
135 // generate the PDR structures. This function iterates through the map to
136 // invoke all lambdas, so that all PDR types can be created.
137 const std::map<Type, std::function<void(const Json& json, Repo& repo)>>
138 generators = {
139 {PLDM_STATE_EFFECTER_PDR, [this](const auto& json, Repo& repo) {
140 generateStateEffecterRepo(json, repo);
141 }}};
142
143 Type pdrType{};
144 for (const auto& dirEntry : fs::directory_iterator(dir))
145 {
146 try
147 {
148 auto json = readJson(dirEntry.path().string());
149 if (!json.empty())
150 {
George Liu1ec85d42020-02-12 16:05:32 +0800151 auto effecterPDRs = json.value("effecterPDRs", empty);
152 for (const auto& effecter : effecterPDRs)
153 {
154 pdrType = effecter.value("pdrType", 0);
155 generators.at(pdrType)(effecter, repo);
156 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600157 }
158 }
159 catch (const InternalFailure& e)
160 {
161 std::cerr << "PDR config directory does not exist or empty, TYPE= "
162 << pdrType << "PATH= " << dirEntry
163 << " ERROR=" << e.what() << "\n";
164 }
165 catch (const Json::exception& e)
166 {
167 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
168 << " ERROR=" << e.what() << "\n";
169 pldm::utils::reportError(
170 "xyz.openbmc_project.bmc.pldm.InternalFailure");
171 }
172 catch (const std::exception& e)
173 {
174 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
175 << " ERROR=" << e.what() << "\n";
176 pldm::utils::reportError(
177 "xyz.openbmc_project.bmc.pldm.InternalFailure");
178 }
179 }
180}
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530181
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600182Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530183{
184 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
185 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
186
187 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
188 {
George Liufb8611d2019-12-06 10:14:15 +0800189 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530190 }
191
192 uint32_t recordHandle{};
193 uint32_t dataTransferHandle{};
194 uint8_t transferOpFlag{};
195 uint16_t reqSizeBytes{};
196 uint16_t recordChangeNum{};
197
George Liufb8611d2019-12-06 10:14:15 +0800198 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
199 &dataTransferHandle, &transferOpFlag,
200 &reqSizeBytes, &recordChangeNum);
201 if (rc != PLDM_SUCCESS)
202 {
203 return CmdHandler::ccOnlyResponse(request, rc);
204 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530205
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530206 uint16_t respSizeBytes{};
207 uint8_t* recordData = nullptr;
208 try
209 {
George Liue53193f2020-02-24 09:23:26 +0800210 pdr_utils::PdrEntry e;
211 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
212 if (record == NULL)
213 {
214 return CmdHandler::ccOnlyResponse(
215 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
216 }
217
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530218 if (reqSizeBytes)
219 {
George Liue53193f2020-02-24 09:23:26 +0800220 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530221 if (respSizeBytes > reqSizeBytes)
222 {
223 respSizeBytes = reqSizeBytes;
224 }
George Liue53193f2020-02-24 09:23:26 +0800225 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530226 }
227 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
228 respSizeBytes,
229 0);
230 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500231 rc = encode_get_pdr_resp(
232 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
233 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800234 if (rc != PLDM_SUCCESS)
235 {
236 return ccOnlyResponse(request, rc);
237 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530238 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530239 catch (const std::exception& e)
240 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600241 std::cerr << "Error accessing PDR, HANDLE=" << recordHandle
242 << " ERROR=" << e.what() << "\n";
George Liufb8611d2019-12-06 10:14:15 +0800243 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530244 }
245 return response;
246}
247
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600248Response Handler::setStateEffecterStates(const pldm_msg* request,
249 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500250{
251 Response response(
252 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
253 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
254 uint16_t effecterId;
255 uint8_t compEffecterCnt;
256 constexpr auto maxCompositeEffecterCnt = 8;
257 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
258 {0, 0});
259
260 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
261 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
262 sizeof(set_effecter_state_field)))
263 {
George Liufb8611d2019-12-06 10:14:15 +0800264 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500265 }
266
267 int rc = decode_set_state_effecter_states_req(request, payloadLength,
268 &effecterId, &compEffecterCnt,
269 stateField.data());
270
George Liufb8611d2019-12-06 10:14:15 +0800271 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500272 {
George Liufb8611d2019-12-06 10:14:15 +0800273 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500274 }
275
George Liufb8611d2019-12-06 10:14:15 +0800276 stateField.resize(compEffecterCnt);
277 const pldm::utils::DBusHandler dBusIntf;
278 rc = setStateEffecterStatesHandler<pldm::utils::DBusHandler>(
279 dBusIntf, effecterId, stateField);
280 if (rc != PLDM_SUCCESS)
281 {
282 return CmdHandler::ccOnlyResponse(request, rc);
283 }
284
285 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
286 responsePtr);
287 if (rc != PLDM_SUCCESS)
288 {
289 return ccOnlyResponse(request, rc);
290 }
291
Sampa Misraa2fa0702019-05-31 01:28:55 -0500292 return response;
293}
294
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600295} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530296} // namespace responder
297} // namespace pldm