xref: /openbmc/bmcweb/redfish-core/include/utils/chassis_utils.hpp (revision 3f95a2772fc4fd57459fdfffbee52d8831fc4f35)
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"
67a1dbc48SGeorge Liu #include "dbus_utility.hpp"
73ccb3adbSEd Tanous #include "error_messages.hpp"
8d7857201SEd Tanous #include "logging.hpp"
9d7857201SEd Tanous 
10d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
110d7702c0SZhenwei Chen 
127a1dbc48SGeorge Liu #include <array>
13d7857201SEd Tanous #include <memory>
14d7857201SEd Tanous #include <optional>
15d7857201SEd Tanous #include <string>
167a1dbc48SGeorge Liu #include <string_view>
177a1dbc48SGeorge Liu 
180d7702c0SZhenwei Chen namespace redfish
190d7702c0SZhenwei Chen {
200d7702c0SZhenwei Chen 
21*3f95a277SMyung Bae static constexpr std::array<std::string_view, 2> chassisInterfaces = {
22*3f95a277SMyung Bae     "xyz.openbmc_project.Inventory.Item.Board",
23*3f95a277SMyung Bae     "xyz.openbmc_project.Inventory.Item.Chassis"};
24*3f95a277SMyung Bae 
250d7702c0SZhenwei Chen namespace chassis_utils
260d7702c0SZhenwei Chen {
270d7702c0SZhenwei Chen /**
280d7702c0SZhenwei Chen  * @brief Retrieves valid chassis path
290d7702c0SZhenwei Chen  * @param asyncResp   Pointer to object holding response data
300d7702c0SZhenwei Chen  * @param callback  Callback for next step to get valid chassis path
310d7702c0SZhenwei Chen  */
320d7702c0SZhenwei Chen template <typename Callback>
getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,Callback && callback)330d7702c0SZhenwei Chen void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
340d7702c0SZhenwei Chen                          const std::string& chassisId, Callback&& callback)
350d7702c0SZhenwei Chen {
3662598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId enter");
370d7702c0SZhenwei Chen 
387a1dbc48SGeorge Liu     // Get the Chassis Collection
397a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
40*3f95a277SMyung Bae         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
418cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
427a1dbc48SGeorge Liu          chassisId](const boost::system::error_code& ec,
430d7702c0SZhenwei Chen                     const dbus::utility::MapperGetSubTreePathsResponse&
440d7702c0SZhenwei Chen                         chassisPaths) mutable {
4562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
460d7702c0SZhenwei Chen             if (ec)
470d7702c0SZhenwei Chen             {
48bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
49bd79bce8SPatrick Williams                     "getValidChassisPath respHandler DBUS error: {}", ec);
500d7702c0SZhenwei Chen                 messages::internalError(asyncResp->res);
510d7702c0SZhenwei Chen                 return;
520d7702c0SZhenwei Chen             }
530d7702c0SZhenwei Chen 
540d7702c0SZhenwei Chen             std::optional<std::string> chassisPath;
550d7702c0SZhenwei Chen             for (const std::string& chassis : chassisPaths)
560d7702c0SZhenwei Chen             {
570d7702c0SZhenwei Chen                 sdbusplus::message::object_path path(chassis);
58f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
590d7702c0SZhenwei Chen                 if (chassisName.empty())
600d7702c0SZhenwei Chen                 {
6162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
620d7702c0SZhenwei Chen                     continue;
630d7702c0SZhenwei Chen                 }
640d7702c0SZhenwei Chen                 if (chassisName == chassisId)
650d7702c0SZhenwei Chen                 {
660d7702c0SZhenwei Chen                     chassisPath = chassis;
670d7702c0SZhenwei Chen                     break;
680d7702c0SZhenwei Chen                 }
690d7702c0SZhenwei Chen             }
700d7702c0SZhenwei Chen             callback(chassisPath);
717a1dbc48SGeorge Liu         });
7262598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId exit");
730d7702c0SZhenwei Chen }
740d7702c0SZhenwei Chen 
750d7702c0SZhenwei Chen } // namespace chassis_utils
760d7702c0SZhenwei Chen } // namespace redfish
77