1 #include "dbus_to_event_handler.hpp" 2 3 #include "libpldmresponder/pdr.hpp" 4 5 #include <libpldm/pldm.h> 6 7 #include <phosphor-logging/lg2.hpp> 8 9 PHOSPHOR_LOG2_USING; 10 11 namespace pldm 12 { 13 using namespace pldm::responder; 14 using namespace pldm::responder::pdr; 15 using namespace pldm::responder::pdr_utils; 16 using namespace pldm::utils; 17 using namespace sdbusplus::bus::match::rules; 18 19 namespace state_sensor 20 { 21 const std::vector<uint8_t> pdrTypes{PLDM_STATE_SENSOR_PDR}; 22 23 DbusToPLDMEvent::DbusToPLDMEvent( 24 int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb, 25 pldm::requester::Handler<pldm::requester::Request>* handler) : 26 mctp_fd(mctp_fd), 27 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler) 28 {} 29 30 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType, 31 const std::vector<uint8_t>& eventDataVec) 32 { 33 auto instanceId = instanceIdDb.next(mctp_eid); 34 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + 35 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 36 eventDataVec.size()); 37 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 38 39 auto rc = encode_platform_event_message_req( 40 instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType, 41 eventDataVec.data(), eventDataVec.size(), request, 42 eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); 43 if (rc != PLDM_SUCCESS) 44 { 45 instanceIdDb.free(mctp_eid, instanceId); 46 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC", 47 rc); 48 return; 49 } 50 51 auto platformEventMessageResponseHandler = 52 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { 53 if (response == nullptr || !respMsgLen) 54 { 55 error("Failed to receive response for platform event message"); 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 error( 65 "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}", 66 "RC", rc, "CC", static_cast<unsigned>(completionCode)); 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 error("Failed to send the platform event message"); 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 = itr.second == props.at(dbusMapping.propertyName) 145 ? true 146 : false; 147 } 148 149 if (findValue) 150 { 151 auto eventData = 152 reinterpret_cast<struct pldm_sensor_event_data*>( 153 sensorEventDataVec.data()); 154 eventData->event_class[1] = itr.first; 155 eventData->event_class[2] = itr.first; 156 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec); 157 break; 158 } 159 } 160 }); 161 stateSensorMatchs.emplace_back(std::move(stateSensorMatch)); 162 } 163 } 164 165 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo, 166 const DbusObjMaps& dbusMaps) 167 { 168 const std::map<Type, sensorEvent> sensorHandlers = { 169 {PLDM_STATE_SENSOR_PDR, 170 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) { 171 this->sendStateSensorEvent(sensorId, dbusMaps); 172 }}}; 173 174 pldm_state_sensor_pdr* pdr = nullptr; 175 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo( 176 pldm_pdr_init(), pldm_pdr_destroy); 177 if (!sensorPdrRepo) 178 { 179 throw std::runtime_error("Unable to instantiate sensor PDR repository"); 180 } 181 182 for (auto pdrType : pdrTypes) 183 { 184 Repo sensorPDRs(sensorPdrRepo.get()); 185 getRepoByType(repo, sensorPDRs, pdrType); 186 if (sensorPDRs.empty()) 187 { 188 return; 189 } 190 191 PdrEntry pdrEntry{}; 192 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); 193 while (pdrRecord) 194 { 195 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); 196 SensorId sensorId = LE16TOH(pdr->sensor_id); 197 if (sensorHandlers.contains(pdrType)) 198 { 199 sensorHandlers.at(pdrType)(sensorId, dbusMaps); 200 } 201 202 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); 203 } 204 } 205 } 206 207 } // namespace state_sensor 208 } // namespace pldm 209