17691cc2fSChicago Duan #pragma once
27691cc2fSChicago Duan 
37691cc2fSChicago Duan #include "error_messages.hpp"
47691cc2fSChicago Duan #include "utility.hpp"
57691cc2fSChicago Duan 
67691cc2fSChicago Duan #include <app.hpp>
77691cc2fSChicago Duan #include <pcie.hpp>
87691cc2fSChicago Duan #include <registries/privilege_registry.hpp>
97691cc2fSChicago Duan #include <utils/json_utils.hpp>
107691cc2fSChicago Duan 
117691cc2fSChicago Duan namespace redfish
127691cc2fSChicago Duan {
137691cc2fSChicago Duan 
147691cc2fSChicago Duan inline std::string dbusSlotTypeToRf(const std::string& slotType)
157691cc2fSChicago Duan {
167691cc2fSChicago Duan     if (slotType ==
177691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength")
187691cc2fSChicago Duan     {
197691cc2fSChicago Duan         return "FullLength";
207691cc2fSChicago Duan     }
217691cc2fSChicago Duan     if (slotType ==
227691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength")
237691cc2fSChicago Duan     {
247691cc2fSChicago Duan         return "HalfLength";
257691cc2fSChicago Duan     }
267691cc2fSChicago Duan     if (slotType ==
277691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile")
287691cc2fSChicago Duan     {
297691cc2fSChicago Duan         return "LowProfile";
307691cc2fSChicago Duan     }
317691cc2fSChicago Duan     if (slotType ==
327691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini")
337691cc2fSChicago Duan     {
347691cc2fSChicago Duan         return "Mini";
357691cc2fSChicago Duan     }
367691cc2fSChicago Duan     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2")
377691cc2fSChicago Duan     {
387691cc2fSChicago Duan         return "M2";
397691cc2fSChicago Duan     }
407691cc2fSChicago Duan     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM")
417691cc2fSChicago Duan     {
427691cc2fSChicago Duan         return "OEM";
437691cc2fSChicago Duan     }
447691cc2fSChicago Duan     if (slotType ==
457691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small")
467691cc2fSChicago Duan     {
477691cc2fSChicago Duan         return "OCP3Small";
487691cc2fSChicago Duan     }
497691cc2fSChicago Duan     if (slotType ==
507691cc2fSChicago Duan         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large")
517691cc2fSChicago Duan     {
527691cc2fSChicago Duan         return "OCP3Large";
537691cc2fSChicago Duan     }
547691cc2fSChicago Duan     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2")
557691cc2fSChicago Duan     {
567691cc2fSChicago Duan         return "U2";
577691cc2fSChicago Duan     }
587691cc2fSChicago Duan 
597691cc2fSChicago Duan     // Unknown or others
607691cc2fSChicago Duan     return "";
617691cc2fSChicago Duan }
627691cc2fSChicago Duan 
637691cc2fSChicago Duan inline void
647691cc2fSChicago Duan     onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
657691cc2fSChicago Duan                          const boost::system::error_code ec,
667691cc2fSChicago Duan                          const dbus::utility::DBusPropertiesMap& propertiesList)
677691cc2fSChicago Duan {
687691cc2fSChicago Duan     if (ec)
697691cc2fSChicago Duan     {
707691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "Can't get PCIeSlot properties!";
717691cc2fSChicago Duan         messages::internalError(asyncResp->res);
727691cc2fSChicago Duan         return;
737691cc2fSChicago Duan     }
747691cc2fSChicago Duan 
757691cc2fSChicago Duan     nlohmann::json& slots = asyncResp->res.jsonValue["Slots"];
767691cc2fSChicago Duan 
777691cc2fSChicago Duan     nlohmann::json::array_t* slotsPtr =
787691cc2fSChicago Duan         slots.get_ptr<nlohmann::json::array_t*>();
797691cc2fSChicago Duan     if (slotsPtr == nullptr)
807691cc2fSChicago Duan     {
817691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "Slots key isn't an array???";
827691cc2fSChicago Duan         messages::internalError(asyncResp->res);
837691cc2fSChicago Duan         return;
847691cc2fSChicago Duan     }
857691cc2fSChicago Duan 
867691cc2fSChicago Duan     nlohmann::json::object_t slot;
877691cc2fSChicago Duan 
887691cc2fSChicago Duan     for (const auto& property : propertiesList)
897691cc2fSChicago Duan     {
907691cc2fSChicago Duan         const std::string& propertyName = property.first;
917691cc2fSChicago Duan 
927691cc2fSChicago Duan         if (propertyName == "Generation")
937691cc2fSChicago Duan         {
947691cc2fSChicago Duan             const std::string* value =
957691cc2fSChicago Duan                 std::get_if<std::string>(&property.second);
967691cc2fSChicago Duan             if (value == nullptr)
977691cc2fSChicago Duan             {
987691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
997691cc2fSChicago Duan                 return;
1007691cc2fSChicago Duan             }
1017691cc2fSChicago Duan             std::optional<std::string> pcieType =
1027691cc2fSChicago Duan                 redfishPcieGenerationFromDbus(*value);
1037691cc2fSChicago Duan             if (!pcieType)
1047691cc2fSChicago Duan             {
1057691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1067691cc2fSChicago Duan                 return;
1077691cc2fSChicago Duan             }
1087691cc2fSChicago Duan             slot["PCIeType"] = !pcieType;
1097691cc2fSChicago Duan         }
1107691cc2fSChicago Duan         else if (propertyName == "Lanes")
1117691cc2fSChicago Duan         {
1127691cc2fSChicago Duan             const size_t* value = std::get_if<size_t>(&property.second);
1137691cc2fSChicago Duan             if (value == nullptr)
1147691cc2fSChicago Duan             {
1157691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1167691cc2fSChicago Duan                 return;
1177691cc2fSChicago Duan             }
1187691cc2fSChicago Duan             slot["Lanes"] = *value;
1197691cc2fSChicago Duan         }
1207691cc2fSChicago Duan         else if (propertyName == "SlotType")
1217691cc2fSChicago Duan         {
1227691cc2fSChicago Duan             const std::string* value =
1237691cc2fSChicago Duan                 std::get_if<std::string>(&property.second);
1247691cc2fSChicago Duan             if (value == nullptr)
1257691cc2fSChicago Duan             {
1267691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1277691cc2fSChicago Duan                 return;
1287691cc2fSChicago Duan             }
1297691cc2fSChicago Duan             std::string slotType = dbusSlotTypeToRf(*value);
1307691cc2fSChicago Duan             if (!slotType.empty())
1317691cc2fSChicago Duan             {
1327691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1337691cc2fSChicago Duan                 return;
1347691cc2fSChicago Duan             }
1357691cc2fSChicago Duan             slot["SlotType"] = slotType;
1367691cc2fSChicago Duan         }
1377691cc2fSChicago Duan         else if (propertyName == "HotPluggable")
1387691cc2fSChicago Duan         {
1397691cc2fSChicago Duan             const bool* value = std::get_if<bool>(&property.second);
1407691cc2fSChicago Duan             if (value == nullptr)
1417691cc2fSChicago Duan             {
1427691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1437691cc2fSChicago Duan                 return;
1447691cc2fSChicago Duan             }
1457691cc2fSChicago Duan             slot["HotPluggable"] = *value;
1467691cc2fSChicago Duan         }
1477691cc2fSChicago Duan     }
1487691cc2fSChicago Duan     slots.emplace_back(std::move(slot));
1497691cc2fSChicago Duan }
1507691cc2fSChicago Duan 
1517691cc2fSChicago Duan inline void onMapperAssociationDone(
1527691cc2fSChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1537691cc2fSChicago Duan     const std::string& chassisID, const std::string& pcieSlotPath,
1547691cc2fSChicago Duan     const std::string& connectionName, const boost::system::error_code ec,
1557691cc2fSChicago Duan     const std::variant<std::vector<std::string>>& endpoints)
1567691cc2fSChicago Duan {
1577691cc2fSChicago Duan     if (ec)
1587691cc2fSChicago Duan     {
1597691cc2fSChicago Duan         if (ec.value() == EBADR)
1607691cc2fSChicago Duan         {
1617691cc2fSChicago Duan             // This PCIeSlot have no chassis association.
1627691cc2fSChicago Duan             return;
1637691cc2fSChicago Duan         }
1647691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "DBUS response error";
1657691cc2fSChicago Duan         messages::internalError(asyncResp->res);
1667691cc2fSChicago Duan         return;
1677691cc2fSChicago Duan     }
1687691cc2fSChicago Duan 
1697691cc2fSChicago Duan     const std::vector<std::string>* pcieSlotChassis =
1707691cc2fSChicago Duan         std::get_if<std::vector<std::string>>(&(endpoints));
1717691cc2fSChicago Duan 
1727691cc2fSChicago Duan     if (pcieSlotChassis == nullptr)
1737691cc2fSChicago Duan     {
1747691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "Error getting PCIe Slot association!";
1757691cc2fSChicago Duan         messages::internalError(asyncResp->res);
1767691cc2fSChicago Duan         return;
1777691cc2fSChicago Duan     }
1787691cc2fSChicago Duan 
1797691cc2fSChicago Duan     if (pcieSlotChassis->size() != 1)
1807691cc2fSChicago Duan     {
1817691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "PCIe Slot association error! ";
1827691cc2fSChicago Duan         messages::internalError(asyncResp->res);
1837691cc2fSChicago Duan         return;
1847691cc2fSChicago Duan     }
1857691cc2fSChicago Duan 
1867691cc2fSChicago Duan     sdbusplus::message::object_path path((*pcieSlotChassis)[0]);
1877691cc2fSChicago Duan     std::string chassisName = path.filename();
1887691cc2fSChicago Duan     if (chassisName != chassisID)
1897691cc2fSChicago Duan     {
1907691cc2fSChicago Duan         // The pcie slot doesn't belong to the chassisID
1917691cc2fSChicago Duan         return;
1927691cc2fSChicago Duan     }
1937691cc2fSChicago Duan 
1947691cc2fSChicago Duan     crow::connections::systemBus->async_method_call(
1957691cc2fSChicago Duan         [asyncResp](const boost::system::error_code ec,
1967691cc2fSChicago Duan                     const dbus::utility::DBusPropertiesMap& propertiesList) {
1977691cc2fSChicago Duan         onPcieSlotGetAllDone(asyncResp, ec, propertiesList);
1987691cc2fSChicago Duan         },
1997691cc2fSChicago Duan         connectionName, pcieSlotPath, "org.freedesktop.DBus.Properties",
2007691cc2fSChicago Duan         "GetAll", "xyz.openbmc_project.Inventory.Item.PCIeSlot");
2017691cc2fSChicago Duan }
2027691cc2fSChicago Duan 
2037691cc2fSChicago Duan inline void
2047691cc2fSChicago Duan     onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2057691cc2fSChicago Duan                         const std::string& chassisID,
2067691cc2fSChicago Duan                         const boost::system::error_code ec,
2077691cc2fSChicago Duan                         const dbus::utility::MapperGetSubTreeResponse& subtree)
2087691cc2fSChicago Duan {
2097691cc2fSChicago Duan     if (ec)
2107691cc2fSChicago Duan     {
2117691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
2127691cc2fSChicago Duan         messages::internalError(asyncResp->res);
2137691cc2fSChicago Duan         return;
2147691cc2fSChicago Duan     }
2157691cc2fSChicago Duan     if (subtree.empty())
2167691cc2fSChicago Duan     {
217*d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "Chassis", chassisID);
2187691cc2fSChicago Duan         return;
2197691cc2fSChicago Duan     }
2207691cc2fSChicago Duan 
2217691cc2fSChicago Duan     BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = "
2227691cc2fSChicago Duan                      << chassisID;
2237691cc2fSChicago Duan 
2247691cc2fSChicago Duan     asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots";
2257691cc2fSChicago Duan     asyncResp->res.jsonValue["Name"] = "PCIe Slot Information";
2267691cc2fSChicago Duan     asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
2277691cc2fSChicago Duan         "redfish", "v1", "Chassis", chassisID, "PCIeSlots");
2287691cc2fSChicago Duan     asyncResp->res.jsonValue["Id"] = "1";
2297691cc2fSChicago Duan     asyncResp->res.jsonValue["Slots"] = nlohmann::json::array();
2307691cc2fSChicago Duan 
2317691cc2fSChicago Duan     for (const auto& pathServicePair : subtree)
2327691cc2fSChicago Duan     {
2337691cc2fSChicago Duan         const std::string& pcieSlotPath = pathServicePair.first;
2347691cc2fSChicago Duan         for (const auto& connectionInterfacePair : pathServicePair.second)
2357691cc2fSChicago Duan         {
2367691cc2fSChicago Duan             const std::string& connectionName = connectionInterfacePair.first;
2377691cc2fSChicago Duan             sdbusplus::message::object_path pcieSlotAssociationPath(
2387691cc2fSChicago Duan                 pcieSlotPath);
2397691cc2fSChicago Duan             pcieSlotAssociationPath /= "chassis";
2407691cc2fSChicago Duan 
2417691cc2fSChicago Duan             // The association of this PCIeSlot is used to determine whether
2427691cc2fSChicago Duan             // it belongs to this ChassisID
2437691cc2fSChicago Duan             crow::connections::systemBus->async_method_call(
2447691cc2fSChicago Duan                 [asyncResp, chassisID, pcieSlotPath, connectionName](
2457691cc2fSChicago Duan                     const boost::system::error_code ec,
2467691cc2fSChicago Duan                     const std::variant<std::vector<std::string>>& endpoints) {
2477691cc2fSChicago Duan                 onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
2487691cc2fSChicago Duan                                         connectionName, ec, endpoints);
2497691cc2fSChicago Duan                 },
2507691cc2fSChicago Duan                 "xyz.openbmc_project.ObjectMapper",
2517691cc2fSChicago Duan                 std::string{pcieSlotAssociationPath},
2527691cc2fSChicago Duan                 "org.freedesktop.DBus.Properties", "Get",
2537691cc2fSChicago Duan                 "xyz.openbmc_project.Association", "endpoints");
2547691cc2fSChicago Duan         }
2557691cc2fSChicago Duan     }
2567691cc2fSChicago Duan }
2577691cc2fSChicago Duan 
2587691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet(
2597691cc2fSChicago Duan     crow::App& app, const crow::Request& req,
2607691cc2fSChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2617691cc2fSChicago Duan     const std::string& chassisID)
2627691cc2fSChicago Duan {
2632aacf856SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2647691cc2fSChicago Duan     {
2657691cc2fSChicago Duan         return;
2667691cc2fSChicago Duan     }
2677691cc2fSChicago Duan 
2687691cc2fSChicago Duan     crow::connections::systemBus->async_method_call(
2697691cc2fSChicago Duan         [asyncResp,
2707691cc2fSChicago Duan          chassisID](const boost::system::error_code ec,
2717691cc2fSChicago Duan                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
2727691cc2fSChicago Duan         onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
2737691cc2fSChicago Duan         },
2747691cc2fSChicago Duan         "xyz.openbmc_project.ObjectMapper",
2757691cc2fSChicago Duan         "/xyz/openbmc_project/object_mapper",
2767691cc2fSChicago Duan         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2777691cc2fSChicago Duan         "/xyz/openbmc_project/inventory", int32_t(0),
2787691cc2fSChicago Duan         std::array<const char*, 1>{
2797691cc2fSChicago Duan             "xyz.openbmc_project.Inventory.Item.PCIeSlot"});
2807691cc2fSChicago Duan }
2817691cc2fSChicago Duan 
2827691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app)
2837691cc2fSChicago Duan {
2847691cc2fSChicago Duan     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/")
2857691cc2fSChicago Duan         .privileges(redfish::privileges::getPCIeSlots)
2867691cc2fSChicago Duan         .methods(boost::beast::http::verb::get)(
2877691cc2fSChicago Duan             std::bind_front(handlePCIeSlotCollectionGet, std::ref(app)));
2887691cc2fSChicago Duan }
2897691cc2fSChicago Duan 
2907691cc2fSChicago Duan } // namespace redfish
291