From: Marcin Niesluchowski Date: Mon, 29 Dec 2014 14:03:18 +0000 (+0100) Subject: Make dbus connection wrapper usable by server X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b208dd6bc66f2d24ccb13b5947c4c494f91e9803;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Make dbus connection wrapper usable by server * add another constructor * add readWrite() function * add dispatch() function * adjust readWriteDispatch() function * add registerObjectPath() function Change-Id: I0bc3cfd6486d78fa94dfe7b1af01916e0a73a4b5 --- diff --git a/tests/common/dbus_connection.cpp b/tests/common/dbus_connection.cpp index 3a111a4a..1ca840a2 100644 --- a/tests/common/dbus_connection.cpp +++ b/tests/common/dbus_connection.cpp @@ -29,6 +29,7 @@ namespace DBus Connection::Connection(DBusBusType busType, bool busPrivate) : m_busPrivate(busPrivate) + , m_unref(true) { DBusError error; dbus_error_init(&error); @@ -45,8 +46,16 @@ Connection::Connection(DBusBusType busType, bool busPrivate) dbus_connection_set_exit_on_disconnect(m_connection, FALSE); } +Connection::Connection(DBusConnection *connection) + : m_connection(connection) + , m_unref(false) +{ +} + Connection::~Connection() { + if (!m_unref) + return; if (m_busPrivate) dbus_connection_close(m_connection); dbus_connection_unref(m_connection); @@ -78,9 +87,22 @@ void Connection::addFilter(DBusHandleMessageFunction handleMessageFunction, RUNNER_ASSERT_MSG(ret == TRUE, "Failed to add filter. Not enough memory"); } -void Connection::readWriteDispatch() +void Connection::readWrite(int timeoutMilliseconds) +{ + dbus_bool_t ret = dbus_connection_read_write(m_connection, timeoutMilliseconds); + RUNNER_ASSERT_MSG(ret == TRUE, "Failed to read write. Disconnect message has been processed"); +} + +void Connection::dispatch() { - dbus_bool_t ret = dbus_connection_read_write_dispatch(m_connection, -1); + DBusDispatchStatus ret = dbus_connection_dispatch(m_connection); + RUNNER_ASSERT_MSG(ret != DBUS_DISPATCH_NEED_MEMORY, + "Failed to dispatch. Not enough memory"); +} + +void Connection::readWriteDispatch(int timeoutMilliseconds) +{ + dbus_bool_t ret = dbus_connection_read_write_dispatch(m_connection, timeoutMilliseconds); RUNNER_ASSERT_MSG(ret == TRUE, "Failed to read write dispatch. Disconnect message has been processed"); } @@ -106,6 +128,33 @@ void Connection::requestName(const std::string &name, int expectedResult) << " Error: " << error.message); } +void Connection::registerObjectPath(const std::string &path, + DBusObjectPathMessageFunction messageFunction, + void *userData) +{ + auto p = m_pathVTables.insert( + std::pair( + path, { [] (DBusConnection *connection, void *userData)->void { (void) connection; + (void) userData; }, + messageFunction, + nullptr, nullptr, nullptr, nullptr + } + ) + ); + RUNNER_ASSERT_MSG(p.second, "Path <" << path << "> already exists"); + + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + dbus_bool_t ret = dbus_connection_try_register_object_path(m_connection, + path.c_str(), + &(p.first->second), + userData, + &error); + RUNNER_ASSERT_MSG(ret != FALSE, "Failed to register object path. " + << "Error: " << error.message); +} + MessageIn Connection::sendWithReplyAndBlock(const MessageOut &messageOut) { DBusError error; @@ -121,4 +170,16 @@ MessageIn Connection::sendWithReplyAndBlock(const MessageOut &messageOut) return MessageIn(messageRecv); } +void Connection::send(const MessageOut &messageOut) +{ + DBusError error; + dbus_error_init(&error); + ErrorPtr errorPtr(&error); + + dbus_bool_t ret = dbus_connection_send(m_connection, + messageOut.getMessage(), + nullptr); + RUNNER_ASSERT_MSG(ret == TRUE, "Failed to send. Not enough memory"); +} + } // namespace DBus diff --git a/tests/common/dbus_connection.h b/tests/common/dbus_connection.h index dba71cd4..d9ee6d69 100644 --- a/tests/common/dbus_connection.h +++ b/tests/common/dbus_connection.h @@ -29,6 +29,7 @@ #include #include +#include #include namespace DBus @@ -40,6 +41,7 @@ class Connection { public: Connection(DBusBusType busType, bool privateGet); + Connection(DBusConnection *connection); Connection(const Connection &other) = delete; ~Connection(); @@ -49,15 +51,23 @@ public: void addFilter(DBusHandleMessageFunction handleMessageFunction, void *userData, DBusFreeFunction freeDataFunction = nullptr); - void readWriteDispatch(); + void readWrite(int timeoutMilliseconds = -1); + void dispatch(); + void readWriteDispatch(int timeoutMilliseconds = -1); void flush(); void requestName(const std::string &name, int expectedResult = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER); + void registerObjectPath(const std::string &path, + DBusObjectPathMessageFunction messageFunction, + void *userData); MessageIn sendWithReplyAndBlock(const MessageOut &messageOut); + void send(const MessageOut &messageOut); private: DBusConnection *m_connection; + std::map m_pathVTables; bool m_busPrivate; + bool m_unref; }; } // namespace DBus