James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Tanous | 95c6307 | 2024-03-26 13:19:52 -0700 | [diff] [blame] | 18 | #include "boost_formatters.hpp" |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 19 | #include "dbus_singleton.hpp" |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 20 | #include "logging.hpp" |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 21 | |
Ed Tanous | 478b7ad | 2024-07-15 19:11:54 -0700 | [diff] [blame] | 22 | #include <boost/system/error_code.hpp> |
George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 23 | #include <sdbusplus/asio/property.hpp> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 24 | #include <sdbusplus/message/native_types.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 25 | |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 26 | #include <array> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <cstdint> |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 29 | #include <filesystem> |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 30 | #include <functional> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 31 | #include <regex> |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 32 | #include <span> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 33 | #include <sstream> |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 34 | #include <string> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 35 | #include <string_view> |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 36 | #include <tuple> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 37 | #include <utility> |
Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 38 | #include <variant> |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 39 | #include <vector> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 40 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 41 | namespace dbus |
| 42 | { |
| 43 | |
| 44 | namespace utility |
| 45 | { |
| 46 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 47 | // clang-format off |
Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 48 | using DbusVariantType = std::variant< |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 49 | std::vector<std::tuple<std::string, std::string, std::string>>, |
| 50 | std::vector<std::string>, |
| 51 | std::vector<double>, |
| 52 | std::string, |
| 53 | int64_t, |
| 54 | uint64_t, |
| 55 | double, |
| 56 | int32_t, |
| 57 | uint32_t, |
| 58 | int16_t, |
| 59 | uint16_t, |
| 60 | uint8_t, |
| 61 | bool, |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 62 | sdbusplus::message::unix_fd, |
| 63 | std::vector<uint32_t>, |
| 64 | std::vector<uint16_t>, |
| 65 | sdbusplus::message::object_path, |
Krzysztof Grobelny | 479e899 | 2021-06-17 13:37:57 +0000 | [diff] [blame] | 66 | std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>, |
Szymon Dompke | f19ab44 | 2022-03-07 14:28:06 +0100 | [diff] [blame] | 67 | std::vector<sdbusplus::message::object_path>, |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 68 | std::vector<std::tuple<std::string, std::string>>, |
| 69 | std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>, |
| 70 | std::vector<std::tuple<uint32_t, size_t>>, |
Krzysztof Grobelny | 479e899 | 2021-06-17 13:37:57 +0000 | [diff] [blame] | 71 | std::vector<std::tuple< |
| 72 | std::vector<std::tuple<sdbusplus::message::object_path, std::string>>, |
| 73 | std::string, std::string, uint64_t>> |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 74 | >; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 75 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 76 | // clang-format on |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 77 | using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 78 | using DBusInterfacesMap = |
| 79 | std::vector<std::pair<std::string, DBusPropertiesMap>>; |
Zbigniew Kurzynski | 755a33c | 2020-02-28 14:06:37 +0100 | [diff] [blame] | 80 | using ManagedObjectType = |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 81 | std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 82 | |
Shantappa Teekappanavar | 5df6eda | 2022-01-18 12:29:28 -0600 | [diff] [blame] | 83 | // Map of service name to list of interfaces |
| 84 | using MapperServiceMap = |
| 85 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 86 | |
| 87 | // Map of object paths to MapperServiceMaps |
| 88 | using MapperGetSubTreeResponse = |
| 89 | std::vector<std::pair<std::string, MapperServiceMap>>; |
| 90 | |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 91 | using MapperGetObject = |
| 92 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 93 | |
| 94 | using MapperGetAncestorsResponse = std::vector< |
| 95 | std::pair<std::string, |
| 96 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 97 | |
| 98 | using MapperGetSubTreePathsResponse = std::vector<std::string>; |
| 99 | |
George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 100 | using MapperEndPoints = std::vector<std::string>; |
| 101 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 102 | inline void escapePathForDbus(std::string& path) |
| 103 | { |
Ed Tanous | 4b24274 | 2023-05-11 09:51:51 -0700 | [diff] [blame] | 104 | const static std::regex reg("[^A-Za-z0-9_/]"); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 105 | std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_"); |
| 106 | } |
| 107 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 108 | inline void logError(const boost::system::error_code& ec) |
| 109 | { |
| 110 | if (ec) |
| 111 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_ERROR("DBus error: {}, cannot call method", ec); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 116 | // gets the string N strings deep into a path |
| 117 | // i.e. /0th/1st/2nd/3rd |
| 118 | inline bool getNthStringFromPath(const std::string& path, int index, |
| 119 | std::string& result) |
| 120 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 121 | if (index < 0) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 122 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | std::filesystem::path p1(path); |
| 127 | int count = -1; |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 128 | for (const auto& element : p1) |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 129 | { |
| 130 | if (element.has_filename()) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 131 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 132 | ++count; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 133 | if (count == index) |
| 134 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 135 | result = element.stem().string(); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
Ed Tanous | dcf2ebc | 2022-01-25 10:07:45 -0800 | [diff] [blame] | 140 | return count >= index; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 143 | template <typename Callback> |
| 144 | inline void checkDbusPathExists(const std::string& path, Callback&& callback) |
| 145 | { |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 146 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 147 | [callback = std::forward<Callback>(callback)]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 148 | const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 149 | const dbus::utility::MapperGetObject& objectNames) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 150 | callback(!ec && !objectNames.empty()); |
| 151 | }, |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 152 | "xyz.openbmc_project.ObjectMapper", |
| 153 | "/xyz/openbmc_project/object_mapper", |
| 154 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, |
| 155 | std::array<std::string, 0>()); |
| 156 | } |
| 157 | |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 158 | inline void |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 159 | getSubTree(const std::string& path, int32_t depth, |
| 160 | std::span<const std::string_view> interfaces, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 161 | std::function<void(const boost::system::error_code&, |
| 162 | const MapperGetSubTreeResponse&)>&& callback) |
| 163 | { |
| 164 | crow::connections::systemBus->async_method_call( |
| 165 | [callback{std::move(callback)}]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 166 | const boost::system::error_code& ec, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 167 | const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); }, |
| 168 | "xyz.openbmc_project.ObjectMapper", |
| 169 | "/xyz/openbmc_project/object_mapper", |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 170 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth, |
| 171 | interfaces); |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | inline void getSubTreePaths( |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 175 | const std::string& path, int32_t depth, |
| 176 | std::span<const std::string_view> interfaces, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 177 | std::function<void(const boost::system::error_code&, |
| 178 | const MapperGetSubTreePathsResponse&)>&& callback) |
| 179 | { |
| 180 | crow::connections::systemBus->async_method_call( |
| 181 | [callback{std::move(callback)}]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 182 | const boost::system::error_code& ec, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 183 | const MapperGetSubTreePathsResponse& subtreePaths) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 184 | callback(ec, subtreePaths); |
| 185 | }, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 186 | "xyz.openbmc_project.ObjectMapper", |
| 187 | "/xyz/openbmc_project/object_mapper", |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 188 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 189 | interfaces); |
| 190 | } |
| 191 | |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 192 | inline void getAssociatedSubTree( |
| 193 | const sdbusplus::message::object_path& associatedPath, |
| 194 | const sdbusplus::message::object_path& path, int32_t depth, |
| 195 | std::span<const std::string_view> interfaces, |
| 196 | std::function<void(const boost::system::error_code&, |
| 197 | const MapperGetSubTreeResponse&)>&& callback) |
| 198 | { |
| 199 | crow::connections::systemBus->async_method_call( |
| 200 | [callback{std::move(callback)}]( |
| 201 | const boost::system::error_code& ec, |
| 202 | const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); }, |
| 203 | "xyz.openbmc_project.ObjectMapper", |
| 204 | "/xyz/openbmc_project/object_mapper", |
| 205 | "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTree", |
| 206 | associatedPath, path, depth, interfaces); |
| 207 | } |
| 208 | |
| 209 | inline void getAssociatedSubTreePaths( |
| 210 | const sdbusplus::message::object_path& associatedPath, |
| 211 | const sdbusplus::message::object_path& path, int32_t depth, |
| 212 | std::span<const std::string_view> interfaces, |
| 213 | std::function<void(const boost::system::error_code&, |
| 214 | const MapperGetSubTreePathsResponse&)>&& callback) |
| 215 | { |
| 216 | crow::connections::systemBus->async_method_call( |
| 217 | [callback{std::move(callback)}]( |
| 218 | const boost::system::error_code& ec, |
| 219 | const MapperGetSubTreePathsResponse& subtreePaths) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 220 | callback(ec, subtreePaths); |
| 221 | }, |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 222 | "xyz.openbmc_project.ObjectMapper", |
| 223 | "/xyz/openbmc_project/object_mapper", |
| 224 | "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePaths", |
| 225 | associatedPath, path, depth, interfaces); |
| 226 | } |
| 227 | |
Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 228 | inline void getAssociatedSubTreeById( |
| 229 | const std::string& id, const std::string& path, |
| 230 | std::span<const std::string_view> subtreeInterfaces, |
| 231 | std::string_view association, |
| 232 | std::span<const std::string_view> endpointInterfaces, |
| 233 | std::function<void(const boost::system::error_code&, |
| 234 | const MapperGetSubTreeResponse&)>&& callback) |
| 235 | { |
| 236 | crow::connections::systemBus->async_method_call( |
| 237 | [callback{std::move(callback)}]( |
| 238 | const boost::system::error_code& ec, |
| 239 | const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); }, |
| 240 | "xyz.openbmc_project.ObjectMapper", |
| 241 | "/xyz/openbmc_project/object_mapper", |
| 242 | "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreeById", id, |
| 243 | path, subtreeInterfaces, association, endpointInterfaces); |
| 244 | } |
| 245 | |
| 246 | inline void getAssociatedSubTreePathsById( |
| 247 | const std::string& id, const std::string& path, |
| 248 | std::span<const std::string_view> subtreeInterfaces, |
| 249 | std::string_view association, |
| 250 | std::span<const std::string_view> endpointInterfaces, |
| 251 | std::function<void(const boost::system::error_code&, |
| 252 | const MapperGetSubTreePathsResponse&)>&& callback) |
| 253 | { |
| 254 | crow::connections::systemBus->async_method_call( |
| 255 | [callback{std::move(callback)}]( |
| 256 | const boost::system::error_code& ec, |
| 257 | const MapperGetSubTreePathsResponse& subtreePaths) { |
| 258 | callback(ec, subtreePaths); |
| 259 | }, |
| 260 | "xyz.openbmc_project.ObjectMapper", |
| 261 | "/xyz/openbmc_project/object_mapper", |
| 262 | "xyz.openbmc_project.ObjectMapper", "GetAssociatedSubTreePathsById", id, |
| 263 | path, subtreeInterfaces, association, endpointInterfaces); |
| 264 | } |
| 265 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 266 | inline void getDbusObject( |
| 267 | const std::string& path, std::span<const std::string_view> interfaces, |
| 268 | std::function<void(const boost::system::error_code&, |
| 269 | const MapperGetObject&)>&& callback) |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 270 | { |
| 271 | crow::connections::systemBus->async_method_call( |
| 272 | [callback{std::move(callback)}](const boost::system::error_code& ec, |
| 273 | const MapperGetObject& object) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 274 | callback(ec, object); |
| 275 | }, |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 276 | "xyz.openbmc_project.ObjectMapper", |
| 277 | "/xyz/openbmc_project/object_mapper", |
| 278 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces); |
| 279 | } |
| 280 | |
George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 281 | inline void getAssociationEndPoints( |
| 282 | const std::string& path, |
| 283 | std::function<void(const boost::system::error_code&, |
| 284 | const MapperEndPoints&)>&& callback) |
| 285 | { |
| 286 | sdbusplus::asio::getProperty<MapperEndPoints>( |
| 287 | *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", path, |
| 288 | "xyz.openbmc_project.Association", "endpoints", std::move(callback)); |
| 289 | } |
| 290 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 291 | inline void getManagedObjects( |
| 292 | const std::string& service, const sdbusplus::message::object_path& path, |
| 293 | std::function<void(const boost::system::error_code&, |
| 294 | const ManagedObjectType&)>&& callback) |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 295 | { |
| 296 | crow::connections::systemBus->async_method_call( |
| 297 | [callback{std::move(callback)}](const boost::system::error_code& ec, |
| 298 | const ManagedObjectType& objects) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 299 | callback(ec, objects); |
| 300 | }, |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 301 | service, path, "org.freedesktop.DBus.ObjectManager", |
| 302 | "GetManagedObjects"); |
| 303 | } |
| 304 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 305 | } // namespace utility |
| 306 | } // namespace dbus |