From 472bd202da308ea7ba6c9f5606e025367b52fccc Mon Sep 17 00:00:00 2001 From: Lakshmi Yadlapati Date: Wed, 22 Mar 2023 09:57:05 -0500 Subject: Move getPCIeDeviceList to pcie_util Currently, getPCIeDeviceList is only used by systems.hpp to obtain the list of PCIe devices. However, there are plans to use this function in other parts of the PCIe code as well. To better organize our code and make the function more reusable, this commit moves getPCIeDeviceList to pcie_util.hpp, a common location for PCIe-related utilities. Tested: ''' curl -k https://$bmc/redfish/v1/Systems/system { "@odata.id": "/redfish/v1/Systems/system", "@odata.type": "#ComputerSystem.v1_16_0.ComputerSystem", "Actions": { "#ComputerSystem.Reset": { "@Redfish.ActionInfo": "/redfish/v1/Systems/system/ResetActionInfo", "target": "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" } }, ..... ..... "PCIeDevices": [ { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/dp0_drive2" }, { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/dp0_drive3" }, ..... ..... { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/pcie_card0" }, { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/pcie_card1" }, { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/pcie_card10" }, ..... { "@odata.id": "/redfish/v1/Systems/system/PCIeDevices/pcie_card9" } ], "PCIeDevices@odata.count": 20, ..... ..... ''' Change-Id: I3aaa5b55e8574929154ffd743db53da6fbaeb75d Signed-off-by: Lakshmi Yadlapati --- redfish-core/include/utils/pcie_util.hpp | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 redfish-core/include/utils/pcie_util.hpp (limited to 'redfish-core/include') diff --git a/redfish-core/include/utils/pcie_util.hpp b/redfish-core/include/utils/pcie_util.hpp new file mode 100644 index 0000000000..51423b435f --- /dev/null +++ b/redfish-core/include/utils/pcie_util.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include "async_resp.hpp" +#include "dbus_utility.hpp" +#include "error_messages.hpp" +#include "http/utility.hpp" + +#include +#include +#include + +#include +#include +#include +#include + +namespace redfish +{ +namespace pcie_util +{ + +/** + * @brief Populate the PCIe Device list from a GetSubTreePaths search of + * inventory + * + * @param[i,o] aResp Async response object + * @param[i] Name Key to store the list of PCIe devices in aResp + * + * @return void + */ + +inline void + getPCIeDeviceList(const std::shared_ptr& asyncResp, + const std::string& name) +{ + static constexpr std::array pcieDeviceInterface = { + "xyz.openbmc_project.Inventory.Item.PCIeDevice"}; + + dbus::utility::getSubTreePaths( + "/xyz/openbmc_project/inventory", 0, pcieDeviceInterface, + [asyncResp, name](const boost::system::error_code& ec, + const dbus::utility::MapperGetSubTreePathsResponse& + pcieDevicePaths) { + if (ec) + { + BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: " + << ec.message(); + // Not an error, system just doesn't have PCIe info + return; + } + nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name]; + pcieDeviceList = nlohmann::json::array(); + for (const std::string& pcieDevicePath : pcieDevicePaths) + { + size_t devStart = pcieDevicePath.rfind('/'); + if (devStart == std::string::npos) + { + continue; + } + + std::string devName = pcieDevicePath.substr(devStart + 1); + if (devName.empty()) + { + continue; + } + nlohmann::json::object_t pcieDevice; + pcieDevice["@odata.id"] = boost::urls::format( + "/redfish/v1/Systems/system/PCIeDevices/{}", devName); + pcieDeviceList.emplace_back(std::move(pcieDevice)); + } + asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size(); + }); +} + +} // namespace pcie_util +} // namespace redfish -- cgit v1.2.3