blob: db6c7b7b1f84ca62beb832ca95051bccddeea371 [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"
Sagar Srinivas90314a32023-10-17 10:38:03 -050014#include "pldmd/handler.hpp"
15#include "requester/handler.hpp"
George Liu83409572019-12-24 18:42:54 +080016
George Liuc453e162022-12-21 17:16:23 +080017#include <libpldm/entity.h>
18#include <libpldm/state_set.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +053019
Riya Dixit49cfb132023-03-02 04:26:53 -060020#include <phosphor-logging/lg2.hpp>
21
22PHOSPHOR_LOG2_USING;
23
Brad Bishop5079ac42021-08-19 18:35:06 -040024using namespace pldm::utils;
25using namespace pldm::responder::pdr;
26using namespace pldm::responder::pdr_utils;
27
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053028namespace pldm
29{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053030namespace responder
31{
Sampa Misraa2fa0702019-05-31 01:28:55 -050032namespace platform
33{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060034using InternalFailure =
35 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
36
George Liu1ec85d42020-02-12 16:05:32 +080037static const Json empty{};
38
George Liua2870722020-02-11 11:09:30 +080039void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080040 uint16_t id,
41 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
42 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080043{
George Liuadbe1722020-05-09 19:20:19 +080044 if (typeId == TypeId::PLDM_SENSOR_ID)
45 {
46 sensorDbusObjMaps.emplace(id, dbusObj);
47 }
48 else
49 {
50 effecterDbusObjMaps.emplace(id, dbusObj);
51 }
George Liu1ec85d42020-02-12 16:05:32 +080052}
53
George Liua2870722020-02-11 11:09:30 +080054const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080055 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080056{
George Liuadbe1722020-05-09 19:20:19 +080057 if (typeId == TypeId::PLDM_SENSOR_ID)
58 {
59 return sensorDbusObjMaps.at(id);
60 }
61 else
62 {
63 return effecterDbusObjMaps.at(id);
64 }
George Liu1ec85d42020-02-12 16:05:32 +080065}
66
George Liu36e81352020-07-01 14:40:30 +080067void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060068 const std::vector<fs::path>& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060069{
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060070 for (const auto& directory : dir)
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050071 {
Riya Dixit89644442024-03-31 05:39:59 -050072 info("Checking if directory '{DIRECTORY}' exists", "DIRECTORY",
73 directory);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060074 if (!fs::exists(directory))
75 {
76 return;
77 }
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050078 }
79
Deepak Kodihallic682fe22020-03-04 00:42:54 -060080 // A map of PDR type to a lambda that handles creation of that PDR type.
81 // The lambda essentially would parse the platform specific PDR JSONs to
82 // generate the PDR structures. This function iterates through the map to
83 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080084
Patrick Williamsa6756622023-10-20 11:19:15 -050085 const std::map<Type, generatePDR> generateHandlers = {
86 {PLDM_STATE_EFFECTER_PDR,
87 [this](const DBusHandler& dBusIntf, const auto& json,
88 RepoInterface& repo) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040089 pdr_state_effecter::generateStateEffecterPDR<
90 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
91 repo);
92 }},
Patrick Williamsa6756622023-10-20 11:19:15 -050093 {PLDM_NUMERIC_EFFECTER_PDR,
94 [this](const DBusHandler& dBusIntf, const auto& json,
95 RepoInterface& repo) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040096 pdr_numeric_effecter::generateNumericEffecterPDR<
97 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
98 repo);
99 }},
George Liuadbe1722020-05-09 19:20:19 +0800100 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
101 const auto& json, RepoInterface& repo) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400102 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
103 Handler>(dBusIntf, json,
104 *this, repo);
105 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600106
107 Type pdrType{};
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600108 for (const auto& directory : dir)
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600109 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600110 for (const auto& dirEntry : fs::directory_iterator(directory))
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600111 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600112 try
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600113 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600114 if (fs::is_regular_file(dirEntry.path().string()))
George Liu1ec85d42020-02-12 16:05:32 +0800115 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600116 auto json = readJson(dirEntry.path().string());
117 if (!json.empty())
118 {
119 auto effecterPDRs = json.value("effecterPDRs", empty);
120 for (const auto& effecter : effecterPDRs)
121 {
122 pdrType = effecter.value("pdrType", 0);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400123 generateHandlers.at(
124 pdrType)(dBusIntf, effecter, repo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600125 }
George Liuadbe1722020-05-09 19:20:19 +0800126
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600127 auto sensorPDRs = json.value("sensorPDRs", empty);
128 for (const auto& sensor : sensorPDRs)
129 {
130 pdrType = sensor.value("pdrType", 0);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400131 generateHandlers.at(
132 pdrType)(dBusIntf, sensor, repo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600133 }
134 }
George Liuadbe1722020-05-09 19:20:19 +0800135 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600136 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600137 catch (const InternalFailure& e)
138 {
139 error(
Riya Dixit89644442024-03-31 05:39:59 -0500140 "PDR config directory '{PATH}' does not exist or empty for '{TYPE}' pdr, error - {ERROR}",
141 "PATH", dirEntry.path(), "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600142 }
143 catch (const Json::exception& e)
144 {
Riya Dixit89644442024-03-31 05:39:59 -0500145 error(
146 "Failed to parse PDR JSON file for '{TYPE}' pdr, error - {ERROR}",
147 "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600148 pldm::utils::reportError(
149 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
150 }
151 catch (const std::exception& e)
152 {
Riya Dixit89644442024-03-31 05:39:59 -0500153 error(
154 "Failed to parse PDR JSON file for '{TYPE}' pdr, error - {ERROR}",
155 "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600156 pldm::utils::reportError(
157 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
158 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600159 }
160 }
161}
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530162
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600163Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530164{
Manojkiran Edac5a11202024-07-03 12:12:57 +0530165 if (oemPlatformHandler)
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500166 {
Manojkiran Edac5a11202024-07-03 12:12:57 +0530167 auto rc = oemPlatformHandler->checkBMCState();
168 if (rc != PLDM_SUCCESS)
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500169 {
Manojkiran Edac5a11202024-07-03 12:12:57 +0530170 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500171 }
172 }
173
174 // Build FRU table if not built, since entity association PDR's
175 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530176 if (fruHandler)
177 {
178 fruHandler->buildFRUTable();
179 }
180
George Liud680ae02020-07-17 09:11:14 +0800181 if (!pdrCreated)
182 {
183 generateTerminusLocatorPDR(pdrRepo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600184 if (platformConfigHandler)
185 {
186 auto systemType = platformConfigHandler->getPlatformName();
187 if (systemType.has_value())
188 {
189 // In case of normal poweron , the system type would have been
190 // already filled by entity manager when ever BMC reaches Ready
191 // state. If this is not filled by time we get a getpdr request
192 // we can assume that the entity manager service is not present
193 // on this system & continue to build the common PDR's.
194 pdrJsonsDir.push_back(pdrJsonDir / systemType.value());
195 }
196 }
197
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500198 if (oemPlatformHandler != nullptr)
199 {
200 oemPlatformHandler->buildOEMPDR(pdrRepo);
201 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600202 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500203
George Liud680ae02020-07-17 09:11:14 +0800204 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800205
206 if (dbusToPLDMEventHandler)
207 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600208 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
209 event,
210 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
211 _processPostGetPDRActions),
212 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800213 }
George Liud680ae02020-07-17 09:11:14 +0800214 }
215
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530216 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530217
218 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
219 {
George Liufb8611d2019-12-06 10:14:15 +0800220 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530221 }
222
223 uint32_t recordHandle{};
224 uint32_t dataTransferHandle{};
225 uint8_t transferOpFlag{};
226 uint16_t reqSizeBytes{};
227 uint16_t recordChangeNum{};
228
George Liufb8611d2019-12-06 10:14:15 +0800229 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
230 &dataTransferHandle, &transferOpFlag,
231 &reqSizeBytes, &recordChangeNum);
232 if (rc != PLDM_SUCCESS)
233 {
234 return CmdHandler::ccOnlyResponse(request, rc);
235 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530236
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530237 uint16_t respSizeBytes{};
238 uint8_t* recordData = nullptr;
239 try
240 {
George Liue53193f2020-02-24 09:23:26 +0800241 pdr_utils::PdrEntry e;
242 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
Jayanth Othayoth7c14fc42024-12-17 10:24:47 -0600243 if (record == nullptr)
George Liue53193f2020-02-24 09:23:26 +0800244 {
245 return CmdHandler::ccOnlyResponse(
246 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
247 }
248
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530249 if (reqSizeBytes)
250 {
George Liue53193f2020-02-24 09:23:26 +0800251 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530252 if (respSizeBytes > reqSizeBytes)
253 {
254 respSizeBytes = reqSizeBytes;
255 }
George Liue53193f2020-02-24 09:23:26 +0800256 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530257 }
258 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
259 respSizeBytes,
260 0);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530261 auto responsePtr = new (response.data()) pldm_msg;
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500262 rc = encode_get_pdr_resp(
263 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
264 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800265 if (rc != PLDM_SUCCESS)
266 {
267 return ccOnlyResponse(request, rc);
268 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530269 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530270 catch (const std::exception& e)
271 {
Riya Dixit89644442024-03-31 05:39:59 -0500272 error(
273 "Failed to access PDR record handle '{RECORD_HANDLE}', error - {ERROR}",
274 "RECORD_HANDLE", recordHandle, "ERROR", e);
George Liufb8611d2019-12-06 10:14:15 +0800275 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530276 }
277 return response;
278}
279
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600280Response Handler::setStateEffecterStates(const pldm_msg* request,
281 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500282{
283 Response response(
284 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530285 auto responsePtr = new (response.data()) pldm_msg;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500286 uint16_t effecterId;
287 uint8_t compEffecterCnt;
288 constexpr auto maxCompositeEffecterCnt = 8;
289 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
290 {0, 0});
291
292 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
293 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
294 sizeof(set_effecter_state_field)))
295 {
George Liufb8611d2019-12-06 10:14:15 +0800296 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500297 }
298
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400299 int rc = decode_set_state_effecter_states_req(
300 request, payloadLength, &effecterId, &compEffecterCnt,
301 stateField.data());
Sampa Misraa2fa0702019-05-31 01:28:55 -0500302
George Liufb8611d2019-12-06 10:14:15 +0800303 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500304 {
George Liufb8611d2019-12-06 10:14:15 +0800305 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500306 }
307
George Liufb8611d2019-12-06 10:14:15 +0800308 stateField.resize(compEffecterCnt);
309 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500310 uint16_t entityType{};
311 uint16_t entityInstance{};
312 uint16_t stateSetId{};
313
314 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
315 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530316 oemPlatformHandler != nullptr &&
317 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500318 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500319 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500320 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
321 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500322 }
323 else
324 {
325 rc = platform_state_effecter::setStateEffecterStatesHandler<
326 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
327 stateField);
328 }
George Liufb8611d2019-12-06 10:14:15 +0800329 if (rc != PLDM_SUCCESS)
330 {
331 return CmdHandler::ccOnlyResponse(request, rc);
332 }
333
334 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
335 responsePtr);
336 if (rc != PLDM_SUCCESS)
337 {
338 return ccOnlyResponse(request, rc);
339 }
340
Sampa Misraa2fa0702019-05-31 01:28:55 -0500341 return response;
342}
343
Tom Joseph56e45c52020-03-16 10:01:45 +0530344Response Handler::platformEventMessage(const pldm_msg* request,
345 size_t payloadLength)
346{
347 uint8_t formatVersion{};
348 uint8_t tid{};
349 uint8_t eventClass{};
350 size_t offset{};
351
352 auto rc = decode_platform_event_message_req(
353 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
354 if (rc != PLDM_SUCCESS)
355 {
356 return CmdHandler::ccOnlyResponse(request, rc);
357 }
358
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500359 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530360 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500361 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500362 if (oemPlatformHandler)
363 {
Sagar Srinivas18145f72022-04-11 07:38:26 -0500364 if (oemPlatformHandler->watchDogRunning())
365 {
366 oemPlatformHandler->resetWatchDogTimer();
367 }
368 else
369 {
370 oemPlatformHandler->setSurvTimer(tid, true);
371 }
Sagar Srinivas79669c92021-04-28 15:43:30 -0500372 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500373 }
374 else
375 {
376 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530377 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500378 const auto& handlers = eventHandlers.at(eventClass);
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000379 bool oneFailedHandler = false;
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500380 for (const auto& handler : handlers)
Tom Joseph56e45c52020-03-16 10:01:45 +0530381 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400382 auto rc =
383 handler(request, payloadLength, formatVersion, tid, offset);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500384 if (rc != PLDM_SUCCESS)
385 {
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000386 oneFailedHandler = true;
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500387 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530388 }
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000389 if (oneFailedHandler)
390 {
391 return CmdHandler::ccOnlyResponse(request, rc);
392 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530393 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500394 catch (const std::out_of_range& e)
395 {
Riya Dixit89644442024-03-31 05:39:59 -0500396 error("Failed to handle platform event msg, error - {ERROR}",
397 "ERROR", e);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500398 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
399 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530400 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530401 Response response(
402 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530403 auto responsePtr = new (response.data()) pldm_msg;
Tom Joseph56e45c52020-03-16 10:01:45 +0530404
405 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
406 PLDM_EVENT_NO_LOGGING, responsePtr);
407 if (rc != PLDM_SUCCESS)
408 {
409 return ccOnlyResponse(request, rc);
410 }
411
412 return response;
413}
414
415int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530416 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530417 size_t eventDataOffset)
418{
419 uint16_t sensorId{};
420 uint8_t eventClass{};
421 size_t eventClassDataOffset{};
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400422 auto eventData =
423 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
Tom Joseph56e45c52020-03-16 10:01:45 +0530424 auto eventDataSize = payloadLength - eventDataOffset;
425
426 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
427 &eventClass, &eventClassDataOffset);
428 if (rc != PLDM_SUCCESS)
429 {
430 return rc;
431 }
432
Zahed Hossain75330f32020-03-24 02:15:03 -0500433 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
434 eventDataOffset + eventClassDataOffset;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400435 auto eventClassDataSize =
436 payloadLength - eventDataOffset - eventClassDataOffset;
Zahed Hossain75330f32020-03-24 02:15:03 -0500437
Tom Joseph56e45c52020-03-16 10:01:45 +0530438 if (eventClass == PLDM_STATE_SENSOR_STATE)
439 {
440 uint8_t sensorOffset{};
441 uint8_t eventState{};
442 uint8_t previousEventState{};
443
Zahed Hossain75330f32020-03-24 02:15:03 -0500444 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530445 &sensorOffset, &eventState,
446 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500447 if (rc != PLDM_SUCCESS)
448 {
449 return PLDM_ERROR;
450 }
451
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800452 // Emitting state sensor event signal
453 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
454 previousEventState);
455
Tom Josephc4959c32020-04-20 19:50:16 +0530456 // If there are no HOST PDR's, there is no further action
Jayanth Othayoth7c14fc42024-12-17 10:24:47 -0600457 if (hostPDRHandler == nullptr)
Tom Josephc4959c32020-04-20 19:50:16 +0530458 {
459 return PLDM_SUCCESS;
460 }
461
462 // Handle PLDM events for which PDR is available
463 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530464
465 pldm::pdr::EntityInfo entityInfo{};
466 pldm::pdr::CompositeSensorStates compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600467 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Tom Josephb70a1962020-07-13 12:56:31 +0530468
Tom Josephc4959c32020-04-20 19:50:16 +0530469 try
470 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600471 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephc4959c32020-04-20 19:50:16 +0530472 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530473 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500474 catch (const std::out_of_range&)
Tom Josephc4959c32020-04-20 19:50:16 +0530475 {
Tom Josephb70a1962020-07-13 12:56:31 +0530476 // If there is no mapping for tid, sensorId combination, try
477 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
478 // implement TL PDR.
479 try
480 {
481 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600482 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephb70a1962020-07-13 12:56:31 +0530483 hostPDRHandler->lookupSensorInfo(sensorEntry);
484 }
485 // If there is no mapping for events return PLDM_SUCCESS
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500486 catch (const std::out_of_range&)
Tom Josephb70a1962020-07-13 12:56:31 +0530487 {
488 return PLDM_SUCCESS;
489 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500490 }
Tom Josephb70a1962020-07-13 12:56:31 +0530491
492 if (sensorOffset >= compositeSensorStates.size())
493 {
494 return PLDM_ERROR_INVALID_DATA;
495 }
496
497 const auto& possibleStates = compositeSensorStates[sensorOffset];
Sagar Srinivas06f9b292024-03-31 11:35:28 -0500498 if (!possibleStates.contains(eventState))
Tom Josephb70a1962020-07-13 12:56:31 +0530499 {
500 return PLDM_ERROR_INVALID_DATA;
501 }
502
503 const auto& [containerId, entityType, entityInstance] = entityInfo;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400504 events::StateSensorEntry stateSensorEntry{
505 containerId,
506 entityType,
507 entityInstance,
508 sensorOffset,
509 stateSetIds[sensorOffset],
510 false};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600511 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
512 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530513 }
514 else
515 {
516 return PLDM_ERROR_INVALID_DATA;
517 }
518
519 return PLDM_SUCCESS;
520}
521
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400522int Handler::pldmPDRRepositoryChgEvent(
523 const pldm_msg* request, size_t payloadLength, uint8_t /*formatVersion*/,
524 uint8_t tid, size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500525{
526 uint8_t eventDataFormat{};
527 uint8_t numberOfChangeRecords{};
528 size_t dataOffset{};
529
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400530 auto eventData =
531 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500532 auto eventDataSize = payloadLength - eventDataOffset;
533
534 auto rc = decode_pldm_pdr_repository_chg_event_data(
535 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
536 &dataOffset);
537 if (rc != PLDM_SUCCESS)
538 {
539 return rc;
540 }
541
542 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500543
544 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
545 {
546 return PLDM_ERROR_INVALID_DATA;
547 }
548
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500549 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
550 {
551 uint8_t eventDataOperation{};
552 uint8_t numberOfChangeEntries{};
553
554 auto changeRecordData = eventData + dataOffset;
555 auto changeRecordDataSize = eventDataSize - dataOffset;
556
557 while (changeRecordDataSize)
558 {
559 rc = decode_pldm_pdr_repository_change_record_data(
560 changeRecordData, changeRecordDataSize, &eventDataOperation,
561 &numberOfChangeEntries, &dataOffset);
562
563 if (rc != PLDM_SUCCESS)
564 {
565 return rc;
566 }
567
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500568 if (eventDataOperation == PLDM_RECORDS_ADDED ||
569 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500570 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500571 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
572 {
573 hostPDRHandler->isHostPdrModified = true;
574 }
575
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500576 rc = getPDRRecordHandles(
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400577 reinterpret_cast<const ChangeEntry*>(
578 changeRecordData + dataOffset),
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500579 changeRecordDataSize - dataOffset,
580 static_cast<size_t>(numberOfChangeEntries),
581 pdrRecordHandles);
582
583 if (rc != PLDM_SUCCESS)
584 {
585 return rc;
586 }
587 }
588
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400589 changeRecordData +=
590 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500591 changeRecordDataSize -=
592 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
593 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500594 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500595 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500596 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530597 // if we get a Repository change event with the eventDataFormat
598 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
599 // have the matched Terminus handle
600 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
601 {
602 // We cannot get the Repo change event from the Terminus
603 // that is not already added to the BMC repository
604
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500605 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
606 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530607 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500608 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530609 {
610 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500611 it->first);
612 hostPDRHandler->tlPDRInfo.erase(it++);
613 }
614 else
615 {
616 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530617 }
618 }
619 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500620 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500621 }
622
623 return PLDM_SUCCESS;
624}
625
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400626int Handler::getPDRRecordHandles(
627 const ChangeEntry* changeEntryData, size_t changeEntryDataSize,
628 size_t numberOfChangeEntries, PDRRecordHandles& pdrRecordHandles)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500629{
630 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
631 {
632 return PLDM_ERROR_INVALID_DATA;
633 }
634 for (size_t i = 0; i < numberOfChangeEntries; i++)
635 {
636 pdrRecordHandles.push_back(changeEntryData[i]);
637 }
638 return PLDM_SUCCESS;
639}
640
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500641Response Handler::getNumericEffecterValue(const pldm_msg* request,
642 size_t payloadLength)
643{
644 if (payloadLength != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES)
645 {
646 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
647 }
648
649 uint16_t effecterId{};
650 auto rc = decode_get_numeric_effecter_value_req(request, payloadLength,
651 &effecterId);
652 if (rc != PLDM_SUCCESS)
653 {
654 return ccOnlyResponse(request, rc);
655 }
656
657 const pldm::utils::DBusHandler dBusIntf;
658 uint8_t effecterDataSize{};
659 pldm::utils::PropertyValue dbusValue;
660 std::string propertyType;
661 using effecterOperationalState = uint8_t;
662 using completionCode = uint8_t;
663
664 rc = platform_numeric_effecter::getNumericEffecterData<
665 pldm::utils::DBusHandler, Handler>(
666 dBusIntf, *this, effecterId, effecterDataSize, propertyType, dbusValue);
667
668 if (rc != PLDM_SUCCESS)
669 {
670 return ccOnlyResponse(request, rc);
671 }
672
673 // Refer DSP0248_1.2.0.pdf (section 22.3, Table 48)
674 // Completion Code (uint8), Effecter Data Size(uint8), Effecter Operational
675 // State(uint8), PendingValue (uint8|sint8|uint16|sint16|uint32|sint32 )
676 // PresentValue (uint8|sint8|uint16|sint16|uint32|sint32 )
677 // Size of PendingValue and PresentValue calculated based on size is
678 // provided in effecter data size
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400679 size_t responsePayloadLength =
680 sizeof(completionCode) + sizeof(effecterDataSize) +
681 sizeof(effecterOperationalState) +
682 getEffecterDataSize(effecterDataSize) +
683 getEffecterDataSize(effecterDataSize);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500684
685 Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530686 auto responsePtr = new (response.data()) pldm_msg;
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500687
688 rc = platform_numeric_effecter::getNumericEffecterValueHandler(
689 propertyType, dbusValue, effecterDataSize, responsePtr,
690 responsePayloadLength, request->hdr.instance_id);
691
692 if (rc != PLDM_SUCCESS)
693 {
694 error(
Riya Dixit89644442024-03-31 05:39:59 -0500695 "Failed to get response of GetNumericEffecterValue for effecter ID '{EFFECTERID}', response code '{RC}'.",
696 "EFFECTERID", effecterId, "RC", rc);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500697 return ccOnlyResponse(request, rc);
698 }
699 return response;
700}
701
George Liueccb0c52020-01-14 11:09:56 +0800702Response Handler::setNumericEffecterValue(const pldm_msg* request,
703 size_t payloadLength)
704{
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400705 Response response(
706 sizeof(pldm_msg_hdr) + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
George Liueccb0c52020-01-14 11:09:56 +0800707 uint16_t effecterId{};
708 uint8_t effecterDataSize{};
709 uint8_t effecterValue[4] = {};
710
711 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
712 sizeof(union_effecter_data_size)) ||
713 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
714 {
715 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
716 }
717
718 int rc = decode_set_numeric_effecter_value_req(
Andrew Jeffery8fbf3cc2023-04-12 13:42:29 +0930719 request, payloadLength, &effecterId, &effecterDataSize, effecterValue);
George Liueccb0c52020-01-14 11:09:56 +0800720
721 if (rc == PLDM_SUCCESS)
722 {
723 const pldm::utils::DBusHandler dBusIntf;
724 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400725 pldm::utils::DBusHandler, Handler>(
726 dBusIntf, *this, effecterId, effecterDataSize, effecterValue,
727 sizeof(effecterValue));
George Liueccb0c52020-01-14 11:09:56 +0800728 }
729
730 return ccOnlyResponse(request, rc);
731}
732
Sampa Misra12afe112020-05-25 11:40:44 -0500733void Handler::generateTerminusLocatorPDR(Repo& repo)
734{
735 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
736
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530737 auto pdr = new (pdrBuffer.data()) pldm_terminus_locator_pdr;
Sampa Misra12afe112020-05-25 11:40:44 -0500738
739 pdr->hdr.record_handle = 0;
740 pdr->hdr.version = 1;
741 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
742 pdr->hdr.record_change_num = 0;
743 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530744 pdr->terminus_handle = TERMINUS_HANDLE;
Sampa Misra12afe112020-05-25 11:40:44 -0500745 pdr->validity = PLDM_TL_PDR_VALID;
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530746 pdr->tid = TERMINUS_ID;
Sampa Misra12afe112020-05-25 11:40:44 -0500747 pdr->container_id = 0x0;
748 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
749 pdr->terminus_locator_value_size =
750 sizeof(pldm_terminus_locator_type_mctp_eid);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530751 auto locatorValue = new (pdr->terminus_locator_value)
752 pldm_terminus_locator_type_mctp_eid;
Thu Nguyen51d66b52024-08-06 09:15:55 +0000753 locatorValue->eid = pldm::BmcMctpEid;
Sampa Misra12afe112020-05-25 11:40:44 -0500754
755 PdrEntry pdrEntry{};
756 pdrEntry.data = pdrBuffer.data();
757 pdrEntry.size = pdrBuffer.size();
758 repo.addRecord(pdrEntry);
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530759 if (hostPDRHandler)
760 {
761 hostPDRHandler->tlPDRInfo.insert_or_assign(
762 pdr->terminus_handle,
763 std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
764 }
Sampa Misra12afe112020-05-25 11:40:44 -0500765}
George Liu362c18d2020-05-14 09:46:36 +0800766
767Response Handler::getStateSensorReadings(const pldm_msg* request,
768 size_t payloadLength)
769{
770 uint16_t sensorId{};
771 bitfield8_t sensorRearm{};
772 uint8_t reserved{};
773
Pavithra Barithaya87083f22023-04-17 01:27:49 -0500774 if (payloadLength != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES)
George Liu362c18d2020-05-14 09:46:36 +0800775 {
776 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
777 }
778
779 int rc = decode_get_state_sensor_readings_req(
780 request, payloadLength, &sensorId, &sensorRearm, &reserved);
781
782 if (rc != PLDM_SUCCESS)
783 {
784 return ccOnlyResponse(request, rc);
785 }
786
787 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800788 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500789 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800790 uint8_t comSensorCnt{};
791 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500792
793 uint16_t entityType{};
794 uint16_t entityInstance{};
795 uint16_t stateSetId{};
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530796 uint16_t containerId{};
Sampa Misraaea5dde2020-08-31 08:33:47 -0500797
798 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530799 entityType, entityInstance, stateSetId, containerId) &&
800 oemPlatformHandler && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500801 {
802 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530803 entityType, entityInstance, containerId, stateSetId, comSensorCnt,
804 sensorId, stateField);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500805 }
806 else
807 {
808 rc = platform_state_sensor::getStateSensorReadingsHandler<
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530809 pldm::utils::DBusHandler, Handler>(
810 dBusIntf, *this, sensorId, sensorRearmCount, comSensorCnt,
811 stateField, dbusToPLDMEventHandler->getSensorCache());
Sampa Misraaea5dde2020-08-31 08:33:47 -0500812 }
George Liu362c18d2020-05-14 09:46:36 +0800813
814 if (rc != PLDM_SUCCESS)
815 {
816 return ccOnlyResponse(request, rc);
817 }
818
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400819 Response response(
820 sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
821 sizeof(get_sensor_state_field) * comSensorCnt);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530822 auto responsePtr = new (response.data()) pldm_msg;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400823 rc = encode_get_state_sensor_readings_resp(
824 request->hdr.instance_id, rc, comSensorCnt, stateField.data(),
825 responsePtr);
George Liu362c18d2020-05-14 09:46:36 +0800826 if (rc != PLDM_SUCCESS)
827 {
828 return ccOnlyResponse(request, rc);
829 }
830
831 return response;
832}
833
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500834void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
835 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600836{
837 deferredGetPDREvent.reset();
838 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
839}
840
Sampa Misraaea5dde2020-08-31 08:33:47 -0500841bool isOemStateSensor(Handler& handler, uint16_t sensorId,
842 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
843 uint16_t& entityType, uint16_t& entityInstance,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530844 uint16_t& stateSetId, uint16_t& containerId)
Sampa Misraaea5dde2020-08-31 08:33:47 -0500845{
846 pldm_state_sensor_pdr* pdr = nullptr;
847
848 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
849 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930850 if (!stateSensorPdrRepo)
851 {
852 error("Failed to instantiate state sensor PDR repository");
853 return false;
854 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500855 Repo stateSensorPDRs(stateSensorPdrRepo.get());
856 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
857 if (stateSensorPDRs.empty())
858 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600859 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500860 return false;
861 }
862
863 PdrEntry pdrEntry{};
864 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
865 while (pdrRecord)
866 {
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530867 pdr = new (pdrEntry.data) pldm_state_sensor_pdr;
Jayanth Othayoth7c14fc42024-12-17 10:24:47 -0600868 assert(pdr != nullptr);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500869 if (pdr->sensor_id != sensorId)
870 {
871 pdr = nullptr;
872 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
873 continue;
874 }
875 auto tmpEntityType = pdr->entity_type;
876 auto tmpEntityInstance = pdr->entity_instance;
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530877 auto tmpEntityContainerId = pdr->container_id;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500878 auto tmpCompSensorCnt = pdr->composite_sensor_count;
879 auto tmpPossibleStates =
880 reinterpret_cast<state_sensor_possible_states*>(
881 pdr->possible_states);
882 auto tmpStateSetId = tmpPossibleStates->state_set_id;
883
884 if (sensorRearmCount > tmpCompSensorCnt)
885 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600886 error(
Riya Dixit89644442024-03-31 05:39:59 -0500887 "The requester sent wrong sensor rearm count '{SENSOR_REARM_COUNT}' for the sensor ID '{SENSORID}'.",
888 "SENSOR_REARM_COUNT", (uint16_t)sensorRearmCount, "SENSORID",
889 sensorId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500890 break;
891 }
892
893 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
894 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
895 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
896 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
897 {
898 entityType = tmpEntityType;
899 entityInstance = tmpEntityInstance;
900 stateSetId = tmpStateSetId;
901 compSensorCnt = tmpCompSensorCnt;
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530902 containerId = tmpEntityContainerId;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500903 return true;
904 }
905 else
906 {
907 return false;
908 }
909 }
910 return false;
911}
912
913bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
914 uint8_t compEffecterCnt, uint16_t& entityType,
915 uint16_t& entityInstance, uint16_t& stateSetId)
916{
917 pldm_state_effecter_pdr* pdr = nullptr;
918
919 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
920 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930921 if (!stateEffecterPdrRepo)
922 {
923 error("Failed to instantiate state effecter PDR repository");
924 return false;
925 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500926 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
927 getRepoByType(handler.getRepo(), stateEffecterPDRs,
928 PLDM_STATE_EFFECTER_PDR);
929 if (stateEffecterPDRs.empty())
930 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600931 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500932 return false;
933 }
934
935 PdrEntry pdrEntry{};
936 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
937 while (pdrRecord)
938 {
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530939 pdr = new (pdrEntry.data) pldm_state_effecter_pdr;
Jayanth Othayoth7c14fc42024-12-17 10:24:47 -0600940 assert(pdr != nullptr);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500941 if (pdr->effecter_id != effecterId)
942 {
943 pdr = nullptr;
944 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
945 continue;
946 }
947
948 auto tmpEntityType = pdr->entity_type;
949 auto tmpEntityInstance = pdr->entity_instance;
950 auto tmpPossibleStates =
951 reinterpret_cast<state_effecter_possible_states*>(
952 pdr->possible_states);
953 auto tmpStateSetId = tmpPossibleStates->state_set_id;
954
955 if (compEffecterCnt > pdr->composite_effecter_count)
956 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600957 error(
Riya Dixit89644442024-03-31 05:39:59 -0500958 "The requester sent wrong composite effecter count '{COMPOSITE_EFFECTER_COUNT}' for the effecter ID '{EFFECTERID}'.",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500959 "COMPOSITE_EFFECTER_COUNT", compEffecterCnt, "EFFECTERID",
960 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500961 return false;
962 }
963
964 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
965 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
966 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
967 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
968 {
969 entityType = tmpEntityType;
970 entityInstance = tmpEntityInstance;
971 stateSetId = tmpStateSetId;
972 return true;
973 }
974 else
975 {
976 return false;
977 }
978 }
979 return false;
980}
981
Sagar Srinivas90314a32023-10-17 10:38:03 -0500982void Handler::setEventReceiver()
983{
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400984 std::vector<uint8_t> requestMsg(
985 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530986 auto request = new (requestMsg.data()) pldm_msg;
Sagar Srinivas90314a32023-10-17 10:38:03 -0500987 auto instanceId = instanceIdDb->next(eid);
988 uint8_t eventMessageGlobalEnable =
989 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
990 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
Thu Nguyen51d66b52024-08-06 09:15:55 +0000991 uint8_t eventReceiverAddressInfo = pldm::BmcMctpEid;
Sagar Srinivas90314a32023-10-17 10:38:03 -0500992 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
993
994 auto rc = encode_set_event_receiver_req(
995 instanceId, eventMessageGlobalEnable, transportProtocolType,
996 eventReceiverAddressInfo, heartbeatTimer, request);
997 if (rc != PLDM_SUCCESS)
998 {
999 instanceIdDb->free(eid, instanceId);
Riya Dixit89644442024-03-31 05:39:59 -05001000 error(
1001 "Failed to encode set event receiver request, response code '{RC}'",
1002 "RC", lg2::hex, rc);
Sagar Srinivas90314a32023-10-17 10:38:03 -05001003 return;
1004 }
1005
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001006 auto processSetEventReceiverResponse = [](mctp_eid_t /*eid*/,
1007 const pldm_msg* response,
1008 size_t respMsgLen) {
Sagar Srinivas90314a32023-10-17 10:38:03 -05001009 if (response == nullptr || !respMsgLen)
1010 {
1011 error("Failed to receive response for setEventReceiver command");
1012 return;
1013 }
1014
1015 uint8_t completionCode{};
1016 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
1017 &completionCode);
1018 if (rc || completionCode)
1019 {
1020 error(
Riya Dixit89644442024-03-31 05:39:59 -05001021 "Failed to decode setEventReceiver command, response code '{RC}' and completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -05001022 "RC", rc, "CC", completionCode);
Sagar Srinivas90314a32023-10-17 10:38:03 -05001023 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +05301024 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Sagar Srinivas90314a32023-10-17 10:38:03 -05001025 }
1026 };
1027 rc = handler->registerRequest(
1028 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
1029 std::move(requestMsg), std::move(processSetEventReceiverResponse));
1030
1031 if (rc != PLDM_SUCCESS)
1032 {
1033 error("Failed to send the setEventReceiver request");
1034 }
1035
1036 if (oemPlatformHandler)
1037 {
1038 oemPlatformHandler->countSetEventReceiver();
1039 oemPlatformHandler->checkAndDisableWatchDog();
1040 }
1041}
1042
Deepak Kodihallibc669f12019-11-28 08:52:07 -06001043} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301044} // namespace responder
1045} // namespace pldm