QDBus与我的服务器一起作为客户端而不是GDbus

时间:2017-09-21 15:55:53

标签: c glib dbus qdbus gdbus

我使用Qt的QDBus在Linux中编写了一个简单的DBus服务器。这是一个非常少量的代码,它的核心在于:

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");

}

然后我在Linux中使用Qt的QDBus编写了一个小型DBus客户端。 效果很好,我可以成功地从这个客户端与我的服务器进行通信。客户代码:

#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();
}

不,我正在尝试使用GDBus来实现客户端。到目前为止,我有这个:

#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;
}

当我运行此代码时,它不像QDBus那样工作,它会出现以下错误:

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

所以QDBus可以与服务器一起使用,但GDBus没有。我究竟做错了什么?感谢。

1 个答案:

答案 0 :(得分:0)

想出来,QDBus生成一个带有奇怪名称的接口,因此我的接口名称指定错误。我使用gdbus工具来解决这个问题。

相关问题