Connection::Connection(DBusBusType busType, bool busPrivate)
: m_busPrivate(busPrivate)
+ , m_unref(true)
{
DBusError error;
dbus_error_init(&error);
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);
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");
}
<< " 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;
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
#include <dbus_message_out.h>
#include <memory.h>
+#include <map>
#include <string>
namespace DBus
{
public:
Connection(DBusBusType busType, bool privateGet);
+ Connection(DBusConnection *connection);
Connection(const Connection &other) = delete;
~Connection();
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