1

我使用 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 不。我究竟做错了什么?谢谢。

4

1 回答 1

0

想通了,QDBus生成了一个奇怪名称的接口,所以我的接口名称指定错误。我使用该gdbus工具来解决这个问题。

于 2017-09-21T18:09:29.100 回答