blob: e0870b2a5e64e52932df8e61000dfee4de815318 [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 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060073 info("checking if : {DIR} exists", "DIR", directory);
74 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 Williams6da4f912023-05-10 07:50:53 -050089 pdr_state_effecter::generateStateEffecterPDR<pldm::utils::DBusHandler,
90 Handler>(dBusIntf, json,
91 *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050092 }},
93 {PLDM_NUMERIC_EFFECTER_PDR,
94 [this](const DBusHandler& dBusIntf, const auto& json,
95 RepoInterface& repo) {
Patrick Williams6da4f912023-05-10 07:50:53 -050096 pdr_numeric_effecter::generateNumericEffecterPDR<
97 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050098 }},
George Liuadbe1722020-05-09 19:20:19 +080099 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
100 const auto& json, RepoInterface& repo) {
Patrick Williamsa6756622023-10-20 11:19:15 -0500101 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
102 Handler>(dBusIntf, json, *this,
103 repo);
104 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600105
106 Type pdrType{};
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600107 for (const auto& directory : dir)
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600108 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600109 for (const auto& dirEntry : fs::directory_iterator(directory))
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600110 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600111 try
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600112 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600113 if (fs::is_regular_file(dirEntry.path().string()))
George Liu1ec85d42020-02-12 16:05:32 +0800114 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600115 auto json = readJson(dirEntry.path().string());
116 if (!json.empty())
117 {
118 auto effecterPDRs = json.value("effecterPDRs", empty);
119 for (const auto& effecter : effecterPDRs)
120 {
121 pdrType = effecter.value("pdrType", 0);
122 generateHandlers.at(pdrType)(dBusIntf, effecter,
123 repo);
124 }
George Liuadbe1722020-05-09 19:20:19 +0800125
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600126 auto sensorPDRs = json.value("sensorPDRs", empty);
127 for (const auto& sensor : sensorPDRs)
128 {
129 pdrType = sensor.value("pdrType", 0);
130 generateHandlers.at(pdrType)(dBusIntf, sensor,
131 repo);
132 }
133 }
George Liuadbe1722020-05-09 19:20:19 +0800134 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600135 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600136 catch (const InternalFailure& e)
137 {
138 error(
139 "PDR config directory '{PATH}' does not exist or empty for '{TYPE}' pdr: {ERROR}",
140 "TYPE", pdrType, "PATH", dirEntry.path(), "ERROR", e);
141 }
142 catch (const Json::exception& e)
143 {
144 error("Failed parsing PDR JSON file for '{TYPE}' pdr: {ERROR}",
145 "TYPE", pdrType, "ERROR", e);
146 pldm::utils::reportError(
147 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
148 }
149 catch (const std::exception& e)
150 {
151 error("Failed parsing PDR JSON file for '{TYPE}' pdr: {ERROR}",
152 "TYPE", pdrType, "ERROR", e);
153 pldm::utils::reportError(
154 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
155 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600156 }
157 }
158}
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600159Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530160{
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500161 if (hostPDRHandler)
162 {
163 if (hostPDRHandler->isHostUp() && oemPlatformHandler != nullptr)
164 {
165 auto rc = oemPlatformHandler->checkBMCState();
166 if (rc != PLDM_SUCCESS)
167 {
168 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
169 }
170 }
171 }
172
173 // Build FRU table if not built, since entity association PDR's
174 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530175 if (fruHandler)
176 {
177 fruHandler->buildFRUTable();
178 }
179
George Liud680ae02020-07-17 09:11:14 +0800180 if (!pdrCreated)
181 {
182 generateTerminusLocatorPDR(pdrRepo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600183 if (platformConfigHandler)
184 {
185 auto systemType = platformConfigHandler->getPlatformName();
186 if (systemType.has_value())
187 {
188 // In case of normal poweron , the system type would have been
189 // already filled by entity manager when ever BMC reaches Ready
190 // state. If this is not filled by time we get a getpdr request
191 // we can assume that the entity manager service is not present
192 // on this system & continue to build the common PDR's.
193 pdrJsonsDir.push_back(pdrJsonDir / systemType.value());
194 }
195 }
196
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500197 if (oemPlatformHandler != nullptr)
198 {
199 oemPlatformHandler->buildOEMPDR(pdrRepo);
200 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600201 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500202
George Liud680ae02020-07-17 09:11:14 +0800203 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800204
205 if (dbusToPLDMEventHandler)
206 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600207 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
208 event,
209 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
210 _processPostGetPDRActions),
211 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800212 }
George Liud680ae02020-07-17 09:11:14 +0800213 }
214
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530215 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530216
217 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
218 {
George Liufb8611d2019-12-06 10:14:15 +0800219 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530220 }
221
222 uint32_t recordHandle{};
223 uint32_t dataTransferHandle{};
224 uint8_t transferOpFlag{};
225 uint16_t reqSizeBytes{};
226 uint16_t recordChangeNum{};
227
George Liufb8611d2019-12-06 10:14:15 +0800228 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
229 &dataTransferHandle, &transferOpFlag,
230 &reqSizeBytes, &recordChangeNum);
231 if (rc != PLDM_SUCCESS)
232 {
233 return CmdHandler::ccOnlyResponse(request, rc);
234 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530235
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530236 uint16_t respSizeBytes{};
237 uint8_t* recordData = nullptr;
238 try
239 {
George Liue53193f2020-02-24 09:23:26 +0800240 pdr_utils::PdrEntry e;
241 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
242 if (record == NULL)
243 {
244 return CmdHandler::ccOnlyResponse(
245 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
246 }
247
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530248 if (reqSizeBytes)
249 {
George Liue53193f2020-02-24 09:23:26 +0800250 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530251 if (respSizeBytes > reqSizeBytes)
252 {
253 respSizeBytes = reqSizeBytes;
254 }
George Liue53193f2020-02-24 09:23:26 +0800255 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530256 }
257 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
258 respSizeBytes,
259 0);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530260 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500261 rc = encode_get_pdr_resp(
262 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
263 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800264 if (rc != PLDM_SUCCESS)
265 {
266 return ccOnlyResponse(request, rc);
267 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530268 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530269 catch (const std::exception& e)
270 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600271 error("Error accessing PDR, HANDLE={REC_HANDLE} ERROR={ERR_EXCEP}",
272 "REC_HANDLE", recordHandle, "ERR_EXCEP", e.what());
George Liufb8611d2019-12-06 10:14:15 +0800273 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530274 }
275 return response;
276}
277
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600278Response Handler::setStateEffecterStates(const pldm_msg* request,
279 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500280{
281 Response response(
282 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
283 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
284 uint16_t effecterId;
285 uint8_t compEffecterCnt;
286 constexpr auto maxCompositeEffecterCnt = 8;
287 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
288 {0, 0});
289
290 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
291 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
292 sizeof(set_effecter_state_field)))
293 {
George Liufb8611d2019-12-06 10:14:15 +0800294 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500295 }
296
297 int rc = decode_set_state_effecter_states_req(request, payloadLength,
298 &effecterId, &compEffecterCnt,
299 stateField.data());
300
George Liufb8611d2019-12-06 10:14:15 +0800301 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500302 {
George Liufb8611d2019-12-06 10:14:15 +0800303 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500304 }
305
George Liufb8611d2019-12-06 10:14:15 +0800306 stateField.resize(compEffecterCnt);
307 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500308 uint16_t entityType{};
309 uint16_t entityInstance{};
310 uint16_t stateSetId{};
311
312 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
313 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530314 oemPlatformHandler != nullptr &&
315 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500316 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500317 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500318 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
319 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500320 }
321 else
322 {
323 rc = platform_state_effecter::setStateEffecterStatesHandler<
324 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
325 stateField);
326 }
George Liufb8611d2019-12-06 10:14:15 +0800327 if (rc != PLDM_SUCCESS)
328 {
329 return CmdHandler::ccOnlyResponse(request, rc);
330 }
331
332 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
333 responsePtr);
334 if (rc != PLDM_SUCCESS)
335 {
336 return ccOnlyResponse(request, rc);
337 }
338
Sampa Misraa2fa0702019-05-31 01:28:55 -0500339 return response;
340}
341
Tom Joseph56e45c52020-03-16 10:01:45 +0530342Response Handler::platformEventMessage(const pldm_msg* request,
343 size_t payloadLength)
344{
345 uint8_t formatVersion{};
346 uint8_t tid{};
347 uint8_t eventClass{};
348 size_t offset{};
349
350 auto rc = decode_platform_event_message_req(
351 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
352 if (rc != PLDM_SUCCESS)
353 {
354 return CmdHandler::ccOnlyResponse(request, rc);
355 }
356
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500357 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530358 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500359 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500360 if (oemPlatformHandler)
361 {
362 oemPlatformHandler->resetWatchDogTimer();
363 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500364 }
365 else
366 {
367 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530368 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500369 const auto& handlers = eventHandlers.at(eventClass);
370 for (const auto& handler : handlers)
Tom Joseph56e45c52020-03-16 10:01:45 +0530371 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500372 auto rc = handler(request, payloadLength, formatVersion, tid,
373 offset);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500374 if (rc != PLDM_SUCCESS)
375 {
376 return CmdHandler::ccOnlyResponse(request, rc);
377 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530378 }
379 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500380 catch (const std::out_of_range& e)
381 {
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500382 error("Error in handling platform event msg: {ERROR}", "ERROR", e);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500383 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
384 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530385 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530386 Response response(
387 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
388 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
389
390 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
391 PLDM_EVENT_NO_LOGGING, responsePtr);
392 if (rc != PLDM_SUCCESS)
393 {
394 return ccOnlyResponse(request, rc);
395 }
396
397 return response;
398}
399
400int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530401 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530402 size_t eventDataOffset)
403{
404 uint16_t sensorId{};
405 uint8_t eventClass{};
406 size_t eventClassDataOffset{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500407 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
408 eventDataOffset;
Tom Joseph56e45c52020-03-16 10:01:45 +0530409 auto eventDataSize = payloadLength - eventDataOffset;
410
411 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
412 &eventClass, &eventClassDataOffset);
413 if (rc != PLDM_SUCCESS)
414 {
415 return rc;
416 }
417
Zahed Hossain75330f32020-03-24 02:15:03 -0500418 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
419 eventDataOffset + eventClassDataOffset;
Patrick Williams6da4f912023-05-10 07:50:53 -0500420 auto eventClassDataSize = payloadLength - eventDataOffset -
421 eventClassDataOffset;
Zahed Hossain75330f32020-03-24 02:15:03 -0500422
Tom Joseph56e45c52020-03-16 10:01:45 +0530423 if (eventClass == PLDM_STATE_SENSOR_STATE)
424 {
425 uint8_t sensorOffset{};
426 uint8_t eventState{};
427 uint8_t previousEventState{};
428
Zahed Hossain75330f32020-03-24 02:15:03 -0500429 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530430 &sensorOffset, &eventState,
431 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500432 if (rc != PLDM_SUCCESS)
433 {
434 return PLDM_ERROR;
435 }
436
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800437 // Emitting state sensor event signal
438 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
439 previousEventState);
440
Tom Josephc4959c32020-04-20 19:50:16 +0530441 // If there are no HOST PDR's, there is no further action
442 if (hostPDRHandler == NULL)
443 {
444 return PLDM_SUCCESS;
445 }
446
447 // Handle PLDM events for which PDR is available
448 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530449
450 pldm::pdr::EntityInfo entityInfo{};
451 pldm::pdr::CompositeSensorStates compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600452 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Tom Josephb70a1962020-07-13 12:56:31 +0530453
Tom Josephc4959c32020-04-20 19:50:16 +0530454 try
455 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600456 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephc4959c32020-04-20 19:50:16 +0530457 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530458 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500459 catch (const std::out_of_range&)
Tom Josephc4959c32020-04-20 19:50:16 +0530460 {
Tom Josephb70a1962020-07-13 12:56:31 +0530461 // If there is no mapping for tid, sensorId combination, try
462 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
463 // implement TL PDR.
464 try
465 {
466 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600467 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephb70a1962020-07-13 12:56:31 +0530468 hostPDRHandler->lookupSensorInfo(sensorEntry);
469 }
470 // If there is no mapping for events return PLDM_SUCCESS
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500471 catch (const std::out_of_range&)
Tom Josephb70a1962020-07-13 12:56:31 +0530472 {
473 return PLDM_SUCCESS;
474 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500475 }
Tom Josephb70a1962020-07-13 12:56:31 +0530476
477 if (sensorOffset >= compositeSensorStates.size())
478 {
479 return PLDM_ERROR_INVALID_DATA;
480 }
481
482 const auto& possibleStates = compositeSensorStates[sensorOffset];
Sagar Srinivas06f9b292024-03-31 11:35:28 -0500483 if (!possibleStates.contains(eventState))
Tom Josephb70a1962020-07-13 12:56:31 +0530484 {
485 return PLDM_ERROR_INVALID_DATA;
486 }
487
488 const auto& [containerId, entityType, entityInstance] = entityInfo;
489 events::StateSensorEntry stateSensorEntry{containerId, entityType,
Sagar Srinivase3607a32024-02-16 03:50:53 -0600490 entityInstance, sensorOffset,
491 stateSetIds[sensorOffset]};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600492 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
493 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530494 }
495 else
496 {
497 return PLDM_ERROR_INVALID_DATA;
498 }
499
500 return PLDM_SUCCESS;
501}
502
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500503int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
504 size_t payloadLength,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530505 uint8_t /*formatVersion*/, uint8_t tid,
506 size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500507{
508 uint8_t eventDataFormat{};
509 uint8_t numberOfChangeRecords{};
510 size_t dataOffset{};
511
Patrick Williams6da4f912023-05-10 07:50:53 -0500512 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
513 eventDataOffset;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500514 auto eventDataSize = payloadLength - eventDataOffset;
515
516 auto rc = decode_pldm_pdr_repository_chg_event_data(
517 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
518 &dataOffset);
519 if (rc != PLDM_SUCCESS)
520 {
521 return rc;
522 }
523
524 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500525
526 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
527 {
528 return PLDM_ERROR_INVALID_DATA;
529 }
530
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500531 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
532 {
533 uint8_t eventDataOperation{};
534 uint8_t numberOfChangeEntries{};
535
536 auto changeRecordData = eventData + dataOffset;
537 auto changeRecordDataSize = eventDataSize - dataOffset;
538
539 while (changeRecordDataSize)
540 {
541 rc = decode_pldm_pdr_repository_change_record_data(
542 changeRecordData, changeRecordDataSize, &eventDataOperation,
543 &numberOfChangeEntries, &dataOffset);
544
545 if (rc != PLDM_SUCCESS)
546 {
547 return rc;
548 }
549
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500550 if (eventDataOperation == PLDM_RECORDS_ADDED ||
551 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500552 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500553 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
554 {
555 hostPDRHandler->isHostPdrModified = true;
556 }
557
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500558 rc = getPDRRecordHandles(
559 reinterpret_cast<const ChangeEntry*>(changeRecordData +
560 dataOffset),
561 changeRecordDataSize - dataOffset,
562 static_cast<size_t>(numberOfChangeEntries),
563 pdrRecordHandles);
564
565 if (rc != PLDM_SUCCESS)
566 {
567 return rc;
568 }
569 }
570
Patrick Williams6da4f912023-05-10 07:50:53 -0500571 changeRecordData += dataOffset +
572 (numberOfChangeEntries * sizeof(ChangeEntry));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500573 changeRecordDataSize -=
574 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
575 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500576 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500577 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500578 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530579 // if we get a Repository change event with the eventDataFormat
580 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
581 // have the matched Terminus handle
582 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
583 {
584 // We cannot get the Repo change event from the Terminus
585 // that is not already added to the BMC repository
586
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500587 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
588 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530589 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500590 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530591 {
592 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500593 it->first);
594 hostPDRHandler->tlPDRInfo.erase(it++);
595 }
596 else
597 {
598 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530599 }
600 }
601 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500602 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500603 }
604
605 return PLDM_SUCCESS;
606}
607
608int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
609 size_t changeEntryDataSize,
610 size_t numberOfChangeEntries,
611 PDRRecordHandles& pdrRecordHandles)
612{
613 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
614 {
615 return PLDM_ERROR_INVALID_DATA;
616 }
617 for (size_t i = 0; i < numberOfChangeEntries; i++)
618 {
619 pdrRecordHandles.push_back(changeEntryData[i]);
620 }
621 return PLDM_SUCCESS;
622}
623
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500624Response Handler::getNumericEffecterValue(const pldm_msg* request,
625 size_t payloadLength)
626{
627 if (payloadLength != PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES)
628 {
629 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
630 }
631
632 uint16_t effecterId{};
633 auto rc = decode_get_numeric_effecter_value_req(request, payloadLength,
634 &effecterId);
635 if (rc != PLDM_SUCCESS)
636 {
637 return ccOnlyResponse(request, rc);
638 }
639
640 const pldm::utils::DBusHandler dBusIntf;
641 uint8_t effecterDataSize{};
642 pldm::utils::PropertyValue dbusValue;
643 std::string propertyType;
644 using effecterOperationalState = uint8_t;
645 using completionCode = uint8_t;
646
647 rc = platform_numeric_effecter::getNumericEffecterData<
648 pldm::utils::DBusHandler, Handler>(
649 dBusIntf, *this, effecterId, effecterDataSize, propertyType, dbusValue);
650
651 if (rc != PLDM_SUCCESS)
652 {
653 return ccOnlyResponse(request, rc);
654 }
655
656 // Refer DSP0248_1.2.0.pdf (section 22.3, Table 48)
657 // Completion Code (uint8), Effecter Data Size(uint8), Effecter Operational
658 // State(uint8), PendingValue (uint8|sint8|uint16|sint16|uint32|sint32 )
659 // PresentValue (uint8|sint8|uint16|sint16|uint32|sint32 )
660 // Size of PendingValue and PresentValue calculated based on size is
661 // provided in effecter data size
662 size_t responsePayloadLength = sizeof(completionCode) +
663 sizeof(effecterDataSize) +
664 sizeof(effecterOperationalState) +
665 getEffecterDataSize(effecterDataSize) +
666 getEffecterDataSize(effecterDataSize);
667
668 Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
669 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
670
671 rc = platform_numeric_effecter::getNumericEffecterValueHandler(
672 propertyType, dbusValue, effecterDataSize, responsePtr,
673 responsePayloadLength, request->hdr.instance_id);
674
675 if (rc != PLDM_SUCCESS)
676 {
677 error(
678 "Reponse to GetNumericEffecterValue failed RC={RC} for EffectorId={EFFECTER_ID} ",
679 "RC", rc, "EFFECTER_ID", effecterId);
680 return ccOnlyResponse(request, rc);
681 }
682 return response;
683}
684
George Liueccb0c52020-01-14 11:09:56 +0800685Response Handler::setNumericEffecterValue(const pldm_msg* request,
686 size_t payloadLength)
687{
688 Response response(sizeof(pldm_msg_hdr) +
689 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
690 uint16_t effecterId{};
691 uint8_t effecterDataSize{};
692 uint8_t effecterValue[4] = {};
693
694 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
695 sizeof(union_effecter_data_size)) ||
696 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
697 {
698 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
699 }
700
701 int rc = decode_set_numeric_effecter_value_req(
Andrew Jeffery8fbf3cc2023-04-12 13:42:29 +0930702 request, payloadLength, &effecterId, &effecterDataSize, effecterValue);
George Liueccb0c52020-01-14 11:09:56 +0800703
704 if (rc == PLDM_SUCCESS)
705 {
706 const pldm::utils::DBusHandler dBusIntf;
707 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
708 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
709 effecterDataSize, effecterValue,
710 sizeof(effecterValue));
711 }
712
713 return ccOnlyResponse(request, rc);
714}
715
Sampa Misra12afe112020-05-25 11:40:44 -0500716void Handler::generateTerminusLocatorPDR(Repo& repo)
717{
718 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
719
720 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
721
722 pdr->hdr.record_handle = 0;
723 pdr->hdr.version = 1;
724 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
725 pdr->hdr.record_change_num = 0;
726 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530727 pdr->terminus_handle = TERMINUS_HANDLE;
Sampa Misra12afe112020-05-25 11:40:44 -0500728 pdr->validity = PLDM_TL_PDR_VALID;
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530729 pdr->tid = TERMINUS_ID;
Sampa Misra12afe112020-05-25 11:40:44 -0500730 pdr->container_id = 0x0;
731 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
732 pdr->terminus_locator_value_size =
733 sizeof(pldm_terminus_locator_type_mctp_eid);
734 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
735 pdr->terminus_locator_value);
736 locatorValue->eid = BmcMctpEid;
737
738 PdrEntry pdrEntry{};
739 pdrEntry.data = pdrBuffer.data();
740 pdrEntry.size = pdrBuffer.size();
741 repo.addRecord(pdrEntry);
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530742 if (hostPDRHandler)
743 {
744 hostPDRHandler->tlPDRInfo.insert_or_assign(
745 pdr->terminus_handle,
746 std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
747 }
Sampa Misra12afe112020-05-25 11:40:44 -0500748}
George Liu362c18d2020-05-14 09:46:36 +0800749
750Response Handler::getStateSensorReadings(const pldm_msg* request,
751 size_t payloadLength)
752{
753 uint16_t sensorId{};
754 bitfield8_t sensorRearm{};
755 uint8_t reserved{};
756
Pavithra Barithaya87083f22023-04-17 01:27:49 -0500757 if (payloadLength != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES)
George Liu362c18d2020-05-14 09:46:36 +0800758 {
759 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
760 }
761
762 int rc = decode_get_state_sensor_readings_req(
763 request, payloadLength, &sensorId, &sensorRearm, &reserved);
764
765 if (rc != PLDM_SUCCESS)
766 {
767 return ccOnlyResponse(request, rc);
768 }
769
770 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800771 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500772 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800773 uint8_t comSensorCnt{};
774 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500775
776 uint16_t entityType{};
777 uint16_t entityInstance{};
778 uint16_t stateSetId{};
779
780 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
781 entityType, entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530782 oemPlatformHandler != nullptr && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500783 {
784 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
785 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
786 }
787 else
788 {
789 rc = platform_state_sensor::getStateSensorReadingsHandler<
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530790 pldm::utils::DBusHandler, Handler>(
791 dBusIntf, *this, sensorId, sensorRearmCount, comSensorCnt,
792 stateField, dbusToPLDMEventHandler->getSensorCache());
Sampa Misraaea5dde2020-08-31 08:33:47 -0500793 }
George Liu362c18d2020-05-14 09:46:36 +0800794
795 if (rc != PLDM_SUCCESS)
796 {
797 return ccOnlyResponse(request, rc);
798 }
799
800 Response response(sizeof(pldm_msg_hdr) +
801 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
802 sizeof(get_sensor_state_field) * comSensorCnt);
803 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
804 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
805 comSensorCnt, stateField.data(),
806 responsePtr);
807 if (rc != PLDM_SUCCESS)
808 {
809 return ccOnlyResponse(request, rc);
810 }
811
812 return response;
813}
814
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500815void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
816 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600817{
818 deferredGetPDREvent.reset();
819 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
820}
821
Sampa Misraaea5dde2020-08-31 08:33:47 -0500822bool isOemStateSensor(Handler& handler, uint16_t sensorId,
823 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
824 uint16_t& entityType, uint16_t& entityInstance,
825 uint16_t& stateSetId)
826{
827 pldm_state_sensor_pdr* pdr = nullptr;
828
829 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
830 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930831 if (!stateSensorPdrRepo)
832 {
833 error("Failed to instantiate state sensor PDR repository");
834 return false;
835 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500836 Repo stateSensorPDRs(stateSensorPdrRepo.get());
837 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
838 if (stateSensorPDRs.empty())
839 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600840 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500841 return false;
842 }
843
844 PdrEntry pdrEntry{};
845 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
846 while (pdrRecord)
847 {
848 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
849 assert(pdr != NULL);
850 if (pdr->sensor_id != sensorId)
851 {
852 pdr = nullptr;
853 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
854 continue;
855 }
856 auto tmpEntityType = pdr->entity_type;
857 auto tmpEntityInstance = pdr->entity_instance;
858 auto tmpCompSensorCnt = pdr->composite_sensor_count;
859 auto tmpPossibleStates =
860 reinterpret_cast<state_sensor_possible_states*>(
861 pdr->possible_states);
862 auto tmpStateSetId = tmpPossibleStates->state_set_id;
863
864 if (sensorRearmCount > tmpCompSensorCnt)
865 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600866 error(
867 "The requester sent wrong sensorRearm count for the sensor, SENSOR_ID={SENSOR_ID} SENSOR_REARM_COUNT={SENSOR_REARM_CNT}",
868 "SENSOR_ID", sensorId, "SENSOR_REARM_CNT",
869 (uint16_t)sensorRearmCount);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500870 break;
871 }
872
873 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
874 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
875 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
876 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
877 {
878 entityType = tmpEntityType;
879 entityInstance = tmpEntityInstance;
880 stateSetId = tmpStateSetId;
881 compSensorCnt = tmpCompSensorCnt;
882 return true;
883 }
884 else
885 {
886 return false;
887 }
888 }
889 return false;
890}
891
892bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
893 uint8_t compEffecterCnt, uint16_t& entityType,
894 uint16_t& entityInstance, uint16_t& stateSetId)
895{
896 pldm_state_effecter_pdr* pdr = nullptr;
897
898 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
899 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930900 if (!stateEffecterPdrRepo)
901 {
902 error("Failed to instantiate state effecter PDR repository");
903 return false;
904 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500905 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
906 getRepoByType(handler.getRepo(), stateEffecterPDRs,
907 PLDM_STATE_EFFECTER_PDR);
908 if (stateEffecterPDRs.empty())
909 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600910 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500911 return false;
912 }
913
914 PdrEntry pdrEntry{};
915 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
916 while (pdrRecord)
917 {
918 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
919 assert(pdr != NULL);
920 if (pdr->effecter_id != effecterId)
921 {
922 pdr = nullptr;
923 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
924 continue;
925 }
926
927 auto tmpEntityType = pdr->entity_type;
928 auto tmpEntityInstance = pdr->entity_instance;
929 auto tmpPossibleStates =
930 reinterpret_cast<state_effecter_possible_states*>(
931 pdr->possible_states);
932 auto tmpStateSetId = tmpPossibleStates->state_set_id;
933
934 if (compEffecterCnt > pdr->composite_effecter_count)
935 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600936 error(
937 "The requester sent wrong composite effecter count for the effecter, EFFECTER_ID={EFFECTER_ID} COMP_EFF_CNT={COMP_EFF_CNT}",
938 "EFFECTER_ID", effecterId, "COMP_EFF_CNT",
939 (uint16_t)compEffecterCnt);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500940 return false;
941 }
942
943 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
944 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
945 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
946 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
947 {
948 entityType = tmpEntityType;
949 entityInstance = tmpEntityInstance;
950 stateSetId = tmpStateSetId;
951 return true;
952 }
953 else
954 {
955 return false;
956 }
957 }
958 return false;
959}
960
Sagar Srinivas90314a32023-10-17 10:38:03 -0500961void Handler::setEventReceiver()
962{
963 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
964 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
965 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
966 auto instanceId = instanceIdDb->next(eid);
967 uint8_t eventMessageGlobalEnable =
968 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
969 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
970 uint8_t eventReceiverAddressInfo = pldm::responder::pdr::BmcMctpEid;
971 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
972
973 auto rc = encode_set_event_receiver_req(
974 instanceId, eventMessageGlobalEnable, transportProtocolType,
975 eventReceiverAddressInfo, heartbeatTimer, request);
976 if (rc != PLDM_SUCCESS)
977 {
978 instanceIdDb->free(eid, instanceId);
979 error("Failed to encode_set_event_receiver_req, rc = {RC}", "RC",
980 lg2::hex, rc);
981 return;
982 }
983
984 auto processSetEventReceiverResponse =
985 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
986 if (response == nullptr || !respMsgLen)
987 {
988 error("Failed to receive response for setEventReceiver command");
989 return;
990 }
991
992 uint8_t completionCode{};
993 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
994 &completionCode);
995 if (rc || completionCode)
996 {
997 error(
998 "Failed to decode setEventReceiver command response, rc = {RC}, cc = {CC}",
999 "RC", rc, "CC", (unsigned)completionCode);
1000 pldm::utils::reportError(
Pavithra Barithayad28f08c2021-12-15 03:37:14 -06001001 "xyz.openbmc_project.PLDM.Error.InternalFailure");
Sagar Srinivas90314a32023-10-17 10:38:03 -05001002 }
1003 };
1004 rc = handler->registerRequest(
1005 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
1006 std::move(requestMsg), std::move(processSetEventReceiverResponse));
1007
1008 if (rc != PLDM_SUCCESS)
1009 {
1010 error("Failed to send the setEventReceiver request");
1011 }
1012
1013 if (oemPlatformHandler)
1014 {
1015 oemPlatformHandler->countSetEventReceiver();
1016 oemPlatformHandler->checkAndDisableWatchDog();
1017 }
1018}
1019
Deepak Kodihallibc669f12019-11-28 08:52:07 -06001020} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301021} // namespace responder
1022} // namespace pldm