blob: d1f4a8415d551607a746d6d9a676f235fd342d84 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
James Feist5b4aa862018-08-16 14:07:01 -07004#pragma once
5
Ed Tanous95c63072024-03-26 13:19:52 -07006#include "boost_formatters.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +00007#include "dbus_singleton.hpp"
8
Ed Tanous478b7ad2024-07-15 19:11:54 -07009#include <boost/system/error_code.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080010#include <sdbusplus/asio/connection.hpp>
George Liua4eb7612023-02-11 15:49:46 +080011#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000012#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070013
Nan Zhoud5c80ad2022-07-11 01:16:31 +000014#include <cstddef>
15#include <cstdint>
George Liu21384832022-11-09 11:21:15 +080016#include <functional>
George Liu21384832022-11-09 11:21:15 +080017#include <span>
Nan Zhou80d37e72022-06-21 17:46:14 +000018#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080019#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000020#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000021#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080022#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000023#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024
James Feist5b4aa862018-08-16 14:07:01 -070025namespace dbus
26{
27
28namespace utility
29{
30
Ed Tanousd1a64812021-12-13 12:14:05 -080031// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080032using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080033 std::vector<std::tuple<std::string, std::string, std::string>>,
34 std::vector<std::string>,
35 std::vector<double>,
36 std::string,
37 int64_t,
38 uint64_t,
39 double,
40 int32_t,
41 uint32_t,
42 int16_t,
43 uint16_t,
44 uint8_t,
45 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080046 std::vector<uint32_t>,
47 std::vector<uint16_t>,
48 sdbusplus::message::object_path,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000049 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
Szymon Dompkef19ab442022-03-07 14:28:06 +010050 std::vector<sdbusplus::message::object_path>,
Ed Tanousd1a64812021-12-13 12:14:05 -080051 std::vector<std::tuple<std::string, std::string>>,
52 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
53 std::vector<std::tuple<uint32_t, size_t>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000054 std::vector<std::tuple<
55 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
Ed Tanouse3648032024-10-16 18:06:39 -070056 std::string, std::string, uint64_t>>,
57 std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
Ed Tanous58c71482024-10-19 14:35:07 -070058 std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
59 std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
Ed Tanousd1a64812021-12-13 12:14:05 -080060 >;
James Feist5b4aa862018-08-16 14:07:01 -070061
Ed Tanousd1a64812021-12-13 12:14:05 -080062// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080063using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
Michael Shen80f79a42023-08-24 13:41:53 +000064using DBusInterfacesMap =
65 std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010066using ManagedObjectType =
Michael Shen80f79a42023-08-24 13:41:53 +000067 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070068
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060069// Map of service name to list of interfaces
70using MapperServiceMap =
71 std::vector<std::pair<std::string, std::vector<std::string>>>;
72
73// Map of object paths to MapperServiceMaps
74using MapperGetSubTreeResponse =
75 std::vector<std::pair<std::string, MapperServiceMap>>;
76
Ed Tanousb9d36b42022-02-26 21:42:46 -080077using MapperGetObject =
78 std::vector<std::pair<std::string, std::vector<std::string>>>;
79
80using MapperGetAncestorsResponse = std::vector<
81 std::pair<std::string,
82 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
83
84using MapperGetSubTreePathsResponse = std::vector<std::string>;
85
George Liua4eb7612023-02-11 15:49:46 +080086using MapperEndPoints = std::vector<std::string>;
87
Ed Tanousbb1c7d32025-02-09 09:39:19 -080088void escapePathForDbus(std::string& path);
James Feist5b4aa862018-08-16 14:07:01 -070089
Ed Tanousbb1c7d32025-02-09 09:39:19 -080090void logError(const boost::system::error_code& ec);
Ed Tanous863c1c22022-02-21 21:33:06 -080091
James Feist5b4aa862018-08-16 14:07:01 -070092// gets the string N strings deep into a path
93// i.e. /0th/1st/2nd/3rd
Ed Tanousbb1c7d32025-02-09 09:39:19 -080094bool getNthStringFromPath(const std::string& path, int index,
95 std::string& result);
Manojkiran Eda17a897d2020-09-12 15:31:58 +053096
Ed Tanousbb1c7d32025-02-09 09:39:19 -080097void getAllProperties(const std::string& service, const std::string& objectPath,
98 const std::string& interface,
99 std::function<void(const boost::system::error_code&,
100 const DBusPropertiesMap&)>&& callback);
Ed Tanousdeae6a72024-11-11 21:58:57 -0800101
102template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800103void getProperty(const std::string& service, const std::string& objectPath,
104 const std::string& interface, const std::string& propertyName,
105 std::function<void(const boost::system::error_code&,
106 const PropertyType&)>&& callback)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800107{
108 sdbusplus::asio::getProperty<PropertyType>(
109 *crow::connections::systemBus, service, objectPath, interface,
110 propertyName, std::move(callback));
111}
112
113template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800114void getProperty(sdbusplus::asio::connection& /*conn*/,
115 const std::string& service, const std::string& objectPath,
116 const std::string& interface, const std::string& propertyName,
117 std::function<void(const boost::system::error_code&,
118 const PropertyType&)>&& callback)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800119{
120 getProperty(service, objectPath, interface, propertyName,
121 std::move(callback));
122}
123
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800124void getAllProperties(sdbusplus::asio::connection& /*conn*/,
125 const std::string& service, const std::string& objectPath,
126 const std::string& interface,
127 std::function<void(const boost::system::error_code&,
128 const DBusPropertiesMap&)>&& callback);
Ed Tanousdeae6a72024-11-11 21:58:57 -0800129
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800130void checkDbusPathExists(const std::string& path,
131 std::function<void(bool)>&& callback);
Ratan Gupta22c33712019-05-03 21:50:28 +0530132
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800133void getSubTree(
Patrick Williams504af5a2025-02-03 14:29:03 -0500134 const std::string& path, int32_t depth,
135 std::span<const std::string_view> interfaces,
136 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800137 const MapperGetSubTreeResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800138
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800139void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800140 const std::string& path, int32_t depth,
141 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800142 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800143 const MapperGetSubTreePathsResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800144
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800145void getAssociatedSubTree(
Willy Tu8d018362023-04-14 09:14:16 -0700146 const sdbusplus::message::object_path& associatedPath,
147 const sdbusplus::message::object_path& path, int32_t depth,
148 std::span<const std::string_view> interfaces,
149 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800150 const MapperGetSubTreeResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700151
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800152void getAssociatedSubTreePaths(
Willy Tu8d018362023-04-14 09:14:16 -0700153 const sdbusplus::message::object_path& associatedPath,
154 const sdbusplus::message::object_path& path, int32_t depth,
155 std::span<const std::string_view> interfaces,
156 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800157 const MapperGetSubTreePathsResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700158
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800159void getAssociatedSubTreeById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500160 const std::string& id, const std::string& path,
161 std::span<const std::string_view> subtreeInterfaces,
162 std::string_view association,
163 std::span<const std::string_view> endpointInterfaces,
164 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800165 const MapperGetSubTreeResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500166
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800167void getAssociatedSubTreePathsById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500168 const std::string& id, const std::string& path,
169 std::span<const std::string_view> subtreeInterfaces,
170 std::string_view association,
171 std::span<const std::string_view> endpointInterfaces,
172 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800173 const MapperGetSubTreePathsResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500174
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800175void getDbusObject(const std::string& path,
176 std::span<const std::string_view> interfaces,
177 std::function<void(const boost::system::error_code&,
178 const MapperGetObject&)>&& callback);
George Liu2b731192023-01-11 16:27:13 +0800179
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800180void getAssociationEndPoints(
George Liua4eb7612023-02-11 15:49:46 +0800181 const std::string& path,
182 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800183 const MapperEndPoints&)>&& callback);
George Liua4eb7612023-02-11 15:49:46 +0800184
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800185void getManagedObjects(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400186 const std::string& service, const sdbusplus::message::object_path& path,
187 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800188 const ManagedObjectType&)>&& callback);
James Feist5b4aa862018-08-16 14:07:01 -0700189} // namespace utility
190} // namespace dbus