1 #include "dbus_to_event_handler.hpp"
2 
3 #include "libpldm/requester/pldm.h"
4 
5 #include "libpldmresponder/pdr.hpp"
6 
7 namespace pldm
8 {
9 
10 using namespace pldm::dbus_api;
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 
21 DbusToPLDMEvent::DbusToPLDMEvent(
22     int mctp_fd, uint8_t mctp_eid, Requester& requester,
23     pldm::requester::Handler<pldm::requester::Request>* handler) :
24     mctp_fd(mctp_fd),
25     mctp_eid(mctp_eid), requester(requester), handler(handler)
26 {}
27 
28 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
29                                    const std::vector<uint8_t>& eventDataVec)
30 {
31     auto instanceId = requester.getInstanceId(mctp_eid);
32     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
33                                     PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
34                                     eventDataVec.size());
35     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
36 
37     auto rc = encode_platform_event_message_req(
38         instanceId, 1 /*formatVersion*/, 0 /*tId*/, eventType,
39         eventDataVec.data(), eventDataVec.size(), request,
40         eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
41     if (rc != PLDM_SUCCESS)
42     {
43         requester.markFree(mctp_eid, instanceId);
44         std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
45                   << std::endl;
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             std::cerr
55                 << "Failed to receive response for platform event message \n";
56             return;
57         }
58         uint8_t completionCode{};
59         uint8_t status{};
60         auto rc = decode_platform_event_message_resp(response, respMsgLen,
61                                                      &completionCode, &status);
62         if (rc || completionCode)
63         {
64             std::cerr << "Failed to decode_platform_event_message_resp: "
65                       << "rc=" << rc
66                       << ", cc=" << static_cast<unsigned>(completionCode)
67                       << std::endl;
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         std::cerr << "Failed to send the platform event message \n";
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         std::cerr << "Invalid sensor ID : " << sensorId << std::endl;
88         return;
89     }
90 
91     size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
92     const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
93     for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
94     {
95         std::vector<uint8_t> sensorEventDataVec{};
96         sensorEventDataVec.resize(sensorEventSize);
97         auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
98             sensorEventDataVec.data());
99         eventData->sensor_id = sensorId;
100         eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
101         eventData->event_class[0] = offset;
102         eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
103         eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
104 
105         const auto& dbusMapping = dbusMappings[offset];
106         const auto& dbusValueMapping = dbusValMaps[offset];
107         auto stateSensorMatch = std::make_unique<sdbusplus::bus::match::match>(
108             pldm::utils::DBusHandler::getBus(),
109             propertiesChanged(dbusMapping.objectPath.c_str(),
110                               dbusMapping.interface.c_str()),
111             [this, sensorEventDataVec, dbusValueMapping,
112              dbusMapping](auto& msg) mutable {
113                 DbusChangedProps props{};
114                 std::string intf;
115                 msg.read(intf, props);
116                 if (!props.contains(dbusMapping.propertyName))
117                 {
118                     return;
119                 }
120                 for (const auto& itr : dbusValueMapping)
121                 {
122                     if (itr.second == props.at(dbusMapping.propertyName))
123                     {
124                         auto eventData =
125                             reinterpret_cast<struct pldm_sensor_event_data*>(
126                                 sensorEventDataVec.data());
127                         eventData->event_class[1] = itr.first;
128                         eventData->event_class[2] = itr.first;
129                         this->sendEventMsg(PLDM_SENSOR_EVENT,
130                                            sensorEventDataVec);
131                     }
132                 }
133             });
134         stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
135     }
136 }
137 
138 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
139                                         const DbusObjMaps& dbusMaps)
140 {
141     const std::map<Type, sensorEvent> sensorHandlers = {
142         {PLDM_STATE_SENSOR_PDR,
143          [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
144              this->sendStateSensorEvent(sensorId, dbusMaps);
145          }}};
146 
147     pldm_state_sensor_pdr* pdr = nullptr;
148     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
149         pldm_pdr_init(), pldm_pdr_destroy);
150 
151     for (auto pdrType : pdrTypes)
152     {
153         Repo sensorPDRs(sensorPdrRepo.get());
154         getRepoByType(repo, sensorPDRs, pdrType);
155         if (sensorPDRs.empty())
156         {
157             return;
158         }
159 
160         PdrEntry pdrEntry{};
161         auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
162         while (pdrRecord)
163         {
164             pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
165             SensorId sensorId = LE16TOH(pdr->sensor_id);
166             if (sensorHandlers.contains(pdrType))
167             {
168                 sensorHandlers.at(pdrType)(sensorId, dbusMaps);
169             }
170 
171             pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
172         }
173     }
174 }
175 
176 } // namespace state_sensor
177 } // namespace pldm
178