1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation 4 #pragma once 5 6 #include "async_resp.hpp" 7 #include "boost_formatters.hpp" 8 #include "dbus_singleton.hpp" 9 10 #include <boost/system/error_code.hpp> 11 #include <sdbusplus/asio/connection.hpp> 12 #include <sdbusplus/asio/property.hpp> 13 #include <sdbusplus/message/native_types.hpp> 14 15 #include <cstddef> 16 #include <cstdint> 17 #include <functional> 18 #include <memory> 19 #include <span> 20 #include <string> 21 #include <string_view> 22 #include <tuple> 23 #include <utility> 24 #include <variant> 25 #include <vector> 26 27 namespace dbus 28 { 29 30 namespace utility 31 { 32 33 // clang-format off 34 using DbusVariantType = std::variant< 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, 48 std::vector<uint32_t>, 49 std::vector<uint16_t>, 50 sdbusplus::message::object_path, 51 std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>, 52 std::vector<sdbusplus::message::object_path>, 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>>, 56 std::vector<std::tuple< 57 std::vector<std::tuple<sdbusplus::message::object_path, std::string>>, 58 std::string, std::string, uint64_t>>, 59 std::vector<std::pair<sdbusplus::message::object_path, std::string>>, 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>> 62 >; 63 64 // clang-format on 65 using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; 66 using DBusInterfacesMap = 67 std::vector<std::pair<std::string, DBusPropertiesMap>>; 68 using ManagedObjectType = 69 std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>; 70 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 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 88 using MapperEndPoints = std::vector<std::string>; 89 90 void escapePathForDbus(std::string& path); 91 92 void logError(const boost::system::error_code& ec); 93 94 // gets the string N strings deep into a path 95 // i.e. /0th/1st/2nd/3rd 96 bool getNthStringFromPath(const std::string& path, int index, 97 std::string& result); 98 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); 103 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 127 template <typename PropertyType> 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) 132 { 133 sdbusplus::asio::getProperty<PropertyType>( 134 *crow::connections::systemBus, service, objectPath, interface, 135 propertyName, std::move(callback)); 136 } 137 138 template <typename PropertyType> 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) 144 { 145 getProperty(service, objectPath, interface, propertyName, 146 std::move(callback)); 147 } 148 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); 154 155 void checkDbusPathExists(const std::string& path, 156 std::function<void(bool)>&& callback); 157 158 void getSubTree( 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&, 162 const MapperGetSubTreeResponse&)>&& callback); 163 164 void getSubTreePaths( 165 const std::string& path, int32_t depth, 166 std::span<const std::string_view> interfaces, 167 std::function<void(const boost::system::error_code&, 168 const MapperGetSubTreePathsResponse&)>&& callback); 169 170 void getAssociatedSubTree( 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&, 175 const MapperGetSubTreeResponse&)>&& callback); 176 177 void getAssociatedSubTreePaths( 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&, 182 const MapperGetSubTreePathsResponse&)>&& callback); 183 184 void getAssociatedSubTreeById( 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&, 190 const MapperGetSubTreeResponse&)>&& callback); 191 192 void getAssociatedSubTreePathsById( 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&, 198 const MapperGetSubTreePathsResponse&)>&& callback); 199 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); 204 205 void getAssociationEndPoints( 206 const std::string& path, 207 std::function<void(const boost::system::error_code&, 208 const MapperEndPoints&)>&& callback); 209 210 void getManagedObjects( 211 const std::string& service, const sdbusplus::message::object_path& path, 212 std::function<void(const boost::system::error_code&, 213 const ManagedObjectType&)>&& callback); 214 } // namespace utility 215 } // namespace dbus 216