blob: d04bb1eee9e7abd10cfc1aac1f2c2fa25d929450 [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 Williams16c2a0a2024-08-16 15:20:59 -040090 pdr_state_effecter::generateStateEffecterPDR<
91 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
92 repo);
93 }},
Patrick Williamsa6756622023-10-20 11:19:15 -050094 {PLDM_NUMERIC_EFFECTER_PDR,
95 [this](const DBusHandler& dBusIntf, const auto& json,
96 RepoInterface& repo) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040097 pdr_numeric_effecter::generateNumericEffecterPDR<
98 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
99 repo);
100 }},
George Liuadbe1722020-05-09 19:20:19 +0800101 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
102 const auto& json, RepoInterface& repo) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400103 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
104 Handler>(dBusIntf, json,
105 *this, repo);
106 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600107
108 Type pdrType{};
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600109 for (const auto& directory : dir)
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600110 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600111 for (const auto& dirEntry : fs::directory_iterator(directory))
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600112 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600113 try
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600114 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600115 if (fs::is_regular_file(dirEntry.path().string()))
George Liu1ec85d42020-02-12 16:05:32 +0800116 {
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600117 auto json = readJson(dirEntry.path().string());
118 if (!json.empty())
119 {
120 auto effecterPDRs = json.value("effecterPDRs", empty);
121 for (const auto& effecter : effecterPDRs)
122 {
123 pdrType = effecter.value("pdrType", 0);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400124 generateHandlers.at(
125 pdrType)(dBusIntf, effecter, repo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600126 }
George Liuadbe1722020-05-09 19:20:19 +0800127
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600128 auto sensorPDRs = json.value("sensorPDRs", empty);
129 for (const auto& sensor : sensorPDRs)
130 {
131 pdrType = sensor.value("pdrType", 0);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400132 generateHandlers.at(
133 pdrType)(dBusIntf, sensor, repo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600134 }
135 }
George Liuadbe1722020-05-09 19:20:19 +0800136 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600137 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600138 catch (const InternalFailure& e)
139 {
140 error(
Riya Dixit89644442024-03-31 05:39:59 -0500141 "PDR config directory '{PATH}' does not exist or empty for '{TYPE}' pdr, error - {ERROR}",
142 "PATH", dirEntry.path(), "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600143 }
144 catch (const Json::exception& e)
145 {
Riya Dixit89644442024-03-31 05:39:59 -0500146 error(
147 "Failed to parse PDR JSON file for '{TYPE}' pdr, error - {ERROR}",
148 "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600149 pldm::utils::reportError(
150 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
151 }
152 catch (const std::exception& e)
153 {
Riya Dixit89644442024-03-31 05:39:59 -0500154 error(
155 "Failed to parse PDR JSON file for '{TYPE}' pdr, error - {ERROR}",
156 "TYPE", pdrType, "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600157 pldm::utils::reportError(
158 "xyz.openbmc_project.PLDM.Error.Generate.PDRJsonFileParseFail");
159 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600160 }
161 }
162}
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530163
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600164Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530165{
Manojkiran Edac5a11202024-07-03 12:12:57 +0530166 if (oemPlatformHandler)
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500167 {
Manojkiran Edac5a11202024-07-03 12:12:57 +0530168 auto rc = oemPlatformHandler->checkBMCState();
169 if (rc != PLDM_SUCCESS)
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500170 {
Manojkiran Edac5a11202024-07-03 12:12:57 +0530171 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500172 }
173 }
174
175 // Build FRU table if not built, since entity association PDR's
176 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530177 if (fruHandler)
178 {
179 fruHandler->buildFRUTable();
180 }
181
George Liud680ae02020-07-17 09:11:14 +0800182 if (!pdrCreated)
183 {
184 generateTerminusLocatorPDR(pdrRepo);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600185 if (platformConfigHandler)
186 {
187 auto systemType = platformConfigHandler->getPlatformName();
188 if (systemType.has_value())
189 {
190 // In case of normal poweron , the system type would have been
191 // already filled by entity manager when ever BMC reaches Ready
192 // state. If this is not filled by time we get a getpdr request
193 // we can assume that the entity manager service is not present
194 // on this system & continue to build the common PDR's.
195 pdrJsonsDir.push_back(pdrJsonDir / systemType.value());
196 }
197 }
198
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500199 if (oemPlatformHandler != nullptr)
200 {
201 oemPlatformHandler->buildOEMPDR(pdrRepo);
202 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600203 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500204
George Liud680ae02020-07-17 09:11:14 +0800205 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800206
207 if (dbusToPLDMEventHandler)
208 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600209 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
210 event,
211 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
212 _processPostGetPDRActions),
213 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800214 }
George Liud680ae02020-07-17 09:11:14 +0800215 }
216
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530217 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530218
219 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
220 {
George Liufb8611d2019-12-06 10:14:15 +0800221 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530222 }
223
224 uint32_t recordHandle{};
225 uint32_t dataTransferHandle{};
226 uint8_t transferOpFlag{};
227 uint16_t reqSizeBytes{};
228 uint16_t recordChangeNum{};
229
George Liufb8611d2019-12-06 10:14:15 +0800230 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
231 &dataTransferHandle, &transferOpFlag,
232 &reqSizeBytes, &recordChangeNum);
233 if (rc != PLDM_SUCCESS)
234 {
235 return CmdHandler::ccOnlyResponse(request, rc);
236 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530237
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530238 uint16_t respSizeBytes{};
239 uint8_t* recordData = nullptr;
240 try
241 {
George Liue53193f2020-02-24 09:23:26 +0800242 pdr_utils::PdrEntry e;
243 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
244 if (record == NULL)
245 {
246 return CmdHandler::ccOnlyResponse(
247 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
248 }
249
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530250 if (reqSizeBytes)
251 {
George Liue53193f2020-02-24 09:23:26 +0800252 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530253 if (respSizeBytes > reqSizeBytes)
254 {
255 respSizeBytes = reqSizeBytes;
256 }
George Liue53193f2020-02-24 09:23:26 +0800257 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530258 }
259 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
260 respSizeBytes,
261 0);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530262 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500263 rc = encode_get_pdr_resp(
264 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
265 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800266 if (rc != PLDM_SUCCESS)
267 {
268 return ccOnlyResponse(request, rc);
269 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530270 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530271 catch (const std::exception& e)
272 {
Riya Dixit89644442024-03-31 05:39:59 -0500273 error(
274 "Failed to access PDR record handle '{RECORD_HANDLE}', error - {ERROR}",
275 "RECORD_HANDLE", recordHandle, "ERROR", e);
George Liufb8611d2019-12-06 10:14:15 +0800276 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530277 }
278 return response;
279}
280
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600281Response Handler::setStateEffecterStates(const pldm_msg* request,
282 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500283{
284 Response response(
285 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
286 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
287 uint16_t effecterId;
288 uint8_t compEffecterCnt;
289 constexpr auto maxCompositeEffecterCnt = 8;
290 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
291 {0, 0});
292
293 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
294 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
295 sizeof(set_effecter_state_field)))
296 {
George Liufb8611d2019-12-06 10:14:15 +0800297 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500298 }
299
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400300 int rc = decode_set_state_effecter_states_req(
301 request, payloadLength, &effecterId, &compEffecterCnt,
302 stateField.data());
Sampa Misraa2fa0702019-05-31 01:28:55 -0500303
George Liufb8611d2019-12-06 10:14:15 +0800304 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500305 {
George Liufb8611d2019-12-06 10:14:15 +0800306 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500307 }
308
George Liufb8611d2019-12-06 10:14:15 +0800309 stateField.resize(compEffecterCnt);
310 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500311 uint16_t entityType{};
312 uint16_t entityInstance{};
313 uint16_t stateSetId{};
314
315 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
316 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530317 oemPlatformHandler != nullptr &&
318 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500319 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500320 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500321 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
322 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500323 }
324 else
325 {
326 rc = platform_state_effecter::setStateEffecterStatesHandler<
327 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
328 stateField);
329 }
George Liufb8611d2019-12-06 10:14:15 +0800330 if (rc != PLDM_SUCCESS)
331 {
332 return CmdHandler::ccOnlyResponse(request, rc);
333 }
334
335 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
336 responsePtr);
337 if (rc != PLDM_SUCCESS)
338 {
339 return ccOnlyResponse(request, rc);
340 }
341
Sampa Misraa2fa0702019-05-31 01:28:55 -0500342 return response;
343}
344
Tom Joseph56e45c52020-03-16 10:01:45 +0530345Response Handler::platformEventMessage(const pldm_msg* request,
346 size_t payloadLength)
347{
348 uint8_t formatVersion{};
349 uint8_t tid{};
350 uint8_t eventClass{};
351 size_t offset{};
352
353 auto rc = decode_platform_event_message_req(
354 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
355 if (rc != PLDM_SUCCESS)
356 {
357 return CmdHandler::ccOnlyResponse(request, rc);
358 }
359
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500360 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530361 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500362 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500363 if (oemPlatformHandler)
364 {
Sagar Srinivas18145f72022-04-11 07:38:26 -0500365 if (oemPlatformHandler->watchDogRunning())
366 {
367 oemPlatformHandler->resetWatchDogTimer();
368 }
369 else
370 {
371 oemPlatformHandler->setSurvTimer(tid, true);
372 }
Sagar Srinivas79669c92021-04-28 15:43:30 -0500373 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500374 }
375 else
376 {
377 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530378 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500379 const auto& handlers = eventHandlers.at(eventClass);
380 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 {
386 return CmdHandler::ccOnlyResponse(request, rc);
387 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530388 }
389 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500390 catch (const std::out_of_range& e)
391 {
Riya Dixit89644442024-03-31 05:39:59 -0500392 error("Failed to handle platform event msg, error - {ERROR}",
393 "ERROR", e);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500394 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
395 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530396 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530397 Response response(
398 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
399 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
400
401 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
402 PLDM_EVENT_NO_LOGGING, responsePtr);
403 if (rc != PLDM_SUCCESS)
404 {
405 return ccOnlyResponse(request, rc);
406 }
407
408 return response;
409}
410
411int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530412 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530413 size_t eventDataOffset)
414{
415 uint16_t sensorId{};
416 uint8_t eventClass{};
417 size_t eventClassDataOffset{};
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400418 auto eventData =
419 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
Tom Joseph56e45c52020-03-16 10:01:45 +0530420 auto eventDataSize = payloadLength - eventDataOffset;
421
422 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
423 &eventClass, &eventClassDataOffset);
424 if (rc != PLDM_SUCCESS)
425 {
426 return rc;
427 }
428
Zahed Hossain75330f32020-03-24 02:15:03 -0500429 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
430 eventDataOffset + eventClassDataOffset;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400431 auto eventClassDataSize =
432 payloadLength - eventDataOffset - eventClassDataOffset;
Zahed Hossain75330f32020-03-24 02:15:03 -0500433
Tom Joseph56e45c52020-03-16 10:01:45 +0530434 if (eventClass == PLDM_STATE_SENSOR_STATE)
435 {
436 uint8_t sensorOffset{};
437 uint8_t eventState{};
438 uint8_t previousEventState{};
439
Zahed Hossain75330f32020-03-24 02:15:03 -0500440 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530441 &sensorOffset, &eventState,
442 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500443 if (rc != PLDM_SUCCESS)
444 {
445 return PLDM_ERROR;
446 }
447
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800448 // Emitting state sensor event signal
449 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
450 previousEventState);
451
Tom Josephc4959c32020-04-20 19:50:16 +0530452 // If there are no HOST PDR's, there is no further action
453 if (hostPDRHandler == NULL)
454 {
455 return PLDM_SUCCESS;
456 }
457
458 // Handle PLDM events for which PDR is available
459 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530460
461 pldm::pdr::EntityInfo entityInfo{};
462 pldm::pdr::CompositeSensorStates compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600463 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Tom Josephb70a1962020-07-13 12:56:31 +0530464
Tom Josephc4959c32020-04-20 19:50:16 +0530465 try
466 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600467 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephc4959c32020-04-20 19:50:16 +0530468 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530469 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500470 catch (const std::out_of_range&)
Tom Josephc4959c32020-04-20 19:50:16 +0530471 {
Tom Josephb70a1962020-07-13 12:56:31 +0530472 // If there is no mapping for tid, sensorId combination, try
473 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
474 // implement TL PDR.
475 try
476 {
477 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600478 std::tie(entityInfo, compositeSensorStates, stateSetIds) =
Tom Josephb70a1962020-07-13 12:56:31 +0530479 hostPDRHandler->lookupSensorInfo(sensorEntry);
480 }
481 // If there is no mapping for events return PLDM_SUCCESS
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500482 catch (const std::out_of_range&)
Tom Josephb70a1962020-07-13 12:56:31 +0530483 {
484 return PLDM_SUCCESS;
485 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500486 }
Tom Josephb70a1962020-07-13 12:56:31 +0530487
488 if (sensorOffset >= compositeSensorStates.size())
489 {
490 return PLDM_ERROR_INVALID_DATA;
491 }
492
493 const auto& possibleStates = compositeSensorStates[sensorOffset];
Sagar Srinivas06f9b292024-03-31 11:35:28 -0500494 if (!possibleStates.contains(eventState))
Tom Josephb70a1962020-07-13 12:56:31 +0530495 {
496 return PLDM_ERROR_INVALID_DATA;
497 }
498
499 const auto& [containerId, entityType, entityInstance] = entityInfo;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400500 events::StateSensorEntry stateSensorEntry{
501 containerId,
502 entityType,
503 entityInstance,
504 sensorOffset,
505 stateSetIds[sensorOffset],
506 false};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600507 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
508 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530509 }
510 else
511 {
512 return PLDM_ERROR_INVALID_DATA;
513 }
514
515 return PLDM_SUCCESS;
516}
517
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400518int Handler::pldmPDRRepositoryChgEvent(
519 const pldm_msg* request, size_t payloadLength, uint8_t /*formatVersion*/,
520 uint8_t tid, size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500521{
522 uint8_t eventDataFormat{};
523 uint8_t numberOfChangeRecords{};
524 size_t dataOffset{};
525
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400526 auto eventData =
527 reinterpret_cast<const uint8_t*>(request->payload) + eventDataOffset;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500528 auto eventDataSize = payloadLength - eventDataOffset;
529
530 auto rc = decode_pldm_pdr_repository_chg_event_data(
531 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
532 &dataOffset);
533 if (rc != PLDM_SUCCESS)
534 {
535 return rc;
536 }
537
538 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500539
540 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
541 {
542 return PLDM_ERROR_INVALID_DATA;
543 }
544
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500545 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
546 {
547 uint8_t eventDataOperation{};
548 uint8_t numberOfChangeEntries{};
549
550 auto changeRecordData = eventData + dataOffset;
551 auto changeRecordDataSize = eventDataSize - dataOffset;
552
553 while (changeRecordDataSize)
554 {
555 rc = decode_pldm_pdr_repository_change_record_data(
556 changeRecordData, changeRecordDataSize, &eventDataOperation,
557 &numberOfChangeEntries, &dataOffset);
558
559 if (rc != PLDM_SUCCESS)
560 {
561 return rc;
562 }
563
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500564 if (eventDataOperation == PLDM_RECORDS_ADDED ||
565 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500566 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500567 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
568 {
569 hostPDRHandler->isHostPdrModified = true;
570 }
571
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500572 rc = getPDRRecordHandles(
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400573 reinterpret_cast<const ChangeEntry*>(
574 changeRecordData + dataOffset),
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500575 changeRecordDataSize - dataOffset,
576 static_cast<size_t>(numberOfChangeEntries),
577 pdrRecordHandles);
578
579 if (rc != PLDM_SUCCESS)
580 {
581 return rc;
582 }
583 }
584
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400585 changeRecordData +=
586 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500587 changeRecordDataSize -=
588 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
589 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500590 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500591 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500592 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530593 // if we get a Repository change event with the eventDataFormat
594 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
595 // have the matched Terminus handle
596 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
597 {
598 // We cannot get the Repo change event from the Terminus
599 // that is not already added to the BMC repository
600
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500601 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
602 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530603 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500604 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530605 {
606 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500607 it->first);
608 hostPDRHandler->tlPDRInfo.erase(it++);
609 }
610 else
611 {
612 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530613 }
614 }
615 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500616 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500617 }
618
619 return PLDM_SUCCESS;
620}
621
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400622int Handler::getPDRRecordHandles(
623 const ChangeEntry* changeEntryData, size_t changeEntryDataSize,
624 size_t numberOfChangeEntries, PDRRecordHandles& pdrRecordHandles)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500625{
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
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400675 size_t responsePayloadLength =
676 sizeof(completionCode) + sizeof(effecterDataSize) +
677 sizeof(effecterOperationalState) +
678 getEffecterDataSize(effecterDataSize) +
679 getEffecterDataSize(effecterDataSize);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500680
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{
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400701 Response response(
702 sizeof(pldm_msg_hdr) + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
George Liueccb0c52020-01-14 11:09:56 +0800703 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<
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400721 pldm::utils::DBusHandler, Handler>(
722 dBusIntf, *this, effecterId, effecterDataSize, effecterValue,
723 sizeof(effecterValue));
George Liueccb0c52020-01-14 11:09:56 +0800724 }
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);
Thu Nguyen51d66b52024-08-06 09:15:55 +0000749 locatorValue->eid = pldm::BmcMctpEid;
Sampa Misra12afe112020-05-25 11:40:44 -0500750
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{};
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530792 uint16_t containerId{};
Sampa Misraaea5dde2020-08-31 08:33:47 -0500793
794 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530795 entityType, entityInstance, stateSetId, containerId) &&
796 oemPlatformHandler && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500797 {
798 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530799 entityType, entityInstance, containerId, stateSetId, comSensorCnt,
800 sensorId, stateField);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500801 }
802 else
803 {
804 rc = platform_state_sensor::getStateSensorReadingsHandler<
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530805 pldm::utils::DBusHandler, Handler>(
806 dBusIntf, *this, sensorId, sensorRearmCount, comSensorCnt,
807 stateField, dbusToPLDMEventHandler->getSensorCache());
Sampa Misraaea5dde2020-08-31 08:33:47 -0500808 }
George Liu362c18d2020-05-14 09:46:36 +0800809
810 if (rc != PLDM_SUCCESS)
811 {
812 return ccOnlyResponse(request, rc);
813 }
814
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400815 Response response(
816 sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
817 sizeof(get_sensor_state_field) * comSensorCnt);
George Liu362c18d2020-05-14 09:46:36 +0800818 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400819 rc = encode_get_state_sensor_readings_resp(
820 request->hdr.instance_id, rc, comSensorCnt, stateField.data(),
821 responsePtr);
George Liu362c18d2020-05-14 09:46:36 +0800822 if (rc != PLDM_SUCCESS)
823 {
824 return ccOnlyResponse(request, rc);
825 }
826
827 return response;
828}
829
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500830void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
831 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600832{
833 deferredGetPDREvent.reset();
834 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
835}
836
Sampa Misraaea5dde2020-08-31 08:33:47 -0500837bool isOemStateSensor(Handler& handler, uint16_t sensorId,
838 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
839 uint16_t& entityType, uint16_t& entityInstance,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530840 uint16_t& stateSetId, uint16_t& containerId)
Sampa Misraaea5dde2020-08-31 08:33:47 -0500841{
842 pldm_state_sensor_pdr* pdr = nullptr;
843
844 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
845 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930846 if (!stateSensorPdrRepo)
847 {
848 error("Failed to instantiate state sensor PDR repository");
849 return false;
850 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500851 Repo stateSensorPDRs(stateSensorPdrRepo.get());
852 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
853 if (stateSensorPDRs.empty())
854 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600855 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500856 return false;
857 }
858
859 PdrEntry pdrEntry{};
860 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
861 while (pdrRecord)
862 {
863 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
864 assert(pdr != NULL);
865 if (pdr->sensor_id != sensorId)
866 {
867 pdr = nullptr;
868 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
869 continue;
870 }
871 auto tmpEntityType = pdr->entity_type;
872 auto tmpEntityInstance = pdr->entity_instance;
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530873 auto tmpEntityContainerId = pdr->container_id;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500874 auto tmpCompSensorCnt = pdr->composite_sensor_count;
875 auto tmpPossibleStates =
876 reinterpret_cast<state_sensor_possible_states*>(
877 pdr->possible_states);
878 auto tmpStateSetId = tmpPossibleStates->state_set_id;
879
880 if (sensorRearmCount > tmpCompSensorCnt)
881 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600882 error(
Riya Dixit89644442024-03-31 05:39:59 -0500883 "The requester sent wrong sensor rearm count '{SENSOR_REARM_COUNT}' for the sensor ID '{SENSORID}'.",
884 "SENSOR_REARM_COUNT", (uint16_t)sensorRearmCount, "SENSORID",
885 sensorId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500886 break;
887 }
888
889 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
890 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
891 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
892 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
893 {
894 entityType = tmpEntityType;
895 entityInstance = tmpEntityInstance;
896 stateSetId = tmpStateSetId;
897 compSensorCnt = tmpCompSensorCnt;
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530898 containerId = tmpEntityContainerId;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500899 return true;
900 }
901 else
902 {
903 return false;
904 }
905 }
906 return false;
907}
908
909bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
910 uint8_t compEffecterCnt, uint16_t& entityType,
911 uint16_t& entityInstance, uint16_t& stateSetId)
912{
913 pldm_state_effecter_pdr* pdr = nullptr;
914
915 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
916 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930917 if (!stateEffecterPdrRepo)
918 {
919 error("Failed to instantiate state effecter PDR repository");
920 return false;
921 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500922 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
923 getRepoByType(handler.getRepo(), stateEffecterPDRs,
924 PLDM_STATE_EFFECTER_PDR);
925 if (stateEffecterPDRs.empty())
926 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600927 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500928 return false;
929 }
930
931 PdrEntry pdrEntry{};
932 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
933 while (pdrRecord)
934 {
935 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
936 assert(pdr != NULL);
937 if (pdr->effecter_id != effecterId)
938 {
939 pdr = nullptr;
940 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
941 continue;
942 }
943
944 auto tmpEntityType = pdr->entity_type;
945 auto tmpEntityInstance = pdr->entity_instance;
946 auto tmpPossibleStates =
947 reinterpret_cast<state_effecter_possible_states*>(
948 pdr->possible_states);
949 auto tmpStateSetId = tmpPossibleStates->state_set_id;
950
951 if (compEffecterCnt > pdr->composite_effecter_count)
952 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600953 error(
Riya Dixit89644442024-03-31 05:39:59 -0500954 "The requester sent wrong composite effecter count '{COMPOSITE_EFFECTER_COUNT}' for the effecter ID '{EFFECTERID}'.",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500955 "COMPOSITE_EFFECTER_COUNT", compEffecterCnt, "EFFECTERID",
956 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500957 return false;
958 }
959
960 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
961 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
962 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
963 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
964 {
965 entityType = tmpEntityType;
966 entityInstance = tmpEntityInstance;
967 stateSetId = tmpStateSetId;
968 return true;
969 }
970 else
971 {
972 return false;
973 }
974 }
975 return false;
976}
977
Sagar Srinivas90314a32023-10-17 10:38:03 -0500978void Handler::setEventReceiver()
979{
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400980 std::vector<uint8_t> requestMsg(
981 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
Sagar Srinivas90314a32023-10-17 10:38:03 -0500982 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
983 auto instanceId = instanceIdDb->next(eid);
984 uint8_t eventMessageGlobalEnable =
985 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
986 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
Thu Nguyen51d66b52024-08-06 09:15:55 +0000987 uint8_t eventReceiverAddressInfo = pldm::BmcMctpEid;
Sagar Srinivas90314a32023-10-17 10:38:03 -0500988 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
989
990 auto rc = encode_set_event_receiver_req(
991 instanceId, eventMessageGlobalEnable, transportProtocolType,
992 eventReceiverAddressInfo, heartbeatTimer, request);
993 if (rc != PLDM_SUCCESS)
994 {
995 instanceIdDb->free(eid, instanceId);
Riya Dixit89644442024-03-31 05:39:59 -0500996 error(
997 "Failed to encode set event receiver request, response code '{RC}'",
998 "RC", lg2::hex, rc);
Sagar Srinivas90314a32023-10-17 10:38:03 -0500999 return;
1000 }
1001
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001002 auto processSetEventReceiverResponse = [](mctp_eid_t /*eid*/,
1003 const pldm_msg* response,
1004 size_t respMsgLen) {
Sagar Srinivas90314a32023-10-17 10:38:03 -05001005 if (response == nullptr || !respMsgLen)
1006 {
1007 error("Failed to receive response for setEventReceiver command");
1008 return;
1009 }
1010
1011 uint8_t completionCode{};
1012 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
1013 &completionCode);
1014 if (rc || completionCode)
1015 {
1016 error(
Riya Dixit89644442024-03-31 05:39:59 -05001017 "Failed to decode setEventReceiver command, response code '{RC}' and completion code '{CC}'",
Riya Dixit1e5c81e2024-05-03 07:54:00 -05001018 "RC", rc, "CC", completionCode);
Sagar Srinivas90314a32023-10-17 10:38:03 -05001019 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +05301020 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Sagar Srinivas90314a32023-10-17 10:38:03 -05001021 }
1022 };
1023 rc = handler->registerRequest(
1024 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
1025 std::move(requestMsg), std::move(processSetEventReceiverResponse));
1026
1027 if (rc != PLDM_SUCCESS)
1028 {
1029 error("Failed to send the setEventReceiver request");
1030 }
1031
1032 if (oemPlatformHandler)
1033 {
1034 oemPlatformHandler->countSetEventReceiver();
1035 oemPlatformHandler->checkAndDisableWatchDog();
1036 }
1037}
1038
Deepak Kodihallibc669f12019-11-28 08:52:07 -06001039} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301040} // namespace responder
1041} // namespace pldm