xref: /openbmc/bmcweb/include/dbus_utility.hpp (revision 81d523a7)
1 /*
2  // Copyright (c) 2018 Intel Corporation
3  //
4  // Licensed under the Apache License, Version 2.0 (the "License");
5  // you may not use this file except in compliance with the License.
6  // You may obtain a copy of the License at
7  //
8  //      http://www.apache.org/licenses/LICENSE-2.0
9  //
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  */
16 #pragma once
17 
18 #include <sdbusplus/message.hpp>
19 
20 #include <filesystem>
21 #include <regex>
22 #include <variant>
23 
24 namespace dbus
25 {
26 
27 namespace utility
28 {
29 
30 // clang-format off
31 using DbusVariantType = std::variant<
32     std::vector<std::tuple<std::string, std::string, std::string>>,
33     std::vector<std::string>,
34     std::vector<double>,
35     std::string,
36     int64_t,
37     uint64_t,
38     double,
39     int32_t,
40     uint32_t,
41     int16_t,
42     uint16_t,
43     uint8_t,
44     bool,
45     sdbusplus::message::unix_fd,
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, std::string, double, uint64_t>>>,
50     std::vector<std::tuple<std::string, std::string>>,
51     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
52     std::vector<std::tuple<uint32_t, size_t>>,
53     std::vector<std::tuple<sdbusplus::message::object_path, std::string,
54                            std::string, std::string>>
55  >;
56 
57 // clang-format on
58 using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
59 using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
60 using ManagedObjectType =
61     std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
62 
63 using ManagedItem = std::pair<
64     sdbusplus::message::object_path,
65     std::vector<std::pair<
66         std::string, std::vector<std::pair<std::string, DbusVariantType>>>>>;
67 
68 // Map of service name to list of interfaces
69 using MapperServiceMap =
70     std::vector<std::pair<std::string, std::vector<std::string>>>;
71 
72 // Map of object paths to MapperServiceMaps
73 using MapperGetSubTreeResponse =
74     std::vector<std::pair<std::string, MapperServiceMap>>;
75 
76 using MapperGetObject =
77     std::vector<std::pair<std::string, std::vector<std::string>>>;
78 
79 using MapperGetAncestorsResponse = std::vector<
80     std::pair<std::string,
81               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
82 
83 using MapperGetSubTreePathsResponse = std::vector<std::string>;
84 
85 inline void escapePathForDbus(std::string& path)
86 {
87     const std::regex reg("[^A-Za-z0-9_/]");
88     std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
89 }
90 
91 // gets the string N strings deep into a path
92 // i.e.  /0th/1st/2nd/3rd
93 inline bool getNthStringFromPath(const std::string& path, int index,
94                                  std::string& result)
95 {
96     if (index < 0)
97     {
98         return false;
99     }
100 
101     std::filesystem::path p1(path);
102     int count = -1;
103     for (auto const& element : p1)
104     {
105         if (element.has_filename())
106         {
107             ++count;
108             if (count == index)
109             {
110                 result = element.stem().string();
111                 break;
112             }
113         }
114     }
115     return count >= index;
116 }
117 
118 template <typename Callback>
119 inline void checkDbusPathExists(const std::string& path, Callback&& callback)
120 {
121     crow::connections::systemBus->async_method_call(
122         [callback{std::forward<Callback>(callback)}](
123             const boost::system::error_code ec,
124             const dbus::utility::MapperGetObject& objectNames) {
125         callback(!ec && !objectNames.empty());
126         },
127         "xyz.openbmc_project.ObjectMapper",
128         "/xyz/openbmc_project/object_mapper",
129         "xyz.openbmc_project.ObjectMapper", "GetObject", path,
130         std::array<std::string, 0>());
131 }
132 
133 } // namespace utility
134 } // namespace dbus
135