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