blob: be7efc2bfb973378a56fa65d069650c4c277199c [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001/*
Ed Tanous6be832e2024-09-10 11:44:48 -07002Copyright (c) 2018 Intel Corporation
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
James Feist5b4aa862018-08-16 14:07:01 -070015 */
16#pragma once
17
Ed Tanous95c63072024-03-26 13:19:52 -070018#include "boost_formatters.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +000019#include "dbus_singleton.hpp"
Ed Tanous863c1c22022-02-21 21:33:06 -080020#include "logging.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +000021
Ed Tanous478b7ad2024-07-15 19:11:54 -070022#include <boost/system/error_code.hpp>
George Liua4eb7612023-02-11 15:49:46 +080023#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000024#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070025
Nan Zhou80d37e72022-06-21 17:46:14 +000026#include <array>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000027#include <cstddef>
28#include <cstdint>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053029#include <filesystem>
George Liu21384832022-11-09 11:21:15 +080030#include <functional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031#include <regex>
George Liu21384832022-11-09 11:21:15 +080032#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000033#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000034#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080035#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000036#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000037#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080038#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000039#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040
James Feist5b4aa862018-08-16 14:07:01 -070041namespace dbus
42{
43
44namespace utility
45{
46
Ed Tanousd1a64812021-12-13 12:14:05 -080047// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080048using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080049 std::vector<std::tuple<std::string, std::string, std::string>>,
50 std::vector<std::string>,
51 std::vector<double>,
52 std::string,
53 int64_t,
54 uint64_t,
55 double,
56 int32_t,
57 uint32_t,
58 int16_t,
59 uint16_t,
60 uint8_t,
61 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080062 std::vector<uint32_t>,
63 std::vector<uint16_t>,
64 sdbusplus::message::object_path,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000065 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
Szymon Dompkef19ab442022-03-07 14:28:06 +010066 std::vector<sdbusplus::message::object_path>,
Ed Tanousd1a64812021-12-13 12:14:05 -080067 std::vector<std::tuple<std::string, std::string>>,
68 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
69 std::vector<std::tuple<uint32_t, size_t>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000070 std::vector<std::tuple<
71 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
Ed Tanouse3648032024-10-16 18:06:39 -070072 std::string, std::string, uint64_t>>,
73 std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
Ed Tanous58c71482024-10-19 14:35:07 -070074 std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
75 std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
Ed Tanousd1a64812021-12-13 12:14:05 -080076 >;
James Feist5b4aa862018-08-16 14:07:01 -070077
Ed Tanousd1a64812021-12-13 12:14:05 -080078// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080079using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
Michael Shen80f79a42023-08-24 13:41:53 +000080using DBusInterfacesMap =
81 std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010082using ManagedObjectType =
Michael Shen80f79a42023-08-24 13:41:53 +000083 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070084
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060085// Map of service name to list of interfaces
86using MapperServiceMap =
87 std::vector<std::pair<std::string, std::vector<std::string>>>;
88
89// Map of object paths to MapperServiceMaps
90using MapperGetSubTreeResponse =
91 std::vector<std::pair<std::string, MapperServiceMap>>;
92
Ed Tanousb9d36b42022-02-26 21:42:46 -080093using MapperGetObject =
94 std::vector<std::pair<std::string, std::vector<std::string>>>;
95
96using MapperGetAncestorsResponse = std::vector<
97 std::pair<std::string,
98 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
99
100using MapperGetSubTreePathsResponse = std::vector<std::string>;
101
George Liua4eb7612023-02-11 15:49:46 +0800102using MapperEndPoints = std::vector<std::string>;
103
James Feist5b4aa862018-08-16 14:07:01 -0700104inline void escapePathForDbus(std::string& path)
105{
Ed Tanous4b242742023-05-11 09:51:51 -0700106 const static std::regex reg("[^A-Za-z0-9_/]");
James Feist5b4aa862018-08-16 14:07:01 -0700107 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
108}
109
Ed Tanous863c1c22022-02-21 21:33:06 -0800110inline void logError(const boost::system::error_code& ec)
111{
112 if (ec)
113 {
Ed Tanous62598e32023-07-17 17:06:25 -0700114 BMCWEB_LOG_ERROR("DBus error: {}, cannot call method", ec);
Ed Tanous863c1c22022-02-21 21:33:06 -0800115 }
116}
117
James Feist5b4aa862018-08-16 14:07:01 -0700118// gets the string N strings deep into a path
119// i.e. /0th/1st/2nd/3rd
120inline bool getNthStringFromPath(const std::string& path, int index,
121 std::string& result)
122{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530123 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700124 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530125 return false;
126 }
127
128 std::filesystem::path p1(path);
129 int count = -1;
Patrick Williams89492a12023-05-10 07:51:34 -0500130 for (const auto& element : p1)
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530131 {
132 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700133 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530134 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700135 if (count == index)
136 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530137 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700138 break;
139 }
140 }
141 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800142 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700143}
144
Ed Tanousdeae6a72024-11-11 21:58:57 -0800145inline void
146 getAllProperties(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 sdbusplus::asio::getAllProperties(*crow::connections::systemBus, service,
152 objectPath, interface,
153 std::move(callback));
154}
155
156template <typename PropertyType>
157inline void getProperty(
158 const std::string& service, const std::string& objectPath,
159 const std::string& interface, const std::string& propertyName,
160 std::function<void(const boost::system::error_code&, const PropertyType&)>&&
161 callback)
162{
163 sdbusplus::asio::getProperty<PropertyType>(
164 *crow::connections::systemBus, service, objectPath, interface,
165 propertyName, std::move(callback));
166}
167
168template <typename PropertyType>
169inline void getProperty(
170 sdbusplus::asio::connection& /*conn*/, const std::string& service,
171 const std::string& objectPath, const std::string& interface,
172 const std::string& propertyName,
173 std::function<void(const boost::system::error_code&, const PropertyType&)>&&
174 callback)
175{
176 getProperty(service, objectPath, interface, propertyName,
177 std::move(callback));
178}
179
180inline void getAllProperties(
181 sdbusplus::asio::connection& /*conn*/, const std::string& service,
182 const std::string& objectPath, const std::string& interface,
183 std::function<void(const boost::system::error_code&,
184 const DBusPropertiesMap&)>&& callback)
185{
186 getAllProperties(service, objectPath, interface, std::move(callback));
187}
188
Ed Tanousd35c2412024-11-18 04:45:30 -0800189inline void checkDbusPathExists(const std::string& path,
190 std::function<void(bool)>&& callback)
Ratan Gupta22c33712019-05-03 21:50:28 +0530191{
Ratan Gupta22c33712019-05-03 21:50:28 +0530192 crow::connections::systemBus->async_method_call(
Ed Tanousd35c2412024-11-18 04:45:30 -0800193 [callback = std::move(callback)](const boost::system::error_code& ec,
194 const MapperGetObject& objectNames) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400195 callback(!ec && !objectNames.empty());
196 },
Ratan Gupta22c33712019-05-03 21:50:28 +0530197 "xyz.openbmc_project.ObjectMapper",
198 "/xyz/openbmc_project/object_mapper",
199 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
200 std::array<std::string, 0>());
201}
202
George Liu21384832022-11-09 11:21:15 +0800203inline void
George Liue99073f2022-12-09 11:06:16 +0800204 getSubTree(const std::string& path, int32_t depth,
205 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800206 std::function<void(const boost::system::error_code&,
207 const MapperGetSubTreeResponse&)>&& callback)
208{
209 crow::connections::systemBus->async_method_call(
210 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800211 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800212 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
213 "xyz.openbmc_project.ObjectMapper",
214 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800215 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
216 interfaces);
George Liu21384832022-11-09 11:21:15 +0800217}
218
219inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800220 const std::string& path, int32_t depth,
221 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800222 std::function<void(const boost::system::error_code&,
223 const MapperGetSubTreePathsResponse&)>&& callback)
224{
225 crow::connections::systemBus->async_method_call(
226 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800227 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800228 const MapperGetSubTreePathsResponse& subtreePaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400229 callback(ec, subtreePaths);
230 },
George Liu21384832022-11-09 11:21:15 +0800231 "xyz.openbmc_project.ObjectMapper",
232 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800233 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800234 interfaces);
235}
236
Willy Tu8d018362023-04-14 09:14:16 -0700237inline void getAssociatedSubTree(
238 const sdbusplus::message::object_path& associatedPath,
239 const sdbusplus::message::object_path& path, int32_t depth,
240 std::span<const std::string_view> interfaces,
241 std::function<void(const boost::system::error_code&,
242 const MapperGetSubTreeResponse&)>&& callback)
243{
244 crow::connections::systemBus->async_method_call(
245 [callback{std::move(callback)}](
246 const boost::system::error_code& ec,
247 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
248 "xyz.openbmc_project.ObjectMapper",
249 "/xyz/openbmc_project/object_mapper",
250 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTree",
251 associatedPath, path, depth, interfaces);
252}
253
254inline void getAssociatedSubTreePaths(
255 const sdbusplus::message::object_path& associatedPath,
256 const sdbusplus::message::object_path& path, int32_t depth,
257 std::span<const std::string_view> interfaces,
258 std::function<void(const boost::system::error_code&,
259 const MapperGetSubTreePathsResponse&)>&& callback)
260{
261 crow::connections::systemBus->async_method_call(
262 [callback{std::move(callback)}](
263 const boost::system::error_code& ec,
264 const MapperGetSubTreePathsResponse& subtreePaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400265 callback(ec, subtreePaths);
266 },
Willy Tu8d018362023-04-14 09:14:16 -0700267 "xyz.openbmc_project.ObjectMapper",
268 "/xyz/openbmc_project/object_mapper",
269 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePaths",
270 associatedPath, path, depth, interfaces);
271}
272
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500273inline void getAssociatedSubTreeById(
274 const std::string& id, const std::string& path,
275 std::span<const std::string_view> subtreeInterfaces,
276 std::string_view association,
277 std::span<const std::string_view> endpointInterfaces,
278 std::function<void(const boost::system::error_code&,
279 const MapperGetSubTreeResponse&)>&& callback)
280{
281 crow::connections::systemBus->async_method_call(
282 [callback{std::move(callback)}](
283 const boost::system::error_code& ec,
284 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
285 "xyz.openbmc_project.ObjectMapper",
286 "/xyz/openbmc_project/object_mapper",
287 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreeById", id,
288 path, subtreeInterfaces, association, endpointInterfaces);
289}
290
291inline void getAssociatedSubTreePathsById(
292 const std::string& id, const std::string& path,
293 std::span<const std::string_view> subtreeInterfaces,
294 std::string_view association,
295 std::span<const std::string_view> endpointInterfaces,
296 std::function<void(const boost::system::error_code&,
297 const MapperGetSubTreePathsResponse&)>&& callback)
298{
299 crow::connections::systemBus->async_method_call(
300 [callback{std::move(callback)}](
301 const boost::system::error_code& ec,
302 const MapperGetSubTreePathsResponse& subtreePaths) {
303 callback(ec, subtreePaths);
304 },
305 "xyz.openbmc_project.ObjectMapper",
306 "/xyz/openbmc_project/object_mapper",
307 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePathsById", id,
308 path, subtreeInterfaces, association, endpointInterfaces);
309}
310
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400311inline void getDbusObject(
312 const std::string& path, std::span<const std::string_view> interfaces,
313 std::function<void(const boost::system::error_code&,
314 const MapperGetObject&)>&& callback)
George Liu2b731192023-01-11 16:27:13 +0800315{
316 crow::connections::systemBus->async_method_call(
317 [callback{std::move(callback)}](const boost::system::error_code& ec,
318 const MapperGetObject& object) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400319 callback(ec, object);
320 },
George Liu2b731192023-01-11 16:27:13 +0800321 "xyz.openbmc_project.ObjectMapper",
322 "/xyz/openbmc_project/object_mapper",
323 "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
324}
325
George Liua4eb7612023-02-11 15:49:46 +0800326inline void getAssociationEndPoints(
327 const std::string& path,
328 std::function<void(const boost::system::error_code&,
329 const MapperEndPoints&)>&& callback)
330{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800331 getProperty<MapperEndPoints>("xyz.openbmc_project.ObjectMapper", path,
332 "xyz.openbmc_project.Association", "endpoints",
333 std::move(callback));
George Liua4eb7612023-02-11 15:49:46 +0800334}
335
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400336inline void getManagedObjects(
337 const std::string& service, const sdbusplus::message::object_path& path,
338 std::function<void(const boost::system::error_code&,
339 const ManagedObjectType&)>&& callback)
George Liuf5892d02023-03-01 10:37:08 +0800340{
341 crow::connections::systemBus->async_method_call(
342 [callback{std::move(callback)}](const boost::system::error_code& ec,
343 const ManagedObjectType& objects) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400344 callback(ec, objects);
345 },
George Liuf5892d02023-03-01 10:37:08 +0800346 service, path, "org.freedesktop.DBus.ObjectManager",
347 "GetManagedObjects");
348}
349
James Feist5b4aa862018-08-16 14:07:01 -0700350} // namespace utility
351} // namespace dbus