Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 3 | #include "host_pdr_handler.hpp" |
| 4 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 5 | #include "libpldm/requester/pldm.h" |
| 6 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
| 10 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 11 | #include <fstream> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 12 | |
| 13 | namespace pldm |
| 14 | { |
| 15 | |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 16 | using namespace pldm::utils; |
| 17 | using namespace sdbusplus::bus::match::rules; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 18 | using Json = nlohmann::json; |
| 19 | namespace fs = std::filesystem; |
| 20 | constexpr auto fruJson = "host_frus.json"; |
| 21 | const Json emptyJson{}; |
| 22 | const std::vector<Json> emptyJsonList{}; |
| 23 | |
| 24 | HostPDRHandler::HostPDRHandler(int mctp_fd, uint8_t mctp_eid, |
| 25 | sdeventplus::Event& event, pldm_pdr* repo, |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 26 | const std::string& eventsJsonsDir, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 27 | pldm_entity_association_tree* entityTree, |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 28 | pldm_entity_association_tree* bmcEntityTree, |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 29 | Requester& requester, bool verbose) : |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 30 | mctp_fd(mctp_fd), |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 31 | mctp_eid(mctp_eid), event(event), repo(repo), |
| 32 | stateSensorHandler(eventsJsonsDir), entityTree(entityTree), |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 33 | bmcEntityTree(bmcEntityTree), requester(requester), verbose(verbose) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 34 | { |
| 35 | fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson); |
| 36 | if (fs::exists(hostFruJson)) |
| 37 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 38 | // Note parent entities for entities sent down by the host firmware. |
| 39 | // This will enable a merge of entity associations. |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 40 | try |
| 41 | { |
| 42 | std::ifstream jsonFile(hostFruJson); |
| 43 | auto data = Json::parse(jsonFile, nullptr, false); |
| 44 | if (data.is_discarded()) |
| 45 | { |
| 46 | std::cerr << "Parsing Host FRU json file failed" << std::endl; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | auto entities = data.value("entities", emptyJsonList); |
| 51 | for (auto& entity : entities) |
| 52 | { |
| 53 | EntityType entityType = entity.value("entity_type", 0); |
| 54 | auto parent = entity.value("parent", emptyJson); |
| 55 | pldm_entity p{}; |
| 56 | p.entity_type = parent.value("entity_type", 0); |
| 57 | p.entity_instance_num = parent.value("entity_instance", 0); |
| 58 | parents.emplace(entityType, std::move(p)); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | catch (const std::exception& e) |
| 63 | { |
| 64 | std::cerr << "Parsing Host FRU json file failed, exception = " |
| 65 | << e.what() << std::endl; |
| 66 | } |
| 67 | } |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 68 | |
| 69 | hostOffMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 70 | pldm::utils::DBusHandler::getBus(), |
| 71 | propertiesChanged("/xyz/openbmc_project/state/host0", |
| 72 | "xyz.openbmc_project.State.Host"), |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 73 | [this, repo, entityTree, |
| 74 | bmcEntityTree](sdbusplus::message::message& msg) { |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 75 | DbusChangedProps props{}; |
| 76 | std::string intf; |
| 77 | msg.read(intf, props); |
| 78 | const auto itr = props.find("CurrentHostState"); |
| 79 | if (itr != props.end()) |
| 80 | { |
| 81 | PropertyValue value = itr->second; |
| 82 | auto propVal = std::get<std::string>(value); |
| 83 | if (propVal == "xyz.openbmc_project.State.Host.HostState.Off") |
| 84 | { |
| 85 | pldm_pdr_remove_remote_pdrs(repo); |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 86 | pldm_entity_association_tree_destroy_root(entityTree); |
| 87 | pldm_entity_association_tree_copy_root(bmcEntityTree, |
| 88 | entityTree); |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 89 | this->sensorMap.clear(); |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | }); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 95 | void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles) |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 96 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 97 | pdrRecordHandles.clear(); |
| 98 | pdrRecordHandles = std::move(recordHandles); |
| 99 | |
| 100 | // Defer the actual fetch of PDRs from the host (by queuing the call on the |
| 101 | // main event loop). That way, we can respond to the platform event msg from |
| 102 | // the host firmware. |
| 103 | pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>( |
| 104 | event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this, |
| 105 | std::placeholders::_1)); |
| 106 | } |
| 107 | |
| 108 | void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/) |
| 109 | { |
| 110 | pdrFetchEvent.reset(); |
| 111 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 112 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 113 | PLDM_GET_PDR_REQ_BYTES); |
| 114 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 115 | bool merged = false; |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 116 | PDRList stateSensorPDRs{}; |
| 117 | TLPDRMap tlpdrInfo{}; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 118 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 119 | uint32_t nextRecordHandle{}; |
| 120 | uint32_t recordHandle{}; |
| 121 | bool isFormatRecHandles = false; |
| 122 | if (!pdrRecordHandles.empty()) |
| 123 | { |
| 124 | recordHandle = pdrRecordHandles.front(); |
| 125 | pdrRecordHandles.pop_front(); |
| 126 | isFormatRecHandles = true; |
| 127 | } |
| 128 | |
| 129 | do |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 130 | { |
| 131 | auto instanceId = requester.getInstanceId(mctp_eid); |
| 132 | |
| 133 | auto rc = |
| 134 | encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART, |
| 135 | UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES); |
| 136 | if (rc != PLDM_SUCCESS) |
| 137 | { |
| 138 | requester.markFree(mctp_eid, instanceId); |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 139 | std::cerr << "Failed to encode_get_pdr_req, rc = " << rc |
| 140 | << std::endl; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 141 | return; |
| 142 | } |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 143 | if (verbose) |
| 144 | { |
| 145 | std::cout << "Sending Msg:" << std::endl; |
| 146 | printBuffer(requestMsg, verbose); |
| 147 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 148 | |
| 149 | uint8_t* responseMsg = nullptr; |
| 150 | size_t responseMsgSize{}; |
| 151 | auto requesterRc = |
| 152 | pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), |
| 153 | requestMsg.size(), &responseMsg, &responseMsgSize); |
| 154 | std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{ |
| 155 | responseMsg, std::free}; |
| 156 | requester.markFree(mctp_eid, instanceId); |
| 157 | if (requesterRc != PLDM_REQUESTER_SUCCESS) |
| 158 | { |
| 159 | std::cerr << "Failed to send msg to fetch pdrs, rc = " |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 160 | << requesterRc << std::endl; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 164 | uint8_t completionCode{}; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 165 | uint32_t nextDataTransferHandle{}; |
| 166 | uint8_t transferFlag{}; |
| 167 | uint16_t respCount{}; |
| 168 | uint8_t transferCRC{}; |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 169 | auto responsePtr = |
| 170 | reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get()); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 171 | rc = decode_get_pdr_resp( |
| 172 | responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), |
| 173 | &completionCode, &nextRecordHandle, &nextDataTransferHandle, |
| 174 | &transferFlag, &respCount, nullptr, 0, &transferCRC); |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 175 | |
| 176 | std::vector<uint8_t> responsePDRMsg; |
| 177 | responsePDRMsg.resize(responseMsgSize); |
| 178 | memcpy(responsePDRMsg.data(), responsePtr, responsePDRMsg.size()); |
| 179 | if (verbose) |
| 180 | { |
| 181 | std::cout << "Receiving Msg:" << std::endl; |
| 182 | printBuffer(responsePDRMsg, verbose); |
| 183 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 184 | if (rc != PLDM_SUCCESS) |
| 185 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 186 | std::cerr << "Failed to decode_get_pdr_resp, rc = " << rc |
| 187 | << std::endl; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | { |
| 191 | std::vector<uint8_t> pdr(respCount, 0); |
| 192 | rc = decode_get_pdr_resp( |
| 193 | responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), |
| 194 | &completionCode, &nextRecordHandle, &nextDataTransferHandle, |
| 195 | &transferFlag, &respCount, pdr.data(), respCount, &transferCRC); |
| 196 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 197 | { |
| 198 | std::cerr << "Failed to decode_get_pdr_resp: " |
| 199 | << "rc=" << rc |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 200 | << ", cc=" << static_cast<unsigned>(completionCode) |
| 201 | << std::endl; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 205 | // Process the PDR host firmware sent us. The most common action |
| 206 | // is to add the PDR to the the BMC's PDR repo. |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 207 | auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data()); |
| 208 | if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION) |
| 209 | { |
| 210 | mergeEntityAssociations(pdr); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 211 | merged = true; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 212 | } |
| 213 | else |
| 214 | { |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 215 | if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR) |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 216 | { |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 217 | auto tlpdr = |
| 218 | reinterpret_cast<const pldm_terminus_locator_pdr*>( |
| 219 | pdr.data()); |
| 220 | tlpdrInfo.emplace( |
| 221 | static_cast<pdr::TerminusHandle>( |
| 222 | tlpdr->terminus_handle), |
| 223 | static_cast<pdr::TerminusID>(tlpdr->tid)); |
| 224 | } |
| 225 | else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR) |
| 226 | { |
| 227 | stateSensorPDRs.emplace_back(pdr); |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 228 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 229 | pldm_pdr_add(repo, pdr.data(), respCount, 0, true); |
| 230 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 231 | } |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 232 | |
| 233 | recordHandle = nextRecordHandle; |
| 234 | if (!pdrRecordHandles.empty()) |
| 235 | { |
| 236 | recordHandle = pdrRecordHandles.front(); |
| 237 | pdrRecordHandles.pop_front(); |
| 238 | } |
| 239 | else if (isFormatRecHandles) |
| 240 | { |
| 241 | break; |
| 242 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 243 | } |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 244 | } while (recordHandle); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 245 | |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 246 | parseStateSensorPDRs(stateSensorPDRs, tlpdrInfo); |
| 247 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 248 | if (merged) |
| 249 | { |
| 250 | // We have merged host's entity association PDRs with our own. Send an |
| 251 | // event to the host firmware to indicate the same. |
| 252 | sendPDRRepositoryChgEvent( |
| 253 | std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)), |
| 254 | FORMAT_IS_PDR_HANDLES); |
| 255 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 258 | int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry, |
| 259 | pdr::EventState state) |
| 260 | { |
| 261 | auto rc = stateSensorHandler.eventAction(entry, state); |
| 262 | if (rc != PLDM_SUCCESS) |
| 263 | { |
| 264 | std::cerr << "Failed to fetch and update D-bus property, rc = " << rc |
| 265 | << std::endl; |
| 266 | return rc; |
| 267 | } |
| 268 | return PLDM_SUCCESS; |
| 269 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 270 | bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent) |
| 271 | { |
| 272 | auto found = parents.find(type); |
| 273 | if (found != parents.end()) |
| 274 | { |
| 275 | parent.entity_type = found->second.entity_type; |
| 276 | parent.entity_instance_num = found->second.entity_instance_num; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr) |
| 284 | { |
| 285 | size_t numEntities{}; |
| 286 | pldm_entity* entities = nullptr; |
| 287 | bool merged = false; |
| 288 | auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>( |
| 289 | const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr)); |
| 290 | |
| 291 | pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities, |
| 292 | &entities); |
| 293 | for (size_t i = 0; i < numEntities; ++i) |
| 294 | { |
| 295 | pldm_entity parent{}; |
| 296 | if (getParent(entities[i].entity_type, parent)) |
| 297 | { |
| 298 | auto node = pldm_entity_association_tree_find(entityTree, &parent); |
| 299 | if (node) |
| 300 | { |
| 301 | pldm_entity_association_tree_add(entityTree, &entities[i], node, |
| 302 | entityPdr->association_type); |
| 303 | merged = true; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | free(entities); |
| 308 | |
| 309 | if (merged) |
| 310 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 311 | // Update our PDR repo with the merged entity association PDRs |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 312 | pldm_entity_association_pdr_add(entityTree, repo, true); |
| 313 | } |
| 314 | } |
| 315 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 316 | void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, |
| 317 | uint8_t eventDataFormat) |
| 318 | { |
| 319 | assert(eventDataFormat == FORMAT_IS_PDR_HANDLES); |
| 320 | |
| 321 | // Extract from the PDR repo record handles of PDRs we want the host |
| 322 | // to pull up. |
| 323 | std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED}; |
| 324 | std::vector<uint8_t> numsOfChangeEntries(1); |
| 325 | std::vector<std::vector<ChangeEntry>> changeEntries( |
| 326 | numsOfChangeEntries.size()); |
| 327 | for (auto pdrType : pdrTypes) |
| 328 | { |
| 329 | const pldm_pdr_record* record{}; |
| 330 | do |
| 331 | { |
| 332 | record = pldm_pdr_find_record_by_type(repo, pdrType, record, |
| 333 | nullptr, nullptr); |
| 334 | if (record && pldm_pdr_record_is_remote(record)) |
| 335 | { |
| 336 | changeEntries[0].push_back( |
| 337 | pldm_pdr_get_record_handle(repo, record)); |
| 338 | } |
| 339 | } while (record); |
| 340 | } |
| 341 | if (changeEntries.empty()) |
| 342 | { |
| 343 | return; |
| 344 | } |
| 345 | numsOfChangeEntries[0] = changeEntries[0].size(); |
| 346 | |
| 347 | // Encode PLDM platform event msg to indicate a PDR repo change. |
| 348 | size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH + |
| 349 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH + |
| 350 | changeEntries[0].size() * sizeof(uint32_t); |
| 351 | std::vector<uint8_t> eventDataVec{}; |
| 352 | eventDataVec.resize(maxSize); |
| 353 | auto eventData = |
| 354 | reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>( |
| 355 | eventDataVec.data()); |
| 356 | size_t actualSize{}; |
| 357 | auto firstEntry = changeEntries[0].data(); |
| 358 | auto rc = encode_pldm_pdr_repository_chg_event_data( |
| 359 | eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(), |
| 360 | &firstEntry, eventData, &actualSize, maxSize); |
| 361 | if (rc != PLDM_SUCCESS) |
| 362 | { |
| 363 | std::cerr |
| 364 | << "Failed to encode_pldm_pdr_repository_chg_event_data, rc = " |
| 365 | << rc << std::endl; |
| 366 | return; |
| 367 | } |
| 368 | auto instanceId = requester.getInstanceId(mctp_eid); |
| 369 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 370 | PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 371 | actualSize); |
| 372 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 373 | rc = encode_platform_event_message_req( |
| 374 | instanceId, 1, 0, PLDM_PDR_REPOSITORY_CHG_EVENT, eventDataVec.data(), |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 375 | actualSize, request, |
| 376 | actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 377 | if (rc != PLDM_SUCCESS) |
| 378 | { |
| 379 | requester.markFree(mctp_eid, instanceId); |
| 380 | std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc |
| 381 | << std::endl; |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // Send up the event to host. |
| 386 | uint8_t* responseMsg = nullptr; |
| 387 | size_t responseMsgSize{}; |
| 388 | auto requesterRc = |
| 389 | pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(), |
| 390 | &responseMsg, &responseMsgSize); |
| 391 | requester.markFree(mctp_eid, instanceId); |
| 392 | if (requesterRc != PLDM_REQUESTER_SUCCESS) |
| 393 | { |
| 394 | std::cerr << "Failed to send msg to report pdrs, rc = " << requesterRc |
| 395 | << std::endl; |
| 396 | return; |
| 397 | } |
| 398 | uint8_t completionCode{}; |
| 399 | uint8_t status{}; |
| 400 | auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg); |
| 401 | rc = decode_platform_event_message_resp( |
| 402 | responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode, |
| 403 | &status); |
| 404 | free(responseMsg); |
| 405 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 406 | { |
| 407 | std::cerr << "Failed to decode_platform_event_message_resp: " |
| 408 | << "rc=" << rc |
| 409 | << ", cc=" << static_cast<unsigned>(completionCode) |
| 410 | << std::endl; |
| 411 | } |
| 412 | } |
| 413 | |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 414 | void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs, |
| 415 | const TLPDRMap& tlpdrInfo) |
| 416 | { |
| 417 | for (const auto& pdr : stateSensorPDRs) |
| 418 | { |
| 419 | SensorEntry sensorEntry{}; |
| 420 | const auto& [terminusHandle, sensorID, sensorInfo] = |
| 421 | responder::pdr_utils::parseStateSensorPDR(pdr); |
| 422 | sensorEntry.sensorID = sensorID; |
| 423 | try |
| 424 | { |
| 425 | sensorEntry.terminusID = tlpdrInfo.at(terminusHandle); |
| 426 | } |
| 427 | // If there is no mapping for terminusHandle assign the reserved TID |
| 428 | // value of 0xFF to indicate that. |
| 429 | catch (const std::out_of_range& e) |
| 430 | { |
| 431 | sensorEntry.terminusID = PLDM_TID_RESERVED; |
| 432 | } |
| 433 | sensorMap.emplace(sensorEntry, std::move(sensorInfo)); |
| 434 | } |
| 435 | } |
| 436 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 437 | } // namespace pldm |