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 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 5 | #include <libpldm/pldm.h> |
| 6 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | |
| 9 | PHOSPHOR_LOG2_USING; |
| 10 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 11 | namespace pldm |
| 12 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 13 | using namespace pldm::responder; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 14 | using namespace pldm::responder::pdr; |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 15 | using namespace pldm::responder::pdr_utils; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 23 | DbusToPLDMEvent::DbusToPLDMEvent( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 24 | int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 25 | pldm::requester::Handler<pldm::requester::Request>* handler) : |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 26 | mctp_fd(mctp_fd), |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 27 | mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), handler(handler) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 28 | {} |
| 29 | |
| 30 | void DbusToPLDMEvent::sendEventMsg(uint8_t eventType, |
| 31 | const std::vector<uint8_t>& eventDataVec) |
| 32 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 33 | auto instanceId = instanceIdDb.next(mctp_eid); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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( |
ArchanaKakani | 6c39c7a | 2022-12-05 04:36:35 -0600 | [diff] [blame] | 40 | instanceId, 1 /*formatVersion*/, TERMINUS_ID /*tId*/, eventType, |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 41 | eventDataVec.data(), eventDataVec.size(), request, |
| 42 | eventDataVec.size() + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
| 43 | if (rc != PLDM_SUCCESS) |
| 44 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame^] | 45 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 46 | error("Failed to encode_platform_event_message_req, rc = {RC}", "RC", |
| 47 | rc); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 51 | auto platformEventMessageResponseHandler = |
| 52 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 53 | if (response == nullptr || !respMsgLen) |
| 54 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 55 | error("Failed to receive response for platform event message"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 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 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 64 | error( |
| 65 | "Failed to decode_platform_event_message_resp: rc = {RC}, cc = {CC}", |
| 66 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 67 | } |
| 68 | }; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 69 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 70 | rc = handler->registerRequest( |
| 71 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, |
| 72 | std::move(requestMsg), std::move(platformEventMessageResponseHandler)); |
| 73 | if (rc) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 74 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 75 | error("Failed to send the platform event message"); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 84 | if (!dbusMaps.contains(sensorId)) |
| 85 | { |
Manojkiran Eda | f34867d | 2021-12-12 15:15:07 +0530 | [diff] [blame] | 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 |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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]; |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 110 | auto stateSensorMatch = std::make_unique<sdbusplus::bus::match_t>( |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 111 | pldm::utils::DBusHandler::getBus(), |
| 112 | propertiesChanged(dbusMapping.objectPath.c_str(), |
| 113 | dbusMapping.interface.c_str()), |
George Liu | 681715c | 2021-11-25 16:00:55 +0800 | [diff] [blame] | 114 | [this, sensorEventDataVec, dbusValueMapping, |
| 115 | dbusMapping](auto& msg) mutable { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 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") |
George Liu | 681715c | 2021-11-25 16:00:55 +0800 | [diff] [blame] | 127 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 128 | std::string src = std::get<std::string>(itr.second); |
| 129 | std::string dst = std::get<std::string>( |
| 130 | props.at(dbusMapping.propertyName)); |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 131 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 132 | auto values = pldm::utils::split(src, "||", " "); |
| 133 | for (auto& value : values) |
| 134 | { |
| 135 | if (value == dst) |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 136 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 137 | findValue = true; |
| 138 | break; |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 139 | } |
| 140 | } |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 141 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 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 | } |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 171 | this->sendStateSensorEvent(sensorId, dbusMaps); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 | |
| 178 | for (auto pdrType : pdrTypes) |
| 179 | { |
| 180 | Repo sensorPDRs(sensorPdrRepo.get()); |
| 181 | getRepoByType(repo, sensorPDRs, pdrType); |
| 182 | if (sensorPDRs.empty()) |
| 183 | { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | PdrEntry pdrEntry{}; |
| 188 | auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); |
| 189 | while (pdrRecord) |
| 190 | { |
| 191 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); |
| 192 | SensorId sensorId = LE16TOH(pdr->sensor_id); |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 193 | if (sensorHandlers.contains(pdrType)) |
| 194 | { |
| 195 | sensorHandlers.at(pdrType)(sensorId, dbusMaps); |
| 196 | } |
| 197 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 198 | pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } // namespace state_sensor |
| 204 | } // namespace pldm |