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