blob: 31a7d6c4180d0bd0abd9492cf20f3fa1282967ce [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#include "platform.hpp"
2
Tom Josephb70a1962020-07-13 12:56:31 +05303#include "common/types.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05004#include "common/utils.hpp"
Tom Josephc4959c32020-04-20 19:50:16 +05305#include "event_parser.hpp"
Sampa Misra12afe112020-05-25 11:40:44 -05006#include "pdr.hpp"
George Liu456c9a22020-01-13 11:36:22 +08007#include "pdr_numeric_effecter.hpp"
George Liua2870722020-02-11 11:09:30 +08008#include "pdr_state_effecter.hpp"
George Liuadbe1722020-05-09 19:20:19 +08009#include "pdr_state_sensor.hpp"
George Liu362c18d2020-05-14 09:46:36 +080010#include "pdr_utils.hpp"
George Liueccb0c52020-01-14 11:09:56 +080011#include "platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +080012#include "platform_state_effecter.hpp"
George Liu362c18d2020-05-14 09:46:36 +080013#include "platform_state_sensor.hpp"
George Liu83409572019-12-24 18:42:54 +080014
Manojkiran Edacc5f1582021-09-29 17:03:06 +053015#include <config.h>
George Liuc453e162022-12-21 17:16:23 +080016#include <libpldm/entity.h>
17#include <libpldm/state_set.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +053018
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{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060029using InternalFailure =
30 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
31
George Liu1ec85d42020-02-12 16:05:32 +080032static const Json empty{};
33
George Liua2870722020-02-11 11:09:30 +080034void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080035 uint16_t id,
36 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
37 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080038{
George Liuadbe1722020-05-09 19:20:19 +080039 if (typeId == TypeId::PLDM_SENSOR_ID)
40 {
41 sensorDbusObjMaps.emplace(id, dbusObj);
42 }
43 else
44 {
45 effecterDbusObjMaps.emplace(id, dbusObj);
46 }
George Liu1ec85d42020-02-12 16:05:32 +080047}
48
George Liua2870722020-02-11 11:09:30 +080049const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080050 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080051{
George Liuadbe1722020-05-09 19:20:19 +080052 if (typeId == TypeId::PLDM_SENSOR_ID)
53 {
54 return sensorDbusObjMaps.at(id);
55 }
56 else
57 {
58 return effecterDbusObjMaps.at(id);
59 }
George Liu1ec85d42020-02-12 16:05:32 +080060}
61
George Liu36e81352020-07-01 14:40:30 +080062void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
63 const std::string& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060064{
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050065 if (!fs::exists(dir))
66 {
67 return;
68 }
69
Deepak Kodihallic682fe22020-03-04 00:42:54 -060070 // A map of PDR type to a lambda that handles creation of that PDR type.
71 // The lambda essentially would parse the platform specific PDR JSONs to
72 // generate the PDR structures. This function iterates through the map to
73 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080074
75 const std::map<Type, generatePDR> generateHandlers = {
76 {PLDM_STATE_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080077 [this](const DBusHandler& dBusIntf, const auto& json,
78 RepoInterface& repo) {
79 pdr_state_effecter::generateStateEffecterPDR<
80 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
81 repo);
George Liu456c9a22020-01-13 11:36:22 +080082 }},
83 {PLDM_NUMERIC_EFFECTER_PDR,
George Liu36e81352020-07-01 14:40:30 +080084 [this](const DBusHandler& dBusIntf, const auto& json,
85 RepoInterface& repo) {
86 pdr_numeric_effecter::generateNumericEffecterPDR<
87 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
88 repo);
George Liuadbe1722020-05-09 19:20:19 +080089 }},
90 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
91 const auto& json, RepoInterface& repo) {
92 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
93 Handler>(dBusIntf, json,
94 *this, repo);
George Liua2870722020-02-11 11:09:30 +080095 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060096
97 Type pdrType{};
98 for (const auto& dirEntry : fs::directory_iterator(dir))
99 {
100 try
101 {
102 auto json = readJson(dirEntry.path().string());
103 if (!json.empty())
104 {
George Liu1ec85d42020-02-12 16:05:32 +0800105 auto effecterPDRs = json.value("effecterPDRs", empty);
106 for (const auto& effecter : effecterPDRs)
107 {
108 pdrType = effecter.value("pdrType", 0);
George Liu36e81352020-07-01 14:40:30 +0800109 generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
George Liu1ec85d42020-02-12 16:05:32 +0800110 }
George Liuadbe1722020-05-09 19:20:19 +0800111
112 auto sensorPDRs = json.value("sensorPDRs", empty);
113 for (const auto& sensor : sensorPDRs)
114 {
115 pdrType = sensor.value("pdrType", 0);
116 generateHandlers.at(pdrType)(dBusIntf, sensor, repo);
117 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600118 }
119 }
120 catch (const InternalFailure& e)
121 {
122 std::cerr << "PDR config directory does not exist or empty, TYPE= "
123 << pdrType << "PATH= " << dirEntry
124 << " ERROR=" << e.what() << "\n";
125 }
126 catch (const Json::exception& e)
127 {
128 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
129 << " ERROR=" << e.what() << "\n";
130 pldm::utils::reportError(
131 "xyz.openbmc_project.bmc.pldm.InternalFailure");
132 }
133 catch (const std::exception& e)
134 {
135 std::cerr << "Failed parsing PDR JSON file, TYPE= " << pdrType
136 << " ERROR=" << e.what() << "\n";
137 pldm::utils::reportError(
138 "xyz.openbmc_project.bmc.pldm.InternalFailure");
139 }
140 }
141}
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530142
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600143Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530144{
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500145 if (hostPDRHandler)
146 {
147 if (hostPDRHandler->isHostUp() && oemPlatformHandler != nullptr)
148 {
149 auto rc = oemPlatformHandler->checkBMCState();
150 if (rc != PLDM_SUCCESS)
151 {
152 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
153 }
154 }
155 }
156
157 // Build FRU table if not built, since entity association PDR's
158 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530159 if (fruHandler)
160 {
161 fruHandler->buildFRUTable();
162 }
163
George Liud680ae02020-07-17 09:11:14 +0800164 if (!pdrCreated)
165 {
166 generateTerminusLocatorPDR(pdrRepo);
167 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500168 if (oemPlatformHandler != nullptr)
169 {
170 oemPlatformHandler->buildOEMPDR(pdrRepo);
171 }
172
George Liud680ae02020-07-17 09:11:14 +0800173 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800174
175 if (dbusToPLDMEventHandler)
176 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600177 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
178 event,
179 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
180 _processPostGetPDRActions),
181 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800182 }
George Liud680ae02020-07-17 09:11:14 +0800183 }
184
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530185 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530186
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);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530230 auto 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;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500278 uint16_t entityType{};
279 uint16_t entityInstance{};
280 uint16_t stateSetId{};
281
282 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
283 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530284 oemPlatformHandler != nullptr &&
285 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500286 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500287 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500288 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
289 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500290 }
291 else
292 {
293 rc = platform_state_effecter::setStateEffecterStatesHandler<
294 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
295 stateField);
296 }
George Liufb8611d2019-12-06 10:14:15 +0800297 if (rc != PLDM_SUCCESS)
298 {
299 return CmdHandler::ccOnlyResponse(request, rc);
300 }
301
302 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
303 responsePtr);
304 if (rc != PLDM_SUCCESS)
305 {
306 return ccOnlyResponse(request, rc);
307 }
308
Sampa Misraa2fa0702019-05-31 01:28:55 -0500309 return response;
310}
311
Tom Joseph56e45c52020-03-16 10:01:45 +0530312Response Handler::platformEventMessage(const pldm_msg* request,
313 size_t payloadLength)
314{
315 uint8_t formatVersion{};
316 uint8_t tid{};
317 uint8_t eventClass{};
318 size_t offset{};
319
320 auto rc = decode_platform_event_message_req(
321 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
322 if (rc != PLDM_SUCCESS)
323 {
324 return CmdHandler::ccOnlyResponse(request, rc);
325 }
326
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500327 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530328 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500329 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500330 if (oemPlatformHandler)
331 {
332 oemPlatformHandler->resetWatchDogTimer();
333 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500334 }
335 else
336 {
337 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530338 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500339 const auto& handlers = eventHandlers.at(eventClass);
340 for (const auto& handler : handlers)
Tom Joseph56e45c52020-03-16 10:01:45 +0530341 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500342 auto rc =
343 handler(request, payloadLength, formatVersion, tid, offset);
344 if (rc != PLDM_SUCCESS)
345 {
346 return CmdHandler::ccOnlyResponse(request, rc);
347 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530348 }
349 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500350 catch (const std::out_of_range& e)
351 {
352 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
353 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530354 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530355 Response response(
356 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
357 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
358
359 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
360 PLDM_EVENT_NO_LOGGING, responsePtr);
361 if (rc != PLDM_SUCCESS)
362 {
363 return ccOnlyResponse(request, rc);
364 }
365
366 return response;
367}
368
369int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530370 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530371 size_t eventDataOffset)
372{
373 uint16_t sensorId{};
374 uint8_t eventClass{};
375 size_t eventClassDataOffset{};
376 auto eventData =
377 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
378 auto eventDataSize = payloadLength - eventDataOffset;
379
380 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
381 &eventClass, &eventClassDataOffset);
382 if (rc != PLDM_SUCCESS)
383 {
384 return rc;
385 }
386
Zahed Hossain75330f32020-03-24 02:15:03 -0500387 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
388 eventDataOffset + eventClassDataOffset;
389 auto eventClassDataSize =
390 payloadLength - eventDataOffset - eventClassDataOffset;
391
Tom Joseph56e45c52020-03-16 10:01:45 +0530392 if (eventClass == PLDM_STATE_SENSOR_STATE)
393 {
394 uint8_t sensorOffset{};
395 uint8_t eventState{};
396 uint8_t previousEventState{};
397
Zahed Hossain75330f32020-03-24 02:15:03 -0500398 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530399 &sensorOffset, &eventState,
400 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500401 if (rc != PLDM_SUCCESS)
402 {
403 return PLDM_ERROR;
404 }
405
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800406 // Emitting state sensor event signal
407 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
408 previousEventState);
409
Tom Josephc4959c32020-04-20 19:50:16 +0530410 // If there are no HOST PDR's, there is no further action
411 if (hostPDRHandler == NULL)
412 {
413 return PLDM_SUCCESS;
414 }
415
416 // Handle PLDM events for which PDR is available
417 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530418
419 pldm::pdr::EntityInfo entityInfo{};
420 pldm::pdr::CompositeSensorStates compositeSensorStates{};
421
Tom Josephc4959c32020-04-20 19:50:16 +0530422 try
423 {
Tom Josephb70a1962020-07-13 12:56:31 +0530424 std::tie(entityInfo, compositeSensorStates) =
Tom Josephc4959c32020-04-20 19:50:16 +0530425 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530426 }
Tom Josephc4959c32020-04-20 19:50:16 +0530427 catch (const std::out_of_range& e)
428 {
Tom Josephb70a1962020-07-13 12:56:31 +0530429 // If there is no mapping for tid, sensorId combination, try
430 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
431 // implement TL PDR.
432 try
433 {
434 sensorEntry.terminusID = PLDM_TID_RESERVED;
435 std::tie(entityInfo, compositeSensorStates) =
436 hostPDRHandler->lookupSensorInfo(sensorEntry);
437 }
438 // If there is no mapping for events return PLDM_SUCCESS
439 catch (const std::out_of_range& e)
440 {
441 return PLDM_SUCCESS;
442 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500443 }
Tom Josephb70a1962020-07-13 12:56:31 +0530444
445 if (sensorOffset >= compositeSensorStates.size())
446 {
447 return PLDM_ERROR_INVALID_DATA;
448 }
449
450 const auto& possibleStates = compositeSensorStates[sensorOffset];
451 if (possibleStates.find(eventState) == possibleStates.end())
452 {
453 return PLDM_ERROR_INVALID_DATA;
454 }
455
456 const auto& [containerId, entityType, entityInstance] = entityInfo;
457 events::StateSensorEntry stateSensorEntry{containerId, entityType,
458 entityInstance, sensorOffset};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600459 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
460 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530461 }
462 else
463 {
464 return PLDM_ERROR_INVALID_DATA;
465 }
466
467 return PLDM_SUCCESS;
468}
469
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500470int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
471 size_t payloadLength,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530472 uint8_t /*formatVersion*/, uint8_t tid,
473 size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500474{
475 uint8_t eventDataFormat{};
476 uint8_t numberOfChangeRecords{};
477 size_t dataOffset{};
478
479 auto eventData =
480 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
481 auto eventDataSize = payloadLength - eventDataOffset;
482
483 auto rc = decode_pldm_pdr_repository_chg_event_data(
484 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
485 &dataOffset);
486 if (rc != PLDM_SUCCESS)
487 {
488 return rc;
489 }
490
491 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500492
493 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
494 {
495 return PLDM_ERROR_INVALID_DATA;
496 }
497
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500498 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
499 {
500 uint8_t eventDataOperation{};
501 uint8_t numberOfChangeEntries{};
502
503 auto changeRecordData = eventData + dataOffset;
504 auto changeRecordDataSize = eventDataSize - dataOffset;
505
506 while (changeRecordDataSize)
507 {
508 rc = decode_pldm_pdr_repository_change_record_data(
509 changeRecordData, changeRecordDataSize, &eventDataOperation,
510 &numberOfChangeEntries, &dataOffset);
511
512 if (rc != PLDM_SUCCESS)
513 {
514 return rc;
515 }
516
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500517 if (eventDataOperation == PLDM_RECORDS_ADDED ||
518 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500519 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500520 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
521 {
522 hostPDRHandler->isHostPdrModified = true;
523 }
524
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500525 rc = getPDRRecordHandles(
526 reinterpret_cast<const ChangeEntry*>(changeRecordData +
527 dataOffset),
528 changeRecordDataSize - dataOffset,
529 static_cast<size_t>(numberOfChangeEntries),
530 pdrRecordHandles);
531
532 if (rc != PLDM_SUCCESS)
533 {
534 return rc;
535 }
536 }
537
538 changeRecordData +=
539 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
540 changeRecordDataSize -=
541 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
542 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500543 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500544 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500545 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530546 // if we get a Repository change event with the eventDataFormat
547 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
548 // have the matched Terminus handle
549 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
550 {
551 // We cannot get the Repo change event from the Terminus
552 // that is not already added to the BMC repository
553
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500554 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
555 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530556 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500557 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530558 {
559 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500560 it->first);
561 hostPDRHandler->tlPDRInfo.erase(it++);
562 }
563 else
564 {
565 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530566 }
567 }
568 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500569 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500570 }
571
572 return PLDM_SUCCESS;
573}
574
575int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
576 size_t changeEntryDataSize,
577 size_t numberOfChangeEntries,
578 PDRRecordHandles& pdrRecordHandles)
579{
580 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
581 {
582 return PLDM_ERROR_INVALID_DATA;
583 }
584 for (size_t i = 0; i < numberOfChangeEntries; i++)
585 {
586 pdrRecordHandles.push_back(changeEntryData[i]);
587 }
588 return PLDM_SUCCESS;
589}
590
George Liueccb0c52020-01-14 11:09:56 +0800591Response Handler::setNumericEffecterValue(const pldm_msg* request,
592 size_t payloadLength)
593{
594 Response response(sizeof(pldm_msg_hdr) +
595 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
596 uint16_t effecterId{};
597 uint8_t effecterDataSize{};
598 uint8_t effecterValue[4] = {};
599
600 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
601 sizeof(union_effecter_data_size)) ||
602 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
603 {
604 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
605 }
606
607 int rc = decode_set_numeric_effecter_value_req(
608 request, payloadLength, &effecterId, &effecterDataSize,
609 reinterpret_cast<uint8_t*>(&effecterValue));
610
611 if (rc == PLDM_SUCCESS)
612 {
613 const pldm::utils::DBusHandler dBusIntf;
614 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
615 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
616 effecterDataSize, effecterValue,
617 sizeof(effecterValue));
618 }
619
620 return ccOnlyResponse(request, rc);
621}
622
Sampa Misra12afe112020-05-25 11:40:44 -0500623void Handler::generateTerminusLocatorPDR(Repo& repo)
624{
625 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
626
627 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
628
629 pdr->hdr.record_handle = 0;
630 pdr->hdr.version = 1;
631 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
632 pdr->hdr.record_change_num = 0;
633 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530634 pdr->terminus_handle = TERMINUS_HANDLE;
Sampa Misra12afe112020-05-25 11:40:44 -0500635 pdr->validity = PLDM_TL_PDR_VALID;
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530636 pdr->tid = TERMINUS_ID;
Sampa Misra12afe112020-05-25 11:40:44 -0500637 pdr->container_id = 0x0;
638 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
639 pdr->terminus_locator_value_size =
640 sizeof(pldm_terminus_locator_type_mctp_eid);
641 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
642 pdr->terminus_locator_value);
643 locatorValue->eid = BmcMctpEid;
644
645 PdrEntry pdrEntry{};
646 pdrEntry.data = pdrBuffer.data();
647 pdrEntry.size = pdrBuffer.size();
648 repo.addRecord(pdrEntry);
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530649 if (hostPDRHandler)
650 {
651 hostPDRHandler->tlPDRInfo.insert_or_assign(
652 pdr->terminus_handle,
653 std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
654 }
Sampa Misra12afe112020-05-25 11:40:44 -0500655}
George Liu362c18d2020-05-14 09:46:36 +0800656
657Response Handler::getStateSensorReadings(const pldm_msg* request,
658 size_t payloadLength)
659{
660 uint16_t sensorId{};
661 bitfield8_t sensorRearm{};
662 uint8_t reserved{};
663
664 if (payloadLength != PLDM_GET_SENSOR_READING_REQ_BYTES)
665 {
666 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
667 }
668
669 int rc = decode_get_state_sensor_readings_req(
670 request, payloadLength, &sensorId, &sensorRearm, &reserved);
671
672 if (rc != PLDM_SUCCESS)
673 {
674 return ccOnlyResponse(request, rc);
675 }
676
677 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800678 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500679 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800680 uint8_t comSensorCnt{};
681 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500682
683 uint16_t entityType{};
684 uint16_t entityInstance{};
685 uint16_t stateSetId{};
686
687 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
688 entityType, entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530689 oemPlatformHandler != nullptr && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500690 {
691 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
692 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
693 }
694 else
695 {
696 rc = platform_state_sensor::getStateSensorReadingsHandler<
697 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, sensorId,
698 sensorRearmCount, comSensorCnt,
699 stateField);
700 }
George Liu362c18d2020-05-14 09:46:36 +0800701
702 if (rc != PLDM_SUCCESS)
703 {
704 return ccOnlyResponse(request, rc);
705 }
706
707 Response response(sizeof(pldm_msg_hdr) +
708 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
709 sizeof(get_sensor_state_field) * comSensorCnt);
710 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
711 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
712 comSensorCnt, stateField.data(),
713 responsePtr);
714 if (rc != PLDM_SUCCESS)
715 {
716 return ccOnlyResponse(request, rc);
717 }
718
719 return response;
720}
721
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500722void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
723 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600724{
725 deferredGetPDREvent.reset();
726 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
727}
728
Sampa Misraaea5dde2020-08-31 08:33:47 -0500729bool isOemStateSensor(Handler& handler, uint16_t sensorId,
730 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
731 uint16_t& entityType, uint16_t& entityInstance,
732 uint16_t& stateSetId)
733{
734 pldm_state_sensor_pdr* pdr = nullptr;
735
736 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
737 pldm_pdr_init(), pldm_pdr_destroy);
738 Repo stateSensorPDRs(stateSensorPdrRepo.get());
739 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
740 if (stateSensorPDRs.empty())
741 {
742 std::cerr << "Failed to get record by PDR type\n";
743 return false;
744 }
745
746 PdrEntry pdrEntry{};
747 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
748 while (pdrRecord)
749 {
750 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
751 assert(pdr != NULL);
752 if (pdr->sensor_id != sensorId)
753 {
754 pdr = nullptr;
755 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
756 continue;
757 }
758 auto tmpEntityType = pdr->entity_type;
759 auto tmpEntityInstance = pdr->entity_instance;
760 auto tmpCompSensorCnt = pdr->composite_sensor_count;
761 auto tmpPossibleStates =
762 reinterpret_cast<state_sensor_possible_states*>(
763 pdr->possible_states);
764 auto tmpStateSetId = tmpPossibleStates->state_set_id;
765
766 if (sensorRearmCount > tmpCompSensorCnt)
767 {
768 std::cerr << "The requester sent wrong sensorRearm"
769 << " count for the sensor, SENSOR_ID=" << sensorId
770 << "SENSOR_REARM_COUNT=" << (uint16_t)sensorRearmCount
771 << "\n";
772 break;
773 }
774
775 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
776 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
777 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
778 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
779 {
780 entityType = tmpEntityType;
781 entityInstance = tmpEntityInstance;
782 stateSetId = tmpStateSetId;
783 compSensorCnt = tmpCompSensorCnt;
784 return true;
785 }
786 else
787 {
788 return false;
789 }
790 }
791 return false;
792}
793
794bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
795 uint8_t compEffecterCnt, uint16_t& entityType,
796 uint16_t& entityInstance, uint16_t& stateSetId)
797{
798 pldm_state_effecter_pdr* pdr = nullptr;
799
800 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
801 pldm_pdr_init(), pldm_pdr_destroy);
802 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
803 getRepoByType(handler.getRepo(), stateEffecterPDRs,
804 PLDM_STATE_EFFECTER_PDR);
805 if (stateEffecterPDRs.empty())
806 {
807 std::cerr << "Failed to get record by PDR type\n";
808 return false;
809 }
810
811 PdrEntry pdrEntry{};
812 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
813 while (pdrRecord)
814 {
815 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
816 assert(pdr != NULL);
817 if (pdr->effecter_id != effecterId)
818 {
819 pdr = nullptr;
820 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
821 continue;
822 }
823
824 auto tmpEntityType = pdr->entity_type;
825 auto tmpEntityInstance = pdr->entity_instance;
826 auto tmpPossibleStates =
827 reinterpret_cast<state_effecter_possible_states*>(
828 pdr->possible_states);
829 auto tmpStateSetId = tmpPossibleStates->state_set_id;
830
831 if (compEffecterCnt > pdr->composite_effecter_count)
832 {
833 std::cerr << "The requester sent wrong composite effecter"
834 << " count for the effecter, EFFECTER_ID=" << effecterId
835 << "COMP_EFF_CNT=" << (uint16_t)compEffecterCnt << "\n";
836 return false;
837 }
838
839 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
840 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
841 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
842 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
843 {
844 entityType = tmpEntityType;
845 entityInstance = tmpEntityInstance;
846 stateSetId = tmpStateSetId;
847 return true;
848 }
849 else
850 {
851 return false;
852 }
853 }
854 return false;
855}
856
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600857} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530858} // namespace responder
859} // namespace pldm