blob: 61b4ccb40231fb117e123c47548d3f419f6e3d7c [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#include "host_pdr_handler.hpp"
2
Andrew Jeffery4668f5c2024-01-15 14:59:17 +10303#include <libpldm/fru.h>
George Liu682ee182020-12-25 15:24:33 +08004#ifdef OEM_IBM
Andrew Jeffery21f128d2024-01-15 15:34:26 +10305#include <libpldm/oem/ibm/fru.h>
Sagar Srinivas3687e2b2023-04-10 05:08:28 -05006#endif
Kamalkumar Patel14107a12024-06-19 08:50:01 -05007#include "dbus/custom_dbus.hpp"
George Liu682ee182020-12-25 15:24:33 +08008
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05009#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
sampmisr6decfc12021-03-02 11:07:36 +053011#include <sdeventplus/clock.hpp>
12#include <sdeventplus/exception.hpp>
13#include <sdeventplus/source/io.hpp>
14#include <sdeventplus/source/time.hpp>
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050015
Pavithra Barithayab3b84b42024-08-23 11:43:57 +053016#include <cassert>
George Liu6492f522020-06-16 10:34:05 +080017#include <fstream>
George Liu96af8cb2021-07-31 15:23:45 +080018#include <type_traits>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050019
Riya Dixit49cfb132023-03-02 04:26:53 -060020PHOSPHOR_LOG2_USING;
21
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050022namespace pldm
23{
Brad Bishop5079ac42021-08-19 18:35:06 -040024using namespace pldm::responder::events;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050025using namespace pldm::utils;
26using namespace sdbusplus::bus::match::rules;
Sagar Srinivas3687e2b2023-04-10 05:08:28 -050027using namespace pldm::responder::pdr_utils;
Kamalkumar Patel516122e2024-05-07 04:39:32 -050028using namespace pldm::hostbmc::utils;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050029using Json = nlohmann::json;
30namespace fs = std::filesystem;
George Liu682ee182020-12-25 15:24:33 +080031using namespace pldm::dbus;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050032const Json emptyJson{};
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050033
Manojkiran Eda3ca40452021-10-04 22:51:37 +053034template <typename T>
35uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr)
36{
37 T* var = nullptr;
38 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
39 {
40 var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
41 }
42 else
43 {
44 var = (T*)(pdr.data());
45 }
46 if (var != nullptr)
47 {
48 return var->terminus_handle;
49 }
50 return TERMINUS_HANDLE;
51}
52
George Liu96af8cb2021-07-31 15:23:45 +080053template <typename T>
54void updateContainerId(pldm_entity_association_tree* entityTree,
55 std::vector<uint8_t>& pdr)
56{
57 T* t = nullptr;
58 if (entityTree == nullptr)
59 {
60 return;
61 }
62 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
63 {
64 t = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
65 }
66 else
67 {
68 t = (T*)(pdr.data());
69 }
70 if (t == nullptr)
71 {
72 return;
73 }
74
75 pldm_entity entity{t->entity_type, t->entity_instance, t->container_id};
Patrick Williams16c2a0a2024-08-16 15:20:59 -040076 auto node = pldm_entity_association_tree_find_with_locality(
77 entityTree, &entity, true);
George Liu96af8cb2021-07-31 15:23:45 +080078 if (node)
79 {
80 pldm_entity e = pldm_entity_extract(node);
81 t->container_id = e.entity_container_id;
82 }
83}
84
Tom Joseph74f27c72021-05-16 07:58:53 -070085HostPDRHandler::HostPDRHandler(
Andrew Jeffery3dd444d2024-07-25 22:00:27 +093086 int /* mctp_fd */, uint8_t mctp_eid, sdeventplus::Event& event,
87 pldm_pdr* repo, const std::string& eventsJsonsDir,
88 pldm_entity_association_tree* entityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093089 pldm_entity_association_tree* bmcEntityTree,
90 pldm::InstanceIdDb& instanceIdDb,
George Liua881c172021-06-21 18:28:11 +080091 pldm::requester::Handler<pldm::requester::Request>* handler) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040092 mctp_eid(mctp_eid), event(event), repo(repo),
93 stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
94 instanceIdDb(instanceIdDb), handler(handler),
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -050095 entityMaps(parseEntityMap(ENTITY_MAP_JSON)), oemUtilsHandler(nullptr)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050096{
George Liuacf2c8c2021-05-10 14:08:52 +080097 mergedHostParents = false;
Patrick Williams84b790c2022-07-22 19:26:56 -050098 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050099 pldm::utils::DBusHandler::getBus(),
100 propertiesChanged("/xyz/openbmc_project/state/host0",
101 "xyz.openbmc_project.State.Host"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500102 [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400103 DbusChangedProps props{};
104 std::string intf;
105 msg.read(intf, props);
106 const auto itr = props.find("CurrentHostState");
107 if (itr != props.end())
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500108 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400109 PropertyValue value = itr->second;
110 auto propVal = std::get<std::string>(value);
111 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
112 {
113 // Delete all the remote terminus information
114 std::erase_if(tlPDRInfo, [](const auto& item) {
115 const auto& [key, value] = item;
116 return key != TERMINUS_HANDLE;
117 });
Archana Kakanif9355372025-02-04 03:13:24 -0600118 // when the host is powered off, set the availability
119 // state of all the dbus objects to false
120 this->setPresenceFrus();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400121 pldm_pdr_remove_remote_pdrs(repo);
122 pldm_entity_association_tree_destroy_root(entityTree);
123 pldm_entity_association_tree_copy_root(bmcEntityTree,
124 entityTree);
125 this->sensorMap.clear();
126 this->responseReceived = false;
127 this->mergedHostParents = false;
128 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500129 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400130 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500131}
132
Archana Kakanif9355372025-02-04 03:13:24 -0600133void HostPDRHandler::setPresenceFrus()
134{
135 // iterate over all dbus objects
136 for (const auto& [path, entityId] : objPathMap)
137 {
138 CustomDBus::getCustomDBus().setAvailabilityState(path, false);
139 }
140}
141
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500142void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500143{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500144 pdrRecordHandles.clear();
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500145 modifiedPDRRecordHandles.clear();
146
147 if (isHostPdrModified)
148 {
149 modifiedPDRRecordHandles = std::move(recordHandles);
150 }
151 else
152 {
153 pdrRecordHandles = std::move(recordHandles);
154 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500155
156 // Defer the actual fetch of PDRs from the host (by queuing the call on the
157 // main event loop). That way, we can respond to the platform event msg from
158 // the host firmware.
159 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
160 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
161 std::placeholders::_1));
162}
163
164void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
165{
Sampa Misrac0c79482021-06-02 08:01:54 -0500166 getHostPDR();
167}
168
169void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle)
170{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500171 pdrFetchEvent.reset();
172
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400173 std::vector<uint8_t> requestMsg(
174 sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530175 auto request = new (requestMsg.data()) pldm_msg;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500176 uint32_t recordHandle{};
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500177 if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) &&
178 isHostPdrModified)
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500179 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500180 recordHandle = modifiedPDRRecordHandles.front();
181 modifiedPDRRecordHandles.pop_front();
182 }
183 else if (!nextRecordHandle && (!pdrRecordHandles.empty()))
184 {
185 recordHandle = pdrRecordHandles.front();
186 pdrRecordHandles.pop_front();
Sampa Misrac0c79482021-06-02 08:01:54 -0500187 }
188 else
189 {
190 recordHandle = nextRecordHandle;
191 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930192 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misrac0c79482021-06-02 08:01:54 -0500193
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400194 auto rc =
195 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
196 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
Sampa Misrac0c79482021-06-02 08:01:54 -0500197 if (rc != PLDM_SUCCESS)
198 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930199 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500200 error("Failed to encode get pdr request, response code '{RC}'", "RC",
201 rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500202 return;
203 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500204
Sampa Misrac0c79482021-06-02 08:01:54 -0500205 rc = handler->registerRequest(
206 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR,
207 std::move(requestMsg),
Eric Yang70eca962025-05-11 01:48:15 +0800208 [this](mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen) {
209 this->processHostPDRs(eid, response, respMsgLen);
210 });
Sampa Misrac0c79482021-06-02 08:01:54 -0500211 if (rc)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500212 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500213 error(
214 "Failed to send the getPDR request to remote terminus, response code '{RC}'",
215 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500216 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500217}
218
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600219int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
220 pdr::EventState state)
221{
222 auto rc = stateSensorHandler.eventAction(entry, state);
223 if (rc != PLDM_SUCCESS)
224 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500225 error("Failed to fetch and update D-bus property, response code '{RC}'",
226 "RC", rc);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600227 return rc;
228 }
229 return PLDM_SUCCESS;
230}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500231
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500232void HostPDRHandler::mergeEntityAssociations(
233 const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size,
234 [[maybe_unused]] const uint32_t& record_handle)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500235{
236 size_t numEntities{};
237 pldm_entity* entities = nullptr;
238 bool merged = false;
Pavithra Barithaya49575832025-01-29 16:27:30 +0530239 auto entityPdr = new (const_cast<uint8_t*>(pdr.data()) +
240 sizeof(pldm_pdr_hdr)) pldm_pdr_entity_association;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500241
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500242 if (oemPlatformHandler &&
243 oemPlatformHandler->checkRecordHandleInRange(record_handle))
244 {
245 // Adding the remote range PDRs to the repo before merging it
246 uint32_t handle = record_handle;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000247 pldm_pdr_add(repo, pdr.data(), size, true, 0xFFFF, &handle);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500248 }
249
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500250 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
251 &entities);
George Liuacf2c8c2021-05-10 14:08:52 +0800252 if (numEntities > 0)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500253 {
George Liuacf2c8c2021-05-10 14:08:52 +0800254 pldm_entity_node* pNode = nullptr;
255 if (!mergedHostParents)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500256 {
George Liuacf2c8c2021-05-10 14:08:52 +0800257 pNode = pldm_entity_association_tree_find_with_locality(
258 entityTree, &entities[0], false);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500259 }
George Liuacf2c8c2021-05-10 14:08:52 +0800260 else
261 {
262 pNode = pldm_entity_association_tree_find_with_locality(
263 entityTree, &entities[0], true);
264 }
265 if (!pNode)
266 {
267 return;
268 }
269
George Liudf9a6d32020-12-22 16:27:16 +0800270 Entities entityAssoc;
271 entityAssoc.push_back(pNode);
George Liuacf2c8c2021-05-10 14:08:52 +0800272 for (size_t i = 1; i < numEntities; ++i)
273 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500274 bool isUpdateContainerId = true;
275 if (oemPlatformHandler)
276 {
277 isUpdateContainerId =
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600278 checkIfLogicalBitSet(entities[i].entity_container_id);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500279 }
George Liudf9a6d32020-12-22 16:27:16 +0800280 auto node = pldm_entity_association_tree_add_entity(
George Liuacf2c8c2021-05-10 14:08:52 +0800281 entityTree, &entities[i], entities[i].entity_instance_num,
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500282 pNode, entityPdr->association_type, true, isUpdateContainerId,
283 0xFFFF);
284 if (!node)
285 {
286 continue;
287 }
George Liuacf2c8c2021-05-10 14:08:52 +0800288 merged = true;
George Liudf9a6d32020-12-22 16:27:16 +0800289 entityAssoc.push_back(node);
George Liuacf2c8c2021-05-10 14:08:52 +0800290 }
291
292 mergedHostParents = true;
George Liudf9a6d32020-12-22 16:27:16 +0800293 if (merged)
294 {
295 entityAssociations.push_back(entityAssoc);
296 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500297 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500298
299 if (merged)
300 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500301 // Update our PDR repo with the merged entity association PDRs
Sampa Misra719ed392021-06-04 05:15:13 -0500302 pldm_entity_node* node = nullptr;
303 pldm_find_entity_ref_in_tree(entityTree, entities[0], &node);
304 if (node == nullptr)
305 {
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530306 error("Failed to find reference of the entity in the tree");
Sampa Misra719ed392021-06-04 05:15:13 -0500307 }
308 else
309 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500310 int rc = 0;
311 if (oemPlatformHandler)
312 {
313 auto record = oemPlatformHandler->fetchLastBMCRecord(repo);
314
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400315 uint32_t record_handle =
316 pldm_pdr_get_record_handle(repo, record);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500317
318 rc =
319 pldm_entity_association_pdr_add_from_node_with_record_handle(
320 node, repo, &entities, numEntities, true,
321 TERMINUS_HANDLE, (record_handle + 1));
322 }
323 else
324 {
Andrew Jeffery7761bd22024-08-01 13:15:36 +0000325 rc = pldm_entity_association_pdr_add_from_node(
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500326 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
327 }
328
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930329 if (rc)
330 {
331 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500332 "Failed to add entity association PDR from node, response code '{RC}'",
333 "RC", rc);
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930334 }
Sampa Misra719ed392021-06-04 05:15:13 -0500335 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500336 }
Sampa Misra719ed392021-06-04 05:15:13 -0500337 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500338}
339
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500340void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
341 uint8_t eventDataFormat)
342{
343 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
344
345 // Extract from the PDR repo record handles of PDRs we want the host
346 // to pull up.
347 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
348 std::vector<uint8_t> numsOfChangeEntries(1);
349 std::vector<std::vector<ChangeEntry>> changeEntries(
350 numsOfChangeEntries.size());
351 for (auto pdrType : pdrTypes)
352 {
353 const pldm_pdr_record* record{};
354 do
355 {
356 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
357 nullptr, nullptr);
358 if (record && pldm_pdr_record_is_remote(record))
359 {
360 changeEntries[0].push_back(
361 pldm_pdr_get_record_handle(repo, record));
362 }
363 } while (record);
364 }
365 if (changeEntries.empty())
366 {
367 return;
368 }
369 numsOfChangeEntries[0] = changeEntries[0].size();
370
371 // Encode PLDM platform event msg to indicate a PDR repo change.
372 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
373 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
374 changeEntries[0].size() * sizeof(uint32_t);
375 std::vector<uint8_t> eventDataVec{};
376 eventDataVec.resize(maxSize);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530377 auto eventData = new (eventDataVec.data())
378 pldm_pdr_repository_chg_event_data;
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500379 size_t actualSize{};
380 auto firstEntry = changeEntries[0].data();
381 auto rc = encode_pldm_pdr_repository_chg_event_data(
382 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
383 &firstEntry, eventData, &actualSize, maxSize);
384 if (rc != PLDM_SUCCESS)
385 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500386 error(
387 "Failed to encode pldm pdr repository change event data, response code '{RC}'",
388 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500389 return;
390 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930391 auto instanceId = instanceIdDb.next(mctp_eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400392 std::vector<uint8_t> requestMsg(
393 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
394 actualSize);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530395 auto request = new (requestMsg.data()) pldm_msg;
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500396 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600397 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
398 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500399 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500400 if (rc != PLDM_SUCCESS)
401 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930402 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500403 error(
404 "Failed to encode platform event message request, response code '{RC}'",
405 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500406 return;
407 }
408
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400409 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
410 const pldm_msg* response,
411 size_t respMsgLen) {
Tom Joseph74f27c72021-05-16 07:58:53 -0700412 if (response == nullptr || !respMsgLen)
413 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600414 error(
415 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700416 return;
417 }
418
419 uint8_t completionCode{};
420 uint8_t status{};
421 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500422 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
423 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500424 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700425 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600426 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500427 "Failed to decode platform event message response, response code '{RC}' and completion code '{CC}'",
428 "RC", rc, "CC", completionCode);
Tom Joseph74f27c72021-05-16 07:58:53 -0700429 }
430 };
431
Sampa Misrac0c79482021-06-02 08:01:54 -0500432 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500433 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700434 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500435 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500436 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500437 error(
438 "Failed to send the PDR repository changed event request, response code '{RC}'",
439 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500440 }
441}
442
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530443void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500444{
445 for (const auto& pdr : stateSensorPDRs)
446 {
447 SensorEntry sensorEntry{};
448 const auto& [terminusHandle, sensorID, sensorInfo] =
449 responder::pdr_utils::parseStateSensorPDR(pdr);
450 sensorEntry.sensorID = sensorID;
451 try
452 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530453 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500454 }
455 // If there is no mapping for terminusHandle assign the reserved TID
456 // value of 0xFF to indicate that.
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500457 catch (const std::out_of_range&)
Sampa Misra868c8792020-05-26 03:12:13 -0500458 {
459 sensorEntry.terminusID = PLDM_TID_RESERVED;
460 }
461 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
462 }
463}
464
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400465void HostPDRHandler::processHostPDRs(
466 mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen)
Sampa Misrac0c79482021-06-02 08:01:54 -0500467{
468 static bool merged = false;
469 static PDRList stateSensorPDRs{};
George Liu682ee182020-12-25 15:24:33 +0800470 static PDRList fruRecordSetPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500471 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600472 uint8_t tlEid = 0;
473 bool tlValid = true;
474 uint32_t rh = 0;
475 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530476 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600477 uint8_t tid = 0;
478
Sampa Misrac0c79482021-06-02 08:01:54 -0500479 uint8_t completionCode{};
480 uint32_t nextDataTransferHandle{};
481 uint8_t transferFlag{};
482 uint16_t respCount{};
483 uint8_t transferCRC{};
484 if (response == nullptr || !respMsgLen)
485 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600486 error("Failed to receive response for the GetPDR command");
Riya Dixit478e71d2024-06-21 14:10:14 -0500487 pldm::utils::reportError(
488 "xyz.openbmc_project.PLDM.Error.GetPDR.PDRExchangeFailure");
Sampa Misrac0c79482021-06-02 08:01:54 -0500489 return;
490 }
491
492 auto rc = decode_get_pdr_resp(
493 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
494 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
495 nullptr, 0, &transferCRC);
496 std::vector<uint8_t> responsePDRMsg;
497 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
498 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500499 if (rc != PLDM_SUCCESS)
500 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500501 error(
502 "Failed to decode getPDR response for next record handle '{NEXT_RECORD_HANDLE}', response code '{RC}'",
503 "NEXT_RECORD_HANDLE", nextRecordHandle, "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500504 return;
505 }
506 else
507 {
508 std::vector<uint8_t> pdr(respCount, 0);
509 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
510 &nextRecordHandle, &nextDataTransferHandle,
511 &transferFlag, &respCount, pdr.data(),
512 respCount, &transferCRC);
513 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
514 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500515 error(
516 "Failed to decode getPDR response for next record handle '{NEXT_RECORD_HANDLE}', next data transfer handle '{DATA_TRANSFER_HANDLE}' and transfer flag '{FLAG}', response code '{RC}' and completion code '{CC}'",
517 "NEXT_RECORD_HANDLE", nextRecordHandle, "DATA_TRANSFER_HANDLE",
518 nextDataTransferHandle, "FLAG", transferFlag, "RC", rc, "CC",
519 completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -0500520 return;
521 }
522 else
523 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600524 // when nextRecordHandle is 0, we need the recordHandle of the last
525 // PDR and not 0-1.
526 if (!nextRecordHandle)
527 {
528 rh = nextRecordHandle;
529 }
530 else
531 {
532 rh = nextRecordHandle - 1;
533 }
534
Pavithra Barithaya49575832025-01-29 16:27:30 +0530535 auto pdrHdr = new (pdr.data()) pldm_pdr_hdr;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600536 if (!rh)
537 {
538 rh = pdrHdr->record_handle;
539 }
540
Sampa Misrac0c79482021-06-02 08:01:54 -0500541 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
542 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500543 this->mergeEntityAssociations(pdr, respCount, rh);
Sampa Misrac0c79482021-06-02 08:01:54 -0500544 merged = true;
545 }
546 else
547 {
548 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
549 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530550 pdrTerminusHandle =
551 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500552 auto tlpdr =
553 reinterpret_cast<const pldm_terminus_locator_pdr*>(
554 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600555
556 terminusHandle = tlpdr->terminus_handle;
557 tid = tlpdr->tid;
558 auto terminus_locator_type = tlpdr->terminus_locator_type;
559 if (terminus_locator_type ==
560 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
561 {
562 auto locatorValue = reinterpret_cast<
563 const pldm_terminus_locator_type_mctp_eid*>(
564 tlpdr->terminus_locator_value);
565 tlEid = static_cast<uint8_t>(locatorValue->eid);
566 }
567 if (tlpdr->validity == 0)
568 {
569 tlValid = false;
570 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500571 for (const auto& terminusMap : tlPDRInfo)
572 {
573 if ((terminusHandle == (terminusMap.first)) &&
574 (get<1>(terminusMap.second) == tlEid) &&
575 (get<2>(terminusMap.second) == tlpdr->validity))
576 {
577 // TL PDR already present with same validity don't
578 // add the PDR to the repo just return
579 return;
580 }
581 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530582 tlPDRInfo.insert_or_assign(
583 tlpdr->terminus_handle,
584 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500585 }
586 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
587 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530588 pdrTerminusHandle =
589 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800590 updateContainerId<pldm_state_sensor_pdr>(entityTree, pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500591 stateSensorPDRs.emplace_back(pdr);
592 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530593 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
594 {
595 pdrTerminusHandle =
596 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800597 updateContainerId<pldm_pdr_fru_record_set>(entityTree, pdr);
George Liu682ee182020-12-25 15:24:33 +0800598 fruRecordSetPDRs.emplace_back(pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530599 }
600 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
601 {
602 pdrTerminusHandle =
603 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800604 updateContainerId<pldm_state_effecter_pdr>(entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530605 }
606 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
607 {
608 pdrTerminusHandle =
609 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
610 pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800611 updateContainerId<pldm_numeric_effecter_value_pdr>(
612 entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530613 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600614 // if the TLPDR is invalid update the repo accordingly
615 if (!tlValid)
616 {
617 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
618 tlValid);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500619
620 if (!isHostUp())
621 {
622 // The terminus PDR becomes invalid when the terminus
623 // itself is down. We don't need to do PDR exchange in
624 // that case, so setting the next record handle to 0.
625 nextRecordHandle = 0;
626 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600627 }
628 else
629 {
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000630 rc = pldm_pdr_add(repo, pdr.data(), respCount, true,
631 pdrTerminusHandle, &rh);
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930632 if (rc)
633 {
634 // pldm_pdr_add() assert()ed on failure to add a PDR.
635 throw std::runtime_error("Failed to add PDR");
636 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600637 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500638 }
639 }
640 }
641 if (!nextRecordHandle)
642 {
Kamalkumar Patel516122e2024-05-07 04:39:32 -0500643 updateEntityAssociation(entityAssociations, entityTree, objPathMap,
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500644 entityMaps, oemPlatformHandler);
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -0500645 if (oemUtilsHandler)
646 {
647 oemUtilsHandler->setCoreCount(entityAssociations, entityMaps);
648 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500649 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530650 this->parseStateSensorPDRs(stateSensorPDRs);
George Liu682ee182020-12-25 15:24:33 +0800651 this->createDbusObjects(fruRecordSetPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600652 if (isHostUp())
653 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530654 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600655 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500656 stateSensorPDRs.clear();
George Liu682ee182020-12-25 15:24:33 +0800657 fruRecordSetPDRs.clear();
George Liudf9a6d32020-12-22 16:27:16 +0800658 entityAssociations.clear();
659
Sampa Misrac0c79482021-06-02 08:01:54 -0500660 if (merged)
661 {
662 merged = false;
663 deferredPDRRepoChgEvent =
664 std::make_unique<sdeventplus::source::Defer>(
665 event,
666 std::bind(
667 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
668 this, std::placeholders::_1));
669 }
670 }
671 else
672 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500673 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
674 {
675 isHostPdrModified = false;
676 }
677 else
678 {
679 deferredFetchPDREvent =
680 std::make_unique<sdeventplus::source::Defer>(
681 event,
682 std::bind(
683 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
684 this, nextRecordHandle, std::placeholders::_1));
685 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500686 }
687}
688
689void HostPDRHandler::_processPDRRepoChgEvent(
690 sdeventplus::source::EventBase& /*source */)
691{
692 deferredPDRRepoChgEvent.reset();
693 this->sendPDRRepositoryChgEvent(
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930694 std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION),
Sampa Misrac0c79482021-06-02 08:01:54 -0500695 FORMAT_IS_PDR_HANDLES);
696}
697
698void HostPDRHandler::_processFetchPDREvent(
699 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
700{
701 deferredFetchPDREvent.reset();
702 if (!this->pdrRecordHandles.empty())
703 {
704 nextRecordHandle = this->pdrRecordHandles.front();
705 this->pdrRecordHandles.pop_front();
706 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500707 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
708 {
709 nextRecordHandle = this->modifiedPDRRecordHandles.front();
710 this->modifiedPDRRecordHandles.pop_front();
711 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500712 this->getHostPDR(nextRecordHandle);
713}
714
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500715void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530716{
sampmisr6decfc12021-03-02 11:07:36 +0530717 responseReceived = false;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930718 auto instanceId = instanceIdDb.next(mctp_eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400719 std::vector<uint8_t> requestMsg(
720 sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530721 auto request = new (requestMsg.data()) pldm_msg;
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500722 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
723 PLDM_BASE, request);
724 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530725 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500726 error("Failed to encode GetPLDMVersion, response code {RC}", "RC",
Riya Dixit49cfb132023-03-02 04:26:53 -0600727 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930728 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530729 return;
730 }
731
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500732 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
733 const pldm_msg* response,
734 size_t respMsgLen) {
735 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530736 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600737 error(
738 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530739 return;
740 }
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500741 info("Getting the response code '{RC}'", "RC", lg2::hex,
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500742 response->payload[0]);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500743 this->responseReceived = true;
sampmisr6decfc12021-03-02 11:07:36 +0530744 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500745 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
746 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
747 std::move(getPLDMVersionHandler));
748 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530749 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500750 error(
751 "Failed to discover remote terminus state. Assuming remote terminus as off");
sampmisr6decfc12021-03-02 11:07:36 +0530752 }
753}
754
755bool HostPDRHandler::isHostUp()
756{
757 return responseReceived;
758}
759
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530760void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600761{
762 for (const auto& stateSensorPDR : stateSensorPDRs)
763 {
764 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
765 stateSensorPDR.data());
766
767 if (!pdr)
768 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500769 error("Failed to get state sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600770 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530771 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600772 return;
773 }
774
775 uint16_t sensorId = pdr->sensor_id;
776
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530777 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600778 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530779 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600780 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530781 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600782 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530783 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600784 }
785
786 bitfield8_t sensorRearm;
787 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530788 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600789
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930790 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600791 std::vector<uint8_t> requestMsg(
792 sizeof(pldm_msg_hdr) +
793 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530794 auto request = new (requestMsg.data()) pldm_msg;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600795 auto rc = encode_get_state_sensor_readings_req(
796 instanceId, sensorId, sensorRearm, 0, request);
797
798 if (rc != PLDM_SUCCESS)
799 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930800 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600801 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500802 "Failed to encode get state sensor readings request for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
803 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
804 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600805 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530806 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600807 return;
808 }
809
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400810 auto getStateSensorReadingRespHandler = [=, this](
811 mctp_eid_t /*eid*/,
812 const pldm_msg*
813 response,
814 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600815 if (response == nullptr || !respMsgLen)
816 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600817 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500818 "Failed to receive response for get state sensor reading command for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}'",
819 "SENSOR_ID", sensorId, "INSTANCE", instanceId);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600820 return;
821 }
822 std::array<get_sensor_state_field, 8> stateField{};
823 uint8_t completionCode = 0;
824 uint8_t comp_sensor_count = 0;
825
826 auto rc = decode_get_state_sensor_readings_resp(
827 response, respMsgLen, &completionCode,
828 &comp_sensor_count, stateField.data());
829
830 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
831 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600832 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500833 "Failed to decode get state sensor readings response for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code'{RC}' and completion code '{CC}'",
834 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
835 rc, "CC", completionCode);
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530836 pldm::utils::reportError(
837 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600838 }
839
840 uint8_t eventState;
841 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600842
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500843 for (uint8_t sensorOffset = 0;
844 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600845 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500846 eventState = stateField[sensorOffset].present_state;
847 previousEventState =
848 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600849
850 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
851 eventState,
852 previousEventState);
853
854 SensorEntry sensorEntry{tid, sensorId};
855
856 pldm::pdr::EntityInfo entityInfo{};
857 pldm::pdr::CompositeSensorStates
858 compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600859 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600860
861 try
862 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600863 std::tie(entityInfo, compositeSensorStates,
864 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600865 lookupSensorInfo(sensorEntry);
866 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500867 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600868 {
869 try
870 {
871 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600872 std::tie(entityInfo, compositeSensorStates,
873 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600874 lookupSensorInfo(sensorEntry);
875 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500876 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600877 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600878 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600879 }
880 }
881
Manojkiran Eda07a07e22024-04-24 19:15:10 +0530882 if ((compositeSensorStates.size() > 1) &&
883 (sensorOffset > (compositeSensorStates.size() - 1)))
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600884 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500885 error(
886 "Error Invalid data, Invalid sensor offset '{SENSOR_OFFSET}'",
887 "SENSOR_OFFSET", sensorOffset);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600888 return;
889 }
890
891 const auto& possibleStates =
892 compositeSensorStates[sensorOffset];
893 if (possibleStates.find(eventState) ==
894 possibleStates.end())
895 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500896 error(
897 "Error invalid_data, Invalid event state '{STATE}'",
898 "STATE", eventState);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600899 return;
900 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400901 const auto& [containerId, entityType, entityInstance] =
902 entityInfo;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600903 auto stateSetId = stateSetIds[sensorOffset];
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600904 pldm::responder::events::StateSensorEntry
Manojkiran Edafa084702024-05-27 10:20:30 +0530905 stateSensorEntry{containerId, entityType,
Sagar Srinivase3607a32024-02-16 03:50:53 -0600906 entityInstance, sensorOffset,
Manojkiran Edafa084702024-05-27 10:20:30 +0530907 stateSetId, false};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600908 handleStateSensorEvent(stateSensorEntry, eventState);
909 }
910 };
911
912 rc = handler->registerRequest(
913 mctp_eid, instanceId, PLDM_PLATFORM,
914 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
915 std::move(getStateSensorReadingRespHandler));
916
917 if (rc != PLDM_SUCCESS)
918 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600919 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500920 "Failed to send request to get state sensor reading on remote terminus for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
921 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
922 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600923 }
924 }
925 }
926 }
927}
George Liu682ee182020-12-25 15:24:33 +0800928
929void HostPDRHandler::getFRURecordTableMetadataByRemote(
930 const PDRList& fruRecordSetPDRs)
931{
932 auto instanceId = instanceIdDb.next(mctp_eid);
933 std::vector<uint8_t> requestMsg(
934 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
935
936 // GetFruRecordTableMetadata
Pavithra Barithaya49575832025-01-29 16:27:30 +0530937 auto request = new (requestMsg.data()) pldm_msg;
George Liu682ee182020-12-25 15:24:33 +0800938 auto rc = encode_get_fru_record_table_metadata_req(
939 instanceId, request, requestMsg.size() - sizeof(pldm_msg_hdr));
940 if (rc != PLDM_SUCCESS)
941 {
942 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500943 error(
944 "Failed to encode get fru record table metadata request, response code '{RC}'",
George Liu682ee182020-12-25 15:24:33 +0800945 "RC", lg2::hex, rc);
946 return;
947 }
948
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400949 auto getFruRecordTableMetadataResponseHandler = [this, fruRecordSetPDRs](
950 mctp_eid_t /*eid*/,
951 const pldm_msg*
952 response,
953 size_t respMsgLen) {
George Liu682ee182020-12-25 15:24:33 +0800954 if (response == nullptr || !respMsgLen)
955 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500956 error(
957 "Failed to receive response for the get fru record table metadata");
George Liu682ee182020-12-25 15:24:33 +0800958 return;
959 }
960
961 uint8_t cc = 0;
962 uint8_t fru_data_major_version, fru_data_minor_version;
963 uint32_t fru_table_maximum_size, fru_table_length;
964 uint16_t total_record_set_identifiers;
965 uint16_t total;
966 uint32_t checksum;
967
968 auto rc = decode_get_fru_record_table_metadata_resp(
969 response, respMsgLen, &cc, &fru_data_major_version,
970 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
971 &total_record_set_identifiers, &total, &checksum);
972
973 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
974 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500975 error(
976 "Failed to decode get fru record table metadata response, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +0800977 "RC", lg2::hex, rc, "CC", cc);
978 return;
979 }
980
981 // pass total to getFRURecordTableByRemote
982 this->getFRURecordTableByRemote(fruRecordSetPDRs, total);
983 };
984
985 rc = handler->registerRequest(
986 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE_METADATA,
987 std::move(requestMsg),
988 std::move(getFruRecordTableMetadataResponseHandler));
989 if (rc != PLDM_SUCCESS)
990 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500991 error(
992 "Failed to send the the set state effecter states request, response code '{RC}'",
993 "RC", rc);
George Liu682ee182020-12-25 15:24:33 +0800994 }
995
996 return;
997}
998
999void HostPDRHandler::getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
1000 uint16_t totalTableRecords)
1001{
1002 fruRecordData.clear();
1003
1004 if (!totalTableRecords)
1005 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001006 error("Failed to get fru record table");
George Liu682ee182020-12-25 15:24:33 +08001007 return;
1008 }
1009
1010 auto instanceId = instanceIdDb.next(mctp_eid);
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001011 std::vector<uint8_t> requestMsg(
1012 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
George Liu682ee182020-12-25 15:24:33 +08001013
1014 // send the getFruRecordTable command
Pavithra Barithaya49575832025-01-29 16:27:30 +05301015 auto request = new (requestMsg.data()) pldm_msg;
George Liu682ee182020-12-25 15:24:33 +08001016 auto rc = encode_get_fru_record_table_req(
1017 instanceId, 0, PLDM_GET_FIRSTPART, request,
1018 requestMsg.size() - sizeof(pldm_msg_hdr));
1019 if (rc != PLDM_SUCCESS)
1020 {
1021 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001022 error(
1023 "Failed to encode get fru record table request, response code '{RC}'",
1024 "RC", lg2::hex, rc);
George Liu682ee182020-12-25 15:24:33 +08001025 return;
1026 }
1027
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001028 auto getFruRecordTableResponseHandler = [totalTableRecords, this,
1029 fruRecordSetPDRs](
1030 mctp_eid_t /*eid*/,
1031 const pldm_msg* response,
1032 size_t respMsgLen) {
George Liu682ee182020-12-25 15:24:33 +08001033 if (response == nullptr || !respMsgLen)
1034 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001035 error("Failed to receive response for the get fru record table");
George Liu682ee182020-12-25 15:24:33 +08001036 return;
1037 }
1038
1039 uint8_t cc = 0;
1040 uint32_t next_data_transfer_handle = 0;
1041 uint8_t transfer_flag = 0;
1042 size_t fru_record_table_length = 0;
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001043 std::vector<uint8_t> fru_record_table_data(
1044 respMsgLen - sizeof(pldm_msg_hdr));
George Liu682ee182020-12-25 15:24:33 +08001045 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
1046 auto rc = decode_get_fru_record_table_resp(
Manojkiran Eda33a9cd02024-01-22 11:06:59 +05301047 responsePtr, respMsgLen, &cc, &next_data_transfer_handle,
1048 &transfer_flag, fru_record_table_data.data(),
1049 &fru_record_table_length);
George Liu682ee182020-12-25 15:24:33 +08001050
1051 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
1052 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001053 error(
1054 "Failed to decode get fru record table resp, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +08001055 "RC", lg2::hex, rc, "CC", cc);
1056 return;
1057 }
1058
1059 fruRecordData = responder::pdr_utils::parseFruRecordTable(
1060 fru_record_table_data.data(), fru_record_table_length);
1061
1062 if (totalTableRecords != fruRecordData.size())
1063 {
1064 fruRecordData.clear();
1065
Manojkiran Eda2576aec2024-06-17 12:05:17 +05301066 error("Failed to parse fru record data format.");
George Liu682ee182020-12-25 15:24:33 +08001067 return;
1068 }
1069
1070 this->setFRUDataOnDBus(fruRecordSetPDRs, fruRecordData);
1071 };
1072
1073 rc = handler->registerRequest(
1074 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE,
1075 std::move(requestMsg), std::move(getFruRecordTableResponseHandler));
1076 if (rc != PLDM_SUCCESS)
1077 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001078 error("Failed to send the the set state effecter states request");
George Liu682ee182020-12-25 15:24:33 +08001079 }
1080}
1081
1082std::optional<uint16_t> HostPDRHandler::getRSI(const PDRList& fruRecordSetPDRs,
1083 const pldm_entity& entity)
1084{
1085 for (const auto& pdr : fruRecordSetPDRs)
1086 {
1087 auto fruPdr = reinterpret_cast<const pldm_pdr_fru_record_set*>(
1088 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
1089
1090 if (fruPdr->entity_type == entity.entity_type &&
Sagar Srinivasebf8bb52023-10-06 01:15:55 -05001091 fruPdr->entity_instance == entity.entity_instance_num &&
1092 fruPdr->container_id == entity.entity_container_id)
George Liu682ee182020-12-25 15:24:33 +08001093 {
1094 return fruPdr->fru_rsi;
1095 }
1096 }
1097
1098 return std::nullopt;
1099}
1100
1101void HostPDRHandler::setFRUDataOnDBus(
Patrick Williamsd310f822023-10-07 19:01:19 -05001102 [[maybe_unused]] const PDRList& fruRecordSetPDRs,
1103 [[maybe_unused]] const std::vector<
1104 responder::pdr_utils::FruRecordDataFormat>& fruRecordData)
George Liu682ee182020-12-25 15:24:33 +08001105{
Patrick Williamsd310f822023-10-07 19:01:19 -05001106#ifdef OEM_IBM
George Liu682ee182020-12-25 15:24:33 +08001107 for (const auto& entity : objPathMap)
1108 {
1109 pldm_entity node = pldm_entity_extract(entity.second);
1110 auto fruRSI = getRSI(fruRecordSetPDRs, node);
1111
1112 for (const auto& data : fruRecordData)
1113 {
1114 if (!fruRSI || *fruRSI != data.fruRSI)
1115 {
1116 continue;
1117 }
1118
1119 if (data.fruRecType == PLDM_FRU_RECORD_TYPE_OEM)
1120 {
1121 for (const auto& tlv : data.fruTLV)
1122 {
1123 if (tlv.fruFieldType ==
1124 PLDM_OEM_FRU_FIELD_TYPE_LOCATION_CODE)
1125 {
1126 CustomDBus::getCustomDBus().setLocationCode(
1127 entity.first,
1128 std::string(reinterpret_cast<const char*>(
1129 tlv.fruFieldValue.data()),
1130 tlv.fruFieldLen));
1131 }
1132 }
1133 }
1134 }
1135 }
Patrick Williamsd310f822023-10-07 19:01:19 -05001136#endif
George Liu682ee182020-12-25 15:24:33 +08001137}
Archana Kakanif9355372025-02-04 03:13:24 -06001138
Archana Kakanic3664472025-02-04 05:36:37 -06001139void HostPDRHandler::setPresentPropertyStatus(const std::string& path)
1140{
1141 CustomDBus::getCustomDBus().updateItemPresentStatus(path, true);
1142}
1143
Archana Kakanif9355372025-02-04 03:13:24 -06001144void HostPDRHandler::setAvailabilityState(const std::string& path)
1145{
1146 CustomDBus::getCustomDBus().setAvailabilityState(path, true);
1147}
1148
George Liu682ee182020-12-25 15:24:33 +08001149void HostPDRHandler::createDbusObjects(const PDRList& fruRecordSetPDRs)
1150{
Archana Kakanif9355372025-02-04 03:13:24 -06001151 // Creating and Refreshing dbus hosted by remote PLDM entity Fru PDRs
1152
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001153 for (const auto& entity : objPathMap)
1154 {
Archana Kakanic3664472025-02-04 05:36:37 -06001155 // update the Present Property
1156 setPresentPropertyStatus(entity.first);
Archana Kakanif9355372025-02-04 03:13:24 -06001157 // Implement & update the Availability to true
1158 setAvailabilityState(entity.first);
1159
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001160 pldm_entity node = pldm_entity_extract(entity.second);
1161 switch (node.entity_type)
1162 {
1163 case PLDM_ENTITY_PROC | 0x8000:
1164 CustomDBus::getCustomDBus().implementCpuCoreInterface(
1165 entity.first);
1166 break;
Archana Kakanidb65c3b2025-02-03 05:27:28 -06001167 case PLDM_ENTITY_SYSTEM_CHASSIS:
1168 CustomDBus::getCustomDBus().implementChassisInterface(
1169 entity.first);
1170 break;
Archana Kakani24e9a9b2025-02-04 00:27:25 -06001171 case PLDM_ENTITY_POWER_SUPPLY:
1172 CustomDBus::getCustomDBus().implementPowerSupplyInterface(
1173 entity.first);
1174 break;
Archana Kakani765cf032025-02-04 06:24:14 -06001175 case PLDM_ENTITY_CHASSIS_FRONT_PANEL_BOARD:
1176 CustomDBus::getCustomDBus().implementPanelInterface(
1177 entity.first);
1178 break;
Archana Kakani2832f2c2025-02-04 22:29:30 -06001179 case PLDM_ENTITY_POWER_CONVERTER:
1180 CustomDBus::getCustomDBus().implementVRMInterface(entity.first);
1181 break;
Archana Kakanibf1fd272024-06-05 13:25:53 -05001182 case PLDM_ENTITY_SLOT:
1183 CustomDBus::getCustomDBus().implementPCIeSlotInterface(
1184 entity.first);
1185 break;
Archana Kakani17b1e8a2025-02-04 03:50:24 -06001186 case PLDM_ENTITY_CONNECTOR:
1187 CustomDBus::getCustomDBus().implementConnecterInterface(
1188 entity.first);
1189 break;
Archana Kakanid432b482025-02-04 22:52:28 -06001190 case PLDM_ENTITY_BOARD:
1191 CustomDBus::getCustomDBus().implementBoard(entity.first);
1192 break;
Archana Kakani733b39d2024-06-05 21:05:20 -05001193 case PLDM_ENTITY_CARD:
1194 CustomDBus::getCustomDBus().implementPCIeDeviceInterface(
1195 entity.first);
1196 break;
Kamalkumar Patel2ed986c2024-05-08 02:20:47 -05001197 case PLDM_ENTITY_SYS_BOARD:
1198 CustomDBus::getCustomDBus().implementMotherboardInterface(
1199 entity.first);
1200 break;
Archana Kakani413f51e2025-02-03 04:14:36 -06001201 case PLDM_ENTITY_FAN:
1202 CustomDBus::getCustomDBus().implementFanInterface(entity.first);
1203 break;
Archana Kakani42876b62025-02-04 04:22:29 -06001204 case PLDM_ENTITY_IO_MODULE:
1205 CustomDBus::getCustomDBus().implementFabricAdapter(
1206 entity.first);
1207 break;
Archana Kakani733b39d2024-06-05 21:05:20 -05001208 default:
1209 break;
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001210 }
1211 }
George Liu682ee182020-12-25 15:24:33 +08001212 getFRURecordTableMetadataByRemote(fruRecordSetPDRs);
1213}
1214
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001215} // namespace pldm