blob: ee9aaab8d31fbc55470a105cec8802c23bf08beb [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>>>,
Ed Tanousd1a64812021-12-13 12:14:05 -080070 std::vector<std::tuple<std::string, std::string>>,
71 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
72 std::vector<std::tuple<uint32_t, size_t>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000073 std::vector<std::tuple<
74 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
75 std::string, std::string, uint64_t>>
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>>;
80using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010081using ManagedObjectType =
82 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070083
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060084// Map of service name to list of interfaces
85using MapperServiceMap =
86 std::vector<std::pair<std::string, std::vector<std::string>>>;
87
88// Map of object paths to MapperServiceMaps
89using MapperGetSubTreeResponse =
90 std::vector<std::pair<std::string, MapperServiceMap>>;
91
Ed Tanousb9d36b42022-02-26 21:42:46 -080092using MapperGetObject =
93 std::vector<std::pair<std::string, std::vector<std::string>>>;
94
95using MapperGetAncestorsResponse = std::vector<
96 std::pair<std::string,
97 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
98
99using MapperGetSubTreePathsResponse = std::vector<std::string>;
100
George Liua4eb7612023-02-11 15:49:46 +0800101using MapperEndPoints = std::vector<std::string>;
102
James Feist5b4aa862018-08-16 14:07:01 -0700103inline void escapePathForDbus(std::string& path)
104{
105 const std::regex reg("[^A-Za-z0-9_/]");
106 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
107}
108
Ed Tanous863c1c22022-02-21 21:33:06 -0800109inline void logError(const boost::system::error_code& ec)
110{
111 if (ec)
112 {
113 BMCWEB_LOG_ERROR << "DBus error: " << ec << ", cannot call method";
114 }
115}
116
James Feist5b4aa862018-08-16 14:07:01 -0700117// gets the string N strings deep into a path
118// i.e. /0th/1st/2nd/3rd
119inline bool getNthStringFromPath(const std::string& path, int index,
120 std::string& result)
121{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530122 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700123 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530124 return false;
125 }
126
127 std::filesystem::path p1(path);
128 int count = -1;
Patrick Williams89492a12023-05-10 07:51:34 -0500129 for (const auto& element : p1)
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530130 {
131 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700132 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530133 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700134 if (count == index)
135 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530136 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700137 break;
138 }
139 }
140 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800141 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700142}
143
Ratan Gupta22c33712019-05-03 21:50:28 +0530144template <typename Callback>
145inline void checkDbusPathExists(const std::string& path, Callback&& callback)
146{
Ratan Gupta22c33712019-05-03 21:50:28 +0530147 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800148 [callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800149 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800150 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700151 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530152 },
153 "xyz.openbmc_project.ObjectMapper",
154 "/xyz/openbmc_project/object_mapper",
155 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
156 std::array<std::string, 0>());
157}
158
George Liu21384832022-11-09 11:21:15 +0800159inline void
George Liue99073f2022-12-09 11:06:16 +0800160 getSubTree(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&,
163 const MapperGetSubTreeResponse&)>&& callback)
164{
165 crow::connections::systemBus->async_method_call(
166 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800167 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800168 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
169 "xyz.openbmc_project.ObjectMapper",
170 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800171 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
172 interfaces);
George Liu21384832022-11-09 11:21:15 +0800173}
174
175inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800176 const std::string& path, int32_t depth,
177 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800178 std::function<void(const boost::system::error_code&,
179 const MapperGetSubTreePathsResponse&)>&& callback)
180{
181 crow::connections::systemBus->async_method_call(
182 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800183 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800184 const MapperGetSubTreePathsResponse& subtreePaths) {
185 callback(ec, subtreePaths);
186 },
187 "xyz.openbmc_project.ObjectMapper",
188 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800189 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800190 interfaces);
191}
192
Willy Tu8d018362023-04-14 09:14:16 -0700193inline void getAssociatedSubTree(
194 const sdbusplus::message::object_path& associatedPath,
195 const sdbusplus::message::object_path& path, int32_t depth,
196 std::span<const std::string_view> interfaces,
197 std::function<void(const boost::system::error_code&,
198 const MapperGetSubTreeResponse&)>&& callback)
199{
200 crow::connections::systemBus->async_method_call(
201 [callback{std::move(callback)}](
202 const boost::system::error_code& ec,
203 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
204 "xyz.openbmc_project.ObjectMapper",
205 "/xyz/openbmc_project/object_mapper",
206 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTree",
207 associatedPath, path, depth, interfaces);
208}
209
210inline void getAssociatedSubTreePaths(
211 const sdbusplus::message::object_path& associatedPath,
212 const sdbusplus::message::object_path& path, int32_t depth,
213 std::span<const std::string_view> interfaces,
214 std::function<void(const boost::system::error_code&,
215 const MapperGetSubTreePathsResponse&)>&& callback)
216{
217 crow::connections::systemBus->async_method_call(
218 [callback{std::move(callback)}](
219 const boost::system::error_code& ec,
220 const MapperGetSubTreePathsResponse& subtreePaths) {
221 callback(ec, subtreePaths);
222 },
223 "xyz.openbmc_project.ObjectMapper",
224 "/xyz/openbmc_project/object_mapper",
225 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePaths",
226 associatedPath, path, depth, interfaces);
227}
228
George Liu2b731192023-01-11 16:27:13 +0800229inline void
230 getDbusObject(const std::string& path,
231 std::span<const std::string_view> interfaces,
232 std::function<void(const boost::system::error_code&,
233 const MapperGetObject&)>&& callback)
234{
235 crow::connections::systemBus->async_method_call(
236 [callback{std::move(callback)}](const boost::system::error_code& ec,
237 const MapperGetObject& object) {
238 callback(ec, object);
239 },
240 "xyz.openbmc_project.ObjectMapper",
241 "/xyz/openbmc_project/object_mapper",
242 "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
243}
244
George Liua4eb7612023-02-11 15:49:46 +0800245inline void getAssociationEndPoints(
246 const std::string& path,
247 std::function<void(const boost::system::error_code&,
248 const MapperEndPoints&)>&& callback)
249{
250 sdbusplus::asio::getProperty<MapperEndPoints>(
251 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", path,
252 "xyz.openbmc_project.Association", "endpoints", std::move(callback));
253}
254
George Liuf5892d02023-03-01 10:37:08 +0800255inline void
256 getManagedObjects(const std::string& service,
257 const sdbusplus::message::object_path& path,
258 std::function<void(const boost::system::error_code&,
259 const ManagedObjectType&)>&& callback)
260{
261 crow::connections::systemBus->async_method_call(
262 [callback{std::move(callback)}](const boost::system::error_code& ec,
263 const ManagedObjectType& objects) {
264 callback(ec, objects);
265 },
266 service, path, "org.freedesktop.DBus.ObjectManager",
267 "GetManagedObjects");
268}
269
James Feist5b4aa862018-08-16 14:07:01 -0700270} // namespace utility
271} // namespace dbus