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 if (!dbusMaps.contains(sensorId)) 84 { 85 std::cerr << "Invalid sensor ID : " << sensorId << std::endl; 86 return; 87 } 88 89 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1; 90 const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId); 91 for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset) 92 { 93 std::vector<uint8_t> sensorEventDataVec{}; 94 sensorEventDataVec.resize(sensorEventSize); 95 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>( 96 sensorEventDataVec.data()); 97 eventData->sensor_id = sensorId; 98 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE; 99 eventData->event_class[0] = offset; 100 eventData->event_class[1] = PLDM_SENSOR_UNKNOWN; 101 eventData->event_class[2] = PLDM_SENSOR_UNKNOWN; 102 103 const auto& dbusMapping = dbusMappings[offset]; 104 const auto& dbusValueMapping = dbusValMaps[offset]; 105 auto stateSensorMatch = std::make_unique<sdbusplus::bus::match::match>( 106 pldm::utils::DBusHandler::getBus(), 107 propertiesChanged(dbusMapping.objectPath.c_str(), 108 dbusMapping.interface.c_str()), 109 [this, sensorEventDataVec, dbusValueMapping](auto& msg) mutable { 110 DbusChangedProps props{}; 111 std::string intf; 112 msg.read(intf, props); 113 const auto& first = props.begin(); 114 for (const auto& itr : dbusValueMapping) 115 { 116 if (itr.second == first->second) 117 { 118 auto eventData = 119 reinterpret_cast<struct pldm_sensor_event_data*>( 120 sensorEventDataVec.data()); 121 eventData->event_class[1] = itr.first; 122 eventData->event_class[2] = itr.first; 123 this->sendEventMsg(PLDM_SENSOR_EVENT, 124 sensorEventDataVec); 125 } 126 } 127 }); 128 stateSensorMatchs.emplace_back(std::move(stateSensorMatch)); 129 } 130 } 131 132 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo, 133 const DbusObjMaps& dbusMaps) 134 { 135 const std::map<Type, sensorEvent> sensorHandlers = { 136 {PLDM_STATE_SENSOR_PDR, 137 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) { 138 this->sendStateSensorEvent(sensorId, dbusMaps); 139 }}}; 140 141 pldm_state_sensor_pdr* pdr = nullptr; 142 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo( 143 pldm_pdr_init(), pldm_pdr_destroy); 144 145 for (auto pdrType : pdrTypes) 146 { 147 Repo sensorPDRs(sensorPdrRepo.get()); 148 getRepoByType(repo, sensorPDRs, pdrType); 149 if (sensorPDRs.empty()) 150 { 151 return; 152 } 153 154 PdrEntry pdrEntry{}; 155 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); 156 while (pdrRecord) 157 { 158 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); 159 SensorId sensorId = LE16TOH(pdr->sensor_id); 160 if (sensorHandlers.contains(pdrType)) 161 { 162 sensorHandlers.at(pdrType)(sensorId, dbusMaps); 163 } 164 165 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); 166 } 167 } 168 } 169 170 } // namespace state_sensor 171 } // namespace pldm 172