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