微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GLIB D-BUS 蓝牙 - 如何获取文件描述符?

如何解决GLIB D-BUS 蓝牙 - 如何获取文件描述符?

我正在使用 BLUEZ 和 GLIB/D-BUS 连接 2 个 RaspBerry Pi(也是一台笔记本电脑和一个 RaspBerry Pi)。 到目前为止,我可以取得公平的进步。

编辑:根据@ukBaz 的良好建议,我使用的是笔记本电脑上的 python client,以及 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,"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.txthttps://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt 作为参考,并在 SO 上使用了其他一些帖子。 在 https://www.linumiz.com/ 中也获得了很多信息。

当前完整代码可在此处获得:btservice

解决方法

哦!我很确定您应该发送一个指向整数的指针(而不是指向它的指针的指针)。

你可以做到

gint32 句柄; // 而不是 gint32 *handle;

它应该可以工作。

这个 API 的设计非常糟糕(依赖可变参数,带有格式说明符......人们不喜欢 C 的原因)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。