我使用QT的QDBU在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");
}
然后,我还使用QT的QDBU在Linux中写了一个小型DBU客户端。它运行良好,我可以成功地从该客户端通信到我的服务器。客户端代码:
#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();
}
不,我正在尝试使用GDBU来实现客户端。到目前为止,我都有:
#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
因此,QDBU与服务器一起使用,但GDBUS不使用。我在做什么错?
弄清楚了, QDBus
用一个奇怪的名称生成接口,因此我的接口名称指定错误。我使用gdbus
工具来弄清楚这一点。