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