Linux蓝牙之核外协议栈BlueZ架构分析
Linux蓝牙之核外协议栈BlueZ架构分析
工程名称: BlueZ — 官方 Linux Bluetooth 协议栈
代码仓库: git://git.kernel.org/pub/scm/bluetooth/bluez.git
协议: GPL-2.0-or-later (核心) / LGPL-2.1-or-later (lib)
代码规模: ~525,695 行 C 代码, 803 个 C 源文件
核心维护者: Marcel Holtmann, Johan Hedberg, Luiz Augusto von Dentz
本报告基于 BlueZ(v5.86) git commit
0fdf34b17
目录
1. 项目概述
BlueZ 是 Linux 内核官方 Bluetooth 子系统的用户态实现,提供完整的蓝牙协议栈支持,涵盖:
- 经典蓝牙 (BR/EDR): 传统蓝牙 2.0/2.1/3.0
- 低功耗蓝牙 (BLE): 蓝牙 4.0/4.1/4.2/5.0/5.1/5.2/5.3
- 双模 (Dual Mode): 同时支持 BR/EDR 和 LE
BlueZ 通过 D-Bus 系统总线向用户态应用提供标准化的蓝牙服务接口,是 Linux 桌面和嵌入式系统中蓝牙功能的事实标准实现。
2. 功能全景
2.1 核心蓝牙功能
| 功能域 | 支持技术 | 说明 |
|---|---|---|
| 设备发现 | BR/EDR Inquiry + LE Scanning | 支持主动/被动扫描 |
| 连接管理 | BR/EDR SCO/eSCO + LE Connection | 多设备并发连接 |
| 配对/绑定 | SSP, LE Legacy, LE Secure Connections | Just Works, Passkey, OOB |
| 广播 | LE Advertising (Legacy + Extended) | 支持 Periodic Advertising |
| GATT | GATT Client + Server | 支持缓存、通知、MTU协商 |
| SDP | BR/EDR 服务发现协议 | 兼容模式已标记废弃 |
2.2 音频/媒体支持 (A2DP/AVRCP/HFP)
| Profile | 角色 | 文件位置 |
|---|---|---|
| A2DP | Source/Sink | profiles/audio/a2dp.c |
| AVRCP | Controller/Target | profiles/audio/avrcp.c |
| AVDTP | 流媒体传输出层 | profiles/audio/avdtp.c |
| HFP | HF/AG | profiles/audio/hfp-hf.c / hfp-ag.c |
| ASHA | 助听器音频流 | profiles/audio/asha.c |
| BAP | LE Audio (Basic Audio Profile) | profiles/audio/bap.c |
| VCP | Volume Control Profile | profiles/audio/vcp.c |
| MCP | Media Control Profile | profiles/audio/mcp.c |
| CCP | Coordinated Set Identification | profiles/audio/ccp.c |
| CSIP | Coordinated Set Identification Profile | profiles/audio/csip.c |
| TMAP | Telephony and Media Audio Profile | profiles/audio/tmap.c |
| GMAP | Generic Media Audio Profile | profiles/audio/gmap.c |
| MICp | Microphone Control Profile | profiles/audio/micp.c |
| RAP | Rendering Adjustment Profile | src/shared/rap.c |
2.3 输入设备 (HID)
| Profile | 说明 | 文件 |
|---|---|---|
| HID/HOG | 键盘/鼠标/游戏手柄 | profiles/input/hog.c |
| Sixaxis | Sony PS3/PS4 手柄支持 | profiles/input/sixaxis.h |
| UHID | 用户态 HID 驱动 | src/shared/uhid.c |
2.4 网络服务 (PAN)
| Profile | 说明 | 文件 |
|---|---|---|
| PAN | 个人局域网 | profiles/network/server.c |
| BNEP | 蓝牙网络封装协议 | profiles/network/bnep.c |
| NAP | 网络访问点 | profiles/network/connection.c |
2.5 LE Audio (下一代)
| Profile | 说明 | 文件 |
|---|---|---|
| LC3 Codec | LE Audio 强制编解码器 | src/shared/bap.c |
| Broadcast Audio | 广播音频 | profiles/audio/bap.c |
| BASS | 广播音频扫描服务 | profiles/audio/bass.c |
2.6 OBEX/拨号/信息
| 功能 | 说明 | 文件 |
|---|---|---|
| OBEX | 对象交换服务器 | obexd/src/ |
| OPP | 对象推送 | obexd/plugins/opp.c |
| FTP | 文件传输 | obexd/plugins/ftp.c |
| PBAP | 电话簿访问 | obexd/plugins/pbap.c |
| MAP | 信息访问 | obexd/plugins/mas.c |
2.7 Mesh 网络
| 组件 | 说明 | 文件 |
|---|---|---|
| Mesh Node | 蓝牙 Mesh 节点实现 | mesh/mesh.c |
| Provisioning | 配置协议 (PB-ADV) | mesh/prov-acceptor.c, prov-initiator.c |
| Configuration | 配置模型服务器 | mesh/cfgmod-server.c |
| Friendship | Friend/LCFN 节点 | mesh/friend.c |
3. 架构总览
3.1 分层架构
┌─────────────────────────────────────────────────────────────────┐
│ 用户态应用 (Applications) │
│ bluetoothctl bluetooth-player btgatt-client ... │
└──────────────────────────┬──────────────────────────────────────┘
│ D-Bus System Bus (org.bluez)
┌──────────────────────────▼──────────────────────────────────────┐
│ bluetoothd (守护进程) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌───────────────────┐ │
│ │ Adapter │ │ Device │ │ Profile │ │ GATT Manager │ │
│ │ Manager │ │ Manager │ │ Manager │ │ Client/Server │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────────┬──────────┘ │
│ │ │ │ │ │
│ ┌────▼────────────▼────────────▼─────────────────▼──────────┐ │
│ │ Plugin 系统 (profiles/*) │ │
│ │ A2DP│AVRCP│HFP│HID│GATT Profiles│PAN│BAP│CSIP│TMAP... │ │
│ └───────────────────────────┬────────────────────────────────┘ │
│ │ │
│ ┌───────────────────────────▼────────────────────────────────┐ │
│ │ 共享库 (src/shared/ + ell/) │ │
│ │ GATT DB │ MGMT │ HCI │ IO │ Crypto │ Queue │ BAP ... │ │
│ └───────────────────────────┬────────────────────────────────┘ │
└──────────────────────────────┼──────────────────────────────────┘
│ mgmt socket / HCI sockets
┌──────────────────────────────▼──────────────────────────────────┐
│ Linux Kernel Bluetooth 子系统 │
│ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │
│ │ HCI │ │ L2CAP │ │ SCO │ │ RFCOMM │ │ BNEP │ │
│ │ Core │ │ │ │ │ │ │ │ │ │
│ └────────┘ └────────┘ └────────┘ └────────┘ └────────┘ │
├──────────────────────────────┬──────────────────────────────────┤
│ Bluetooth 控制器 (USB/UART/PCI) │
└─────────────────────────────────────────────────────────────────┘
3.2 关键架构特性
- D-Bus 驱动架构: 所有核心功能通过 D-Bus 系统总线暴露,应用与
bluetoothd解耦 - Plugin 插件体系: 蓝牙 Profile 作为插件动态加载 (
profiles/*/) - 双循环支持: 支持 GLib 主循环和 ELL 主循环两种事件模型
- 共享组件: 公共基础库
src/shared/支持 GLib 和 ELL 两套后端 - MGMT 协议: 通过
mgmtsocket 与内核通信,取代直接操作 HCI
4. 核心模块详解
4.1 bluetoothd 守护进程 (src/)
BlueZ 核心守护进程,处理所有蓝牙操作。
入口: src/main.c:1481 — main()` 函数
启动流程:
main()
├── init_defaults() — 初始化全局配置默认值
├── parse_config(config) — 解析 /etc/bluetooth/main.conf
├── connect_dbus() — 连接到 system D-Bus
├── adapter_init() — 初始化适配器管理
│ ├── 创建 mgmt socket
│ └── 枚举已连接的蓝牙适配器
├── btd_device_init() — 设备管理器初始化
├── btd_agent_init() — Agent 交互层初始化
├── btd_profile_init() — Profile 管理器初始化
├── start_sdp_server() — SDP 服务 (BR/EDR)
├── plugin_init() — 加载所有内置及外部插件
├── rfkill_init() — RFKILL 监控
├── mainloop_run_with_signal() — 进入 GLib 主循环
└── shutdown sequence — 按相反顺序清理
核心源文件:
| 文件 | 职责 |
|---|---|
main.c |
入口、配置解析、初始化编排 |
adapter.c (+ adapter.h) |
蓝牙适配器管理、配对绑定、mgmt 命令 |
device.c (+ device.h) |
远程设备生命周期管理、GATT 客户端 |
profile.c (+ profile.h) |
Profile 注册/探测/连接 |
agent.c |
Pairing Agent 交互 |
gatt-database.c |
GATT Server 数据库 (D-Bus 暴露) |
gatt-client.c |
GATT Client (内核 GATT 接口) |
advertising.c |
LE Advertising Manager |
adv_monitor.c |
广播监控器管理 |
battery.c |
电池信息服务 |
bearer.c |
Bearer (BR/EDR 与 LE) 抽象层 |
set.c |
Device Set (CSIP 协调集) 管理 |
settings.c |
设置存储 |
storage.c |
持久化存储 |
eir.c |
Extended Inquiry Response 处理 |
sdpd.c |
SDP 守护进程 |
sdp-client.c |
SDP 客户端 |
4.2 适配器管理 (Adapter Manager)
src/adapter.c — 12,000+ 行,最大源文件之一。
核心职责:
- 枚举所有蓝牙适配器 (
hci0,hci1, …) - 通过 mgmt socket 与内核蓝牙子系统通信
- 管理配对、发现、扫描参数
- 控制电源、可发现性、可连接性
- 实验性功能注册 (Debug, BQR, RPA Resolution, LE Simultaneous Roles, ISO Socket, Codec Offload)
mgmt 命令接口:
// 与内核通信的核心机制 — mgmt socket
static struct mgmt *mgmt_primary = NULL;
// 重要操作:
MGMT_OP_READ_INDEX_LIST — 枚举适配器
MGMT_OP_READ_INFO — 读取适配器信息
MGMT_OP_SET_POWERED — 电源控制
MGMT_OP_SET_DISCOVERABLE — 可发现模式
MGMT_OP_SET_CONNECTABLE — 可连接模式
MGMT_OP_START_DISCOVERY — 开始发现
MGMT_OP_STOP_DISCOVERY — 停止发现
MGMT_OP_PAIR_DEVICE — 配对
MGMT_OP_CANCEL_PAIR_DEVICE — 取消配对
MGMT_OP_UNPAIR_DEVICE — 解除绑定
4.3 设备管理 (Device Manager)
src/device.c — 约 5,800 行。
数据模型:
btd_device
├── bdaddr / bdaddr_type — 地址
├── adapter — 所属适配器
├── name — 设备名称
├── class — 设备类
├── appearance — LE 外观
├── paired / bonded — 配对状态
├── gatt_client / gatt_server — GATT 通道
├── services / profiles — 已发现的服务
├── keys (LTK/IRK/CSRK) — 安全密钥
├── connection state — 连接状态
└── uuids — 支持的 UUID 列表
5. D-Bus 接口体系
BlueZ 通过 D-Bus 系统总线 org.bluez 暴露所有功能。
5.1 Core Interfaces (在 bluetoothd 中实现)
| 接口 | 对象路径 | 用途 |
|---|---|---|
org.bluez.Adapter1 |
/org/bluez/hciX |
适配器控制 |
org.bluez.Device1 |
/org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX |
远程设备管理 |
org.bluez.Agent1 |
由客户端提供 | 配对交互 |
org.bluez.AgentManager1 |
/org/bluez |
Agent 注册 |
org.bluez.ProfileManager1 |
/org/bluez |
Profile 注册 |
org.bluez.GattManager1 |
/org/bluez/hciX |
GATT Server 管理 |
org.bluez.GattService1 |
/org/bluez/hciX/dev_XX/serviceX |
GATT 服务 |
org.bluez.GattCharacteristic1 |
/org/bluez/hciX/dev_XX/serviceX/charX |
GATT 特征 |
org.bluez.GattDescriptor1 |
/org/bluez/hciX/dev_XX/serviceX/charX/descX |
GATT 描述符 |
org.bluez.LEAdvertisingManager1 |
/org/bluez/hciX |
广播管理 |
org.bluez.LEAdvertisement1 |
由客户端提供 | 广播数据 |
org.bluez.BatteryProviderManager1 |
/org/bluez |
电池信息 |
org.bluez.Battery1 |
由客户端或系统提供 | 电池状态 |
org.bluez.AdvertisementMonitorManager1 |
/org/bluez |
广播监控管理 |
org.bluez.DeviceSet1 |
/org/bluez/set_X |
协调设备集 |
5.2 Profile-Specific Interfaces (插件暴露)
| 接口 | 用途 |
|---|---|
org.bluez.Media1 |
A2DP 媒体控制 |
org.bluez.MediaEndpoint1 |
A2DP 编解码端点 |
org.bluez.MediaTransport1 |
A2DP 媒体传输通道 |
org.bluez.MediaControl1 |
AVRCP 媒体控制 |
org.bluez.MediaFolder1 |
AVRCP 媒体浏览 |
org.bluez.MediaPlayer1 |
AVRCP 播放器状态 |
org.bluez.MediaItem1 |
AVRCP 媒体条目 |
org.bluez.Input1 |
HID 设备 |
org.bluez.Network1 |
PAN 网络 |
org.bluez.HealthDevice1 |
HDP 健康设备 |
org.bluez.HeartRate1 |
心率监测 |
org.bluez.Thermometer1 |
温度计 |
6. 核心流程分析
6.1 设备发现流程
用户态应用 bluetoothd 内核
│ │ │
│ StartDiscovery() │ │
│ ───────────────────────► │ MGMT_OP_START_DISCOVERY │
│ │ ──────────────────────────────►│
│ │ │
│ │ ◄─── MGMT_EV_DEVICE_FOUND ───┤
│ │ (每个发现到的设备) │
│ │ │
│ PropertiesChanged │ │
│ ◄─────────────────────── │ │
│ (Device1 Found / RSSI) │ │
│ │ │
│ StopDiscovery() │ │
│ ───────────────────────► │ MGMT_OP_STOP_DISCOVERY │
│ │ ──────────────────────────────►│
│ Discovering = false │ │
│ ◄─────────────────────── │ │
6.2 配对绑定流程 (LE Secure Connections)
应用 bluetoothd Agent 内核
│ │ │ │
│ PairDevice(addr) │ │ │
│────────────────────► │ │ │
│ │ MGMT_OP_PAIR_DEVICE │ │
│ │─────────────────────►│ │
│ │ │ │
│ │ ◄─ MGMT_EV_NEW_IRK ─┤ │
│ │ ◄─ MGMT_EV_NEW_LTK ─┤ │
│ │ ◄─ MGMT_EV_NEW_CSRK─┤ │
│ │ │ │
│ │ RequestConfirmation │ │
│ │◄────────────────────│ │
│ │ (Agent交互) │ │
│ │────────────────────►│ │
│ │ │ │
│ ◄─ Pairing Complete ────┤ │ │
6.3 GATT 服务发现流程
bluetoothd Device 远端 GATT Server
│ │
│ Exchange MTU Request │
│ ───────────────────────────────────► │
│ ◄─── Exchange MTU Response ────────── │
│ │
│ Discover All Primary Services │
│ ───────────────────────────────────► │
│ ◄─── Read by Group Type Response ──── │
│ │
│ Discover All Characteristics │
│ ───────────────────────────────────► │
│ ◄─── Read by Type Response ────────── │
│ │
│ Discover All Descriptors │
│ ───────────────────────────────────► │
│ ◄─── Find Information Response ───────│
│ │
│ Enable Notifications/Indications │
│ ───────────────────────────────────► │
│ ◄─── Write Response ──────────────── │
6.4 A2DP 音频流建立流程
应用 bluetoothd 远端 A2DP Sink
│ │ │
│ Connect("A2DP_Sink") │ │
│────────────────────► │ │
│ │ AVDTP Discover │
│ │ ───────────────────────────► │
│ │ ◄── AVDTP Discover Resp ──── │
│ │ │
│ │ AVDTP Get Capabilities │
│ │ ───────────────────────────► │
│ │ ◄── AVDTP Get Caps Resp ──── │
│ │ │
│ │ AVDTP Set Configuration │
│ │ ───────────────────────────► │
│ │ ◄── AVDTP Set Config Resp ───│
│ │ │
│ │ AVDTP Open │
│ │ ───────────────────────────► │
│ │ ◄── AVDTP Open Resp ──────── │
│ │ │
│ │ AVDTP Start │
│ │ ───────────────────────────► │
│ │ ◄── AVDTP Start Resp ────────│
│ │ │
│ TransportCreated │ │
│◄──────────────────── │ │
│ │ ◄ 音频流 ► │
7. 关键子系统
7.1 Plugin 系统
插件是 BlueZ 的核心扩展机制,每个蓝牙 Profile 独立封装为插件。
注册机制:
// 每个插件使用 BLUETOOTH_PLUGIN_DEFINE 宏
BLUETOOTH_PLUGIN_DEFINE(a2dp, ...)
内置插件 (plugins/):
| 插件 | 功能 |
|---|---|
policy |
连接策略管理 (自动连接、重连) |
autopair |
自动配对 (Simple Pairing) |
neard |
NFC 交互集成 |
hostname |
主机名管理 |
admin |
管理策略 (UUID 允许列表) |
sixaxis |
Sony PS 手柄支持 |
外部插件 (profiles/*/): 所有蓝牙 Profile 实现
7.2 共享库 (src/shared/)
双后端架构 — 同时支持 GLib 和 ELL (Embedded Linux Library):
| 组件 | 文件 | 功能 |
|---|---|---|
| GATT DB | gatt-db.c |
GATT 数据库/树管理 |
| GATT Client | gatt-client.c |
GATT 客户端实现 |
| GATT Server | gatt-server.c |
GATT 服务端实现 |
| ATT | att.c |
Attribute Protocol |
| HCI | hci.c |
HCI 命令/事件 |
| MGMT | mgmt.c |
mgmt socket 通信 |
| BAP | bap.c |
Basic Audio Profile |
| BASS | bass.c |
Broadcast Audio Scan Service |
| CSIP | csip.c |
Coordinated Set Ident. Profile |
| Crypto | crypto.c |
蓝牙加密/签名 |
| ECC | ecc.c |
椭圆曲线加密 (P-256) |
| IO | io-glib.c / io-ell.c |
IO 抽象层 |
| Queue | queue.c |
队列数据结构 |
| RingBuf | ringbuf.c |
环形缓冲区 |
| Shell | shell.c |
交互式 Shell 框架 |
| UHID | uhid.c |
用户态 HID |
| UInput | uinput.c |
用户态输入设备 |
| Util | util.c |
通用工具函数 |
| IO (GLib) | mainloop-glib.c |
GLib 事件循环 |
| IO (ELL) | mainloop-ell.c |
ELL 事件循环 |
| Crypto+ | hci-crypto.c |
HCI 级加密 |
| BTP | btp.c |
BTP 测试协议 |
| BTSnoop | btsnoop.c |
蓝牙抓包格式 |
| IO (BTP) | io-ell.c / io-glib.c |
跨平台 IO |
| L2CAP test | l2cap-test.c |
L2CAP 测试 |
| Tester | tester.c |
测试框架 |
| AD | ad.c |
广播数据解析 |
| Att types | att-types.h |
ATT 类型定义 |
| Log | log.c |
日志系统 |
| IO redirect | mainloop-notify.c |
IO 重定向 |
| Gap | gap.c |
Generic Access Profile |
| HFP | hfp.c |
Hands-Free Profile |
| IO redirect | io-mainloop.c |
IO 主循环 |
7.3 OBEX 守护进程 (obexd/)
独立的 OBEX 守护进程,处理文件传输和信息同步:
obexd/src/
├── main.c — OBEX 守护进程入口
├── obex.c — OBEX 协议核心
├── server.c — OBEX 服务器
├── transport.c — 传输层抽象 (蓝牙/UNIX)
├── plugin.c — OBEX 插件系统
├── manager.c — 会话管理
├── service.c — 服务注册
└── mimetype.c — MIME 类型处理
obexd/plugins/
├── bluetooth.c — 蓝牙传输后端
├── filesystem.c — 文件系统后端
├── ftp.c — 文件传输
├── opp.c — 对象推送
├── pbap.c — 电话簿访问
├── mas.c — 消息访问
├── irmc.c — IRMC 同步
├── pcsuite.c — PC Suite 同步
└── vcard.c — vCard 解析
7.4 Mesh 实现 (mesh/)
独立的蓝牙 Mesh 网络实现,支持:
- PB-ADV Provisioning
- 配置模型服务器
- 朋友节点 (Friend Node)
- 低功耗节点 (LCFN)
- 中继 (Relay)
- RPL (重放保护列表)
- 多个 AppKey/NetKey 管理
- Model 扩展框架
8. 工具生态
8.1 用户态工具
| 工具 | 文件位置 | 用途 |
|---|---|---|
| bluetoothctl | client/main.c |
主要交互式控制工具 (通过 D-Bus) |
| btmgmt | tools/btmgmt.c |
直接 mgmt socket 操作 (低层) |
| hcitool | tools/hcitool.c |
HCI 级设备管理 (已废弃) |
| hciconfig | tools/hciconfig.c |
适配器配置 (已废弃) |
| btattach | tools/btattach.c |
通过 UART 连接蓝牙控制器 |
| btproxy | tools/btproxy.c |
蓝牙代理 |
| btsnoop | tools/btsnoop.c |
蓝牙抓包工具 |
| btmon | monitor/ |
蓝牙协议监控器 |
| btplayer | tools/bluetooth-player.c |
蓝牙音频播放器 |
| btgatt-client | tools/btgatt-client.c |
GATT 客户端示例 |
| btgatt-server | tools/btgatt-server.c |
GATT 服务端示例 |
| meshctl | mesh/ |
Mesh 网络控制工具 |
| obexctl | — | OBEX 控制工具 |
| btpclient | client/btpclient/ |
BTP 测试客户端 |
| btinfo | tools/btinfo.c |
蓝牙设备信息查看 |
| bcmfw | tools/bcmfw.c |
Broadcom 固件升级 |
| bluemoon | tools/bluemoon.c |
Intel 蓝牙调试 |
8.2 测试工具
| 工具 | 用途 |
|---|---|
| hci-tester | HCI 接口测试 |
| gap-tester | GAP 测试 |
| bnep-tester | BNEP 网络测试 |
| 6lowpan | 6LoWPAN 测试 |
| check-selftest | 自测试检查 |
| unit/ | 单元测试目录 (unit/test-*.c) |
8.3 调试工具
| 工具 | 用途 |
|---|---|
| monitor/btmon | 实时 HCI 协议抓包/解码 |
| monitor/analyzer | btmon 分析器 |
| btsnoop | 生成/读取 btsnoop 格式抓包文件 |
| hcidump (通过 btmon) | 传统 HCI 抓包兼容 |
| logger | btmon 日志记录器 |
9. 配置与存储
9.1 配置文件
| 文件 | 用途 |
|---|---|
/etc/bluetooth/main.conf |
主配置 (General, BR, LE, Policy, GATT, CSIS, AVDTP, AVRCP, AdvMon) |
/etc/bluetooth/input.conf |
输入设备配置 |
/etc/bluetooth/network.conf |
网络配置 |
/etc/dbus-1/system.d/bluetooth.conf |
D-Bus 安全策略 |
9.2 main.conf 配置
| 配置节 | 关键选项 |
|---|---|
[General] |
Name, Class, DiscoverableTimeout, AlwaysPairable, ControllerMode, Privacy, FastConnectable |
[BR] |
PageScanType/Interval/Window, InquiryScanType/Interval/Window |
[LE] |
ScanInterval*, MinConnectionInterval, MaxConnectionInterval |
[Policy] |
ReconnectUUIDs, ReconnectAttempts, AutoEnable |
[GATT] |
Cache, ExchangeMTU, KeySize, Client |
[CSIS] |
SIRK, Encryption, Size, Rank |
[AVDTP] |
SessionMode, StreamMode |
[AVRCP] |
VolumeWithoutTarget, VolumeCategory |
[AdvMon] |
RSSISamplingPeriod |
9.3 持久化存储
Bluetooth 状态存储在 /var/lib/bluetooth/:
/var/lib/bluetooth/
├── XX:XX:XX:XX:XX:XX/ # 每适配器一个目录
│ ├── settings # 适配器设置
│ ├── cache/ # GATT 缓存
│ └── YY:YY:YY:YY:YY:YY/ # 每个配对设备一个目录
│ ├── info # 设备信息 (名称, 地址, 类等)
│ ├── primary # GATT 主服务
│ └── attributes # GATT 缓存属性
9.4 GATT 缓存策略
通过 main.conf 的 [GATT].Cache 选项控制:
always: 始终使用缓存yes: 使用缓存 (服务变更检测启用时)no: 禁用缓存,始终完整发现
10. 体系结构图
10.1 内部组件依赖
bluetoothd
│
┌─────────────────────┼─────────────────────┐
│ │ │
src/adapter.c src/device.c src/profile.c
│ │ │
├── mgmt socket ──────┤ │
│ │ │
profiles/audio/a2dp.c │ plugins/policy.c
│ │ │
profiles/input/hog.c src/gatt-database.c │
│ │ │
profiles/network/ src/gatt-client.c │
│ │ │
└──────────────┬──────┘ │
│ │
src/shared/ (lib) │
┌───────────┼───────────┐ │
│ │ │ │
gatt-db mgmt/hci io/queue ell/
10.2 数据流 (以 A2DP 音频为例)
[手机/蓝牙音箱] [Linux 桌面]
│ │
│ HCI 链路 (ACL+SCO) │
│ ──────────────────────────► │
│ kernel/bluetooth
│ │
│ MGMT_EV_CONNECTED
│ AVDTP start stream
│ ──────────────────────────► │
│ bluetoothd
│ │
│ profiles/audio/a2dp.c
│ │
│ ├── a2dp-sep.c (Sink Endpoint)
│ ├── avdtp.c (信令)
│ └── transport.c (传输)
│ │
│ D-Bus: MediaTransport1
│ │
│ [PulseAudio/PipeWire]
│ │ 解码 PCM → ALSA
│ ▼
│ [扬声器输出]
附录 A: 代码统计
| 目录 | 说明 | C 文件数 | 估计行数 |
|---|---|---|---|
src/ |
bluetoothd 核心 | 30+ | ~100,000 |
src/shared/ |
共享库 | 40+ | ~50,000 |
profiles/audio/ |
音频 Profile | 15+ | ~60,000 |
profiles/input/ |
HID Profile | 6 | ~10,000 |
profiles/network/ |
PAN Profile | 4 | ~5,000 |
profiles/ 其他 |
其他 Profile | 10+ | ~15,000 |
client/ |
bluetoothctl | 10+ | ~15,000 |
tools/ |
工具 | 30+ | ~30,000 |
monitor/ |
btmon 监控器 | 10+ | ~20,000 |
obexd/ |
OBEX 守护进程 | 20+ | ~20,000 |
mesh/ |
Mesh 网络 | 30+ | ~30,000 |
lib/ |
libbluetooth | 5+ | ~10,000 |
plugins/ |
内置插件 | 5+ | ~5,000 |
gdbus/ |
D-Bus 绑定 | 5+ | ~5,000 |
ell/ |
Embedded Linux Library | 30+ | ~30,000 |
btio/ |
BtIO 库 | 5+ | ~3,000 |
| 总计 | ~250+ | ~525,000 |
附录 B: D-Bus 操作路径总览
/org/bluez ─ Manager, AgentManager1, ProfileManager1
├── hci0 ─ Adapter1, GattManager1, LEAdvertisingManager1
│ ├── dev_XX_XX_XX_XX_XX_XX ─ Device1
│ │ ├── serviceXXXX ─ GattService1
│ │ │ ├── charXXXX ─ GattCharacteristic1
│ │ │ │ └── descXXXX ─ GattDescriptor1
│ ├── set_XXXX ─ DeviceSet1
│ └── fd_XXXX ─ Bearer1 (LE/BREDR)
└── hci1 ─ (同上)a







