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