diff options
-rw-r--r-- | redfish-core/include/utils/collection.hpp | 40 | ||||
-rw-r--r-- | redfish-core/include/utils/pcie_util.hpp | 41 | ||||
-rw-r--r-- | redfish-core/lib/pcie.hpp | 5 | ||||
-rw-r--r-- | redfish-core/lib/systems.hpp | 3 |
4 files changed, 41 insertions, 48 deletions
diff --git a/redfish-core/include/utils/collection.hpp b/redfish-core/include/utils/collection.hpp index 7d28c0f72b..79e4fc0ee1 100644 --- a/redfish-core/include/utils/collection.hpp +++ b/redfish-core/include/utils/collection.hpp @@ -22,13 +22,20 @@ namespace collection_util inline void handleCollectionMembers( const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, - const boost::urls::url& collectionPath, const boost::system::error_code& ec, + const boost::urls::url& collectionPath, + const nlohmann::json::json_pointer& jsonKeyName, + const boost::system::error_code& ec, const dbus::utility::MapperGetSubTreePathsResponse& objects) { + nlohmann::json::json_pointer jsonCountKeyName = jsonKeyName; + std::string back = jsonCountKeyName.back(); + jsonCountKeyName.pop_back(); + jsonCountKeyName /= back + "@data.count"; + if (ec == boost::system::errc::io_error) { - asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); - asyncResp->res.jsonValue["Members@odata.count"] = 0; + asyncResp->res.jsonValue[jsonKeyName] = nlohmann::json::array(); + asyncResp->res.jsonValue[jsonCountKeyName] = 0; return; } @@ -52,7 +59,7 @@ inline void handleCollectionMembers( } std::ranges::sort(pathNames, AlphanumLess<std::string>()); - nlohmann::json& members = asyncResp->res.jsonValue["Members"]; + nlohmann::json& members = asyncResp->res.jsonValue[jsonKeyName]; members = nlohmann::json::array(); for (const std::string& leaf : pathNames) { @@ -62,11 +69,11 @@ inline void handleCollectionMembers( member["@odata.id"] = std::move(url); members.emplace_back(std::move(member)); } - asyncResp->res.jsonValue["Members@odata.count"] = members.size(); + asyncResp->res.jsonValue[jsonCountKeyName] = members.size(); } /** - * @brief Populate the collection "Members" from a GetSubTreePaths search of + * @brief Populate the collection members from a GetSubTreePaths search of * inventory * * @param[i,o] asyncResp Async response object @@ -74,19 +81,32 @@ inline void handleCollectionMembers( * Members Redfish Path * @param[i] interfaces List of interfaces to constrain the GetSubTree search * @param[in] subtree D-Bus base path to constrain search to. + * @param[in] jsonKeyName Key name in which the collection members will be + * stored. * * @return void */ inline void + getCollectionToKey(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, + const boost::urls::url& collectionPath, + std::span<const std::string_view> interfaces, + const std::string& subtree, + const nlohmann::json::json_pointer& jsonKeyName) +{ + BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer()); + dbus::utility::getSubTreePaths(subtree, 0, interfaces, + std::bind_front(handleCollectionMembers, + asyncResp, collectionPath, + jsonKeyName)); +} +inline void getCollectionMembers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const boost::urls::url& collectionPath, std::span<const std::string_view> interfaces, const std::string& subtree) { - BMCWEB_LOG_DEBUG("Get collection members for: {}", collectionPath.buffer()); - dbus::utility::getSubTreePaths( - subtree, 0, interfaces, - std::bind_front(handleCollectionMembers, asyncResp, collectionPath)); + getCollectionToKey(asyncResp, collectionPath, interfaces, subtree, + nlohmann::json::json_pointer("/Members")); } } // namespace collection_util diff --git a/redfish-core/include/utils/pcie_util.hpp b/redfish-core/include/utils/pcie_util.hpp index 1a9d2bbf56..c685c3e3af 100644 --- a/redfish-core/include/utils/pcie_util.hpp +++ b/redfish-core/include/utils/pcie_util.hpp @@ -6,6 +6,7 @@ #include "generated/enums/pcie_device.hpp" #include "generated/enums/pcie_slots.hpp" #include "http/utility.hpp" +#include "utils/collection.hpp" #include <boost/system/error_code.hpp> #include <boost/url/format.hpp> @@ -34,44 +35,16 @@ namespace pcie_util inline void getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, - const std::string& name) + const nlohmann::json::json_pointer& jsonKeyName) { static constexpr std::array<std::string_view, 1> pcieDeviceInterface = { "xyz.openbmc_project.Inventory.Item.PCIeDevice"}; + const boost::urls::url pcieDeviceUrl = + boost::urls::url("/redfish/v1/Systems/system/PCIeDevices"); - 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(); - }); + collection_util::getCollectionToKey( + asyncResp, pcieDeviceUrl, pcieDeviceInterface, + "/xyz/openbmc_project/inventory", jsonKeyName); } inline std::optional<pcie_slots::SlotTypes> diff --git a/redfish-core/lib/pcie.hpp b/redfish-core/lib/pcie.hpp index 6bd74f9188..e8da2195a8 100644 --- a/redfish-core/lib/pcie.hpp +++ b/redfish-core/lib/pcie.hpp @@ -131,10 +131,9 @@ static inline void handlePCIeDeviceCollectionGet( "/redfish/v1/Systems/system/PCIeDevices"; asyncResp->res.jsonValue["Name"] = "PCIe Device Collection"; asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices"; - asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); - asyncResp->res.jsonValue["Members@odata.count"] = 0; - pcie_util::getPCIeDeviceList(asyncResp, "Members"); + pcie_util::getPCIeDeviceList(asyncResp, + nlohmann::json::json_pointer("/Members")); } inline void requestRoutesSystemPCIeDeviceCollection(App& app) diff --git a/redfish-core/lib/systems.hpp b/redfish-core/lib/systems.hpp index e044c670d2..5f6cd8783c 100644 --- a/redfish-core/lib/systems.hpp +++ b/redfish-core/lib/systems.hpp @@ -3339,7 +3339,8 @@ inline void getBootProperties(asyncResp); getBootProgress(asyncResp); getBootProgressLastStateTime(asyncResp); - pcie_util::getPCIeDeviceList(asyncResp, "PCIeDevices"); + pcie_util::getPCIeDeviceList(asyncResp, + nlohmann::json::json_pointer("/PCIeDevices")); getHostWatchdogTimer(asyncResp); getPowerRestorePolicy(asyncResp); getStopBootOnFault(asyncResp); |