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 = [](mctp_eid_t /*eid*/,
53                                                   const pldm_msg* response,
54                                                   size_t respMsgLen) {
55         if (response == nullptr || !respMsgLen)
56         {
57             error("Failed to receive response for platform event message");
58             return;
59         }
60         uint8_t completionCode{};
61         uint8_t status{};
62         auto rc = decode_platform_event_message_resp(response, respMsgLen,
63                                                      &completionCode, &status);
64         if (rc || completionCode)
65         {
66             error(
67                 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}",
68                 "RC", rc, "CC", static_cast<unsigned>(completionCode));
69         }
70     };
71 
72     rc = handler->registerRequest(
73         mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
74         std::move(requestMsg), std::move(platformEventMessageResponseHandler));
75     if (rc)
76     {
77         error("Failed to send the platform event message");
78     }
79 }
80 
81 void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
82                                            const DbusObjMaps& dbusMaps)
83 {
84     // Encode PLDM platform event msg to indicate a state sensor change.
85     // DSP0248_1.2.0 Table 19
86     if (!dbusMaps.contains(sensorId))
87     {
88         // this is not an error condition, if we end up here
89         // that means that the sensor with the sensor id has
90         // custom behaviour(or probably an oem sensor) in
91         // sending events that cannot be captured via standard
92         // dbus-json infastructure
93         return;
94     }
95 
96     size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
97     const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
98     for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
99     {
100         std::vector<uint8_t> sensorEventDataVec{};
101         sensorEventDataVec.resize(sensorEventSize);
102         auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
103             sensorEventDataVec.data());
104         eventData->sensor_id = sensorId;
105         eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
106         eventData->event_class[0] = offset;
107         eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
108         eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
109 
110         const auto& dbusMapping = dbusMappings[offset];
111         const auto& dbusValueMapping = dbusValMaps[offset];
112         auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
113             pldm::utils::DBusHandler::getBus(),
114             propertiesChanged(dbusMapping.objectPath.c_str(),
115                               dbusMapping.interface.c_str()),
116             [this, sensorEventDataVec, dbusValueMapping,
117              dbusMapping](auto& msg) mutable {
118                 DbusChangedProps props{};
119                 std::string intf;
120                 msg.read(intf, props);
121                 if (!props.contains(dbusMapping.propertyName))
122                 {
123                     return;
124                 }
125                 for (const auto& itr : dbusValueMapping)
126                 {
127                     bool findValue = false;
128                     if (dbusMapping.propertyType == "string")
129                     {
130                         std::string src = std::get<std::string>(itr.second);
131                         std::string dst = std::get<std::string>(
132                             props.at(dbusMapping.propertyName));
133 
134                         auto values = pldm::utils::split(src, "||", " ");
135                         for (auto& value : values)
136                         {
137                             if (value == dst)
138                             {
139                                 findValue = true;
140                                 break;
141                             }
142                         }
143                     }
144                     else
145                     {
146                         findValue =
147                             itr.second == props.at(dbusMapping.propertyName)
148                                 ? true
149                                 : false;
150                     }
151 
152                     if (findValue)
153                     {
154                         auto eventData =
155                             reinterpret_cast<struct pldm_sensor_event_data*>(
156                                 sensorEventDataVec.data());
157                         eventData->event_class[1] = itr.first;
158                         eventData->event_class[2] = itr.first;
159                         this->sendEventMsg(PLDM_SENSOR_EVENT,
160                                            sensorEventDataVec);
161                         break;
162                     }
163                 }
164             });
165         stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
166     }
167 }
168 
169 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
170                                         const DbusObjMaps& dbusMaps)
171 {
172     const std::map<Type, sensorEvent> sensorHandlers = {
173         {PLDM_STATE_SENSOR_PDR,
174          [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
175              this->sendStateSensorEvent(sensorId, dbusMaps);
176          }}};
177 
178     pldm_state_sensor_pdr* pdr = nullptr;
179     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
180         pldm_pdr_init(), pldm_pdr_destroy);
181 
182     for (auto pdrType : pdrTypes)
183     {
184         Repo sensorPDRs(sensorPdrRepo.get());
185         getRepoByType(repo, sensorPDRs, pdrType);
186         if (sensorPDRs.empty())
187         {
188             return;
189         }
190 
191         PdrEntry pdrEntry{};
192         auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
193         while (pdrRecord)
194         {
195             pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
196             SensorId sensorId = LE16TOH(pdr->sensor_id);
197             if (sensorHandlers.contains(pdrType))
198             {
199                 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
200             }
201 
202             pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
203         }
204     }
205 }
206 
207 } // namespace state_sensor
208 } // namespace pldm
209