1 #pragma once
2 
3 #include "async_resp.hpp"
4 #include "dbus_utility.hpp"
5 #include "error_messages.hpp"
6 #include "http/utility.hpp"
7 
8 #include <boost/system/error_code.hpp>
9 #include <boost/url/format.hpp>
10 #include <nlohmann/json.hpp>
11 
12 #include <array>
13 #include <memory>
14 #include <string>
15 #include <string_view>
16 
17 namespace redfish
18 {
19 namespace pcie_util
20 {
21 
22 /**
23  * @brief Populate the PCIe Device list from a GetSubTreePaths search of
24  *        inventory
25  *
26  * @param[i,o] aResp  Async response object
27  * @param[i]   Name   Key to store the list of PCIe devices in aResp
28  *
29  * @return void
30  */
31 
32 inline void
33     getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34                       const std::string& name)
35 {
36     static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
37         "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
38 
39     dbus::utility::getSubTreePaths(
40         "/xyz/openbmc_project/inventory", 0, pcieDeviceInterface,
41         [asyncResp, name](const boost::system::error_code& ec,
42                           const dbus::utility::MapperGetSubTreePathsResponse&
43                               pcieDevicePaths) {
44         if (ec)
45         {
46             BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: "
47                              << ec.message();
48             // Not an error, system just doesn't have PCIe info
49             return;
50         }
51         nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name];
52         pcieDeviceList = nlohmann::json::array();
53         for (const std::string& pcieDevicePath : pcieDevicePaths)
54         {
55             size_t devStart = pcieDevicePath.rfind('/');
56             if (devStart == std::string::npos)
57             {
58                 continue;
59             }
60 
61             std::string devName = pcieDevicePath.substr(devStart + 1);
62             if (devName.empty())
63             {
64                 continue;
65             }
66             nlohmann::json::object_t pcieDevice;
67             pcieDevice["@odata.id"] = boost::urls::format(
68                 "/redfish/v1/Systems/system/PCIeDevices/{}", devName);
69             pcieDeviceList.emplace_back(std::move(pcieDevice));
70         }
71         asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size();
72         });
73 }
74 
75 } // namespace pcie_util
76 } // namespace redfish
77