blob: 1ff6bc4a2faa8eb58431ccf0e80f7663fb70f169 [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#pragma once
2
Sampa Misraa2fa0702019-05-31 01:28:55 -05003#include "config.h"
4
George Liu6492f522020-06-16 10:34:05 +08005#include "libpldm/platform.h"
6#include "libpldm/states.h"
7
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05008#include "common/utils.hpp"
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05309#include "event_parser.hpp"
Tom Joseph33e9c7e2020-06-11 22:09:52 +053010#include "fru.hpp"
Deepak Kodihalliac19bd62020-06-16 08:25:23 -050011#include "host-bmc/host_pdr_handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050012#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +080013#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050014#include "pldmd/handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -050015
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053016#include <stdint.h>
17
Sampa Misraa2fa0702019-05-31 01:28:55 -050018#include <map>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053019
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053020namespace pldm
21{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053022namespace responder
23{
Sampa Misraa2fa0702019-05-31 01:28:55 -050024namespace platform
25{
26
George Liu1ec85d42020-02-12 16:05:32 +080027using namespace pldm::utils;
28using namespace pldm::responder::pdr_utils;
29
George Liua2870722020-02-11 11:09:30 +080030using generatePDR =
George Liu36e81352020-07-01 14:40:30 +080031 std::function<void(const pldm::utils::DBusHandler& dBusIntf,
32 const Json& json, pdr_utils::RepoInterface& repo)>;
George Liua2870722020-02-11 11:09:30 +080033
George Liu1ec85d42020-02-12 16:05:32 +080034using EffecterId = uint16_t;
George Liua2870722020-02-11 11:09:30 +080035using DbusObjMaps =
36 std::map<EffecterId,
37 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>>;
Tom Joseph56e45c52020-03-16 10:01:45 +053038using DbusPath = std::string;
39using EffecterObjs = std::vector<DbusPath>;
40using EventType = uint8_t;
41using EventHandler = std::function<int(
42 const pldm_msg* request, size_t payloadLength, uint8_t formatVersion,
43 uint8_t tid, size_t eventDataOffset)>;
44using EventHandlers = std::vector<EventHandler>;
45using EventMap = std::map<EventType, EventHandlers>;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060046
Zahed Hossain75330f32020-03-24 02:15:03 -050047// EventEntry = <uint8_t> - EventState <uint8_t> - SensorOffset <uint16_t> -
48// SensorID
49using EventEntry = uint32_t;
50struct DBusInfo
51{
52 pldm::utils::DBusMapping dBusValues;
53 pldm::utils::PropertyValue dBusPropertyValue;
54};
55
Deepak Kodihallibc669f12019-11-28 08:52:07 -060056class Handler : public CmdHandler
Sampa Misraa2fa0702019-05-31 01:28:55 -050057{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060058 public:
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050059 Handler(const pldm::utils::DBusHandler* dBusIntf,
George Liu36e81352020-07-01 14:40:30 +080060 const std::string& pdrJsonsDir, const std::string& eventsJsonsDir,
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053061 pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050062 fru::Handler* fruHandler, bool buildPDRLazily = false,
Tom Joseph56e45c52020-03-16 10:01:45 +053063 const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050064 pdrRepo(repo),
Tom Joseph33e9c7e2020-06-11 22:09:52 +053065 hostPDRHandler(hostPDRHandler), stateSensorHandler(eventsJsonsDir),
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050066 fruHandler(fruHandler), dBusIntf(dBusIntf), pdrJsonsDir(pdrJsonsDir),
67 pdrCreated(false)
Sampa Misraa2fa0702019-05-31 01:28:55 -050068 {
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050069 if (!buildPDRLazily)
70 {
71 generate(*dBusIntf, pdrJsonsDir, pdrRepo);
72 pdrCreated = true;
73 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -060074
Deepak Kodihallibc669f12019-11-28 08:52:07 -060075 handlers.emplace(PLDM_GET_PDR,
76 [this](const pldm_msg* request, size_t payloadLength) {
77 return this->getPDR(request, payloadLength);
78 });
George Liueccb0c52020-01-14 11:09:56 +080079 handlers.emplace(PLDM_SET_NUMERIC_EFFECTER_VALUE,
80 [this](const pldm_msg* request, size_t payloadLength) {
81 return this->setNumericEffecterValue(
82 request, payloadLength);
83 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -060084 handlers.emplace(PLDM_SET_STATE_EFFECTER_STATES,
85 [this](const pldm_msg* request, size_t payloadLength) {
86 return this->setStateEffecterStates(request,
87 payloadLength);
88 });
Tom Joseph56e45c52020-03-16 10:01:45 +053089 handlers.emplace(PLDM_PLATFORM_EVENT_MESSAGE,
90 [this](const pldm_msg* request, size_t payloadLength) {
91 return this->platformEventMessage(request,
92 payloadLength);
93 });
94
95 // Default handler for PLDM Events
96 eventHandlers[PLDM_SENSOR_EVENT].emplace_back(
97 [this](const pldm_msg* request, size_t payloadLength,
98 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
99 return this->sensorEvent(request, payloadLength, formatVersion,
100 tid, eventDataOffset);
101 });
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500102 eventHandlers[PLDM_PDR_REPOSITORY_CHG_EVENT].emplace_back(
103 [this](const pldm_msg* request, size_t payloadLength,
104 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset) {
105 return this->pldmPDRRepositoryChgEvent(request, payloadLength,
106 formatVersion, tid,
107 eventDataOffset);
108 });
Tom Joseph56e45c52020-03-16 10:01:45 +0530109
110 // Additional OEM event handlers for PLDM events, append it to the
111 // standard handlers
112 if (addOnHandlersMap)
113 {
114 auto addOnHandlers = addOnHandlersMap.value();
115 for (EventMap::iterator iter = addOnHandlers.begin();
116 iter != addOnHandlers.end(); ++iter)
117 {
118 auto search = eventHandlers.find(iter->first);
119 if (search != eventHandlers.end())
120 {
121 search->second.insert(std::end(search->second),
122 std::begin(iter->second),
123 std::end(iter->second));
124 }
125 else
126 {
127 eventHandlers.emplace(iter->first, iter->second);
128 }
129 }
130 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500131 }
132
George Liu1ec85d42020-02-12 16:05:32 +0800133 pdr_utils::Repo& getRepo()
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600134 {
George Liu1ec85d42020-02-12 16:05:32 +0800135 return this->pdrRepo;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600136 }
137
George Liu1ec85d42020-02-12 16:05:32 +0800138 /** @brief Add D-Bus mapping and value mapping(stateId to D-Bus) for the
139 * effecterId. If the same id is added, the previous dbusObjs will
140 * be "over-written".
141 *
142 * @param[in] effecterId - effecter id
143 * @param[in] dbusObj - list of D-Bus object structure and list of D-Bus
144 * property value to attribute value
145 */
George Liua2870722020-02-11 11:09:30 +0800146 void addDbusObjMaps(
147 uint16_t effecterId,
148 std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> dbusObj);
George Liu1ec85d42020-02-12 16:05:32 +0800149
150 /** @brief Retrieve an effecter id -> D-Bus objects mapping
151 *
152 * @param[in] effecterId - effecter id
153 *
George Liua2870722020-02-11 11:09:30 +0800154 * @return std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps> -
155 * list of D-Bus object structure and list of D-Bus property value
156 * to attribute value
George Liu1ec85d42020-02-12 16:05:32 +0800157 */
George Liua2870722020-02-11 11:09:30 +0800158 const std::tuple<pdr_utils::DbusMappings, pdr_utils::DbusValMaps>&
George Liu1ec85d42020-02-12 16:05:32 +0800159 getDbusObjMaps(uint16_t effecterId) const;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600160
161 uint16_t getNextEffecterId()
162 {
163 return ++nextEffecterId;
164 }
165
166 /** @brief Parse PDR JSONs and build PDR repository
167 *
George Liu36e81352020-07-01 14:40:30 +0800168 * @param[in] dBusIntf - The interface object
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600169 * @param[in] dir - directory housing platform specific PDR JSON files
170 * @param[in] repo - instance of concrete implementation of Repo
171 */
George Liu36e81352020-07-01 14:40:30 +0800172 void generate(const pldm::utils::DBusHandler& dBusIntf,
173 const std::string& dir, Repo& repo);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600174
175 /** @brief Parse PDR JSONs and build state effecter PDR repository
176 *
177 * @param[in] json - platform specific PDR JSON files
178 * @param[in] repo - instance of state effecter implementation of Repo
179 */
180 void generateStateEffecterRepo(const Json& json, Repo& repo);
181
Tom Joseph56e45c52020-03-16 10:01:45 +0530182 /** @brief map of PLDM event type to EventHandlers
183 *
184 */
185 EventMap eventHandlers;
186
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600187 /** @brief Handler for GetPDR
188 *
189 * @param[in] request - Request message payload
190 * @param[in] payloadLength - Request payload length
191 * @param[out] Response - Response message written here
192 */
193 Response getPDR(const pldm_msg* request, size_t payloadLength);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500194
George Liueccb0c52020-01-14 11:09:56 +0800195 /** @brief Handler for setNumericEffecterValue
196 *
197 * @param[in] request - Request message
198 * @param[in] payloadLength - Request payload length
199 * @return Response - PLDM Response message
200 */
201 Response setNumericEffecterValue(const pldm_msg* request,
202 size_t payloadLength);
203
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600204 /** @brief Handler for setStateEffecterStates
205 *
206 * @param[in] request - Request message
207 * @param[in] payloadLength - Request payload length
208 * @return Response - PLDM Response message
209 */
210 Response setStateEffecterStates(const pldm_msg* request,
211 size_t payloadLength);
212
Tom Joseph56e45c52020-03-16 10:01:45 +0530213 /** @brief Handler for PlatformEventMessage
214 *
215 * @param[in] request - Request message
216 * @param[in] payloadLength - Request payload length
217 * @return Response - PLDM Response message
218 */
219 Response platformEventMessage(const pldm_msg* request,
220 size_t payloadLength);
221
222 /** @brief Handler for event class Sensor event
223 *
224 * @param[in] request - Request message
225 * @param[in] payloadLength - Request payload length
226 * @param[in] formatVersion - Version of the event format
227 * @param[in] tid - Terminus ID of the event's originator
228 * @param[in] eventDataOffset - Offset of the event data in the request
229 * message
230 * @return PLDM completion code
231 */
232 int sensorEvent(const pldm_msg* request, size_t payloadLength,
233 uint8_t formatVersion, uint8_t tid, size_t eventDataOffset);
234
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500235 /** @brief Handler for pldmPDRRepositoryChgEvent
236 *
237 * @param[in] request - Request message
238 * @param[in] payloadLength - Request payload length
239 * @param[in] formatVersion - Version of the event format
240 * @param[in] tid - Terminus ID of the event's originator
241 * @param[in] eventDataOffset - Offset of the event data in the request
242 * message
243 * @return PLDM completion code
244 */
245 int pldmPDRRepositoryChgEvent(const pldm_msg* request, size_t payloadLength,
246 uint8_t formatVersion, uint8_t tid,
247 size_t eventDataOffset);
248
249 /** @brief Handler for extracting the PDR handles from changeEntries
250 *
251 * @param[in] changeEntryData - ChangeEntry data from changeRecord
252 * @param[in] changeEntryDataSize - total size of changeEntryData
253 * @param[in] numberOfChangeEntries - total number of changeEntries to
254 * extract
255 * @param[out] pdrRecordHandles - std::vector where the extracted PDR
256 * handles are placed
257 * @return PLDM completion code
258 */
259 int getPDRRecordHandles(const ChangeEntry* changeEntryData,
260 size_t changeEntryDataSize,
261 size_t numberOfChangeEntries,
262 PDRRecordHandles& pdrRecordHandles);
263
Zahed Hossain75330f32020-03-24 02:15:03 -0500264 /** @brief Handler for setting Sensor event data
265 *
266 * @param[in] sensorId - sensorID value of the sensor
267 * @param[in] sensorOffset - Identifies which state sensor within a
268 * composite state sensor the event is being returned for
269 * @param[in] eventState - The event state value from the state change that
270 * triggered the event message
271 * @return PLDM completion code
272 */
273 int setSensorEventData(uint16_t sensorId, uint8_t sensorOffset,
274 uint8_t eventState);
275
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600276 /** @brief Function to set the effecter requested by pldm requester
277 * @param[in] dBusIntf - The interface object
278 * @param[in] effecterId - Effecter ID sent by the requester to act on
279 * @param[in] stateField - The state field data for each of the states,
280 * equal to composite effecter count in number
281 * @return - Success or failure in setting the states. Returns failure in
282 * terms of PLDM completion codes if atleast one state fails to be set
283 */
284 template <class DBusInterface>
285 int setStateEffecterStatesHandler(
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600286 const DBusInterface& dBusIntf, uint16_t effecterId,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600287 const std::vector<set_effecter_state_field>& stateField)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500288 {
George Liue53193f2020-02-24 09:23:26 +0800289 using namespace pldm::responder::pdr;
George Liu1e44c732020-02-28 20:20:06 +0800290 using namespace pldm::utils;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600291 using StateSetNum = uint8_t;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600292
293 state_effecter_possible_states* states = nullptr;
294 pldm_state_effecter_pdr* pdr = nullptr;
295 uint8_t compEffecterCnt = stateField.size();
George Liu1ec85d42020-02-12 16:05:32 +0800296
297 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)>
298 stateEffecterPdrRepo(pldm_pdr_init(), pldm_pdr_destroy);
299 Repo stateEffecterPDRs(stateEffecterPdrRepo.get());
300 getRepoByType(pdrRepo, stateEffecterPDRs, PLDM_STATE_EFFECTER_PDR);
301 if (stateEffecterPDRs.empty())
302 {
303 std::cerr << "Failed to get record by PDR type\n";
304 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
305 }
306
George Liue53193f2020-02-24 09:23:26 +0800307 PdrEntry pdrEntry{};
George Liu1ec85d42020-02-12 16:05:32 +0800308 auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800309 while (pdrRecord)
310 {
311 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
312 if (pdr->effecter_id != effecterId)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500313 {
George Liue53193f2020-02-24 09:23:26 +0800314 pdr = nullptr;
George Liu1ec85d42020-02-12 16:05:32 +0800315 pdrRecord =
316 stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800317 continue;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600318 }
George Liue53193f2020-02-24 09:23:26 +0800319
320 states = reinterpret_cast<state_effecter_possible_states*>(
321 pdr->possible_states);
322 if (compEffecterCnt > pdr->composite_effecter_count)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600323 {
George Liue53193f2020-02-24 09:23:26 +0800324 std::cerr << "The requester sent wrong composite effecter"
325 << " count for the effecter, EFFECTER_ID="
326 << effecterId << "COMP_EFF_CNT=" << compEffecterCnt
327 << "\n";
328 return PLDM_ERROR_INVALID_DATA;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500329 }
George Liue53193f2020-02-24 09:23:26 +0800330 break;
331 }
332
333 if (!pdr)
334 {
335 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500336 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500337
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600338 int rc = PLDM_SUCCESS;
George Liu1ec85d42020-02-12 16:05:32 +0800339 try
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600340 {
George Liu1ec85d42020-02-12 16:05:32 +0800341 const auto& [dbusMappings, dbusValMaps] =
342 dbusObjMaps.at(effecterId);
343 for (uint8_t currState = 0; currState < compEffecterCnt;
344 ++currState)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600345 {
George Liu1ec85d42020-02-12 16:05:32 +0800346 std::vector<StateSetNum> allowed{};
347 // computation is based on table 79 from DSP0248 v1.1.1
348 uint8_t bitfieldIndex =
349 stateField[currState].effecter_state / 8;
350 uint8_t bit =
351 stateField[currState].effecter_state - (8 * bitfieldIndex);
352 if (states->possible_states_size < bitfieldIndex ||
353 !(states->states[bitfieldIndex].byte & (1 << bit)))
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600354 {
George Liu1ec85d42020-02-12 16:05:32 +0800355 std::cerr
356 << "Invalid state set value, EFFECTER_ID=" << effecterId
357 << " VALUE=" << stateField[currState].effecter_state
358 << " COMPOSITE_EFFECTER_ID=" << currState
359 << " DBUS_PATH=" << dbusMappings[currState].objectPath
360 << "\n";
361 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600362 break;
363 }
George Liu1ec85d42020-02-12 16:05:32 +0800364 const DBusMapping& dbusMapping = dbusMappings[currState];
365 const StatestoDbusVal& dbusValToMap = dbusValMaps[currState];
366
367 if (stateField[currState].set_request == PLDM_REQUEST_SET)
368 {
369 try
370 {
371 dBusIntf.setDbusProperty(
372 dbusMapping,
373 dbusValToMap.at(
374 stateField[currState].effecter_state));
375 }
376 catch (const std::exception& e)
377 {
378 std::cerr
379 << "Error setting property, ERROR=" << e.what()
380 << " PROPERTY=" << dbusMapping.propertyName
381 << " INTERFACE="
382 << dbusMapping.interface << " PATH="
383 << dbusMapping.objectPath << "\n";
384 return PLDM_ERROR;
385 }
386 }
387 uint8_t* nextState =
388 reinterpret_cast<uint8_t*>(states) +
389 sizeof(state_effecter_possible_states) -
390 sizeof(states->states) +
391 (states->possible_states_size * sizeof(states->states));
392 states = reinterpret_cast<state_effecter_possible_states*>(
393 nextState);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600394 }
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600395 }
George Liu1ec85d42020-02-12 16:05:32 +0800396 catch (const std::out_of_range& e)
397 {
398 std::cerr << "the effecterId does not exist. effecter id: "
399 << effecterId << e.what() << '\n';
400 }
401
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600402 return rc;
403 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600404
405 private:
406 pdr_utils::Repo pdrRepo;
407 uint16_t nextEffecterId{};
George Liu1ec85d42020-02-12 16:05:32 +0800408 DbusObjMaps dbusObjMaps{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500409 HostPDRHandler* hostPDRHandler;
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530410 events::StateSensorHandler stateSensorHandler;
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530411 fru::Handler* fruHandler;
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500412 const pldm::utils::DBusHandler* dBusIntf;
413 std::string pdrJsonsDir;
414 bool pdrCreated;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600415};
416
417} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530418} // namespace responder
419} // namespace pldm