Bluez: 我在/doc目录中可以找到所有的定义文档吗?

huangapple go评论80阅读模式
英文:

Bluez: where can i find all the definitions documented in /doc?

问题

I want to learn about bluetooth programming in C. After some research, I found this linked on the Bluez official website. https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/

Since I am not familiar with this Linux type of documentation, I can't really get much out of it, but I made some assumptions.

My first try was to look at the first function in doc/adapter-api.txt, and there is no mention where the functions are located. So I searched for it in every /lib file and could not find anything.

So where can I find all the definitions documented in /doc?

英文:

i want to learn about bluetooth programming in C.
After some research i found this linked on the Bluez official website. https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/

since iam not familiar with this linux type of documentation i cant really get much out of it but i made some assumptions.

my first try was to look at the first function in doc/adapter-api.txt and there is no mention where the functions are located.
so i searched for it in every /lib file and could not find anything.

so where can i find all the definitions documented in /doc?

答案1

得分: 3

BlueZ 是 Linux 的蓝牙堆栈,它提供了一些 API 供开发人员使用。以下是可能的 API 列表,从最低级别到最高级别。对于大多数人来说,级别越高越好。

原始 HCI

这绕过了在 Linux 系统上运行的 BlueZ bluetoothd,该服务用于协调与蓝牙硬件的交互。这个 API 在 BlueZ 基础设施之外,因此没有被 BlueZ 项目记录在案。所有信息都可以在蓝牙核心规范中找到,该规范包含大约 3,256 页,适用于规范的 5.2 版本。

如果使用这个级别,最好停止蓝牙服务启动蓝牙守护程序,以避免冲突。HCI 没有安全网,所以您真的需要非常了解自己在做什么。

MGMT Socket

BlueZ 蓝牙管理 API 是下一个级别,也是 BlueZ 开发人员建议的最低级别。它在 API 方面看起来很 "HCI",但它与发出 HCI 命令到硬件的 bluetoothd 通信。

这在以下位置有文档:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt

D-Bus API

这应该是大多数希望与 BlueZ API 交互的人的首选级别。命令与 bluetoothd 交互,后者向硬件发出 HCI 命令。

D-Bus API 的好处是它是与编程语言无关的。然而,熟悉 D-Bus 的人似乎相对较少,因此在使用 API 之前需要学习另一个级别的间接层。

大多数流行的编程语言似乎都有与 D-Bus 的绑定。一些绑定列在以下位置:
https://www.freedesktop.org/wiki/Software/DBusBindings/

BlueZ D-Bus API 根据所需的功能拆分成多个文档。要与蓝牙适配器进行交互,可以参考以下文档:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

设备 API 位于以下位置:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt

等等...

在深入编码之前,可以尝试使用 D-Bus API 与 BlueZ bluetoothd 进行交互。可以使用各种命令行工具来完成这项任务。我假设您将在 C 代码中使用 gdbus 库,因此这似乎是在命令行上进行实验的不错选择。

Linux 蓝牙 adapter 的 BlueZ D-Bus API 可能是最容易入门的部分。

有关此 API 的文档位于以下位置:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

在文档的顶部,说明了 D-Bus 服务和接口是什么。对象路径可以是可变的。

Service     org.bluez
Interface   org.bluez.Adapter1

bluetoothd 在 D-Bus System 总线上通信。

D-Bus 有一个 GetManagedObjects 方法,我们可以使用它来报告关于 BlueZ 的所有信息。要列出关于 BlueZ 的所有信息,可以使用以下命令:

$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects

这通常包含大量信息,因此让我们使用 grep 来查找适配器的对象路径:

$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects | grep -Pio "/org/bluez/hci.*Adapter1"

/org/bluez/hci0': {'org.freedesktop.DBus.Introspectable': {}, 'org.bluez.Adapter1

因此,我们现在可以看到(对我来说)D-Bus 对象路径是 /org/bluez/hci0。我现在可以进行内省:

$ gdbus introspect --system --dest org.bluez --object-path /org/bluez/hci0

现在我有服务、接口和对象路径,可以根据 BlueZ 文档调用方法。例如,要查找可以提供给 SetDiscoveryFilter 的可用筛选器:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.bluez.Adapter1.GetDiscoveryFilters

(['UUIDs', 'RSSI', 'Pathloss', 'Transport', 'DuplicateData'],)

