blob: 303a55323b7be18cf383ba0a3b23ef16857478b1 [file] [log] [blame]
Patrick Williams70a47ba2021-09-02 09:53:31 -05001#include "event_parser.hpp"
2
Riya Dixit49cfb132023-03-02 04:26:53 -06003#include <phosphor-logging/lg2.hpp>
Patrick Williams70a47ba2021-09-02 09:53:31 -05004#include <xyz/openbmc_project/Common/error.hpp>
5
6#include <filesystem>
7#include <fstream>
Patrick Williams70a47ba2021-09-02 09:53:31 -05008#include <set>
9
Riya Dixit49cfb132023-03-02 04:26:53 -060010PHOSPHOR_LOG2_USING;
11
Patrick Williams70a47ba2021-09-02 09:53:31 -050012namespace pldm::responder::events
13{
Patrick Williams70a47ba2021-09-02 09:53:31 -050014namespace fs = std::filesystem;
15using InternalFailure =
16 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17
18const Json emptyJson{};
19const std::vector<Json> emptyJsonList{};
Patrick Williams70a47ba2021-09-02 09:53:31 -050020
21const std::set<std::string_view> supportedDbusPropertyTypes = {
22 "bool", "uint8_t", "int16_t", "uint16_t", "int32_t",
23 "uint32_t", "int64_t", "uint64_t", "double", "string"};
24
25StateSensorHandler::StateSensorHandler(const std::string& dirPath)
26{
27 fs::path dir(dirPath);
28 if (!fs::exists(dir) || fs::is_empty(dir))
29 {
Riya Dixit49cfb132023-03-02 04:26:53 -060030 error("Event config directory does not exist or empty, DIR={DIR_PATH}",
31 "DIR_PATH", dirPath.c_str());
Patrick Williams70a47ba2021-09-02 09:53:31 -050032 return;
33 }
34
35 for (auto& file : fs::directory_iterator(dirPath))
36 {
37 std::ifstream jsonFile(file.path());
38
39 auto data = Json::parse(jsonFile, nullptr, false);
40 if (data.is_discarded())
41 {
Riya Dixit49cfb132023-03-02 04:26:53 -060042 error(
43 "Parsing Event state sensor JSON file failed, FILE={FILE_PATH}",
44 "FILE_PATH", file.path().c_str());
Patrick Williams70a47ba2021-09-02 09:53:31 -050045 continue;
46 }
47
48 auto entries = data.value("entries", emptyJsonList);
49 for (const auto& entry : entries)
50 {
51 StateSensorEntry stateSensorEntry{};
52 stateSensorEntry.containerId =
53 static_cast<uint16_t>(entry.value("containerID", 0));
54 stateSensorEntry.entityType =
55 static_cast<uint16_t>(entry.value("entityType", 0));
56 stateSensorEntry.entityInstance =
57 static_cast<uint16_t>(entry.value("entityInstance", 0));
58 stateSensorEntry.sensorOffset =
59 static_cast<uint8_t>(entry.value("sensorOffset", 0));
Sagar Srinivase3607a32024-02-16 03:50:53 -060060 stateSensorEntry.stateSetid =
61 static_cast<uint16_t>(entry.value("stateSetId", 0));
Patrick Williams70a47ba2021-09-02 09:53:31 -050062
63 pldm::utils::DBusMapping dbusInfo{};
64
65 auto dbus = entry.value("dbus", emptyJson);
66 dbusInfo.objectPath = dbus.value("object_path", "");
67 dbusInfo.interface = dbus.value("interface", "");
68 dbusInfo.propertyName = dbus.value("property_name", "");
69 dbusInfo.propertyType = dbus.value("property_type", "");
70 if (dbusInfo.objectPath.empty() || dbusInfo.interface.empty() ||
71 dbusInfo.propertyName.empty() ||
72 (supportedDbusPropertyTypes.find(dbusInfo.propertyType) ==
73 supportedDbusPropertyTypes.end()))
74 {
Riya Dixit49cfb132023-03-02 04:26:53 -060075 error(
76 "Invalid dbus config, OBJPATH= {DBUS_OBJ_PATH} INTERFACE={DBUS_INTF} PROPERTY_NAME={DBUS_PROP} PROPERTY_TYPE={DBUS_PROP_TYPE}",
77 "DBUS_OBJ_PATH", dbusInfo.objectPath.c_str(), "DBUS_INTF",
78 dbusInfo.interface, "DBUS_PROP", dbusInfo.propertyName,
79 "DBUS_PROP_TYPE", dbusInfo.propertyType);
Patrick Williams70a47ba2021-09-02 09:53:31 -050080 continue;
81 }
82
83 auto eventStates = entry.value("event_states", emptyJsonList);
84 auto propertyValues = dbus.value("property_values", emptyJsonList);
85 if ((eventStates.size() == 0) || (propertyValues.size() == 0) ||
86 (eventStates.size() != propertyValues.size()))
87 {
Riya Dixit49cfb132023-03-02 04:26:53 -060088 error(
89 "Invalid event state JSON config, EVENT_STATE_SIZE={EVENT_STATE_SIZE} PROPERTY_VALUE_SIZE={PROP_VAL_SIZE}",
90 "EVENT_STATE_SIZE", eventStates.size(), "PROP_VAL_SIZE",
91 propertyValues.size());
Patrick Williams70a47ba2021-09-02 09:53:31 -050092 continue;
93 }
94
95 auto eventStateMap = mapStateToDBusVal(eventStates, propertyValues,
96 dbusInfo.propertyType);
97 eventMap.emplace(
98 stateSensorEntry,
99 std::make_tuple(std::move(dbusInfo), std::move(eventStateMap)));
100 }
101 }
102}
103
104StateToDBusValue StateSensorHandler::mapStateToDBusVal(
105 const Json& eventStates, const Json& propertyValues, std::string_view type)
106{
107 StateToDBusValue eventStateMap{};
108 auto stateIt = eventStates.begin();
109 auto propIt = propertyValues.begin();
110
111 for (; stateIt != eventStates.end(); ++stateIt, ++propIt)
112 {
113 auto propValue = utils::jsonEntryToDbusVal(type, propIt.value());
114 eventStateMap.emplace((*stateIt).get<uint8_t>(), std::move(propValue));
115 }
116
117 return eventStateMap;
118}
119
120int StateSensorHandler::eventAction(const StateSensorEntry& entry,
121 pdr::EventState state)
122{
123 try
124 {
125 const auto& [dbusMapping, eventStateMap] = eventMap.at(entry);
126 utils::PropertyValue propValue{};
127 try
128 {
129 propValue = eventStateMap.at(state);
130 }
131 catch (const std::out_of_range& e)
132 {
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500133 error("Invalid event state '{EVENT_STATE}': {ERROR}", "EVENT_STATE",
134 state, "ERROR", e);
Patrick Williams70a47ba2021-09-02 09:53:31 -0500135 return PLDM_ERROR_INVALID_DATA;
136 }
137
138 try
139 {
140 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, propValue);
141 }
142 catch (const std::exception& e)
143 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600144 error(
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500145 "Error setting property value '{PROPERTY}' on interface '{INTERFACE}' at '{PATH}': {ERROR}",
146 "PROPERTY", dbusMapping.propertyName, "INTERFACE",
147 dbusMapping.interface, "PATH", dbusMapping.objectPath, "ERROR",
148 e);
Patrick Williams70a47ba2021-09-02 09:53:31 -0500149 return PLDM_ERROR;
150 }
151 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500152 catch (const std::out_of_range&)
Patrick Williams70a47ba2021-09-02 09:53:31 -0500153 {
154 // There is no BMC action for this PLDM event
155 return PLDM_SUCCESS;
156 }
157 return PLDM_SUCCESS;
158}
159
Riya Dixit49cfb132023-03-02 04:26:53 -0600160} // namespace pldm::responder::events