c - GLIB D-BUS 蓝牙 - 如何获取文件描述符?
问题描述
我正在使用 BLUEZ 和 GLIB/D-BUS 连接 2 个 Raspberry Pi(也是笔记本电脑和 Raspberry Pi)。到目前为止,我可以取得公平的进展。
编辑:根据@ukBaz 的好建议,我正在使用笔记本电脑上的python 客户端,以及 Raspberry Pi 上的 C 代码服务器。
在“服务器”上,我可以使用自定义服务 UUID 和串行 RFCOMM 配置文件 UUID 注册设备,然后等待连接。与 python 客户端连接可以工作,我可以看到有一个可用的处理程序(请参阅下面的代码以获取调试输出)我正在使用此代码(在 dbus 循环中,代码为便于阅读而简化):
static void new_connection(GDBusMethodInvocation *inv)
{
g_log(LOG_SERVER, G_LOG_LEVEL_MESSAGE, "New connection.");
GDBusMessage *msg = g_dbus_method_invocation_get_message(inv);
// This prints the output below this code snippet
gchar *content = g_dbus_message_print(msg, 2);
g_log(LOG_SERVER, G_LOG_LEVEL_INFO, "Message is:\n%s", content);
g_free(content);
GVariant *params = g_dbus_method_invocation_get_parameters(inv);
const char *object;
GVariant *properties;
gint32 *handle;
g_variant_get(params, "(oha{sv})", &object, &handle, &properties);
// Problem here, 'handle' is NULL
g_log(LOG_SERVER, G_LOG_LEVEL_INFO, "Object is [%s]\nHandle is [%ls]", object, handle);
GVariantIter iter;
g_variant_iter_init(&iter, properties);
display_properties(&iter);
}
这是输出:
New connection.
Message is:
Type: method-call
Flags: none
Version: 0
Serial: 32
Headers:
path -> objectpath '/org/bluez/jscturret'
interface -> 'org.bluez.Profile1'
member -> 'NewConnection'
destination -> ':1.18'
sender -> ':1.11'
signature -> signature 'oha{sv}'
num-unix-fds -> uint32 1
Body: (objectpath '/org/bluez/hci0/dev_00_AA_AA_AA_AA_AA', handle 0, @a{sv} {})
UNIX File Descriptors:
fd 7: dev=0:8,mode=0140777,ino=41101,uid=0,gid=0,rdev=0:0,size=0,atime=0,mtime=0,ctime=0
Object is [/org/bluez/hci0/dev_00_AA_AA_AA_AA_AA]
Handle is [(null)]
它显示有一个文件描述符fd 7
,但是当我读取 GVariant 参数时,我得到了NULL
.
如何访问文件描述符?我的理解是我需要能够从/向客户端读取/写入。
我使用了 https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt和https://git.kernel.org/pub/scm/bluetooth/bluez。 git/tree/doc/adapter-api.txt供参考,以及其他一些关于 SO 的帖子。在https://www.linumiz.com/中也有很多信息。
当前的完整代码可在此处获得:btservice
解决方案
哦!我很确定您应该发送一个指向整数的指针(而不是指向它的指针的指针)。
你可以做
gint32 句柄;// 而不是 gint32 *handle;
它应该可以工作。
这个 API 的设计非常糟糕(依赖于可变参数,带有格式说明符......人们不喜欢 C 的原因)。
推荐阅读
- integer - python中的精确除法
- php - 在我的自定义无索引函数 (Wordpress) 中包含其他 url
- javascript - 如何在chart.js中将日期显示为x轴上的标签
- redirect - Spring Security 注销成功 url 到另一台主机
- elasticsearch - Elasticsearch 拒绝文件上传以尝试创建新的映射类型
- javascript - org.openqa.selenium.JavascriptException: SyntaxError: '' string literal contains an unscaped line break while using executeScript through Selenium
- jquery - 禁用 Jqgrid 的某些行的编辑弹出选项
- android - 将对象传递给片段而不使用参数
- android-studio - 从编译中删除一个文件,将其标记为文本
- virtual-machine - 在 Oracle VM VirtualBox 中嗅探来自不同 VM 的数据包