xref: /openbmc/bmcweb/include/dbus_utility.hpp (revision 1aa94df4cb3b7ab864a4c5fb009538c259c2801e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
4 #pragma once
5 
6 #include "async_resp.hpp"
7 #include "boost_formatters.hpp"
8 #include "dbus_singleton.hpp"
9 
10 #include <boost/system/error_code.hpp>
11 #include <sdbusplus/asio/connection.hpp>
12 #include <sdbusplus/asio/property.hpp>
13 #include <sdbusplus/message/native_types.hpp>
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <functional>
18 #include <memory>
19 #include <span>
20 #include <string>
21 #include <string_view>
22 #include <tuple>
23 #include <utility>
24 #include <variant>
25 #include <vector>
26 
27 namespace dbus
28 {
29 
30 namespace utility
31 {
32 
33 // clang-format off
34 using DbusVariantType = std::variant<
35     std::vector<std::tuple<std::string, std::string, std::string>>,
36     std::vector<std::string>,
37     std::vector<double>,
38     std::string,
39     int64_t,
40     uint64_t,
41     double,
42     int32_t,
43     uint32_t,
44     int16_t,
45     uint16_t,
46     uint8_t,
47     bool,
48     std::vector<uint32_t>,
49     std::vector<uint16_t>,
50     sdbusplus::message::object_path,
51     std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
52     std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
53     std::vector<sdbusplus::message::object_path>,
54     std::vector<std::tuple<std::string, std::string>>,
55     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
56     std::vector<std::tuple<uint32_t, size_t>>,
57     std::vector<std::tuple<
58       std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
59       std::string, std::string, uint64_t>>,
60     std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
61     std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
62     std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
63  >;
64 
65 // clang-format on
66 using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
67 using DBusInterfacesMap =
68     std::vector<std::pair<std::string, DBusPropertiesMap>>;
69 using ManagedObjectType =
70     std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
71 
72 // Map of service name to list of interfaces
73 using MapperServiceMap =
74     std::vector<std::pair<std::string, std::vector<std::string>>>;
75 
76 // Map of object paths to MapperServiceMaps
77 using MapperGetSubTreeResponse =
78     std::vector<std::pair<std::string, MapperServiceMap>>;
79 
80 using MapperGetObject =
81     std::vector<std::pair<std::string, std::vector<std::string>>>;
82 
83 using MapperGetAncestorsResponse = std::vector<
84     std::pair<std::string,
85               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
86 
87 using MapperGetSubTreePathsResponse = std::vector<std::string>;
88 
89 using MapperEndPoints = std::vector<std::string>;
90 
91 void escapePathForDbus(std::string& path);
92 
93 void logError(const boost::system::error_code& ec);
94 
95 void getAllProperties(const std::string& service, const std::string& objectPath,
96                       const std::string& interface,
97                       std::function<void(const boost::system::error_code&,
98                                          const DBusPropertiesMap&)>&& callback);
99 
100 template <typename MessageHandler, typename... InputArgs>
101 // NOLINTNEXTLINE(readability-identifier-naming)
async_method_call(MessageHandler && handler,const std::string & service,const std::string & objpath,const std::string & interf,const std::string & method,const InputArgs &...a)102 void async_method_call(MessageHandler&& handler, const std::string& service,
103                        const std::string& objpath, const std::string& interf,
104                        const std::string& method, const InputArgs&... a)
105 {
106     crow::connections::systemBus->async_method_call(
107         std::forward<MessageHandler>(handler), service, objpath, interf, method,
108         a...);
109 }
110 
111 template <typename MessageHandler, typename... InputArgs>
112 // NOLINTNEXTLINE(readability-identifier-naming)
async_method_call(const std::shared_ptr<bmcweb::AsyncResp> &,MessageHandler && handler,const std::string & service,const std::string & objpath,const std::string & interf,const std::string & method,const InputArgs &...a)113 void async_method_call(const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
114                        MessageHandler&& handler, const std::string& service,
115                        const std::string& objpath, const std::string& interf,
116                        const std::string& method, const InputArgs&... a)
117 {
118     crow::connections::systemBus->async_method_call(
119         std::forward<MessageHandler>(handler), service, objpath, interf, method,
120         a...);
121 }
122 
123 template <typename PropertyType>
getProperty(const std::string & service,const std::string & objectPath,const std::string & interface,const std::string & propertyName,std::function<void (const boost::system::error_code &,const PropertyType &)> && callback)124 void getProperty(const std::string& service, const std::string& objectPath,
125                  const std::string& interface, const std::string& propertyName,
126                  std::function<void(const boost::system::error_code&,
127                                     const PropertyType&)>&& callback)
128 {
129     sdbusplus::asio::getProperty<PropertyType>(
130         *crow::connections::systemBus, service, objectPath, interface,
131         propertyName, std::move(callback));
132 }
133 
134 template <typename PropertyType>
getProperty(sdbusplus::asio::connection &,const std::string & service,const std::string & objectPath,const std::string & interface,const std::string & propertyName,std::function<void (const boost::system::error_code &,const PropertyType &)> && callback)135 void getProperty(sdbusplus::asio::connection& /*conn*/,
136                  const std::string& service, const std::string& objectPath,
137                  const std::string& interface, const std::string& propertyName,
138                  std::function<void(const boost::system::error_code&,
139                                     const PropertyType&)>&& callback)
140 {
141     getProperty(service, objectPath, interface, propertyName,
142                 std::move(callback));
143 }
144 
145 void getAllProperties(sdbusplus::asio::connection& /*conn*/,
146                       const std::string& service, const std::string& objectPath,
147                       const std::string& interface,
148                       std::function<void(const boost::system::error_code&,
149                                          const DBusPropertiesMap&)>&& callback);
150 
151 void checkDbusPathExists(const std::string& path,
152                          std::function<void(bool)>&& callback);
153 
154 void getSubTree(
155     const std::string& path, int32_t depth,
156     std::span<const std::string_view> interfaces,
157     std::function<void(const boost::system::error_code&,
158                        const MapperGetSubTreeResponse&)>&& callback);
159 
160 void getSubTreePaths(
161     const std::string& path, int32_t depth,
162     std::span<const std::string_view> interfaces,
163     std::function<void(const boost::system::error_code&,
164                        const MapperGetSubTreePathsResponse&)>&& callback);
165 
166 void getAssociatedSubTree(
167     const sdbusplus::message::object_path& associatedPath,
168     const sdbusplus::message::object_path& path, int32_t depth,
169     std::span<const std::string_view> interfaces,
170     std::function<void(const boost::system::error_code&,
171                        const MapperGetSubTreeResponse&)>&& callback);
172 
173 void getAssociatedSubTreePaths(
174     const sdbusplus::message::object_path& associatedPath,
175     const sdbusplus::message::object_path& path, int32_t depth,
176     std::span<const std::string_view> interfaces,
177     std::function<void(const boost::system::error_code&,
178                        const MapperGetSubTreePathsResponse&)>&& callback);
179 
180 void getAssociatedSubTreeById(
181     const std::string& id, const std::string& path,
182     std::span<const std::string_view> subtreeInterfaces,
183     std::string_view association,
184     std::span<const std::string_view> endpointInterfaces,
185     std::function<void(const boost::system::error_code&,
186                        const MapperGetSubTreeResponse&)>&& callback);
187 
188 void getAssociatedSubTreePathsById(
189     const std::string& id, const std::string& path,
190     std::span<const std::string_view> subtreeInterfaces,
191     std::string_view association,
192     std::span<const std::string_view> endpointInterfaces,
193     std::function<void(const boost::system::error_code&,
194                        const MapperGetSubTreePathsResponse&)>&& callback);
195 
196 void getDbusObject(const std::string& path,
197                    std::span<const std::string_view> interfaces,
198                    std::function<void(const boost::system::error_code&,
199                                       const MapperGetObject&)>&& callback);
200 
201 void getAssociationEndPoints(
202     const std::string& path,
203     std::function<void(const boost::system::error_code&,
204                        const MapperEndPoints&)>&& callback);
205 
206 void getManagedObjects(
207     const std::string& service, const sdbusplus::message::object_path& path,
208     std::function<void(const boost::system::error_code&,
209                        const ManagedObjectType&)>&& callback);
210 } // namespace utility
211 } // namespace dbus
212