blob: 5f71252201e8cff40a7128422193f43d232631e1 [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#include "platform.hpp"
2
Tom Josephb70a1962020-07-13 12:56:31 +05303#include "common/types.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05004#include "common/utils.hpp"
Tom Josephc4959c32020-04-20 19:50:16 +05305#include "event_parser.hpp"
Sampa Misra12afe112020-05-25 11:40:44 -05006#include "pdr.hpp"
George Liu456c9a22020-01-13 11:36:22 +08007#include "pdr_numeric_effecter.hpp"
George Liua2870722020-02-11 11:09:30 +08008#include "pdr_state_effecter.hpp"
George Liuadbe1722020-05-09 19:20:19 +08009#include "pdr_state_sensor.hpp"
George Liu362c18d2020-05-14 09:46:36 +080010#include "pdr_utils.hpp"
George Liueccb0c52020-01-14 11:09:56 +080011#include "platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +080012#include "platform_state_effecter.hpp"
George Liu362c18d2020-05-14 09:46:36 +080013#include "platform_state_sensor.hpp"
George Liu83409572019-12-24 18:42:54 +080014
George Liuc453e162022-12-21 17:16:23 +080015#include <libpldm/entity.h>
16#include <libpldm/state_set.h>
Manojkiran Edacc5f1582021-09-29 17:03:06 +053017
Riya Dixit49cfb132023-03-02 04:26:53 -060018#include <phosphor-logging/lg2.hpp>
19
20PHOSPHOR_LOG2_USING;
21
Brad Bishop5079ac42021-08-19 18:35:06 -040022using namespace pldm::utils;
23using namespace pldm::responder::pdr;
24using namespace pldm::responder::pdr_utils;
25
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053026namespace pldm
27{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053028namespace responder
29{
Sampa Misraa2fa0702019-05-31 01:28:55 -050030namespace platform
31{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060032using InternalFailure =
33 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
34
George Liu1ec85d42020-02-12 16:05:32 +080035static const Json empty{};
36
George Liua2870722020-02-11 11:09:30 +080037void Handler::addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +080038 uint16_t id,
39 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
40 TypeId typeId)
George Liu1ec85d42020-02-12 16:05:32 +080041{
George Liuadbe1722020-05-09 19:20:19 +080042 if (typeId == TypeId::PLDM_SENSOR_ID)
43 {
44 sensorDbusObjMaps.emplace(id, dbusObj);
45 }
46 else
47 {
48 effecterDbusObjMaps.emplace(id, dbusObj);
49 }
George Liu1ec85d42020-02-12 16:05:32 +080050}
51
George Liua2870722020-02-11 11:09:30 +080052const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liuadbe1722020-05-09 19:20:19 +080053 Handler::getDbusObjMaps(uint16_t id, TypeId typeId) const
George Liu1ec85d42020-02-12 16:05:32 +080054{
George Liuadbe1722020-05-09 19:20:19 +080055 if (typeId == TypeId::PLDM_SENSOR_ID)
56 {
57 return sensorDbusObjMaps.at(id);
58 }
59 else
60 {
61 return effecterDbusObjMaps.at(id);
62 }
George Liu1ec85d42020-02-12 16:05:32 +080063}
64
George Liu36e81352020-07-01 14:40:30 +080065void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
66 const std::string& dir, Repo& repo)
Deepak Kodihallic682fe22020-03-04 00:42:54 -060067{
Deepak Kodihallic6e49c42020-07-01 03:39:27 -050068 if (!fs::exists(dir))
69 {
70 return;
71 }
72
Deepak Kodihallic682fe22020-03-04 00:42:54 -060073 // A map of PDR type to a lambda that handles creation of that PDR type.
74 // The lambda essentially would parse the platform specific PDR JSONs to
75 // generate the PDR structures. This function iterates through the map to
76 // invoke all lambdas, so that all PDR types can be created.
George Liua2870722020-02-11 11:09:30 +080077
Patrick Williamsa6756622023-10-20 11:19:15 -050078 const std::map<Type, generatePDR> generateHandlers = {
79 {PLDM_STATE_EFFECTER_PDR,
80 [this](const DBusHandler& dBusIntf, const auto& json,
81 RepoInterface& repo) {
Patrick Williams6da4f912023-05-10 07:50:53 -050082 pdr_state_effecter::generateStateEffecterPDR<pldm::utils::DBusHandler,
83 Handler>(dBusIntf, json,
84 *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050085 }},
86 {PLDM_NUMERIC_EFFECTER_PDR,
87 [this](const DBusHandler& dBusIntf, const auto& json,
88 RepoInterface& repo) {
Patrick Williams6da4f912023-05-10 07:50:53 -050089 pdr_numeric_effecter::generateNumericEffecterPDR<
90 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this, repo);
Patrick Williamsa6756622023-10-20 11:19:15 -050091 }},
George Liuadbe1722020-05-09 19:20:19 +080092 {PLDM_STATE_SENSOR_PDR, [this](const DBusHandler& dBusIntf,
93 const auto& json, RepoInterface& repo) {
Patrick Williamsa6756622023-10-20 11:19:15 -050094 pdr_state_sensor::generateStateSensorPDR<pldm::utils::DBusHandler,
95 Handler>(dBusIntf, json, *this,
96 repo);
97 }}};
Deepak Kodihallic682fe22020-03-04 00:42:54 -060098
99 Type pdrType{};
100 for (const auto& dirEntry : fs::directory_iterator(dir))
101 {
102 try
103 {
104 auto json = readJson(dirEntry.path().string());
105 if (!json.empty())
106 {
George Liu1ec85d42020-02-12 16:05:32 +0800107 auto effecterPDRs = json.value("effecterPDRs", empty);
108 for (const auto& effecter : effecterPDRs)
109 {
110 pdrType = effecter.value("pdrType", 0);
George Liu36e81352020-07-01 14:40:30 +0800111 generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
George Liu1ec85d42020-02-12 16:05:32 +0800112 }
George Liuadbe1722020-05-09 19:20:19 +0800113
114 auto sensorPDRs = json.value("sensorPDRs", empty);
115 for (const auto& sensor : sensorPDRs)
116 {
117 pdrType = sensor.value("pdrType", 0);
118 generateHandlers.at(pdrType)(dBusIntf, sensor, repo);
119 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600120 }
121 }
122 catch (const InternalFailure& e)
123 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600124 error(
125 "PDR config directory does not exist or empty, TYPE= {PDR_TYPE} PATH={DIR_PATH} ERROR={ERR_EXCEP}",
126 "PDR_TYPE", pdrType, "DIR_PATH", dirEntry.path().string(),
127 "ERR_EXCEP", e.what());
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600128 }
129 catch (const Json::exception& e)
130 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600131 error(
132 "Failed parsing PDR JSON file, TYPE={PDR_TYPE} ERROR={ERR_EXCEP}",
133 "PDR_TYPE", pdrType, "ERR_EXCEP", e.what());
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600134 pldm::utils::reportError(
135 "xyz.openbmc_project.bmc.pldm.InternalFailure");
136 }
137 catch (const std::exception& e)
138 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600139 error(
140 "Failed parsing PDR JSON file, TYPE= {PDR_TYPE} ERROR={ERR_EXCEP}",
141 "PDR_TYPE", pdrType, "ERR_EXCEP", e.what());
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600142 pldm::utils::reportError(
143 "xyz.openbmc_project.bmc.pldm.InternalFailure");
144 }
145 }
146}
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530147
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600148Response Handler::getPDR(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530149{
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500150 if (hostPDRHandler)
151 {
152 if (hostPDRHandler->isHostUp() && oemPlatformHandler != nullptr)
153 {
154 auto rc = oemPlatformHandler->checkBMCState();
155 if (rc != PLDM_SUCCESS)
156 {
157 return ccOnlyResponse(request, PLDM_ERROR_NOT_READY);
158 }
159 }
160 }
161
162 // Build FRU table if not built, since entity association PDR's
163 // are built when the FRU table is constructed.
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530164 if (fruHandler)
165 {
166 fruHandler->buildFRUTable();
167 }
168
George Liud680ae02020-07-17 09:11:14 +0800169 if (!pdrCreated)
170 {
171 generateTerminusLocatorPDR(pdrRepo);
172 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500173 if (oemPlatformHandler != nullptr)
174 {
175 oemPlatformHandler->buildOEMPDR(pdrRepo);
176 }
177
George Liud680ae02020-07-17 09:11:14 +0800178 pdrCreated = true;
George Liu5eed8e52020-12-18 11:24:37 +0800179
180 if (dbusToPLDMEventHandler)
181 {
Sampa Misra5fb37d52021-03-06 07:26:00 -0600182 deferredGetPDREvent = std::make_unique<sdeventplus::source::Defer>(
183 event,
184 std::bind(std::mem_fn(&pldm::responder::platform::Handler::
185 _processPostGetPDRActions),
186 this, std::placeholders::_1));
George Liu5eed8e52020-12-18 11:24:37 +0800187 }
George Liud680ae02020-07-17 09:11:14 +0800188 }
189
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530190 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES, 0);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530191
192 if (payloadLength != PLDM_GET_PDR_REQ_BYTES)
193 {
George Liufb8611d2019-12-06 10:14:15 +0800194 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530195 }
196
197 uint32_t recordHandle{};
198 uint32_t dataTransferHandle{};
199 uint8_t transferOpFlag{};
200 uint16_t reqSizeBytes{};
201 uint16_t recordChangeNum{};
202
George Liufb8611d2019-12-06 10:14:15 +0800203 auto rc = decode_get_pdr_req(request, payloadLength, &recordHandle,
204 &dataTransferHandle, &transferOpFlag,
205 &reqSizeBytes, &recordChangeNum);
206 if (rc != PLDM_SUCCESS)
207 {
208 return CmdHandler::ccOnlyResponse(request, rc);
209 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530210
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530211 uint16_t respSizeBytes{};
212 uint8_t* recordData = nullptr;
213 try
214 {
George Liue53193f2020-02-24 09:23:26 +0800215 pdr_utils::PdrEntry e;
216 auto record = pdr::getRecordByHandle(pdrRepo, recordHandle, e);
217 if (record == NULL)
218 {
219 return CmdHandler::ccOnlyResponse(
220 request, PLDM_PLATFORM_INVALID_RECORD_HANDLE);
221 }
222
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530223 if (reqSizeBytes)
224 {
George Liue53193f2020-02-24 09:23:26 +0800225 respSizeBytes = e.size;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530226 if (respSizeBytes > reqSizeBytes)
227 {
228 respSizeBytes = reqSizeBytes;
229 }
George Liue53193f2020-02-24 09:23:26 +0800230 recordData = e.data;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530231 }
232 response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES +
233 respSizeBytes,
234 0);
Manojkiran Eda31a78442021-09-12 15:18:25 +0530235 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli22b5a7d2020-03-17 23:28:41 -0500236 rc = encode_get_pdr_resp(
237 request->hdr.instance_id, PLDM_SUCCESS, e.handle.nextRecordHandle,
238 0, PLDM_START_AND_END, respSizeBytes, recordData, 0, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800239 if (rc != PLDM_SUCCESS)
240 {
241 return ccOnlyResponse(request, rc);
242 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530243 }
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530244 catch (const std::exception& e)
245 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600246 error("Error accessing PDR, HANDLE={REC_HANDLE} ERROR={ERR_EXCEP}",
247 "REC_HANDLE", recordHandle, "ERR_EXCEP", e.what());
George Liufb8611d2019-12-06 10:14:15 +0800248 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530249 }
250 return response;
251}
252
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600253Response Handler::setStateEffecterStates(const pldm_msg* request,
254 size_t payloadLength)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500255{
256 Response response(
257 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES, 0);
258 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
259 uint16_t effecterId;
260 uint8_t compEffecterCnt;
261 constexpr auto maxCompositeEffecterCnt = 8;
262 std::vector<set_effecter_state_field> stateField(maxCompositeEffecterCnt,
263 {0, 0});
264
265 if ((payloadLength > PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES) ||
266 (payloadLength < sizeof(effecterId) + sizeof(compEffecterCnt) +
267 sizeof(set_effecter_state_field)))
268 {
George Liufb8611d2019-12-06 10:14:15 +0800269 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500270 }
271
272 int rc = decode_set_state_effecter_states_req(request, payloadLength,
273 &effecterId, &compEffecterCnt,
274 stateField.data());
275
George Liufb8611d2019-12-06 10:14:15 +0800276 if (rc != PLDM_SUCCESS)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500277 {
George Liufb8611d2019-12-06 10:14:15 +0800278 return CmdHandler::ccOnlyResponse(request, rc);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500279 }
280
George Liufb8611d2019-12-06 10:14:15 +0800281 stateField.resize(compEffecterCnt);
282 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500283 uint16_t entityType{};
284 uint16_t entityInstance{};
285 uint16_t stateSetId{};
286
287 if (isOemStateEffecter(*this, effecterId, compEffecterCnt, entityType,
288 entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530289 oemPlatformHandler != nullptr &&
290 !effecterDbusObjMaps.contains(effecterId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500291 {
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500292 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500293 entityType, entityInstance, stateSetId, compEffecterCnt, stateField,
294 effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500295 }
296 else
297 {
298 rc = platform_state_effecter::setStateEffecterStatesHandler<
299 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
300 stateField);
301 }
George Liufb8611d2019-12-06 10:14:15 +0800302 if (rc != PLDM_SUCCESS)
303 {
304 return CmdHandler::ccOnlyResponse(request, rc);
305 }
306
307 rc = encode_set_state_effecter_states_resp(request->hdr.instance_id, rc,
308 responsePtr);
309 if (rc != PLDM_SUCCESS)
310 {
311 return ccOnlyResponse(request, rc);
312 }
313
Sampa Misraa2fa0702019-05-31 01:28:55 -0500314 return response;
315}
316
Tom Joseph56e45c52020-03-16 10:01:45 +0530317Response Handler::platformEventMessage(const pldm_msg* request,
318 size_t payloadLength)
319{
320 uint8_t formatVersion{};
321 uint8_t tid{};
322 uint8_t eventClass{};
323 size_t offset{};
324
325 auto rc = decode_platform_event_message_req(
326 request, payloadLength, &formatVersion, &tid, &eventClass, &offset);
327 if (rc != PLDM_SUCCESS)
328 {
329 return CmdHandler::ccOnlyResponse(request, rc);
330 }
331
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500332 if (eventClass == PLDM_HEARTBEAT_TIMER_ELAPSED_EVENT)
Tom Joseph56e45c52020-03-16 10:01:45 +0530333 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500334 rc = PLDM_SUCCESS;
Sagar Srinivas79669c92021-04-28 15:43:30 -0500335 if (oemPlatformHandler)
336 {
337 oemPlatformHandler->resetWatchDogTimer();
338 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500339 }
340 else
341 {
342 try
Tom Joseph56e45c52020-03-16 10:01:45 +0530343 {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500344 const auto& handlers = eventHandlers.at(eventClass);
345 for (const auto& handler : handlers)
Tom Joseph56e45c52020-03-16 10:01:45 +0530346 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500347 auto rc = handler(request, payloadLength, formatVersion, tid,
348 offset);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500349 if (rc != PLDM_SUCCESS)
350 {
351 return CmdHandler::ccOnlyResponse(request, rc);
352 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530353 }
354 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500355 catch (const std::out_of_range& e)
356 {
357 return CmdHandler::ccOnlyResponse(request, PLDM_ERROR_INVALID_DATA);
358 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530359 }
Tom Joseph56e45c52020-03-16 10:01:45 +0530360 Response response(
361 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES, 0);
362 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
363
364 rc = encode_platform_event_message_resp(request->hdr.instance_id, rc,
365 PLDM_EVENT_NO_LOGGING, responsePtr);
366 if (rc != PLDM_SUCCESS)
367 {
368 return ccOnlyResponse(request, rc);
369 }
370
371 return response;
372}
373
374int Handler::sensorEvent(const pldm_msg* request, size_t payloadLength,
Tom Josephc4959c32020-04-20 19:50:16 +0530375 uint8_t /*formatVersion*/, uint8_t tid,
Tom Joseph56e45c52020-03-16 10:01:45 +0530376 size_t eventDataOffset)
377{
378 uint16_t sensorId{};
379 uint8_t eventClass{};
380 size_t eventClassDataOffset{};
Patrick Williams6da4f912023-05-10 07:50:53 -0500381 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
382 eventDataOffset;
Tom Joseph56e45c52020-03-16 10:01:45 +0530383 auto eventDataSize = payloadLength - eventDataOffset;
384
385 auto rc = decode_sensor_event_data(eventData, eventDataSize, &sensorId,
386 &eventClass, &eventClassDataOffset);
387 if (rc != PLDM_SUCCESS)
388 {
389 return rc;
390 }
391
Zahed Hossain75330f32020-03-24 02:15:03 -0500392 auto eventClassData = reinterpret_cast<const uint8_t*>(request->payload) +
393 eventDataOffset + eventClassDataOffset;
Patrick Williams6da4f912023-05-10 07:50:53 -0500394 auto eventClassDataSize = payloadLength - eventDataOffset -
395 eventClassDataOffset;
Zahed Hossain75330f32020-03-24 02:15:03 -0500396
Tom Joseph56e45c52020-03-16 10:01:45 +0530397 if (eventClass == PLDM_STATE_SENSOR_STATE)
398 {
399 uint8_t sensorOffset{};
400 uint8_t eventState{};
401 uint8_t previousEventState{};
402
Zahed Hossain75330f32020-03-24 02:15:03 -0500403 rc = decode_state_sensor_data(eventClassData, eventClassDataSize,
Tom Joseph56e45c52020-03-16 10:01:45 +0530404 &sensorOffset, &eventState,
405 &previousEventState);
Zahed Hossain75330f32020-03-24 02:15:03 -0500406 if (rc != PLDM_SUCCESS)
407 {
408 return PLDM_ERROR;
409 }
410
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800411 // Emitting state sensor event signal
412 emitStateSensorEventSignal(tid, sensorId, sensorOffset, eventState,
413 previousEventState);
414
Tom Josephc4959c32020-04-20 19:50:16 +0530415 // If there are no HOST PDR's, there is no further action
416 if (hostPDRHandler == NULL)
417 {
418 return PLDM_SUCCESS;
419 }
420
421 // Handle PLDM events for which PDR is available
422 SensorEntry sensorEntry{tid, sensorId};
Tom Josephb70a1962020-07-13 12:56:31 +0530423
424 pldm::pdr::EntityInfo entityInfo{};
425 pldm::pdr::CompositeSensorStates compositeSensorStates{};
426
Tom Josephc4959c32020-04-20 19:50:16 +0530427 try
428 {
Tom Josephb70a1962020-07-13 12:56:31 +0530429 std::tie(entityInfo, compositeSensorStates) =
Tom Josephc4959c32020-04-20 19:50:16 +0530430 hostPDRHandler->lookupSensorInfo(sensorEntry);
Tom Josephc4959c32020-04-20 19:50:16 +0530431 }
Tom Josephc4959c32020-04-20 19:50:16 +0530432 catch (const std::out_of_range& e)
433 {
Tom Josephb70a1962020-07-13 12:56:31 +0530434 // If there is no mapping for tid, sensorId combination, try
435 // PLDM_TID_RESERVED, sensorId for terminus that is yet to
436 // implement TL PDR.
437 try
438 {
439 sensorEntry.terminusID = PLDM_TID_RESERVED;
440 std::tie(entityInfo, compositeSensorStates) =
441 hostPDRHandler->lookupSensorInfo(sensorEntry);
442 }
443 // If there is no mapping for events return PLDM_SUCCESS
444 catch (const std::out_of_range& e)
445 {
446 return PLDM_SUCCESS;
447 }
Zahed Hossain75330f32020-03-24 02:15:03 -0500448 }
Tom Josephb70a1962020-07-13 12:56:31 +0530449
450 if (sensorOffset >= compositeSensorStates.size())
451 {
452 return PLDM_ERROR_INVALID_DATA;
453 }
454
455 const auto& possibleStates = compositeSensorStates[sensorOffset];
456 if (possibleStates.find(eventState) == possibleStates.end())
457 {
458 return PLDM_ERROR_INVALID_DATA;
459 }
460
461 const auto& [containerId, entityType, entityInstance] = entityInfo;
462 events::StateSensorEntry stateSensorEntry{containerId, entityType,
463 entityInstance, sensorOffset};
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600464 return hostPDRHandler->handleStateSensorEvent(stateSensorEntry,
465 eventState);
Tom Joseph56e45c52020-03-16 10:01:45 +0530466 }
467 else
468 {
469 return PLDM_ERROR_INVALID_DATA;
470 }
471
472 return PLDM_SUCCESS;
473}
474
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500475int Handler::pldmPDRRepositoryChgEvent(const pldm_msg* request,
476 size_t payloadLength,
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530477 uint8_t /*formatVersion*/, uint8_t tid,
478 size_t eventDataOffset)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500479{
480 uint8_t eventDataFormat{};
481 uint8_t numberOfChangeRecords{};
482 size_t dataOffset{};
483
Patrick Williams6da4f912023-05-10 07:50:53 -0500484 auto eventData = reinterpret_cast<const uint8_t*>(request->payload) +
485 eventDataOffset;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500486 auto eventDataSize = payloadLength - eventDataOffset;
487
488 auto rc = decode_pldm_pdr_repository_chg_event_data(
489 eventData, eventDataSize, &eventDataFormat, &numberOfChangeRecords,
490 &dataOffset);
491 if (rc != PLDM_SUCCESS)
492 {
493 return rc;
494 }
495
496 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500497
498 if (eventDataFormat == FORMAT_IS_PDR_TYPES)
499 {
500 return PLDM_ERROR_INVALID_DATA;
501 }
502
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500503 if (eventDataFormat == FORMAT_IS_PDR_HANDLES)
504 {
505 uint8_t eventDataOperation{};
506 uint8_t numberOfChangeEntries{};
507
508 auto changeRecordData = eventData + dataOffset;
509 auto changeRecordDataSize = eventDataSize - dataOffset;
510
511 while (changeRecordDataSize)
512 {
513 rc = decode_pldm_pdr_repository_change_record_data(
514 changeRecordData, changeRecordDataSize, &eventDataOperation,
515 &numberOfChangeEntries, &dataOffset);
516
517 if (rc != PLDM_SUCCESS)
518 {
519 return rc;
520 }
521
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500522 if (eventDataOperation == PLDM_RECORDS_ADDED ||
523 eventDataOperation == PLDM_RECORDS_MODIFIED)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500524 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500525 if (eventDataOperation == PLDM_RECORDS_MODIFIED)
526 {
527 hostPDRHandler->isHostPdrModified = true;
528 }
529
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500530 rc = getPDRRecordHandles(
531 reinterpret_cast<const ChangeEntry*>(changeRecordData +
532 dataOffset),
533 changeRecordDataSize - dataOffset,
534 static_cast<size_t>(numberOfChangeEntries),
535 pdrRecordHandles);
536
537 if (rc != PLDM_SUCCESS)
538 {
539 return rc;
540 }
541 }
542
Patrick Williams6da4f912023-05-10 07:50:53 -0500543 changeRecordData += dataOffset +
544 (numberOfChangeEntries * sizeof(ChangeEntry));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500545 changeRecordDataSize -=
546 dataOffset + (numberOfChangeEntries * sizeof(ChangeEntry));
547 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500548 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500549 if (hostPDRHandler)
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500550 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530551 // if we get a Repository change event with the eventDataFormat
552 // as REFRESH_ENTIRE_REPOSITORY, then delete all the PDR's that
553 // have the matched Terminus handle
554 if (eventDataFormat == REFRESH_ENTIRE_REPOSITORY)
555 {
556 // We cannot get the Repo change event from the Terminus
557 // that is not already added to the BMC repository
558
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500559 for (auto it = hostPDRHandler->tlPDRInfo.cbegin();
560 it != hostPDRHandler->tlPDRInfo.cend();)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530561 {
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500562 if (std::get<0>(it->second) == tid)
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530563 {
564 pldm_pdr_remove_pdrs_by_terminus_handle(pdrRepo.getPdr(),
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500565 it->first);
566 hostPDRHandler->tlPDRInfo.erase(it++);
567 }
568 else
569 {
570 ++it;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530571 }
572 }
573 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500574 hostPDRHandler->fetchPDR(std::move(pdrRecordHandles));
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500575 }
576
577 return PLDM_SUCCESS;
578}
579
580int Handler::getPDRRecordHandles(const ChangeEntry* changeEntryData,
581 size_t changeEntryDataSize,
582 size_t numberOfChangeEntries,
583 PDRRecordHandles& pdrRecordHandles)
584{
585 if (numberOfChangeEntries > (changeEntryDataSize / sizeof(ChangeEntry)))
586 {
587 return PLDM_ERROR_INVALID_DATA;
588 }
589 for (size_t i = 0; i < numberOfChangeEntries; i++)
590 {
591 pdrRecordHandles.push_back(changeEntryData[i]);
592 }
593 return PLDM_SUCCESS;
594}
595
George Liueccb0c52020-01-14 11:09:56 +0800596Response Handler::setNumericEffecterValue(const pldm_msg* request,
597 size_t payloadLength)
598{
599 Response response(sizeof(pldm_msg_hdr) +
600 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
601 uint16_t effecterId{};
602 uint8_t effecterDataSize{};
603 uint8_t effecterValue[4] = {};
604
605 if ((payloadLength > sizeof(effecterId) + sizeof(effecterDataSize) +
606 sizeof(union_effecter_data_size)) ||
607 (payloadLength < sizeof(effecterId) + sizeof(effecterDataSize) + 1))
608 {
609 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
610 }
611
612 int rc = decode_set_numeric_effecter_value_req(
Andrew Jeffery8fbf3cc2023-04-12 13:42:29 +0930613 request, payloadLength, &effecterId, &effecterDataSize, effecterValue);
George Liueccb0c52020-01-14 11:09:56 +0800614
615 if (rc == PLDM_SUCCESS)
616 {
617 const pldm::utils::DBusHandler dBusIntf;
618 rc = platform_numeric_effecter::setNumericEffecterValueHandler<
619 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, effecterId,
620 effecterDataSize, effecterValue,
621 sizeof(effecterValue));
622 }
623
624 return ccOnlyResponse(request, rc);
625}
626
Sampa Misra12afe112020-05-25 11:40:44 -0500627void Handler::generateTerminusLocatorPDR(Repo& repo)
628{
629 std::vector<uint8_t> pdrBuffer(sizeof(pldm_terminus_locator_pdr));
630
631 auto pdr = reinterpret_cast<pldm_terminus_locator_pdr*>(pdrBuffer.data());
632
633 pdr->hdr.record_handle = 0;
634 pdr->hdr.version = 1;
635 pdr->hdr.type = PLDM_TERMINUS_LOCATOR_PDR;
636 pdr->hdr.record_change_num = 0;
637 pdr->hdr.length = sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530638 pdr->terminus_handle = TERMINUS_HANDLE;
Sampa Misra12afe112020-05-25 11:40:44 -0500639 pdr->validity = PLDM_TL_PDR_VALID;
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530640 pdr->tid = TERMINUS_ID;
Sampa Misra12afe112020-05-25 11:40:44 -0500641 pdr->container_id = 0x0;
642 pdr->terminus_locator_type = PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID;
643 pdr->terminus_locator_value_size =
644 sizeof(pldm_terminus_locator_type_mctp_eid);
645 auto locatorValue = reinterpret_cast<pldm_terminus_locator_type_mctp_eid*>(
646 pdr->terminus_locator_value);
647 locatorValue->eid = BmcMctpEid;
648
649 PdrEntry pdrEntry{};
650 pdrEntry.data = pdrBuffer.data();
651 pdrEntry.size = pdrBuffer.size();
652 repo.addRecord(pdrEntry);
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530653 if (hostPDRHandler)
654 {
655 hostPDRHandler->tlPDRInfo.insert_or_assign(
656 pdr->terminus_handle,
657 std::make_tuple(pdr->tid, locatorValue->eid, pdr->validity));
658 }
Sampa Misra12afe112020-05-25 11:40:44 -0500659}
George Liu362c18d2020-05-14 09:46:36 +0800660
661Response Handler::getStateSensorReadings(const pldm_msg* request,
662 size_t payloadLength)
663{
664 uint16_t sensorId{};
665 bitfield8_t sensorRearm{};
666 uint8_t reserved{};
667
Pavithra Barithaya87083f22023-04-17 01:27:49 -0500668 if (payloadLength != PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES)
George Liu362c18d2020-05-14 09:46:36 +0800669 {
670 return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);
671 }
672
673 int rc = decode_get_state_sensor_readings_req(
674 request, payloadLength, &sensorId, &sensorRearm, &reserved);
675
676 if (rc != PLDM_SUCCESS)
677 {
678 return ccOnlyResponse(request, rc);
679 }
680
681 // 0x01 to 0x08
George Liuc1230ca2021-08-03 16:06:50 +0800682 uint8_t sensorRearmCount = std::popcount(sensorRearm.byte);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500683 std::vector<get_sensor_state_field> stateField(sensorRearmCount);
George Liu362c18d2020-05-14 09:46:36 +0800684 uint8_t comSensorCnt{};
685 const pldm::utils::DBusHandler dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500686
687 uint16_t entityType{};
688 uint16_t entityInstance{};
689 uint16_t stateSetId{};
690
691 if (isOemStateSensor(*this, sensorId, sensorRearmCount, comSensorCnt,
692 entityType, entityInstance, stateSetId) &&
Manojkiran Eda321804e2022-03-03 12:36:54 +0530693 oemPlatformHandler != nullptr && !sensorDbusObjMaps.contains(sensorId))
Sampa Misraaea5dde2020-08-31 08:33:47 -0500694 {
695 rc = oemPlatformHandler->getOemStateSensorReadingsHandler(
696 entityType, entityInstance, stateSetId, comSensorCnt, stateField);
697 }
698 else
699 {
700 rc = platform_state_sensor::getStateSensorReadingsHandler<
701 pldm::utils::DBusHandler, Handler>(dBusIntf, *this, sensorId,
702 sensorRearmCount, comSensorCnt,
703 stateField);
704 }
George Liu362c18d2020-05-14 09:46:36 +0800705
706 if (rc != PLDM_SUCCESS)
707 {
708 return ccOnlyResponse(request, rc);
709 }
710
711 Response response(sizeof(pldm_msg_hdr) +
712 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
713 sizeof(get_sensor_state_field) * comSensorCnt);
714 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
715 rc = encode_get_state_sensor_readings_resp(request->hdr.instance_id, rc,
716 comSensorCnt, stateField.data(),
717 responsePtr);
718 if (rc != PLDM_SUCCESS)
719 {
720 return ccOnlyResponse(request, rc);
721 }
722
723 return response;
724}
725
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500726void Handler::_processPostGetPDRActions(sdeventplus::source::EventBase&
727 /*source */)
Sampa Misra5fb37d52021-03-06 07:26:00 -0600728{
729 deferredGetPDREvent.reset();
730 dbusToPLDMEventHandler->listenSensorEvent(pdrRepo, sensorDbusObjMaps);
731}
732
Sampa Misraaea5dde2020-08-31 08:33:47 -0500733bool isOemStateSensor(Handler& handler, uint16_t sensorId,
734 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
735 uint16_t& entityType, uint16_t& entityInstance,
736 uint16_t& stateSetId)
737{
738 pldm_state_sensor_pdr* pdr = nullptr;
739
740 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateSensorPdrRepo(
741 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930742 if (!stateSensorPdrRepo)
743 {
744 error("Failed to instantiate state sensor PDR repository");
745 return false;
746 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500747 Repo stateSensorPDRs(stateSensorPdrRepo.get());
748 getRepoByType(handler.getRepo(), stateSensorPDRs, PLDM_STATE_SENSOR_PDR);
749 if (stateSensorPDRs.empty())
750 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600751 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500752 return false;
753 }
754
755 PdrEntry pdrEntry{};
756 auto pdrRecord = stateSensorPDRs.getFirstRecord(pdrEntry);
757 while (pdrRecord)
758 {
759 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
760 assert(pdr != NULL);
761 if (pdr->sensor_id != sensorId)
762 {
763 pdr = nullptr;
764 pdrRecord = stateSensorPDRs.getNextRecord(pdrRecord, pdrEntry);
765 continue;
766 }
767 auto tmpEntityType = pdr->entity_type;
768 auto tmpEntityInstance = pdr->entity_instance;
769 auto tmpCompSensorCnt = pdr->composite_sensor_count;
770 auto tmpPossibleStates =
771 reinterpret_cast<state_sensor_possible_states*>(
772 pdr->possible_states);
773 auto tmpStateSetId = tmpPossibleStates->state_set_id;
774
775 if (sensorRearmCount > tmpCompSensorCnt)
776 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600777 error(
778 "The requester sent wrong sensorRearm count for the sensor, SENSOR_ID={SENSOR_ID} SENSOR_REARM_COUNT={SENSOR_REARM_CNT}",
779 "SENSOR_ID", sensorId, "SENSOR_REARM_CNT",
780 (uint16_t)sensorRearmCount);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500781 break;
782 }
783
784 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
785 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
786 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
787 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
788 {
789 entityType = tmpEntityType;
790 entityInstance = tmpEntityInstance;
791 stateSetId = tmpStateSetId;
792 compSensorCnt = tmpCompSensorCnt;
793 return true;
794 }
795 else
796 {
797 return false;
798 }
799 }
800 return false;
801}
802
803bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
804 uint8_t compEffecterCnt, uint16_t& entityType,
805 uint16_t& entityInstance, uint16_t& stateSetId)
806{
807 pldm_state_effecter_pdr* pdr = nullptr;
808
809 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
810 pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930811 if (!stateEffecterPdrRepo)
812 {
813 error("Failed to instantiate state effecter PDR repository");
814 return false;
815 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500816 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
817 getRepoByType(handler.getRepo(), stateEffecterPDRs,
818 PLDM_STATE_EFFECTER_PDR);
819 if (stateEffecterPDRs.empty())
820 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600821 error("Failed to get record by PDR type");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500822 return false;
823 }
824
825 PdrEntry pdrEntry{};
826 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
827 while (pdrRecord)
828 {
829 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
830 assert(pdr != NULL);
831 if (pdr->effecter_id != effecterId)
832 {
833 pdr = nullptr;
834 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
835 continue;
836 }
837
838 auto tmpEntityType = pdr->entity_type;
839 auto tmpEntityInstance = pdr->entity_instance;
840 auto tmpPossibleStates =
841 reinterpret_cast<state_effecter_possible_states*>(
842 pdr->possible_states);
843 auto tmpStateSetId = tmpPossibleStates->state_set_id;
844
845 if (compEffecterCnt > pdr->composite_effecter_count)
846 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600847 error(
848 "The requester sent wrong composite effecter count for the effecter, EFFECTER_ID={EFFECTER_ID} COMP_EFF_CNT={COMP_EFF_CNT}",
849 "EFFECTER_ID", effecterId, "COMP_EFF_CNT",
850 (uint16_t)compEffecterCnt);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500851 return false;
852 }
853
854 if ((tmpEntityType >= PLDM_OEM_ENTITY_TYPE_START &&
855 tmpEntityType <= PLDM_OEM_ENTITY_TYPE_END) ||
856 (tmpStateSetId >= PLDM_OEM_STATE_SET_ID_START &&
857 tmpStateSetId < PLDM_OEM_STATE_SET_ID_END))
858 {
859 entityType = tmpEntityType;
860 entityInstance = tmpEntityInstance;
861 stateSetId = tmpStateSetId;
862 return true;
863 }
864 else
865 {
866 return false;
867 }
868 }
869 return false;
870}
871
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600872} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530873} // namespace responder
874} // namespace pldm