Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / dbus / server / dbus_request.hpp
1 /*
2  *    Copyright (c) 2019, The OpenThread Authors.
3  *    All rights reserved.
4  *
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.
15  *
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.
27  */
28
29 /**
30  * @file
31  * This file includes definitions for a d-bus request.
32  */
33
34 #include "common/logging.hpp"
35
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"
40
41 namespace otbr {
42 namespace DBus {
43
44 /**
45  * This class represents a incoming call for a d-bus method.
46  *
47  */
48 class DBusRequest
49 {
50 public:
51     /**
52      * The constructor of dbus request.
53      *
54      * @param[in]   aConnection   The dbus connection.
55      * @param[in]   aMessage      The incoming dbus message.
56      *
57      */
58     DBusRequest(DBusConnection *aConnection, DBusMessage *aMessage)
59         : mConnection(aConnection)
60         , mMessage(aMessage)
61     {
62         dbus_message_ref(aMessage);
63         dbus_connection_ref(aConnection);
64     }
65
66     /**
67      * The copy constructor of dbus request.
68      *
69      * @param[in]   aOther    The object to be copied from.
70      *
71      */
72     DBusRequest(const DBusRequest &aOther)
73         : mConnection(nullptr)
74         , mMessage(nullptr)
75     {
76         CopyFrom(aOther);
77     }
78
79     /**
80      * The assignment operator of dbus request.
81      *
82      * @param[in]   aOther    The object to be copied from.
83      *
84      */
85     DBusRequest &operator=(const DBusRequest &aOther)
86     {
87         CopyFrom(aOther);
88         return *this;
89     }
90
91     /**
92      * This method returns the message sent to call the d-bus method.
93      *
94      * @returns   The dbus message.
95      *
96      */
97     DBusMessage *GetMessage(void) { return mMessage; }
98
99     /**
100      * This method returns underlying d-bus connection.
101      *
102      * @returns   The dbus connection.
103      *
104      */
105     DBusConnection *GetConnection(void) { return mConnection; }
106
107     /**
108      * This method replies to the d-bus method call.
109      *
110      * @param[in] aReply  The tuple to be sent.
111      *
112      */
113     template <typename... Args> void Reply(const std::tuple<Args...> &aReply)
114     {
115         UniqueDBusMessage reply{dbus_message_new_method_return(mMessage)};
116
117         VerifyOrExit(reply != nullptr);
118         VerifyOrExit(otbr::DBus::TupleToDBusMessage(*reply, aReply) == OTBR_ERROR_NONE);
119
120         if (otbrLogGetLevel() >= OTBR_LOG_DEBUG)
121         {
122             otbrLog(OTBR_LOG_DEBUG, "Replied to %s.%s :", dbus_message_get_interface(mMessage),
123                     dbus_message_get_member(mMessage));
124             DumpDBusMessage(*reply);
125         }
126         dbus_connection_send(mConnection, reply.get(), nullptr);
127
128     exit:
129         return;
130     }
131
132     /**
133      * This method replies an otError to the d-bus method call.
134      *
135      * @param[in] aError  The error to be sent.
136      *
137      */
138     void ReplyOtResult(otError aError)
139     {
140         UniqueDBusMessage reply{nullptr};
141         auto              logLevel = (aError == OT_ERROR_NONE) ? OTBR_LOG_INFO : OTBR_LOG_ERR;
142
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)
146         {
147             reply = UniqueDBusMessage(dbus_message_new_method_return(mMessage));
148         }
149         else
150         {
151             reply = UniqueDBusMessage(dbus_message_new_error(mMessage, ConvertToDBusErrorName(aError), nullptr));
152         }
153
154         VerifyOrExit(reply != nullptr);
155         dbus_connection_send(mConnection, reply.get(), nullptr);
156
157     exit:
158         return;
159     }
160
161     /**
162      * The destructor of DBusRequest
163      *
164      */
165     ~DBusRequest(void)
166     {
167         if (mConnection)
168         {
169             dbus_connection_unref(mConnection);
170         }
171         if (mMessage)
172         {
173             dbus_message_unref(mMessage);
174         }
175     }
176
177 private:
178     void CopyFrom(const DBusRequest &aOther)
179     {
180         if (mMessage)
181         {
182             dbus_message_unref(mMessage);
183         }
184         if (mConnection)
185         {
186             dbus_connection_unref(mConnection);
187         }
188         mConnection = aOther.mConnection;
189         mMessage    = aOther.mMessage;
190         dbus_message_ref(mMessage);
191         dbus_connection_ref(mConnection);
192     }
193
194     DBusConnection *mConnection;
195     DBusMessage *   mMessage;
196 };
197
198 } // namespace DBus
199 } // namespace otbr