xref: /openbmc/pldm/host-bmc/dbus_to_event_handler.cpp (revision 6da4f91b061aae1e7d918006e54ae5e8cd8a162e)
1 #include "dbus_to_event_handler.hpp"
2 
3 #include "libpldmresponder/pdr.hpp"
4 
5 #include <libpldm/pldm.h>
6 
7 #include <phosphor-logging/lg2.hpp>
8 
9 PHOSPHOR_LOG2_USING;
10 
11 namespace pldm
12 {
13 using namespace pldm::dbus_api;
14 using namespace pldm::responder;
15 using namespace pldm::responder::pdr;
16 using namespace pldm::responder::pdr_utils;
17 using namespace pldm::utils;
18 using namespace sdbusplus::bus::match::rules;
19 
20 namespace state_sensor
21 {
22 const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
23 
24 DbusToPLDMEvent::DbusToPLDMEvent(
25     int mctp_fd, uint8_t mctp_eid, Requester& requester,
26     pldm::requester::Handler<pldm::requester::Request>* handler) :
27     mctp_fd(mctp_fd),
28     mctp_eid(mctp_eid), requester(requester), handler(handler)
29 {}
30 
31 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
32                                    const std::vector<uint8_t>& eventDataVec)
33 {
34     auto instanceId = requester.getInstanceId(mctp_eid);
35     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
36                                     PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
37                                     eventDataVec.size());
38     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
39 
40     auto rc = encode_platform_event_message_req(
41         instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType,
42         eventDataVec.data(), eventDataVec.size(), request,
43         eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
44     if (rc != PLDM_SUCCESS)
45     {
46         requester.markFree(mctp_eid, instanceId);
47         error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
48               rc);
49         return;
50     }
51 
52     auto platformEventMessageResponseHandler =
53         [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
54         if (response == nullptr || !respMsgLen)
55         {
56             error("Failed to receive response for platform event message");
57             return;
58         }
59         uint8_t completionCode{};
60         uint8_t status{};
61         auto rc = decode_platform_event_message_resp(response, respMsgLen,
62                                                      &completionCode, &status);
63         if (rc || completionCode)
64         {
65             error(
66                 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
67                 "RC", rc, "CC", static_cast<unsigned>(completionCode));
68         }
69     };
70 
71     rc = handler->registerRequest(
72         mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
73         std::move(requestMsg), std::move(platformEventMessageResponseHandler));
74     if (rc)
75     {
76         error("Failed to send the platform event message");
77     }
78 }
79 
80 void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
81                                            const DbusObjMaps& dbusMaps)
82 {
83     // Encode PLDM platform event msg to indicate a state sensor change.
84     // DSP0248_1.2.0 Table 19
85     if (!dbusMaps.contains(sensorId))
86     {
87         // this is not an error condition, if we end up here
88         // that means that the sensor with the sensor id has
89         // custom behaviour(or probably an oem sensor) in
90         // sending events that cannot be captured via standard
91         // dbus-json infastructure
92         return;
93     }
94 
95     size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
96     const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
97     for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
98     {
99         std::vector<uint8_t> sensorEventDataVec{};
100         sensorEventDataVec.resize(sensorEventSize);
101         auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
102             sensorEventDataVec.data());
103         eventData->sensor_id = sensorId;
104         eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
105         eventData->event_class[0] = offset;
106         eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
107         eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
108 
109         const auto& dbusMapping = dbusMappings[offset];
110         const auto& dbusValueMapping = dbusValMaps[offset];
111         auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
112             pldm::utils::DBusHandler::getBus(),
113             propertiesChanged(dbusMapping.objectPath.c_str(),
114                               dbusMapping.interface.c_str()),
115             [this, sensorEventDataVec, dbusValueMapping,
116              dbusMapping](auto& msg) mutable {
117             DbusChangedProps props{};
118             std::string intf;
119             msg.read(intf, props);
120             if (!props.contains(dbusMapping.propertyName))
121             {
122                 return;
123             }
124             for (const auto& itr : dbusValueMapping)
125             {
126                 bool findValue = false;
127                 if (dbusMapping.propertyType == "string")
128                 {
129                     std::string src = std::get<std::string>(itr.second);
130                     std::string dst = std::get<std::string>(
131                         props.at(dbusMapping.propertyName));
132 
133                     auto values = pldm::utils::split(src, "||", " ");
134                     for (auto& value : values)
135                     {
136                         if (value == dst)
137                         {
138                             findValue = true;
139                             break;
140                         }
141                     }
142                 }
143                 else
144                 {
145                     findValue = itr.second == props.at(dbusMapping.propertyName)
146                                     ? true
147                                     : false;
148                 }
149 
150                 if (findValue)
151                 {
152                     auto eventData =
153                         reinterpret_cast<struct pldm_sensor_event_data*>(
154                             sensorEventDataVec.data());
155                     eventData->event_class[1] = itr.first;
156                     eventData->event_class[2] = itr.first;
157                     this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec);
158                     break;
159                 }
160             }
161             });
162         stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
163     }
164 }
165 
166 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
167                                         const DbusObjMaps& dbusMaps)
168 {
169     const std::map<Type, sensorEvent> sensorHandlers = {
170         {PLDM_STATE_SENSOR_PDR,
171          [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
172         this->sendStateSensorEvent(sensorId, dbusMaps);
173          }}};
174 
175     pldm_state_sensor_pdr* pdr = nullptr;
176     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
177         pldm_pdr_init(), pldm_pdr_destroy);
178 
179     for (auto pdrType : pdrTypes)
180     {
181         Repo sensorPDRs(sensorPdrRepo.get());
182         getRepoByType(repo, sensorPDRs, pdrType);
183         if (sensorPDRs.empty())
184         {
185             return;
186         }
187 
188         PdrEntry pdrEntry{};
189         auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
190         while (pdrRecord)
191         {
192             pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
193             SensorId sensorId = LE16TOH(pdr->sensor_id);
194             if (sensorHandlers.contains(pdrType))
195             {
196                 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
197             }
198 
199             pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
200         }
201     }
202 }
203 
204 } // namespace state_sensor
205 } // namespace pldm
206