--- /dev/null
+/*
+ * 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 <dbus_connection.h>
+
+#include <dpl/test/test_runner.h>
+
+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
--- /dev/null
+/*
+ * 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 <dbus/dbus.h>
+
+#include <dbus_message_in.h>
+#include <dbus_message_out.h>
+#include <memory.h>
+
+#include <string>
+
+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