James Feist | 284a0f9 | 2018-04-05 15:28:16 -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 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 18 | #ifndef BOOST_COROUTINES_NO_DEPRECATION_WARNING |
| 19 | // users should define this if they directly include boost/asio/spawn.hpp, |
| 20 | // but by defining it here, warnings won't cause problems with a compile |
| 21 | #define BOOST_COROUTINES_NO_DEPRECATION_WARNING |
| 22 | #endif |
| 23 | |
Ed Tanous | 38ab5ec | 2020-08-17 17:08:20 -0700 | [diff] [blame] | 24 | #include <boost/asio/async_result.hpp> |
| 25 | #include <boost/asio/io_context.hpp> |
| 26 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 27 | #include <boost/asio/post.hpp> |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 28 | #include <boost/asio/spawn.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 29 | #include <boost/callable_traits.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 30 | #include <sdbusplus/asio/detail/async_send_handler.hpp> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 31 | #include <sdbusplus/message.hpp> |
| 32 | #include <sdbusplus/utility/read_into_tuple.hpp> |
| 33 | #include <sdbusplus/utility/type_traits.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 34 | |
| 35 | #include <chrono> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 36 | #include <string> |
James Feist | c14699f | 2019-06-04 14:11:48 -0700 | [diff] [blame] | 37 | #include <tuple> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 38 | |
| 39 | namespace sdbusplus |
| 40 | { |
| 41 | |
| 42 | namespace asio |
| 43 | { |
| 44 | |
| 45 | /// Root D-Bus IO object |
| 46 | /** |
| 47 | * A connection to a bus, through which messages may be sent or received. |
| 48 | */ |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 49 | class connection : public sdbusplus::bus_t |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
| 52 | // default to system bus |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 53 | connection(boost::asio::io_context& io) : |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 54 | sdbusplus::bus_t(sdbusplus::bus::new_default()), io_(io), socket(io_) |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 55 | { |
| 56 | socket.assign(get_fd()); |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 57 | read_wait(); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 58 | } |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 59 | connection(boost::asio::io_context& io, sd_bus* bus) : |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 60 | sdbusplus::bus_t(bus), io_(io), socket(io_) |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 61 | { |
| 62 | socket.assign(get_fd()); |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 63 | read_wait(); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 64 | } |
| 65 | ~connection() |
| 66 | { |
| 67 | // The FD will be closed by the socket object, so assign null to the |
| 68 | // sd_bus object to avoid a double close() Ignore return codes here, |
| 69 | // because there's nothing we can do about errors |
| 70 | socket.release(); |
| 71 | } |
| 72 | |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 73 | /** @brief Perform an asynchronous send of a message, executing the handler |
| 74 | * upon return and return |
| 75 | * |
| 76 | * @param[in] m - A message ready to send |
Ed Tanous | 6c97486 | 2022-08-31 13:21:32 -0700 | [diff] [blame] | 77 | * @param[in] token- The completion token to execute upon completion; |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 78 | * |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 79 | */ |
Ed Tanous | 6c97486 | 2022-08-31 13:21:32 -0700 | [diff] [blame] | 80 | template <typename CompletionToken> |
| 81 | inline auto async_send(message_t& m, CompletionToken&& token, |
| 82 | uint64_t timeout = 0) |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | 6c97486 | 2022-08-31 13:21:32 -0700 | [diff] [blame] | 84 | constexpr bool is_yield = |
| 85 | std::is_same_v<CompletionToken, boost::asio::yield_context>; |
| 86 | using return_t = std::conditional_t<is_yield, message_t, message_t&>; |
| 87 | using callback_t = void(boost::system::error_code, return_t); |
| 88 | return boost::asio::async_initiate<CompletionToken, callback_t>( |
| 89 | detail::async_send_handler(get(), m, timeout), token); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 92 | /** @brief Perform an asynchronous method call, with input parameter packing |
Konrad Sztyber | da0b3f1 | 2020-03-27 16:48:26 +0100 | [diff] [blame] | 93 | * and return value unpacking. |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 94 | * |
| 95 | * @param[in] handler - A function object that is to be called as a |
| 96 | * continuation for the async dbus method call. The |
| 97 | * arguments to parse on the return are deduced from |
| 98 | * the handler's signature and then passed in along |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 99 | * with an error code and optional message_t |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 100 | * @param[in] service - The service to call. |
| 101 | * @param[in] objpath - The object's path for the call. |
| 102 | * @param[in] interf - The object's interface to call. |
| 103 | * @param[in] method - The object's method to call. |
Konrad Sztyber | da0b3f1 | 2020-03-27 16:48:26 +0100 | [diff] [blame] | 104 | * @param[in] timeout - The timeout for the method call in usec (0 results |
| 105 | * in using the default value). |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 106 | * @param[in] a... - Optional parameters for the method call. |
| 107 | * |
| 108 | * @return immediate return of the internal handler registration. The |
| 109 | * result of the actual asynchronous call will get unpacked from |
| 110 | * the message and passed into the handler when the call is |
| 111 | * complete. |
| 112 | */ |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 113 | template <typename MessageHandler, typename... InputArgs> |
Konrad Sztyber | da0b3f1 | 2020-03-27 16:48:26 +0100 | [diff] [blame] | 114 | void async_method_call_timed(MessageHandler&& handler, |
| 115 | const std::string& service, |
| 116 | const std::string& objpath, |
| 117 | const std::string& interf, |
| 118 | const std::string& method, uint64_t timeout, |
| 119 | const InputArgs&... a) |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 120 | { |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 121 | using FunctionTuple = boost::callable_traits::args_t<MessageHandler>; |
Patrick Williams | b16ea98 | 2021-07-14 10:01:52 -0500 | [diff] [blame] | 122 | using FunctionTupleType = utility::decay_tuple_t<FunctionTuple>; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 123 | constexpr bool returnWithMsg = []() { |
Patrick Williams | 4b64623 | 2021-02-22 13:50:48 -0600 | [diff] [blame] | 124 | if constexpr ((std::tuple_size_v<FunctionTupleType>) > 1) |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 125 | { |
| 126 | return std::is_same_v< |
| 127 | std::tuple_element_t<1, FunctionTupleType>, |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 128 | sdbusplus::message_t>; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 129 | } |
| 130 | return false; |
| 131 | }(); |
Patrick Williams | b16ea98 | 2021-07-14 10:01:52 -0500 | [diff] [blame] | 132 | using UnpackType = utility::strip_first_n_args_t<returnWithMsg ? 2 : 1, |
| 133 | FunctionTupleType>; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 134 | auto applyHandler = [handler = std::forward<MessageHandler>(handler)]( |
| 135 | boost::system::error_code ec, |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 136 | message_t& r) mutable { |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 137 | UnpackType responseData; |
| 138 | if (!ec) |
| 139 | { |
Vernon Mauery | c077190 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 140 | try |
| 141 | { |
| 142 | utility::read_into_tuple(responseData, r); |
| 143 | } |
Brad Bishop | 5a6c790 | 2022-09-29 11:05:47 -0400 | [diff] [blame^] | 144 | catch (const std::exception&) |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 145 | { |
| 146 | // Set error code if not already set |
| 147 | ec = boost::system::errc::make_error_code( |
| 148 | boost::system::errc::invalid_argument); |
| 149 | } |
| 150 | } |
| 151 | // Note. Callback is called whether or not the unpack was |
Gunnar Mills | 31a4b13 | 2018-08-14 11:59:13 -0500 | [diff] [blame] | 152 | // successful to allow the user to implement their own handling |
Richard Marian Thomaiyar | 4212292 | 2019-07-01 21:31:04 +0530 | [diff] [blame] | 153 | if constexpr (returnWithMsg) |
| 154 | { |
| 155 | auto response = std::tuple_cat(std::make_tuple(ec), |
| 156 | std::forward_as_tuple(r), |
| 157 | std::move(responseData)); |
| 158 | std::apply(handler, response); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | auto response = std::tuple_cat(std::make_tuple(ec), |
| 163 | std::move(responseData)); |
| 164 | std::apply(handler, response); |
| 165 | } |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 166 | }; |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 167 | message_t m; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 168 | boost::system::error_code ec; |
| 169 | try |
| 170 | { |
| 171 | m = new_method_call(service.c_str(), objpath.c_str(), |
| 172 | interf.c_str(), method.c_str()); |
| 173 | m.append(a...); |
| 174 | } |
| 175 | catch (const exception::SdBusError& e) |
| 176 | { |
| 177 | ec = boost::system::errc::make_error_code( |
| 178 | static_cast<boost::system::errc::errc_t>(e.get_errno())); |
| 179 | applyHandler(ec, m); |
| 180 | return; |
| 181 | } |
Konrad Sztyber | da0b3f1 | 2020-03-27 16:48:26 +0100 | [diff] [blame] | 182 | async_send(m, std::forward<decltype(applyHandler)>(applyHandler), |
| 183 | timeout); |
| 184 | } |
| 185 | |
| 186 | /** @brief Perform an asynchronous method call, with input parameter packing |
| 187 | * and return value unpacking. Uses the default timeout value. |
| 188 | * |
| 189 | * @param[in] handler - A function object that is to be called as a |
| 190 | * continuation for the async dbus method call. The |
| 191 | * arguments to parse on the return are deduced from |
| 192 | * the handler's signature and then passed in along |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 193 | * with an error code and optional message_t |
Konrad Sztyber | da0b3f1 | 2020-03-27 16:48:26 +0100 | [diff] [blame] | 194 | * @param[in] service - The service to call. |
| 195 | * @param[in] objpath - The object's path for the call. |
| 196 | * @param[in] interf - The object's interface to call. |
| 197 | * @param[in] method - The object's method to call. |
| 198 | * @param[in] a... - Optional parameters for the method call. |
| 199 | * |
| 200 | * @return immediate return of the internal handler registration. The |
| 201 | * result of the actual asynchronous call will get unpacked from |
| 202 | * the message and passed into the handler when the call is |
| 203 | * complete. |
| 204 | */ |
| 205 | template <typename MessageHandler, typename... InputArgs> |
| 206 | void async_method_call(MessageHandler&& handler, const std::string& service, |
| 207 | const std::string& objpath, |
| 208 | const std::string& interf, const std::string& method, |
| 209 | const InputArgs&... a) |
| 210 | { |
| 211 | async_method_call_timed(std::forward<MessageHandler>(handler), service, |
| 212 | objpath, interf, method, 0, a...); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 215 | /** @brief Perform a yielding asynchronous method call, with input |
| 216 | * parameter packing and return value unpacking |
| 217 | * |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 218 | * @param[in] yield - A yield context to async block upon. |
| 219 | * @param[in] ec - an error code that will be set for any errors |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 220 | * @param[in] service - The service to call. |
| 221 | * @param[in] objpath - The object's path for the call. |
| 222 | * @param[in] interf - The object's interface to call. |
| 223 | * @param[in] method - The object's method to call. |
| 224 | * @param[in] a... - Optional parameters for the method call. |
| 225 | * |
| 226 | * @return Unpacked value of RetType |
| 227 | */ |
| 228 | template <typename... RetTypes, typename... InputArgs> |
| 229 | auto yield_method_call(boost::asio::yield_context yield, |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 230 | boost::system::error_code& ec, |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 231 | const std::string& service, |
| 232 | const std::string& objpath, |
| 233 | const std::string& interf, const std::string& method, |
| 234 | const InputArgs&... a) |
| 235 | { |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 236 | message_t m; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 237 | try |
| 238 | { |
| 239 | m = new_method_call(service.c_str(), objpath.c_str(), |
| 240 | interf.c_str(), method.c_str()); |
| 241 | m.append(a...); |
| 242 | } |
| 243 | catch (const exception::SdBusError& e) |
| 244 | { |
| 245 | ec = boost::system::errc::make_error_code( |
| 246 | static_cast<boost::system::errc::errc_t>(e.get_errno())); |
| 247 | } |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 248 | message_t r; |
Vernon Mauery | c079db4 | 2020-02-06 09:22:01 -0800 | [diff] [blame] | 249 | if (!ec) |
| 250 | { |
| 251 | r = async_send(m, yield[ec]); |
| 252 | } |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 253 | if constexpr (sizeof...(RetTypes) == 0) |
| 254 | { |
| 255 | // void return |
| 256 | return; |
| 257 | } |
| 258 | else if constexpr (sizeof...(RetTypes) == 1) |
| 259 | { |
Patrick Williams | 64f0122 | 2021-07-14 10:14:51 -0500 | [diff] [blame] | 260 | if constexpr (std::is_same_v<utility::first_type_t<RetTypes...>, |
| 261 | void>) |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 262 | { |
| 263 | return; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | // single item return |
Patrick Williams | b16ea98 | 2021-07-14 10:01:52 -0500 | [diff] [blame] | 268 | utility::first_type_t<RetTypes...> responseData{}; |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 269 | // before attempting to read, check ec and bail on error |
| 270 | if (ec) |
| 271 | { |
| 272 | return responseData; |
| 273 | } |
| 274 | try |
| 275 | { |
| 276 | r.read(responseData); |
| 277 | } |
Brad Bishop | 5a6c790 | 2022-09-29 11:05:47 -0400 | [diff] [blame^] | 278 | catch (const std::exception&) |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 279 | { |
| 280 | ec = boost::system::errc::make_error_code( |
| 281 | boost::system::errc::invalid_argument); |
| 282 | // responseData will be default-constructed... |
| 283 | } |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 284 | return responseData; |
| 285 | } |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | // tuple of things to return |
Patrick Williams | 5d4e4b2 | 2020-05-20 21:39:22 -0500 | [diff] [blame] | 290 | std::tuple<RetTypes...> responseData{}; |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 291 | // before attempting to read, check ec and bail on error |
| 292 | if (ec) |
| 293 | { |
| 294 | return responseData; |
| 295 | } |
| 296 | try |
| 297 | { |
| 298 | r.read(responseData); |
| 299 | } |
Brad Bishop | 5a6c790 | 2022-09-29 11:05:47 -0400 | [diff] [blame^] | 300 | catch (const std::exception&) |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 301 | { |
| 302 | ec = boost::system::errc::make_error_code( |
| 303 | boost::system::errc::invalid_argument); |
| 304 | // responseData will be default-constructed... |
| 305 | } |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 306 | return responseData; |
| 307 | } |
| 308 | } |
| 309 | |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 310 | boost::asio::io_context& get_io_context() |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 311 | { |
| 312 | return io_; |
| 313 | } |
| 314 | |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 315 | private: |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 316 | boost::asio::io_context& io_; |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 317 | boost::asio::posix::stream_descriptor socket; |
| 318 | |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 319 | void read_wait() |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 320 | { |
| 321 | socket.async_read_some( |
| 322 | boost::asio::null_buffers(), |
Vernon Mauery | 0c76546 | 2020-12-15 14:36:34 -0800 | [diff] [blame] | 323 | [&](const boost::system::error_code& ec, std::size_t) { |
| 324 | if (ec) |
| 325 | { |
| 326 | return; |
| 327 | } |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 328 | if (process_discard()) |
| 329 | { |
| 330 | read_immediate(); |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | read_wait(); |
| 335 | } |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 336 | }); |
| 337 | } |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 338 | void read_immediate() |
| 339 | { |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 340 | boost::asio::post(io_, [&] { |
James Feist | b575518 | 2018-07-16 10:30:08 -0700 | [diff] [blame] | 341 | if (process_discard()) |
| 342 | { |
| 343 | read_immediate(); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | read_wait(); |
| 348 | } |
| 349 | }); |
| 350 | } |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | } // namespace asio |
| 354 | |
| 355 | } // namespace sdbusplus |