添加Linux BlueZ DBus外围角色支持

Adding Linux BlueZ DBus peripheral role support

添加Linux BlueZ DBus外围角色支持

January 18, 2023 by Juha Vuolle | Comments

2023年1月18日,Juha Vuolle |评论

Broadly speaking the Qt’s Bluetooth Low Energy (“BT LE") support consists of two complementing use cases: the central/client and the peripheral/server roles. For more details about the two roles please consult the Qt Bluetooth documentation.

广义地说,Qt的蓝牙低能耗(“BT LE”)支持包括两个互补的用例:中央/客户端和外围/服务器角色。有关这两个角色的更多详细信息,请参阅Qt蓝牙文档。

Qt 6.5 introduces a new Linux backend for the peripheral (server) role which is based on the BlueZ DBus API. This backend now complements the already existing Qt BlueZ DBus client backend.

Qt 6.5为外围设备(服务器)角色引入了一个新的Linux后端,它基于BlueZ DBus API。该后端现在补充了现有的Qt BlueZ DBus客户端后端。

The existing BlueZ Bluetooth management socket peripheral backend support is kept available for reasons laid out later in this post.

现有的BlueZ蓝牙管理套接字外围后端支持仍然可用,原因在本文稍后介绍。

The Brief History

简史

The current Qt Linux peripheral backend was introduced in 2014. It operates on a somewhat lower abstraction level by using Bluetooth management sockets. These sockets allow a very detailed control over the operation of the Bluetooth subsystem, and the resulting backend implementation is very detailed and, in my view, an ambitious and admirable implementation. Back in 2014 it was more or less the only viable option for implementing BT LE on Linux.

当前的Qt Linux外围设备后端于2014年推出。通过使用蓝牙管理套接字,它在稍微低一些的抽象级别上运行。这些套接字允许对蓝牙子系统的操作进行非常详细的控制,由此产生的后端实现非常详细,在我看来,这是一个雄心勃勃且令人钦佩的实现。早在2014年,它或多或少是在Linux上实现BT LE的唯一可行选项。

Fast forward a few years to 2016, the BlueZ project marked the D-Bus API as non-experimental. Following that Qt Bluetooth released support for the BlueZ DBus client role backend, while the peripheral role implementation remained on the management sockets.

快进几年到2016年,BlueZ项目将D-Bus API标记为非实验性的。之后,Qt蓝牙发布了对BlueZ DBus客户端角色后端的支持,而外围角色实现仍然保留在管理套接字上。

Fast forward again, and the BlueZ D-Bus peripheral role support starts to mature, and for instance the Advertising API required for the peripheral role is marked as non-experimental.

再次快进,BlueZ D-Bus外围角色支持开始成熟,例如,外围角色所需的广告API被标记为非实验性。

This brings us to the current day: adding the BlueZ DBus peripheral role support. The high level Figure below illustrates how it fits in with the existing backends:

这将我们带到了今天:添加BlueZ DBus外围角色支持。下图显示了它如何与现有后端相适应:

Why

原因

There are several reasons why to make this change, for instance:

进行此更改有几个原因,例如:

  • The BlueZ DBus and Qt management sockets implementation both operate on a lower abstraction level themselves, resulting in some sense a duplicate implementation.

  • BlueZ DBus和Qt管理套接字实现本身都在较低的抽象级别上运行,这在某种意义上导致了重复的实现。

  • The BlueZ DBus API is probably the eventual successor of the management sockets, and some day in the future it is plausible that the support for management sockets might cease. Therefore Qt 6.5, as an LTS release, is safe should such phasing out happen.

  • BlueZ DBus API可能是管理套接字的最终继承者,在未来的某一天,对管理套接字的支持可能会停止。因此,Qt 6.5作为一个LTS版本,在这种逐步淘汰的情况下是安全的。

  • Also there are reports from users that the DBus API is in fact more stable for the typical use, see for example the commentary here

  • 此外,还有来自用户的报告称,DBusAPI实际上对于典型用途更稳定,例如,请参见此处的示例

  • For some use cases such as encryption and general data protection the bluetooth daemon provides most of the support out-of-the-box already. The BlueZ Bluetooth management socket API required Qt to implement many aspects. Relying on the BlueZ projects expertise allows QtBluetooth to leverage this expertise.

  • 对于某些使用情况,如加密和一般数据保护,蓝牙守护程序已经提供了大部分开箱即用的支持。BlueZ蓝牙管理套接字API需要Qt实现许多方面。依靠BlueZ项目的专业知识,QtBluetooth可以利用这一专业知识。

  • There are some use cases where a low level access via the socket API can clash with other Bluetooth applications who try to manage peripheral services via the BlueZ’s DBus API.

  • 在某些使用情况下,通过套接字API进行的低级别访问可能会与试图通过BlueZ的DBus API管理外围服务的其他蓝牙应用程序发生冲突。

While developing the BlueZ DBus backend I’ve learned to appreciate its overall stability and operation. I’ve run tests literally hundreds of times, against several Bluetooth stacks and with different Bluetooth hardware, and it appears to be a solid foundation to build on.

在开发BlueZ DBus后端时,我学会了欣赏它的整体稳定性和操作性。我已经对几个蓝牙堆栈和不同的蓝牙硬件进行了数百次测试,这似乎是一个坚实的基础。

As a detail I also find it pleasing that using BlueZ DBus API does not require the CAP_NET_ADMIN capability for the application process. This requirement probably meant for many people in practice running the peripheral application as a root during the development phase.

作为一个细节,我还发现使用BlueZ DBus API不需要应用程序过程的CAP_NET_ADMIN功能。这一要求可能意味着许多人在开发阶段将外围应用程序作为根运行。

Requirements and opting in

要求和选择

The new backend is an opt-in backend for the applications. This means that unless the user opts in, the current management socket API will be used. The opting in happens by setting the QT_BLUETOOTH_USE_DBUS_PERIPHERAL environment variable.

新后端是应用程序的可选后端。这意味着除非用户选择加入,否则将使用当前的管理套接字API。通过设置QT_BLUETOOTH_USE_DBUS_PERIPHERAL环境变量进行选择。

The primary reason for this is that the BlueZ DBus API is not feature-wise on par with the lower level socket implementation; some, arguably less-used, features are not supported. These are indicated in the respective feature documentation throughout the Qt Bluetooth documentation. To provide some intuition on these features, they include things such as connection request whitelists and advertising intervals, and updating connection parameters.

其主要原因是BlueZ DBus API与较低级别的套接字实现相比,在功能方面不尽相同;一些可以说使用较少的功能不受支持。这些在Qt蓝牙文档中的各个功能文档中均有说明。为了提供这些特性的一些直观性,它们包括连接请求白名单和广告间隔,以及更新连接参数。

The minimum required BlueZ version is 5.56. There were some issues with versions prior to that, which would have made the backend implementation trickier. For clarity it should be noted that a typical application wouldn’t likely hit those issues, but the story is a bit different for Qt Bluetooth which, as a library, needs to be prepared for various Qt Bluetooth API use cases.

所需的最低BlueZ版本为5.56。在此之前的版本存在一些问题,这会使后端实现变得更加困难。为了清楚起见,应该注意,典型的应用程序不太可能遇到这些问题,但Qt蓝牙的情况有点不同,作为一个库,需要为各种Qt蓝牙API用例做好准备。

Please see the documentation on the requirements.

请参阅有关要求的文档。

That’s it

就是这样

Thank you for reading this far. As always feedback is welcome, and in particular please be sure to file any bugs you encounter here. Bluetooth stacks, use cases, devices, and Bluetooth environments differ vastly and such reports are invaluable.

感谢您阅读本文。一如既往,欢迎反馈,特别是请确保在此处提交您遇到的任何错误。蓝牙堆栈、使用案例、设备和蓝牙环境差异巨大,此类报告非常宝贵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值