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