要获取适配器上的所有属性,我们可以使用 GetAll 方法(可以从内省中看到),该方法位于 org.freedesktop.DBus.Properties 接口上。示例调用如下:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.GetAll "org.bluez.Adapter1"

要获取一个属性的值,我们使用 Get

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Get "org.bluez.Adapter1" "Powered"

要设置属性的值,我们使用 Set

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Set "org.bluez.Adapter1" "Powered" "<boolean true>"

以下链接似乎是在 C 中进行一些此类操作的有用介绍:
https://www.linumiz.com/bl

英文:

BlueZ is the Bluetooth stack for Linux and it has a few API's for people to use. Below is a list of the possible API’s starting from lowest level and going to the highest. For most people, the higher the better.

Raw HCI

This bypasses the BlueZ bluetoothd that is running on a Linux system that is used to coordinate interactions with the Bluetooth hardware. This API is outside of the BlueZ infrastructure so is not documented by the BlueZ project. All the information is available in the Bluetooth Core Specification which runs to about 3,256 pages for the 5.2 version of the spec.

If using this level it is a good idea to stop the Bluetooth service from starting the Bluetooth daemon so they are not conflicting. There is no safety net with HCI so you really, really need to know what you are doing.

MGMT Socket

The BlueZ Bluetooth Mamagement API is the next step up and the lowest level that the BlueZ developers recommend. This looks quite "HCI" in its API but it commnicates with the bluetoothd which issues HCI commands to the hardware.

This is documented at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt

D-Bus API

This should be the go to level for most people wanting to interact with the BlueZ API’s. The commands interact with the bluetoothd which issues HCI commands to the hardware.

The good point of a D-Bus API is that it is language agnostic. However, it seems the number of people that are familiar with D-Bus is a relatively small and so it is another level of indirection to learn before using the API.

Most popular programming languages appear to have bindings to D-Bus. Some of the bindings are listed at:

https://www.freedesktop.org/wiki/Software/DBusBindings/

The BlueZ D-Bus API is split across multiple documents depending what piece of functionality is required. For interacting with the Bluetooth Adapter it is documented at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

And the device API is at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt

etc...

Before diving in to coding it might be useful to experiment with interacting with the BlueZ bluetoothd through the D-Bus API. This can be done with various command line tools. I'm going to assume that you will be using the gdbus library for your C code so that seems like a good choice to experiment with on the command line.

The BlueZ D-Bus API for the Linux Bluetooth adapter is probably the easiest to get started with.

The documentation for this API is at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt

At the top of the document it says what the D-Bus Service and Interface is. And that the object path can be variable.

Service     org.bluez
Interface   org.bluez.Adapter1

The bluetoothd is communicating on the D-Bus System bus.

D-Bus has a GetManagedObjects method that we can use to report all the things BlueZ knows about. To list all this information about BlueZ use:

$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects

This is usually a lot of information so let's use grep to find the object path for the adapter:

$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects | grep -Pio "/org/bluez/hci.*Adapter1"

/org/bluez/hci0': {'org.freedesktop.DBus.Introspectable': {}, 'org.bluez.Adapter1

So we can now see that (for me) the D-Bus object path is /org/bluez/hci0. I can introspect this now:

$ gdbus introspect --system --dest org.bluez --object-path /org/bluez/hci0

Now I have the Service, Interface and Object Path I can call methods as documented by BlueZ. For example to find what available filters that can be given to SetDiscoveryFilter:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.bluez.Adapter1.GetDiscoveryFilters

(['UUIDs', 'RSSI', 'Pathloss', 'Transport', 'DuplicateData'],)

To get all the properties on the Adapter then we can use the GetAll method (that we can see from the introspection) is on the org.freedesktop.DBus.Properties interface. A call example:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.GetAll "org.bluez.Adapter1"

To get the value of one property we use Get:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Get "org.bluez.Adapter1" "Powered"

To set the value of a property we use Set:

$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Set "org.bluez.Adapter1" "Powered" "<boolean true>"

The following looks like a useful introduction to doing some of this in C:

https://www.linumiz.com/bluetooth-list-devices-using-gdbus/

There is a talk "Doing Bluetooth Low Energy on Linux" by Szymon Janc that may also be useful background. It is available on YouTube:

https://youtu.be/VMDyebKT5c4

And a PDF of the slides:

https://elinux.org/images/3/32/Doing_Bluetooth_Low_Energy_on_Linux.pdf

huangapple
  • 本文由 发表于 2023年2月6日 09:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定