xref: /openbmc/pldm/libpldmresponder/event_parser.hpp (revision d4d97a53e2bf328a26ea23f826c7735cae70fd86)
1 #pragma once
2 
3 #include "types.hpp"
4 #include "utils.hpp"
5 
6 #include <filesystem>
7 #include <map>
8 #include <nlohmann/json.hpp>
9 #include <string>
10 #include <tuple>
11 #include <vector>
12 
13 namespace pldm::responder::events
14 {
15 
16 /** @struct StateSensorEntry
17  *
18  *  StateSensorEntry is a key to uniquely identify a state sensor, so that a
19  *  D-Bus action can be defined for PlatformEventMessage command with
20  *  sensorEvent type. This struct is used as a key in a std::map so implemented
21  *  operator== and operator<.
22  */
23 struct StateSensorEntry
24 {
25     pdr::ContainerID containerId;
26     pdr::EntityType entityType;
27     pdr::EntityInstance entityInstance;
28     pdr::SensorOffset sensorOffset;
29 
30     bool operator==(const StateSensorEntry& e) const
31     {
32         return ((containerId == e.containerId) &&
33                 (entityType == e.entityType) &&
34                 (entityInstance == e.entityInstance) &&
35                 (sensorOffset == e.sensorOffset));
36     }
37 
38     bool operator<(const StateSensorEntry& e) const
39     {
40         return (
41             (containerId < e.containerId) ||
42             ((containerId == e.containerId) && (entityType < e.entityType)) ||
43             ((containerId == e.containerId) && (entityType == e.entityType) &&
44              (entityInstance < e.entityInstance)) ||
45             ((containerId == e.containerId) && (entityType == e.entityType) &&
46              (entityInstance == e.entityInstance) &&
47              (sensorOffset < e.sensorOffset)));
48     }
49 };
50 
51 using StateToDBusValue = std::map<pdr::EventState, pldm::utils::PropertyValue>;
52 using EventDBusInfo = std::tuple<pldm::utils::DBusMapping, StateToDBusValue>;
53 using EventMap = std::map<StateSensorEntry, EventDBusInfo>;
54 using Json = nlohmann::json;
55 
56 /** @class StateSensorHandler
57  *
58  *  @brief Parses the event state sensor configuration JSON file and build
59  *         the lookup data structure, which can map the event state for a
60  *         sensor in the PlatformEventMessage command to a D-Bus property and
61  *         the property value.
62  */
63 class StateSensorHandler
64 {
65   public:
66     StateSensorHandler() = delete;
67 
68     /** @brief Parse the event state sensor configuration JSON file and build
69      *         the lookup data stucture.
70      *
71      *  @param[in] dirPath - directory path which has the config JSONs
72      */
73     explicit StateSensorHandler(const std::string& dirPath);
74     virtual ~StateSensorHandler() = default;
75     StateSensorHandler(const StateSensorHandler&) = default;
76     StateSensorHandler& operator=(const StateSensorHandler&) = default;
77     StateSensorHandler(StateSensorHandler&&) = default;
78     StateSensorHandler& operator=(StateSensorHandler&&) = default;
79 
80     /** @brief If the StateSensorEntry and EventState is valid, the D-Bus
81      *         property corresponding to the StateSensorEntry is set based on
82      *         the EventState
83      *
84      *  @param[in] entry - state sensor entry
85      *  @param[in] state - event state
86      *
87      *  @return PLDM completion code
88      */
89     int eventAction(const StateSensorEntry& entry, pdr::EventState state);
90 
91     /** @brief Helper API to get D-Bus information for a StateSensorEntry
92      *
93      *  @param[in] entry - state sensor entry
94      *
95      *  @return D-Bus information corresponding to the SensorEntry
96      */
97     const EventDBusInfo& getEventInfo(const StateSensorEntry& entry) const
98     {
99         return eventMap.at(entry);
100     }
101 
102   private:
103     EventMap eventMap; //!< a map of StateSensorEntry to D-Bus information
104 
105     /** @brief Create a map of EventState to D-Bus property values from
106      *         the information provided in the event state configuration
107      *         JSON
108      *
109      *  @param[in] eventStates - a JSON array of event states
110      *  @param[in] propertyValues - a JSON array of D-Bus property values
111      *  @param[in] type - the type of D-Bus property
112      *
113      *  @return a map of EventState to D-Bus property values
114      */
115     StateToDBusValue mapStateToDBusVal(const Json& eventStates,
116                                        const Json& propertyValues,
117                                        std::string_view type);
118 };
119 
120 } // namespace pldm::responder::events