xref: /openbmc/bmcweb/redfish-core/include/utils/chassis_utils.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
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 
9 #include <array>
10 #include <string_view>
11 
12 namespace redfish
13 {
14 
15 namespace chassis_utils
16 {
17 /**
18  * @brief Retrieves valid chassis path
19  * @param asyncResp   Pointer to object holding response data
20  * @param callback  Callback for next step to get valid chassis path
21  */
22 template <typename Callback>
23 void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24                          const std::string& chassisId, Callback&& callback)
25 {
26     BMCWEB_LOG_DEBUG("checkChassisId enter");
27     constexpr std::array<std::string_view, 2> interfaces = {
28         "xyz.openbmc_project.Inventory.Item.Board",
29         "xyz.openbmc_project.Inventory.Item.Chassis"};
30 
31     // Get the Chassis Collection
32     dbus::utility::getSubTreePaths(
33         "/xyz/openbmc_project/inventory", 0, interfaces,
34         [callback = std::forward<Callback>(callback), asyncResp,
35          chassisId](const boost::system::error_code& ec,
36                     const dbus::utility::MapperGetSubTreePathsResponse&
37                         chassisPaths) mutable {
38             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
39             if (ec)
40             {
41                 BMCWEB_LOG_ERROR(
42                     "getValidChassisPath respHandler DBUS error: {}", ec);
43                 messages::internalError(asyncResp->res);
44                 return;
45             }
46 
47             std::optional<std::string> chassisPath;
48             for (const std::string& chassis : chassisPaths)
49             {
50                 sdbusplus::message::object_path path(chassis);
51                 std::string chassisName = path.filename();
52                 if (chassisName.empty())
53                 {
54                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
55                     continue;
56                 }
57                 if (chassisName == chassisId)
58                 {
59                     chassisPath = chassis;
60                     break;
61                 }
62             }
63             callback(chassisPath);
64         });
65     BMCWEB_LOG_DEBUG("checkChassisId exit");
66 }
67 
68 } // namespace chassis_utils
69 } // namespace redfish
70