2017-09-21 43 views
1

我在Linux中使用Qt的QDBus編寫了一個簡單的DBus服務器。它的代碼一個非常小的量和它的核心是在這裏:QDBus與我的服務器作爲客戶端使用,但不是GDbus

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

} 

然後我寫了一個小的DBus客戶在Linux中也使用Qt的QDBus。 它工作的很好,我可以成功地從這個客戶端通信到我的服務器。客戶端代碼:

#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沒有。我究竟做錯了什麼?謝謝。

回答

0

把它想出來...... QDBus生成一個奇怪名稱的接口,所以我的接口名稱被指定爲錯誤的。我使用「gdbus」工具來解決這個問題。

相關問題