1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3 #pragma once 4 5 #include "async_resp.hpp" 6 #include "dbus_utility.hpp" 7 #include "error_messages.hpp" 8 #include "logging.hpp" 9 10 #include <sdbusplus/message/native_types.hpp> 11 12 #include <array> 13 #include <memory> 14 #include <optional> 15 #include <string> 16 #include <string_view> 17 18 namespace redfish 19 { 20 21 static constexpr std::array<std::string_view, 2> chassisInterfaces = { 22 "xyz.openbmc_project.Inventory.Item.Board", 23 "xyz.openbmc_project.Inventory.Item.Chassis"}; 24 25 namespace chassis_utils 26 { 27 /** 28 * @brief Retrieves valid chassis path 29 * @param asyncResp Pointer to object holding response data 30 * @param callback Callback for next step to get valid chassis path 31 */ 32 template <typename Callback> 33 void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 34 const std::string& chassisId, Callback&& callback) 35 { 36 BMCWEB_LOG_DEBUG("checkChassisId enter"); 37 38 // Get the Chassis Collection 39 dbus::utility::getSubTreePaths( 40 "/xyz/openbmc_project/inventory", 0, chassisInterfaces, 41 [callback = std::forward<Callback>(callback), asyncResp, 42 chassisId](const boost::system::error_code& ec, 43 const dbus::utility::MapperGetSubTreePathsResponse& 44 chassisPaths) mutable { 45 BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter"); 46 if (ec) 47 { 48 BMCWEB_LOG_ERROR( 49 "getValidChassisPath respHandler DBUS error: {}", ec); 50 messages::internalError(asyncResp->res); 51 return; 52 } 53 54 std::optional<std::string> chassisPath; 55 for (const std::string& chassis : chassisPaths) 56 { 57 sdbusplus::message::object_path path(chassis); 58 std::string chassisName = path.filename(); 59 if (chassisName.empty()) 60 { 61 BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis); 62 continue; 63 } 64 if (chassisName == chassisId) 65 { 66 chassisPath = chassis; 67 break; 68 } 69 } 70 callback(chassisPath); 71 }); 72 BMCWEB_LOG_DEBUG("checkChassisId exit"); 73 } 74 75 } // namespace chassis_utils 76 } // namespace redfish 77