1 #pragma once
2 
3 #include "async_resp.hpp"
4 #include "dbus_utility.hpp"
5 #include "error_messages.hpp"
6 #include "generated/enums/pcie_device.hpp"
7 #include "generated/enums/pcie_slots.hpp"
8 #include "http/utility.hpp"
9 #include "utils/collection.hpp"
10 
11 #include <boost/system/error_code.hpp>
12 #include <boost/url/format.hpp>
13 #include <nlohmann/json.hpp>
14 
15 #include <array>
16 #include <memory>
17 #include <optional>
18 #include <string>
19 #include <string_view>
20 
21 namespace redfish
22 {
23 namespace pcie_util
24 {
25 
26 /**
27  * @brief Populate the PCIe Device list from a GetSubTreePaths search of
28  *        inventory
29  *
30  * @param[i,o] asyncResp  Async response object
31  * @param[i]   Name   Key to store the list of PCIe devices in asyncResp
32  *
33  * @return void
34  */
35 
36 inline void
37     getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38                       const nlohmann::json::json_pointer& jsonKeyName)
39 {
40     static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
41         "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
42     const boost::urls::url pcieDeviceUrl = boost::urls::format(
43         "/redfish/v1/Systems/{}/PCIeDevices", BMCWEB_REDFISH_SYSTEM_URI_NAME);
44 
45     collection_util::getCollectionToKey(
46         asyncResp, pcieDeviceUrl, pcieDeviceInterface,
47         "/xyz/openbmc_project/inventory", jsonKeyName);
48 }
49 
50 inline std::optional<pcie_slots::SlotTypes>
51     dbusSlotTypeToRf(const std::string& slotType)
52 {
53     if (slotType ==
54         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength")
55     {
56         return pcie_slots::SlotTypes::FullLength;
57     }
58     if (slotType ==
59         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength")
60     {
61         return pcie_slots::SlotTypes::HalfLength;
62     }
63     if (slotType ==
64         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile")
65     {
66         return pcie_slots::SlotTypes::LowProfile;
67     }
68     if (slotType ==
69         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini")
70     {
71         return pcie_slots::SlotTypes::Mini;
72     }
73     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2")
74     {
75         return pcie_slots::SlotTypes::M2;
76     }
77     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM")
78     {
79         return pcie_slots::SlotTypes::OEM;
80     }
81     if (slotType ==
82         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small")
83     {
84         return pcie_slots::SlotTypes::OCP3Small;
85     }
86     if (slotType ==
87         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large")
88     {
89         return pcie_slots::SlotTypes::OCP3Large;
90     }
91     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2")
92     {
93         return pcie_slots::SlotTypes::U2;
94     }
95     if (slotType ==
96         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Unknown")
97     {
98         return std::nullopt;
99     }
100 
101     return pcie_slots::SlotTypes::Invalid;
102 }
103 
104 inline std::optional<pcie_device::PCIeTypes>
105     redfishPcieGenerationFromDbus(const std::string& generationInUse)
106 {
107     if (generationInUse ==
108         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
109     {
110         return pcie_device::PCIeTypes::Gen1;
111     }
112     if (generationInUse ==
113         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
114     {
115         return pcie_device::PCIeTypes::Gen2;
116     }
117     if (generationInUse ==
118         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
119     {
120         return pcie_device::PCIeTypes::Gen3;
121     }
122     if (generationInUse ==
123         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
124     {
125         return pcie_device::PCIeTypes::Gen4;
126     }
127     if (generationInUse ==
128         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
129     {
130         return pcie_device::PCIeTypes::Gen5;
131     }
132     if (generationInUse.empty() ||
133         generationInUse ==
134             "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
135     {
136         return std::nullopt;
137     }
138 
139     return pcie_device::PCIeTypes::Invalid;
140 }
141 
142 } // namespace pcie_util
143 } // namespace redfish
144