1 #include "dbus_to_event_handler.hpp" 2 3 #include "libpldmresponder/pdr.hpp" 4 5 #include <phosphor-logging/lg2.hpp> 6 7 PHOSPHOR_LOG2_USING; 8 9 namespace pldm 10 { 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, pldm::InstanceIdDb& instanceIdDb, 23 pldm::requester::Handler<pldm::requester::Request>* handler) : 24 mctp_eid(mctp_eid), 25 instanceIdDb(instanceIdDb), handler(handler) 26 {} 27 28 void DbusToPLDMEvent::sendEventMsg(uint8_t eventType, 29 const std::vector<uint8_t>& eventDataVec) 30 { 31 auto instanceId = instanceIdDb.next(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*/, TERMINUS_ID /*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 instanceIdDb.free(mctp_eid, instanceId); 44 error( 45 "Failed to encode platform event message request, response code '{RC}'", 46 "RC", rc); 47 return; 48 } 49 50 auto platformEventMessageResponseHandler = 51 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { 52 if (response == nullptr || !respMsgLen) 53 { 54 error("Failed to receive response for platform event message"); 55 return; 56 } 57 uint8_t completionCode{}; 58 uint8_t status{}; 59 auto rc = decode_platform_event_message_resp(response, respMsgLen, 60 &completionCode, &status); 61 if (rc || completionCode) 62 { 63 error( 64 "Failed to decode response of platform event message, response code '{RC}' and completion code '{CC}'", 65 "RC", rc, "CC", completionCode); 66 } 67 }; 68 69 rc = handler->registerRequest( 70 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, 71 std::move(requestMsg), std::move(platformEventMessageResponseHandler)); 72 if (rc) 73 { 74 error("Failed to send the platform event message, response code '{RC}'", 75 "RC", rc); 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, dbusMapping, sensorId, 115 offset](auto& msg) mutable { 116 DbusChangedProps props{}; 117 std::string intf; 118 uint8_t previousState = PLDM_SENSOR_UNKNOWN; 119 msg.read(intf, props); 120 if (!props.contains(dbusMapping.propertyName)) 121 { 122 return; 123 } 124 for (const auto& itr : dbusValueMapping) 125 { 126 bool findValue = false; 127 if (dbusMapping.propertyType == "string") 128 { 129 std::string src = std::get<std::string>(itr.second); 130 std::string dst = std::get<std::string>( 131 props.at(dbusMapping.propertyName)); 132 133 auto values = pldm::utils::split(src, "||", " "); 134 for (const auto& value : values) 135 { 136 if (value == dst) 137 { 138 findValue = true; 139 break; 140 } 141 } 142 } 143 else 144 { 145 findValue = itr.second == props.at(dbusMapping.propertyName) 146 ? true 147 : false; 148 } 149 150 if (findValue) 151 { 152 auto eventData = 153 reinterpret_cast<struct pldm_sensor_event_data*>( 154 sensorEventDataVec.data()); 155 eventData->event_class[1] = itr.first; 156 if (sensorCacheMap.contains(sensorId) && 157 sensorCacheMap[sensorId][offset] != PLDM_SENSOR_UNKNOWN) 158 { 159 previousState = sensorCacheMap[sensorId][offset]; 160 } 161 else 162 { 163 previousState = itr.first; 164 } 165 eventData->event_class[2] = previousState; 166 this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec); 167 updateSensorCacheMaps(sensorId, offset, previousState); 168 break; 169 } 170 } 171 }); 172 stateSensorMatchs.emplace_back(std::move(stateSensorMatch)); 173 } 174 } 175 176 void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo, 177 const DbusObjMaps& dbusMaps) 178 { 179 const std::map<Type, sensorEvent> sensorHandlers = { 180 {PLDM_STATE_SENSOR_PDR, 181 [this](SensorId sensorId, const DbusObjMaps& dbusMaps) { 182 this->sendStateSensorEvent(sensorId, dbusMaps); 183 }}}; 184 185 pldm_state_sensor_pdr* pdr = nullptr; 186 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo( 187 pldm_pdr_init(), pldm_pdr_destroy); 188 if (!sensorPdrRepo) 189 { 190 throw std::runtime_error("Unable to instantiate sensor PDR repository"); 191 } 192 193 for (auto pdrType : pdrTypes) 194 { 195 Repo sensorPDRs(sensorPdrRepo.get()); 196 getRepoByType(repo, sensorPDRs, pdrType); 197 if (sensorPDRs.empty()) 198 { 199 return; 200 } 201 202 PdrEntry pdrEntry{}; 203 auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); 204 while (pdrRecord) 205 { 206 pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); 207 SensorId sensorId = LE16TOH(pdr->sensor_id); 208 if (sensorHandlers.contains(pdrType)) 209 { 210 sensorHandlers.at(pdrType)(sensorId, dbusMaps); 211 } 212 213 pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); 214 } 215 } 216 } 217 218 } // namespace state_sensor 219 } // namespace pldm 220