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