如何为 D-Bus 方法创建 String, Variant 的字典?
Posted
技术标签:
【中文标题】如何为 D-Bus 方法创建 String, Variant 的字典?【英文标题】:How to create Dict of String, Variant for D-Bus method?如何为 D-Bus 方法创建 String, Variant 的字典? 【发布时间】:2021-11-28 10:38:25 【问题描述】:我正在尝试调用 D-Bus 方法,但不明白如何添加所需的参数。
方法名称: CreateSession (String destination, Dict of String, Variant args) ↦ (Object Path session) 巴士名称: org.bluez.obex 对象路径: /org/bluez/obex 接口: org.bluez.obex.Client1我可以用这个参数在 D-Feet 应用程序中成功调用这个方法:
"D0:9C:7A:A1:A4:63",'Target':GLib.Variant('s','OPP')
但是如何只使用 dbus.h 来实现呢?
这是我的代码:
// Do before compile sudo apt-get install -y libdbus-1-dev
// gcc -o test -Wall test.c `pkg-config --cflags dbus-1` `pkg-config --libs dbus-1`
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus.h>
int main()
DBusConnection *sesBusCon;
DBusError error;
DBusMessage *call;
DBusMessage *reply;
dbus_error_init(&error);
if (dbus_error_is_set(&error))
fprintf(stderr, "%s", error.message);
return 1;
sesBusCon = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (dbus_error_is_set(&error))
fprintf(stderr, "%s", error.message);
return 1;
call = dbus_message_new_method_call("org.bluez.obex", "/org/bluez/obex", "org.bluez.obex.Client1", "CreateSession");
reply = dbus_connection_send_with_reply_and_block (sesBusCon, call, 100000, &error);
if (dbus_error_is_set(&error))
fprintf(stderr, "%s", error.message);
return 1;
return 0;
【问题讨论】:
您必须使用dbus_message_iter_init_append()
、dbus_message_iter_open_container()
、dbus_message_iter_close_container()
、dbus_message_iter_append_basic()
API 将结构/复杂参数附加到消息中。检查 this 链接并搜索 API 以获取更多描述。如果文档有帮助,请告诉我。如果没有,我有几个示例可能会有所帮助或检查this 文件,这有我上面提到的 API 的用法。
【参考方案1】:
也许它可以帮助某人:
// Do before compile sudo apt-get install -y libdbus-1-dev
// gcc -o test -Wall test.c `pkg-config --cflags dbus-1` `pkg-config --libs dbus-1`
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus.h>
int main()
DBusConnection *sesBusCon;
DBusError error;
DBusMessage *call;
DBusMessage *reply;
DBusMessageIter iterInit, iterDict, iterEntry, iterValue;
dbus_error_init(&error);
if (dbus_error_is_set(&error))
fprintf(stderr, "%s", error.message);
return 1;
sesBusCon = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (dbus_error_is_set(&error))
fprintf(stderr, "%s", error.message);
return 1;
call = dbus_message_new_method_call("org.bluez.obex", "/org/bluez/obex", "org.bluez.obex.Client1", "CreateSession");
char *destination = "D0:9C:7A:A1:A4:63";
dbus_message_append_args(call, DBUS_TYPE_STRING, &destination, DBUS_TYPE_INVALID);
dbus_message_iter_init_append(call, &iterInit);
dbus_message_iter_open_container(&iterInit, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &iterDict);
dbus_message_iter_open_container(&iterDict, DBUS_TYPE_DICT_ENTRY, NULL, &iterEntry);
char *key = "Target";
dbus_message_iter_append_basic(&iterEntry, DBUS_TYPE_STRING, &key);
dbus_message_iter_open_container(&iterEntry, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &iterValue);
void *val = "OPP";
dbus_message_iter_append_basic(&iterValue, DBUS_TYPE_STRING, &val);
dbus_message_iter_close_container(&iterEntry, &iterValue);
dbus_message_iter_close_container(&iterDict, &iterEntry);
dbus_message_iter_close_container(&iterInit, &iterDict);
DBusPendingCall* replyPending;
DBusMessage* replyMessage;
DBusMessageIter replyIter;
dbus_connection_send_with_reply(sesBusCon, call, &replyPending, -1);
dbus_pending_call_block(replyPending);
if (dbus_pending_call_get_completed (replyPending))
replyMessage = dbus_pending_call_steal_reply(replyPending);
dbus_message_iter_init(replyMessage, &replyIter);
char *objectPath;
dbus_message_iter_get_basic(&replyIter, &objectPath);
printf("object path %s \n",objectPath);
return 0;
【讨论】:
以上是关于如何为 D-Bus 方法创建 String, Variant 的字典?的主要内容,如果未能解决你的问题,请参考以下文章
如何为 Product* getProductFromID(std::string) 编写方法定义;