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 | |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 20 | #include <filesystem> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 21 | #include <regex> |
| 22 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 23 | namespace dbus |
| 24 | { |
| 25 | |
| 26 | namespace utility |
| 27 | { |
| 28 | |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 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>; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 34 | |
Zbigniew Kurzynski | 755a33c | 2020-02-28 14:06:37 +0100 | [diff] [blame] | 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>>; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 41 | |
James Feist | 73df0db | 2019-03-25 15:29:35 -0700 | [diff] [blame] | 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 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 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 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 58 | if (index < 0) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 59 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 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()) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 68 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 69 | ++count; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 70 | if (count == index) |
| 71 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 72 | result = element.stem().string(); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (count < index) |
| 78 | { |
| 79 | return false; |
| 80 | } |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 81 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 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, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 93 | const GetObjectType& objectNames) { |
| 94 | callback(!ec && objectNames.size() != 0); |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 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 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 102 | } // namespace utility |
| 103 | } // namespace dbus |