openthread-br  0.3.0-72c0388
dbus_request.hpp
Go to the documentation of this file.
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 
34 #ifndef OTBR_LOG_TAG
35 #define OTBR_LOG_TAG "DBUS"
36 #endif
37 
38 #include "common/code_utils.hpp"
39 #include "common/logging.hpp"
40 
45 
46 namespace otbr {
47 namespace DBus {
48 
54 {
55 public:
63  DBusRequest(DBusConnection *aConnection, DBusMessage *aMessage)
64  : mConnection(aConnection)
65  , mMessage(aMessage)
66  {
67  dbus_message_ref(aMessage);
68  dbus_connection_ref(aConnection);
69  }
70 
77  DBusRequest(const DBusRequest &aOther)
78  : mConnection(nullptr)
79  , mMessage(nullptr)
80  {
81  CopyFrom(aOther);
82  }
83 
91  {
92  CopyFrom(aOther);
93  return *this;
94  }
95 
102  DBusMessage *GetMessage(void) { return mMessage; }
103 
110  DBusConnection *GetConnection(void) { return mConnection; }
111 
118  template <typename... Args> void Reply(const std::tuple<Args...> &aReply)
119  {
120  UniqueDBusMessage reply{dbus_message_new_method_return(mMessage)};
121 
122  VerifyOrExit(reply != nullptr);
124 
126  {
127  otbrLogDebug("Replied to %s.%s :", dbus_message_get_interface(mMessage), dbus_message_get_member(mMessage));
128  DumpDBusMessage(*reply);
129  }
130  dbus_connection_send(mConnection, reply.get(), nullptr);
131 
132  exit:
133  return;
134  }
135 
143  template <typename ResultType = int>
144  void ReplyOtResult(otError aError, Optional<ResultType> aResult = Optional<ResultType>())
145  {
146  UniqueDBusMessage reply{nullptr};
147 
148  if (aError == OT_ERROR_NONE)
149  {
150  otbrLogInfo("Replied to %s.%s with result %s", dbus_message_get_interface(mMessage),
151  dbus_message_get_member(mMessage), ConvertToDBusErrorName(aError));
152  }
153  else
154  {
155  otbrLogErr("Replied to %s.%s with result %s", dbus_message_get_interface(mMessage),
156  dbus_message_get_member(mMessage), ConvertToDBusErrorName(aError));
157  }
158 
159  if (aError == OT_ERROR_NONE)
160  {
161  reply = UniqueDBusMessage(dbus_message_new_method_return(mMessage));
162  }
163  else
164  {
165  reply = UniqueDBusMessage(dbus_message_new_error(mMessage, ConvertToDBusErrorName(aError), nullptr));
166  }
167  VerifyOrDie(reply != nullptr, "Failed to allocate message");
168 
169  if (aResult.HasValue())
170  {
171  DBusMessageIter replyIter;
172  otbrError error;
173 
174  dbus_message_iter_init_append(reply.get(), &replyIter);
175  error = DBusMessageEncode(&replyIter, *aResult);
176  VerifyOrDie(error == OTBR_ERROR_NONE, "Failed to encode result");
177  }
178 
179  dbus_connection_send(mConnection, reply.get(), nullptr);
180  }
181 
187  {
188  if (mConnection)
189  {
190  dbus_connection_unref(mConnection);
191  }
192  if (mMessage)
193  {
194  dbus_message_unref(mMessage);
195  }
196  }
197 
198 private:
199  void CopyFrom(const DBusRequest &aOther)
200  {
201  if (mMessage)
202  {
203  dbus_message_unref(mMessage);
204  }
205  if (mConnection)
206  {
207  dbus_connection_unref(mConnection);
208  }
209  mConnection = aOther.mConnection;
210  mMessage = aOther.mMessage;
211  dbus_message_ref(mMessage);
212  dbus_connection_ref(mConnection);
213  }
214 
215  DBusConnection *mConnection;
216  DBusMessage * mMessage;
217 };
218 
219 } // namespace DBus
220 } // namespace otbr
otbr::DBus::DumpDBusMessage
void DumpDBusMessage(DBusMessage &aMessage)
Definition: dbus_message_dump.cpp:170
dbus_message_helper.hpp
otbr::DBus::ConvertToDBusErrorName
const char * ConvertToDBusErrorName(otError aError)
Definition: error_helper.cpp:76
otbr::DBus::DBusRequest::Reply
void Reply(const std::tuple< Args... > &aReply)
Definition: dbus_request.hpp:118
otbr::DBus::DBusRequest
Definition: dbus_request.hpp:53
otbrLogGetLevel
otbrLogLevel otbrLogGetLevel(void)
Definition: logging.cpp:58
otbr::DBus::DBusRequest::ReplyOtResult
void ReplyOtResult(otError aError, Optional< ResultType > aResult=Optional< ResultType >())
Definition: dbus_request.hpp:144
dbus_resources.hpp
otbr::DBus::DBusRequest::GetConnection
DBusConnection * GetConnection(void)
Definition: dbus_request.hpp:110
error_helper.hpp
logging.hpp
code_utils.hpp
otbr::DBus::DBusRequest::operator=
DBusRequest & operator=(const DBusRequest &aOther)
Definition: dbus_request.hpp:90
otbrLogDebug
#define otbrLogDebug(...)
Definition: logging.hpp:246
dbus_message_dump.hpp
otbr::DBus::TupleToDBusMessage
otbrError TupleToDBusMessage(DBusMessage &aMessage, const std::tuple< FieldTypes... > &aValues)
Definition: dbus_message_helper.hpp:786
VerifyOrExit
#define VerifyOrExit(aCondition,...)
Definition: code_utils.hpp:110
otbrLogInfo
#define otbrLogInfo(...)
Definition: logging.hpp:245
OTBR_ERROR_NONE
@ OTBR_ERROR_NONE
No error.
Definition: types.hpp:72
otbr::DBus::DBusRequest::GetMessage
DBusMessage * GetMessage(void)
Definition: dbus_request.hpp:102
otbrError
otbrError
Definition: types.hpp:70
otbrLogErr
#define otbrLogErr(...)
Definition: logging.hpp:242
VerifyOrDie
#define VerifyOrDie(aCondition, aMessage)
Definition: code_utils.hpp:128
otbr::DBus::DBusRequest::DBusRequest
DBusRequest(const DBusRequest &aOther)
Definition: dbus_request.hpp:77
OTBR_LOG_DEBUG
@ OTBR_LOG_DEBUG
Debug level messages.
Definition: logging.hpp:60
Optional
Definition: code_utils.hpp:179
otbr::DBus::DBusRequest::~DBusRequest
~DBusRequest(void)
Definition: dbus_request.hpp:186
otbr::DBus::DBusRequest::DBusRequest
DBusRequest(DBusConnection *aConnection, DBusMessage *aMessage)
Definition: dbus_request.hpp:63