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::responder::pdr;
11 using namespace pldm::utils;
12 using namespace sdbusplus::bus::match::rules;
13 
14 namespace state_sensor
15 {
16 const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR};
17 
18 DbusToPLDMEvent::DbusToPLDMEvent(int mctp_fd, uint8_t mctp_eid,
19                                  Requester& requester) :
20     mctp_fd(mctp_fd),
21     mctp_eid(mctp_eid), requester(requester)
22 {}
23 
24 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType,
25                                    const std::vector<uint8_t>& eventDataVec)
26 {
27     auto instanceId = requester.getInstanceId(mctp_eid);
28     std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
29                                     PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
30                                     eventDataVec.size());
31     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
32 
33     auto rc = encode_platform_event_message_req(
34         instanceId, 1 /*formatVersion*/, 0 /*tId*/, eventType,
35         eventDataVec.data(), eventDataVec.size(), request,
36         eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
37     if (rc != PLDM_SUCCESS)
38     {
39         requester.markFree(mctp_eid, instanceId);
40         std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
41                   << std::endl;
42         return;
43     }
44 
45     uint8_t* responseMsg = nullptr;
46     size_t responseMsgSize{};
47 
48     auto requesterRc =
49         pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(),
50                        &responseMsg, &responseMsgSize);
51     std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{responseMsg,
52                                                                   std::free};
53 
54     requester.markFree(mctp_eid, instanceId);
55     if (requesterRc != PLDM_REQUESTER_SUCCESS)
56     {
57         std::cerr
58             << "Failed to send msg to report state sensor event changes, rc = "
59             << requesterRc << std::endl;
60         return;
61     }
62     uint8_t completionCode{};
63     uint8_t status{};
64     auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
65     rc = decode_platform_event_message_resp(
66         responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode,
67         &status);
68 
69     if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
70     {
71         std::cerr << "Failed to decode_platform_event_message_resp: "
72                   << "rc=" << rc
73                   << ", cc=" << static_cast<unsigned>(completionCode)
74                   << std::endl;
75     }
76 }
77 
78 void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId,
79                                            const DbusObjMaps& dbusMaps)
80 {
81     // Encode PLDM platform event msg to indicate a state sensor change.
82     // DSP0248_1.2.0 Table 19
83     size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1;
84     const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId);
85     for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset)
86     {
87         std::vector<uint8_t> sensorEventDataVec{};
88         sensorEventDataVec.resize(sensorEventSize);
89         auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>(
90             sensorEventDataVec.data());
91         eventData->sensor_id = sensorId;
92         eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
93         eventData->event_class[0] = offset;
94         eventData->event_class[1] = PLDM_SENSOR_UNKNOWN;
95         eventData->event_class[2] = PLDM_SENSOR_UNKNOWN;
96 
97         const auto& dbusMapping = dbusMappings[offset];
98         const auto& dbusValueMapping = dbusValMaps[offset];
99         auto stateSensorMatch = std::make_unique<sdbusplus::bus::match::match>(
100             pldm::utils::DBusHandler::getBus(),
101             propertiesChanged(dbusMapping.objectPath.c_str(),
102                               dbusMapping.interface.c_str()),
103             [this, sensorEventDataVec, dbusValueMapping](auto& msg) mutable {
104                 DbusChangedProps props{};
105                 std::string intf;
106                 msg.read(intf, props);
107                 const auto& first = props.begin();
108                 for (const auto& itr : dbusValueMapping)
109                 {
110                     if (itr.second == first->second)
111                     {
112                         auto eventData =
113                             reinterpret_cast<struct pldm_sensor_event_data*>(
114                                 sensorEventDataVec.data());
115                         eventData->event_class[1] = itr.first;
116                         eventData->event_class[2] = itr.first;
117                         this->sendEventMsg(PLDM_SENSOR_EVENT,
118                                            sensorEventDataVec);
119                     }
120                 }
121             });
122         stateSensorMatchs.emplace_back(std::move(stateSensorMatch));
123     }
124 }
125 
126 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo,
127                                         const DbusObjMaps& dbusMaps)
128 {
129     const std::map<Type, sensorEvent> sensorHandlers = {
130         {PLDM_STATE_SENSOR_PDR,
131          [this](SensorId sensorId, const DbusObjMaps& dbusMaps) {
132              this->sendStateSensorEvent(sensorId, dbusMaps);
133          }}};
134 
135     pldm_state_sensor_pdr* pdr = nullptr;
136     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo(
137         pldm_pdr_init(), pldm_pdr_destroy);
138 
139     for (auto pdrType : pdrTypes)
140     {
141         Repo sensorPDRs(sensorPdrRepo.get());
142         getRepoByType(repo, sensorPDRs, pdrType);
143         if (sensorPDRs.empty())
144         {
145             return;
146         }
147 
148         PdrEntry pdrEntry{};
149         auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry);
150         while (pdrRecord)
151         {
152             pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data);
153             SensorId sensorId = LE16TOH(pdr->sensor_id);
154             sensorHandlers.at(pdrType)(sensorId, dbusMaps);
155             pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry);
156         }
157     }
158 }
159 
160 } // namespace state_sensor
161 } // namespace pldm
162