xref: /openbmc/bmcweb/features/redfish/include/utils/fan_utils.hpp (revision 57d41fed30c140c308155076c0642cca7c418201)
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 <asm-generic/errno.h>
11 
12 #include <boost/system/error_code.hpp>
13 #include <sdbusplus/message/native_types.hpp>
14 
15 #include <array>
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 
21 namespace redfish
22 {
23 constexpr std::array<std::string_view, 1> fanInterface = {
24     "xyz.openbmc_project.Inventory.Item.Fan"};
25 
26 namespace fan_utils
27 {
28 inline void getFanPaths(
29     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30     const std::string& validChassisPath,
31     const std::function<void(const dbus::utility::MapperGetSubTreePathsResponse&
32                                  fanPaths)>& callback)
33 {
34     sdbusplus::message::object_path endpointPath{validChassisPath};
35     endpointPath /= "cooled_by";
36 
37     dbus::utility::getAssociatedSubTreePaths(
38         endpointPath,
39         sdbusplus::message::object_path("/xyz/openbmc_project/inventory"), 0,
40         fanInterface,
41         [asyncResp, callback](
42             const boost::system::error_code& ec,
43             const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) {
44             if (ec)
45             {
46                 if (ec.value() != EBADR)
47                 {
48                     BMCWEB_LOG_ERROR(
49                         "DBUS response error for getAssociatedSubTreePaths {}",
50                         ec.value());
51                     messages::internalError(asyncResp->res);
52                 }
53                 return;
54             }
55             callback(subtreePaths);
56         });
57 }
58 
59 } // namespace fan_utils
60 } // namespace redfish
61