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