1 #include "dbus_to_event_handler.hpp"
2 
3 #include "libpldmresponder/pdr.hpp"
4 
5 #include <libpldm/pldm.h>
6 
7 namespace pldm
8 {
9 using namespace pldm::dbus_api;
10 using namespace pldm::responder;
11 using namespace pldm::responder::pdr;
12 using namespace pldm::responder::pdr_utils;
13 using namespace pldm::utils;
14 using namespace sdbusplus::bus::match::rules;
15 
16 namespace state_sensor
17 {
18 const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
19 
20 DbusToPLDMEvent::DbusToPLDMEvent(
21     int mctp_fd, uint8_t mctp_eid, Requester& requester,
22     pldm::requester::Handler<pldm::requester::Request>* handler) :
23     mctp_fd(mctp_fd),
24     mctp_eid(mctp_eid), requester(requester), handler(handler)
25 {}
26 
27 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
28                                    const std::vector<uint8_t>& eventDataVec)
29 {
30     auto instanceId = requester.getInstanceId(mctp_eid);
31     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
32                                     PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
33                                     eventDataVec.size());
34     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
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         requester.markFree(mctp_eid, instanceId);
43         std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
44                   << std::endl;
45         return;
46     }
47 
48     auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
49                                                   const pldm_msg* response,
50                                                   size_t respMsgLen) {
51         if (response == nullptr || !respMsgLen)
52         {
53             std::cerr
54                 << "Failed to receive response for platform event message \n";
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             std::cerr << "Failed to decode_platform_event_message_resp: "
64                       << "rc=" << rc
65                       << ", cc=" << static_cast<unsigned>(completionCode)
66                       << std::endl;
67         }
68     };
69 
70     rc = handler->registerRequest(
71         mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
72         std::move(requestMsg), std::move(platformEventMessageResponseHandler));
73     if (rc)
74     {
75         std::cerr << "Failed to send the platform event message \n";
76     }
77 }
78 
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 (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
97     {
98         std::vector<uint8_t> sensorEventDataVec{};
99         sensorEventDataVec.resize(sensorEventSize);
100         auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
101             sensorEventDataVec.data());
102         eventData->sensor_id = sensorId;
103         eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
104         eventData->event_class[0] = offset;
105         eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
106         eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
107 
108         const auto& dbusMapping = dbusMappings[offset];
109         const auto& dbusValueMapping = dbusValMaps[offset];
110         auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>(
111             pldm::utils::DBusHandler::getBus(),
112             propertiesChanged(dbusMapping.objectPath.c_str(),
113                               dbusMapping.interface.c_str()),
114             [this, sensorEventDataVec, dbusValueMapping,
115              dbusMapping](auto& msg) mutable {
116                 DbusChangedProps props{};
117                 std::string intf;
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 (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 =
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,
158                                            sensorEventDataVec);
159                         break;
160                     }
161                 }
162             });
163         stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
164     }
165 }
166 
167 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
168                                         const DbusObjMaps& dbusMaps)
169 {
170     const std::map<Type, sensorEvent> sensorHandlers = {
171         {PLDM_STATE_SENSOR_PDR,
172          [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
173              this->sendStateSensorEvent(sensorId, dbusMaps);
174          }}};
175 
176     pldm_state_sensor_pdr* pdr = nullptr;
177     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
178         pldm_pdr_init(), pldm_pdr_destroy);
179 
180     for (auto pdrType : pdrTypes)
181     {
182         Repo sensorPDRs(sensorPdrRepo.get());
183         getRepoByType(repo, sensorPDRs, pdrType);
184         if (sensorPDRs.empty())
185         {
186             return;
187         }
188 
189         PdrEntry pdrEntry{};
190         auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
191         while (pdrRecord)
192         {
193             pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
194             SensorId sensorId = LE16TOH(pdr->sensor_id);
195             if (sensorHandlers.contains(pdrType))
196             {
197                 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
198             }
199 
200             pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
201         }
202     }
203 }
204 
205 } // namespace state_sensor
206 } // namespace pldm
207