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