Search code examples
cglibdbusqdbusgdbus

QDBus Works with my Server as Client but not GDbus


I wrote a simple DBus server in Linux using Qt's QDBus. It is a very small amount of code and the core of it is here:

InterfaceDescription::InterfaceDescription()
{

    new ifadapter(this); // Cleans itself up

    qDebug() << "Creating";

    QDBusConnection dbus = QDBusConnection::sessionBus(); // Use session bus

    dbus.registerObject("/mygatt",this); // Register object on the bus

    dbus.registerService("com.my.gatt.interface"); // Expose interface to others

    qDebug() << "Done creating";

}

QByteArray InterfaceDescription::read() {

    qDebug() << "CALLING READ";

    return QByteArray("HELLO");

}

I then wrote a small DBus client in Linux also using Qt's QDBus. It works great and I can successfully communicate from this client to my server. Client code:

#include <QCoreApplication>

#include "clientIf.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    clientIf* client = new clientIf("com.my.gatt.interface", "/mygatt", QDBusConnection::sessionBus(), 0);

    qDebug() << "Sending Read() command over Dbus to server...";

    client->read();

    qDebug() << "Done sending read command...";

    return a.exec();
}

No I am trying to use GDBus to implement the client. So far I have this:

#include <stdbool.h>
#include <stdio.h>
#include <glib/gprintf.h>
#include <gio/gio.h>

void test_Echo(GDBusProxy *proxy)
{
    GVariant *result;
    GError *error = NULL;
    const gchar *str;

    g_printf("Calling read...\n");
    result = g_dbus_proxy_call_sync(proxy,
                    "read",
                    NULL,
                    G_DBUS_CALL_FLAGS_NONE,
                    -1,
                    NULL,
                    &error);
    g_assert_no_error(error);
    g_variant_get(result, "(&s)", &str);
    g_printf("The server answered: '%s'\n", str);
    g_variant_unref(result);
}

void test_Quit(GDBusProxy *proxy)
{
    GVariant *result;
    GError *error = NULL;

    g_printf("Calling method Quit()...\n");
    result = g_dbus_proxy_call_sync(proxy,
                    "Quit",
                    NULL,
                    G_DBUS_CALL_FLAGS_NONE,
                    -1,
                    NULL,
                    &error);
    g_assert_no_error(error);
    g_variant_unref(result);
}


int main(void)
{
    GDBusProxy *proxy;
    GDBusConnection *conn;
    GError *error = NULL;
    const char *version;
    GVariant *variant;

    conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
    g_assert_no_error(error);

    proxy = g_dbus_proxy_new_sync(conn,
                      G_DBUS_PROXY_FLAGS_NONE,
                      NULL,             /* GDBusInterfaceInfo */
                      "com.my.gatt.interface",      /* name */
                      "/mygatt",    /* object path */
                      "com.my.gatt.interface",  /* interface */
                      NULL,             /* GCancellable */
                      &error);
    g_assert_no_error(error);

    /* Test all server methods */
    test_Echo(proxy);
    test_Quit(proxy);

    g_object_unref(proxy);
    g_object_unref(conn);
    return 0;
}

When I run this code, it does not work like the QDBus does, it errors with the following error:

ERROR:../dbustester/main.cpp:29:void test_Echo(GDBusProxy*): assertion failed (error == NULL): GDBus.Error:org.freedesktop.DBus.Error.UnknownInterface: No such interface 'com.my.gatt.interface' at object path '/mygatt' (g-dbus-error-quark, 42)
Calling read...
Aborted

So QDBus works with the server, but GDBus does not. What am I doing wrong?


Solution

  • Figured it out, QDBus generates the interface with a strange name, so my Interface Name was specified wrong. I used the gdbus tool to figure this out.