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