event_parser.cpp (70a47baf6f8952e4d5dcc3c0505f390bc78a0043) event_parser.cpp (49cfb138af156020599361584b20c9ed591eeeb6)
1#include "event_parser.hpp"
2
1#include "event_parser.hpp"
2
3#include <phosphor-logging/lg2.hpp>
3#include <xyz/openbmc_project/Common/error.hpp>
4
5#include <filesystem>
6#include <fstream>
7#include <iostream>
8#include <set>
9
4#include <xyz/openbmc_project/Common/error.hpp>
5
6#include <filesystem>
7#include <fstream>
8#include <iostream>
9#include <set>
10
11PHOSPHOR_LOG2_USING;
12
10namespace pldm::responder::events
11{
13namespace pldm::responder::events
14{
12
13namespace fs = std::filesystem;
14using InternalFailure =
15 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
16
17const Json emptyJson{};
18const std::vector<Json> emptyJsonList{};
19const std::vector<std::string> emptyStringVec{};
20
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 {
15namespace fs = std::filesystem;
16using InternalFailure =
17 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
18
19const Json emptyJson{};
20const std::vector<Json> emptyJsonList{};
21const std::vector<std::string> emptyStringVec{};
22
23const std::set<std::string_view> supportedDbusPropertyTypes = {
24 "bool", "uint8_t", "int16_t", "uint16_t", "int32_t",
25 "uint32_t", "int64_t", "uint64_t", "double", "string"};
26
27StateSensorHandler::StateSensorHandler(const std::string& dirPath)
28{
29 fs::path dir(dirPath);
30 if (!fs::exists(dir) || fs::is_empty(dir))
31 {
30 std::cerr << "Event config directory does not exist or empty, DIR="
31 << dirPath << "\n";
32 error("Event config directory does not exist or empty, DIR={DIR_PATH}",
33 "DIR_PATH", dirPath.c_str());
32 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 {
34 return;
35 }
36
37 for (auto& file : fs::directory_iterator(dirPath))
38 {
39 std::ifstream jsonFile(file.path());
40
41 auto data = Json::parse(jsonFile, nullptr, false);
42 if (data.is_discarded())
43 {
42 std::cerr << "Parsing Event state sensor JSON file failed, FILE="
43 << file.path();
44 error(
45 "Parsing Event state sensor JSON file failed, FILE={FILE_PATH}",
46 "FILE_PATH", file.path().c_str());
44 continue;
45 }
46
47 auto entries = data.value("entries", emptyJsonList);
48 for (const auto& entry : entries)
49 {
50 StateSensorEntry stateSensorEntry{};
51 stateSensorEntry.containerId =

--- 12 unchanged lines hidden (view full) ---

64 dbusInfo.interface = dbus.value("interface", "");
65 dbusInfo.propertyName = dbus.value("property_name", "");
66 dbusInfo.propertyType = dbus.value("property_type", "");
67 if (dbusInfo.objectPath.empty() || dbusInfo.interface.empty() ||
68 dbusInfo.propertyName.empty() ||
69 (supportedDbusPropertyTypes.find(dbusInfo.propertyType) ==
70 supportedDbusPropertyTypes.end()))
71 {
47 continue;
48 }
49
50 auto entries = data.value("entries", emptyJsonList);
51 for (const auto& entry : entries)
52 {
53 StateSensorEntry stateSensorEntry{};
54 stateSensorEntry.containerId =

--- 12 unchanged lines hidden (view full) ---

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 {
72 std::cerr << "Invalid dbus config,"
73 << " OBJPATH=" << dbusInfo.objectPath << " INTERFACE="
74 << dbusInfo.interface << " PROPERTY_NAME="
75 << dbusInfo.propertyName
76 << " PROPERTY_TYPE=" << dbusInfo.propertyType << "\n";
75 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);
77 continue;
78 }
79
80 auto eventStates = entry.value("event_states", emptyJsonList);
81 auto propertyValues = dbus.value("property_values", emptyJsonList);
82 if ((eventStates.size() == 0) || (propertyValues.size() == 0) ||
83 (eventStates.size() != propertyValues.size()))
84 {
80 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 {
85 std::cerr << "Invalid event state JSON config,"
86 << " EVENT_STATE_SIZE=" << eventStates.size()
87 << " PROPERTY_VALUE_SIZE=" << propertyValues.size()
88 << "\n";
88 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());
89 continue;
90 }
91
92 auto eventStateMap = mapStateToDBusVal(eventStates, propertyValues,
93 dbusInfo.propertyType);
94 eventMap.emplace(
95 stateSensorEntry,
96 std::make_tuple(std::move(dbusInfo), std::move(eventStateMap)));

--- 25 unchanged lines hidden (view full) ---

122 const auto& [dbusMapping, eventStateMap] = eventMap.at(entry);
123 utils::PropertyValue propValue{};
124 try
125 {
126 propValue = eventStateMap.at(state);
127 }
128 catch (const std::out_of_range& e)
129 {
92 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)));

--- 25 unchanged lines hidden (view full) ---

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 {
130 std::cerr << "Invalid event state" << static_cast<unsigned>(state)
131 << '\n';
133 error("Invalid event state {EVENT_STATE}", "EVENT_STATE",
134 static_cast<unsigned>(state));
132 return PLDM_ERROR_INVALID_DATA;
133 }
134
135 try
136 {
137 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, propValue);
138 }
139 catch (const std::exception& e)
140 {
135 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 {
141 std::cerr << "Error setting property, ERROR=" << e.what()
142 << " PROPERTY=" << dbusMapping.propertyName
143 << " INTERFACE=" << dbusMapping.interface << " PATH="
144 << dbusMapping.objectPath << "\n";
144 error(
145 "Error setting property, ERROR={ERR_EXCEP} PROPERTY={DBUS_PROP} INTERFACE={DBUS_INTF} PATH = {DBUS_OBJ_PATH}",
146 "ERR_EXCEP", e.what(), "DBUS_PROP", dbusMapping.propertyName,
147 "DBUS_INTF", dbusMapping.interface, "DBUS_OBJ_PATH",
148 dbusMapping.objectPath.c_str());
145 return PLDM_ERROR;
146 }
147 }
148 catch (const std::out_of_range& e)
149 {
150 // There is no BMC action for this PLDM event
151 return PLDM_SUCCESS;
152 }
153 return PLDM_SUCCESS;
154}
155
156} // namespace pldm::responder::events
149 return PLDM_ERROR;
150 }
151 }
152 catch (const std::out_of_range& e)
153 {
154 // There is no BMC action for this PLDM event
155 return PLDM_SUCCESS;
156 }
157 return PLDM_SUCCESS;
158}
159
160} // namespace pldm::responder::events