blob: 319c22e20c8bb7f7beebd10dea850c05e5785348 [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
Brad Bishop5079ac42021-08-19 18:35:06 -040019using namespace pldm::utils;
20using namespace pldm::responder::pdr;
21using namespace pldm::responder::pdr_utils;
22
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053023namespace pldm
24{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053025namespace responder
26{
Sampa Misraa2fa0702019-05-31 01:28:55 -050027namespace platform
28{
29
Deepak Kodihallic682fe22020-03-04 00:42:54 -060030using InternalFailure =
31 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
32
George Liu1ec85d42020-02-12 16:05:32 +080033static const Json empty{};
34
George Liua2870722020-02-11 11:09:30 +080035void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080036 uint16_t id,
37 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
38 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080039{
George Liuadbe1722020-05-09 19:20:19 +080040 if (typeId == TypeId::PLDM_SENSOR_ID)
41 {
42 sensorDbusObjMaps.emplace(id, dbusObj);
43 }
44 else
45 {
46 effecterDbusObjMaps.emplace(id, dbusObj);
47 }
George Liu1ec85d42020-02-12 16:05:32 +080048}
49
George Liua2870722020-02-11 11:09:30 +080050const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080051 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080052{
George Liuadbe1722020-05-09 19:20:19 +080053 if (typeId == TypeId::PLDM_SENSOR_ID)
54 {
55 return sensorDbusObjMaps.at(id);
56 }
57 else
58 {
59 return effecterDbusObjMaps.at(id);
60 }
George Liu1ec85d42020-02-12 16:05:32 +080061}
62
George Liu36e81352020-07-01 14:40:30 +080063void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
64 const std::string& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060065{
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050066 if (!fs::exists(dir))
67 {
68 return;
69 }
70
Deepak Kodihallic682fe22020-03-04 00:42:54 -060071 // A map of PDR type to a lambda that handles creation of that PDR type.
72 // The lambda essentially would parse the platform specific PDR JSONs to
73 // generate the PDR structures. This function iterates through the map to
74 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080075
76 const std::map<Type, generatePDR> generateHandlers = {
77 {PLDM_STATE_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080078 [this](const DBusHandler& dBusIntf, const auto& json,
79 RepoInterface& repo) {
80 pdr_state_effecter::generateStateEffecterPDR<
81 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
82 repo);
George Liu456c9a22020-01-13 11:36:22 +080083 }},
84 {PLDM_NUMERIC_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080085 [this](const DBusHandler& dBusIntf, const auto& json,
86 RepoInterface& repo) {
87 pdr_numeric_effecter::generateNumericEffecterPDR<
88 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
89 repo);
George Liuadbe1722020-05-09 19:20:19 +080090 }},
91 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
92 const auto& json, RepoInterface& repo) {
93 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
94 Handler>(dBusIntf, json,
95 *this, repo);
George Liua2870722020-02-11 11:09:30 +080096 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060097
98 Type pdrType{};
99 for (const auto& dirEntry : fs::directory_iterator(dir))
100 {
101 try
102 {
103 auto json = readJson(dirEntry.path().string());
104 if (!json.empty())
105 {
George Liu1ec85d42020-02-12 16:05:32 +0800106 auto effecterPDRs = json.value("effecterPDRs", empty);
107 for (const auto& effecter : effecterPDRs)
108 {
109 pdrType = effecter.value("pdrType", 0);
George Liu36e81352020-07-01 14:40:30 +0800110 generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
George Liu1ec85d42020-02-12 16:05:32 +0800111 }
George Liuadbe1722020-05-09 19:20:19 +0800112
113 auto sensorPDRs = json.value("sensorPDRs", empty);
114 for (const auto& sensor : sensorPDRs)
115 {
116 pdrType = sensor.value("pdrType", 0);
117 generateHandlers.at(pdrType)(dBusIntf, sensor, repo);
118 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600119 }
120 }
121 catch (const InternalFailure& e)
122 {
123 std::cerr << "PDR config directory does not exist or empty, TYPE= "
124 << pdrType << "PATH= " << dirEntry
125 << " ERROR=" << e.what() << "\n";
126 }
127 catch (const Json::exception& e)
128 {
129 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
130 << " ERROR=" << e.what() << "\n";
131 pldm::utils::reportError(
132 "xyz.openbmc_project.bmc.pldm.InternalFailure");
133 }
134 catch (const std::exception& e)
135 {
136 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
137 << " ERROR=" << e.what() << "\n";
138 pldm::utils::reportError(
139 "xyz.openbmc_project.bmc.pldm.InternalFailure");
140 }
141 }
142}
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530143
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600144Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530145{
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530146 // Build FRU table if not built, since entity association PDR's are built
147 // when the FRU table is constructed.
148 if (fruHandler)
149 {
150 fruHandler->buildFRUTable();
151 }
152
George Liud680ae02020-07-17 09:11:14 +0800153 if (!pdrCreated)
154 {
155 generateTerminusLocatorPDR(pdrRepo);
156 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500157 if (oemPlatformHandler != nullptr)
158 {
159 oemPlatformHandler->buildOEMPDR(pdrRepo);
160 }
161
George Liud680ae02020-07-17 09:11:14 +0800162 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800163
164 if (dbusToPLDMEventHandler)
165 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600166 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
167 event,
168 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
169 _processPostGetPDRActions),
170 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800171 }
George Liud680ae02020-07-17 09:11:14 +0800172 }
173
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530174 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
175 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
176
177 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
178 {
George Liufb8611d2019-12-06 10:14:15 +0800179 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530180 }
181
182 uint32_t recordHandle{};
183 uint32_t dataTransferHandle{};
184 uint8_t transferOpFlag{};
185 uint16_t reqSizeBytes{};
186 uint16_t recordChangeNum{};
187
George Liufb8611d2019-12-06 10:14:15 +0800188 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
189 &dataTransferHandle, &transferOpFlag,
190 &reqSizeBytes, &recordChangeNum);
191 if (rc != PLDM_SUCCESS)
192 {
193 return CmdHandler::ccOnlyResponse(request, rc);
194 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530195
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530196 uint16_t respSizeBytes{};
197 uint8_t* recordData = nullptr;
198 try
199 {
George Liue53193f2020-02-24 09:23:26 +0800200 pdr_utils::PdrEntry e;
201 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
202 if (record == NULL)
203 {
204 return CmdHandler::ccOnlyResponse(
205 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
206 }
207
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530208 if (reqSizeBytes)
209 {
George Liue53193f2020-02-24 09:23:26 +0800210 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530211 if (respSizeBytes > reqSizeBytes)
212 {
213 respSizeBytes = reqSizeBytes;
214 }
George Liue53193f2020-02-24 09:23:26 +0800215 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530216 }
217 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
218 respSizeBytes,
219 0);
220 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500221 rc = encode_get_pdr_resp(
222 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
223 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800224 if (rc != PLDM_SUCCESS)
225 {
226 return ccOnlyResponse(request, rc);
227 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530228 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530229 catch (const std::exception& e)
230 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600231 std::cerr << "Error accessing PDR, HANDLE=" << recordHandle
232 << " ERROR=" << e.what() << "\n";
George Liufb8611d2019-12-06 10:14:15 +0800233 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530234 }
235 return response;
236}
237
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600238Response Handler::setStateEffecterStates(const pldm_msg* request,
239 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500240{
241 Response response(
242 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
243 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
244 uint16_t effecterId;
245 uint8_t compEffecterCnt;
246 constexpr auto maxCompositeEffecterCnt = 8;
247 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
248 {0, 0});
249
250 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
251 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
252 sizeof(set_effecter_state_field)))
253 {
George Liufb8611d2019-12-06 10:14:15 +0800254 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500255 }
256
257 int rc = decode_set_state_effecter_states_req(request, payloadLength,
258 &effecterId, &compEffecterCnt,
259 stateField.data());
260
George Liufb8611d2019-12-06 10:14:15 +0800261 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500262 {
George Liufb8611d2019-12-06 10:14:15 +0800263 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500264 }
265
George Liufb8611d2019-12-06 10:14:15 +0800266 stateField.resize(compEffecterCnt);
267 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500268 uint16_t entityType{};
269 uint16_t entityInstance{};
270 uint16_t stateSetId{};
271
272 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
273 entityInstance, stateSetId) &&
274 oemPlatformHandler != nullptr)
275 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500276 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500277 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
278 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500279 }
280 else
281 {
282 rc = platform_state_effecter::setStateEffecterStatesHandler<
283 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
284 stateField);
285 }
George Liufb8611d2019-12-06 10:14:15 +0800286 if (rc != PLDM_SUCCESS)
287 {
288 return CmdHandler::ccOnlyResponse(request, rc);
289 }
290
291 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
292 responsePtr);
293 if (rc != PLDM_SUCCESS)
294 {
295 return ccOnlyResponse(request, rc);
296 }
297
Sampa Misraa2fa0702019-05-31 01:28:55 -0500298 return response;
299}
300
Tom Joseph56e45c52020-03-16 10:01:45 +0530301Response Handler::platformEventMessage(const pldm_msg* request,
302 size_t payloadLength)
303{
304 uint8_t formatVersion{};
305 uint8_t tid{};
306 uint8_t eventClass{};
307 size_t offset{};
308
309 auto rc = decode_platform_event_message_req(
310 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
311 if (rc != PLDM_SUCCESS)
312 {
313 return CmdHandler::ccOnlyResponse(request, rc);
314 }
315
316 try
317 {
318 const auto& handlers = eventHandlers.at(eventClass);
319 for (const auto& handler : handlers)
320 {
321 auto rc =
322 handler(request, payloadLength, formatVersion, tid, offset);
323 if (rc != PLDM_SUCCESS)
324 {
325 return CmdHandler::ccOnlyResponse(request, rc);
326 }
327 }
328 }
329 catch (const std::out_of_range& e)
330 {
331 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
332 }
333
334 Response response(
335 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
336 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
337
338 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
339 PLDM_EVENT_NO_LOGGING, responsePtr);
340 if (rc != PLDM_SUCCESS)
341 {
342 return ccOnlyResponse(request, rc);
343 }
344
345 return response;
346}
347
348int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530349 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530350 size_t eventDataOffset)
351{
352 uint16_t sensorId{};
353 uint8_t eventClass{};
354 size_t eventClassDataOffset{};
355 auto eventData =
356 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
357 auto eventDataSize = payloadLength - eventDataOffset;
358
359 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
360 &eventClass, &eventClassDataOffset);
361 if (rc != PLDM_SUCCESS)
362 {
363 return rc;
364 }
365
Zahed Hossain75330f32020-03-24 02:15:03 -0500366 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
367 eventDataOffset + eventClassDataOffset;
368 auto eventClassDataSize =
369 payloadLength - eventDataOffset - eventClassDataOffset;
370
Tom Joseph56e45c52020-03-16 10:01:45 +0530371 if (eventClass == PLDM_STATE_SENSOR_STATE)
372 {
373 uint8_t sensorOffset{};
374 uint8_t eventState{};
375 uint8_t previousEventState{};
376
Zahed Hossain75330f32020-03-24 02:15:03 -0500377 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530378 &sensorOffset, &eventState,
379 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500380 if (rc != PLDM_SUCCESS)
381 {
382 return PLDM_ERROR;
383 }
384
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800385 // Emitting state sensor event signal
386 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
387 previousEventState);
388
Tom Josephc4959c32020-04-20 19:50:16 +0530389 // If there are no HOST PDR's, there is no further action
390 if (hostPDRHandler == NULL)
391 {
392 return PLDM_SUCCESS;
393 }
394
395 // Handle PLDM events for which PDR is available
396 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530397
398 pldm::pdr::EntityInfo entityInfo{};
399 pldm::pdr::CompositeSensorStates compositeSensorStates{};
400
Tom Josephc4959c32020-04-20 19:50:16 +0530401 try
402 {
Tom Josephb70a1962020-07-13 12:56:31 +0530403 std::tie(entityInfo, compositeSensorStates) =
Tom Josephc4959c32020-04-20 19:50:16 +0530404 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530405 }
Tom Josephc4959c32020-04-20 19:50:16 +0530406 catch (const std::out_of_range& e)
407 {
Tom Josephb70a1962020-07-13 12:56:31 +0530408 // If there is no mapping for tid, sensorId combination, try
409 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
410 // implement TL PDR.
411 try
412 {
413 sensorEntry.terminusID = PLDM_TID_RESERVED;
414 std::tie(entityInfo, compositeSensorStates) =
415 hostPDRHandler->lookupSensorInfo(sensorEntry);
416 }
417 // If there is no mapping for events return PLDM_SUCCESS
418 catch (const std::out_of_range& e)
419 {
420 return PLDM_SUCCESS;
421 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500422 }
Tom Josephb70a1962020-07-13 12:56:31 +0530423
424 if (sensorOffset >= compositeSensorStates.size())
425 {
426 return PLDM_ERROR_INVALID_DATA;
427 }
428
429 const auto& possibleStates = compositeSensorStates[sensorOffset];
430 if (possibleStates.find(eventState) == possibleStates.end())
431 {
432 return PLDM_ERROR_INVALID_DATA;
433 }
434
435 const auto& [containerId, entityType, entityInstance] = entityInfo;
436 events::StateSensorEntry stateSensorEntry{containerId, entityType,
437 entityInstance, sensorOffset};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600438 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
439 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530440 }
441 else
442 {
443 return PLDM_ERROR_INVALID_DATA;
444 }
445
446 return PLDM_SUCCESS;
447}
448
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500449int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
450 size_t payloadLength,
451 uint8_t /*formatVersion*/,
452 uint8_t /*tid*/, size_t eventDataOffset)
453{
454 uint8_t eventDataFormat{};
455 uint8_t numberOfChangeRecords{};
456 size_t dataOffset{};
457
458 auto eventData =
459 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
460 auto eventDataSize = payloadLength - eventDataOffset;
461
462 auto rc = decode_pldm_pdr_repository_chg_event_data(
463 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
464 &dataOffset);
465 if (rc != PLDM_SUCCESS)
466 {
467 return rc;
468 }
469
470 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500471
472 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
473 {
474 return PLDM_ERROR_INVALID_DATA;
475 }
476
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500477 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
478 {
479 uint8_t eventDataOperation{};
480 uint8_t numberOfChangeEntries{};
481
482 auto changeRecordData = eventData + dataOffset;
483 auto changeRecordDataSize = eventDataSize - dataOffset;
484
485 while (changeRecordDataSize)
486 {
487 rc = decode_pldm_pdr_repository_change_record_data(
488 changeRecordData, changeRecordDataSize, &eventDataOperation,
489 &numberOfChangeEntries, &dataOffset);
490
491 if (rc != PLDM_SUCCESS)
492 {
493 return rc;
494 }
495
496 if (eventDataOperation == PLDM_RECORDS_ADDED)
497 {
498 rc = getPDRRecordHandles(
499 reinterpret_cast<const ChangeEntry*>(changeRecordData +
500 dataOffset),
501 changeRecordDataSize - dataOffset,
502 static_cast<size_t>(numberOfChangeEntries),
503 pdrRecordHandles);
504
505 if (rc != PLDM_SUCCESS)
506 {
507 return rc;
508 }
509 }
510
511 changeRecordData +=
512 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
513 changeRecordDataSize -=
514 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
515 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500516 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500517 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500518 {
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500519 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500520 }
521
522 return PLDM_SUCCESS;
523}
524
525int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
526 size_t changeEntryDataSize,
527 size_t numberOfChangeEntries,
528 PDRRecordHandles& pdrRecordHandles)
529{
530 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
531 {
532 return PLDM_ERROR_INVALID_DATA;
533 }
534 for (size_t i = 0; i < numberOfChangeEntries; i++)
535 {
536 pdrRecordHandles.push_back(changeEntryData[i]);
537 }
538 return PLDM_SUCCESS;
539}
540
George Liueccb0c52020-01-14 11:09:56 +0800541Response Handler::setNumericEffecterValue(const pldm_msg* request,
542 size_t payloadLength)
543{
544 Response response(sizeof(pldm_msg_hdr) +
545 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
546 uint16_t effecterId{};
547 uint8_t effecterDataSize{};
548 uint8_t effecterValue[4] = {};
549
550 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
551 sizeof(union_effecter_data_size)) ||
552 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
553 {
554 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
555 }
556
557 int rc = decode_set_numeric_effecter_value_req(
558 request, payloadLength, &effecterId, &effecterDataSize,
559 reinterpret_cast<uint8_t*>(&effecterValue));
560
561 if (rc == PLDM_SUCCESS)
562 {
563 const pldm::utils::DBusHandler dBusIntf;
564 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
565 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
566 effecterDataSize, effecterValue,
567 sizeof(effecterValue));
568 }
569
570 return ccOnlyResponse(request, rc);
571}
572
Sampa Misra12afe112020-05-25 11:40:44 -0500573void Handler::generateTerminusLocatorPDR(Repo& repo)
574{
575 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
576
577 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
578
579 pdr->hdr.record_handle = 0;
580 pdr->hdr.version = 1;
581 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
582 pdr->hdr.record_change_num = 0;
583 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
584 pdr->terminus_handle = BmcPldmTerminusHandle;
585 pdr->validity = PLDM_TL_PDR_VALID;
586 pdr->tid = BmcTerminusId;
587 pdr->container_id = 0x0;
588 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
589 pdr->terminus_locator_value_size =
590 sizeof(pldm_terminus_locator_type_mctp_eid);
591 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
592 pdr->terminus_locator_value);
593 locatorValue->eid = BmcMctpEid;
594
595 PdrEntry pdrEntry{};
596 pdrEntry.data = pdrBuffer.data();
597 pdrEntry.size = pdrBuffer.size();
598 repo.addRecord(pdrEntry);
599}
George Liu362c18d2020-05-14 09:46:36 +0800600
601Response Handler::getStateSensorReadings(const pldm_msg* request,
602 size_t payloadLength)
603{
604 uint16_t sensorId{};
605 bitfield8_t sensorRearm{};
606 uint8_t reserved{};
607
608 if (payloadLength != PLDM_GET_SENSOR_READING_REQ_BYTES)
609 {
610 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
611 }
612
613 int rc = decode_get_state_sensor_readings_req(
614 request, payloadLength, &sensorId, &sensorRearm, &reserved);
615
616 if (rc != PLDM_SUCCESS)
617 {
618 return ccOnlyResponse(request, rc);
619 }
620
621 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800622 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500623 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800624 uint8_t comSensorCnt{};
625 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500626
627 uint16_t entityType{};
628 uint16_t entityInstance{};
629 uint16_t stateSetId{};
630
631 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
632 entityType, entityInstance, stateSetId) &&
633 oemPlatformHandler != nullptr)
634 {
635 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
636 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
637 }
638 else
639 {
640 rc = platform_state_sensor::getStateSensorReadingsHandler<
641 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, sensorId,
642 sensorRearmCount, comSensorCnt,
643 stateField);
644 }
George Liu362c18d2020-05-14 09:46:36 +0800645
646 if (rc != PLDM_SUCCESS)
647 {
648 return ccOnlyResponse(request, rc);
649 }
650
651 Response response(sizeof(pldm_msg_hdr) +
652 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
653 sizeof(get_sensor_state_field) * comSensorCnt);
654 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
655 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
656 comSensorCnt, stateField.data(),
657 responsePtr);
658 if (rc != PLDM_SUCCESS)
659 {
660 return ccOnlyResponse(request, rc);
661 }
662
663 return response;
664}
665
Sampa Misra5fb37d52021-03-06 07:26:00 -0600666void Handler::_processPostGetPDRActions(
667 sdeventplus::source::EventBase& /*source */)
668{
669 deferredGetPDREvent.reset();
670 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
671}
672
Sampa Misraaea5dde2020-08-31 08:33:47 -0500673bool isOemStateSensor(Handler& handler, uint16_t sensorId,
674 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
675 uint16_t& entityType, uint16_t& entityInstance,
676 uint16_t& stateSetId)
677{
678 pldm_state_sensor_pdr* pdr = nullptr;
679
680 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
681 pldm_pdr_init(), pldm_pdr_destroy);
682 Repo stateSensorPDRs(stateSensorPdrRepo.get());
683 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
684 if (stateSensorPDRs.empty())
685 {
686 std::cerr << "Failed to get record by PDR type\n";
687 return false;
688 }
689
690 PdrEntry pdrEntry{};
691 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
692 while (pdrRecord)
693 {
694 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
695 assert(pdr != NULL);
696 if (pdr->sensor_id != sensorId)
697 {
698 pdr = nullptr;
699 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
700 continue;
701 }
702 auto tmpEntityType = pdr->entity_type;
703 auto tmpEntityInstance = pdr->entity_instance;
704 auto tmpCompSensorCnt = pdr->composite_sensor_count;
705 auto tmpPossibleStates =
706 reinterpret_cast<state_sensor_possible_states*>(
707 pdr->possible_states);
708 auto tmpStateSetId = tmpPossibleStates->state_set_id;
709
710 if (sensorRearmCount > tmpCompSensorCnt)
711 {
712 std::cerr << "The requester sent wrong sensorRearm"
713 << " count for the sensor, SENSOR_ID=" << sensorId
714 << "SENSOR_REARM_COUNT=" << (uint16_t)sensorRearmCount
715 << "\n";
716 break;
717 }
718
719 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
720 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
721 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
722 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
723 {
724 entityType = tmpEntityType;
725 entityInstance = tmpEntityInstance;
726 stateSetId = tmpStateSetId;
727 compSensorCnt = tmpCompSensorCnt;
728 return true;
729 }
730 else
731 {
732 return false;
733 }
734 }
735 return false;
736}
737
738bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
739 uint8_t compEffecterCnt, uint16_t& entityType,
740 uint16_t& entityInstance, uint16_t& stateSetId)
741{
742 pldm_state_effecter_pdr* pdr = nullptr;
743
744 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
745 pldm_pdr_init(), pldm_pdr_destroy);
746 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
747 getRepoByType(handler.getRepo(), stateEffecterPDRs,
748 PLDM_STATE_EFFECTER_PDR);
749 if (stateEffecterPDRs.empty())
750 {
751 std::cerr << "Failed to get record by PDR type\n";
752 return false;
753 }
754
755 PdrEntry pdrEntry{};
756 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
757 while (pdrRecord)
758 {
759 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
760 assert(pdr != NULL);
761 if (pdr->effecter_id != effecterId)
762 {
763 pdr = nullptr;
764 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
765 continue;
766 }
767
768 auto tmpEntityType = pdr->entity_type;
769 auto tmpEntityInstance = pdr->entity_instance;
770 auto tmpPossibleStates =
771 reinterpret_cast<state_effecter_possible_states*>(
772 pdr->possible_states);
773 auto tmpStateSetId = tmpPossibleStates->state_set_id;
774
775 if (compEffecterCnt > pdr->composite_effecter_count)
776 {
777 std::cerr << "The requester sent wrong composite effecter"
778 << " count for the effecter, EFFECTER_ID=" << effecterId
779 << "COMP_EFF_CNT=" << (uint16_t)compEffecterCnt << "\n";
780 return false;
781 }
782
783 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
784 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
785 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
786 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
787 {
788 entityType = tmpEntityType;
789 entityInstance = tmpEntityInstance;
790 stateSetId = tmpStateSetId;
791 return true;
792 }
793 else
794 {
795 return false;
796 }
797 }
798 return false;
799}
800
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600801} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530802} // namespace responder
803} // namespace pldm