George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 1 | #include "dbus_to_event_handler.hpp" |
| 2 | |
| 3 | #include "libpldm/requester/pldm.h" |
| 4 | |
| 5 | #include "libpldmresponder/pdr.hpp" |
| 6 | |
| 7 | namespace pldm |
| 8 | { |
| 9 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 10 | using namespace pldm::dbus_api; |
| 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( |
| 22 | int mctp_fd, uint8_t mctp_eid, Requester& requester, |
| 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), |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 25 | mctp_eid(mctp_eid), requester(requester), 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 | { |
| 31 | auto instanceId = requester.getInstanceId(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*/, 0 /*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 | requester.markFree(mctp_eid, instanceId); |
| 44 | std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc |
| 45 | << std::endl; |
| 46 | return; |
| 47 | } |
| 48 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 49 | auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/, |
| 50 | const pldm_msg* response, |
| 51 | size_t respMsgLen) { |
| 52 | if (response == nullptr || !respMsgLen) |
| 53 | { |
| 54 | std::cerr |
| 55 | << "Failed to receive response for platform event message \n"; |
| 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 | std::cerr << "Failed to decode_platform_event_message_resp: " |
| 65 | << "rc=" << rc |
| 66 | << ", cc=" << static_cast<unsigned>(completionCode) |
| 67 | << std::endl; |
| 68 | } |
| 69 | }; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 70 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 71 | rc = handler->registerRequest( |
| 72 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, |
| 73 | std::move(requestMsg), std::move(platformEventMessageResponseHandler)); |
| 74 | if (rc) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 75 | { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 76 | std::cerr << "Failed to send the platform event message \n"; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | void DbusToPLDMEvent::sendStateSensorEvent(SensorId sensorId, |
| 81 | const DbusObjMaps& dbusMaps) |
| 82 | { |
| 83 | // Encode PLDM platform event msg to indicate a state sensor change. |
| 84 | // DSP0248_1.2.0 Table 19 |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 85 | if (!dbusMaps.contains(sensorId)) |
| 86 | { |
Manojkiran Eda | f34867d | 2021-12-12 15:15:07 +0530 | [diff] [blame] | 87 | // this is not an error condition, if we end up here |
| 88 | // that means that the sensor with the sensor id has |
| 89 | // custom behaviour(or probably an oem sensor) in |
| 90 | // sending events that cannot be captured via standard |
| 91 | // dbus-json infastructure |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 95 | size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1; |
| 96 | const auto& [dbusMappings, dbusValMaps] = dbusMaps.at(sensorId); |
| 97 | for (uint8_t offset = 0; offset < dbusMappings.size(); ++offset) |
| 98 | { |
| 99 | std::vector<uint8_t> sensorEventDataVec{}; |
| 100 | sensorEventDataVec.resize(sensorEventSize); |
| 101 | auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>( |
| 102 | sensorEventDataVec.data()); |
| 103 | eventData->sensor_id = sensorId; |
| 104 | eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE; |
| 105 | eventData->event_class[0] = offset; |
| 106 | eventData->event_class[1] = PLDM_SENSOR_UNKNOWN; |
| 107 | eventData->event_class[2] = PLDM_SENSOR_UNKNOWN; |
| 108 | |
| 109 | const auto& dbusMapping = dbusMappings[offset]; |
| 110 | const auto& dbusValueMapping = dbusValMaps[offset]; |
| 111 | auto stateSensorMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 112 | pldm::utils::DBusHandler::getBus(), |
| 113 | propertiesChanged(dbusMapping.objectPath.c_str(), |
| 114 | dbusMapping.interface.c_str()), |
George Liu | 681715c | 2021-11-25 16:00:55 +0800 | [diff] [blame] | 115 | [this, sensorEventDataVec, dbusValueMapping, |
| 116 | dbusMapping](auto& msg) mutable { |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 117 | DbusChangedProps props{}; |
| 118 | std::string intf; |
| 119 | msg.read(intf, props); |
George Liu | 681715c | 2021-11-25 16:00:55 +0800 | [diff] [blame] | 120 | if (!props.contains(dbusMapping.propertyName)) |
| 121 | { |
| 122 | return; |
| 123 | } |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 124 | for (const auto& itr : dbusValueMapping) |
| 125 | { |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 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 (auto& value : values) |
| 135 | { |
| 136 | if (value == dst) |
| 137 | { |
| 138 | findValue = true; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | findValue = |
| 146 | itr.second == props.at(dbusMapping.propertyName) |
| 147 | ? true |
| 148 | : false; |
| 149 | } |
| 150 | |
| 151 | if (findValue) |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 152 | { |
| 153 | auto eventData = |
| 154 | reinterpret_cast<struct pldm_sensor_event_data*>( |
| 155 | sensorEventDataVec.data()); |
| 156 | eventData->event_class[1] = itr.first; |
| 157 | eventData->event_class[2] = itr.first; |
| 158 | this->sendEventMsg(PLDM_SENSOR_EVENT, |
| 159 | sensorEventDataVec); |
George Liu | 9999991 | 2021-11-25 16:39:57 +0800 | [diff] [blame] | 160 | break; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | }); |
| 164 | stateSensorMatchs.emplace_back(std::move(stateSensorMatch)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void DbusToPLDMEvent::listenSensorEvent(const pdr_utils::Repo& repo, |
| 169 | const DbusObjMaps& dbusMaps) |
| 170 | { |
| 171 | const std::map<Type, sensorEvent> sensorHandlers = { |
| 172 | {PLDM_STATE_SENSOR_PDR, |
| 173 | [this](SensorId sensorId, const DbusObjMaps& dbusMaps) { |
| 174 | this->sendStateSensorEvent(sensorId, dbusMaps); |
| 175 | }}}; |
| 176 | |
| 177 | pldm_state_sensor_pdr* pdr = nullptr; |
| 178 | std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> sensorPdrRepo( |
| 179 | pldm_pdr_init(), pldm_pdr_destroy); |
| 180 | |
| 181 | for (auto pdrType : pdrTypes) |
| 182 | { |
| 183 | Repo sensorPDRs(sensorPdrRepo.get()); |
| 184 | getRepoByType(repo, sensorPDRs, pdrType); |
| 185 | if (sensorPDRs.empty()) |
| 186 | { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | PdrEntry pdrEntry{}; |
| 191 | auto pdrRecord = sensorPDRs.getFirstRecord(pdrEntry); |
| 192 | while (pdrRecord) |
| 193 | { |
| 194 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdrEntry.data); |
| 195 | SensorId sensorId = LE16TOH(pdr->sensor_id); |
George Liu | bd5e2ea | 2021-04-22 20:33:06 +0800 | [diff] [blame] | 196 | if (sensorHandlers.contains(pdrType)) |
| 197 | { |
| 198 | sensorHandlers.at(pdrType)(sensorId, dbusMaps); |
| 199 | } |
| 200 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 201 | pdrRecord = sensorPDRs.getNextRecord(pdrRecord, pdrEntry); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } // namespace state_sensor |
| 207 | } // namespace pldm |