blob: be554dd8efc48e3dc21c6784d847a6ec22954cff [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/dbus_impl_requester.hpp"
15#include "pldmd/handler.hpp"
16#include "requester/handler.hpp"
George Liu83409572019-12-24 18:42:54 +080017
George Liuc453e162022-12-21 17:16:23 +080018#include <libpldm/entity.h>
19#include <libpldm/state_set.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +053020
Riya Dixit49cfb132023-03-02 04:26:53 -060021#include <phosphor-logging/lg2.hpp>
22
23PHOSPHOR_LOG2_USING;
24
Brad Bishop5079ac42021-08-19 18:35:06 -040025using namespace pldm::utils;
26using namespace pldm::responder::pdr;
27using namespace pldm::responder::pdr_utils;
28
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053029namespace pldm
30{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053031namespace responder
32{
Sampa Misraa2fa0702019-05-31 01:28:55 -050033namespace platform
34{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060035using InternalFailure =
36 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
37
George Liu1ec85d42020-02-12 16:05:32 +080038static const Json empty{};
39
George Liua2870722020-02-11 11:09:30 +080040void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080041 uint16_t id,
42 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
43 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080044{
George Liuadbe1722020-05-09 19:20:19 +080045 if (typeId == TypeId::PLDM_SENSOR_ID)
46 {
47 sensorDbusObjMaps.emplace(id, dbusObj);
48 }
49 else
50 {
51 effecterDbusObjMaps.emplace(id, dbusObj);
52 }
George Liu1ec85d42020-02-12 16:05:32 +080053}
54
George Liua2870722020-02-11 11:09:30 +080055const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080056 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080057{
George Liuadbe1722020-05-09 19:20:19 +080058 if (typeId == TypeId::PLDM_SENSOR_ID)
59 {
60 return sensorDbusObjMaps.at(id);
61 }
62 else
63 {
64 return effecterDbusObjMaps.at(id);
65 }
George Liu1ec85d42020-02-12 16:05:32 +080066}
67
George Liu36e81352020-07-01 14:40:30 +080068void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060069 const std::vector<fs::path>& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060070{
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060071 for (const auto& directory : dir)
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050072 {
Riya Dixit89644442024-03-31 05:39:59 -050073 info("Checking if directory '{DIRECTORY}' exists", "DIRECTORY",
74 directory);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060075 if (!fs::exists(directory))
76 {
77 return;
78 }
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050079 }
80
Deepak Kodihallic682fe22020-03-04 00:42:54 -060081 // A map of PDR type to a lambda that handles creation of that PDR type.
82 // The lambda essentially would parse the platform specific PDR JSONs to
83 // generate the PDR structures. This function iterates through the map to
84 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080085
Patrick Williamsa6756622023-10-20 11:19:15 -050086 const std::map<Type, generatePDR> generateHandlers = {
87 {PLDM_STATE_EFFECTER_PDR,
88 [this](const DBusHandler& dBusIntf, const auto& json,
89 RepoInterface& repo) {
Patrick Williams6da4f912023-05-10 07:50:53 -050090 pdr_state_effecter::generateStateEffecterPDR<pldm::utils::DBusHandler,
91 Handler>(dBusIntf, json,
92 *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050093 }},
94 {PLDM_NUMERIC_EFFECTER_PDR,
95 [this](const DBusHandler& dBusIntf, const auto& json,
96 RepoInterface& repo) {
Patrick Williams6da4f912023-05-10 07:50:53 -050097 pdr_numeric_effecter::generateNumericEffecterPDR<
98 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050099 }},
George Liuadbe1722020-05-09 19:20:19 +0800100 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
101 const auto& json, RepoInterface& repo) {
Patrick Williamsa6756622023-10-20 11:19:15 -0500102 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
103 Handler>(dBusIntf, json, *this,
104 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);
123 generateHandlers.at(pdrType)(dBusIntf, effecter,
124 repo);
125 }
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);
131 generateHandlers.at(pdrType)(dBusIntf, sensor,
132 repo);
133 }
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{
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500165 if (hostPDRHandler)
166 {
167 if (hostPDRHandler->isHostUp() && oemPlatformHandler != nullptr)
168 {
169 auto rc = oemPlatformHandler->checkBMCState();
170 if (rc != PLDM_SUCCESS)
171 {
172 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
173 }
174 }
175 }
176
177 // Build FRU table if not built, since entity association PDR's
178 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530179 if (fruHandler)
180 {
181 fruHandler->buildFRUTable();
182 }
183
George Liud680ae02020-07-17 09:11:14 +0800184 if (!pdrCreated)
185 {
186 generateTerminusLocatorPDR(pdrRepo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600187 if (platformConfigHandler)
188 {
189 auto systemType = platformConfigHandler->getPlatformName();
190 if (systemType.has_value())
191 {
192 // In case of normal poweron , the system type would have been
193 // already filled by entity manager when ever BMC reaches Ready
194 // state. If this is not filled by time we get a getpdr request
195 // we can assume that the entity manager service is not present
196 // on this system & continue to build the common PDR's.
197 pdrJsonsDir.push_back(pdrJsonDir / systemType.value());
198 }
199 }
200
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500201 if (oemPlatformHandler != nullptr)
202 {
203 oemPlatformHandler->buildOEMPDR(pdrRepo);
204 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600205 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500206
George Liud680ae02020-07-17 09:11:14 +0800207 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800208
209 if (dbusToPLDMEventHandler)
210 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600211 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
212 event,
213 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
214 _processPostGetPDRActions),
215 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800216 }
George Liud680ae02020-07-17 09:11:14 +0800217 }
218
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530219 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530220
221 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
222 {
George Liufb8611d2019-12-06 10:14:15 +0800223 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530224 }
225
226 uint32_t recordHandle{};
227 uint32_t dataTransferHandle{};
228 uint8_t transferOpFlag{};
229 uint16_t reqSizeBytes{};
230 uint16_t recordChangeNum{};
231
George Liufb8611d2019-12-06 10:14:15 +0800232 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
233 &dataTransferHandle, &transferOpFlag,
234 &reqSizeBytes, &recordChangeNum);
235 if (rc != PLDM_SUCCESS)
236 {
237 return CmdHandler::ccOnlyResponse(request, rc);
238 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530239
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530240 uint16_t respSizeBytes{};
241 uint8_t* recordData = nullptr;
242 try
243 {
George Liue53193f2020-02-24 09:23:26 +0800244 pdr_utils::PdrEntry e;
245 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
246 if (record == NULL)
247 {
248 return CmdHandler::ccOnlyResponse(
249 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
250 }
251
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530252 if (reqSizeBytes)
253 {
George Liue53193f2020-02-24 09:23:26 +0800254 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530255 if (respSizeBytes > reqSizeBytes)
256 {
257 respSizeBytes = reqSizeBytes;
258 }
George Liue53193f2020-02-24 09:23:26 +0800259 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530260 }
261 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
262 respSizeBytes,
263 0);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530264 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500265 rc = encode_get_pdr_resp(
266 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
267 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800268 if (rc != PLDM_SUCCESS)
269 {
270 return ccOnlyResponse(request, rc);
271 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530272 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530273 catch (const std::exception& e)
274 {
Riya Dixit89644442024-03-31 05:39:59 -0500275 error(
276 "Failed to access PDR record handle '{RECORD_HANDLE}', error - {ERROR}",
277 "RECORD_HANDLE", recordHandle, "ERROR", e);
George Liufb8611d2019-12-06 10:14:15 +0800278 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530279 }
280 return response;
281}
282
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600283Response Handler::setStateEffecterStates(const pldm_msg* request,
284 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500285{
286 Response response(
287 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
288 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
289 uint16_t effecterId;
290 uint8_t compEffecterCnt;
291 constexpr auto maxCompositeEffecterCnt = 8;
292 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
293 {0, 0});
294
295 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
296 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
297 sizeof(set_effecter_state_field)))
298 {
George Liufb8611d2019-12-06 10:14:15 +0800299 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500300 }
301
302 int rc = decode_set_state_effecter_states_req(request, payloadLength,
303 &effecterId, &compEffecterCnt,
304 stateField.data());
305
George Liufb8611d2019-12-06 10:14:15 +0800306 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500307 {
George Liufb8611d2019-12-06 10:14:15 +0800308 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500309 }
310
George Liufb8611d2019-12-06 10:14:15 +0800311 stateField.resize(compEffecterCnt);
312 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500313 uint16_t entityType{};
314 uint16_t entityInstance{};
315 uint16_t stateSetId{};
316
317 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
318 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530319 oemPlatformHandler != nullptr &&
320 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500321 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500322 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500323 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
324 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500325 }
326 else
327 {
328 rc = platform_state_effecter::setStateEffecterStatesHandler<
329 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
330 stateField);
331 }
George Liufb8611d2019-12-06 10:14:15 +0800332 if (rc != PLDM_SUCCESS)
333 {
334 return CmdHandler::ccOnlyResponse(request, rc);
335 }
336
337 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
338 responsePtr);
339 if (rc != PLDM_SUCCESS)
340 {
341 return ccOnlyResponse(request, rc);
342 }
343
Sampa Misraa2fa0702019-05-31 01:28:55 -0500344 return response;
345}
346
Tom Joseph56e45c52020-03-16 10:01:45 +0530347Response Handler::platformEventMessage(const pldm_msg* request,
348 size_t payloadLength)
349{
350 uint8_t formatVersion{};
351 uint8_t tid{};
352 uint8_t eventClass{};
353 size_t offset{};
354
355 auto rc = decode_platform_event_message_req(
356 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
357 if (rc != PLDM_SUCCESS)
358 {
359 return CmdHandler::ccOnlyResponse(request, rc);
360 }
361
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500362 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530363 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500364 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500365 if (oemPlatformHandler)
366 {
Sagar Srinivas18145f72022-04-11 07:38:26 -0500367 if (oemPlatformHandler->watchDogRunning())
368 {
369 oemPlatformHandler->resetWatchDogTimer();
370 }
371 else
372 {
373 oemPlatformHandler->setSurvTimer(tid, true);
374 }
Sagar Srinivas79669c92021-04-28 15:43:30 -0500375 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500376 }
377 else
378 {
379 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530380 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500381 const auto& handlers = eventHandlers.at(eventClass);
382 for (const auto& handler : handlers)
Tom Joseph56e45c52020-03-16 10:01:45 +0530383 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500384 auto rc = handler(request, payloadLength, formatVersion, tid,
385 offset);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500386 if (rc != PLDM_SUCCESS)
387 {
388 return CmdHandler::ccOnlyResponse(request, rc);
389 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530390 }
391 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500392 catch (const std::out_of_range& e)
393 {
Riya Dixit89644442024-03-31 05:39:59 -0500394 error("Failed to handle platform event msg, error - {ERROR}",
395 "ERROR", e);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500396 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
397 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530398 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530399 Response response(
400 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
401 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
402
403 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
404 PLDM_EVENT_NO_LOGGING, responsePtr);
405 if (rc != PLDM_SUCCESS)
406 {
407 return ccOnlyResponse(request, rc);
408 }
409
410 return response;
411}
412
413int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530414 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530415 size_t eventDataOffset)
416{
417 uint16_t sensorId{};
418 uint8_t eventClass{};
419 size_t eventClassDataOffset{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500420 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
421 eventDataOffset;
Tom Joseph56e45c52020-03-16 10:01:45 +0530422 auto eventDataSize = payloadLength - eventDataOffset;
423
424 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
425 &eventClass, &eventClassDataOffset);
426 if (rc != PLDM_SUCCESS)
427 {
428 return rc;
429 }
430
Zahed Hossain75330f32020-03-24 02:15:03 -0500431 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
432 eventDataOffset + eventClassDataOffset;
Patrick Williams6da4f912023-05-10 07:50:53 -0500433 auto eventClassDataSize = payloadLength - eventDataOffset -
434 eventClassDataOffset;
Zahed Hossain75330f32020-03-24 02:15:03 -0500435
Tom Joseph56e45c52020-03-16 10:01:45 +0530436 if (eventClass == PLDM_STATE_SENSOR_STATE)
437 {
438 uint8_t sensorOffset{};
439 uint8_t eventState{};
440 uint8_t previousEventState{};
441
Zahed Hossain75330f32020-03-24 02:15:03 -0500442 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530443 &sensorOffset, &eventState,
444 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500445 if (rc != PLDM_SUCCESS)
446 {
447 return PLDM_ERROR;
448 }
449
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800450 // Emitting state sensor event signal
451 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
452 previousEventState);
453
Tom Josephc4959c32020-04-20 19:50:16 +0530454 // If there are no HOST PDR's, there is no further action
455 if (hostPDRHandler == NULL)
456 {
457 return PLDM_SUCCESS;
458 }
459
460 // Handle PLDM events for which PDR is available
461 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530462
463 pldm::pdr::EntityInfo entityInfo{};
464 pldm::pdr::CompositeSensorStates compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600465 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Tom Josephb70a1962020-07-13 12:56:31 +0530466
Tom Josephc4959c32020-04-20 19:50:16 +0530467 try
468 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600469 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephc4959c32020-04-20 19:50:16 +0530470 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530471 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500472 catch (const std::out_of_range&)
Tom Josephc4959c32020-04-20 19:50:16 +0530473 {
Tom Josephb70a1962020-07-13 12:56:31 +0530474 // If there is no mapping for tid, sensorId combination, try
475 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
476 // implement TL PDR.
477 try
478 {
479 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600480 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephb70a1962020-07-13 12:56:31 +0530481 hostPDRHandler->lookupSensorInfo(sensorEntry);
482 }
483 // If there is no mapping for events return PLDM_SUCCESS
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500484 catch (const std::out_of_range&)
Tom Josephb70a1962020-07-13 12:56:31 +0530485 {
486 return PLDM_SUCCESS;
487 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500488 }
Tom Josephb70a1962020-07-13 12:56:31 +0530489
490 if (sensorOffset >= compositeSensorStates.size())
491 {
492 return PLDM_ERROR_INVALID_DATA;
493 }
494
495 const auto& possibleStates = compositeSensorStates[sensorOffset];
Sagar Srinivas06f9b292024-03-31 11:35:28 -0500496 if (!possibleStates.contains(eventState))
Tom Josephb70a1962020-07-13 12:56:31 +0530497 {
498 return PLDM_ERROR_INVALID_DATA;
499 }
500
501 const auto& [containerId, entityType, entityInstance] = entityInfo;
502 events::StateSensorEntry stateSensorEntry{containerId, entityType,
Sagar Srinivase3607a32024-02-16 03:50:53 -0600503 entityInstance, sensorOffset,
504 stateSetIds[sensorOffset]};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600505 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
506 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530507 }
508 else
509 {
510 return PLDM_ERROR_INVALID_DATA;
511 }
512
513 return PLDM_SUCCESS;
514}
515
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500516int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
517 size_t payloadLength,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530518 uint8_t /*formatVersion*/, uint8_t tid,
519 size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500520{
521 uint8_t eventDataFormat{};
522 uint8_t numberOfChangeRecords{};
523 size_t dataOffset{};
524
Patrick Williams6da4f912023-05-10 07:50:53 -0500525 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
526 eventDataOffset;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500527 auto eventDataSize = payloadLength - eventDataOffset;
528
529 auto rc = decode_pldm_pdr_repository_chg_event_data(
530 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
531 &dataOffset);
532 if (rc != PLDM_SUCCESS)
533 {
534 return rc;
535 }
536
537 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500538
539 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
540 {
541 return PLDM_ERROR_INVALID_DATA;
542 }
543
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500544 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
545 {
546 uint8_t eventDataOperation{};
547 uint8_t numberOfChangeEntries{};
548
549 auto changeRecordData = eventData + dataOffset;
550 auto changeRecordDataSize = eventDataSize - dataOffset;
551
552 while (changeRecordDataSize)
553 {
554 rc = decode_pldm_pdr_repository_change_record_data(
555 changeRecordData, changeRecordDataSize, &eventDataOperation,
556 &numberOfChangeEntries, &dataOffset);
557
558 if (rc != PLDM_SUCCESS)
559 {
560 return rc;
561 }
562
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500563 if (eventDataOperation == PLDM_RECORDS_ADDED ||
564 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500565 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500566 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
567 {
568 hostPDRHandler->isHostPdrModified = true;
569 }
570
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500571 rc = getPDRRecordHandles(
572 reinterpret_cast<const ChangeEntry*>(changeRecordData +
573 dataOffset),
574 changeRecordDataSize - dataOffset,
575 static_cast<size_t>(numberOfChangeEntries),
576 pdrRecordHandles);
577
578 if (rc != PLDM_SUCCESS)
579 {
580 return rc;
581 }
582 }
583
Patrick Williams6da4f912023-05-10 07:50:53 -0500584 changeRecordData += dataOffset +
585 (numberOfChangeEntries * sizeof(ChangeEntry));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500586 changeRecordDataSize -=
587 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
588 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500589 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500590 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500591 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530592 // if we get a Repository change event with the eventDataFormat
593 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
594 // have the matched Terminus handle
595 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
596 {
597 // We cannot get the Repo change event from the Terminus
598 // that is not already added to the BMC repository
599
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500600 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
601 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530602 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500603 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530604 {
605 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500606 it->first);
607 hostPDRHandler->tlPDRInfo.erase(it++);
608 }
609 else
610 {
611 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530612 }
613 }
614 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500615 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500616 }
617
618 return PLDM_SUCCESS;
619}
620
621int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
622 size_t changeEntryDataSize,
623 size_t numberOfChangeEntries,
624 PDRRecordHandles& pdrRecordHandles)
625{
626 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
627 {
628 return PLDM_ERROR_INVALID_DATA;
629 }
630 for (size_t i = 0; i < numberOfChangeEntries; i++)
631 {
632 pdrRecordHandles.push_back(changeEntryData[i]);
633 }
634 return PLDM_SUCCESS;
635}
636
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500637Response Handler::getNumericEffecterValue(const pldm_msg* request,
638 size_t payloadLength)
639{
640 if (payloadLength != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES)
641 {
642 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
643 }
644
645 uint16_t effecterId{};
646 auto rc = decode_get_numeric_effecter_value_req(request, payloadLength,
647 &effecterId);
648 if (rc != PLDM_SUCCESS)
649 {
650 return ccOnlyResponse(request, rc);
651 }
652
653 const pldm::utils::DBusHandler dBusIntf;
654 uint8_t effecterDataSize{};
655 pldm::utils::PropertyValue dbusValue;
656 std::string propertyType;
657 using effecterOperationalState = uint8_t;
658 using completionCode = uint8_t;
659
660 rc = platform_numeric_effecter::getNumericEffecterData<
661 pldm::utils::DBusHandler, Handler>(
662 dBusIntf, *this, effecterId, effecterDataSize, propertyType, dbusValue);
663
664 if (rc != PLDM_SUCCESS)
665 {
666 return ccOnlyResponse(request, rc);
667 }
668
669 // Refer DSP0248_1.2.0.pdf (section 22.3, Table 48)
670 // Completion Code (uint8), Effecter Data Size(uint8), Effecter Operational
671 // State(uint8), PendingValue (uint8|sint8|uint16|sint16|uint32|sint32 )
672 // PresentValue (uint8|sint8|uint16|sint16|uint32|sint32 )
673 // Size of PendingValue and PresentValue calculated based on size is
674 // provided in effecter data size
675 size_t responsePayloadLength = sizeof(completionCode) +
676 sizeof(effecterDataSize) +
677 sizeof(effecterOperationalState) +
678 getEffecterDataSize(effecterDataSize) +
679 getEffecterDataSize(effecterDataSize);
680
681 Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
682 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
683
684 rc = platform_numeric_effecter::getNumericEffecterValueHandler(
685 propertyType, dbusValue, effecterDataSize, responsePtr,
686 responsePayloadLength, request->hdr.instance_id);
687
688 if (rc != PLDM_SUCCESS)
689 {
690 error(
Riya Dixit89644442024-03-31 05:39:59 -0500691 "Failed to get response of GetNumericEffecterValue for effecter ID '{EFFECTERID}', response code '{RC}'.",
692 "EFFECTERID", effecterId, "RC", rc);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500693 return ccOnlyResponse(request, rc);
694 }
695 return response;
696}
697
George Liueccb0c52020-01-14 11:09:56 +0800698Response Handler::setNumericEffecterValue(const pldm_msg* request,
699 size_t payloadLength)
700{
701 Response response(sizeof(pldm_msg_hdr) +
702 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
703 uint16_t effecterId{};
704 uint8_t effecterDataSize{};
705 uint8_t effecterValue[4] = {};
706
707 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
708 sizeof(union_effecter_data_size)) ||
709 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
710 {
711 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
712 }
713
714 int rc = decode_set_numeric_effecter_value_req(
Andrew Jeffery8fbf3cc2023-04-12 13:42:29 +0930715 request, payloadLength, &effecterId, &effecterDataSize, effecterValue);
George Liueccb0c52020-01-14 11:09:56 +0800716
717 if (rc == PLDM_SUCCESS)
718 {
719 const pldm::utils::DBusHandler dBusIntf;
720 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
721 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
722 effecterDataSize, effecterValue,
723 sizeof(effecterValue));
724 }
725
726 return ccOnlyResponse(request, rc);
727}
728
Sampa Misra12afe112020-05-25 11:40:44 -0500729void Handler::generateTerminusLocatorPDR(Repo& repo)
730{
731 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
732
733 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
734
735 pdr->hdr.record_handle = 0;
736 pdr->hdr.version = 1;
737 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
738 pdr->hdr.record_change_num = 0;
739 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530740 pdr->terminus_handle = TERMINUS_HANDLE;
Sampa Misra12afe112020-05-25 11:40:44 -0500741 pdr->validity = PLDM_TL_PDR_VALID;
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530742 pdr->tid = TERMINUS_ID;
Sampa Misra12afe112020-05-25 11:40:44 -0500743 pdr->container_id = 0x0;
744 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
745 pdr->terminus_locator_value_size =
746 sizeof(pldm_terminus_locator_type_mctp_eid);
747 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
748 pdr->terminus_locator_value);
749 locatorValue->eid = BmcMctpEid;
750
751 PdrEntry pdrEntry{};
752 pdrEntry.data = pdrBuffer.data();
753 pdrEntry.size = pdrBuffer.size();
754 repo.addRecord(pdrEntry);
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530755 if (hostPDRHandler)
756 {
757 hostPDRHandler->tlPDRInfo.insert_or_assign(
758 pdr->terminus_handle,
759 std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
760 }
Sampa Misra12afe112020-05-25 11:40:44 -0500761}
George Liu362c18d2020-05-14 09:46:36 +0800762
763Response Handler::getStateSensorReadings(const pldm_msg* request,
764 size_t payloadLength)
765{
766 uint16_t sensorId{};
767 bitfield8_t sensorRearm{};
768 uint8_t reserved{};
769
Pavithra Barithaya87083f22023-04-17 01:27:49 -0500770 if (payloadLength != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES)
George Liu362c18d2020-05-14 09:46:36 +0800771 {
772 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
773 }
774
775 int rc = decode_get_state_sensor_readings_req(
776 request, payloadLength, &sensorId, &sensorRearm, &reserved);
777
778 if (rc != PLDM_SUCCESS)
779 {
780 return ccOnlyResponse(request, rc);
781 }
782
783 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800784 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500785 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800786 uint8_t comSensorCnt{};
787 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500788
789 uint16_t entityType{};
790 uint16_t entityInstance{};
791 uint16_t stateSetId{};
792
793 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
794 entityType, entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530795 oemPlatformHandler != nullptr && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500796 {
797 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
798 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
799 }
800 else
801 {
802 rc = platform_state_sensor::getStateSensorReadingsHandler<
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530803 pldm::utils::DBusHandler, Handler>(
804 dBusIntf, *this, sensorId, sensorRearmCount, comSensorCnt,
805 stateField, dbusToPLDMEventHandler->getSensorCache());
Sampa Misraaea5dde2020-08-31 08:33:47 -0500806 }
George Liu362c18d2020-05-14 09:46:36 +0800807
808 if (rc != PLDM_SUCCESS)
809 {
810 return ccOnlyResponse(request, rc);
811 }
812
813 Response response(sizeof(pldm_msg_hdr) +
814 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
815 sizeof(get_sensor_state_field) * comSensorCnt);
816 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
817 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
818 comSensorCnt, stateField.data(),
819 responsePtr);
820 if (rc != PLDM_SUCCESS)
821 {
822 return ccOnlyResponse(request, rc);
823 }
824
825 return response;
826}
827
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500828void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
829 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600830{
831 deferredGetPDREvent.reset();
832 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
833}
834
Sampa Misraaea5dde2020-08-31 08:33:47 -0500835bool isOemStateSensor(Handler& handler, uint16_t sensorId,
836 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
837 uint16_t& entityType, uint16_t& entityInstance,
838 uint16_t& stateSetId)
839{
840 pldm_state_sensor_pdr* pdr = nullptr;
841
842 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
843 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930844 if (!stateSensorPdrRepo)
845 {
846 error("Failed to instantiate state sensor PDR repository");
847 return false;
848 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500849 Repo stateSensorPDRs(stateSensorPdrRepo.get());
850 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
851 if (stateSensorPDRs.empty())
852 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600853 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500854 return false;
855 }
856
857 PdrEntry pdrEntry{};
858 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
859 while (pdrRecord)
860 {
861 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
862 assert(pdr != NULL);
863 if (pdr->sensor_id != sensorId)
864 {
865 pdr = nullptr;
866 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
867 continue;
868 }
869 auto tmpEntityType = pdr->entity_type;
870 auto tmpEntityInstance = pdr->entity_instance;
871 auto tmpCompSensorCnt = pdr->composite_sensor_count;
872 auto tmpPossibleStates =
873 reinterpret_cast<state_sensor_possible_states*>(
874 pdr->possible_states);
875 auto tmpStateSetId = tmpPossibleStates->state_set_id;
876
877 if (sensorRearmCount > tmpCompSensorCnt)
878 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600879 error(
Riya Dixit89644442024-03-31 05:39:59 -0500880 "The requester sent wrong sensor rearm count '{SENSOR_REARM_COUNT}' for the sensor ID '{SENSORID}'.",
881 "SENSOR_REARM_COUNT", (uint16_t)sensorRearmCount, "SENSORID",
882 sensorId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500883 break;
884 }
885
886 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
887 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
888 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
889 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
890 {
891 entityType = tmpEntityType;
892 entityInstance = tmpEntityInstance;
893 stateSetId = tmpStateSetId;
894 compSensorCnt = tmpCompSensorCnt;
895 return true;
896 }
897 else
898 {
899 return false;
900 }
901 }
902 return false;
903}
904
905bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
906 uint8_t compEffecterCnt, uint16_t& entityType,
907 uint16_t& entityInstance, uint16_t& stateSetId)
908{
909 pldm_state_effecter_pdr* pdr = nullptr;
910
911 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
912 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930913 if (!stateEffecterPdrRepo)
914 {
915 error("Failed to instantiate state effecter PDR repository");
916 return false;
917 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500918 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
919 getRepoByType(handler.getRepo(), stateEffecterPDRs,
920 PLDM_STATE_EFFECTER_PDR);
921 if (stateEffecterPDRs.empty())
922 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600923 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500924 return false;
925 }
926
927 PdrEntry pdrEntry{};
928 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
929 while (pdrRecord)
930 {
931 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
932 assert(pdr != NULL);
933 if (pdr->effecter_id != effecterId)
934 {
935 pdr = nullptr;
936 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
937 continue;
938 }
939
940 auto tmpEntityType = pdr->entity_type;
941 auto tmpEntityInstance = pdr->entity_instance;
942 auto tmpPossibleStates =
943 reinterpret_cast<state_effecter_possible_states*>(
944 pdr->possible_states);
945 auto tmpStateSetId = tmpPossibleStates->state_set_id;
946
947 if (compEffecterCnt > pdr->composite_effecter_count)
948 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600949 error(
Riya Dixit89644442024-03-31 05:39:59 -0500950 "The requester sent wrong composite effecter count '{COMPOSITE_EFFECTER_COUNT}' for the effecter ID '{EFFECTERID}'.",
951 "COMPOSITE_EFFECTER_COUNT", (uint16_t)compEffecterCnt,
952 "EFFECTERID", effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500953 return false;
954 }
955
956 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
957 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
958 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
959 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
960 {
961 entityType = tmpEntityType;
962 entityInstance = tmpEntityInstance;
963 stateSetId = tmpStateSetId;
964 return true;
965 }
966 else
967 {
968 return false;
969 }
970 }
971 return false;
972}
973
Sagar Srinivas90314a32023-10-17 10:38:03 -0500974void Handler::setEventReceiver()
975{
976 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
977 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
978 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
979 auto instanceId = instanceIdDb->next(eid);
980 uint8_t eventMessageGlobalEnable =
981 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
982 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
983 uint8_t eventReceiverAddressInfo = pldm::responder::pdr::BmcMctpEid;
984 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
985
986 auto rc = encode_set_event_receiver_req(
987 instanceId, eventMessageGlobalEnable, transportProtocolType,
988 eventReceiverAddressInfo, heartbeatTimer, request);
989 if (rc != PLDM_SUCCESS)
990 {
991 instanceIdDb->free(eid, instanceId);
Riya Dixit89644442024-03-31 05:39:59 -0500992 error(
993 "Failed to encode set event receiver request, response code '{RC}'",
994 "RC", lg2::hex, rc);
Sagar Srinivas90314a32023-10-17 10:38:03 -0500995 return;
996 }
997
998 auto processSetEventReceiverResponse =
999 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
1000 if (response == nullptr || !respMsgLen)
1001 {
1002 error("Failed to receive response for setEventReceiver command");
1003 return;
1004 }
1005
1006 uint8_t completionCode{};
1007 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
1008 &completionCode);
1009 if (rc || completionCode)
1010 {
1011 error(
Riya Dixit89644442024-03-31 05:39:59 -05001012 "Failed to decode setEventReceiver command, response code '{RC}' and completion code '{CC}'",
Sagar Srinivas90314a32023-10-17 10:38:03 -05001013 "RC", rc, "CC", (unsigned)completionCode);
1014 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +05301015 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Sagar Srinivas90314a32023-10-17 10:38:03 -05001016 }
1017 };
1018 rc = handler->registerRequest(
1019 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
1020 std::move(requestMsg), std::move(processSetEventReceiverResponse));
1021
1022 if (rc != PLDM_SUCCESS)
1023 {
1024 error("Failed to send the setEventReceiver request");
1025 }
1026
1027 if (oemPlatformHandler)
1028 {
1029 oemPlatformHandler->countSetEventReceiver();
1030 oemPlatformHandler->checkAndDisableWatchDog();
1031 }
1032}
1033
Deepak Kodihallibc669f12019-11-28 08:52:07 -06001034} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301035} // namespace responder
1036} // namespace pldm