| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 4 | #pragma once |
| 5 | |
| Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" |
| Ed Tanous | 95c6307 | 2024-03-26 13:19:52 -0700 | [diff] [blame] | 7 | #include "boost_formatters.hpp" |
| Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 8 | #include "dbus_singleton.hpp" |
| 9 | |
| Ed Tanous | 478b7ad | 2024-07-15 19:11:54 -0700 | [diff] [blame] | 10 | #include <boost/system/error_code.hpp> |
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include <sdbusplus/asio/connection.hpp> |
| George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 12 | #include <sdbusplus/asio/property.hpp> |
| Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 13 | #include <sdbusplus/message/native_types.hpp> |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 14 | |
| Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 15 | #include <cstddef> |
| 16 | #include <cstdint> |
| George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 17 | #include <functional> |
| Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 18 | #include <memory> |
| George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 19 | #include <span> |
| Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 20 | #include <string> |
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 21 | #include <string_view> |
| Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 22 | #include <tuple> |
| Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 23 | #include <utility> |
| Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 24 | #include <variant> |
| Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 25 | #include <vector> |
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 27 | namespace dbus |
| 28 | { |
| 29 | |
| 30 | namespace utility |
| 31 | { |
| 32 | |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 33 | // clang-format off |
| Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 34 | using DbusVariantType = std::variant< |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 35 | std::vector<std::tuple<std::string, std::string, std::string>>, |
| 36 | std::vector<std::string>, |
| 37 | std::vector<double>, |
| 38 | std::string, |
| 39 | int64_t, |
| 40 | uint64_t, |
| 41 | double, |
| 42 | int32_t, |
| 43 | uint32_t, |
| 44 | int16_t, |
| 45 | uint16_t, |
| 46 | uint8_t, |
| 47 | bool, |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 48 | std::vector<uint32_t>, |
| 49 | std::vector<uint16_t>, |
| 50 | sdbusplus::message::object_path, |
| Krzysztof Grobelny | 479e899 | 2021-06-17 13:37:57 +0000 | [diff] [blame] | 51 | 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] | 52 | std::vector<sdbusplus::message::object_path>, |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 53 | std::vector<std::tuple<std::string, std::string>>, |
| 54 | std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>, |
| 55 | std::vector<std::tuple<uint32_t, size_t>>, |
| Krzysztof Grobelny | 479e899 | 2021-06-17 13:37:57 +0000 | [diff] [blame] | 56 | std::vector<std::tuple< |
| 57 | std::vector<std::tuple<sdbusplus::message::object_path, std::string>>, |
| Ed Tanous | e364803 | 2024-10-16 18:06:39 -0700 | [diff] [blame] | 58 | std::string, std::string, uint64_t>>, |
| 59 | std::vector<std::pair<sdbusplus::message::object_path, std::string>>, |
| Ed Tanous | 58c7148 | 2024-10-19 14:35:07 -0700 | [diff] [blame] | 60 | std::vector<std::tuple<std::string, uint64_t, std::string, double>>, |
| 61 | std::vector<std::tuple<std::string, std::string, uint64_t, std::string>> |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 62 | >; |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 63 | |
| Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 64 | // clang-format on |
| Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 65 | using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; |
| Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 66 | using DBusInterfacesMap = |
| 67 | std::vector<std::pair<std::string, DBusPropertiesMap>>; |
| Zbigniew Kurzynski | 755a33c | 2020-02-28 14:06:37 +0100 | [diff] [blame] | 68 | using ManagedObjectType = |
| Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 69 | std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>; |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 70 | |
| Shantappa Teekappanavar | 5df6eda | 2022-01-18 12:29:28 -0600 | [diff] [blame] | 71 | // Map of service name to list of interfaces |
| 72 | using MapperServiceMap = |
| 73 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 74 | |
| 75 | // Map of object paths to MapperServiceMaps |
| 76 | using MapperGetSubTreeResponse = |
| 77 | std::vector<std::pair<std::string, MapperServiceMap>>; |
| 78 | |
| Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 79 | using MapperGetObject = |
| 80 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 81 | |
| 82 | using MapperGetAncestorsResponse = std::vector< |
| 83 | std::pair<std::string, |
| 84 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 85 | |
| 86 | using MapperGetSubTreePathsResponse = std::vector<std::string>; |
| 87 | |
| George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 88 | using MapperEndPoints = std::vector<std::string>; |
| 89 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 90 | void escapePathForDbus(std::string& path); |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 91 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 92 | void logError(const boost::system::error_code& ec); |
| Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 93 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 94 | void getAllProperties(const std::string& service, const std::string& objectPath, |
| 95 | const std::string& interface, |
| 96 | std::function<void(const boost::system::error_code&, |
| 97 | const DBusPropertiesMap&)>&& callback); |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 98 | |
| Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 99 | template <typename MessageHandler, typename... InputArgs> |
| 100 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 101 | void async_method_call(MessageHandler&& handler, const std::string& service, |
| 102 | const std::string& objpath, const std::string& interf, |
| 103 | const std::string& method, const InputArgs&... a) |
| 104 | { |
| 105 | crow::connections::systemBus->async_method_call( |
| 106 | std::forward<MessageHandler>(handler), service, objpath, interf, method, |
| 107 | a...); |
| 108 | } |
| 109 | |
| 110 | template <typename MessageHandler, typename... InputArgs> |
| 111 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 112 | void async_method_call(const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
| 113 | MessageHandler&& handler, const std::string& service, |
| 114 | const std::string& objpath, const std::string& interf, |
| 115 | const std::string& method, const InputArgs&... a) |
| 116 | { |
| 117 | crow::connections::systemBus->async_method_call( |
| 118 | std::forward<MessageHandler>(handler), service, objpath, interf, method, |
| 119 | a...); |
| 120 | } |
| 121 | |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 122 | template <typename PropertyType> |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 123 | void getProperty(const std::string& service, const std::string& objectPath, |
| 124 | const std::string& interface, const std::string& propertyName, |
| 125 | std::function<void(const boost::system::error_code&, |
| 126 | const PropertyType&)>&& callback) |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 127 | { |
| 128 | sdbusplus::asio::getProperty<PropertyType>( |
| 129 | *crow::connections::systemBus, service, objectPath, interface, |
| 130 | propertyName, std::move(callback)); |
| 131 | } |
| 132 | |
| 133 | template <typename PropertyType> |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 134 | void getProperty(sdbusplus::asio::connection& /*conn*/, |
| 135 | const std::string& service, const std::string& objectPath, |
| 136 | const std::string& interface, const std::string& propertyName, |
| 137 | std::function<void(const boost::system::error_code&, |
| 138 | const PropertyType&)>&& callback) |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 139 | { |
| 140 | getProperty(service, objectPath, interface, propertyName, |
| 141 | std::move(callback)); |
| 142 | } |
| 143 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 144 | void getAllProperties(sdbusplus::asio::connection& /*conn*/, |
| 145 | const std::string& service, const std::string& objectPath, |
| 146 | const std::string& interface, |
| 147 | std::function<void(const boost::system::error_code&, |
| 148 | const DBusPropertiesMap&)>&& callback); |
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 149 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 150 | void checkDbusPathExists(const std::string& path, |
| 151 | std::function<void(bool)>&& callback); |
| Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 152 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 153 | void getSubTree( |
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 154 | const std::string& path, int32_t depth, |
| 155 | std::span<const std::string_view> interfaces, |
| 156 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 157 | const MapperGetSubTreeResponse&)>&& callback); |
| George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 158 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 159 | void getSubTreePaths( |
| George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 160 | const std::string& path, int32_t depth, |
| 161 | std::span<const std::string_view> interfaces, |
| George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 162 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 163 | const MapperGetSubTreePathsResponse&)>&& callback); |
| George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 164 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 165 | void getAssociatedSubTree( |
| Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 166 | const sdbusplus::message::object_path& associatedPath, |
| 167 | const sdbusplus::message::object_path& path, int32_t depth, |
| 168 | std::span<const std::string_view> interfaces, |
| 169 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 170 | const MapperGetSubTreeResponse&)>&& callback); |
| Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 171 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 172 | void getAssociatedSubTreePaths( |
| Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 173 | const sdbusplus::message::object_path& associatedPath, |
| 174 | const sdbusplus::message::object_path& path, int32_t depth, |
| 175 | std::span<const std::string_view> interfaces, |
| 176 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 177 | const MapperGetSubTreePathsResponse&)>&& callback); |
| Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 178 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 179 | void getAssociatedSubTreeById( |
| Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 180 | const std::string& id, const std::string& path, |
| 181 | std::span<const std::string_view> subtreeInterfaces, |
| 182 | std::string_view association, |
| 183 | std::span<const std::string_view> endpointInterfaces, |
| 184 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 185 | const MapperGetSubTreeResponse&)>&& callback); |
| Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 186 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 187 | void getAssociatedSubTreePathsById( |
| Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 188 | const std::string& id, const std::string& path, |
| 189 | std::span<const std::string_view> subtreeInterfaces, |
| 190 | std::string_view association, |
| 191 | std::span<const std::string_view> endpointInterfaces, |
| 192 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 193 | const MapperGetSubTreePathsResponse&)>&& callback); |
| Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 194 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 195 | void getDbusObject(const std::string& path, |
| 196 | std::span<const std::string_view> interfaces, |
| 197 | std::function<void(const boost::system::error_code&, |
| 198 | const MapperGetObject&)>&& callback); |
| George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 199 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 200 | void getAssociationEndPoints( |
| George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 201 | const std::string& path, |
| 202 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 203 | const MapperEndPoints&)>&& callback); |
| George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 204 | |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 205 | void getManagedObjects( |
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 206 | const std::string& service, const sdbusplus::message::object_path& path, |
| 207 | std::function<void(const boost::system::error_code&, |
| Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 208 | const ManagedObjectType&)>&& callback); |
| James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 209 | } // namespace utility |
| 210 | } // namespace dbus |