2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file dbus_message_out.cpp
18 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
20 * @brief DBus outgoing message wrapper class source file
23 #include <dbus_message_out.h>
25 #include <dpl/test/test_runner.h>
30 MessageOut::MessageOut(const std::string &destination,
31 const std::string &path,
32 const std::string &interface,
33 const std::string &method)
35 m_message = dbus_message_new_method_call(destination.c_str(),
39 RUNNER_ASSERT_MSG(nullptr != m_message,
40 "Failed to create new method call. Not enough memory");
43 MessageOut::MessageOut(MessageOut &&other)
44 : m_message(other.m_message)
46 other.m_message = nullptr;
49 MessageOut::~MessageOut()
51 if (m_message != nullptr)
52 dbus_message_unref(m_message);
55 DBusMessage* MessageOut::getMessage() const
60 void MessageOut::append(bool b)
63 dbus_message_iter_init_append(m_message, &iter);
65 dbus_bool_t bArg = b ? TRUE : FALSE;
66 dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
69 RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic boolean. Not enough memory");
72 void MessageOut::append(const char *cstr)
75 dbus_message_iter_init_append(m_message, &iter);
77 dbus_bool_t ret = dbus_message_iter_append_basic(&iter,
80 RUNNER_ASSERT_MSG(ret != FALSE, "Failed to append basic string. Not enough memory");
83 void MessageOut::append(const std::string &str)
88 void MessageOut::append(const std::vector<std::string> &strs)
91 dbus_message_iter_init_append(m_message, &iter);
93 DBusMessageIter subIter;
94 dbus_bool_t ret = dbus_message_iter_open_container(&iter,
96 DBUS_TYPE_STRING_AS_STRING,
98 RUNNER_ASSERT_MSG(ret != FALSE, "Failed to open container. Not enough memory");
99 for (const auto &str : strs) {
100 const char *cstr = str.c_str();
101 ret = dbus_message_iter_append_basic(&subIter, DBUS_TYPE_STRING, &cstr);
103 dbus_message_iter_abandon_container(&iter, &subIter);
104 RUNNER_FAIL_MSG("Failed to append basic string. Not enough memory");
107 ret = dbus_message_iter_close_container(&iter, &subIter);
108 RUNNER_ASSERT_MSG(ret != FALSE, "Failed to close container. Not enough memory");