Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 1 | #include "host_pdr_handler.hpp" |
| 2 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 3 | #include <assert.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 4 | #include <libpldm/pldm.h> |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 5 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 6 | #include <nlohmann/json.hpp> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 8 | #include <sdeventplus/clock.hpp> |
| 9 | #include <sdeventplus/exception.hpp> |
| 10 | #include <sdeventplus/source/io.hpp> |
| 11 | #include <sdeventplus/source/time.hpp> |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 12 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 13 | #include <fstream> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 14 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 15 | PHOSPHOR_LOG2_USING; |
| 16 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 17 | namespace pldm |
| 18 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 19 | using namespace pldm::responder::events; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 20 | using namespace pldm::utils; |
| 21 | using namespace sdbusplus::bus::match::rules; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 22 | using Json = nlohmann::json; |
| 23 | namespace fs = std::filesystem; |
| 24 | constexpr auto fruJson = "host_frus.json"; |
| 25 | const Json emptyJson{}; |
| 26 | const std::vector<Json> emptyJsonList{}; |
| 27 | |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 28 | template <typename T> |
| 29 | uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr) |
| 30 | { |
| 31 | T* var = nullptr; |
| 32 | if (std::is_same<T, pldm_pdr_fru_record_set>::value) |
| 33 | { |
| 34 | var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr)); |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | var = (T*)(pdr.data()); |
| 39 | } |
| 40 | if (var != nullptr) |
| 41 | { |
| 42 | return var->terminus_handle; |
| 43 | } |
| 44 | return TERMINUS_HANDLE; |
| 45 | } |
| 46 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 47 | HostPDRHandler::HostPDRHandler( |
| 48 | int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, pldm_pdr* repo, |
| 49 | const std::string& eventsJsonsDir, pldm_entity_association_tree* entityTree, |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 50 | pldm_entity_association_tree* bmcEntityTree, |
| 51 | pldm::InstanceIdDb& instanceIdDb, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 52 | pldm::requester::Handler<pldm::requester::Request>* handler) : |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 53 | mctp_fd(mctp_fd), |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 54 | mctp_eid(mctp_eid), event(event), repo(repo), |
| 55 | stateSensorHandler(eventsJsonsDir), entityTree(entityTree), |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 56 | bmcEntityTree(bmcEntityTree), instanceIdDb(instanceIdDb), handler(handler) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 57 | { |
| 58 | fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson); |
| 59 | if (fs::exists(hostFruJson)) |
| 60 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 61 | // Note parent entities for entities sent down by the host firmware. |
| 62 | // This will enable a merge of entity associations. |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 63 | try |
| 64 | { |
| 65 | std::ifstream jsonFile(hostFruJson); |
| 66 | auto data = Json::parse(jsonFile, nullptr, false); |
| 67 | if (data.is_discarded()) |
| 68 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 69 | error("Parsing Host FRU json file failed"); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 70 | } |
| 71 | else |
| 72 | { |
| 73 | auto entities = data.value("entities", emptyJsonList); |
| 74 | for (auto& entity : entities) |
| 75 | { |
| 76 | EntityType entityType = entity.value("entity_type", 0); |
| 77 | auto parent = entity.value("parent", emptyJson); |
| 78 | pldm_entity p{}; |
| 79 | p.entity_type = parent.value("entity_type", 0); |
| 80 | p.entity_instance_num = parent.value("entity_instance", 0); |
| 81 | parents.emplace(entityType, std::move(p)); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | catch (const std::exception& e) |
| 86 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 87 | error("Parsing Host FRU json file failed, exception = {ERR_EXCEP}", |
| 88 | "ERR_EXCEP", e.what()); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 89 | } |
| 90 | } |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 91 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 92 | hostOffMatch = std::make_unique<sdbusplus::bus::match_t>( |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 93 | pldm::utils::DBusHandler::getBus(), |
| 94 | propertiesChanged("/xyz/openbmc_project/state/host0", |
| 95 | "xyz.openbmc_project.State.Host"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 96 | [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 97 | DbusChangedProps props{}; |
| 98 | std::string intf; |
| 99 | msg.read(intf, props); |
| 100 | const auto itr = props.find("CurrentHostState"); |
| 101 | if (itr != props.end()) |
| 102 | { |
| 103 | PropertyValue value = itr->second; |
| 104 | auto propVal = std::get<std::string>(value); |
| 105 | if (propVal == "xyz.openbmc_project.State.Host.HostState.Off") |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 106 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 107 | // Delete all the remote terminus information |
| 108 | std::erase_if(tlPDRInfo, [](const auto& item) { |
| 109 | auto const& [key, value] = item; |
| 110 | return key != TERMINUS_HANDLE; |
| 111 | }); |
| 112 | pldm_pdr_remove_remote_pdrs(repo); |
| 113 | pldm_entity_association_tree_destroy_root(entityTree); |
| 114 | pldm_entity_association_tree_copy_root(bmcEntityTree, |
| 115 | entityTree); |
| 116 | this->sensorMap.clear(); |
| 117 | this->responseReceived = false; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 118 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 119 | } |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 120 | }); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 123 | void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles) |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 124 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 125 | pdrRecordHandles.clear(); |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 126 | modifiedPDRRecordHandles.clear(); |
| 127 | |
| 128 | if (isHostPdrModified) |
| 129 | { |
| 130 | modifiedPDRRecordHandles = std::move(recordHandles); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | pdrRecordHandles = std::move(recordHandles); |
| 135 | } |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 136 | |
| 137 | // Defer the actual fetch of PDRs from the host (by queuing the call on the |
| 138 | // main event loop). That way, we can respond to the platform event msg from |
| 139 | // the host firmware. |
| 140 | pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>( |
| 141 | event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this, |
| 142 | std::placeholders::_1)); |
| 143 | } |
| 144 | |
| 145 | void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/) |
| 146 | { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 147 | getHostPDR(); |
| 148 | } |
| 149 | |
| 150 | void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle) |
| 151 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 152 | pdrFetchEvent.reset(); |
| 153 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 154 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 155 | PLDM_GET_PDR_REQ_BYTES); |
| 156 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 157 | uint32_t recordHandle{}; |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 158 | if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) && |
| 159 | isHostPdrModified) |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 160 | { |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 161 | recordHandle = modifiedPDRRecordHandles.front(); |
| 162 | modifiedPDRRecordHandles.pop_front(); |
| 163 | } |
| 164 | else if (!nextRecordHandle && (!pdrRecordHandles.empty())) |
| 165 | { |
| 166 | recordHandle = pdrRecordHandles.front(); |
| 167 | pdrRecordHandles.pop_front(); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 168 | } |
| 169 | else |
| 170 | { |
| 171 | recordHandle = nextRecordHandle; |
| 172 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 173 | auto instanceId = instanceIdDb.next(mctp_eid); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 174 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 175 | auto rc = encode_get_pdr_req(instanceId, recordHandle, 0, |
| 176 | PLDM_GET_FIRSTPART, UINT16_MAX, 0, request, |
| 177 | PLDM_GET_PDR_REQ_BYTES); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 178 | if (rc != PLDM_SUCCESS) |
| 179 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 180 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 181 | error("Failed to encode_get_pdr_req, rc = {RC}", "RC", rc); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 182 | return; |
| 183 | } |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 184 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 185 | rc = handler->registerRequest( |
| 186 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR, |
| 187 | std::move(requestMsg), |
| 188 | std::move(std::bind_front(&HostPDRHandler::processHostPDRs, this))); |
| 189 | if (rc) |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 190 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 191 | error("Failed to send the GetPDR request to Host"); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 192 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 195 | int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry, |
| 196 | pdr::EventState state) |
| 197 | { |
| 198 | auto rc = stateSensorHandler.eventAction(entry, state); |
| 199 | if (rc != PLDM_SUCCESS) |
| 200 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 201 | error("Failed to fetch and update D-bus property, rc = {RC}", "RC", rc); |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 202 | return rc; |
| 203 | } |
| 204 | return PLDM_SUCCESS; |
| 205 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 206 | bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent) |
| 207 | { |
| 208 | auto found = parents.find(type); |
| 209 | if (found != parents.end()) |
| 210 | { |
| 211 | parent.entity_type = found->second.entity_type; |
| 212 | parent.entity_instance_num = found->second.entity_instance_num; |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr) |
| 220 | { |
| 221 | size_t numEntities{}; |
| 222 | pldm_entity* entities = nullptr; |
| 223 | bool merged = false; |
| 224 | auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>( |
| 225 | const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr)); |
| 226 | |
| 227 | pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities, |
| 228 | &entities); |
| 229 | for (size_t i = 0; i < numEntities; ++i) |
| 230 | { |
| 231 | pldm_entity parent{}; |
| 232 | if (getParent(entities[i].entity_type, parent)) |
| 233 | { |
| 234 | auto node = pldm_entity_association_tree_find(entityTree, &parent); |
| 235 | if (node) |
| 236 | { |
George Liu | 64a8f0f | 2021-06-12 10:56:11 +0800 | [diff] [blame] | 237 | pldm_entity_association_tree_add(entityTree, &entities[i], |
| 238 | 0xFFFF, node, |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 239 | entityPdr->association_type); |
| 240 | merged = true; |
| 241 | } |
| 242 | } |
| 243 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 244 | |
| 245 | if (merged) |
| 246 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 247 | // Update our PDR repo with the merged entity association PDRs |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 248 | pldm_entity_node* node = nullptr; |
| 249 | pldm_find_entity_ref_in_tree(entityTree, entities[0], &node); |
| 250 | if (node == nullptr) |
| 251 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 252 | error("could not find referrence of the entity in the tree"); |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | { |
Andrew Jeffery | 1fd3c51 | 2023-07-03 13:02:03 +0930 | [diff] [blame] | 256 | int rc = pldm_entity_association_pdr_add_from_node_check( |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 257 | node, repo, &entities, numEntities, true, TERMINUS_HANDLE); |
Andrew Jeffery | 1fd3c51 | 2023-07-03 13:02:03 +0930 | [diff] [blame] | 258 | if (rc) |
| 259 | { |
| 260 | error( |
| 261 | "Failed to add entity association PDR from node: {LIBPLDM_ERROR}", |
| 262 | "LIBPLDM_ERROR", rc); |
| 263 | } |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 264 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 265 | } |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 266 | free(entities); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 269 | void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, |
| 270 | uint8_t eventDataFormat) |
| 271 | { |
| 272 | assert(eventDataFormat == FORMAT_IS_PDR_HANDLES); |
| 273 | |
| 274 | // Extract from the PDR repo record handles of PDRs we want the host |
| 275 | // to pull up. |
| 276 | std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED}; |
| 277 | std::vector<uint8_t> numsOfChangeEntries(1); |
| 278 | std::vector<std::vector<ChangeEntry>> changeEntries( |
| 279 | numsOfChangeEntries.size()); |
| 280 | for (auto pdrType : pdrTypes) |
| 281 | { |
| 282 | const pldm_pdr_record* record{}; |
| 283 | do |
| 284 | { |
| 285 | record = pldm_pdr_find_record_by_type(repo, pdrType, record, |
| 286 | nullptr, nullptr); |
| 287 | if (record && pldm_pdr_record_is_remote(record)) |
| 288 | { |
| 289 | changeEntries[0].push_back( |
| 290 | pldm_pdr_get_record_handle(repo, record)); |
| 291 | } |
| 292 | } while (record); |
| 293 | } |
| 294 | if (changeEntries.empty()) |
| 295 | { |
| 296 | return; |
| 297 | } |
| 298 | numsOfChangeEntries[0] = changeEntries[0].size(); |
| 299 | |
| 300 | // Encode PLDM platform event msg to indicate a PDR repo change. |
| 301 | size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH + |
| 302 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH + |
| 303 | changeEntries[0].size() * sizeof(uint32_t); |
| 304 | std::vector<uint8_t> eventDataVec{}; |
| 305 | eventDataVec.resize(maxSize); |
| 306 | auto eventData = |
| 307 | reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>( |
| 308 | eventDataVec.data()); |
| 309 | size_t actualSize{}; |
| 310 | auto firstEntry = changeEntries[0].data(); |
| 311 | auto rc = encode_pldm_pdr_repository_chg_event_data( |
| 312 | eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(), |
| 313 | &firstEntry, eventData, &actualSize, maxSize); |
| 314 | if (rc != PLDM_SUCCESS) |
| 315 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 316 | error("Failed to encode_pldm_pdr_repository_chg_event_data, rc = {RC}", |
| 317 | "RC", rc); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 318 | return; |
| 319 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 320 | auto instanceId = instanceIdDb.next(mctp_eid); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 321 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 322 | PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 323 | actualSize); |
| 324 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 325 | rc = encode_platform_event_message_req( |
ArchanaKakani | 6c39c7a | 2022-12-05 04:36:35 -0600 | [diff] [blame] | 326 | instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT, |
| 327 | eventDataVec.data(), actualSize, request, |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 328 | actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 329 | if (rc != PLDM_SUCCESS) |
| 330 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 331 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 332 | error("Failed to encode_platform_event_message_req, rc = {RC}", "RC", |
| 333 | rc); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 337 | auto platformEventMessageResponseHandler = |
| 338 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 339 | if (response == nullptr || !respMsgLen) |
| 340 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 341 | error( |
| 342 | "Failed to receive response for the PDR repository changed event"); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
| 346 | uint8_t completionCode{}; |
| 347 | uint8_t status{}; |
| 348 | auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response); |
Pavithra Barithaya | 54b5a56 | 2021-09-27 06:07:10 -0500 | [diff] [blame] | 349 | auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen, |
| 350 | &completionCode, &status); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 351 | if (rc || completionCode) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 352 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 353 | error( |
| 354 | "Failed to decode_platform_event_message_resp: {RC}, cc = {CC}", |
| 355 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 356 | } |
| 357 | }; |
| 358 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 359 | rc = handler->registerRequest( |
Sampa Misra | 626c565 | 2021-08-11 10:28:48 -0500 | [diff] [blame] | 360 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 361 | std::move(requestMsg), std::move(platformEventMessageResponseHandler)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 362 | if (rc) |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 363 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 364 | error("Failed to send the PDR repository changed event request"); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 368 | void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs) |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 369 | { |
| 370 | for (const auto& pdr : stateSensorPDRs) |
| 371 | { |
| 372 | SensorEntry sensorEntry{}; |
| 373 | const auto& [terminusHandle, sensorID, sensorInfo] = |
| 374 | responder::pdr_utils::parseStateSensorPDR(pdr); |
| 375 | sensorEntry.sensorID = sensorID; |
| 376 | try |
| 377 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 378 | sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle)); |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 379 | } |
| 380 | // If there is no mapping for terminusHandle assign the reserved TID |
| 381 | // value of 0xFF to indicate that. |
| 382 | catch (const std::out_of_range& e) |
| 383 | { |
| 384 | sensorEntry.terminusID = PLDM_TID_RESERVED; |
| 385 | } |
| 386 | sensorMap.emplace(sensorEntry, std::move(sensorInfo)); |
| 387 | } |
| 388 | } |
| 389 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 390 | void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/, |
| 391 | const pldm_msg* response, |
| 392 | size_t respMsgLen) |
| 393 | { |
| 394 | static bool merged = false; |
| 395 | static PDRList stateSensorPDRs{}; |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 396 | uint32_t nextRecordHandle{}; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 397 | uint8_t tlEid = 0; |
| 398 | bool tlValid = true; |
| 399 | uint32_t rh = 0; |
| 400 | uint16_t terminusHandle = 0; |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 401 | uint16_t pdrTerminusHandle = 0; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 402 | uint8_t tid = 0; |
| 403 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 404 | uint8_t completionCode{}; |
| 405 | uint32_t nextDataTransferHandle{}; |
| 406 | uint8_t transferFlag{}; |
| 407 | uint16_t respCount{}; |
| 408 | uint8_t transferCRC{}; |
| 409 | if (response == nullptr || !respMsgLen) |
| 410 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 411 | error("Failed to receive response for the GetPDR command"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
| 415 | auto rc = decode_get_pdr_resp( |
| 416 | response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode, |
| 417 | &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount, |
| 418 | nullptr, 0, &transferCRC); |
| 419 | std::vector<uint8_t> responsePDRMsg; |
| 420 | responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr)); |
| 421 | memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 422 | if (rc != PLDM_SUCCESS) |
| 423 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 424 | error("Failed to decode_get_pdr_resp, rc = {RC}", "RC", rc); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | std::vector<uint8_t> pdr(respCount, 0); |
| 430 | rc = decode_get_pdr_resp(response, respMsgLen, &completionCode, |
| 431 | &nextRecordHandle, &nextDataTransferHandle, |
| 432 | &transferFlag, &respCount, pdr.data(), |
| 433 | respCount, &transferCRC); |
| 434 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 435 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 436 | error("Failed to decode_get_pdr_resp: rc = {RC}, cc = {CC}", "RC", |
| 437 | rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | else |
| 441 | { |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 442 | // when nextRecordHandle is 0, we need the recordHandle of the last |
| 443 | // PDR and not 0-1. |
| 444 | if (!nextRecordHandle) |
| 445 | { |
| 446 | rh = nextRecordHandle; |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | rh = nextRecordHandle - 1; |
| 451 | } |
| 452 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 453 | auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data()); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 454 | if (!rh) |
| 455 | { |
| 456 | rh = pdrHdr->record_handle; |
| 457 | } |
| 458 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 459 | if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION) |
| 460 | { |
| 461 | this->mergeEntityAssociations(pdr); |
| 462 | merged = true; |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR) |
| 467 | { |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 468 | pdrTerminusHandle = |
| 469 | extractTerminusHandle<pldm_terminus_locator_pdr>(pdr); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 470 | auto tlpdr = |
| 471 | reinterpret_cast<const pldm_terminus_locator_pdr*>( |
| 472 | pdr.data()); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 473 | |
| 474 | terminusHandle = tlpdr->terminus_handle; |
| 475 | tid = tlpdr->tid; |
| 476 | auto terminus_locator_type = tlpdr->terminus_locator_type; |
| 477 | if (terminus_locator_type == |
| 478 | PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID) |
| 479 | { |
| 480 | auto locatorValue = reinterpret_cast< |
| 481 | const pldm_terminus_locator_type_mctp_eid*>( |
| 482 | tlpdr->terminus_locator_value); |
| 483 | tlEid = static_cast<uint8_t>(locatorValue->eid); |
| 484 | } |
| 485 | if (tlpdr->validity == 0) |
| 486 | { |
| 487 | tlValid = false; |
| 488 | } |
Pavithra Barithaya | 52aad39 | 2022-08-02 04:18:52 -0500 | [diff] [blame] | 489 | for (const auto& terminusMap : tlPDRInfo) |
| 490 | { |
| 491 | if ((terminusHandle == (terminusMap.first)) && |
| 492 | (get<1>(terminusMap.second) == tlEid) && |
| 493 | (get<2>(terminusMap.second) == tlpdr->validity)) |
| 494 | { |
| 495 | // TL PDR already present with same validity don't |
| 496 | // add the PDR to the repo just return |
| 497 | return; |
| 498 | } |
| 499 | } |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 500 | tlPDRInfo.insert_or_assign( |
| 501 | tlpdr->terminus_handle, |
| 502 | std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 503 | } |
| 504 | else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR) |
| 505 | { |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 506 | pdrTerminusHandle = |
| 507 | extractTerminusHandle<pldm_state_sensor_pdr>(pdr); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 508 | stateSensorPDRs.emplace_back(pdr); |
| 509 | } |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 510 | else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET) |
| 511 | { |
| 512 | pdrTerminusHandle = |
| 513 | extractTerminusHandle<pldm_pdr_fru_record_set>(pdr); |
| 514 | } |
| 515 | else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR) |
| 516 | { |
| 517 | pdrTerminusHandle = |
| 518 | extractTerminusHandle<pldm_state_effecter_pdr>(pdr); |
| 519 | } |
| 520 | else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR) |
| 521 | { |
| 522 | pdrTerminusHandle = |
| 523 | extractTerminusHandle<pldm_numeric_effecter_value_pdr>( |
| 524 | pdr); |
| 525 | } |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 526 | // if the TLPDR is invalid update the repo accordingly |
| 527 | if (!tlValid) |
| 528 | { |
| 529 | pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid, |
| 530 | tlValid); |
| 531 | } |
| 532 | else |
| 533 | { |
Andrew Jeffery | 64f37fe | 2023-07-03 15:41:13 +0930 | [diff] [blame] | 534 | rc = pldm_pdr_add_check(repo, pdr.data(), respCount, true, |
| 535 | pdrTerminusHandle, &rh); |
| 536 | if (rc) |
| 537 | { |
| 538 | // pldm_pdr_add() assert()ed on failure to add a PDR. |
| 539 | throw std::runtime_error("Failed to add PDR"); |
| 540 | } |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 541 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | if (!nextRecordHandle) |
| 546 | { |
| 547 | /*received last record*/ |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 548 | this->parseStateSensorPDRs(stateSensorPDRs); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 549 | if (isHostUp()) |
| 550 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 551 | this->setHostSensorState(stateSensorPDRs); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 552 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 553 | stateSensorPDRs.clear(); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 554 | if (merged) |
| 555 | { |
| 556 | merged = false; |
| 557 | deferredPDRRepoChgEvent = |
| 558 | std::make_unique<sdeventplus::source::Defer>( |
| 559 | event, |
| 560 | std::bind( |
| 561 | std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)), |
| 562 | this, std::placeholders::_1)); |
| 563 | } |
| 564 | } |
| 565 | else |
| 566 | { |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 567 | if (modifiedPDRRecordHandles.empty() && isHostPdrModified) |
| 568 | { |
| 569 | isHostPdrModified = false; |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | deferredFetchPDREvent = |
| 574 | std::make_unique<sdeventplus::source::Defer>( |
| 575 | event, |
| 576 | std::bind( |
| 577 | std::mem_fn((&HostPDRHandler::_processFetchPDREvent)), |
| 578 | this, nextRecordHandle, std::placeholders::_1)); |
| 579 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | void HostPDRHandler::_processPDRRepoChgEvent( |
| 584 | sdeventplus::source::EventBase& /*source */) |
| 585 | { |
| 586 | deferredPDRRepoChgEvent.reset(); |
| 587 | this->sendPDRRepositoryChgEvent( |
| 588 | std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)), |
| 589 | FORMAT_IS_PDR_HANDLES); |
| 590 | } |
| 591 | |
| 592 | void HostPDRHandler::_processFetchPDREvent( |
| 593 | uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */) |
| 594 | { |
| 595 | deferredFetchPDREvent.reset(); |
| 596 | if (!this->pdrRecordHandles.empty()) |
| 597 | { |
| 598 | nextRecordHandle = this->pdrRecordHandles.front(); |
| 599 | this->pdrRecordHandles.pop_front(); |
| 600 | } |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 601 | if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty())) |
| 602 | { |
| 603 | nextRecordHandle = this->modifiedPDRRecordHandles.front(); |
| 604 | this->modifiedPDRRecordHandles.pop_front(); |
| 605 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 606 | this->getHostPDR(nextRecordHandle); |
| 607 | } |
| 608 | |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 609 | void HostPDRHandler::setHostFirmwareCondition() |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 610 | { |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 611 | responseReceived = false; |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 612 | auto instanceId = instanceIdDb.next(mctp_eid); |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 613 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 614 | PLDM_GET_VERSION_REQ_BYTES); |
| 615 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 616 | auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART, |
| 617 | PLDM_BASE, request); |
| 618 | if (rc != PLDM_SUCCESS) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 619 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 620 | error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC", |
| 621 | lg2::hex, rc); |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 622 | instanceIdDb.free(mctp_eid, instanceId); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 626 | auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/, |
| 627 | const pldm_msg* response, |
| 628 | size_t respMsgLen) { |
| 629 | if (response == nullptr || !respMsgLen) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 630 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 631 | error( |
| 632 | "Failed to receive response for getPLDMVersion command, Host seems to be off"); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 633 | return; |
| 634 | } |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 635 | info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex, |
| 636 | static_cast<uint16_t>(response->payload[0])); |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 637 | this->responseReceived = true; |
| 638 | getHostPDR(); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 639 | }; |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 640 | rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE, |
| 641 | PLDM_GET_PLDM_VERSION, std::move(requestMsg), |
| 642 | std::move(getPLDMVersionHandler)); |
| 643 | if (rc) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 644 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 645 | error("Failed to discover Host state. Assuming Host as off"); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | bool HostPDRHandler::isHostUp() |
| 650 | { |
| 651 | return responseReceived; |
| 652 | } |
| 653 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 654 | void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 655 | { |
| 656 | for (const auto& stateSensorPDR : stateSensorPDRs) |
| 657 | { |
| 658 | auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>( |
| 659 | stateSensorPDR.data()); |
| 660 | |
| 661 | if (!pdr) |
| 662 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 663 | error("Failed to get State sensor PDR"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 664 | pldm::utils::reportError( |
| 665 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | uint16_t sensorId = pdr->sensor_id; |
| 670 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 671 | for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 672 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 673 | if (terminusHandle == pdr->terminus_handle) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 674 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 675 | if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 676 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 677 | mctp_eid = std::get<1>(terminusInfo); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | bitfield8_t sensorRearm; |
| 681 | sensorRearm.byte = 0; |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 682 | uint8_t tid = std::get<0>(terminusInfo); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 683 | |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 684 | auto instanceId = instanceIdDb.next(mctp_eid); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 685 | std::vector<uint8_t> requestMsg( |
| 686 | sizeof(pldm_msg_hdr) + |
| 687 | PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES); |
| 688 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 689 | auto rc = encode_get_state_sensor_readings_req( |
| 690 | instanceId, sensorId, sensorRearm, 0, request); |
| 691 | |
| 692 | if (rc != PLDM_SUCCESS) |
| 693 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 694 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 695 | error( |
| 696 | "Failed to encode_get_state_sensor_readings_req, rc = {RC}", |
| 697 | "RC", rc); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 698 | pldm::utils::reportError( |
| 699 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 700 | return; |
| 701 | } |
| 702 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 703 | auto getStateSensorReadingRespHandler = |
| 704 | [=, this](mctp_eid_t /*eid*/, const pldm_msg* response, |
| 705 | size_t respMsgLen) { |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 706 | if (response == nullptr || !respMsgLen) |
| 707 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 708 | error( |
| 709 | "Failed to receive response for getStateSensorReading command"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | std::array<get_sensor_state_field, 8> stateField{}; |
| 713 | uint8_t completionCode = 0; |
| 714 | uint8_t comp_sensor_count = 0; |
| 715 | |
| 716 | auto rc = decode_get_state_sensor_readings_resp( |
| 717 | response, respMsgLen, &completionCode, |
| 718 | &comp_sensor_count, stateField.data()); |
| 719 | |
| 720 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 721 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 722 | error( |
| 723 | "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}", |
| 724 | "RC", rc, "CC", |
| 725 | static_cast<unsigned>(completionCode)); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 726 | pldm::utils::reportError( |
| 727 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 728 | } |
| 729 | |
| 730 | uint8_t eventState; |
| 731 | uint8_t previousEventState; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 732 | |
RIYA DIXIT | bb5fda4 | 2022-09-27 06:48:08 -0500 | [diff] [blame] | 733 | for (uint8_t sensorOffset = 0; |
| 734 | sensorOffset < comp_sensor_count; sensorOffset++) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 735 | { |
RIYA DIXIT | bb5fda4 | 2022-09-27 06:48:08 -0500 | [diff] [blame] | 736 | eventState = stateField[sensorOffset].present_state; |
| 737 | previousEventState = |
| 738 | stateField[sensorOffset].previous_state; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 739 | |
| 740 | emitStateSensorEventSignal(tid, sensorId, sensorOffset, |
| 741 | eventState, |
| 742 | previousEventState); |
| 743 | |
| 744 | SensorEntry sensorEntry{tid, sensorId}; |
| 745 | |
| 746 | pldm::pdr::EntityInfo entityInfo{}; |
| 747 | pldm::pdr::CompositeSensorStates |
| 748 | compositeSensorStates{}; |
| 749 | |
| 750 | try |
| 751 | { |
| 752 | std::tie(entityInfo, compositeSensorStates) = |
| 753 | lookupSensorInfo(sensorEntry); |
| 754 | } |
| 755 | catch (const std::out_of_range& e) |
| 756 | { |
| 757 | try |
| 758 | { |
| 759 | sensorEntry.terminusID = PLDM_TID_RESERVED; |
| 760 | std::tie(entityInfo, compositeSensorStates) = |
| 761 | lookupSensorInfo(sensorEntry); |
| 762 | } |
| 763 | catch (const std::out_of_range& e) |
| 764 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 765 | error("No mapping for the events"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | if (sensorOffset > compositeSensorStates.size()) |
| 770 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 771 | error("Error Invalid data, Invalid sensor offset"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 772 | return; |
| 773 | } |
| 774 | |
| 775 | const auto& possibleStates = |
| 776 | compositeSensorStates[sensorOffset]; |
| 777 | if (possibleStates.find(eventState) == |
| 778 | possibleStates.end()) |
| 779 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 780 | error("Error invalid_data, Invalid event state"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 781 | return; |
| 782 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 783 | const auto& [containerId, entityType, |
| 784 | entityInstance] = entityInfo; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 785 | pldm::responder::events::StateSensorEntry |
| 786 | stateSensorEntry{containerId, entityType, |
| 787 | entityInstance, sensorOffset}; |
| 788 | handleStateSensorEvent(stateSensorEntry, eventState); |
| 789 | } |
| 790 | }; |
| 791 | |
| 792 | rc = handler->registerRequest( |
| 793 | mctp_eid, instanceId, PLDM_PLATFORM, |
| 794 | PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg), |
| 795 | std::move(getStateSensorReadingRespHandler)); |
| 796 | |
| 797 | if (rc != PLDM_SUCCESS) |
| 798 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 799 | error( |
| 800 | "Failed to send request to get State sensor reading on Host"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 806 | } // namespace pldm |