xref: /openbmc/bmcweb/include/dbus_utility.hpp (revision 262d7d4b)
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 
23 namespace dbus
24 {
25 
26 namespace utility
27 {
28 
29 using DbusVariantType =
30     std::variant<std::vector<std::tuple<std::string, std::string, std::string>>,
31                  std::vector<std::string>, std::vector<double>, std::string,
32                  int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
33                  uint16_t, uint8_t, bool>;
34 
35 using DBusPropertiesMap =
36     boost::container::flat_map<std::string, DbusVariantType>;
37 using DBusInteracesMap =
38     boost::container::flat_map<std::string, DBusPropertiesMap>;
39 using ManagedObjectType =
40     std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
41 
42 using ManagedItem = std::pair<
43     sdbusplus::message::object_path,
44     boost::container::flat_map<
45         std::string, boost::container::flat_map<std::string, DbusVariantType>>>;
46 
47 inline void escapePathForDbus(std::string& path)
48 {
49     const std::regex reg("[^A-Za-z0-9_/]");
50     std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
51 }
52 
53 // gets the string N strings deep into a path
54 // i.e.  /0th/1st/2nd/3rd
55 inline bool getNthStringFromPath(const std::string& path, int index,
56                                  std::string& result)
57 {
58     if (index < 0)
59     {
60         return false;
61     }
62 
63     std::filesystem::path p1(path);
64     int count = -1;
65     for (auto const& element : p1)
66     {
67         if (element.has_filename())
68         {
69             ++count;
70             if (count == index)
71             {
72                 result = element.stem().string();
73                 break;
74             }
75         }
76     }
77     if (count < index)
78     {
79         return false;
80     }
81 
82     return true;
83 }
84 
85 template <typename Callback>
86 inline void checkDbusPathExists(const std::string& path, Callback&& callback)
87 {
88     using GetObjectType =
89         std::vector<std::pair<std::string, std::vector<std::string>>>;
90 
91     crow::connections::systemBus->async_method_call(
92         [callback{std::move(callback)}](const boost::system::error_code ec,
93                                         const GetObjectType& objectNames) {
94             callback(!ec && objectNames.size() != 0);
95         },
96         "xyz.openbmc_project.ObjectMapper",
97         "/xyz/openbmc_project/object_mapper",
98         "xyz.openbmc_project.ObjectMapper", "GetObject", path,
99         std::array<std::string, 0>());
100 }
101 
102 } // namespace utility
103 } // namespace dbus
104