blob: 6796ee5a30e4fc2ea27884ffb274cbc81f2ee014 [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301
2#include "platform.hpp"
3
Sampa Misraaea5dde2020-08-31 08:33:47 -05004#include "libpldm/entity.h"
5#include "libpldm/state_set.h"
6
Tom Josephb70a1962020-07-13 12:56:31 +05307#include "common/types.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05008#include "common/utils.hpp"
Tom Josephc4959c32020-04-20 19:50:16 +05309#include "event_parser.hpp"
Sampa Misra12afe112020-05-25 11:40:44 -050010#include "pdr.hpp"
George Liu456c9a22020-01-13 11:36:22 +080011#include "pdr_numeric_effecter.hpp"
George Liua2870722020-02-11 11:09:30 +080012#include "pdr_state_effecter.hpp"
George Liuadbe1722020-05-09 19:20:19 +080013#include "pdr_state_sensor.hpp"
George Liu362c18d2020-05-14 09:46:36 +080014#include "pdr_utils.hpp"
George Liueccb0c52020-01-14 11:09:56 +080015#include "platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +080016#include "platform_state_effecter.hpp"
George Liu362c18d2020-05-14 09:46:36 +080017#include "platform_state_sensor.hpp"
George Liu83409572019-12-24 18:42:54 +080018
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053019namespace pldm
20{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053021namespace responder
22{
Sampa Misraa2fa0702019-05-31 01:28:55 -050023namespace platform
24{
25
Deepak Kodihallic682fe22020-03-04 00:42:54 -060026using InternalFailure =
27 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
28
George Liu1ec85d42020-02-12 16:05:32 +080029static const Json empty{};
30
George Liua2870722020-02-11 11:09:30 +080031void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080032 uint16_t id,
33 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
34 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080035{
George Liuadbe1722020-05-09 19:20:19 +080036 if (typeId == TypeId::PLDM_SENSOR_ID)
37 {
38 sensorDbusObjMaps.emplace(id, dbusObj);
39 }
40 else
41 {
42 effecterDbusObjMaps.emplace(id, dbusObj);
43 }
George Liu1ec85d42020-02-12 16:05:32 +080044}
45
George Liua2870722020-02-11 11:09:30 +080046const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080047 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080048{
George Liuadbe1722020-05-09 19:20:19 +080049 if (typeId == TypeId::PLDM_SENSOR_ID)
50 {
51 return sensorDbusObjMaps.at(id);
52 }
53 else
54 {
55 return effecterDbusObjMaps.at(id);
56 }
George Liu1ec85d42020-02-12 16:05:32 +080057}
58
George Liu36e81352020-07-01 14:40:30 +080059void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
60 const std::string& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060061{
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050062 if (!fs::exists(dir))
63 {
64 return;
65 }
66
Deepak Kodihallic682fe22020-03-04 00:42:54 -060067 // A map of PDR type to a lambda that handles creation of that PDR type.
68 // The lambda essentially would parse the platform specific PDR JSONs to
69 // generate the PDR structures. This function iterates through the map to
70 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080071
72 const std::map<Type, generatePDR> generateHandlers = {
73 {PLDM_STATE_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080074 [this](const DBusHandler& dBusIntf, const auto& json,
75 RepoInterface& repo) {
76 pdr_state_effecter::generateStateEffecterPDR<
77 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
78 repo);
George Liu456c9a22020-01-13 11:36:22 +080079 }},
80 {PLDM_NUMERIC_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080081 [this](const DBusHandler& dBusIntf, const auto& json,
82 RepoInterface& repo) {
83 pdr_numeric_effecter::generateNumericEffecterPDR<
84 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
85 repo);
George Liuadbe1722020-05-09 19:20:19 +080086 }},
87 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
88 const auto& json, RepoInterface& repo) {
89 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
90 Handler>(dBusIntf, json,
91 *this, repo);
George Liua2870722020-02-11 11:09:30 +080092 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060093
94 Type pdrType{};
95 for (const auto& dirEntry : fs::directory_iterator(dir))
96 {
97 try
98 {
99 auto json = readJson(dirEntry.path().string());
100 if (!json.empty())
101 {
George Liu1ec85d42020-02-12 16:05:32 +0800102 auto effecterPDRs = json.value("effecterPDRs", empty);
103 for (const auto& effecter : effecterPDRs)
104 {
105 pdrType = effecter.value("pdrType", 0);
George Liu36e81352020-07-01 14:40:30 +0800106 generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
George Liu1ec85d42020-02-12 16:05:32 +0800107 }
George Liuadbe1722020-05-09 19:20:19 +0800108
109 auto sensorPDRs = json.value("sensorPDRs", empty);
110 for (const auto& sensor : sensorPDRs)
111 {
112 pdrType = sensor.value("pdrType", 0);
113 generateHandlers.at(pdrType)(dBusIntf, sensor, repo);
114 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600115 }
116 }
117 catch (const InternalFailure& e)
118 {
119 std::cerr << "PDR config directory does not exist or empty, TYPE= "
120 << pdrType << "PATH= " << dirEntry
121 << " ERROR=" << e.what() << "\n";
122 }
123 catch (const Json::exception& e)
124 {
125 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
126 << " ERROR=" << e.what() << "\n";
127 pldm::utils::reportError(
128 "xyz.openbmc_project.bmc.pldm.InternalFailure");
129 }
130 catch (const std::exception& e)
131 {
132 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
133 << " ERROR=" << e.what() << "\n";
134 pldm::utils::reportError(
135 "xyz.openbmc_project.bmc.pldm.InternalFailure");
136 }
137 }
138}
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530139
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600140Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530141{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530142 // Build FRU table if not built, since entity association PDR's are built
143 // when the FRU table is constructed.
144 if (fruHandler)
145 {
146 fruHandler->buildFRUTable();
147 }
148
George Liud680ae02020-07-17 09:11:14 +0800149 if (!pdrCreated)
150 {
151 generateTerminusLocatorPDR(pdrRepo);
152 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500153 if (oemPlatformHandler != nullptr)
154 {
155 oemPlatformHandler->buildOEMPDR(pdrRepo);
156 }
157
George Liud680ae02020-07-17 09:11:14 +0800158 pdrCreated = true;
159 }
160
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530161 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
162 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
163
164 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
165 {
George Liufb8611d2019-12-06 10:14:15 +0800166 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530167 }
168
169 uint32_t recordHandle{};
170 uint32_t dataTransferHandle{};
171 uint8_t transferOpFlag{};
172 uint16_t reqSizeBytes{};
173 uint16_t recordChangeNum{};
174
George Liufb8611d2019-12-06 10:14:15 +0800175 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
176 &dataTransferHandle, &transferOpFlag,
177 &reqSizeBytes, &recordChangeNum);
178 if (rc != PLDM_SUCCESS)
179 {
180 return CmdHandler::ccOnlyResponse(request, rc);
181 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530182
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530183 uint16_t respSizeBytes{};
184 uint8_t* recordData = nullptr;
185 try
186 {
George Liue53193f2020-02-24 09:23:26 +0800187 pdr_utils::PdrEntry e;
188 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
189 if (record == NULL)
190 {
191 return CmdHandler::ccOnlyResponse(
192 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
193 }
194
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530195 if (reqSizeBytes)
196 {
George Liue53193f2020-02-24 09:23:26 +0800197 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530198 if (respSizeBytes > reqSizeBytes)
199 {
200 respSizeBytes = reqSizeBytes;
201 }
George Liue53193f2020-02-24 09:23:26 +0800202 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530203 }
204 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
205 respSizeBytes,
206 0);
207 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500208 rc = encode_get_pdr_resp(
209 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
210 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800211 if (rc != PLDM_SUCCESS)
212 {
213 return ccOnlyResponse(request, rc);
214 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530215 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530216 catch (const std::exception& e)
217 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600218 std::cerr << "Error accessing PDR, HANDLE=" << recordHandle
219 << " ERROR=" << e.what() << "\n";
George Liufb8611d2019-12-06 10:14:15 +0800220 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530221 }
222 return response;
223}
224
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600225Response Handler::setStateEffecterStates(const pldm_msg* request,
226 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500227{
228 Response response(
229 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
230 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
231 uint16_t effecterId;
232 uint8_t compEffecterCnt;
233 constexpr auto maxCompositeEffecterCnt = 8;
234 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
235 {0, 0});
236
237 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
238 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
239 sizeof(set_effecter_state_field)))
240 {
George Liufb8611d2019-12-06 10:14:15 +0800241 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500242 }
243
244 int rc = decode_set_state_effecter_states_req(request, payloadLength,
245 &effecterId, &compEffecterCnt,
246 stateField.data());
247
George Liufb8611d2019-12-06 10:14:15 +0800248 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249 {
George Liufb8611d2019-12-06 10:14:15 +0800250 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500251 }
252
George Liufb8611d2019-12-06 10:14:15 +0800253 stateField.resize(compEffecterCnt);
254 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500255 uint16_t entityType{};
256 uint16_t entityInstance{};
257 uint16_t stateSetId{};
258
259 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
260 entityInstance, stateSetId) &&
261 oemPlatformHandler != nullptr)
262 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500263 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500264 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
265 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500266 }
267 else
268 {
269 rc = platform_state_effecter::setStateEffecterStatesHandler<
270 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
271 stateField);
272 }
George Liufb8611d2019-12-06 10:14:15 +0800273 if (rc != PLDM_SUCCESS)
274 {
275 return CmdHandler::ccOnlyResponse(request, rc);
276 }
277
278 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
279 responsePtr);
280 if (rc != PLDM_SUCCESS)
281 {
282 return ccOnlyResponse(request, rc);
283 }
284
Sampa Misraa2fa0702019-05-31 01:28:55 -0500285 return response;
286}
287
Tom Joseph56e45c52020-03-16 10:01:45 +0530288Response Handler::platformEventMessage(const pldm_msg* request,
289 size_t payloadLength)
290{
291 uint8_t formatVersion{};
292 uint8_t tid{};
293 uint8_t eventClass{};
294 size_t offset{};
295
296 auto rc = decode_platform_event_message_req(
297 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
298 if (rc != PLDM_SUCCESS)
299 {
300 return CmdHandler::ccOnlyResponse(request, rc);
301 }
302
303 try
304 {
305 const auto& handlers = eventHandlers.at(eventClass);
306 for (const auto& handler : handlers)
307 {
308 auto rc =
309 handler(request, payloadLength, formatVersion, tid, offset);
310 if (rc != PLDM_SUCCESS)
311 {
312 return CmdHandler::ccOnlyResponse(request, rc);
313 }
314 }
315 }
316 catch (const std::out_of_range& e)
317 {
318 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
319 }
320
321 Response response(
322 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
323 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
324
325 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
326 PLDM_EVENT_NO_LOGGING, responsePtr);
327 if (rc != PLDM_SUCCESS)
328 {
329 return ccOnlyResponse(request, rc);
330 }
331
332 return response;
333}
334
335int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530336 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530337 size_t eventDataOffset)
338{
339 uint16_t sensorId{};
340 uint8_t eventClass{};
341 size_t eventClassDataOffset{};
342 auto eventData =
343 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
344 auto eventDataSize = payloadLength - eventDataOffset;
345
346 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
347 &eventClass, &eventClassDataOffset);
348 if (rc != PLDM_SUCCESS)
349 {
350 return rc;
351 }
352
Zahed Hossain75330f32020-03-24 02:15:03 -0500353 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
354 eventDataOffset + eventClassDataOffset;
355 auto eventClassDataSize =
356 payloadLength - eventDataOffset - eventClassDataOffset;
357
Tom Joseph56e45c52020-03-16 10:01:45 +0530358 if (eventClass == PLDM_STATE_SENSOR_STATE)
359 {
360 uint8_t sensorOffset{};
361 uint8_t eventState{};
362 uint8_t previousEventState{};
363
Zahed Hossain75330f32020-03-24 02:15:03 -0500364 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530365 &sensorOffset, &eventState,
366 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500367 if (rc != PLDM_SUCCESS)
368 {
369 return PLDM_ERROR;
370 }
371
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800372 // Emitting state sensor event signal
373 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
374 previousEventState);
375
Tom Josephc4959c32020-04-20 19:50:16 +0530376 // If there are no HOST PDR's, there is no further action
377 if (hostPDRHandler == NULL)
378 {
379 return PLDM_SUCCESS;
380 }
381
382 // Handle PLDM events for which PDR is available
383 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530384
385 pldm::pdr::EntityInfo entityInfo{};
386 pldm::pdr::CompositeSensorStates compositeSensorStates{};
387
Tom Josephc4959c32020-04-20 19:50:16 +0530388 try
389 {
Tom Josephb70a1962020-07-13 12:56:31 +0530390 std::tie(entityInfo, compositeSensorStates) =
Tom Josephc4959c32020-04-20 19:50:16 +0530391 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530392 }
Tom Josephc4959c32020-04-20 19:50:16 +0530393 catch (const std::out_of_range& e)
394 {
Tom Josephb70a1962020-07-13 12:56:31 +0530395 // If there is no mapping for tid, sensorId combination, try
396 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
397 // implement TL PDR.
398 try
399 {
400 sensorEntry.terminusID = PLDM_TID_RESERVED;
401 std::tie(entityInfo, compositeSensorStates) =
402 hostPDRHandler->lookupSensorInfo(sensorEntry);
403 }
404 // If there is no mapping for events return PLDM_SUCCESS
405 catch (const std::out_of_range& e)
406 {
407 return PLDM_SUCCESS;
408 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500409 }
Tom Josephb70a1962020-07-13 12:56:31 +0530410
411 if (sensorOffset >= compositeSensorStates.size())
412 {
413 return PLDM_ERROR_INVALID_DATA;
414 }
415
416 const auto& possibleStates = compositeSensorStates[sensorOffset];
417 if (possibleStates.find(eventState) == possibleStates.end())
418 {
419 return PLDM_ERROR_INVALID_DATA;
420 }
421
422 const auto& [containerId, entityType, entityInstance] = entityInfo;
423 events::StateSensorEntry stateSensorEntry{containerId, entityType,
424 entityInstance, sensorOffset};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600425 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
426 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530427 }
428 else
429 {
430 return PLDM_ERROR_INVALID_DATA;
431 }
432
433 return PLDM_SUCCESS;
434}
435
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500436int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
437 size_t payloadLength,
438 uint8_t /*formatVersion*/,
439 uint8_t /*tid*/, size_t eventDataOffset)
440{
441 uint8_t eventDataFormat{};
442 uint8_t numberOfChangeRecords{};
443 size_t dataOffset{};
444
445 auto eventData =
446 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
447 auto eventDataSize = payloadLength - eventDataOffset;
448
449 auto rc = decode_pldm_pdr_repository_chg_event_data(
450 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
451 &dataOffset);
452 if (rc != PLDM_SUCCESS)
453 {
454 return rc;
455 }
456
457 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500458
459 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
460 {
461 return PLDM_ERROR_INVALID_DATA;
462 }
463
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500464 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
465 {
466 uint8_t eventDataOperation{};
467 uint8_t numberOfChangeEntries{};
468
469 auto changeRecordData = eventData + dataOffset;
470 auto changeRecordDataSize = eventDataSize - dataOffset;
471
472 while (changeRecordDataSize)
473 {
474 rc = decode_pldm_pdr_repository_change_record_data(
475 changeRecordData, changeRecordDataSize, &eventDataOperation,
476 &numberOfChangeEntries, &dataOffset);
477
478 if (rc != PLDM_SUCCESS)
479 {
480 return rc;
481 }
482
483 if (eventDataOperation == PLDM_RECORDS_ADDED)
484 {
485 rc = getPDRRecordHandles(
486 reinterpret_cast<const ChangeEntry*>(changeRecordData +
487 dataOffset),
488 changeRecordDataSize - dataOffset,
489 static_cast<size_t>(numberOfChangeEntries),
490 pdrRecordHandles);
491
492 if (rc != PLDM_SUCCESS)
493 {
494 return rc;
495 }
496 }
497
498 changeRecordData +=
499 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
500 changeRecordDataSize -=
501 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
502 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500503 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500504 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500505 {
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500506 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500507 }
508
509 return PLDM_SUCCESS;
510}
511
512int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
513 size_t changeEntryDataSize,
514 size_t numberOfChangeEntries,
515 PDRRecordHandles& pdrRecordHandles)
516{
517 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
518 {
519 return PLDM_ERROR_INVALID_DATA;
520 }
521 for (size_t i = 0; i < numberOfChangeEntries; i++)
522 {
523 pdrRecordHandles.push_back(changeEntryData[i]);
524 }
525 return PLDM_SUCCESS;
526}
527
George Liueccb0c52020-01-14 11:09:56 +0800528Response Handler::setNumericEffecterValue(const pldm_msg* request,
529 size_t payloadLength)
530{
531 Response response(sizeof(pldm_msg_hdr) +
532 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
533 uint16_t effecterId{};
534 uint8_t effecterDataSize{};
535 uint8_t effecterValue[4] = {};
536
537 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
538 sizeof(union_effecter_data_size)) ||
539 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
540 {
541 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
542 }
543
544 int rc = decode_set_numeric_effecter_value_req(
545 request, payloadLength, &effecterId, &effecterDataSize,
546 reinterpret_cast<uint8_t*>(&effecterValue));
547
548 if (rc == PLDM_SUCCESS)
549 {
550 const pldm::utils::DBusHandler dBusIntf;
551 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
552 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
553 effecterDataSize, effecterValue,
554 sizeof(effecterValue));
555 }
556
557 return ccOnlyResponse(request, rc);
558}
559
Sampa Misra12afe112020-05-25 11:40:44 -0500560void Handler::generateTerminusLocatorPDR(Repo& repo)
561{
562 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
563
564 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
565
566 pdr->hdr.record_handle = 0;
567 pdr->hdr.version = 1;
568 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
569 pdr->hdr.record_change_num = 0;
570 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
571 pdr->terminus_handle = BmcPldmTerminusHandle;
572 pdr->validity = PLDM_TL_PDR_VALID;
573 pdr->tid = BmcTerminusId;
574 pdr->container_id = 0x0;
575 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
576 pdr->terminus_locator_value_size =
577 sizeof(pldm_terminus_locator_type_mctp_eid);
578 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
579 pdr->terminus_locator_value);
580 locatorValue->eid = BmcMctpEid;
581
582 PdrEntry pdrEntry{};
583 pdrEntry.data = pdrBuffer.data();
584 pdrEntry.size = pdrBuffer.size();
585 repo.addRecord(pdrEntry);
586}
George Liu362c18d2020-05-14 09:46:36 +0800587
588Response Handler::getStateSensorReadings(const pldm_msg* request,
589 size_t payloadLength)
590{
591 uint16_t sensorId{};
592 bitfield8_t sensorRearm{};
593 uint8_t reserved{};
594
595 if (payloadLength != PLDM_GET_SENSOR_READING_REQ_BYTES)
596 {
597 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
598 }
599
600 int rc = decode_get_state_sensor_readings_req(
601 request, payloadLength, &sensorId, &sensorRearm, &reserved);
602
603 if (rc != PLDM_SUCCESS)
604 {
605 return ccOnlyResponse(request, rc);
606 }
607
608 // 0x01 to 0x08
Sampa Misraaea5dde2020-08-31 08:33:47 -0500609 uint8_t sensorRearmCount = getBitfieldCount(sensorRearm);
610 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800611 uint8_t comSensorCnt{};
612 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500613
614 uint16_t entityType{};
615 uint16_t entityInstance{};
616 uint16_t stateSetId{};
617
618 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
619 entityType, entityInstance, stateSetId) &&
620 oemPlatformHandler != nullptr)
621 {
622 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
623 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
624 }
625 else
626 {
627 rc = platform_state_sensor::getStateSensorReadingsHandler<
628 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, sensorId,
629 sensorRearmCount, comSensorCnt,
630 stateField);
631 }
George Liu362c18d2020-05-14 09:46:36 +0800632
633 if (rc != PLDM_SUCCESS)
634 {
635 return ccOnlyResponse(request, rc);
636 }
637
638 Response response(sizeof(pldm_msg_hdr) +
639 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
640 sizeof(get_sensor_state_field) * comSensorCnt);
641 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
642 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
643 comSensorCnt, stateField.data(),
644 responsePtr);
645 if (rc != PLDM_SUCCESS)
646 {
647 return ccOnlyResponse(request, rc);
648 }
649
650 return response;
651}
652
Sampa Misraaea5dde2020-08-31 08:33:47 -0500653bool isOemStateSensor(Handler& handler, uint16_t sensorId,
654 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
655 uint16_t& entityType, uint16_t& entityInstance,
656 uint16_t& stateSetId)
657{
658 pldm_state_sensor_pdr* pdr = nullptr;
659
660 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
661 pldm_pdr_init(), pldm_pdr_destroy);
662 Repo stateSensorPDRs(stateSensorPdrRepo.get());
663 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
664 if (stateSensorPDRs.empty())
665 {
666 std::cerr << "Failed to get record by PDR type\n";
667 return false;
668 }
669
670 PdrEntry pdrEntry{};
671 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
672 while (pdrRecord)
673 {
674 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
675 assert(pdr != NULL);
676 if (pdr->sensor_id != sensorId)
677 {
678 pdr = nullptr;
679 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
680 continue;
681 }
682 auto tmpEntityType = pdr->entity_type;
683 auto tmpEntityInstance = pdr->entity_instance;
684 auto tmpCompSensorCnt = pdr->composite_sensor_count;
685 auto tmpPossibleStates =
686 reinterpret_cast<state_sensor_possible_states*>(
687 pdr->possible_states);
688 auto tmpStateSetId = tmpPossibleStates->state_set_id;
689
690 if (sensorRearmCount > tmpCompSensorCnt)
691 {
692 std::cerr << "The requester sent wrong sensorRearm"
693 << " count for the sensor, SENSOR_ID=" << sensorId
694 << "SENSOR_REARM_COUNT=" << (uint16_t)sensorRearmCount
695 << "\n";
696 break;
697 }
698
699 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
700 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
701 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
702 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
703 {
704 entityType = tmpEntityType;
705 entityInstance = tmpEntityInstance;
706 stateSetId = tmpStateSetId;
707 compSensorCnt = tmpCompSensorCnt;
708 return true;
709 }
710 else
711 {
712 return false;
713 }
714 }
715 return false;
716}
717
718bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
719 uint8_t compEffecterCnt, uint16_t& entityType,
720 uint16_t& entityInstance, uint16_t& stateSetId)
721{
722 pldm_state_effecter_pdr* pdr = nullptr;
723
724 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
725 pldm_pdr_init(), pldm_pdr_destroy);
726 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
727 getRepoByType(handler.getRepo(), stateEffecterPDRs,
728 PLDM_STATE_EFFECTER_PDR);
729 if (stateEffecterPDRs.empty())
730 {
731 std::cerr << "Failed to get record by PDR type\n";
732 return false;
733 }
734
735 PdrEntry pdrEntry{};
736 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
737 while (pdrRecord)
738 {
739 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
740 assert(pdr != NULL);
741 if (pdr->effecter_id != effecterId)
742 {
743 pdr = nullptr;
744 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
745 continue;
746 }
747
748 auto tmpEntityType = pdr->entity_type;
749 auto tmpEntityInstance = pdr->entity_instance;
750 auto tmpPossibleStates =
751 reinterpret_cast<state_effecter_possible_states*>(
752 pdr->possible_states);
753 auto tmpStateSetId = tmpPossibleStates->state_set_id;
754
755 if (compEffecterCnt > pdr->composite_effecter_count)
756 {
757 std::cerr << "The requester sent wrong composite effecter"
758 << " count for the effecter, EFFECTER_ID=" << effecterId
759 << "COMP_EFF_CNT=" << (uint16_t)compEffecterCnt << "\n";
760 return false;
761 }
762
763 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
764 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
765 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
766 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
767 {
768 entityType = tmpEntityType;
769 entityInstance = tmpEntityInstance;
770 stateSetId = tmpStateSetId;
771 return true;
772 }
773 else
774 {
775 return false;
776 }
777 }
778 return false;
779}
780
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600781} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530782} // namespace responder
783} // namespace pldm