1d4d97a53STOM JOSEPH #include "event_parser.hpp"
2d4d97a53STOM JOSEPH 
349cfb138SRiya Dixit #include <phosphor-logging/lg2.hpp>
46492f524SGeorge Liu #include <xyz/openbmc_project/Common/error.hpp>
56492f524SGeorge Liu 
6d4d97a53STOM JOSEPH #include <filesystem>
7d4d97a53STOM JOSEPH #include <fstream>
8d4d97a53STOM JOSEPH #include <set>
9d4d97a53STOM JOSEPH 
1049cfb138SRiya Dixit PHOSPHOR_LOG2_USING;
1149cfb138SRiya Dixit 
12d4d97a53STOM JOSEPH namespace pldm::responder::events
13d4d97a53STOM JOSEPH {
14d4d97a53STOM JOSEPH namespace fs = std::filesystem;
15d4d97a53STOM JOSEPH using InternalFailure =
16d4d97a53STOM JOSEPH     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17d4d97a53STOM JOSEPH 
18d4d97a53STOM JOSEPH const Json emptyJson{};
19d4d97a53STOM JOSEPH const std::vector<Json> emptyJsonList{};
20d4d97a53STOM JOSEPH 
21d4d97a53STOM JOSEPH const std::set<std::string_view> supportedDbusPropertyTypes = {
22d4d97a53STOM JOSEPH     "bool",     "uint8_t", "int16_t",  "uint16_t", "int32_t",
23d4d97a53STOM JOSEPH     "uint32_t", "int64_t", "uint64_t", "double",   "string"};
24d4d97a53STOM JOSEPH 
25d4d97a53STOM JOSEPH StateSensorHandler::StateSensorHandler(const std::string& dirPath)
26d4d97a53STOM JOSEPH {
27d4d97a53STOM JOSEPH     fs::path dir(dirPath);
28d4d97a53STOM JOSEPH     if (!fs::exists(dir) || fs::is_empty(dir))
29d4d97a53STOM JOSEPH     {
30*89644441SRiya Dixit         error("Event config directory at '{PATH}' does not exist or empty",
31*89644441SRiya Dixit               "PATH", dirPath.c_str());
32d4d97a53STOM JOSEPH         return;
33d4d97a53STOM JOSEPH     }
34d4d97a53STOM JOSEPH 
35a792b216STom Joseph     for (auto& file : fs::directory_iterator(dirPath))
36d4d97a53STOM JOSEPH     {
37a792b216STom Joseph         std::ifstream jsonFile(file.path());
38d4d97a53STOM JOSEPH 
39d4d97a53STOM JOSEPH         auto data = Json::parse(jsonFile, nullptr, false);
40d4d97a53STOM JOSEPH         if (data.is_discarded())
41d4d97a53STOM JOSEPH         {
42*89644441SRiya Dixit             error("Failed to parse event state sensor JSON file at '{PATH}'",
43*89644441SRiya Dixit                   "PATH", file.path().c_str());
44a792b216STom Joseph             continue;
45d4d97a53STOM JOSEPH         }
46d4d97a53STOM JOSEPH 
47d4d97a53STOM JOSEPH         auto entries = data.value("entries", emptyJsonList);
48d4d97a53STOM JOSEPH         for (const auto& entry : entries)
49d4d97a53STOM JOSEPH         {
50d4d97a53STOM JOSEPH             StateSensorEntry stateSensorEntry{};
51d4d97a53STOM JOSEPH             stateSensorEntry.containerId =
52d4d97a53STOM JOSEPH                 static_cast<uint16_t>(entry.value("containerID", 0));
53d4d97a53STOM JOSEPH             stateSensorEntry.entityType =
54d4d97a53STOM JOSEPH                 static_cast<uint16_t>(entry.value("entityType", 0));
55d4d97a53STOM JOSEPH             stateSensorEntry.entityInstance =
56d4d97a53STOM JOSEPH                 static_cast<uint16_t>(entry.value("entityInstance", 0));
57d4d97a53STOM JOSEPH             stateSensorEntry.sensorOffset =
58d4d97a53STOM JOSEPH                 static_cast<uint8_t>(entry.value("sensorOffset", 0));
59e3607a3cSSagar Srinivas             stateSensorEntry.stateSetid =
60e3607a3cSSagar Srinivas                 static_cast<uint16_t>(entry.value("stateSetId", 0));
61d4d97a53STOM JOSEPH 
62d4d97a53STOM JOSEPH             pldm::utils::DBusMapping dbusInfo{};
63d4d97a53STOM JOSEPH 
64d4d97a53STOM JOSEPH             auto dbus = entry.value("dbus", emptyJson);
65d4d97a53STOM JOSEPH             dbusInfo.objectPath = dbus.value("object_path", "");
66d4d97a53STOM JOSEPH             dbusInfo.interface = dbus.value("interface", "");
67d4d97a53STOM JOSEPH             dbusInfo.propertyName = dbus.value("property_name", "");
68d4d97a53STOM JOSEPH             dbusInfo.propertyType = dbus.value("property_type", "");
69d4d97a53STOM JOSEPH             if (dbusInfo.objectPath.empty() || dbusInfo.interface.empty() ||
70d4d97a53STOM JOSEPH                 dbusInfo.propertyName.empty() ||
7106f9b29eSSagar Srinivas                 !supportedDbusPropertyTypes.contains(dbusInfo.propertyType))
72d4d97a53STOM JOSEPH             {
7349cfb138SRiya Dixit                 error(
74*89644441SRiya Dixit                     "Invalid dbus config at '{PATH}', interface '{DBUS_INTERFACE}', property name '{PROPERTY_NAME}' and property type '{PROPERTY_TYPE}'",
75*89644441SRiya Dixit                     "PATH", dbusInfo.objectPath.c_str(), "DBUS_INTERFACE",
76*89644441SRiya Dixit                     dbusInfo.interface, "PROPERTY_NAME", dbusInfo.propertyName,
77*89644441SRiya Dixit                     "PROPERTY_TYPE", dbusInfo.propertyType);
78d4d97a53STOM JOSEPH                 continue;
79d4d97a53STOM JOSEPH             }
80d4d97a53STOM JOSEPH 
81d4d97a53STOM JOSEPH             auto eventStates = entry.value("event_states", emptyJsonList);
82d4d97a53STOM JOSEPH             auto propertyValues = dbus.value("property_values", emptyJsonList);
83d4d97a53STOM JOSEPH             if ((eventStates.size() == 0) || (propertyValues.size() == 0) ||
84d4d97a53STOM JOSEPH                 (eventStates.size() != propertyValues.size()))
85d4d97a53STOM JOSEPH             {
8649cfb138SRiya Dixit                 error(
87*89644441SRiya Dixit                     "Invalid event state JSON config size '{EVENT_STATE_SIZE}' and property value size '{PROPERTY_VALUE_SIZE}'",
88*89644441SRiya Dixit                     "EVENT_STATE_SIZE", eventStates.size(),
89*89644441SRiya Dixit                     "PROPERTY_VALUE_SIZE", propertyValues.size());
90d4d97a53STOM JOSEPH                 continue;
91d4d97a53STOM JOSEPH             }
92d4d97a53STOM JOSEPH 
93d4d97a53STOM JOSEPH             auto eventStateMap = mapStateToDBusVal(eventStates, propertyValues,
94d4d97a53STOM JOSEPH                                                    dbusInfo.propertyType);
95d4d97a53STOM JOSEPH             eventMap.emplace(
96d4d97a53STOM JOSEPH                 stateSensorEntry,
97d4d97a53STOM JOSEPH                 std::make_tuple(std::move(dbusInfo), std::move(eventStateMap)));
98d4d97a53STOM JOSEPH         }
99d4d97a53STOM JOSEPH     }
100a792b216STom Joseph }
101d4d97a53STOM JOSEPH 
102d4d97a53STOM JOSEPH StateToDBusValue StateSensorHandler::mapStateToDBusVal(
103d4d97a53STOM JOSEPH     const Json& eventStates, const Json& propertyValues, std::string_view type)
104d4d97a53STOM JOSEPH {
105d4d97a53STOM JOSEPH     StateToDBusValue eventStateMap{};
106d4d97a53STOM JOSEPH     auto stateIt = eventStates.begin();
107d4d97a53STOM JOSEPH     auto propIt = propertyValues.begin();
108d4d97a53STOM JOSEPH 
109d4d97a53STOM JOSEPH     for (; stateIt != eventStates.end(); ++stateIt, ++propIt)
110d4d97a53STOM JOSEPH     {
111d4d97a53STOM JOSEPH         auto propValue = utils::jsonEntryToDbusVal(type, propIt.value());
112d4d97a53STOM JOSEPH         eventStateMap.emplace((*stateIt).get<uint8_t>(), std::move(propValue));
113d4d97a53STOM JOSEPH     }
114d4d97a53STOM JOSEPH 
115d4d97a53STOM JOSEPH     return eventStateMap;
116d4d97a53STOM JOSEPH }
117d4d97a53STOM JOSEPH 
118d4d97a53STOM JOSEPH int StateSensorHandler::eventAction(const StateSensorEntry& entry,
119d4d97a53STOM JOSEPH                                     pdr::EventState state)
120d4d97a53STOM JOSEPH {
121d4d97a53STOM JOSEPH     try
122d4d97a53STOM JOSEPH     {
123d4d97a53STOM JOSEPH         const auto& [dbusMapping, eventStateMap] = eventMap.at(entry);
124d4d97a53STOM JOSEPH         utils::PropertyValue propValue{};
125d4d97a53STOM JOSEPH         try
126d4d97a53STOM JOSEPH         {
127d4d97a53STOM JOSEPH             propValue = eventStateMap.at(state);
128d4d97a53STOM JOSEPH         }
129d4d97a53STOM JOSEPH         catch (const std::out_of_range& e)
130d4d97a53STOM JOSEPH         {
131*89644441SRiya Dixit             error("Invalid event state '{EVENT_STATE}', error - {ERROR}",
132*89644441SRiya Dixit                   "EVENT_STATE", state, "ERROR", e);
133d4d97a53STOM JOSEPH             return PLDM_ERROR_INVALID_DATA;
134d4d97a53STOM JOSEPH         }
135d4d97a53STOM JOSEPH 
136d4d97a53STOM JOSEPH         try
137d4d97a53STOM JOSEPH         {
138d4d97a53STOM JOSEPH             pldm::utils::DBusHandler().setDbusProperty(dbusMapping, propValue);
139d4d97a53STOM JOSEPH         }
140d4d97a53STOM JOSEPH         catch (const std::exception& e)
141d4d97a53STOM JOSEPH         {
14249cfb138SRiya Dixit             error(
143*89644441SRiya Dixit                 "Failed to  set property '{PROPERTY}' on interface '{INTERFACE}' at path '{PATH}', error - {ERROR}",
14458cbcaf2SKamalkumar Patel                 "PROPERTY", dbusMapping.propertyName, "INTERFACE",
14558cbcaf2SKamalkumar Patel                 dbusMapping.interface, "PATH", dbusMapping.objectPath, "ERROR",
14658cbcaf2SKamalkumar Patel                 e);
147d4d97a53STOM JOSEPH             return PLDM_ERROR;
148d4d97a53STOM JOSEPH         }
149d4d97a53STOM JOSEPH     }
15058cbcaf2SKamalkumar Patel     catch (const std::out_of_range&)
151d4d97a53STOM JOSEPH     {
152d4d97a53STOM JOSEPH         // There is no BMC action for this PLDM event
153d4d97a53STOM JOSEPH         return PLDM_SUCCESS;
154d4d97a53STOM JOSEPH     }
155d4d97a53STOM JOSEPH     return PLDM_SUCCESS;
156d4d97a53STOM JOSEPH }
157d4d97a53STOM JOSEPH 
158d4d97a53STOM JOSEPH } // namespace pldm::responder::events
159