2 * Copyright (c) 2019, The OpenThread Authors.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * This file includes definitions for a d-bus request.
34 #include "common/logging.hpp"
36 #include "dbus/common/dbus_message_dump.hpp"
37 #include "dbus/common/dbus_message_helper.hpp"
38 #include "dbus/common/dbus_resources.hpp"
39 #include "dbus/server/error_helper.hpp"
45 * This class represents a incoming call for a d-bus method.
52 * The constructor of dbus request.
54 * @param[in] aConnection The dbus connection.
55 * @param[in] aMessage The incoming dbus message.
58 DBusRequest(DBusConnection *aConnection, DBusMessage *aMessage)
59 : mConnection(aConnection)
62 dbus_message_ref(aMessage);
63 dbus_connection_ref(aConnection);
67 * The copy constructor of dbus request.
69 * @param[in] aOther The object to be copied from.
72 DBusRequest(const DBusRequest &aOther)
73 : mConnection(nullptr)
80 * The assignment operator of dbus request.
82 * @param[in] aOther The object to be copied from.
85 DBusRequest &operator=(const DBusRequest &aOther)
92 * This method returns the message sent to call the d-bus method.
94 * @returns The dbus message.
97 DBusMessage *GetMessage(void) { return mMessage; }
100 * This method returns underlying d-bus connection.
102 * @returns The dbus connection.
105 DBusConnection *GetConnection(void) { return mConnection; }
108 * This method replies to the d-bus method call.
110 * @param[in] aReply The tuple to be sent.
113 template <typename... Args> void Reply(const std::tuple<Args...> &aReply)
115 UniqueDBusMessage reply{dbus_message_new_method_return(mMessage)};
117 VerifyOrExit(reply != nullptr);
118 VerifyOrExit(otbr::DBus::TupleToDBusMessage(*reply, aReply) == OTBR_ERROR_NONE);
120 if (otbrLogGetLevel() >= OTBR_LOG_DEBUG)
122 otbrLog(OTBR_LOG_DEBUG, "Replied to %s.%s :", dbus_message_get_interface(mMessage),
123 dbus_message_get_member(mMessage));
124 DumpDBusMessage(*reply);
126 dbus_connection_send(mConnection, reply.get(), nullptr);
133 * This method replies an otError to the d-bus method call.
135 * @param[in] aError The error to be sent.
138 void ReplyOtResult(otError aError)
140 UniqueDBusMessage reply{nullptr};
141 auto logLevel = (aError == OT_ERROR_NONE) ? OTBR_LOG_INFO : OTBR_LOG_ERR;
143 otbrLog(logLevel, "Replied to %s.%s with result %s", dbus_message_get_interface(mMessage),
144 dbus_message_get_member(mMessage), ConvertToDBusErrorName(aError));
145 if (aError == OT_ERROR_NONE)
147 reply = UniqueDBusMessage(dbus_message_new_method_return(mMessage));
151 reply = UniqueDBusMessage(dbus_message_new_error(mMessage, ConvertToDBusErrorName(aError), nullptr));
154 VerifyOrExit(reply != nullptr);
155 dbus_connection_send(mConnection, reply.get(), nullptr);
162 * The destructor of DBusRequest
169 dbus_connection_unref(mConnection);
173 dbus_message_unref(mMessage);
178 void CopyFrom(const DBusRequest &aOther)
182 dbus_message_unref(mMessage);
186 dbus_connection_unref(mConnection);
188 mConnection = aOther.mConnection;
189 mMessage = aOther.mMessage;
190 dbus_message_ref(mMessage);
191 dbus_connection_ref(mConnection);
194 DBusConnection *mConnection;
195 DBusMessage * mMessage;