George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 1 | #include "dbus_to_event_handler.hpp" |
| 2 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 3 | #include "libpldmresponder/pdr.hpp" |
| 4 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
| 6 | |
| 7 | PHOSPHOR_LOG2_USING; |
| 8 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 9 | namespace pldm |
| 10 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 11 | using namespace pldm::responder; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 12 | using namespace pldm::responder::pdr; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 13 | using namespace pldm::responder::pdr_utils; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 21 | DbusToPLDMEvent::DbusToPLDMEvent( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 22 | int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 23 | pldm::requester::Handler<pldm::requester::Request>* handler) : |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 24 | mctp_fd(mctp_fd), |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 25 | mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 26 | {} |
| 27 | |
| 28 | void DbusToPLDMEvent::sendEventMsg(uint8_t eventType, |
| 29 | const std::vector<uint8_t>& eventDataVec) |
| 30 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 31 | auto instanceId = instanceIdDb.next(mctp_eid); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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( |
ArchanaKakani | 6c39c7a | 2022-12-05 04:36:35 -0600 | [diff] [blame] | 38 | instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType, |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 39 | eventDataVec.data(), eventDataVec.size(), request, |
| 40 | eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
| 41 | if (rc != PLDM_SUCCESS) |
| 42 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 43 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 44 | error("Failed to encode_platform_event_message_req, rc = {RC}", "RC", |
| 45 | rc); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 49 | auto platformEventMessageResponseHandler = |
| 50 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 51 | if (response == nullptr || !respMsgLen) |
| 52 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 53 | error("Failed to receive response for platform event message"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | uint8_t completionCode{}; |
| 57 | uint8_t status{}; |
| 58 | auto rc = decode_platform_event_message_resp(response, respMsgLen, |
| 59 | &completionCode, &status); |
| 60 | if (rc || completionCode) |
| 61 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 62 | error( |
| 63 | "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}", |
| 64 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 65 | } |
| 66 | }; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 67 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 68 | rc = handler->registerRequest( |
| 69 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, |
| 70 | std::move(requestMsg), std::move(platformEventMessageResponseHandler)); |
| 71 | if (rc) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 72 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 73 | error("Failed to send the platform event message"); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 82 | if (!dbusMaps.contains(sensorId)) |
| 83 | { |
Manojkiran Eda | f34867d | 2021-12-12 15:15:07 +0530 | [diff] [blame] | 84 | // this is not an error condition, if we end up here |
| 85 | // that means that the sensor with the sensor id has |
| 86 | // custom behaviour(or probably an oem sensor) in |
| 87 | // sending events that cannot be captured via standard |
| 88 | // dbus-json infastructure |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 92 | size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1; |
| 93 | const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId); |
| 94 | for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset) |
| 95 | { |
| 96 | std::vector<uint8_t> sensorEventDataVec{}; |
| 97 | sensorEventDataVec.resize(sensorEventSize); |
| 98 | auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>( |
| 99 | sensorEventDataVec.data()); |
| 100 | eventData->sensor_id = sensorId; |
| 101 | eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE; |
| 102 | eventData->event_class[0] = offset; |
| 103 | eventData->event_class[1] = PLDM_SENSOR_UNKNOWN; |
| 104 | eventData->event_class[2] = PLDM_SENSOR_UNKNOWN; |
| 105 | |
| 106 | const auto& dbusMapping = dbusMappings[offset]; |
| 107 | const auto& dbusValueMapping = dbusValMaps[offset]; |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 108 | auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>( |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 109 | pldm::utils::DBusHandler::getBus(), |
| 110 | propertiesChanged(dbusMapping.objectPath.c_str(), |
| 111 | dbusMapping.interface.c_str()), |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 112 | [this, sensorEventDataVec, dbusValueMapping, dbusMapping, sensorId, |
| 113 | offset](auto& msg) mutable { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 114 | DbusChangedProps props{}; |
| 115 | std::string intf; |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 116 | uint8_t previousState = PLDM_SENSOR_UNKNOWN; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 117 | msg.read(intf, props); |
| 118 | if (!props.contains(dbusMapping.propertyName)) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | for (const auto& itr : dbusValueMapping) |
| 123 | { |
| 124 | bool findValue = false; |
| 125 | if (dbusMapping.propertyType == "string") |
George Liu | 681715c | 2021-11-25 16:00:55 +0800 | [diff] [blame] | 126 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 127 | std::string src = std::get<std::string>(itr.second); |
| 128 | std::string dst = std::get<std::string>( |
| 129 | props.at(dbusMapping.propertyName)); |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 130 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 131 | auto values = pldm::utils::split(src, "||", " "); |
| 132 | for (auto& value : values) |
| 133 | { |
| 134 | if (value == dst) |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 135 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 136 | findValue = true; |
| 137 | break; |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 138 | } |
| 139 | } |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 140 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 141 | else |
| 142 | { |
| 143 | findValue = itr.second == props.at(dbusMapping.propertyName) |
| 144 | ? true |
| 145 | : false; |
| 146 | } |
| 147 | |
| 148 | if (findValue) |
| 149 | { |
| 150 | auto eventData = |
| 151 | reinterpret_cast<struct pldm_sensor_event_data*>( |
| 152 | sensorEventDataVec.data()); |
| 153 | eventData->event_class[1] = itr.first; |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 154 | if (sensorCacheMap.contains(sensorId) && |
| 155 | sensorCacheMap[sensorId][offset] != PLDM_SENSOR_UNKNOWN) |
| 156 | { |
| 157 | previousState = sensorCacheMap[sensorId][offset]; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | previousState = itr.first; |
| 162 | } |
| 163 | eventData->event_class[2] = previousState; |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 164 | this->sendEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec); |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 165 | updateSensorCacheMaps(sensorId, offset, previousState); |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | } |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 169 | }); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 170 | stateSensorMatchs.emplace_back(std::move(stateSensorMatch)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo, |
| 175 | const DbusObjMaps& dbusMaps) |
| 176 | { |
| 177 | const std::map<Type, sensorEvent> sensorHandlers = { |
| 178 | {PLDM_STATE_SENSOR_PDR, |
| 179 | [this](SensorId sensorId, const DbusObjMaps& dbusMaps) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 180 | this->sendStateSensorEvent(sensorId, dbusMaps); |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 181 | }}}; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 182 | |
| 183 | pldm_state_sensor_pdr* pdr = nullptr; |
| 184 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo( |
| 185 | pldm_pdr_init(), pldm_pdr_destroy); |
Andrew Jeffery | acb2029 | 2023-06-30 11:47:44 +0930 | [diff] [blame] | 186 | if (!sensorPdrRepo) |
| 187 | { |
| 188 | throw std::runtime_error("Unable to instantiate sensor PDR repository"); |
| 189 | } |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 190 | |
| 191 | for (auto pdrType : pdrTypes) |
| 192 | { |
| 193 | Repo sensorPDRs(sensorPdrRepo.get()); |
| 194 | getRepoByType(repo, sensorPDRs, pdrType); |
| 195 | if (sensorPDRs.empty()) |
| 196 | { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | PdrEntry pdrEntry{}; |
| 201 | auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); |
| 202 | while (pdrRecord) |
| 203 | { |
| 204 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); |
| 205 | SensorId sensorId = LE16TOH(pdr->sensor_id); |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 206 | if (sensorHandlers.contains(pdrType)) |
| 207 | { |
| 208 | sensorHandlers.at(pdrType)(sensorId, dbusMaps); |
| 209 | } |
| 210 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 211 | pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | } // namespace state_sensor |
| 217 | } // namespace pldm |