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