blob: 7498c4c35d714b08f5865a3da76e03a84b863b80 [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;
Patrick Williams70a47ba2021-09-02 09:53:31 -050031
32 bool operator==(const StateSensorEntry& e) const
33 {
Sagar Srinivase3607a32024-02-16 03:50:53 -060034 return (
35 (containerId == e.containerId) && (entityType == e.entityType) &&
36 (entityInstance == e.entityInstance) &&
37 (sensorOffset == e.sensorOffset) && (stateSetid == e.stateSetid));
Patrick Williams70a47ba2021-09-02 09:53:31 -050038 }
39
40 bool operator<(const StateSensorEntry& e) const
41 {
42 return (
43 (containerId < e.containerId) ||
44 ((containerId == e.containerId) && (entityType < e.entityType)) ||
45 ((containerId == e.containerId) && (entityType == e.entityType) &&
46 (entityInstance < e.entityInstance)) ||
47 ((containerId == e.containerId) && (entityType == e.entityType) &&
48 (entityInstance == e.entityInstance) &&
Sagar Srinivase3607a32024-02-16 03:50:53 -060049 (sensorOffset < e.sensorOffset)) ||
50 ((containerId == e.containerId) && (entityType == e.entityType) &&
51 (entityInstance == e.entityInstance) &&
52 (sensorOffset == e.sensorOffset) && (stateSetid < e.stateSetid)));
Patrick Williams70a47ba2021-09-02 09:53:31 -050053 }
54};
55
56using StateToDBusValue = std::map<pdr::EventState, pldm::utils::PropertyValue>;
57using EventDBusInfo = std::tuple<pldm::utils::DBusMapping, StateToDBusValue>;
58using EventMap = std::map<StateSensorEntry, EventDBusInfo>;
59using Json = nlohmann::json;
60
61/** @class StateSensorHandler
62 *
63 * @brief Parses the event state sensor configuration JSON file and build
64 * the lookup data structure, which can map the event state for a
65 * sensor in the PlatformEventMessage command to a D-Bus property and
66 * the property value.
67 */
68class StateSensorHandler
69{
70 public:
71 StateSensorHandler() = delete;
72
73 /** @brief Parse the event state sensor configuration JSON file and build
74 * the lookup data stucture.
75 *
76 * @param[in] dirPath - directory path which has the config JSONs
77 */
78 explicit StateSensorHandler(const std::string& dirPath);
79 virtual ~StateSensorHandler() = default;
80 StateSensorHandler(const StateSensorHandler&) = default;
81 StateSensorHandler& operator=(const StateSensorHandler&) = default;
82 StateSensorHandler(StateSensorHandler&&) = default;
83 StateSensorHandler& operator=(StateSensorHandler&&) = default;
84
85 /** @brief If the StateSensorEntry and EventState is valid, the D-Bus
86 * property corresponding to the StateSensorEntry is set based on
87 * the EventState
88 *
89 * @param[in] entry - state sensor entry
90 * @param[in] state - event state
91 *
92 * @return PLDM completion code
93 */
94 int eventAction(const StateSensorEntry& entry, pdr::EventState state);
95
96 /** @brief Helper API to get D-Bus information for a StateSensorEntry
97 *
98 * @param[in] entry - state sensor entry
99 *
100 * @return D-Bus information corresponding to the SensorEntry
101 */
102 const EventDBusInfo& getEventInfo(const StateSensorEntry& entry) const
103 {
104 return eventMap.at(entry);
105 }
106
107 private:
108 EventMap eventMap; //!< a map of StateSensorEntry to D-Bus information
109
110 /** @brief Create a map of EventState to D-Bus property values from
111 * the information provided in the event state configuration
112 * JSON
113 *
114 * @param[in] eventStates - a JSON array of event states
115 * @param[in] propertyValues - a JSON array of D-Bus property values
116 * @param[in] type - the type of D-Bus property
117 *
118 * @return a map of EventState to D-Bus property values
119 */
120 StateToDBusValue mapStateToDBusVal(const Json& eventStates,
121 const Json& propertyValues,
122 std::string_view type);
123};
124
Patrick Williams6da4f912023-05-10 07:50:53 -0500125} // namespace pldm::responder::events