blob: c9604f0bc548595c1cc97fc1d18e8d33c978e595 [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
Ed Tanous95c63072024-03-26 13:19:52 -070018#include "boost_formatters.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +000019#include "dbus_singleton.hpp"
Ed Tanous863c1c22022-02-21 21:33:06 -080020#include "logging.hpp"
Nan Zhou80d37e72022-06-21 17:46:14 +000021
Nan Zhoud5c80ad2022-07-11 01:16:31 +000022#include <boost/system/error_code.hpp> // IWYU pragma: keep
George Liua4eb7612023-02-11 15:49:46 +080023#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000024#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070025
Nan Zhou80d37e72022-06-21 17:46:14 +000026#include <array>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000027#include <cstddef>
28#include <cstdint>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053029#include <filesystem>
George Liu21384832022-11-09 11:21:15 +080030#include <functional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031#include <regex>
George Liu21384832022-11-09 11:21:15 +080032#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000033#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000034#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080035#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000036#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000037#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080038#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000039#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040
Nan Zhoud5c80ad2022-07-11 01:16:31 +000041// IWYU pragma: no_include <stddef.h>
42// IWYU pragma: no_include <stdint.h>
43// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
44
James Feist5b4aa862018-08-16 14:07:01 -070045namespace dbus
46{
47
48namespace utility
49{
50
Ed Tanousd1a64812021-12-13 12:14:05 -080051// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080052using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080053 std::vector<std::tuple<std::string, std::string, std::string>>,
54 std::vector<std::string>,
55 std::vector<double>,
56 std::string,
57 int64_t,
58 uint64_t,
59 double,
60 int32_t,
61 uint32_t,
62 int16_t,
63 uint16_t,
64 uint8_t,
65 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080066 sdbusplus::message::unix_fd,
67 std::vector<uint32_t>,
68 std::vector<uint16_t>,
69 sdbusplus::message::object_path,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000070 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
Szymon Dompkef19ab442022-03-07 14:28:06 +010071 std::vector<sdbusplus::message::object_path>,
Ed Tanousd1a64812021-12-13 12:14:05 -080072 std::vector<std::tuple<std::string, std::string>>,
73 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
74 std::vector<std::tuple<uint32_t, size_t>>,
Krzysztof Grobelny479e8992021-06-17 13:37:57 +000075 std::vector<std::tuple<
76 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
77 std::string, std::string, uint64_t>>
Ed Tanousd1a64812021-12-13 12:14:05 -080078 >;
James Feist5b4aa862018-08-16 14:07:01 -070079
Ed Tanousd1a64812021-12-13 12:14:05 -080080// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080081using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
Michael Shen80f79a42023-08-24 13:41:53 +000082using DBusInterfacesMap =
83 std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010084using ManagedObjectType =
Michael Shen80f79a42023-08-24 13:41:53 +000085 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070086
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060087// Map of service name to list of interfaces
88using MapperServiceMap =
89 std::vector<std::pair<std::string, std::vector<std::string>>>;
90
91// Map of object paths to MapperServiceMaps
92using MapperGetSubTreeResponse =
93 std::vector<std::pair<std::string, MapperServiceMap>>;
94
Ed Tanousb9d36b42022-02-26 21:42:46 -080095using MapperGetObject =
96 std::vector<std::pair<std::string, std::vector<std::string>>>;
97
98using MapperGetAncestorsResponse = std::vector<
99 std::pair<std::string,
100 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
101
102using MapperGetSubTreePathsResponse = std::vector<std::string>;
103
George Liua4eb7612023-02-11 15:49:46 +0800104using MapperEndPoints = std::vector<std::string>;
105
James Feist5b4aa862018-08-16 14:07:01 -0700106inline void escapePathForDbus(std::string& path)
107{
Ed Tanous4b242742023-05-11 09:51:51 -0700108 const static std::regex reg("[^A-Za-z0-9_/]");
James Feist5b4aa862018-08-16 14:07:01 -0700109 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
110}
111
Ed Tanous863c1c22022-02-21 21:33:06 -0800112inline void logError(const boost::system::error_code& ec)
113{
114 if (ec)
115 {
Ed Tanous62598e32023-07-17 17:06:25 -0700116 BMCWEB_LOG_ERROR("DBus error: {}, cannot call method", ec);
Ed Tanous863c1c22022-02-21 21:33:06 -0800117 }
118}
119
James Feist5b4aa862018-08-16 14:07:01 -0700120// gets the string N strings deep into a path
121// i.e. /0th/1st/2nd/3rd
122inline bool getNthStringFromPath(const std::string& path, int index,
123 std::string& result)
124{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530125 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700126 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530127 return false;
128 }
129
130 std::filesystem::path p1(path);
131 int count = -1;
Patrick Williams89492a12023-05-10 07:51:34 -0500132 for (const auto& element : p1)
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530133 {
134 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700135 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530136 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700137 if (count == index)
138 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530139 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700140 break;
141 }
142 }
143 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800144 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700145}
146
Ratan Gupta22c33712019-05-03 21:50:28 +0530147template <typename Callback>
148inline void checkDbusPathExists(const std::string& path, Callback&& callback)
149{
Ratan Gupta22c33712019-05-03 21:50:28 +0530150 crow::connections::systemBus->async_method_call(
Ed Tanous8cb2c022024-03-27 16:31:46 -0700151 [callback = std::forward<Callback>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800152 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800153 const dbus::utility::MapperGetObject& objectNames) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400154 callback(!ec && !objectNames.empty());
155 },
Ratan Gupta22c33712019-05-03 21:50:28 +0530156 "xyz.openbmc_project.ObjectMapper",
157 "/xyz/openbmc_project/object_mapper",
158 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
159 std::array<std::string, 0>());
160}
161
George Liu21384832022-11-09 11:21:15 +0800162inline void
George Liue99073f2022-12-09 11:06:16 +0800163 getSubTree(const std::string& path, int32_t depth,
164 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800165 std::function<void(const boost::system::error_code&,
166 const MapperGetSubTreeResponse&)>&& callback)
167{
168 crow::connections::systemBus->async_method_call(
169 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800170 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800171 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
172 "xyz.openbmc_project.ObjectMapper",
173 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800174 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
175 interfaces);
George Liu21384832022-11-09 11:21:15 +0800176}
177
178inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800179 const std::string& path, int32_t depth,
180 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800181 std::function<void(const boost::system::error_code&,
182 const MapperGetSubTreePathsResponse&)>&& callback)
183{
184 crow::connections::systemBus->async_method_call(
185 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800186 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800187 const MapperGetSubTreePathsResponse& subtreePaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400188 callback(ec, subtreePaths);
189 },
George Liu21384832022-11-09 11:21:15 +0800190 "xyz.openbmc_project.ObjectMapper",
191 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800192 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800193 interfaces);
194}
195
Willy Tu8d018362023-04-14 09:14:16 -0700196inline void getAssociatedSubTree(
197 const sdbusplus::message::object_path& associatedPath,
198 const sdbusplus::message::object_path& path, int32_t depth,
199 std::span<const std::string_view> interfaces,
200 std::function<void(const boost::system::error_code&,
201 const MapperGetSubTreeResponse&)>&& callback)
202{
203 crow::connections::systemBus->async_method_call(
204 [callback{std::move(callback)}](
205 const boost::system::error_code& ec,
206 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
207 "xyz.openbmc_project.ObjectMapper",
208 "/xyz/openbmc_project/object_mapper",
209 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTree",
210 associatedPath, path, depth, interfaces);
211}
212
213inline void getAssociatedSubTreePaths(
214 const sdbusplus::message::object_path& associatedPath,
215 const sdbusplus::message::object_path& path, int32_t depth,
216 std::span<const std::string_view> interfaces,
217 std::function<void(const boost::system::error_code&,
218 const MapperGetSubTreePathsResponse&)>&& callback)
219{
220 crow::connections::systemBus->async_method_call(
221 [callback{std::move(callback)}](
222 const boost::system::error_code& ec,
223 const MapperGetSubTreePathsResponse& subtreePaths) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400224 callback(ec, subtreePaths);
225 },
Willy Tu8d018362023-04-14 09:14:16 -0700226 "xyz.openbmc_project.ObjectMapper",
227 "/xyz/openbmc_project/object_mapper",
228 "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePaths",
229 associatedPath, path, depth, interfaces);
230}
231
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400232inline void getDbusObject(
233 const std::string& path, std::span<const std::string_view> interfaces,
234 std::function<void(const boost::system::error_code&,
235 const MapperGetObject&)>&& callback)
George Liu2b731192023-01-11 16:27:13 +0800236{
237 crow::connections::systemBus->async_method_call(
238 [callback{std::move(callback)}](const boost::system::error_code& ec,
239 const MapperGetObject& object) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400240 callback(ec, object);
241 },
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
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400257inline void getManagedObjects(
258 const std::string& service, const sdbusplus::message::object_path& path,
259 std::function<void(const boost::system::error_code&,
260 const ManagedObjectType&)>&& callback)
George Liuf5892d02023-03-01 10:37:08 +0800261{
262 crow::connections::systemBus->async_method_call(
263 [callback{std::move(callback)}](const boost::system::error_code& ec,
264 const ManagedObjectType& objects) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400265 callback(ec, objects);
266 },
George Liuf5892d02023-03-01 10:37:08 +0800267 service, path, "org.freedesktop.DBus.ObjectManager",
268 "GetManagedObjects");
269}
270
James Feist5b4aa862018-08-16 14:07:01 -0700271} // namespace utility
272} // namespace dbus