From 11ed631d4c9bbb4ab59d66904fa8a7d9d33afc97 Mon Sep 17 00:00:00 2001 From: Marcin Niesluchowski Date: Tue, 16 Dec 2014 16:40:06 +0100 Subject: [PATCH] Add dbus connection wrapper class Change-Id: I494354b5d0c262abd90b3c20d36b3db5f1efef93 --- tests/common/CMakeLists.txt | 1 + tests/common/dbus_connection.cpp | 135 +++++++++++++++++++++++++++++++++++++++ tests/common/dbus_connection.h | 64 +++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 tests/common/dbus_connection.cpp create mode 100644 tests/common/dbus_connection.h diff --git a/tests/common/CMakeLists.txt b/tests/common/CMakeLists.txt index 7db2584..dcf486f 100644 --- a/tests/common/CMakeLists.txt +++ b/tests/common/CMakeLists.txt @@ -15,6 +15,7 @@ SET(COMMON_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/tests/common/access_provider.cpp ${PROJECT_SOURCE_DIR}/tests/common/smack_access.cpp ${PROJECT_SOURCE_DIR}/tests/common/dbus_access.cpp + ${PROJECT_SOURCE_DIR}/tests/common/dbus_connection.cpp ${PROJECT_SOURCE_DIR}/tests/common/dbus_message_in.cpp ${PROJECT_SOURCE_DIR}/tests/common/dbus_message_out.cpp ${PROJECT_SOURCE_DIR}/tests/common/memory.cpp diff --git a/tests/common/dbus_connection.cpp b/tests/common/dbus_connection.cpp new file mode 100644 index 0000000..424bd0e --- /dev/null +++ b/tests/common/dbus_connection.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file dbus_connection.cpp + * @author Marcin Niesluchowski (m.niesluchow@samsung.com) + * @version 1.0 + * @brief DBus connection wrapper class source file + */ + +#include + +#include + +namespace DBus +{ + +Connection::Connection(DBusBusType busType, bool busPrivate) + : m_busPrivate(busPrivate) +{ + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + + if (busPrivate) + m_connection = dbus_bus_get_private(busType, &error); + else + m_connection = dbus_bus_get(busType, &error); + RUNNER_ASSERT_MSG(m_connection != nullptr, + "Failed to open connection on " + << (busPrivate ? "private" : "public") << " bus." + << " Error: " << error.message); + dbus_connection_set_exit_on_disconnect(m_connection, FALSE); +} + +Connection::~Connection() +{ + if (m_busPrivate) + dbus_connection_close(m_connection); + dbus_connection_unref(m_connection); +} + +void Connection::addMatch(const std::string &rule) +{ + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + + dbus_bus_add_match(m_connection, rule.c_str(), &error); + RUNNER_ASSERT_MSG(dbus_error_is_set(&error) != TRUE, "Failed to add match." + << " Rule: " << rule << ";" + << " Error: " << error.message); +} + +void Connection::addFilter(DBusHandleMessageFunction handleMessageFunction, + void *userData, + DBusFreeFunction freeDataFunction) +{ + if (freeDataFunction == nullptr) + freeDataFunction = [](void*)->void {}; + + dbus_bool_t ret = dbus_connection_add_filter(m_connection, + handleMessageFunction, + userData, + freeDataFunction); + RUNNER_ASSERT_MSG(ret == TRUE, "Failed to add filter. Not enough memory"); +} + +void Connection::readWriteDispatch() +{ + dbus_bool_t ret = dbus_connection_read_write_dispatch(m_connection, -1); + RUNNER_ASSERT_MSG(ret == TRUE, "Failed to read write dispatch. Disconnect message has been processed"); +} + +void Connection::flush() +{ + dbus_connection_flush(m_connection); +} + +void Connection::requestName(const std::string &name) +{ + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + + int ret = dbus_bus_request_name(m_connection, + name.c_str(), + DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE, + &error); + switch (ret) + { + case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER: + case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER: + return; + case DBUS_REQUEST_NAME_REPLY_EXISTS: + RUNNER_FAIL_MSG("Failed to request name." + << " Name: " << name << ";" + << " Owner did not specified DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag"); + case -1: + RUNNER_FAIL_MSG("Failed to request name." + << " Name: " << name << ";" + << " Error: " << error.message); + default: // DBUS_REQUEST_NAME_REPLY_IN_QUEUE + RUNNER_FAIL_MSG("Should not happen"); + } +} + +MessageIn Connection::sendWithReplyAndBlock(const MessageOut &messageOut) +{ + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + + DBusMessage *messageRecv = dbus_connection_send_with_reply_and_block(m_connection, + messageOut.getMessage(), + -1, + &error); + RUNNER_ASSERT_MSG(messageRecv != nullptr, "Failed to send with reply and block. " + << "Error: " << error.message); + return MessageIn(messageRecv); +} + +} // namespace DBus diff --git a/tests/common/dbus_connection.h b/tests/common/dbus_connection.h new file mode 100644 index 0000000..f2db5ac --- /dev/null +++ b/tests/common/dbus_connection.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file dbus_connection.h + * @author Marcin Niesluchowski (m.niesluchow@samsung.com) + * @version 1.0 + * @brief DBus connection wrapper class header + */ + +#ifndef COMMON_DBUS_CONNECTION_H +#define COMMON_DBUS_CONNECTION_H + +#include + +#include +#include +#include + +#include + +namespace DBus +{ + +DEFINE_SMARTPTR(dbus_error_free, DBusError, ErrorPtr); + +class Connection +{ +public: + Connection(DBusBusType busType, bool privateGet); + Connection(const Connection &other) = delete; + ~Connection(); + + Connection& operator=(const Connection &other) = delete; + + void addMatch(const std::string &rule); + void addFilter(DBusHandleMessageFunction handleMessageFunction, + void *userData, + DBusFreeFunction freeDataFunction = nullptr); + void readWriteDispatch(); + void flush(); + void requestName(const std::string &name); + MessageIn sendWithReplyAndBlock(const MessageOut &messageOut); + +private: + DBusConnection *m_connection; + bool m_busPrivate; +}; + +} // namespace DBus + +#endif // COMMON_DBUS_CONNECTION_H -- 2.7.4