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 | |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 18 | #include "dbus_singleton.hpp" |
| 19 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 20 | #include <boost/system/error_code.hpp> // IWYU pragma: keep |
| 21 | #include <sdbusplus/message/native_types.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 22 | |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 23 | #include <array> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 24 | #include <cstddef> |
| 25 | #include <cstdint> |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 26 | #include <filesystem> |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 27 | #include <functional> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | #include <regex> |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 29 | #include <span> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 30 | #include <sstream> |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 31 | #include <string> |
| 32 | #include <tuple> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 33 | #include <utility> |
Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 34 | #include <variant> |
Nan Zhou | 80d37e7 | 2022-06-21 17:46:14 +0000 | [diff] [blame] | 35 | #include <vector> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 36 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 37 | // IWYU pragma: no_include <stddef.h> |
| 38 | // IWYU pragma: no_include <stdint.h> |
| 39 | // IWYU pragma: no_include <boost/system/detail/error_code.hpp> |
| 40 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 41 | namespace dbus |
| 42 | { |
| 43 | |
| 44 | namespace utility |
| 45 | { |
| 46 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 47 | // clang-format off |
Ed Tanous | 4068129 | 2022-02-22 10:11:43 -0800 | [diff] [blame] | 48 | using DbusVariantType = std::variant< |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 49 | std::vector<std::tuple<std::string, std::string, std::string>>, |
| 50 | std::vector<std::string>, |
| 51 | std::vector<double>, |
| 52 | std::string, |
| 53 | int64_t, |
| 54 | uint64_t, |
| 55 | double, |
| 56 | int32_t, |
| 57 | uint32_t, |
| 58 | int16_t, |
| 59 | uint16_t, |
| 60 | uint8_t, |
| 61 | bool, |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 62 | sdbusplus::message::unix_fd, |
| 63 | std::vector<uint32_t>, |
| 64 | std::vector<uint16_t>, |
| 65 | sdbusplus::message::object_path, |
| 66 | std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>, |
| 67 | std::vector<std::tuple<std::string, std::string>>, |
| 68 | std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>, |
| 69 | std::vector<std::tuple<uint32_t, size_t>>, |
| 70 | std::vector<std::tuple<sdbusplus::message::object_path, std::string, |
| 71 | std::string, std::string>> |
| 72 | >; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 73 | |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 74 | // clang-format on |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 75 | using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>; |
| 76 | using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>; |
Zbigniew Kurzynski | 755a33c | 2020-02-28 14:06:37 +0100 | [diff] [blame] | 77 | using ManagedObjectType = |
| 78 | std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 79 | |
Shantappa Teekappanavar | 5df6eda | 2022-01-18 12:29:28 -0600 | [diff] [blame] | 80 | // Map of service name to list of interfaces |
| 81 | using MapperServiceMap = |
| 82 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 83 | |
| 84 | // Map of object paths to MapperServiceMaps |
| 85 | using MapperGetSubTreeResponse = |
| 86 | std::vector<std::pair<std::string, MapperServiceMap>>; |
| 87 | |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 88 | using MapperGetObject = |
| 89 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 90 | |
| 91 | using MapperGetAncestorsResponse = std::vector< |
| 92 | std::pair<std::string, |
| 93 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 94 | |
| 95 | using MapperGetSubTreePathsResponse = std::vector<std::string>; |
| 96 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 97 | inline void escapePathForDbus(std::string& path) |
| 98 | { |
| 99 | const std::regex reg("[^A-Za-z0-9_/]"); |
| 100 | std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_"); |
| 101 | } |
| 102 | |
| 103 | // gets the string N strings deep into a path |
| 104 | // i.e. /0th/1st/2nd/3rd |
| 105 | inline bool getNthStringFromPath(const std::string& path, int index, |
| 106 | std::string& result) |
| 107 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 108 | if (index < 0) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 109 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | std::filesystem::path p1(path); |
| 114 | int count = -1; |
| 115 | for (auto const& element : p1) |
| 116 | { |
| 117 | if (element.has_filename()) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 118 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 119 | ++count; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 120 | if (count == index) |
| 121 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 122 | result = element.stem().string(); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
Ed Tanous | dcf2ebc | 2022-01-25 10:07:45 -0800 | [diff] [blame] | 127 | return count >= index; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 130 | template <typename Callback> |
| 131 | inline void checkDbusPathExists(const std::string& path, Callback&& callback) |
| 132 | { |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 133 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 134 | [callback{std::forward<Callback>(callback)}]( |
| 135 | const boost::system::error_code ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 136 | const dbus::utility::MapperGetObject& objectNames) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 137 | callback(!ec && !objectNames.empty()); |
Ratan Gupta | 22c3371 | 2019-05-03 21:50:28 +0530 | [diff] [blame] | 138 | }, |
| 139 | "xyz.openbmc_project.ObjectMapper", |
| 140 | "/xyz/openbmc_project/object_mapper", |
| 141 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, |
| 142 | std::array<std::string, 0>()); |
| 143 | } |
| 144 | |
George Liu | 2138483 | 2022-11-09 11:21:15 +0800 | [diff] [blame] | 145 | inline void |
| 146 | getSubTree(const std::string& path, std::span<std::string> interfaces, |
| 147 | std::function<void(const boost::system::error_code&, |
| 148 | const MapperGetSubTreeResponse&)>&& callback) |
| 149 | { |
| 150 | crow::connections::systemBus->async_method_call( |
| 151 | [callback{std::move(callback)}]( |
| 152 | const boost::system::error_code ec, |
| 153 | const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); }, |
| 154 | "xyz.openbmc_project.ObjectMapper", |
| 155 | "/xyz/openbmc_project/object_mapper", |
| 156 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces); |
| 157 | } |
| 158 | |
| 159 | inline void getSubTreePaths( |
| 160 | const std::string& path, std::span<std::string> interfaces, |
| 161 | std::function<void(const boost::system::error_code&, |
| 162 | const MapperGetSubTreePathsResponse&)>&& callback) |
| 163 | { |
| 164 | crow::connections::systemBus->async_method_call( |
| 165 | [callback{std::move(callback)}]( |
| 166 | const boost::system::error_code ec, |
| 167 | const MapperGetSubTreePathsResponse& subtreePaths) { |
| 168 | callback(ec, subtreePaths); |
| 169 | }, |
| 170 | "xyz.openbmc_project.ObjectMapper", |
| 171 | "/xyz/openbmc_project/object_mapper", |
| 172 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, 0, |
| 173 | interfaces); |
| 174 | } |
| 175 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 176 | } // namespace utility |
| 177 | } // namespace dbus |