gpt4 book ai didi

c - 如何为 D-Bus 方法创建 {String, Variant} 的字典?

转载 作者:行者123 更新时间:2023-12-04 17:10:43 25 4
gpt4 key购买 nike

我正在尝试调用 D-Bus 方法,但不明白如何添加所需的参数。

  • 方法名称:CreateSession(字符串目标,{String,Variant} args 的字典)↦(对象路径 session )
  • 总线名称: org.bluez.obex
  • 对象路径:/org/bluez/obex
  • 接口(interface): 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;
}

最佳答案

也许它可以帮助某人:

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

关于c - 如何为 D-Bus 方法创建 {String, Variant} 的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69496372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com