blob: 407909a18124ea844e9dec935a15c24ed511f6f9 [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
Ed Tanousbb1c7d32025-02-09 09:39:19 -080094void 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);
Ed Tanousdeae6a72024-11-11 21:58:57 -080098
Ed Tanous177612a2025-02-14 15:16:09 -080099template <typename MessageHandler, typename... InputArgs>
100// NOLINTNEXTLINE(readability-identifier-naming)
101void 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
110template <typename MessageHandler, typename... InputArgs>
111// NOLINTNEXTLINE(readability-identifier-naming)
112void 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
Ed Tanousdeae6a72024-11-11 21:58:57 -0800122template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800123void 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)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800127{
128 sdbusplus::asio::getProperty<PropertyType>(
129 *crow::connections::systemBus, service, objectPath, interface,
130 propertyName, std::move(callback));
131}
132
133template <typename PropertyType>
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800134void 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)
Ed Tanousdeae6a72024-11-11 21:58:57 -0800139{
140 getProperty(service, objectPath, interface, propertyName,
141 std::move(callback));
142}
143
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800144void 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);
Ed Tanousdeae6a72024-11-11 21:58:57 -0800149
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800150void checkDbusPathExists(const std::string& path,
151 std::function<void(bool)>&& callback);
Ratan Gupta22c33712019-05-03 21:50:28 +0530152
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800153void getSubTree(
Patrick Williams504af5a2025-02-03 14:29:03 -0500154 const std::string& 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 MapperGetSubTreeResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800158
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800159void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800160 const std::string& path, int32_t depth,
161 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800162 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800163 const MapperGetSubTreePathsResponse&)>&& callback);
George Liu21384832022-11-09 11:21:15 +0800164
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800165void getAssociatedSubTree(
Willy Tu8d018362023-04-14 09:14:16 -0700166 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&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800170 const MapperGetSubTreeResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700171
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800172void getAssociatedSubTreePaths(
Willy Tu8d018362023-04-14 09:14:16 -0700173 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&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800177 const MapperGetSubTreePathsResponse&)>&& callback);
Willy Tu8d018362023-04-14 09:14:16 -0700178
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800179void getAssociatedSubTreeById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500180 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&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800185 const MapperGetSubTreeResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500186
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800187void getAssociatedSubTreePathsById(
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500188 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&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800193 const MapperGetSubTreePathsResponse&)>&& callback);
Lakshmi Yadlapati64d8e802024-06-26 17:41:19 -0500194
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800195void 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);
George Liu2b731192023-01-11 16:27:13 +0800199
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800200void getAssociationEndPoints(
George Liua4eb7612023-02-11 15:49:46 +0800201 const std::string& path,
202 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800203 const MapperEndPoints&)>&& callback);
George Liua4eb7612023-02-11 15:49:46 +0800204
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800205void getManagedObjects(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400206 const std::string& service, const sdbusplus::message::object_path& path,
207 std::function<void(const boost::system::error_code&,
Ed Tanousbb1c7d32025-02-09 09:39:19 -0800208 const ManagedObjectType&)>&& callback);
James Feist5b4aa862018-08-16 14:07:01 -0700209} // namespace utility
210} // namespace dbus