blob: 8b9684a18651f7d2024cbd3a663088824449c07f [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#pragma once
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/utils.hpp"
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05304#include "event_parser.hpp"
Tom Joseph33e9c7e2020-06-11 22:09:52 +05305#include "fru.hpp"
George Liucae18662020-05-15 09:32:57 +08006#include "host-bmc/dbus_to_event_handler.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -05007#include "host-bmc/host_pdr_handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05008#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08009#include "libpldmresponder/pdr_utils.hpp"
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060010#include "libpldmresponder/platform_config.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050011#include "oem_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050012#include "pldmd/handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050013
George Liuc453e162022-12-21 17:16:23 +080014#include <libpldm/pdr.h>
15#include <libpldm/platform.h>
16#include <libpldm/states.h>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053017
Riya Dixit49cfb132023-03-02 04:26:53 -060018#include <phosphor-logging/lg2.hpp>
19
Pavithra Barithayab3b84b42024-08-23 11:43:57 +053020#include <cstdint>
Sampa Misraa2fa0702019-05-31 01:28:55 -050021#include <map>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053022
Riya Dixit49cfb132023-03-02 04:26:53 -060023PHOSPHOR_LOG2_USING;
24
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053025namespace pldm
26{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053027namespace responder
28{
Sampa Misraa2fa0702019-05-31 01:28:55 -050029namespace platform
30{
Patrick Williams16c2a0a2024-08-16 15:20:59 -040031using generatePDR = std::function<void(
32 const pldm::utils::DBusHandler& dBusIntf, const pldm::utils::Json& json,
33 pdr_utils::RepoInterface& repo)>;
George Liua2870722020-02-11 11:09:30 +080034
George Liu1ec85d42020-02-12 16:05:32 +080035using EffecterId = uint16_t;
George Liua2870722020-02-11 11:09:30 +080036using DbusObjMaps =
37 std::map<EffecterId,
38 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>>;
Tom Joseph56e45c52020-03-16 10:01:45 +053039using DbusPath = std::string;
40using EffecterObjs = std::vector<DbusPath>;
41using EventType = uint8_t;
42using EventHandler = std::function<int(
43 const pldm_msg* request, size_t payloadLength, uint8_t formatVersion,
44 uint8_t tid, size_t eventDataOffset)>;
45using EventHandlers = std::vector<EventHandler>;
46using EventMap = std::map<EventType, EventHandlers>;
George Liuc4ea6a92020-07-14 15:48:44 +080047using AssociatedEntityMap = std::map<DbusPath, pldm_entity>;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060048
Pavithra Barithayae1978492025-04-24 16:30:55 +053049using ChangeEntry = uint32_t;
50
Deepak Kodihallibc669f12019-11-28 08:52:07 -060051class Handler : public CmdHandler
Sampa Misraa2fa0702019-05-31 01:28:55 -050052{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060053 public:
Sagar Srinivas90314a32023-10-17 10:38:03 -050054 Handler(const pldm::utils::DBusHandler* dBusIntf, uint8_t eid,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060055 pldm::InstanceIdDb* instanceIdDb, const fs::path& pdrJsonDir,
Sagar Srinivas90314a32023-10-17 10:38:03 -050056 pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
Brad Bishop5079ac42021-08-19 18:35:06 -040057 pldm::state_sensor::DbusToPLDMEvent* dbusToPLDMEventHandler,
58 fru::Handler* fruHandler,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060059 pldm::responder::platform_config::Handler* platformConfigHandler,
Sagar Srinivas90314a32023-10-17 10:38:03 -050060 pldm::requester::Handler<pldm::requester::Request>* handler,
Sampa Misra5fb37d52021-03-06 07:26:00 -060061 sdeventplus::Event& event, bool buildPDRLazily = false,
Tom Joseph56e45c52020-03-16 10:01:45 +053062 const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040063 eid(eid), instanceIdDb(instanceIdDb), pdrRepo(repo),
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060064 hostPDRHandler(hostPDRHandler),
George Liucae18662020-05-15 09:32:57 +080065 dbusToPLDMEventHandler(dbusToPLDMEventHandler), fruHandler(fruHandler),
George Liua881c172021-06-21 18:28:11 +080066 dBusIntf(dBusIntf), platformConfigHandler(platformConfigHandler),
67 handler(handler), event(event), pdrJsonDir(pdrJsonDir),
68 pdrCreated(false), pdrJsonsDir({pdrJsonDir})
Sampa Misraa2fa0702019-05-31 01:28:55 -050069 {
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050070 if (!buildPDRLazily)
71 {
Sampa Misra12afe112020-05-25 11:40:44 -050072 generateTerminusLocatorPDR(pdrRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050073 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
74 pdrCreated = true;
75 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -060076
Delphine CC Chiud2e48992023-12-05 16:29:51 +080077 handlers.emplace(
78 PLDM_GET_PDR,
79 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040080 return this->getPDR(request, payloadLength);
81 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080082 handlers.emplace(
83 PLDM_SET_NUMERIC_EFFECTER_VALUE,
84 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040085 return this->setNumericEffecterValue(request, payloadLength);
86 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080087 handlers.emplace(
88 PLDM_GET_NUMERIC_EFFECTER_VALUE,
89 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040090 return this->getNumericEffecterValue(request, payloadLength);
91 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080092 handlers.emplace(
93 PLDM_SET_STATE_EFFECTER_STATES,
94 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040095 return this->setStateEffecterStates(request, payloadLength);
96 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080097 handlers.emplace(
98 PLDM_PLATFORM_EVENT_MESSAGE,
99 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400100 return this->platformEventMessage(request, payloadLength);
101 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800102 handlers.emplace(
103 PLDM_GET_STATE_SENSOR_READINGS,
104 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400105 return this->getStateSensorReadings(request, payloadLength);
106 });
Tom Joseph56e45c52020-03-16 10:01:45 +0530107
108 // Default handler for PLDM Events
109 eventHandlers[PLDM_SENSOR_EVENT].emplace_back(
110 [this](const pldm_msg* request, size_t payloadLength,
111 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400112 return this->sensorEvent(request, payloadLength, formatVersion,
113 tid, eventDataOffset);
114 });
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500115 eventHandlers[PLDM_PDR_REPOSITORY_CHG_EVENT].emplace_back(
116 [this](const pldm_msg* request, size_t payloadLength,
117 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400118 return this->pldmPDRRepositoryChgEvent(
119 request, payloadLength, formatVersion, tid,
120 eventDataOffset);
121 });
Tom Joseph56e45c52020-03-16 10:01:45 +0530122
123 // Additional OEM event handlers for PLDM events, append it to the
124 // standard handlers
125 if (addOnHandlersMap)
126 {
127 auto addOnHandlers = addOnHandlersMap.value();
128 for (EventMap::iterator iter = addOnHandlers.begin();
129 iter != addOnHandlers.end(); ++iter)
130 {
131 auto search = eventHandlers.find(iter->first);
132 if (search != eventHandlers.end())
133 {
134 search->second.insert(std::end(search->second),
135 std::begin(iter->second),
136 std::end(iter->second));
137 }
138 else
139 {
140 eventHandlers.emplace(iter->first, iter->second);
141 }
142 }
143 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500144 }
145
George Liu1ec85d42020-02-12 16:05:32 +0800146 pdr_utils::Repo& getRepo()
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600147 {
George Liu1ec85d42020-02-12 16:05:32 +0800148 return this->pdrRepo;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600149 }
150
George Liu1ec85d42020-02-12 16:05:32 +0800151 /** @brief Add D-Bus mapping and value mapping(stateId to D-Bus) for the
George Liuadbe1722020-05-09 19:20:19 +0800152 * Id. If the same id is added, the previous dbusObjs will
George Liu1ec85d42020-02-12 16:05:32 +0800153 * be "over-written".
154 *
George Liuadbe1722020-05-09 19:20:19 +0800155 * @param[in] Id - effecter/sensor id
George Liu1ec85d42020-02-12 16:05:32 +0800156 * @param[in] dbusObj - list of D-Bus object structure and list of D-Bus
157 * property value to attribute value
George Liuadbe1722020-05-09 19:20:19 +0800158 * @param[in] typeId - the type id of enum
George Liu1ec85d42020-02-12 16:05:32 +0800159 */
George Liua2870722020-02-11 11:09:30 +0800160 void addDbusObjMaps(
George Liuadbe1722020-05-09 19:20:19 +0800161 uint16_t id,
162 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj,
Brad Bishop5079ac42021-08-19 18:35:06 -0400163 pldm::responder::pdr_utils::TypeId typeId =
164 pldm::responder::pdr_utils::TypeId::PLDM_EFFECTER_ID);
George Liu1ec85d42020-02-12 16:05:32 +0800165
George Liuadbe1722020-05-09 19:20:19 +0800166 /** @brief Retrieve an id -> D-Bus objects mapping
George Liu1ec85d42020-02-12 16:05:32 +0800167 *
George Liuadbe1722020-05-09 19:20:19 +0800168 * @param[in] Id - id
169 * @param[in] typeId - the type id of enum
George Liu1ec85d42020-02-12 16:05:32 +0800170 *
George Liua2870722020-02-11 11:09:30 +0800171 * @return std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> -
172 * list of D-Bus object structure and list of D-Bus property value
173 * to attribute value
George Liu1ec85d42020-02-12 16:05:32 +0800174 */
George Liua2870722020-02-11 11:09:30 +0800175 const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
Brad Bishop5079ac42021-08-19 18:35:06 -0400176 getDbusObjMaps(
177 uint16_t id,
178 pldm::responder::pdr_utils::TypeId typeId =
179 pldm::responder::pdr_utils::TypeId::PLDM_EFFECTER_ID) const;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600180
181 uint16_t getNextEffecterId()
182 {
183 return ++nextEffecterId;
184 }
185
George Liuadbe1722020-05-09 19:20:19 +0800186 uint16_t getNextSensorId()
187 {
188 return ++nextSensorId;
189 }
190
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600191 /** @brief Parse PDR JSONs and build PDR repository
192 *
George Liu36e81352020-07-01 14:40:30 +0800193 * @param[in] dBusIntf - The interface object
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600194 * @param[in] dir - directory housing platform specific PDR JSON files
195 * @param[in] repo - instance of concrete implementation of Repo
196 */
George Liu36e81352020-07-01 14:40:30 +0800197 void generate(const pldm::utils::DBusHandler& dBusIntf,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600198 const std::vector<fs::path>& dir,
Brad Bishop5079ac42021-08-19 18:35:06 -0400199 pldm::responder::pdr_utils::Repo& repo);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600200
201 /** @brief Parse PDR JSONs and build state effecter PDR repository
202 *
203 * @param[in] json - platform specific PDR JSON files
204 * @param[in] repo - instance of state effecter implementation of Repo
205 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400206 void generateStateEffecterRepo(const pldm::utils::Json& json,
207 pldm::responder::pdr_utils::Repo& repo);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600208
Tom Joseph56e45c52020-03-16 10:01:45 +0530209 /** @brief map of PLDM event type to EventHandlers
210 *
211 */
212 EventMap eventHandlers;
213
George Liua881c172021-06-21 18:28:11 +0800214 /* @brief Method to set the oem platform handler in platform handler class
215 *
216 * @param[in] handler - oem platform handler
217 */
Patrick Williams366507c2025-02-03 14:28:01 -0500218 inline void setOemPlatformHandler(
219 pldm::responder::oem_platform::Handler* handler)
George Liua881c172021-06-21 18:28:11 +0800220 {
221 oemPlatformHandler = handler;
222 }
223
Chau Lya743e382024-10-26 11:12:22 +0000224 /* @brief Method to register event handlers
225 *
226 * @param[in] handler - oem event handlers
227 */
228 inline void registerEventHandlers(EventType eventId, EventHandlers handlers)
229 {
230 // Try to emplace the eventId with an empty vector if it doesn't exist
231 auto [iter,
232 inserted] = eventHandlers.try_emplace(eventId, EventHandlers{});
233
234 for (const auto& handler : handlers)
235 {
236 iter->second.emplace_back(handler);
237 }
238 }
239
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600240 /** @brief Handler for GetPDR
241 *
242 * @param[in] request - Request message payload
243 * @param[in] payloadLength - Request payload length
244 * @param[out] Response - Response message written here
245 */
246 Response getPDR(const pldm_msg* request, size_t payloadLength);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247
George Liueccb0c52020-01-14 11:09:56 +0800248 /** @brief Handler for setNumericEffecterValue
249 *
250 * @param[in] request - Request message
251 * @param[in] payloadLength - Request payload length
252 * @return Response - PLDM Response message
253 */
254 Response setNumericEffecterValue(const pldm_msg* request,
255 size_t payloadLength);
256
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500257 /** @brief Handler for getNumericEffecterValue
258 *
259 * @param[in] request - Request message
260 * @param[in] payloadLength - Request payload length
261 * @return Response - PLDM Response message
262 */
263 Response getNumericEffecterValue(const pldm_msg* request,
264 size_t payloadLength);
265
George Liu362c18d2020-05-14 09:46:36 +0800266 /** @brief Handler for getStateSensorReadings
267 *
268 * @param[in] request - Request message
269 * @param[in] payloadLength - Request payload length
270 * @return Response - PLDM Response message
271 */
272 Response getStateSensorReadings(const pldm_msg* request,
273 size_t payloadLength);
274
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600275 /** @brief Handler for setStateEffecterStates
276 *
277 * @param[in] request - Request message
278 * @param[in] payloadLength - Request payload length
279 * @return Response - PLDM Response message
280 */
281 Response setStateEffecterStates(const pldm_msg* request,
282 size_t payloadLength);
283
Tom Joseph56e45c52020-03-16 10:01:45 +0530284 /** @brief Handler for PlatformEventMessage
285 *
286 * @param[in] request - Request message
287 * @param[in] payloadLength - Request payload length
288 * @return Response - PLDM Response message
289 */
290 Response platformEventMessage(const pldm_msg* request,
291 size_t payloadLength);
292
293 /** @brief Handler for event class Sensor event
294 *
295 * @param[in] request - Request message
296 * @param[in] payloadLength - Request payload length
297 * @param[in] formatVersion - Version of the event format
298 * @param[in] tid - Terminus ID of the event's originator
299 * @param[in] eventDataOffset - Offset of the event data in the request
300 * message
301 * @return PLDM completion code
302 */
303 int sensorEvent(const pldm_msg* request, size_t payloadLength,
304 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset);
305
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500306 /** @brief Handler for pldmPDRRepositoryChgEvent
307 *
308 * @param[in] request - Request message
309 * @param[in] payloadLength - Request payload length
310 * @param[in] formatVersion - Version of the event format
311 * @param[in] tid - Terminus ID of the event's originator
312 * @param[in] eventDataOffset - Offset of the event data in the request
313 * message
314 * @return PLDM completion code
315 */
316 int pldmPDRRepositoryChgEvent(const pldm_msg* request, size_t payloadLength,
317 uint8_t formatVersion, uint8_t tid,
318 size_t eventDataOffset);
319
320 /** @brief Handler for extracting the PDR handles from changeEntries
321 *
322 * @param[in] changeEntryData - ChangeEntry data from changeRecord
323 * @param[in] changeEntryDataSize - total size of changeEntryData
324 * @param[in] numberOfChangeEntries - total number of changeEntries to
325 * extract
326 * @param[out] pdrRecordHandles - std::vector where the extracted PDR
327 * handles are placed
328 * @return PLDM completion code
329 */
330 int getPDRRecordHandles(const ChangeEntry* changeEntryData,
331 size_t changeEntryDataSize,
332 size_t numberOfChangeEntries,
333 PDRRecordHandles& pdrRecordHandles);
334
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600335 /** @brief Function to set the effecter requested by pldm requester
336 * @param[in] dBusIntf - The interface object
337 * @param[in] effecterId - Effecter ID sent by the requester to act on
338 * @param[in] stateField - The state field data for each of the states,
339 * equal to composite effecter count in number
340 * @return - Success or failure in setting the states. Returns failure in
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530341 * terms of PLDM completion codes if at least one state fails to be set
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600342 */
343 template <class DBusInterface>
344 int setStateEffecterStatesHandler(
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600345 const DBusInterface& dBusIntf, uint16_t effecterId,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600346 const std::vector<set_effecter_state_field>& stateField)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500347 {
George Liue53193f2020-02-24 09:23:26 +0800348 using namespace pldm::responder::pdr;
George Liu1e44c732020-02-28 20:20:06 +0800349 using namespace pldm::utils;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600350 using StateSetNum = uint8_t;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600351
352 state_effecter_possible_states* states = nullptr;
353 pldm_state_effecter_pdr* pdr = nullptr;
354 uint8_t compEffecterCnt = stateField.size();
George Liu1ec85d42020-02-12 16:05:32 +0800355
356 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)>
357 stateEffecterPdrRepo(pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930358 if (!stateEffecterPdrRepo)
359 {
360 throw std::runtime_error(
361 "Failed to instantiate state effecter PDR repository");
362 }
Brad Bishop5079ac42021-08-19 18:35:06 -0400363 pldm::responder::pdr_utils::Repo stateEffecterPDRs(
364 stateEffecterPdrRepo.get());
George Liu1ec85d42020-02-12 16:05:32 +0800365 getRepoByType(pdrRepo, stateEffecterPDRs, PLDM_STATE_EFFECTER_PDR);
366 if (stateEffecterPDRs.empty())
367 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600368 error("Failed to get record by PDR type");
George Liu1ec85d42020-02-12 16:05:32 +0800369 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
370 }
371
Brad Bishop5079ac42021-08-19 18:35:06 -0400372 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liu1ec85d42020-02-12 16:05:32 +0800373 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800374 while (pdrRecord)
375 {
Pavithra Barithaya677a4552025-01-31 10:33:39 +0530376 pdr = new (pdrEntry.data) pldm_state_effecter_pdr;
George Liue53193f2020-02-24 09:23:26 +0800377 if (pdr->effecter_id != effecterId)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500378 {
George Liue53193f2020-02-24 09:23:26 +0800379 pdr = nullptr;
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400380 pdrRecord =
381 stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800382 continue;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600383 }
George Liue53193f2020-02-24 09:23:26 +0800384
385 states = reinterpret_cast<state_effecter_possible_states*>(
386 pdr->possible_states);
387 if (compEffecterCnt > pdr->composite_effecter_count)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600388 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600389 error(
Riya Dixit89644442024-03-31 05:39:59 -0500390 "The requester sent wrong composite effecter count '{COMPOSITE_EFFECTER_COUNT}' for the effecter ID '{EFFECTERID}'.",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500391 "COMPOSITE_EFFECTER_COUNT", compEffecterCnt, "EFFECTERID",
392 effecterId);
George Liue53193f2020-02-24 09:23:26 +0800393 return PLDM_ERROR_INVALID_DATA;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500394 }
George Liue53193f2020-02-24 09:23:26 +0800395 break;
396 }
397
398 if (!pdr)
399 {
400 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500401 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500402
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600403 int rc = PLDM_SUCCESS;
George Liu1ec85d42020-02-12 16:05:32 +0800404 try
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600405 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400406 const auto& [dbusMappings, dbusValMaps] =
407 effecterDbusObjMaps.at(effecterId);
George Liu1ec85d42020-02-12 16:05:32 +0800408 for (uint8_t currState = 0; currState < compEffecterCnt;
409 ++currState)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600410 {
George Liu1ec85d42020-02-12 16:05:32 +0800411 std::vector<StateSetNum> allowed{};
412 // computation is based on table 79 from DSP0248 v1.1.1
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400413 uint8_t bitfieldIndex =
414 stateField[currState].effecter_state / 8;
Patrick Williams6da4f912023-05-10 07:50:53 -0500415 uint8_t bit = stateField[currState].effecter_state -
416 (8 * bitfieldIndex);
George Liu1ec85d42020-02-12 16:05:32 +0800417 if (states->possible_states_size < bitfieldIndex ||
418 !(states->states[bitfieldIndex].byte & (1 << bit)))
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600419 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600420 error(
Riya Dixit89644442024-03-31 05:39:59 -0500421 "Invalid state set value for effecter ID '{EFFECTERID}', effecter state '{EFFECTER_STATE}', composite effecter ID '{COMPOSITE_EFFECTER_ID}' and path '{PATH}'.",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500422 "EFFECTERID", effecterId, "EFFECTER_STATE",
423 stateField[currState].effecter_state,
424 "COMPOSITE_EFFECTER_COUNT", currState, "PATH",
425 dbusMappings[currState].objectPath);
George Liu1ec85d42020-02-12 16:05:32 +0800426 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600427 break;
428 }
George Liu1ec85d42020-02-12 16:05:32 +0800429 const DBusMapping& dbusMapping = dbusMappings[currState];
Brad Bishop5079ac42021-08-19 18:35:06 -0400430 const pldm::responder::pdr_utils::StatestoDbusVal&
431 dbusValToMap = dbusValMaps[currState];
George Liu1ec85d42020-02-12 16:05:32 +0800432
433 if (stateField[currState].set_request == PLDM_REQUEST_SET)
434 {
435 try
436 {
437 dBusIntf.setDbusProperty(
438 dbusMapping,
439 dbusValToMap.at(
440 stateField[currState].effecter_state));
441 }
442 catch (const std::exception& e)
443 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600444 error(
Riya Dixit89644442024-03-31 05:39:59 -0500445 "Failed to set property '{PROPERTY}' of interface '{INTERFACE}' at path '{PATH}', error - {ERROR}",
446 "PROPERTY", dbusMapping.propertyName, "DBUS_INTF",
Riya Dixit49cfb132023-03-02 04:26:53 -0600447 dbusMapping.interface, "DBUS_OBJ_PATH",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500448 dbusMapping.objectPath, "ERROR", e);
George Liu1ec85d42020-02-12 16:05:32 +0800449 return PLDM_ERROR;
450 }
451 }
452 uint8_t* nextState =
453 reinterpret_cast<uint8_t*>(states) +
454 sizeof(state_effecter_possible_states) -
455 sizeof(states->states) +
456 (states->possible_states_size * sizeof(states->states));
457 states = reinterpret_cast<state_effecter_possible_states*>(
458 nextState);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600459 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600460 }
George Liu1ec85d42020-02-12 16:05:32 +0800461 catch (const std::out_of_range& e)
462 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600463 error(
Riya Dixit89644442024-03-31 05:39:59 -0500464 "The effecter ID '{EFFECTERID}' does not exist, error - {ERROR}.",
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500465 "EFFECTERID", effecterId, "ERROR", e);
George Liu1ec85d42020-02-12 16:05:32 +0800466 }
467
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600468 return rc;
469 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600470
Sampa Misra12afe112020-05-25 11:40:44 -0500471 /** @brief Build BMC Terminus Locator PDR
472 *
473 * @param[in] repo - instance of concrete implementation of Repo
474 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400475 void generateTerminusLocatorPDR(pldm::responder::pdr_utils::Repo& repo);
Sampa Misra12afe112020-05-25 11:40:44 -0500476
George Liuc4ea6a92020-07-14 15:48:44 +0800477 /** @brief Get std::map associated with the entity
478 * key: object path
479 * value: pldm_entity
480 *
481 * @return std::map<ObjectPath, pldm_entity>
482 */
483 inline const AssociatedEntityMap& getAssociateEntityMap() const
484 {
485 if (fruHandler == nullptr)
486 {
487 throw InternalFailure();
488 }
489 return fruHandler->getAssociateEntityMap();
490 }
491
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530492 /** @brief update the sensor cache map
493 * @param[in] sensorId - sensor id that needs an update
494 * @param[in] sensorRearm - rearm value within the sensor
495 * @param[in] value - value that needs to be cached
496 */
497
498 inline void updateSensorCache(pldm::pdr::SensorID sensorId,
499 size_t sensorRearm, uint8_t value)
500 {
501 if (dbusToPLDMEventHandler)
502 {
503 dbusToPLDMEventHandler->updateSensorCacheMaps(sensorId, sensorRearm,
504 value);
505 }
506 }
507
Sampa Misra5fb37d52021-03-06 07:26:00 -0600508 /** @brief process the actions that needs to be performed after a GetPDR
509 * call is received
510 * @param[in] source - sdeventplus event source
511 */
512 void _processPostGetPDRActions(sdeventplus::source::EventBase& source);
513
Sagar Srinivas90314a32023-10-17 10:38:03 -0500514 /** @brief Method for setEventreceiver */
515 void setEventReceiver();
516
Pavithra Barithayae1978492025-04-24 16:30:55 +0530517 /* @brief Send a PLDM event to host firmware containing a list of record
518 * handles of PDRs that the host firmware has to fetch.
519 *
520 * @param[in] pdrRecordHandles - list of PDR record handles
521 * @param[in] eventDataOps - event data operation for PDRRepositoryChgEvent
522 * as in DSP0248 SPEC
523 */
524 void sendPDRRepositoryChgEventbyPDRHandles(
525 const std::vector<uint32_t>& pdrRecordHandles,
526 const std::vector<uint8_t>& eventDataOps);
527
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600528 private:
Sagar Srinivas90314a32023-10-17 10:38:03 -0500529 uint8_t eid;
530 InstanceIdDb* instanceIdDb;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600531 pdr_utils::Repo pdrRepo;
532 uint16_t nextEffecterId{};
George Liuadbe1722020-05-09 19:20:19 +0800533 uint16_t nextSensorId{};
534 DbusObjMaps effecterDbusObjMaps{};
535 DbusObjMaps sensorDbusObjMaps{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500536 HostPDRHandler* hostPDRHandler;
Brad Bishop5079ac42021-08-19 18:35:06 -0400537 pldm::state_sensor::DbusToPLDMEvent* dbusToPLDMEventHandler;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530538 fru::Handler* fruHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500539 const pldm::utils::DBusHandler* dBusIntf;
George Liua881c172021-06-21 18:28:11 +0800540 pldm::responder::oem_platform::Handler* oemPlatformHandler = nullptr;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600541 pldm::responder::platform_config::Handler* platformConfigHandler;
Sagar Srinivas90314a32023-10-17 10:38:03 -0500542 pldm::requester::Handler<pldm::requester::Request>* handler;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600543 sdeventplus::Event& event;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600544 fs::path pdrJsonDir;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500545 bool pdrCreated;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600546 std::vector<fs::path> pdrJsonsDir;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600547 std::unique_ptr<sdeventplus::source::Defer> deferredGetPDREvent;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600548};
549
Sampa Misraaea5dde2020-08-31 08:33:47 -0500550/** @brief Function to check if a sensor falls in OEM range
551 * A sensor is considered to be oem if either of entity
552 * type or state set or both falls in oem range
553 *
554 * @param[in] handler - the interface object
555 * @param[in] sensorId - sensor id
556 * @param[in] sensorRearmCount - sensor rearm count
557 * @param[out] compSensorCnt - composite sensor count
558 * @param[out] entityType - entity type
559 * @param[out] entityInstance - entity instance number
560 * @param[out] stateSetId - state set id
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530561 * @param[out] containerId - container id
Sampa Misraaea5dde2020-08-31 08:33:47 -0500562 *
563 * @return true if the sensor is OEM. All out parameters are invalid
564 * for a non OEM sensor
565 */
566bool isOemStateSensor(Handler& handler, uint16_t sensorId,
567 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
568 uint16_t& entityType, uint16_t& entityInstance,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530569 uint16_t& stateSetId, uint16_t& containerId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500570
571/** @brief Function to check if an effecter falls in OEM range
572 * An effecter is considered to be oem if either of entity
573 * type or state set or both falls in oem range
574 *
575 * @param[in] handler - the interface object
576 * @param[in] effecterId - effecter id
577 * @param[in] compEffecterCnt - composite effecter count
578 * @param[out] entityType - entity type
579 * @param[out] entityInstance - entity instance number
580 * @param[out] stateSetId - state set id
581 *
582 * @return true if the effecter is OEM. All out parameters are invalid
583 * for a non OEM effecter
584 */
585bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
586 uint8_t compEffecterCnt, uint16_t& entityType,
587 uint16_t& entityInstance, uint16_t& stateSetId);
588
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600589} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530590} // namespace responder
591} // namespace pldm