Make dbus connection wrapper usable by server 35/32935/14
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Mon, 29 Dec 2014 14:03:18 +0000 (15:03 +0100)
committerZbigniew Jasinski <z.jasinski@samsung.com>
Thu, 9 Apr 2015 14:38:57 +0000 (07:38 -0700)
* add another constructor
* add readWrite() function
* add dispatch() function
* adjust readWriteDispatch() function
* add registerObjectPath() function

Change-Id: I0bc3cfd6486d78fa94dfe7b1af01916e0a73a4b5

tests/common/dbus_connection.cpp
tests/common/dbus_connection.h

index 3a111a4a835895caf9e56b5dba46367f90dcadbf..1ca840a296a7b91c19b0cfff46d5938e31c3671a 100644 (file)
@@ -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<std::string, DBusObjectPathVTable>(
+            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
index dba71cd408607ac286ebecc2fbe83fb198fd1ce0..d9ee6d6907deaab7f4aed4dd0107f7e588fadeef 100644 (file)
@@ -29,6 +29,7 @@
 #include <dbus_message_out.h>
 #include <memory.h>
 
+#include <map>
 #include <string>
 
 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<std::string, DBusObjectPathVTable> m_pathVTables;
     bool m_busPrivate;
+    bool m_unref;
 };
 
 } // namespace DBus