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 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 94 | // gets the string N strings deep into a path |
| 95 | // i.e. /0th/1st/2nd/3rd |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 96 | bool getNthStringFromPath(const std::string& path, int index, |
| 97 | std::string& result); |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 98 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 99 | void getAllProperties(const std::string& service, const std::string& objectPath, |
| 100 | const std::string& interface, |
| 101 | std::function<void(const boost::system::error_code&, |
| 102 | const DBusPropertiesMap&)>&& callback); |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 103 | |
Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 104 | template <typename MessageHandler, typename... InputArgs> |
| 105 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 106 | void async_method_call(MessageHandler&& handler, const std::string& service, |
| 107 | const std::string& objpath, const std::string& interf, |
| 108 | const std::string& method, const InputArgs&... a) |
| 109 | { |
| 110 | crow::connections::systemBus->async_method_call( |
| 111 | std::forward<MessageHandler>(handler), service, objpath, interf, method, |
| 112 | a...); |
| 113 | } |
| 114 | |
| 115 | template <typename MessageHandler, typename... InputArgs> |
| 116 | // NOLINTNEXTLINE(readability-identifier-naming) |
| 117 | void async_method_call(const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
| 118 | MessageHandler&& handler, const std::string& service, |
| 119 | const std::string& objpath, const std::string& interf, |
| 120 | const std::string& method, const InputArgs&... a) |
| 121 | { |
| 122 | crow::connections::systemBus->async_method_call( |
| 123 | std::forward<MessageHandler>(handler), service, objpath, interf, method, |
| 124 | a...); |
| 125 | } |
| 126 | |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 127 | template <typename PropertyType> |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 128 | void getProperty(const std::string& service, const std::string& objectPath, |
| 129 | const std::string& interface, const std::string& propertyName, |
| 130 | std::function<void(const boost::system::error_code&, |
| 131 | const PropertyType&)>&& callback) |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 132 | { |
| 133 | sdbusplus::asio::getProperty<PropertyType>( |
| 134 | *crow::connections::systemBus, service, objectPath, interface, |
| 135 | propertyName, std::move(callback)); |
| 136 | } |
| 137 | |
| 138 | template <typename PropertyType> |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 139 | void getProperty(sdbusplus::asio::connection& /*conn*/, |
| 140 | const std::string& service, const std::string& objectPath, |
| 141 | const std::string& interface, const std::string& propertyName, |
| 142 | std::function<void(const boost::system::error_code&, |
| 143 | const PropertyType&)>&& callback) |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 144 | { |
| 145 | getProperty(service, objectPath, interface, propertyName, |
| 146 | std::move(callback)); |
| 147 | } |
| 148 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 149 | void getAllProperties(sdbusplus::asio::connection& /*conn*/, |
| 150 | const std::string& service, const std::string& objectPath, |
| 151 | const std::string& interface, |
| 152 | std::function<void(const boost::system::error_code&, |
| 153 | const DBusPropertiesMap&)>&& callback); |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 154 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 155 | void checkDbusPathExists(const std::string& path, |
| 156 | std::function<void(bool)>&& callback); |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 157 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 158 | void getSubTree( |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 159 | const std::string& path, int32_t depth, |
| 160 | std::span<const std::string_view> interfaces, |
| 161 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 162 | const MapperGetSubTreeResponse&)>&& callback); |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 163 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 164 | void getSubTreePaths( |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 165 | const std::string& path, int32_t depth, |
| 166 | std::span<const std::string_view> interfaces, |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 167 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 168 | const MapperGetSubTreePathsResponse&)>&& callback); |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 169 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 170 | void getAssociatedSubTree( |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 171 | const sdbusplus::message::object_path& associatedPath, |
| 172 | const sdbusplus::message::object_path& path, int32_t depth, |
| 173 | std::span<const std::string_view> interfaces, |
| 174 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 175 | const MapperGetSubTreeResponse&)>&& callback); |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 176 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 177 | void getAssociatedSubTreePaths( |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 178 | const sdbusplus::message::object_path& associatedPath, |
| 179 | const sdbusplus::message::object_path& path, int32_t depth, |
| 180 | std::span<const std::string_view> interfaces, |
| 181 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 182 | const MapperGetSubTreePathsResponse&)>&& callback); |
Willy Tu | 8d01836 | 2023-04-14 09:14:16 -0700 | [diff] [blame] | 183 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 184 | void getAssociatedSubTreeById( |
Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 185 | const std::string& id, const std::string& path, |
| 186 | std::span<const std::string_view> subtreeInterfaces, |
| 187 | std::string_view association, |
| 188 | std::span<const std::string_view> endpointInterfaces, |
| 189 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 190 | const MapperGetSubTreeResponse&)>&& callback); |
Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 191 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 192 | void getAssociatedSubTreePathsById( |
Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 193 | const std::string& id, const std::string& path, |
| 194 | std::span<const std::string_view> subtreeInterfaces, |
| 195 | std::string_view association, |
| 196 | std::span<const std::string_view> endpointInterfaces, |
| 197 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 198 | const MapperGetSubTreePathsResponse&)>&& callback); |
Lakshmi Yadlapati | 64d8e80 | 2024-06-26 17:41:19 -0500 | [diff] [blame] | 199 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 200 | void getDbusObject(const std::string& path, |
| 201 | std::span<const std::string_view> interfaces, |
| 202 | std::function<void(const boost::system::error_code&, |
| 203 | const MapperGetObject&)>&& callback); |
George Liu | 2b73119 | 2023-01-11 16:27:13 +0800 | [diff] [blame] | 204 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 205 | void getAssociationEndPoints( |
George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 206 | const std::string& path, |
| 207 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 208 | const MapperEndPoints&)>&& callback); |
George Liu | a4eb761 | 2023-02-11 15:49:46 +0800 | [diff] [blame] | 209 | |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 210 | void getManagedObjects( |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 211 | const std::string& service, const sdbusplus::message::object_path& path, |
| 212 | std::function<void(const boost::system::error_code&, |
Ed Tanous | bb1c7d3 | 2025-02-09 09:39:19 -0800 | [diff] [blame] | 213 | const ManagedObjectType&)>&& callback); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 214 | } // namespace utility |
| 215 | } // namespace dbus |