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_connection.cpp
18 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
20 * @brief DBus connection wrapper class source file
23 #include <dbus_connection.h>
25 #include <dpl/test/test_runner.h>
30 Connection::Connection(DBusBusType busType, bool busPrivate)
31 : m_busPrivate(busPrivate)
34 dbus_error_init(&error);
35 ErrorPtr errorPtr(&error);
38 m_connection = dbus_bus_get_private(busType, &error);
40 m_connection = dbus_bus_get(busType, &error);
41 RUNNER_ASSERT_MSG(m_connection != nullptr,
42 "Failed to open connection on "
43 << (busPrivate ? "private" : "public") << " bus."
44 << " Error: " << error.message);
45 dbus_connection_set_exit_on_disconnect(m_connection, FALSE);
48 Connection::~Connection()
51 dbus_connection_close(m_connection);
52 dbus_connection_unref(m_connection);
55 void Connection::addMatch(const std::string &rule)
58 dbus_error_init(&error);
59 ErrorPtr errorPtr(&error);
61 dbus_bus_add_match(m_connection, rule.c_str(), &error);
62 RUNNER_ASSERT_MSG(dbus_error_is_set(&error) != TRUE, "Failed to add match."
63 << " Rule: " << rule << ";"
64 << " Error: " << error.message);
67 void Connection::addFilter(DBusHandleMessageFunction handleMessageFunction,
69 DBusFreeFunction freeDataFunction)
71 if (freeDataFunction == nullptr)
72 freeDataFunction = [](void*)->void {};
74 dbus_bool_t ret = dbus_connection_add_filter(m_connection,
75 handleMessageFunction,
78 RUNNER_ASSERT_MSG(ret == TRUE, "Failed to add filter. Not enough memory");
81 void Connection::readWriteDispatch()
83 dbus_bool_t ret = dbus_connection_read_write_dispatch(m_connection, -1);
84 RUNNER_ASSERT_MSG(ret == TRUE, "Failed to read write dispatch. Disconnect message has been processed");
87 void Connection::flush()
89 dbus_connection_flush(m_connection);
92 void Connection::requestName(const std::string &name)
95 dbus_error_init(&error);
96 ErrorPtr errorPtr(&error);
98 int ret = dbus_bus_request_name(m_connection,
100 DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE,
104 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
105 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
107 case DBUS_REQUEST_NAME_REPLY_EXISTS:
108 RUNNER_FAIL_MSG("Failed to request name."
109 << " Name: " << name << ";"
110 << " Owner did not specified DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag");
112 RUNNER_FAIL_MSG("Failed to request name."
113 << " Name: " << name << ";"
114 << " Error: " << error.message);
115 default: // DBUS_REQUEST_NAME_REPLY_IN_QUEUE
116 RUNNER_FAIL_MSG("Should not happen");
120 MessageIn Connection::sendWithReplyAndBlock(const MessageOut &messageOut)
123 dbus_error_init(&error);
124 ErrorPtr errorPtr(&error);
126 DBusMessage *messageRecv = dbus_connection_send_with_reply_and_block(m_connection,
127 messageOut.getMessage(),
130 RUNNER_ASSERT_MSG(messageRecv != nullptr, "Failed to send with reply and block. "
131 << "Error: " << error.message);
132 return MessageIn(messageRecv);