xref: /openbmc/bmcweb/features/redfish/include/utils/chassis_utils.hpp (revision 177612aaa0633cf9d5aef0b763a43135cf552d9b)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
30d7702c0SZhenwei Chen #pragma once
47a1dbc48SGeorge Liu 
53ccb3adbSEd Tanous #include "async_resp.hpp"
6*177612aaSEd Tanous #include "boost_formatters.hpp"
77a1dbc48SGeorge Liu #include "dbus_utility.hpp"
83ccb3adbSEd Tanous #include "error_messages.hpp"
9d7857201SEd Tanous #include "logging.hpp"
10d7857201SEd Tanous 
11d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
120d7702c0SZhenwei Chen 
137a1dbc48SGeorge Liu #include <array>
14d7857201SEd Tanous #include <memory>
15d7857201SEd Tanous #include <optional>
16d7857201SEd Tanous #include <string>
177a1dbc48SGeorge Liu #include <string_view>
187a1dbc48SGeorge Liu 
190d7702c0SZhenwei Chen namespace redfish
200d7702c0SZhenwei Chen {
210d7702c0SZhenwei Chen 
223f95a277SMyung Bae static constexpr std::array<std::string_view, 2> chassisInterfaces = {
233f95a277SMyung Bae     "xyz.openbmc_project.Inventory.Item.Board",
243f95a277SMyung Bae     "xyz.openbmc_project.Inventory.Item.Chassis"};
253f95a277SMyung Bae 
260d7702c0SZhenwei Chen namespace chassis_utils
270d7702c0SZhenwei Chen {
280d7702c0SZhenwei Chen /**
290d7702c0SZhenwei Chen  * @brief Retrieves valid chassis path
300d7702c0SZhenwei Chen  * @param asyncResp   Pointer to object holding response data
310d7702c0SZhenwei Chen  * @param callback  Callback for next step to get valid chassis path
320d7702c0SZhenwei Chen  */
330d7702c0SZhenwei Chen template <typename Callback>
340d7702c0SZhenwei Chen void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
350d7702c0SZhenwei Chen                          const std::string& chassisId, Callback&& callback)
360d7702c0SZhenwei Chen {
3762598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId enter");
380d7702c0SZhenwei Chen 
397a1dbc48SGeorge Liu     // Get the Chassis Collection
407a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
413f95a277SMyung Bae         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
428cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
437a1dbc48SGeorge Liu          chassisId](const boost::system::error_code& ec,
440d7702c0SZhenwei Chen                     const dbus::utility::MapperGetSubTreePathsResponse&
450d7702c0SZhenwei Chen                         chassisPaths) mutable {
4662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
470d7702c0SZhenwei Chen             if (ec)
480d7702c0SZhenwei Chen             {
49bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
50bd79bce8SPatrick Williams                     "getValidChassisPath respHandler DBUS error: {}", ec);
510d7702c0SZhenwei Chen                 messages::internalError(asyncResp->res);
520d7702c0SZhenwei Chen                 return;
530d7702c0SZhenwei Chen             }
540d7702c0SZhenwei Chen 
550d7702c0SZhenwei Chen             std::optional<std::string> chassisPath;
560d7702c0SZhenwei Chen             for (const std::string& chassis : chassisPaths)
570d7702c0SZhenwei Chen             {
580d7702c0SZhenwei Chen                 sdbusplus::message::object_path path(chassis);
59f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
600d7702c0SZhenwei Chen                 if (chassisName.empty())
610d7702c0SZhenwei Chen                 {
6262598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
630d7702c0SZhenwei Chen                     continue;
640d7702c0SZhenwei Chen                 }
650d7702c0SZhenwei Chen                 if (chassisName == chassisId)
660d7702c0SZhenwei Chen                 {
670d7702c0SZhenwei Chen                     chassisPath = chassis;
680d7702c0SZhenwei Chen                     break;
690d7702c0SZhenwei Chen                 }
700d7702c0SZhenwei Chen             }
710d7702c0SZhenwei Chen             callback(chassisPath);
727a1dbc48SGeorge Liu         });
7362598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId exit");
740d7702c0SZhenwei Chen }
750d7702c0SZhenwei Chen 
760d7702c0SZhenwei Chen } // namespace chassis_utils
770d7702c0SZhenwei Chen } // namespace redfish
78