#pragma once #include "app.hpp" #include "dbus_utility.hpp" #include "error_messages.hpp" #include "query.hpp" #include "registries/privilege_registry.hpp" #include "utils/chassis_utils.hpp" #include #include #include #include #include #include #include namespace redfish { constexpr std::array fanInterface = { "xyz.openbmc_project.Inventory.Item.Fan"}; inline void updateFanList(const std::shared_ptr& asyncResp, const std::string& chassisId, const dbus::utility::MapperGetSubTreePathsResponse& fanPaths) { nlohmann::json& fanList = asyncResp->res.jsonValue["Members"]; for (const std::string& fanPath : fanPaths) { std::string fanName = sdbusplus::message::object_path(fanPath).filename(); if (fanName.empty()) { continue; } nlohmann::json item = nlohmann::json::object(); item["@odata.id"] = boost::urls::format( "/redfish/v1/Chassis/{}/ThermalSubsystem/Fans/{}", chassisId, fanName); fanList.emplace_back(std::move(item)); } asyncResp->res.jsonValue["Members@odata.count"] = fanList.size(); } inline void getFanPaths( const std::shared_ptr& asyncResp, const std::optional& validChassisPath, const std::function& callback) { sdbusplus::message::object_path endpointPath{*validChassisPath}; endpointPath /= "cooled_by"; dbus::utility::getAssociatedSubTreePaths( endpointPath, sdbusplus::message::object_path("/xyz/openbmc_project/inventory"), 0, fanInterface, [asyncResp, callback]( const boost::system::error_code& ec, const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) { if (ec) { if (ec.value() != EBADR) { BMCWEB_LOG_ERROR << "DBUS response error for getAssociatedSubTreePaths " << ec.value(); messages::internalError(asyncResp->res); } return; } callback(subtreePaths); }); } inline void doFanCollection(const std::shared_ptr& asyncResp, const std::string& chassisId, const std::optional& validChassisPath) { if (!validChassisPath) { messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); return; } asyncResp->res.addHeader( boost::beast::http::field::link, "; rel=describedby"); asyncResp->res.jsonValue["@odata.type"] = "#FanCollection.FanCollection"; asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( "/redfish/v1/Chassis/{}/ThermalSubsystem/Fans", chassisId); asyncResp->res.jsonValue["Name"] = "Fan Collection"; asyncResp->res.jsonValue["Description"] = "The collection of Fan resource instances " + chassisId; asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); asyncResp->res.jsonValue["Members@odata.count"] = 0; getFanPaths(asyncResp, validChassisPath, std::bind_front(updateFanList, asyncResp, chassisId)); } inline void handleFanCollectionHead(App& app, const crow::Request& req, const std::shared_ptr& asyncResp, const std::string& chassisId) { if (!redfish::setUpRedfishRoute(app, req, asyncResp)) { return; } redfish::chassis_utils::getValidChassisPath( asyncResp, chassisId, [asyncResp, chassisId](const std::optional& validChassisPath) { if (!validChassisPath) { messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); return; } asyncResp->res.addHeader( boost::beast::http::field::link, "; rel=describedby"); }); } inline void handleFanCollectionGet(App& app, const crow::Request& req, const std::shared_ptr& asyncResp, const std::string& chassisId) { if (!redfish::setUpRedfishRoute(app, req, asyncResp)) { return; } redfish::chassis_utils::getValidChassisPath( asyncResp, chassisId, std::bind_front(doFanCollection, asyncResp, chassisId)); } inline void requestRoutesFanCollection(App& app) { BMCWEB_ROUTE(app, "/redfish/v1/Chassis//ThermalSubsystem/Fans/") .privileges(redfish::privileges::headFanCollection) .methods(boost::beast::http::verb::head)( std::bind_front(handleFanCollectionHead, std::ref(app))); BMCWEB_ROUTE(app, "/redfish/v1/Chassis//ThermalSubsystem/Fans/") .privileges(redfish::privileges::getFanCollection) .methods(boost::beast::http::verb::get)( std::bind_front(handleFanCollectionGet, std::ref(app))); } } // namespace redfish