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