blob: 933d733f96f60be2a6013343113aad1ed131ae75 [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001/*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16#pragma once
17
Nan Zhou80d37e72022-06-21 17:46:14 +000018#include "dbus_singleton.hpp"
Ed Tanous863c1c22022-02-21 21:33:06 -080019#include "logging.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +000020
Nan Zhoud5c80ad2022-07-11 01:16:31 +000021#include <boost/system/error_code.hpp> // IWYU pragma: keep
George Liua4eb7612023-02-11 15:49:46 +080022#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000023#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070024
Nan Zhou80d37e72022-06-21 17:46:14 +000025#include <array>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000026#include <cstddef>
27#include <cstdint>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053028#include <filesystem>
George Liu21384832022-11-09 11:21:15 +080029#include <functional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030#include <regex>
George Liu21384832022-11-09 11:21:15 +080031#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000032#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000033#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080034#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000035#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000036#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080037#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000038#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050039
Nan Zhoud5c80ad2022-07-11 01:16:31 +000040// IWYU pragma: no_include <stddef.h>
41// IWYU pragma: no_include <stdint.h>
42// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
43
James Feist5b4aa862018-08-16 14:07:01 -070044namespace dbus
45{
46
47namespace utility
48{
49
Ed Tanousd1a64812021-12-13 12:14:05 -080050// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080051using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080052 std::vector<std::tuple<std::string, std::string, std::string>>,
53 std::vector<std::string>,
54 std::vector<double>,
55 std::string,
56 int64_t,
57 uint64_t,
58 double,
59 int32_t,
60 uint32_t,
61 int16_t,
62 uint16_t,
63 uint8_t,
64 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080065 sdbusplus::message::unix_fd,
66 std::vector<uint32_t>,
67 std::vector<uint16_t>,
68 sdbusplus::message::object_path,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000069 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
Szymon Dompkef19ab442022-03-07 14:28:06 +010070 std::vector<sdbusplus::message::object_path>,
Ed Tanousd1a64812021-12-13 12:14:05 -080071 std::vector<std::tuple<std::string, std::string>>,
72 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
73 std::vector<std::tuple<uint32_t, size_t>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000074 std::vector<std::tuple<
75 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
76 std::string, std::string, uint64_t>>
Ed Tanousd1a64812021-12-13 12:14:05 -080077 >;
James Feist5b4aa862018-08-16 14:07:01 -070078
Ed Tanousd1a64812021-12-13 12:14:05 -080079// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080080using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
Michael Shen80f79a42023-08-24 13:41:53 +000081using DBusInterfacesMap =
82 std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010083using ManagedObjectType =
Michael Shen80f79a42023-08-24 13:41:53 +000084 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070085
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060086// Map of service name to list of interfaces
87using MapperServiceMap =
88 std::vector<std::pair<std::string, std::vector<std::string>>>;
89
90// Map of object paths to MapperServiceMaps
91using MapperGetSubTreeResponse =
92 std::vector<std::pair<std::string, MapperServiceMap>>;
93
Ed Tanousb9d36b42022-02-26 21:42:46 -080094using MapperGetObject =
95 std::vector<std::pair<std::string, std::vector<std::string>>>;
96
97using MapperGetAncestorsResponse = std::vector<
98 std::pair<std::string,
99 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
100
101using MapperGetSubTreePathsResponse = std::vector<std::string>;
102
George Liua4eb7612023-02-11 15:49:46 +0800103using MapperEndPoints = std::vector<std::string>;
104
James Feist5b4aa862018-08-16 14:07:01 -0700105inline void escapePathForDbus(std::string& path)
106{
Ed Tanous4b242742023-05-11 09:51:51 -0700107 const static std::regex reg("[^A-Za-z0-9_/]");
James Feist5b4aa862018-08-16 14:07:01 -0700108 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
109}
110
Ed Tanous863c1c22022-02-21 21:33:06 -0800111inline void logError(const boost::system::error_code& ec)
112{
113 if (ec)
114 {
Ed Tanous62598e32023-07-17 17:06:25 -0700115 BMCWEB_LOG_ERROR("DBus error: {}, cannot call method", ec);
Ed Tanous863c1c22022-02-21 21:33:06 -0800116 }
117}
118
James Feist5b4aa862018-08-16 14:07:01 -0700119// gets the string N strings deep into a path
120// i.e. /0th/1st/2nd/3rd
121inline bool getNthStringFromPath(const std::string& path, int index,
122 std::string& result)
123{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530124 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700125 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530126 return false;
127 }
128
129 std::filesystem::path p1(path);
130 int count = -1;
Patrick Williams89492a12023-05-10 07:51:34 -0500131 for (const auto& element : p1)
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530132 {
133 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700134 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530135 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700136 if (count == index)
137 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530138 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700139 break;
140 }
141 }
142 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800143 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700144}
145
Ratan Gupta22c33712019-05-03 21:50:28 +0530146template <typename Callback>
147inline void checkDbusPathExists(const std::string& path, Callback&& callback)
148{
Ratan Gupta22c33712019-05-03 21:50:28 +0530149 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800150 [callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800151 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800152 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 callback(!ec && !objectNames.empty());
Patrick Williams5a39f772023-10-20 11:20:21 -0500154 },
Ratan Gupta22c33712019-05-03 21:50:28 +0530155 "xyz.openbmc_project.ObjectMapper",
156 "/xyz/openbmc_project/object_mapper",
157 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
158 std::array<std::string, 0>());
159}
160
George Liu21384832022-11-09 11:21:15 +0800161inline void
George Liue99073f2022-12-09 11:06:16 +0800162 getSubTree(const std::string& path, int32_t depth,
163 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800164 std::function<void(const boost::system::error_code&,
165 const MapperGetSubTreeResponse&)>&& callback)
166{
167 crow::connections::systemBus->async_method_call(
168 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800169 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800170 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
171 "xyz.openbmc_project.ObjectMapper",
172 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800173 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
174 interfaces);
George Liu21384832022-11-09 11:21:15 +0800175}
176
177inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800178 const std::string& path, int32_t depth,
179 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800180 std::function<void(const boost::system::error_code&,
181 const MapperGetSubTreePathsResponse&)>&& callback)
182{
183 crow::connections::systemBus->async_method_call(
184 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800185 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800186 const MapperGetSubTreePathsResponse& subtreePaths) {
187 callback(ec, subtreePaths);
Patrick Williams5a39f772023-10-20 11:20:21 -0500188 },
George Liu21384832022-11-09 11:21:15 +0800189 "xyz.openbmc_project.ObjectMapper",
190 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800191 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800192 interfaces);
193}
194
Willy Tu8d018362023-04-14 09:14:16 -0700195inline void getAssociatedSubTree(
196 const sdbusplus::message::object_path& associatedPath,
197 const sdbusplus::message::object_path& path, int32_t depth,
198 std::span<const std::string_view> interfaces,
199 std::function<void(const boost::system::error_code&,
200 const MapperGetSubTreeResponse&)>&& callback)
201{
202 crow::connections::systemBus->async_method_call(
203 [callback{std::move(callback)}](
204 const boost::system::error_code& ec,
205 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
206 "xyz.openbmc_project.ObjectMapper",
207 "/xyz/openbmc_project/object_mapper",
208 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTree",
209 associatedPath, path, depth, interfaces);
210}
211
212inline void getAssociatedSubTreePaths(
213 const sdbusplus::message::object_path& associatedPath,
214 const sdbusplus::message::object_path& path, int32_t depth,
215 std::span<const std::string_view> interfaces,
216 std::function<void(const boost::system::error_code&,
217 const MapperGetSubTreePathsResponse&)>&& callback)
218{
219 crow::connections::systemBus->async_method_call(
220 [callback{std::move(callback)}](
221 const boost::system::error_code& ec,
222 const MapperGetSubTreePathsResponse& subtreePaths) {
223 callback(ec, subtreePaths);
Patrick Williams5a39f772023-10-20 11:20:21 -0500224 },
Willy Tu8d018362023-04-14 09:14:16 -0700225 "xyz.openbmc_project.ObjectMapper",
226 "/xyz/openbmc_project/object_mapper",
227 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePaths",
228 associatedPath, path, depth, interfaces);
229}
230
George Liu2b731192023-01-11 16:27:13 +0800231inline void
232 getDbusObject(const std::string& path,
233 std::span<const std::string_view> interfaces,
234 std::function<void(const boost::system::error_code&,
235 const MapperGetObject&)>&& callback)
236{
237 crow::connections::systemBus->async_method_call(
238 [callback{std::move(callback)}](const boost::system::error_code& ec,
239 const MapperGetObject& object) {
240 callback(ec, object);
Patrick Williams5a39f772023-10-20 11:20:21 -0500241 },
George Liu2b731192023-01-11 16:27:13 +0800242 "xyz.openbmc_project.ObjectMapper",
243 "/xyz/openbmc_project/object_mapper",
244 "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
245}
246
George Liua4eb7612023-02-11 15:49:46 +0800247inline void getAssociationEndPoints(
248 const std::string& path,
249 std::function<void(const boost::system::error_code&,
250 const MapperEndPoints&)>&& callback)
251{
252 sdbusplus::asio::getProperty<MapperEndPoints>(
253 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", path,
254 "xyz.openbmc_project.Association", "endpoints", std::move(callback));
255}
256
George Liuf5892d02023-03-01 10:37:08 +0800257inline void
258 getManagedObjects(const std::string& service,
259 const sdbusplus::message::object_path& path,
260 std::function<void(const boost::system::error_code&,
261 const ManagedObjectType&)>&& callback)
262{
263 crow::connections::systemBus->async_method_call(
264 [callback{std::move(callback)}](const boost::system::error_code& ec,
265 const ManagedObjectType& objects) {
266 callback(ec, objects);
Patrick Williams5a39f772023-10-20 11:20:21 -0500267 },
George Liuf5892d02023-03-01 10:37:08 +0800268 service, path, "org.freedesktop.DBus.ObjectManager",
269 "GetManagedObjects");
270}
271
James Feist5b4aa862018-08-16 14:07:01 -0700272} // namespace utility
273} // namespace dbus