xref: /openbmc/bmcweb/include/dbus_utility.hpp (revision bb1c7d30d6799b4e0fcd0b3fc4f97be9a253e6c0)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
45b4aa86bSJames Feist #pragma once
55b4aa86bSJames Feist 
695c6307aSEd Tanous #include "boost_formatters.hpp"
780d37e76SNan Zhou #include "dbus_singleton.hpp"
880d37e76SNan Zhou 
9478b7adfSEd Tanous #include <boost/system/error_code.hpp>
10d7857201SEd Tanous #include <sdbusplus/asio/connection.hpp>
11a4eb761aSGeorge Liu #include <sdbusplus/asio/property.hpp>
12d5c80ad9SNan Zhou #include <sdbusplus/message/native_types.hpp>
135b4aa86bSJames Feist 
14d5c80ad9SNan Zhou #include <cstddef>
15d5c80ad9SNan Zhou #include <cstdint>
162138483cSGeorge Liu #include <functional>
172138483cSGeorge Liu #include <span>
1880d37e76SNan Zhou #include <string>
197a1dbc48SGeorge Liu #include <string_view>
2080d37e76SNan Zhou #include <tuple>
21d5c80ad9SNan Zhou #include <utility>
224068129aSEd Tanous #include <variant>
2380d37e76SNan Zhou #include <vector>
241214b7e7SGunnar Mills 
255b4aa86bSJames Feist namespace dbus
265b4aa86bSJames Feist {
275b4aa86bSJames Feist 
285b4aa86bSJames Feist namespace utility
295b4aa86bSJames Feist {
305b4aa86bSJames Feist 
31d1a64814SEd Tanous // clang-format off
324068129aSEd Tanous using DbusVariantType = std::variant<
33d1a64814SEd Tanous     std::vector<std::tuple<std::string, std::string, std::string>>,
34d1a64814SEd Tanous     std::vector<std::string>,
35d1a64814SEd Tanous     std::vector<double>,
36d1a64814SEd Tanous     std::string,
37d1a64814SEd Tanous     int64_t,
38d1a64814SEd Tanous     uint64_t,
39d1a64814SEd Tanous     double,
40d1a64814SEd Tanous     int32_t,
41d1a64814SEd Tanous     uint32_t,
42d1a64814SEd Tanous     int16_t,
43d1a64814SEd Tanous     uint16_t,
44d1a64814SEd Tanous     uint8_t,
45d1a64814SEd Tanous     bool,
46d1a64814SEd Tanous     std::vector<uint32_t>,
47d1a64814SEd Tanous     std::vector<uint16_t>,
48d1a64814SEd Tanous     sdbusplus::message::object_path,
49479e899dSKrzysztof Grobelny     std::tuple<uint64_t, std::vector<std::tuple<std::string, double, uint64_t>>>,
50f19ab44aSSzymon Dompke     std::vector<sdbusplus::message::object_path>,
51d1a64814SEd Tanous     std::vector<std::tuple<std::string, std::string>>,
52d1a64814SEd Tanous     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
53d1a64814SEd Tanous     std::vector<std::tuple<uint32_t, size_t>>,
54479e899dSKrzysztof Grobelny     std::vector<std::tuple<
55479e899dSKrzysztof Grobelny       std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
56e3648032SEd Tanous       std::string, std::string, uint64_t>>,
57e3648032SEd Tanous     std::vector<std::pair<sdbusplus::message::object_path, std::string>>,
58e3648032SEd Tanous     std::vector<std::tuple<std::string, uint64_t, std::string, double>>,
59e3648032SEd Tanous     std::vector<std::tuple<std::string, std::string, uint64_t, std::string>>
60d1a64814SEd Tanous  >;
615b4aa86bSJames Feist 
62d1a64814SEd Tanous // clang-format on
63711ac7a9SEd Tanous using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
6480f79a40SMichael Shen using DBusInterfacesMap =
6580f79a40SMichael Shen     std::vector<std::pair<std::string, DBusPropertiesMap>>;
66755a33c4SZbigniew Kurzynski using ManagedObjectType =
6780f79a40SMichael Shen     std::vector<std::pair<sdbusplus::message::object_path, DBusInterfacesMap>>;
685b4aa86bSJames Feist 
695df6eda2SShantappa Teekappanavar // Map of service name to list of interfaces
705df6eda2SShantappa Teekappanavar using MapperServiceMap =
715df6eda2SShantappa Teekappanavar     std::vector<std::pair<std::string, std::vector<std::string>>>;
725df6eda2SShantappa Teekappanavar 
735df6eda2SShantappa Teekappanavar // Map of object paths to MapperServiceMaps
745df6eda2SShantappa Teekappanavar using MapperGetSubTreeResponse =
755df6eda2SShantappa Teekappanavar     std::vector<std::pair<std::string, MapperServiceMap>>;
765df6eda2SShantappa Teekappanavar 
77b9d36b47SEd Tanous using MapperGetObject =
78b9d36b47SEd Tanous     std::vector<std::pair<std::string, std::vector<std::string>>>;
79b9d36b47SEd Tanous 
80b9d36b47SEd Tanous using MapperGetAncestorsResponse = std::vector<
81b9d36b47SEd Tanous     std::pair<std::string,
82b9d36b47SEd Tanous               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
83b9d36b47SEd Tanous 
84b9d36b47SEd Tanous using MapperGetSubTreePathsResponse = std::vector<std::string>;
85b9d36b47SEd Tanous 
86a4eb761aSGeorge Liu using MapperEndPoints = std::vector<std::string>;
87a4eb761aSGeorge Liu 
88*bb1c7d30SEd Tanous void escapePathForDbus(std::string& path);
895b4aa86bSJames Feist 
90*bb1c7d30SEd Tanous void logError(const boost::system::error_code& ec);
91863c1c2eSEd Tanous 
925b4aa86bSJames Feist // gets the string N strings deep into a path
935b4aa86bSJames Feist // i.e.  /0th/1st/2nd/3rd
94*bb1c7d30SEd Tanous bool getNthStringFromPath(const std::string& path, int index,
95*bb1c7d30SEd Tanous                           std::string& result);
9617a897dfSManojkiran Eda 
97*bb1c7d30SEd Tanous void getAllProperties(const std::string& service, const std::string& objectPath,
98deae6a78SEd Tanous                       const std::string& interface,
99deae6a78SEd Tanous                       std::function<void(const boost::system::error_code&,
100*bb1c7d30SEd Tanous                                          const DBusPropertiesMap&)>&& callback);
101deae6a78SEd Tanous 
102deae6a78SEd Tanous 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*bb1c7d30SEd Tanous void getProperty(const std::string& service, const std::string& objectPath,
104deae6a78SEd Tanous                  const std::string& interface, const std::string& propertyName,
105*bb1c7d30SEd Tanous                  std::function<void(const boost::system::error_code&,
106*bb1c7d30SEd Tanous                                     const PropertyType&)>&& callback)
107deae6a78SEd Tanous {
108deae6a78SEd Tanous     sdbusplus::asio::getProperty<PropertyType>(
109deae6a78SEd Tanous         *crow::connections::systemBus, service, objectPath, interface,
110deae6a78SEd Tanous         propertyName, std::move(callback));
111deae6a78SEd Tanous }
112deae6a78SEd Tanous 
113deae6a78SEd Tanous 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*bb1c7d30SEd Tanous void getProperty(sdbusplus::asio::connection& /*conn*/,
115*bb1c7d30SEd Tanous                  const std::string& service, const std::string& objectPath,
116*bb1c7d30SEd Tanous                  const std::string& interface, const std::string& propertyName,
117*bb1c7d30SEd Tanous                  std::function<void(const boost::system::error_code&,
118*bb1c7d30SEd Tanous                                     const PropertyType&)>&& callback)
119deae6a78SEd Tanous {
120deae6a78SEd Tanous     getProperty(service, objectPath, interface, propertyName,
121deae6a78SEd Tanous                 std::move(callback));
122deae6a78SEd Tanous }
123deae6a78SEd Tanous 
124*bb1c7d30SEd Tanous void getAllProperties(sdbusplus::asio::connection& /*conn*/,
125*bb1c7d30SEd Tanous                       const std::string& service, const std::string& objectPath,
126*bb1c7d30SEd Tanous                       const std::string& interface,
127deae6a78SEd Tanous                       std::function<void(const boost::system::error_code&,
128*bb1c7d30SEd Tanous                                          const DBusPropertiesMap&)>&& callback);
129deae6a78SEd Tanous 
130*bb1c7d30SEd Tanous void checkDbusPathExists(const std::string& path,
131*bb1c7d30SEd Tanous                          std::function<void(bool)>&& callback);
13222c33710SRatan Gupta 
133*bb1c7d30SEd Tanous void getSubTree(
134504af5a0SPatrick Williams     const std::string& path, int32_t depth,
135e99073f5SGeorge Liu     std::span<const std::string_view> interfaces,
1362138483cSGeorge Liu     std::function<void(const boost::system::error_code&,
137*bb1c7d30SEd Tanous                        const MapperGetSubTreeResponse&)>&& callback);
1382138483cSGeorge Liu 
139*bb1c7d30SEd Tanous void getSubTreePaths(
1407a1dbc48SGeorge Liu     const std::string& path, int32_t depth,
1417a1dbc48SGeorge Liu     std::span<const std::string_view> interfaces,
1422138483cSGeorge Liu     std::function<void(const boost::system::error_code&,
143*bb1c7d30SEd Tanous                        const MapperGetSubTreePathsResponse&)>&& callback);
1442138483cSGeorge Liu 
145*bb1c7d30SEd Tanous void getAssociatedSubTree(
1468d01836cSWilly Tu     const sdbusplus::message::object_path& associatedPath,
1478d01836cSWilly Tu     const sdbusplus::message::object_path& path, int32_t depth,
1488d01836cSWilly Tu     std::span<const std::string_view> interfaces,
1498d01836cSWilly Tu     std::function<void(const boost::system::error_code&,
150*bb1c7d30SEd Tanous                        const MapperGetSubTreeResponse&)>&& callback);
1518d01836cSWilly Tu 
152*bb1c7d30SEd Tanous void getAssociatedSubTreePaths(
1538d01836cSWilly Tu     const sdbusplus::message::object_path& associatedPath,
1548d01836cSWilly Tu     const sdbusplus::message::object_path& path, int32_t depth,
1558d01836cSWilly Tu     std::span<const std::string_view> interfaces,
1568d01836cSWilly Tu     std::function<void(const boost::system::error_code&,
157*bb1c7d30SEd Tanous                        const MapperGetSubTreePathsResponse&)>&& callback);
1588d01836cSWilly Tu 
159*bb1c7d30SEd Tanous void getAssociatedSubTreeById(
16064d8e80dSLakshmi Yadlapati     const std::string& id, const std::string& path,
16164d8e80dSLakshmi Yadlapati     std::span<const std::string_view> subtreeInterfaces,
16264d8e80dSLakshmi Yadlapati     std::string_view association,
16364d8e80dSLakshmi Yadlapati     std::span<const std::string_view> endpointInterfaces,
16464d8e80dSLakshmi Yadlapati     std::function<void(const boost::system::error_code&,
165*bb1c7d30SEd Tanous                        const MapperGetSubTreeResponse&)>&& callback);
16664d8e80dSLakshmi Yadlapati 
167*bb1c7d30SEd Tanous void getAssociatedSubTreePathsById(
16864d8e80dSLakshmi Yadlapati     const std::string& id, const std::string& path,
16964d8e80dSLakshmi Yadlapati     std::span<const std::string_view> subtreeInterfaces,
17064d8e80dSLakshmi Yadlapati     std::string_view association,
17164d8e80dSLakshmi Yadlapati     std::span<const std::string_view> endpointInterfaces,
17264d8e80dSLakshmi Yadlapati     std::function<void(const boost::system::error_code&,
173*bb1c7d30SEd Tanous                        const MapperGetSubTreePathsResponse&)>&& callback);
17464d8e80dSLakshmi Yadlapati 
175*bb1c7d30SEd Tanous void getDbusObject(const std::string& path,
176*bb1c7d30SEd Tanous                    std::span<const std::string_view> interfaces,
1772b73119cSGeorge Liu                    std::function<void(const boost::system::error_code&,
178*bb1c7d30SEd Tanous                                       const MapperGetObject&)>&& callback);
1792b73119cSGeorge Liu 
180*bb1c7d30SEd Tanous void getAssociationEndPoints(
181a4eb761aSGeorge Liu     const std::string& path,
182a4eb761aSGeorge Liu     std::function<void(const boost::system::error_code&,
183*bb1c7d30SEd Tanous                        const MapperEndPoints&)>&& callback);
184a4eb761aSGeorge Liu 
185*bb1c7d30SEd Tanous void getManagedObjects(
186bd79bce8SPatrick Williams     const std::string& service, const sdbusplus::message::object_path& path,
187f5892d0dSGeorge Liu     std::function<void(const boost::system::error_code&,
188*bb1c7d30SEd Tanous                        const ManagedObjectType&)>&& callback);
1895b4aa86bSJames Feist } // namespace utility
1905b4aa86bSJames Feist } // namespace dbus
191