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::vector<sdbusplus::message::object_path>,
53 std::vector<std::tuple<std::string, std::string>>,
54 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
55 std::vector<std::tuple<uint32_t, size_t>>,
56 std::vector<std::tuple<
57 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
58 std::string, std::string, uint64_t>>,
59 std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
60 std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
61 std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
62 >;
63
64 // clang-format on
65 using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
66 using DBusInterfacesMap =
67 std::vector<std::pair<std::string, DBusPropertiesMap>>;
68 using ManagedObjectType =
69 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
70
71 // Map of service name to list of interfaces
72 using MapperServiceMap =
73 std::vector<std::pair<std::string, std::vector<std::string>>>;
74
75 // Map of object paths to MapperServiceMaps
76 using MapperGetSubTreeResponse =
77 std::vector<std::pair<std::string, MapperServiceMap>>;
78
79 using MapperGetObject =
80 std::vector<std::pair<std::string, std::vector<std::string>>>;
81
82 using MapperGetAncestorsResponse = std::vector<
83 std::pair<std::string,
84 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
85
86 using MapperGetSubTreePathsResponse = std::vector<std::string>;
87
88 using MapperEndPoints = std::vector<std::string>;
89
90 void escapePathForDbus(std::string& path);
91
92 void logError(const boost::system::error_code& ec);
93
94 void getAllProperties(const std::string& service, const std::string& objectPath,
95 const std::string& interface,
96 std::function<void(const boost::system::error_code&,
97 const DBusPropertiesMap&)>&& callback);
98
99 template <typename MessageHandler, typename... InputArgs>
100 // 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)101 void async_method_call(MessageHandler&& handler, const std::string& service,
102 const std::string& objpath, const std::string& interf,
103 const std::string& method, const InputArgs&... a)
104 {
105 crow::connections::systemBus->async_method_call(
106 std::forward<MessageHandler>(handler), service, objpath, interf, method,
107 a...);
108 }
109
110 template <typename MessageHandler, typename... InputArgs>
111 // 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)112 void async_method_call(const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
113 MessageHandler&& handler, const std::string& service,
114 const std::string& objpath, const std::string& interf,
115 const std::string& method, const InputArgs&... a)
116 {
117 crow::connections::systemBus->async_method_call(
118 std::forward<MessageHandler>(handler), service, objpath, interf, method,
119 a...);
120 }
121
122 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)123 void getProperty(const std::string& service, const std::string& objectPath,
124 const std::string& interface, const std::string& propertyName,
125 std::function<void(const boost::system::error_code&,
126 const PropertyType&)>&& callback)
127 {
128 sdbusplus::asio::getProperty<PropertyType>(
129 *crow::connections::systemBus, service, objectPath, interface,
130 propertyName, std::move(callback));
131 }
132
133 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)134 void getProperty(sdbusplus::asio::connection& /*conn*/,
135 const std::string& service, const std::string& objectPath,
136 const std::string& interface, const std::string& propertyName,
137 std::function<void(const boost::system::error_code&,
138 const PropertyType&)>&& callback)
139 {
140 getProperty(service, objectPath, interface, propertyName,
141 std::move(callback));
142 }
143
144 void getAllProperties(sdbusplus::asio::connection& /*conn*/,
145 const std::string& service, const std::string& objectPath,
146 const std::string& interface,
147 std::function<void(const boost::system::error_code&,
148 const DBusPropertiesMap&)>&& callback);
149
150 void checkDbusPathExists(const std::string& path,
151 std::function<void(bool)>&& callback);
152
153 void getSubTree(
154 const std::string& path, int32_t depth,
155 std::span<const std::string_view> interfaces,
156 std::function<void(const boost::system::error_code&,
157 const MapperGetSubTreeResponse&)>&& callback);
158
159 void getSubTreePaths(
160 const std::string& path, int32_t depth,
161 std::span<const std::string_view> interfaces,
162 std::function<void(const boost::system::error_code&,
163 const MapperGetSubTreePathsResponse&)>&& callback);
164
165 void getAssociatedSubTree(
166 const sdbusplus::message::object_path& associatedPath,
167 const sdbusplus::message::object_path& path, int32_t depth,
168 std::span<const std::string_view> interfaces,
169 std::function<void(const boost::system::error_code&,
170 const MapperGetSubTreeResponse&)>&& callback);
171
172 void getAssociatedSubTreePaths(
173 const sdbusplus::message::object_path& associatedPath,
174 const sdbusplus::message::object_path& path, int32_t depth,
175 std::span<const std::string_view> interfaces,
176 std::function<void(const boost::system::error_code&,
177 const MapperGetSubTreePathsResponse&)>&& callback);
178
179 void getAssociatedSubTreeById(
180 const std::string& id, const std::string& path,
181 std::span<const std::string_view> subtreeInterfaces,
182 std::string_view association,
183 std::span<const std::string_view> endpointInterfaces,
184 std::function<void(const boost::system::error_code&,
185 const MapperGetSubTreeResponse&)>&& callback);
186
187 void getAssociatedSubTreePathsById(
188 const std::string& id, const std::string& path,
189 std::span<const std::string_view> subtreeInterfaces,
190 std::string_view association,
191 std::span<const std::string_view> endpointInterfaces,
192 std::function<void(const boost::system::error_code&,
193 const MapperGetSubTreePathsResponse&)>&& callback);
194
195 void getDbusObject(const std::string& path,
196 std::span<const std::string_view> interfaces,
197 std::function<void(const boost::system::error_code&,
198 const MapperGetObject&)>&& callback);
199
200 void getAssociationEndPoints(
201 const std::string& path,
202 std::function<void(const boost::system::error_code&,
203 const MapperEndPoints&)>&& callback);
204
205 void getManagedObjects(
206 const std::string& service, const sdbusplus::message::object_path& path,
207 std::function<void(const boost::system::error_code&,
208 const ManagedObjectType&)>&& callback);
209 } // namespace utility
210 } // namespace dbus
211