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> |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 19 | #include <sdbusplus/utility/dedup_variant.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 20 | |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 21 | #include <filesystem> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 22 | #include <regex> |
| 23 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 24 | namespace dbus |
| 25 | { |
| 26 | |
| 27 | namespace utility |
| 28 | { |
| 29 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 30 | // clang-format off |
| 31 | using DbusVariantType = sdbusplus::utility::dedup_variant_t< |
| 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 | size_t, |
| 46 | sdbusplus::message::unix_fd, |
| 47 | std::vector<uint32_t>, |
| 48 | std::vector<uint16_t>, |
| 49 | sdbusplus::message::object_path, |
| 50 | std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>, |
| 51 | std::vector<std::tuple<std::string, std::string>>, |
| 52 | std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>, |
| 53 | std::vector<std::tuple<uint32_t, size_t>>, |
| 54 | std::vector<std::tuple<sdbusplus::message::object_path, std::string, |
| 55 | std::string, std::string>> |
| 56 | >; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 57 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 58 | // clang-format on |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 59 | using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; |
| 60 | using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>; |
Zbigniew Kurzynski | 755a33c | 2020-02-28 14:06:37 +0100 | [diff] [blame] | 61 | using ManagedObjectType = |
| 62 | std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 63 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 64 | using ManagedItem = std::pair< |
| 65 | sdbusplus::message::object_path, |
| 66 | boost::container::flat_map< |
| 67 | std::string, boost::container::flat_map<std::string, DbusVariantType>>>; |
| 68 | |
Shantappa Teekappanavar | 5df6eda | 2022-01-18 12:29:28 -0600 | [diff] [blame] | 69 | // Map of service name to list of interfaces |
| 70 | using MapperServiceMap = |
| 71 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 72 | |
| 73 | // Map of object paths to MapperServiceMaps |
| 74 | using MapperGetSubTreeResponse = |
| 75 | std::vector<std::pair<std::string, MapperServiceMap>>; |
| 76 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 77 | inline void escapePathForDbus(std::string& path) |
| 78 | { |
| 79 | const std::regex reg("[^A-Za-z0-9_/]"); |
| 80 | std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_"); |
| 81 | } |
| 82 | |
| 83 | // gets the string N strings deep into a path |
| 84 | // i.e. /0th/1st/2nd/3rd |
| 85 | inline bool getNthStringFromPath(const std::string& path, int index, |
| 86 | std::string& result) |
| 87 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 88 | if (index < 0) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 89 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | std::filesystem::path p1(path); |
| 94 | int count = -1; |
| 95 | for (auto const& element : p1) |
| 96 | { |
| 97 | if (element.has_filename()) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 98 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 99 | ++count; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 100 | if (count == index) |
| 101 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 102 | result = element.stem().string(); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | if (count < index) |
| 108 | { |
| 109 | return false; |
| 110 | } |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 111 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 115 | template <typename Callback> |
| 116 | inline void checkDbusPathExists(const std::string& path, Callback&& callback) |
| 117 | { |
| 118 | using GetObjectType = |
| 119 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 120 | |
| 121 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 122 | [callback{std::forward<Callback>(callback)}]( |
| 123 | const boost::system::error_code ec, |
| 124 | const GetObjectType& objectNames) { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 125 | callback(!ec && objectNames.size() != 0); |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 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 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 133 | } // namespace utility |
| 134 | } // namespace dbus |