blob: 54895914f63598eb6d6da133260ea34535d9a682 [file] [log] [blame]
Patrick Williams70a47ba2021-09-02 09:53:31 -05001#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
14namespace 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 */
24struct StateSensorEntry
25{
26 pdr::ContainerID containerId;
27 pdr::EntityType entityType;
28 pdr::EntityInstance entityInstance;
29 pdr::SensorOffset sensorOffset;
Sagar Srinivase3607a32024-02-16 03:50:53 -060030 pdr::StateSetId stateSetid;
Manojkiran Edafa084702024-05-27 10:20:30 +053031 bool skipContainerId;
Patrick Williams70a47ba2021-09-02 09:53:31 -050032
33 bool operator==(const StateSensorEntry& e) const
34 {
Manojkiran Edafa084702024-05-27 10:20:30 +053035 if (!skipContainerId)
36 {
37 return ((containerId == e.containerId) &&
38 (entityType == e.entityType) &&
39 (entityInstance == e.entityInstance) &&
40 (sensorOffset == e.sensorOffset) &&
41 (stateSetid == e.stateSetid));
42 }
43 else
44 {
45 return ((entityType == e.entityType) &&
46 (entityInstance == e.entityInstance) &&
47 (sensorOffset == e.sensorOffset) &&
48 (stateSetid == e.stateSetid));
49 }
Patrick Williams70a47ba2021-09-02 09:53:31 -050050 }
51
52 bool operator<(const StateSensorEntry& e) const
53 {
Manojkiran Edafa084702024-05-27 10:20:30 +053054 if (!skipContainerId)
55 {
56 return std::tie(entityType, entityInstance, containerId,
57 sensorOffset, stateSetid) <
58 std::tie(e.entityType, e.entityInstance, e.containerId,
59 e.sensorOffset, e.stateSetid);
60 }
61 else
62 {
63 return std::tie(entityType, entityInstance, sensorOffset,
64 stateSetid) <
65 std::tie(e.entityType, e.entityInstance, e.sensorOffset,
66 e.stateSetid);
67 }
Patrick Williams70a47ba2021-09-02 09:53:31 -050068 }
69};
70
71using StateToDBusValue = std::map<pdr::EventState, pldm::utils::PropertyValue>;
72using EventDBusInfo = std::tuple<pldm::utils::DBusMapping, StateToDBusValue>;
73using EventMap = std::map<StateSensorEntry, EventDBusInfo>;
74using Json = nlohmann::json;
75
76/** @class StateSensorHandler
77 *
78 * @brief Parses the event state sensor configuration JSON file and build
79 * the lookup data structure, which can map the event state for a
80 * sensor in the PlatformEventMessage command to a D-Bus property and
81 * the property value.
82 */
83class StateSensorHandler
84{
85 public:
86 StateSensorHandler() = delete;
87
88 /** @brief Parse the event state sensor configuration JSON file and build
Manojkiran Eda2576aec2024-06-17 12:05:17 +053089 * the lookup data structure.
Patrick Williams70a47ba2021-09-02 09:53:31 -050090 *
91 * @param[in] dirPath - directory path which has the config JSONs
92 */
93 explicit StateSensorHandler(const std::string& dirPath);
94 virtual ~StateSensorHandler() = default;
95 StateSensorHandler(const StateSensorHandler&) = default;
96 StateSensorHandler& operator=(const StateSensorHandler&) = default;
97 StateSensorHandler(StateSensorHandler&&) = default;
98 StateSensorHandler& operator=(StateSensorHandler&&) = default;
99
100 /** @brief If the StateSensorEntry and EventState is valid, the D-Bus
101 * property corresponding to the StateSensorEntry is set based on
102 * the EventState
103 *
104 * @param[in] entry - state sensor entry
105 * @param[in] state - event state
106 *
107 * @return PLDM completion code
108 */
109 int eventAction(const StateSensorEntry& entry, pdr::EventState state);
110
111 /** @brief Helper API to get D-Bus information for a StateSensorEntry
112 *
113 * @param[in] entry - state sensor entry
114 *
115 * @return D-Bus information corresponding to the SensorEntry
116 */
117 const EventDBusInfo& getEventInfo(const StateSensorEntry& entry) const
118 {
119 return eventMap.at(entry);
120 }
121
122 private:
123 EventMap eventMap; //!< a map of StateSensorEntry to D-Bus information
124
125 /** @brief Create a map of EventState to D-Bus property values from
126 * the information provided in the event state configuration
127 * JSON
128 *
129 * @param[in] eventStates - a JSON array of event states
130 * @param[in] propertyValues - a JSON array of D-Bus property values
131 * @param[in] type - the type of D-Bus property
132 *
133 * @return a map of EventState to D-Bus property values
134 */
135 StateToDBusValue mapStateToDBusVal(const Json& eventStates,
136 const Json& propertyValues,
137 std::string_view type);
138};
139
Patrick Williams6da4f912023-05-10 07:50:53 -0500140} // namespace pldm::responder::events