blob: 20f36df6044315f2149289b58f034f5bdb2c6bd3 [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 }
Eric Yang70262ed2025-07-02 06:35:03 +0000192 auto instanceIdResult =
193 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
194 if (!instanceIdResult)
195 {
196 return;
197 }
198 auto instanceId = instanceIdResult.value();
Sampa Misrac0c79482021-06-02 08:01:54 -0500199
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400200 auto rc =
201 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
202 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
Sampa Misrac0c79482021-06-02 08:01:54 -0500203 if (rc != PLDM_SUCCESS)
204 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930205 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500206 error("Failed to encode get pdr request, response code '{RC}'", "RC",
207 rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500208 return;
209 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500210
Sampa Misrac0c79482021-06-02 08:01:54 -0500211 rc = handler->registerRequest(
212 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR,
213 std::move(requestMsg),
Eric Yang70eca962025-05-11 01:48:15 +0800214 [this](mctp_eid_t eid, const pldm_msg* response, size_t respMsgLen) {
215 this->processHostPDRs(eid, response, respMsgLen);
216 });
Sampa Misrac0c79482021-06-02 08:01:54 -0500217 if (rc)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500218 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500219 error(
220 "Failed to send the getPDR request to remote terminus, response code '{RC}'",
221 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500222 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500223}
224
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600225int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
226 pdr::EventState state)
227{
228 auto rc = stateSensorHandler.eventAction(entry, state);
229 if (rc != PLDM_SUCCESS)
230 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500231 error("Failed to fetch and update D-bus property, response code '{RC}'",
232 "RC", rc);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600233 return rc;
234 }
235 return PLDM_SUCCESS;
236}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500237
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500238void HostPDRHandler::mergeEntityAssociations(
239 const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size,
240 [[maybe_unused]] const uint32_t& record_handle)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500241{
242 size_t numEntities{};
243 pldm_entity* entities = nullptr;
244 bool merged = false;
Pavithra Barithaya49575832025-01-29 16:27:30 +0530245 auto entityPdr = new (const_cast<uint8_t*>(pdr.data()) +
246 sizeof(pldm_pdr_hdr)) pldm_pdr_entity_association;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500247
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500248 if (oemPlatformHandler &&
249 oemPlatformHandler->checkRecordHandleInRange(record_handle))
250 {
251 // Adding the remote range PDRs to the repo before merging it
252 uint32_t handle = record_handle;
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000253 pldm_pdr_add(repo, pdr.data(), size, true, 0xFFFF, &handle);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500254 }
255
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500256 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
257 &entities);
George Liuacf2c8c2021-05-10 14:08:52 +0800258 if (numEntities > 0)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500259 {
George Liuacf2c8c2021-05-10 14:08:52 +0800260 pldm_entity_node* pNode = nullptr;
261 if (!mergedHostParents)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500262 {
George Liuacf2c8c2021-05-10 14:08:52 +0800263 pNode = pldm_entity_association_tree_find_with_locality(
264 entityTree, &entities[0], false);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500265 }
George Liuacf2c8c2021-05-10 14:08:52 +0800266 else
267 {
268 pNode = pldm_entity_association_tree_find_with_locality(
269 entityTree, &entities[0], true);
270 }
271 if (!pNode)
272 {
273 return;
274 }
275
George Liudf9a6d32020-12-22 16:27:16 +0800276 Entities entityAssoc;
277 entityAssoc.push_back(pNode);
George Liuacf2c8c2021-05-10 14:08:52 +0800278 for (size_t i = 1; i < numEntities; ++i)
279 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500280 bool isUpdateContainerId = true;
281 if (oemPlatformHandler)
282 {
283 isUpdateContainerId =
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600284 checkIfLogicalBitSet(entities[i].entity_container_id);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500285 }
George Liudf9a6d32020-12-22 16:27:16 +0800286 auto node = pldm_entity_association_tree_add_entity(
George Liuacf2c8c2021-05-10 14:08:52 +0800287 entityTree, &entities[i], entities[i].entity_instance_num,
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500288 pNode, entityPdr->association_type, true, isUpdateContainerId,
289 0xFFFF);
290 if (!node)
291 {
292 continue;
293 }
George Liuacf2c8c2021-05-10 14:08:52 +0800294 merged = true;
George Liudf9a6d32020-12-22 16:27:16 +0800295 entityAssoc.push_back(node);
George Liuacf2c8c2021-05-10 14:08:52 +0800296 }
297
298 mergedHostParents = true;
George Liudf9a6d32020-12-22 16:27:16 +0800299 if (merged)
300 {
301 entityAssociations.push_back(entityAssoc);
302 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500303 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500304
305 if (merged)
306 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500307 // Update our PDR repo with the merged entity association PDRs
Sampa Misra719ed392021-06-04 05:15:13 -0500308 pldm_entity_node* node = nullptr;
309 pldm_find_entity_ref_in_tree(entityTree, entities[0], &node);
310 if (node == nullptr)
311 {
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530312 error("Failed to find reference of the entity in the tree");
Sampa Misra719ed392021-06-04 05:15:13 -0500313 }
314 else
315 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500316 int rc = 0;
317 if (oemPlatformHandler)
318 {
319 auto record = oemPlatformHandler->fetchLastBMCRecord(repo);
320
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400321 uint32_t record_handle =
322 pldm_pdr_get_record_handle(repo, record);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500323
324 rc =
325 pldm_entity_association_pdr_add_from_node_with_record_handle(
326 node, repo, &entities, numEntities, true,
327 TERMINUS_HANDLE, (record_handle + 1));
328 }
329 else
330 {
Andrew Jeffery7761bd22024-08-01 13:15:36 +0000331 rc = pldm_entity_association_pdr_add_from_node(
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500332 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
333 }
334
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930335 if (rc)
336 {
337 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500338 "Failed to add entity association PDR from node, response code '{RC}'",
339 "RC", rc);
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930340 }
Sampa Misra719ed392021-06-04 05:15:13 -0500341 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500342 }
Sampa Misra719ed392021-06-04 05:15:13 -0500343 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500344}
345
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500346void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
347 uint8_t eventDataFormat)
348{
349 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
350
351 // Extract from the PDR repo record handles of PDRs we want the host
352 // to pull up.
353 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
354 std::vector<uint8_t> numsOfChangeEntries(1);
355 std::vector<std::vector<ChangeEntry>> changeEntries(
356 numsOfChangeEntries.size());
357 for (auto pdrType : pdrTypes)
358 {
359 const pldm_pdr_record* record{};
360 do
361 {
362 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
363 nullptr, nullptr);
364 if (record && pldm_pdr_record_is_remote(record))
365 {
366 changeEntries[0].push_back(
367 pldm_pdr_get_record_handle(repo, record));
368 }
369 } while (record);
370 }
371 if (changeEntries.empty())
372 {
373 return;
374 }
375 numsOfChangeEntries[0] = changeEntries[0].size();
376
377 // Encode PLDM platform event msg to indicate a PDR repo change.
378 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
379 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
380 changeEntries[0].size() * sizeof(uint32_t);
381 std::vector<uint8_t> eventDataVec{};
382 eventDataVec.resize(maxSize);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530383 auto eventData = new (eventDataVec.data())
384 pldm_pdr_repository_chg_event_data;
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500385 size_t actualSize{};
386 auto firstEntry = changeEntries[0].data();
387 auto rc = encode_pldm_pdr_repository_chg_event_data(
388 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
389 &firstEntry, eventData, &actualSize, maxSize);
390 if (rc != PLDM_SUCCESS)
391 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500392 error(
393 "Failed to encode pldm pdr repository change event data, response code '{RC}'",
394 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500395 return;
396 }
Eric Yang70262ed2025-07-02 06:35:03 +0000397 auto instanceIdResult =
398 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
399 if (!instanceIdResult)
400 {
401 return;
402 }
403 auto instanceId = instanceIdResult.value();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400404 std::vector<uint8_t> requestMsg(
405 sizeof(pldm_msg_hdr) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
406 actualSize);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530407 auto request = new (requestMsg.data()) pldm_msg;
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500408 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600409 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
410 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500411 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500412 if (rc != PLDM_SUCCESS)
413 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930414 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500415 error(
416 "Failed to encode platform event message request, response code '{RC}'",
417 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500418 return;
419 }
420
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400421 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
422 const pldm_msg* response,
423 size_t respMsgLen) {
Tom Joseph74f27c72021-05-16 07:58:53 -0700424 if (response == nullptr || !respMsgLen)
425 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600426 error(
427 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700428 return;
429 }
430
431 uint8_t completionCode{};
432 uint8_t status{};
433 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500434 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
435 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500436 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700437 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600438 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500439 "Failed to decode platform event message response, response code '{RC}' and completion code '{CC}'",
440 "RC", rc, "CC", completionCode);
Tom Joseph74f27c72021-05-16 07:58:53 -0700441 }
442 };
443
Sampa Misrac0c79482021-06-02 08:01:54 -0500444 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500445 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700446 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500447 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500448 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500449 error(
450 "Failed to send the PDR repository changed event request, response code '{RC}'",
451 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500452 }
453}
454
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530455void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500456{
457 for (const auto& pdr : stateSensorPDRs)
458 {
459 SensorEntry sensorEntry{};
460 const auto& [terminusHandle, sensorID, sensorInfo] =
461 responder::pdr_utils::parseStateSensorPDR(pdr);
462 sensorEntry.sensorID = sensorID;
463 try
464 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530465 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500466 }
467 // If there is no mapping for terminusHandle assign the reserved TID
468 // value of 0xFF to indicate that.
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500469 catch (const std::out_of_range&)
Sampa Misra868c8792020-05-26 03:12:13 -0500470 {
471 sensorEntry.terminusID = PLDM_TID_RESERVED;
472 }
473 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
474 }
475}
476
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400477void HostPDRHandler::processHostPDRs(
478 mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen)
Sampa Misrac0c79482021-06-02 08:01:54 -0500479{
480 static bool merged = false;
481 static PDRList stateSensorPDRs{};
George Liu682ee182020-12-25 15:24:33 +0800482 static PDRList fruRecordSetPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500483 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600484 uint8_t tlEid = 0;
485 bool tlValid = true;
486 uint32_t rh = 0;
487 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530488 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600489 uint8_t tid = 0;
490
Sampa Misrac0c79482021-06-02 08:01:54 -0500491 uint8_t completionCode{};
492 uint32_t nextDataTransferHandle{};
493 uint8_t transferFlag{};
494 uint16_t respCount{};
495 uint8_t transferCRC{};
496 if (response == nullptr || !respMsgLen)
497 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600498 error("Failed to receive response for the GetPDR command");
Riya Dixit478e71d2024-06-21 14:10:14 -0500499 pldm::utils::reportError(
500 "xyz.openbmc_project.PLDM.Error.GetPDR.PDRExchangeFailure");
Sampa Misrac0c79482021-06-02 08:01:54 -0500501 return;
502 }
503
504 auto rc = decode_get_pdr_resp(
505 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
506 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
507 nullptr, 0, &transferCRC);
508 std::vector<uint8_t> responsePDRMsg;
509 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
510 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500511 if (rc != PLDM_SUCCESS)
512 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500513 error(
514 "Failed to decode getPDR response for next record handle '{NEXT_RECORD_HANDLE}', response code '{RC}'",
515 "NEXT_RECORD_HANDLE", nextRecordHandle, "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500516 return;
517 }
518 else
519 {
520 std::vector<uint8_t> pdr(respCount, 0);
521 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
522 &nextRecordHandle, &nextDataTransferHandle,
523 &transferFlag, &respCount, pdr.data(),
524 respCount, &transferCRC);
525 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
526 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500527 error(
528 "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}'",
529 "NEXT_RECORD_HANDLE", nextRecordHandle, "DATA_TRANSFER_HANDLE",
530 nextDataTransferHandle, "FLAG", transferFlag, "RC", rc, "CC",
531 completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -0500532 return;
533 }
534 else
535 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600536 // when nextRecordHandle is 0, we need the recordHandle of the last
537 // PDR and not 0-1.
538 if (!nextRecordHandle)
539 {
540 rh = nextRecordHandle;
541 }
542 else
543 {
544 rh = nextRecordHandle - 1;
545 }
546
Pavithra Barithaya49575832025-01-29 16:27:30 +0530547 auto pdrHdr = new (pdr.data()) pldm_pdr_hdr;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600548 if (!rh)
549 {
550 rh = pdrHdr->record_handle;
551 }
552
Sampa Misrac0c79482021-06-02 08:01:54 -0500553 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
554 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500555 this->mergeEntityAssociations(pdr, respCount, rh);
Sampa Misrac0c79482021-06-02 08:01:54 -0500556 merged = true;
557 }
558 else
559 {
560 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
561 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530562 pdrTerminusHandle =
563 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500564 auto tlpdr =
565 reinterpret_cast<const pldm_terminus_locator_pdr*>(
566 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600567
568 terminusHandle = tlpdr->terminus_handle;
569 tid = tlpdr->tid;
570 auto terminus_locator_type = tlpdr->terminus_locator_type;
571 if (terminus_locator_type ==
572 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
573 {
574 auto locatorValue = reinterpret_cast<
575 const pldm_terminus_locator_type_mctp_eid*>(
576 tlpdr->terminus_locator_value);
577 tlEid = static_cast<uint8_t>(locatorValue->eid);
578 }
579 if (tlpdr->validity == 0)
580 {
581 tlValid = false;
582 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500583 for (const auto& terminusMap : tlPDRInfo)
584 {
585 if ((terminusHandle == (terminusMap.first)) &&
586 (get<1>(terminusMap.second) == tlEid) &&
587 (get<2>(terminusMap.second) == tlpdr->validity))
588 {
589 // TL PDR already present with same validity don't
590 // add the PDR to the repo just return
591 return;
592 }
593 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530594 tlPDRInfo.insert_or_assign(
595 tlpdr->terminus_handle,
596 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500597 }
598 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
599 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530600 pdrTerminusHandle =
601 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800602 updateContainerId<pldm_state_sensor_pdr>(entityTree, pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500603 stateSensorPDRs.emplace_back(pdr);
604 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530605 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
606 {
607 pdrTerminusHandle =
608 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800609 updateContainerId<pldm_pdr_fru_record_set>(entityTree, pdr);
George Liu682ee182020-12-25 15:24:33 +0800610 fruRecordSetPDRs.emplace_back(pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530611 }
612 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
613 {
614 pdrTerminusHandle =
615 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800616 updateContainerId<pldm_state_effecter_pdr>(entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530617 }
618 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
619 {
620 pdrTerminusHandle =
621 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
622 pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800623 updateContainerId<pldm_numeric_effecter_value_pdr>(
624 entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530625 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600626 // if the TLPDR is invalid update the repo accordingly
627 if (!tlValid)
628 {
629 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
630 tlValid);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500631
632 if (!isHostUp())
633 {
634 // The terminus PDR becomes invalid when the terminus
635 // itself is down. We don't need to do PDR exchange in
636 // that case, so setting the next record handle to 0.
637 nextRecordHandle = 0;
638 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600639 }
640 else
641 {
Andrew Jeffery5a945bd2024-08-01 13:15:36 +0000642 rc = pldm_pdr_add(repo, pdr.data(), respCount, true,
643 pdrTerminusHandle, &rh);
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930644 if (rc)
645 {
646 // pldm_pdr_add() assert()ed on failure to add a PDR.
647 throw std::runtime_error("Failed to add PDR");
648 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600649 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500650 }
651 }
652 }
653 if (!nextRecordHandle)
654 {
Kamalkumar Patel516122e2024-05-07 04:39:32 -0500655 updateEntityAssociation(entityAssociations, entityTree, objPathMap,
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500656 entityMaps, oemPlatformHandler);
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -0500657 if (oemUtilsHandler)
658 {
659 oemUtilsHandler->setCoreCount(entityAssociations, entityMaps);
660 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500661 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530662 this->parseStateSensorPDRs(stateSensorPDRs);
George Liu682ee182020-12-25 15:24:33 +0800663 this->createDbusObjects(fruRecordSetPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600664 if (isHostUp())
665 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530666 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600667 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500668 stateSensorPDRs.clear();
George Liu682ee182020-12-25 15:24:33 +0800669 fruRecordSetPDRs.clear();
George Liudf9a6d32020-12-22 16:27:16 +0800670 entityAssociations.clear();
671
Sampa Misrac0c79482021-06-02 08:01:54 -0500672 if (merged)
673 {
674 merged = false;
675 deferredPDRRepoChgEvent =
676 std::make_unique<sdeventplus::source::Defer>(
677 event,
678 std::bind(
679 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
680 this, std::placeholders::_1));
681 }
682 }
683 else
684 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500685 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
686 {
687 isHostPdrModified = false;
688 }
689 else
690 {
691 deferredFetchPDREvent =
692 std::make_unique<sdeventplus::source::Defer>(
693 event,
694 std::bind(
695 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
696 this, nextRecordHandle, std::placeholders::_1));
697 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500698 }
699}
700
701void HostPDRHandler::_processPDRRepoChgEvent(
702 sdeventplus::source::EventBase& /*source */)
703{
704 deferredPDRRepoChgEvent.reset();
705 this->sendPDRRepositoryChgEvent(
Andrew Jefferyc14fb4b2024-07-25 22:13:09 +0930706 std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION),
Sampa Misrac0c79482021-06-02 08:01:54 -0500707 FORMAT_IS_PDR_HANDLES);
708}
709
710void HostPDRHandler::_processFetchPDREvent(
711 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
712{
713 deferredFetchPDREvent.reset();
714 if (!this->pdrRecordHandles.empty())
715 {
716 nextRecordHandle = this->pdrRecordHandles.front();
717 this->pdrRecordHandles.pop_front();
718 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500719 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
720 {
721 nextRecordHandle = this->modifiedPDRRecordHandles.front();
722 this->modifiedPDRRecordHandles.pop_front();
723 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500724 this->getHostPDR(nextRecordHandle);
725}
726
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500727void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530728{
sampmisr6decfc12021-03-02 11:07:36 +0530729 responseReceived = false;
Eric Yang70262ed2025-07-02 06:35:03 +0000730 auto instanceIdResult =
731 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
732 if (!instanceIdResult)
733 {
734 return;
735 }
736 auto instanceId = instanceIdResult.value();
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400737 std::vector<uint8_t> requestMsg(
738 sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530739 auto request = new (requestMsg.data()) pldm_msg;
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500740 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
741 PLDM_BASE, request);
742 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530743 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500744 error("Failed to encode GetPLDMVersion, response code {RC}", "RC",
Riya Dixit49cfb132023-03-02 04:26:53 -0600745 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930746 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530747 return;
748 }
749
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500750 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
751 const pldm_msg* response,
752 size_t respMsgLen) {
753 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530754 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600755 error(
756 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530757 return;
758 }
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500759 info("Getting the response code '{RC}'", "RC", lg2::hex,
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500760 response->payload[0]);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500761 this->responseReceived = true;
sampmisr6decfc12021-03-02 11:07:36 +0530762 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500763 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
764 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
765 std::move(getPLDMVersionHandler));
766 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530767 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500768 error(
769 "Failed to discover remote terminus state. Assuming remote terminus as off");
sampmisr6decfc12021-03-02 11:07:36 +0530770 }
771}
772
773bool HostPDRHandler::isHostUp()
774{
775 return responseReceived;
776}
777
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530778void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600779{
780 for (const auto& stateSensorPDR : stateSensorPDRs)
781 {
782 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
783 stateSensorPDR.data());
784
785 if (!pdr)
786 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500787 error("Failed to get state sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600788 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530789 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600790 return;
791 }
792
793 uint16_t sensorId = pdr->sensor_id;
794
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530795 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600796 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530797 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600798 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530799 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600800 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530801 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600802 }
803
804 bitfield8_t sensorRearm;
805 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530806 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600807
Eric Yang70262ed2025-07-02 06:35:03 +0000808 auto instanceIdResult =
809 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
810 if (!instanceIdResult)
811 {
812 return;
813 }
814 auto instanceId = instanceIdResult.value();
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600815 std::vector<uint8_t> requestMsg(
816 sizeof(pldm_msg_hdr) +
817 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
Pavithra Barithaya49575832025-01-29 16:27:30 +0530818 auto request = new (requestMsg.data()) pldm_msg;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600819 auto rc = encode_get_state_sensor_readings_req(
820 instanceId, sensorId, sensorRearm, 0, request);
821
822 if (rc != PLDM_SUCCESS)
823 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930824 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600825 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500826 "Failed to encode get state sensor readings request for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
827 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
828 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600829 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530830 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600831 return;
832 }
833
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400834 auto getStateSensorReadingRespHandler = [=, this](
835 mctp_eid_t /*eid*/,
836 const pldm_msg*
837 response,
838 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600839 if (response == nullptr || !respMsgLen)
840 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600841 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500842 "Failed to receive response for get state sensor reading command for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}'",
843 "SENSOR_ID", sensorId, "INSTANCE", instanceId);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600844 return;
845 }
846 std::array<get_sensor_state_field, 8> stateField{};
847 uint8_t completionCode = 0;
848 uint8_t comp_sensor_count = 0;
849
850 auto rc = decode_get_state_sensor_readings_resp(
851 response, respMsgLen, &completionCode,
852 &comp_sensor_count, stateField.data());
853
854 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
855 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600856 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500857 "Failed to decode get state sensor readings response for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code'{RC}' and completion code '{CC}'",
858 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
859 rc, "CC", completionCode);
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530860 pldm::utils::reportError(
861 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600862 }
863
864 uint8_t eventState;
865 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600866
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500867 for (uint8_t sensorOffset = 0;
868 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600869 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500870 eventState = stateField[sensorOffset].present_state;
871 previousEventState =
872 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600873
874 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
875 eventState,
876 previousEventState);
877
878 SensorEntry sensorEntry{tid, sensorId};
879
880 pldm::pdr::EntityInfo entityInfo{};
881 pldm::pdr::CompositeSensorStates
882 compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600883 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600884
885 try
886 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600887 std::tie(entityInfo, compositeSensorStates,
888 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600889 lookupSensorInfo(sensorEntry);
890 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500891 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600892 {
893 try
894 {
895 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600896 std::tie(entityInfo, compositeSensorStates,
897 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600898 lookupSensorInfo(sensorEntry);
899 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500900 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600901 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600902 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600903 }
904 }
905
Manojkiran Eda07a07e22024-04-24 19:15:10 +0530906 if ((compositeSensorStates.size() > 1) &&
907 (sensorOffset > (compositeSensorStates.size() - 1)))
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600908 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500909 error(
910 "Error Invalid data, Invalid sensor offset '{SENSOR_OFFSET}'",
911 "SENSOR_OFFSET", sensorOffset);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600912 return;
913 }
914
915 const auto& possibleStates =
916 compositeSensorStates[sensorOffset];
917 if (possibleStates.find(eventState) ==
918 possibleStates.end())
919 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500920 error(
921 "Error invalid_data, Invalid event state '{STATE}'",
922 "STATE", eventState);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600923 return;
924 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400925 const auto& [containerId, entityType, entityInstance] =
926 entityInfo;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600927 auto stateSetId = stateSetIds[sensorOffset];
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600928 pldm::responder::events::StateSensorEntry
Manojkiran Edafa084702024-05-27 10:20:30 +0530929 stateSensorEntry{containerId, entityType,
Sagar Srinivase3607a32024-02-16 03:50:53 -0600930 entityInstance, sensorOffset,
Manojkiran Edafa084702024-05-27 10:20:30 +0530931 stateSetId, false};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600932 handleStateSensorEvent(stateSensorEntry, eventState);
933 }
934 };
935
936 rc = handler->registerRequest(
937 mctp_eid, instanceId, PLDM_PLATFORM,
938 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
939 std::move(getStateSensorReadingRespHandler));
940
941 if (rc != PLDM_SUCCESS)
942 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600943 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500944 "Failed to send request to get state sensor reading on remote terminus for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
945 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
946 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600947 }
948 }
949 }
950 }
951}
George Liu682ee182020-12-25 15:24:33 +0800952
953void HostPDRHandler::getFRURecordTableMetadataByRemote(
954 const PDRList& fruRecordSetPDRs)
955{
Eric Yang70262ed2025-07-02 06:35:03 +0000956 auto instanceIdResult =
957 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
958 if (!instanceIdResult)
959 {
960 return;
961 }
962 auto instanceId = instanceIdResult.value();
George Liu682ee182020-12-25 15:24:33 +0800963 std::vector<uint8_t> requestMsg(
964 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
965
966 // GetFruRecordTableMetadata
Pavithra Barithaya49575832025-01-29 16:27:30 +0530967 auto request = new (requestMsg.data()) pldm_msg;
George Liu682ee182020-12-25 15:24:33 +0800968 auto rc = encode_get_fru_record_table_metadata_req(
969 instanceId, request, requestMsg.size() - sizeof(pldm_msg_hdr));
970 if (rc != PLDM_SUCCESS)
971 {
972 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500973 error(
974 "Failed to encode get fru record table metadata request, response code '{RC}'",
George Liu682ee182020-12-25 15:24:33 +0800975 "RC", lg2::hex, rc);
976 return;
977 }
978
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400979 auto getFruRecordTableMetadataResponseHandler = [this, fruRecordSetPDRs](
980 mctp_eid_t /*eid*/,
981 const pldm_msg*
982 response,
983 size_t respMsgLen) {
George Liu682ee182020-12-25 15:24:33 +0800984 if (response == nullptr || !respMsgLen)
985 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500986 error(
987 "Failed to receive response for the get fru record table metadata");
George Liu682ee182020-12-25 15:24:33 +0800988 return;
989 }
990
991 uint8_t cc = 0;
992 uint8_t fru_data_major_version, fru_data_minor_version;
993 uint32_t fru_table_maximum_size, fru_table_length;
994 uint16_t total_record_set_identifiers;
995 uint16_t total;
996 uint32_t checksum;
997
998 auto rc = decode_get_fru_record_table_metadata_resp(
999 response, respMsgLen, &cc, &fru_data_major_version,
1000 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
1001 &total_record_set_identifiers, &total, &checksum);
1002
1003 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
1004 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001005 error(
1006 "Failed to decode get fru record table metadata response, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +08001007 "RC", lg2::hex, rc, "CC", cc);
1008 return;
1009 }
1010
1011 // pass total to getFRURecordTableByRemote
1012 this->getFRURecordTableByRemote(fruRecordSetPDRs, total);
1013 };
1014
1015 rc = handler->registerRequest(
1016 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE_METADATA,
1017 std::move(requestMsg),
1018 std::move(getFruRecordTableMetadataResponseHandler));
1019 if (rc != PLDM_SUCCESS)
1020 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001021 error(
1022 "Failed to send the the set state effecter states request, response code '{RC}'",
1023 "RC", rc);
George Liu682ee182020-12-25 15:24:33 +08001024 }
1025
1026 return;
1027}
1028
1029void HostPDRHandler::getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
1030 uint16_t totalTableRecords)
1031{
1032 fruRecordData.clear();
1033
1034 if (!totalTableRecords)
1035 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001036 error("Failed to get fru record table");
George Liu682ee182020-12-25 15:24:33 +08001037 return;
1038 }
1039
Eric Yang70262ed2025-07-02 06:35:03 +00001040 auto instanceIdResult =
1041 pldm::utils::getInstanceId(instanceIdDb.next(mctp_eid));
1042 if (!instanceIdResult)
1043 {
1044 return;
1045 }
1046 auto instanceId = instanceIdResult.value();
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001047 std::vector<uint8_t> requestMsg(
1048 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
George Liu682ee182020-12-25 15:24:33 +08001049
1050 // send the getFruRecordTable command
Pavithra Barithaya49575832025-01-29 16:27:30 +05301051 auto request = new (requestMsg.data()) pldm_msg;
George Liu682ee182020-12-25 15:24:33 +08001052 auto rc = encode_get_fru_record_table_req(
1053 instanceId, 0, PLDM_GET_FIRSTPART, request,
1054 requestMsg.size() - sizeof(pldm_msg_hdr));
1055 if (rc != PLDM_SUCCESS)
1056 {
1057 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001058 error(
1059 "Failed to encode get fru record table request, response code '{RC}'",
1060 "RC", lg2::hex, rc);
George Liu682ee182020-12-25 15:24:33 +08001061 return;
1062 }
1063
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001064 auto getFruRecordTableResponseHandler = [totalTableRecords, this,
1065 fruRecordSetPDRs](
1066 mctp_eid_t /*eid*/,
1067 const pldm_msg* response,
1068 size_t respMsgLen) {
George Liu682ee182020-12-25 15:24:33 +08001069 if (response == nullptr || !respMsgLen)
1070 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001071 error("Failed to receive response for the get fru record table");
George Liu682ee182020-12-25 15:24:33 +08001072 return;
1073 }
1074
1075 uint8_t cc = 0;
1076 uint32_t next_data_transfer_handle = 0;
1077 uint8_t transfer_flag = 0;
1078 size_t fru_record_table_length = 0;
Patrick Williams16c2a0a2024-08-16 15:20:59 -04001079 std::vector<uint8_t> fru_record_table_data(
1080 respMsgLen - sizeof(pldm_msg_hdr));
George Liu682ee182020-12-25 15:24:33 +08001081 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
1082 auto rc = decode_get_fru_record_table_resp(
Manojkiran Eda33a9cd02024-01-22 11:06:59 +05301083 responsePtr, respMsgLen, &cc, &next_data_transfer_handle,
1084 &transfer_flag, fru_record_table_data.data(),
1085 &fru_record_table_length);
George Liu682ee182020-12-25 15:24:33 +08001086
1087 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
1088 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001089 error(
1090 "Failed to decode get fru record table resp, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +08001091 "RC", lg2::hex, rc, "CC", cc);
1092 return;
1093 }
1094
1095 fruRecordData = responder::pdr_utils::parseFruRecordTable(
1096 fru_record_table_data.data(), fru_record_table_length);
1097
1098 if (totalTableRecords != fruRecordData.size())
1099 {
1100 fruRecordData.clear();
1101
Manojkiran Eda2576aec2024-06-17 12:05:17 +05301102 error("Failed to parse fru record data format.");
George Liu682ee182020-12-25 15:24:33 +08001103 return;
1104 }
1105
1106 this->setFRUDataOnDBus(fruRecordSetPDRs, fruRecordData);
1107 };
1108
1109 rc = handler->registerRequest(
1110 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE,
1111 std::move(requestMsg), std::move(getFruRecordTableResponseHandler));
1112 if (rc != PLDM_SUCCESS)
1113 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001114 error("Failed to send the the set state effecter states request");
George Liu682ee182020-12-25 15:24:33 +08001115 }
1116}
1117
1118std::optional<uint16_t> HostPDRHandler::getRSI(const PDRList& fruRecordSetPDRs,
1119 const pldm_entity& entity)
1120{
1121 for (const auto& pdr : fruRecordSetPDRs)
1122 {
1123 auto fruPdr = reinterpret_cast<const pldm_pdr_fru_record_set*>(
1124 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
1125
1126 if (fruPdr->entity_type == entity.entity_type &&
Sagar Srinivasebf8bb52023-10-06 01:15:55 -05001127 fruPdr->entity_instance == entity.entity_instance_num &&
1128 fruPdr->container_id == entity.entity_container_id)
George Liu682ee182020-12-25 15:24:33 +08001129 {
1130 return fruPdr->fru_rsi;
1131 }
1132 }
1133
1134 return std::nullopt;
1135}
1136
1137void HostPDRHandler::setFRUDataOnDBus(
Patrick Williamsd310f822023-10-07 19:01:19 -05001138 [[maybe_unused]] const PDRList& fruRecordSetPDRs,
1139 [[maybe_unused]] const std::vector<
1140 responder::pdr_utils::FruRecordDataFormat>& fruRecordData)
George Liu682ee182020-12-25 15:24:33 +08001141{
Patrick Williamsd310f822023-10-07 19:01:19 -05001142#ifdef OEM_IBM
George Liu682ee182020-12-25 15:24:33 +08001143 for (const auto& entity : objPathMap)
1144 {
1145 pldm_entity node = pldm_entity_extract(entity.second);
1146 auto fruRSI = getRSI(fruRecordSetPDRs, node);
1147
1148 for (const auto& data : fruRecordData)
1149 {
1150 if (!fruRSI || *fruRSI != data.fruRSI)
1151 {
1152 continue;
1153 }
1154
1155 if (data.fruRecType == PLDM_FRU_RECORD_TYPE_OEM)
1156 {
1157 for (const auto& tlv : data.fruTLV)
1158 {
1159 if (tlv.fruFieldType ==
1160 PLDM_OEM_FRU_FIELD_TYPE_LOCATION_CODE)
1161 {
1162 CustomDBus::getCustomDBus().setLocationCode(
1163 entity.first,
1164 std::string(reinterpret_cast<const char*>(
1165 tlv.fruFieldValue.data()),
1166 tlv.fruFieldLen));
1167 }
1168 }
1169 }
1170 }
1171 }
Patrick Williamsd310f822023-10-07 19:01:19 -05001172#endif
George Liu682ee182020-12-25 15:24:33 +08001173}
Archana Kakanif9355372025-02-04 03:13:24 -06001174
Archana Kakanic3664472025-02-04 05:36:37 -06001175void HostPDRHandler::setPresentPropertyStatus(const std::string& path)
1176{
1177 CustomDBus::getCustomDBus().updateItemPresentStatus(path, true);
1178}
1179
Archana Kakanif9355372025-02-04 03:13:24 -06001180void HostPDRHandler::setAvailabilityState(const std::string& path)
1181{
1182 CustomDBus::getCustomDBus().setAvailabilityState(path, true);
1183}
1184
George Liu682ee182020-12-25 15:24:33 +08001185void HostPDRHandler::createDbusObjects(const PDRList& fruRecordSetPDRs)
1186{
Archana Kakanif9355372025-02-04 03:13:24 -06001187 // Creating and Refreshing dbus hosted by remote PLDM entity Fru PDRs
1188
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001189 for (const auto& entity : objPathMap)
1190 {
Archana Kakanic3664472025-02-04 05:36:37 -06001191 // update the Present Property
1192 setPresentPropertyStatus(entity.first);
Archana Kakanif9355372025-02-04 03:13:24 -06001193 // Implement & update the Availability to true
1194 setAvailabilityState(entity.first);
1195
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001196 pldm_entity node = pldm_entity_extract(entity.second);
1197 switch (node.entity_type)
1198 {
1199 case PLDM_ENTITY_PROC | 0x8000:
1200 CustomDBus::getCustomDBus().implementCpuCoreInterface(
1201 entity.first);
1202 break;
Archana Kakanidb65c3b2025-02-03 05:27:28 -06001203 case PLDM_ENTITY_SYSTEM_CHASSIS:
1204 CustomDBus::getCustomDBus().implementChassisInterface(
1205 entity.first);
1206 break;
Archana Kakani24e9a9b2025-02-04 00:27:25 -06001207 case PLDM_ENTITY_POWER_SUPPLY:
1208 CustomDBus::getCustomDBus().implementPowerSupplyInterface(
1209 entity.first);
1210 break;
Archana Kakani765cf032025-02-04 06:24:14 -06001211 case PLDM_ENTITY_CHASSIS_FRONT_PANEL_BOARD:
1212 CustomDBus::getCustomDBus().implementPanelInterface(
1213 entity.first);
1214 break;
Archana Kakani2832f2c2025-02-04 22:29:30 -06001215 case PLDM_ENTITY_POWER_CONVERTER:
1216 CustomDBus::getCustomDBus().implementVRMInterface(entity.first);
1217 break;
Archana Kakanibf1fd272024-06-05 13:25:53 -05001218 case PLDM_ENTITY_SLOT:
1219 CustomDBus::getCustomDBus().implementPCIeSlotInterface(
1220 entity.first);
1221 break;
Archana Kakani17b1e8a2025-02-04 03:50:24 -06001222 case PLDM_ENTITY_CONNECTOR:
1223 CustomDBus::getCustomDBus().implementConnecterInterface(
1224 entity.first);
1225 break;
Archana Kakanid432b482025-02-04 22:52:28 -06001226 case PLDM_ENTITY_BOARD:
1227 CustomDBus::getCustomDBus().implementBoard(entity.first);
1228 break;
Archana Kakani733b39d2024-06-05 21:05:20 -05001229 case PLDM_ENTITY_CARD:
1230 CustomDBus::getCustomDBus().implementPCIeDeviceInterface(
1231 entity.first);
1232 break;
Kamalkumar Patel2ed986c2024-05-08 02:20:47 -05001233 case PLDM_ENTITY_SYS_BOARD:
1234 CustomDBus::getCustomDBus().implementMotherboardInterface(
1235 entity.first);
1236 break;
Archana Kakani413f51e2025-02-03 04:14:36 -06001237 case PLDM_ENTITY_FAN:
1238 CustomDBus::getCustomDBus().implementFanInterface(entity.first);
1239 break;
Archana Kakani42876b62025-02-04 04:22:29 -06001240 case PLDM_ENTITY_IO_MODULE:
1241 CustomDBus::getCustomDBus().implementFabricAdapter(
1242 entity.first);
1243 break;
Archana Kakani733b39d2024-06-05 21:05:20 -05001244 default:
1245 break;
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001246 }
1247 }
George Liu682ee182020-12-25 15:24:33 +08001248 getFRURecordTableMetadataByRemote(fruRecordSetPDRs);
1249}
1250
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001251} // namespace pldm