blob: 4eedcbf79d2a37dedc31090d89d807814014593a [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 Tanous177612a2025-02-14 15:16:09 -08006#include "async_resp.hpp"
Ed Tanous95c63072024-03-26 13:19:52 -07007#include "boost_formatters.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +00008#include "dbus_singleton.hpp"
9
Ed Tanous478b7ad2024-07-15 19:11:54 -070010#include <boost/system/error_code.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080011#include <sdbusplus/asio/connection.hpp>
George Liua4eb7612023-02-11 15:49:46 +080012#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000013#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070014
Nan Zhoud5c80ad2022-07-11 01:16:31 +000015#include <cstddef>
16#include <cstdint>
George Liu21384832022-11-09 11:21:15 +080017#include <functional>
Ed Tanous177612a2025-02-14 15:16:09 -080018#include <memory>
George Liu21384832022-11-09 11:21:15 +080019#include <span>
Nan Zhou80d37e72022-06-21 17:46:14 +000020#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080021#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000022#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000023#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080024#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000025#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
James Feist5b4aa862018-08-16 14:07:01 -070027namespace dbus
28{
29
30namespace utility
31{
32
Ed Tanousd1a64812021-12-13 12:14:05 -080033// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080034using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080035 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,
Ed Tanousd1a64812021-12-13 12:14:05 -080048 std::vector<uint32_t>,
49 std::vector<uint16_t>,
50 sdbusplus::message::object_path,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000051 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
Szymon Dompkef19ab442022-03-07 14:28:06 +010052 std::vector<sdbusplus::message::object_path>,
Ed Tanousd1a64812021-12-13 12:14:05 -080053 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>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000056 std::vector<std::tuple<
57 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
Ed Tanouse3648032024-10-16 18:06:39 -070058 std::string, std::string, uint64_t>>,
59 std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
Ed Tanous58c71482024-10-19 14:35:07 -070060 std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
61 std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
Ed Tanousd1a64812021-12-13 12:14:05 -080062 >;
James Feist5b4aa862018-08-16 14:07:01 -070063
Ed Tanousd1a64812021-12-13 12:14:05 -080064// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080065using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
Michael Shen80f79a42023-08-24 13:41:53 +000066using DBusInterfacesMap =
67 std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010068using ManagedObjectType =
Michael Shen80f79a42023-08-24 13:41:53 +000069 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070070
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060071// Map of service name to list of interfaces
72using MapperServiceMap =
73 std::vector<std::pair<std::string, std::vector<std::string>>>;
74
75// Map of object paths to MapperServiceMaps
76using MapperGetSubTreeResponse =
77 std::vector<std::pair<std::string, MapperServiceMap>>;
78
Ed Tanousb9d36b42022-02-26 21:42:46 -080079using MapperGetObject =
80 std::vector<std::pair<std::string, std::vector<std::string>>>;
81
82using MapperGetAncestorsResponse = std::vector<
83 std::pair<std::string,
84 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
85
86using MapperGetSubTreePathsResponse = std::vector<std::string>;
87
George Liua4eb7612023-02-11 15:49:46 +080088using MapperEndPoints = std::vector<std::string>;
89
Ed Tanousbb1c7d32025-02-09 09:39:19 -080090void escapePathForDbus(std::string& path);
James Feist5b4aa862018-08-16 14:07:01 -070091
Ed Tanousbb1c7d32025-02-09 09:39:19 -080092void logError(const boost::system::error_code& ec);
Ed Tanous863c1c22022-02-21 21:33:06 -080093
James Feist5b4aa862018-08-16 14:07:01 -070094// gets the string N strings deep into a path
95// i.e. /0th/1st/2nd/3rd
Ed Tanousbb1c7d32025-02-09 09:39:19 -080096bool getNthStringFromPath(const std::string& path, int index,
97 std::string& result);
Manojkiran Eda17a897d2020-09-12 15:31:58 +053098
Ed Tanousbb1c7d32025-02-09 09:39:19 -080099void getAllProperties(const std::string& service, const std::string& objectPath,
100 const std::string& interface,
101 std::function<void(const boost::system::error_code&,
102 const DBusPropertiesMap&)>&& callback);
Ed Tanousdeae6a72024-11-11 21:58:57 -0800103
Ed Tanous177612a2025-02-14 15:16:09 -0800104template <typename MessageHandler, typename... InputArgs>
105// NOLINTNEXTLINE(readability-identifier-naming)
106void async_method_call(MessageHandler&& handler, const std::string& service,
107 const std::string& objpath, const std::string& interf,
108 const std::string& method, const InputArgs&... a)
109{
110 crow::connections::systemBus->async_method_call(
111 std::forward<MessageHandler>(handler), service, objpath, interf, method,
112 a...);
113}
114
115template <typename MessageHandler, typename... InputArgs>
116// NOLINTNEXTLINE(readability-identifier-naming)
117void async_method_call(const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
118 MessageHandler&& handler, const std::string& service,
119 const std::string& objpath, const std::string& interf,
120 const std::string& method, const InputArgs&... a)
121{
122 crow::connections::systemBus->async_method_call(
123 std::forward<MessageHandler>(handler), service, objpath, interf, method,
124 a...);
125}
126
Ed Tanousdeae6a72024-11-11 21:58:57 -0800127template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800128void getProperty(const std::string& service, const std::string& objectPath,
129 const std::string& interface, const std::string& propertyName,
130 std::function<void(const boost::system::error_code&,
131 const PropertyType&)>&& callback)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800132{
133 sdbusplus::asio::getProperty<PropertyType>(
134 *crow::connections::systemBus, service, objectPath, interface,
135 propertyName, std::move(callback));
136}
137
138template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800139void getProperty(sdbusplus::asio::connection& /*conn*/,
140 const std::string& service, const std::string& objectPath,
141 const std::string& interface, const std::string& propertyName,
142 std::function<void(const boost::system::error_code&,
143 const PropertyType&)>&& callback)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800144{
145 getProperty(service, objectPath, interface, propertyName,
146 std::move(callback));
147}
148
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800149void getAllProperties(sdbusplus::asio::connection& /*conn*/,
150 const std::string& service, const std::string& objectPath,
151 const std::string& interface,
152 std::function<void(const boost::system::error_code&,
153 const DBusPropertiesMap&)>&& callback);
Ed Tanousdeae6a72024-11-11 21:58:57 -0800154
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800155void checkDbusPathExists(const std::string& path,
156 std::function<void(bool)>&& callback);
Ratan Gupta22c33712019-05-03 21:50:28 +0530157
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800158void getSubTree(
Patrick Williams504af5a2025-02-03 14:29:03 -0500159 const std::string& path, int32_t depth,
160 std::span<const std::string_view> interfaces,
161 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800162 const MapperGetSubTreeResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800163
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800164void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800165 const std::string& path, int32_t depth,
166 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800167 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800168 const MapperGetSubTreePathsResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800169
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800170void getAssociatedSubTree(
Willy Tu8d018362023-04-14 09:14:16 -0700171 const sdbusplus::message::object_path& associatedPath,
172 const sdbusplus::message::object_path& path, int32_t depth,
173 std::span<const std::string_view> interfaces,
174 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800175 const MapperGetSubTreeResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700176
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800177void getAssociatedSubTreePaths(
Willy Tu8d018362023-04-14 09:14:16 -0700178 const sdbusplus::message::object_path& associatedPath,
179 const sdbusplus::message::object_path& path, int32_t depth,
180 std::span<const std::string_view> interfaces,
181 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800182 const MapperGetSubTreePathsResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700183
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800184void getAssociatedSubTreeById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500185 const std::string& id, const std::string& path,
186 std::span<const std::string_view> subtreeInterfaces,
187 std::string_view association,
188 std::span<const std::string_view> endpointInterfaces,
189 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800190 const MapperGetSubTreeResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500191
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800192void getAssociatedSubTreePathsById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500193 const std::string& id, const std::string& path,
194 std::span<const std::string_view> subtreeInterfaces,
195 std::string_view association,
196 std::span<const std::string_view> endpointInterfaces,
197 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800198 const MapperGetSubTreePathsResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500199
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800200void getDbusObject(const std::string& path,
201 std::span<const std::string_view> interfaces,
202 std::function<void(const boost::system::error_code&,
203 const MapperGetObject&)>&& callback);
George Liu2b731192023-01-11 16:27:13 +0800204
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800205void getAssociationEndPoints(
George Liua4eb7612023-02-11 15:49:46 +0800206 const std::string& path,
207 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800208 const MapperEndPoints&)>&& callback);
George Liua4eb7612023-02-11 15:49:46 +0800209
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800210void getManagedObjects(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400211 const std::string& service, const sdbusplus::message::object_path& path,
212 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800213 const ManagedObjectType&)>&& callback);
James Feist5b4aa862018-08-16 14:07:01 -0700214} // namespace utility
215} // namespace dbus