xref: /openbmc/bmcweb/include/dbus_utility.hpp (revision bb1c7d30d6799b4e0fcd0b3fc4f97be9a253e6c0)
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 "boost_formatters.hpp"
7 #include "dbus_singleton.hpp"
8 
9 #include <boost/system/error_code.hpp>
10 #include <sdbusplus/asio/connection.hpp>
11 #include <sdbusplus/asio/property.hpp>
12 #include <sdbusplus/message/native_types.hpp>
13 
14 #include <cstddef>
15 #include <cstdint>
16 #include <functional>
17 #include <span>
18 #include <string>
19 #include <string_view>
20 #include <tuple>
21 #include <utility>
22 #include <variant>
23 #include <vector>
24 
25 namespace dbus
26 {
27 
28 namespace utility
29 {
30 
31 // clang-format off
32 using DbusVariantType = std::variant<
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,
46     std::vector<uint32_t>,
47     std::vector<uint16_t>,
48     sdbusplus::message::object_path,
49     std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
50     std::vector<sdbusplus::message::object_path>,
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>>,
54     std::vector<std::tuple<
55       std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
56       std::string, std::string, uint64_t>>,
57     std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
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>>
60  >;
61 
62 // clang-format on
63 using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
64 using DBusInterfacesMap =
65     std::vector<std::pair<std::string, DBusPropertiesMap>>;
66 using ManagedObjectType =
67     std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
68 
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 
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 
86 using MapperEndPoints = std::vector<std::string>;
87 
88 void escapePathForDbus(std::string& path);
89 
90 void logError(const boost::system::error_code& ec);
91 
92 // gets the string N strings deep into a path
93 // i.e.  /0th/1st/2nd/3rd
94 bool getNthStringFromPath(const std::string& path, int index,
95                           std::string& result);
96 
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);
101 
102 template <typename PropertyType>
getProperty(const std::string & service,const std::string & objectPath,const std::string & interface,const std::string & propertyName,std::function<void (const boost::system::error_code &,const PropertyType &)> && callback)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)
107 {
108     sdbusplus::asio::getProperty<PropertyType>(
109         *crow::connections::systemBus, service, objectPath, interface,
110         propertyName, std::move(callback));
111 }
112 
113 template <typename PropertyType>
getProperty(sdbusplus::asio::connection &,const std::string & service,const std::string & objectPath,const std::string & interface,const std::string & propertyName,std::function<void (const boost::system::error_code &,const PropertyType &)> && callback)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)
119 {
120     getProperty(service, objectPath, interface, propertyName,
121                 std::move(callback));
122 }
123 
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);
129 
130 void checkDbusPathExists(const std::string& path,
131                          std::function<void(bool)>&& callback);
132 
133 void getSubTree(
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&,
137                        const MapperGetSubTreeResponse&)>&& callback);
138 
139 void getSubTreePaths(
140     const std::string& path, int32_t depth,
141     std::span<const std::string_view> interfaces,
142     std::function<void(const boost::system::error_code&,
143                        const MapperGetSubTreePathsResponse&)>&& callback);
144 
145 void getAssociatedSubTree(
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&,
150                        const MapperGetSubTreeResponse&)>&& callback);
151 
152 void getAssociatedSubTreePaths(
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&,
157                        const MapperGetSubTreePathsResponse&)>&& callback);
158 
159 void getAssociatedSubTreeById(
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&,
165                        const MapperGetSubTreeResponse&)>&& callback);
166 
167 void getAssociatedSubTreePathsById(
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&,
173                        const MapperGetSubTreePathsResponse&)>&& callback);
174 
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);
179 
180 void getAssociationEndPoints(
181     const std::string& path,
182     std::function<void(const boost::system::error_code&,
183                        const MapperEndPoints&)>&& callback);
184 
185 void getManagedObjects(
186     const std::string& service, const sdbusplus::message::object_path& path,
187     std::function<void(const boost::system::error_code&,
188                        const ManagedObjectType&)>&& callback);
189 } // namespace utility
190 } // namespace dbus
191