blob: b50a1f439b654ffb80cd486bef12b8b96bb1fdc0 [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#include <stdint.h>
18
Riya Dixit49cfb132023-03-02 04:26:53 -060019#include <phosphor-logging/lg2.hpp>
20
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{
Brad Bishop5079ac42021-08-19 18:35:06 -040031using generatePDR = std::function<void(const pldm::utils::DBusHandler& dBusIntf,
32 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
Deepak Kodihallibc669f12019-11-28 08:52:07 -060049class Handler : public CmdHandler
Sampa Misraa2fa0702019-05-31 01:28:55 -050050{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060051 public:
Sagar Srinivas90314a32023-10-17 10:38:03 -050052 Handler(const pldm::utils::DBusHandler* dBusIntf, uint8_t eid,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060053 pldm::InstanceIdDb* instanceIdDb, const fs::path& pdrJsonDir,
Sagar Srinivas90314a32023-10-17 10:38:03 -050054 pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
Brad Bishop5079ac42021-08-19 18:35:06 -040055 pldm::state_sensor::DbusToPLDMEvent* dbusToPLDMEventHandler,
56 fru::Handler* fruHandler,
Sampa Misraaea5dde2020-08-31 08:33:47 -050057 pldm::responder::oem_platform::Handler* oemPlatformHandler,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060058 pldm::responder::platform_config::Handler* platformConfigHandler,
Sagar Srinivas90314a32023-10-17 10:38:03 -050059 pldm::requester::Handler<pldm::requester::Request>* handler,
Sampa Misra5fb37d52021-03-06 07:26:00 -060060 sdeventplus::Event& event, bool buildPDRLazily = false,
Tom Joseph56e45c52020-03-16 10:01:45 +053061 const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
Sagar Srinivas90314a32023-10-17 10:38:03 -050062 eid(eid),
63 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),
Sampa Misraaea5dde2020-08-31 08:33:47 -050066 dBusIntf(dBusIntf), oemPlatformHandler(oemPlatformHandler),
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060067 platformConfigHandler(platformConfigHandler), handler(handler),
68 event(event), pdrJsonDir(pdrJsonDir), pdrCreated(false),
69 pdrJsonsDir({pdrJsonDir})
Sampa Misraa2fa0702019-05-31 01:28:55 -050070 {
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050071 if (!buildPDRLazily)
72 {
Sampa Misra12afe112020-05-25 11:40:44 -050073 generateTerminusLocatorPDR(pdrRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050074 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
75 pdrCreated = true;
76 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -060077
Delphine CC Chiud2e48992023-12-05 16:29:51 +080078 handlers.emplace(
79 PLDM_GET_PDR,
80 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050081 return this->getPDR(request, payloadLength);
82 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080083 handlers.emplace(
84 PLDM_SET_NUMERIC_EFFECTER_VALUE,
85 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050086 return this->setNumericEffecterValue(request, payloadLength);
87 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080088 handlers.emplace(
89 PLDM_GET_NUMERIC_EFFECTER_VALUE,
90 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Archana Kakani6ece21fb2023-10-10 08:21:52 -050091 return this->getNumericEffecterValue(request, payloadLength);
92 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080093 handlers.emplace(
94 PLDM_SET_STATE_EFFECTER_STATES,
95 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -050096 return this->setStateEffecterStates(request, payloadLength);
97 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +080098 handlers.emplace(
99 PLDM_PLATFORM_EVENT_MESSAGE,
100 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500101 return this->platformEventMessage(request, payloadLength);
102 });
Delphine CC Chiud2e48992023-12-05 16:29:51 +0800103 handlers.emplace(
104 PLDM_GET_STATE_SENSOR_READINGS,
105 [this](pldm_tid_t, const pldm_msg* request, size_t payloadLength) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500106 return this->getStateSensorReadings(request, payloadLength);
107 });
Tom Joseph56e45c52020-03-16 10:01:45 +0530108
109 // Default handler for PLDM Events
110 eventHandlers[PLDM_SENSOR_EVENT].emplace_back(
111 [this](const pldm_msg* request, size_t payloadLength,
112 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500113 return this->sensorEvent(request, payloadLength, formatVersion, tid,
114 eventDataOffset);
115 });
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500116 eventHandlers[PLDM_PDR_REPOSITORY_CHG_EVENT].emplace_back(
117 [this](const pldm_msg* request, size_t payloadLength,
118 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500119 return this->pldmPDRRepositoryChgEvent(
120 request, payloadLength, formatVersion, tid, 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
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600214 /** @brief Handler for GetPDR
215 *
216 * @param[in] request - Request message payload
217 * @param[in] payloadLength - Request payload length
218 * @param[out] Response - Response message written here
219 */
220 Response getPDR(const pldm_msg* request, size_t payloadLength);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500221
George Liueccb0c52020-01-14 11:09:56 +0800222 /** @brief Handler for setNumericEffecterValue
223 *
224 * @param[in] request - Request message
225 * @param[in] payloadLength - Request payload length
226 * @return Response - PLDM Response message
227 */
228 Response setNumericEffecterValue(const pldm_msg* request,
229 size_t payloadLength);
230
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500231 /** @brief Handler for getNumericEffecterValue
232 *
233 * @param[in] request - Request message
234 * @param[in] payloadLength - Request payload length
235 * @return Response - PLDM Response message
236 */
237 Response getNumericEffecterValue(const pldm_msg* request,
238 size_t payloadLength);
239
George Liu362c18d2020-05-14 09:46:36 +0800240 /** @brief Handler for getStateSensorReadings
241 *
242 * @param[in] request - Request message
243 * @param[in] payloadLength - Request payload length
244 * @return Response - PLDM Response message
245 */
246 Response getStateSensorReadings(const pldm_msg* request,
247 size_t payloadLength);
248
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600249 /** @brief Handler for setStateEffecterStates
250 *
251 * @param[in] request - Request message
252 * @param[in] payloadLength - Request payload length
253 * @return Response - PLDM Response message
254 */
255 Response setStateEffecterStates(const pldm_msg* request,
256 size_t payloadLength);
257
Tom Joseph56e45c52020-03-16 10:01:45 +0530258 /** @brief Handler for PlatformEventMessage
259 *
260 * @param[in] request - Request message
261 * @param[in] payloadLength - Request payload length
262 * @return Response - PLDM Response message
263 */
264 Response platformEventMessage(const pldm_msg* request,
265 size_t payloadLength);
266
267 /** @brief Handler for event class Sensor event
268 *
269 * @param[in] request - Request message
270 * @param[in] payloadLength - Request payload length
271 * @param[in] formatVersion - Version of the event format
272 * @param[in] tid - Terminus ID of the event's originator
273 * @param[in] eventDataOffset - Offset of the event data in the request
274 * message
275 * @return PLDM completion code
276 */
277 int sensorEvent(const pldm_msg* request, size_t payloadLength,
278 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset);
279
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500280 /** @brief Handler for pldmPDRRepositoryChgEvent
281 *
282 * @param[in] request - Request message
283 * @param[in] payloadLength - Request payload length
284 * @param[in] formatVersion - Version of the event format
285 * @param[in] tid - Terminus ID of the event's originator
286 * @param[in] eventDataOffset - Offset of the event data in the request
287 * message
288 * @return PLDM completion code
289 */
290 int pldmPDRRepositoryChgEvent(const pldm_msg* request, size_t payloadLength,
291 uint8_t formatVersion, uint8_t tid,
292 size_t eventDataOffset);
293
294 /** @brief Handler for extracting the PDR handles from changeEntries
295 *
296 * @param[in] changeEntryData - ChangeEntry data from changeRecord
297 * @param[in] changeEntryDataSize - total size of changeEntryData
298 * @param[in] numberOfChangeEntries - total number of changeEntries to
299 * extract
300 * @param[out] pdrRecordHandles - std::vector where the extracted PDR
301 * handles are placed
302 * @return PLDM completion code
303 */
304 int getPDRRecordHandles(const ChangeEntry* changeEntryData,
305 size_t changeEntryDataSize,
306 size_t numberOfChangeEntries,
307 PDRRecordHandles& pdrRecordHandles);
308
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600309 /** @brief Function to set the effecter requested by pldm requester
310 * @param[in] dBusIntf - The interface object
311 * @param[in] effecterId - Effecter ID sent by the requester to act on
312 * @param[in] stateField - The state field data for each of the states,
313 * equal to composite effecter count in number
314 * @return - Success or failure in setting the states. Returns failure in
315 * terms of PLDM completion codes if atleast one state fails to be set
316 */
317 template <class DBusInterface>
318 int setStateEffecterStatesHandler(
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600319 const DBusInterface& dBusIntf, uint16_t effecterId,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600320 const std::vector<set_effecter_state_field>& stateField)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500321 {
George Liue53193f2020-02-24 09:23:26 +0800322 using namespace pldm::responder::pdr;
George Liu1e44c732020-02-28 20:20:06 +0800323 using namespace pldm::utils;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600324 using StateSetNum = uint8_t;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600325
326 state_effecter_possible_states* states = nullptr;
327 pldm_state_effecter_pdr* pdr = nullptr;
328 uint8_t compEffecterCnt = stateField.size();
George Liu1ec85d42020-02-12 16:05:32 +0800329
330 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)>
331 stateEffecterPdrRepo(pldm_pdr_init(), pldm_pdr_destroy);
Andrew Jefferyacb20292023-06-30 11:47:44 +0930332 if (!stateEffecterPdrRepo)
333 {
334 throw std::runtime_error(
335 "Failed to instantiate state effecter PDR repository");
336 }
Brad Bishop5079ac42021-08-19 18:35:06 -0400337 pldm::responder::pdr_utils::Repo stateEffecterPDRs(
338 stateEffecterPdrRepo.get());
George Liu1ec85d42020-02-12 16:05:32 +0800339 getRepoByType(pdrRepo, stateEffecterPDRs, PLDM_STATE_EFFECTER_PDR);
340 if (stateEffecterPDRs.empty())
341 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600342 error("Failed to get record by PDR type");
George Liu1ec85d42020-02-12 16:05:32 +0800343 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
344 }
345
Brad Bishop5079ac42021-08-19 18:35:06 -0400346 pldm::responder::pdr_utils::PdrEntry pdrEntry{};
George Liu1ec85d42020-02-12 16:05:32 +0800347 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800348 while (pdrRecord)
349 {
350 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
351 if (pdr->effecter_id != effecterId)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500352 {
George Liue53193f2020-02-24 09:23:26 +0800353 pdr = nullptr;
Patrick Williams6da4f912023-05-10 07:50:53 -0500354 pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord,
355 pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800356 continue;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600357 }
George Liue53193f2020-02-24 09:23:26 +0800358
359 states = reinterpret_cast<state_effecter_possible_states*>(
360 pdr->possible_states);
361 if (compEffecterCnt > pdr->composite_effecter_count)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600362 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600363 error(
364 "The requester sent wrong composite effecter count for the effecter, EFFECTER_ID={EFFECTER_ID} COMP_EFF_CNT={COMP_EFF_CNT}",
365 "EFFECTER_ID", (unsigned)effecterId, "COMP_EFF_CNT",
366 (unsigned)compEffecterCnt);
George Liue53193f2020-02-24 09:23:26 +0800367 return PLDM_ERROR_INVALID_DATA;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500368 }
George Liue53193f2020-02-24 09:23:26 +0800369 break;
370 }
371
372 if (!pdr)
373 {
374 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500375 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500376
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600377 int rc = PLDM_SUCCESS;
George Liu1ec85d42020-02-12 16:05:32 +0800378 try
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600379 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500380 const auto& [dbusMappings,
381 dbusValMaps] = effecterDbusObjMaps.at(effecterId);
George Liu1ec85d42020-02-12 16:05:32 +0800382 for (uint8_t currState = 0; currState < compEffecterCnt;
383 ++currState)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600384 {
George Liu1ec85d42020-02-12 16:05:32 +0800385 std::vector<StateSetNum> allowed{};
386 // computation is based on table 79 from DSP0248 v1.1.1
Patrick Williams6da4f912023-05-10 07:50:53 -0500387 uint8_t bitfieldIndex = stateField[currState].effecter_state /
388 8;
389 uint8_t bit = stateField[currState].effecter_state -
390 (8 * bitfieldIndex);
George Liu1ec85d42020-02-12 16:05:32 +0800391 if (states->possible_states_size < bitfieldIndex ||
392 !(states->states[bitfieldIndex].byte & (1 << bit)))
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600393 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600394 error(
395 "Invalid state set value, EFFECTER_ID={EFFECTER_ID} VALUE={EFFECTER_STATE} COMPOSITE_EFFECTER_ID={CURR_STATE} DBUS_PATH={DBUS_OBJ_PATH}",
396 "EFFECTER_ID", (unsigned)effecterId, "EFFECTER_STATE",
397 (unsigned)stateField[currState].effecter_state,
398 "CURR_STATE", (unsigned)currState, "DBUS_OBJ_PATH",
399 dbusMappings[currState].objectPath.c_str());
George Liu1ec85d42020-02-12 16:05:32 +0800400 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600401 break;
402 }
George Liu1ec85d42020-02-12 16:05:32 +0800403 const DBusMapping& dbusMapping = dbusMappings[currState];
Brad Bishop5079ac42021-08-19 18:35:06 -0400404 const pldm::responder::pdr_utils::StatestoDbusVal&
405 dbusValToMap = dbusValMaps[currState];
George Liu1ec85d42020-02-12 16:05:32 +0800406
407 if (stateField[currState].set_request == PLDM_REQUEST_SET)
408 {
409 try
410 {
411 dBusIntf.setDbusProperty(
412 dbusMapping,
413 dbusValToMap.at(
414 stateField[currState].effecter_state));
415 }
416 catch (const std::exception& e)
417 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600418 error(
419 "Error setting property, ERROR={ERR_EXCEP} PROPERTY={DBUS_PROP} INTERFACE={DBUS_INTF} PATH={DBUS_OBJ_PATH}",
420 "ERR_EXCEP", e.what(), "DBUS_PROP",
421 dbusMapping.propertyName, "DBUS_INTF",
422 dbusMapping.interface, "DBUS_OBJ_PATH",
423 dbusMapping.objectPath.c_str());
George Liu1ec85d42020-02-12 16:05:32 +0800424 return PLDM_ERROR;
425 }
426 }
427 uint8_t* nextState =
428 reinterpret_cast<uint8_t*>(states) +
429 sizeof(state_effecter_possible_states) -
430 sizeof(states->states) +
431 (states->possible_states_size * sizeof(states->states));
432 states = reinterpret_cast<state_effecter_possible_states*>(
433 nextState);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600434 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600435 }
George Liu1ec85d42020-02-12 16:05:32 +0800436 catch (const std::out_of_range& e)
437 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600438 error(
439 "the effecterId does not exist. effecter id: {EFFECTER_ID} {ERR_EXCEP}",
440 "EFFECTER_ID", (unsigned)effecterId, "ERR_EXCEP", e.what());
George Liu1ec85d42020-02-12 16:05:32 +0800441 }
442
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600443 return rc;
444 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600445
Sampa Misra12afe112020-05-25 11:40:44 -0500446 /** @brief Build BMC Terminus Locator PDR
447 *
448 * @param[in] repo - instance of concrete implementation of Repo
449 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400450 void generateTerminusLocatorPDR(pldm::responder::pdr_utils::Repo& repo);
Sampa Misra12afe112020-05-25 11:40:44 -0500451
George Liuc4ea6a92020-07-14 15:48:44 +0800452 /** @brief Get std::map associated with the entity
453 * key: object path
454 * value: pldm_entity
455 *
456 * @return std::map<ObjectPath, pldm_entity>
457 */
458 inline const AssociatedEntityMap& getAssociateEntityMap() const
459 {
460 if (fruHandler == nullptr)
461 {
462 throw InternalFailure();
463 }
464 return fruHandler->getAssociateEntityMap();
465 }
466
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530467 /** @brief update the sensor cache map
468 * @param[in] sensorId - sensor id that needs an update
469 * @param[in] sensorRearm - rearm value within the sensor
470 * @param[in] value - value that needs to be cached
471 */
472
473 inline void updateSensorCache(pldm::pdr::SensorID sensorId,
474 size_t sensorRearm, uint8_t value)
475 {
476 if (dbusToPLDMEventHandler)
477 {
478 dbusToPLDMEventHandler->updateSensorCacheMaps(sensorId, sensorRearm,
479 value);
480 }
481 }
482
Sampa Misra5fb37d52021-03-06 07:26:00 -0600483 /** @brief process the actions that needs to be performed after a GetPDR
484 * call is received
485 * @param[in] source - sdeventplus event source
486 */
487 void _processPostGetPDRActions(sdeventplus::source::EventBase& source);
488
Sagar Srinivas90314a32023-10-17 10:38:03 -0500489 /** @brief Method for setEventreceiver */
490 void setEventReceiver();
491
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600492 private:
Sagar Srinivas90314a32023-10-17 10:38:03 -0500493 uint8_t eid;
494 InstanceIdDb* instanceIdDb;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600495 pdr_utils::Repo pdrRepo;
496 uint16_t nextEffecterId{};
George Liuadbe1722020-05-09 19:20:19 +0800497 uint16_t nextSensorId{};
498 DbusObjMaps effecterDbusObjMaps{};
499 DbusObjMaps sensorDbusObjMaps{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500500 HostPDRHandler* hostPDRHandler;
Brad Bishop5079ac42021-08-19 18:35:06 -0400501 pldm::state_sensor::DbusToPLDMEvent* dbusToPLDMEventHandler;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530502 fru::Handler* fruHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500503 const pldm::utils::DBusHandler* dBusIntf;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500504 pldm::responder::oem_platform::Handler* oemPlatformHandler;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600505 pldm::responder::platform_config::Handler* platformConfigHandler;
Sagar Srinivas90314a32023-10-17 10:38:03 -0500506 pldm::requester::Handler<pldm::requester::Request>* handler;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600507 sdeventplus::Event& event;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600508 fs::path pdrJsonDir;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500509 bool pdrCreated;
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600510 std::vector<fs::path> pdrJsonsDir;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600511 std::unique_ptr<sdeventplus::source::Defer> deferredGetPDREvent;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600512};
513
Sampa Misraaea5dde2020-08-31 08:33:47 -0500514/** @brief Function to check if a sensor falls in OEM range
515 * A sensor is considered to be oem if either of entity
516 * type or state set or both falls in oem range
517 *
518 * @param[in] handler - the interface object
519 * @param[in] sensorId - sensor id
520 * @param[in] sensorRearmCount - sensor rearm count
521 * @param[out] compSensorCnt - composite sensor count
522 * @param[out] entityType - entity type
523 * @param[out] entityInstance - entity instance number
524 * @param[out] stateSetId - state set id
525 *
526 * @return true if the sensor is OEM. All out parameters are invalid
527 * for a non OEM sensor
528 */
529bool isOemStateSensor(Handler& handler, uint16_t sensorId,
530 uint8_t sensorRearmCount, uint8_t& compSensorCnt,
531 uint16_t& entityType, uint16_t& entityInstance,
532 uint16_t& stateSetId);
533
534/** @brief Function to check if an effecter falls in OEM range
535 * An effecter is considered to be oem if either of entity
536 * type or state set or both falls in oem range
537 *
538 * @param[in] handler - the interface object
539 * @param[in] effecterId - effecter id
540 * @param[in] compEffecterCnt - composite effecter count
541 * @param[out] entityType - entity type
542 * @param[out] entityInstance - entity instance number
543 * @param[out] stateSetId - state set id
544 *
545 * @return true if the effecter is OEM. All out parameters are invalid
546 * for a non OEM effecter
547 */
548bool isOemStateEffecter(Handler& handler, uint16_t effecterId,
549 uint8_t compEffecterCnt, uint16_t& entityType,
550 uint16_t& entityInstance, uint16_t& stateSetId);
551
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600552} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530553} // namespace responder
554} // namespace pldm