Patrick Williams | 70a47ba | 2021-09-02 09:53:31 -0500 | [diff] [blame] | 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; |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame] | 30 | pdr::StateSetId stateSetid; |
Manojkiran Eda | fa08470 | 2024-05-27 10:20:30 +0530 | [diff] [blame] | 31 | bool skipContainerId; |
Patrick Williams | 70a47ba | 2021-09-02 09:53:31 -0500 | [diff] [blame] | 32 | |
| 33 | bool operator==(const StateSensorEntry& e) const |
| 34 | { |
Manojkiran Eda | fa08470 | 2024-05-27 10:20:30 +0530 | [diff] [blame] | 35 | 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 Williams | 70a47ba | 2021-09-02 09:53:31 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool operator<(const StateSensorEntry& e) const |
| 53 | { |
Manojkiran Eda | fa08470 | 2024-05-27 10:20:30 +0530 | [diff] [blame] | 54 | 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 Williams | 70a47ba | 2021-09-02 09:53:31 -0500 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | using StateToDBusValue = std::map<pdr::EventState, pldm::utils::PropertyValue>; |
| 72 | using EventDBusInfo = std::tuple<pldm::utils::DBusMapping, StateToDBusValue>; |
| 73 | using EventMap = std::map<StateSensorEntry, EventDBusInfo>; |
| 74 | using 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 | */ |
| 83 | class StateSensorHandler |
| 84 | { |
| 85 | public: |
| 86 | StateSensorHandler() = delete; |
| 87 | |
| 88 | /** @brief Parse the event state sensor configuration JSON file and build |
| 89 | * the lookup data stucture. |
| 90 | * |
| 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 Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 140 | } // namespace pldm::responder::events |