BT_interface Bluedroid分析

870 阅读16分钟

基于Android P版本分析

Bluedroid

针对上层,蓝牙的核心就是AdapterService,它是蓝牙负责BT整个流程调度的中间服务,负责和JNI层进行交互,提供了控制蓝牙状态的调用逻辑;

对应的通过JNI与之交互的底层也有响应的逻辑。一般函数调用流程:APP -> FW -> JNI -> bluetoothinterface -> bluetooth HCI interface;

Bluedroid 架构

bluedroid架构.jpeg

bluedroid 作为蓝牙协议栈,实现了蓝牙的所有操作;bluedroid主要分为3个部分:BTIF、BTA、Stack;

  • BTIF:作为bluedroid与上层Java层的通道,提供了操作接口;
  • BTA:提供各种Profile的实现;
  • Stack:协议实现与连接管理;

整体的流转如下

事件 & 数据流转.png

Bluetooth HAL

一般情况下,Bluetooth HAL层定义了Bluetooth Process和bluedroid之间进行交互的标准接口,即简单的理解为h头文件。其中bluetooth h 头文件定义在/hardware/libhardware目录中,但是在B16项目中,我们使用了在/system/bt目录下也定义了相同的标准接口;

InterfaceDesc
bluetooth.h包含蓝牙硬件操作,设备管理和设备操作抽象接口
bt_av.h包含advanced audio profile的抽象接口
bt_hf.h包含handsfree profile的抽象接口
bt_hh.h包含hid host profile的抽象接口
bt_bl.h包含health profile的抽象接口
bt_pan.h包含pan profile的抽象接口
bt_sock.h包含socket操作的抽象接口
bt_gatt.h包含gatt 操作的抽象接口
bt_hd.h
bt_mce.h
bt_rc.h包含 avrcp profile的抽象接口
bt_sdp.h包含 sdp 操作的抽象接口

bluedroid 目录结构

bluedroid目录结构.png

我们描述几个重要的目录:

  • main:定处理配置信息,各个模块的初始化。链接 btif 和 hci,提供 btif 和 hci 的接口;

  • bta:bta用于Stack和Bluetooth Process层交互,实现蓝牙设备管理、状态管理以及一些Profile的bluedroid实现;

    • ag:实现BTA音频网关;
    • ar:负责Audio/Video注册;
    • av:实现BTA advanced audio/video;
    • dm:实现BTA设备管理;
    • gatt:实现通用属性配置文件;
    • hd:
    • hearing_aid:
    • hf_client:
    • hh:实现HID协议(人机交互);
    • hl:实现HDP协议(健康);
    • mce:实现MAP协议;
    • pan:实现PAN(蓝牙个人局域网)协议;
    • pb:实现PBAP协议;
    • sdp:实现SDP(简单发现)协议;
    • sys:主要实现BTA系统管理;
  • btif:提供所有Bluetooth Process需要的API;

  • hci:HCI library的实现,主要内容包括HCI接口的打开和收/发控制、so的打开和回调注册、LPM的实现、btsnoop的抓取。读取或写入数据到蓝牙hw;

  • stack:协议实现与连接管理,协议包a2dp、avctp、avdtp、avrcp、bnep、gap、gatt、hid、l2cap、pan、rfcomm、sdp、mcap(多通道适配协议)、smp(用于生成对等协议的加密密钥和身份密钥)等;

    • btm:bluetooth Manager;
    • btu:该功能主要用于核心协议层之间的事件处理与转换,连接btif和HCI;
    • gap:通用访问协议,定义了设备如何发现,建立连接,实现绑定;
    • gatt:通用属性配置文件实现
    • mcap:多通道适配协议
    • smp:用于生成对等协议的加密密钥和身份密钥

bluetooth.h

我们现在存在两个bluetooth.h,一个在/hardware目录下,一个在/system/bt/include目录下,根据引入bluetooth.h不同来确定使用bluetooth.c还是bluetooth.cc;

/system/bt bluetooth.h/system/bt bluetooth.ccDesc
int (*init)init初始化
int (*enable)enable使能/开启
int (*disable)disable释能/关闭
void (*cleanup)cleanup关闭协议栈接口
int (*get_adapter_properties)get_adapter_properties获取所有的Adapter Property
int (*get_adapter_property)get_adapter_property根据指定的Type来获取对应的Adapter Property
int (*set_adapter_property)set_adapter_property通过指定的Type设置对应的Adapter Property
int (*get_remote_device_properties)get_remote_device_properties获取远端设备的所有Property
int (*get_remote_device_property)get_remote_device_property根据指定的Type来获取远端设备的Property
int (*set_remote_device_property)set_remote_device_property通过指定的Type设置对应远端设备的Property
int (*get_remote_service_record)get_remote_service_record根据UUID信息获取远端设备的Service 列表
int (*get_remote_services)get_remote_services开启 SDP 协议获取远程 Service
int (*start_discovery)start_discovery开始搜寻发现
int (*cancel_discovery)cancel_discovery终止搜寻发现
int (*create_bond)create_bond创建蓝牙绑定
int (*create_bond_out_of_band)create_bond_out_of_band使用带外数据创建蓝牙绑定
int (*add_out_of_band_bond_device)add_out_of_band_bond_device添加带外绑定设备
void (*get_link_key)get_link_key获取链接key Message
int (*remove_bond)remove_bond移除绑定
int (*cancel_bond)cancel_bond终止绑定
int (*get_connection_state)get_connection_state获取连接状态
int (*pin_reply)pin_replyBT 传统 PIN码 恢复
int (*ssp_reply)ssp_reply
const void* (*get_profile_interface)get_profile_interface获取蓝牙配置文件接口
int (*dut_mode_configure)dut_mode_configure配置DUT模式
int (*dut_mode_send)dut_mode_send发送DUT模式
int (*le_test_mode)le_test_modeBLE 测试模式 API
int (*set_os_callouts)set_os_callouts设置bluedroid用于报警和唤醒锁的操作系统调出功能。应在init执行成功之后立即调用此函数
int (*read_energy_info)read_energy_info读取Energy详细信息
void (*dump)dumpdump信息
void (*dumpMetrics)dumpMetrics
int (*config_clear)config_clear清空/data/misc/bt_config.conf文件,其中包含了BT device(localhost and Remote)信息
void (*interop_database_clear)interop_database_clear清除(重置)设备互操作性数据库的动态部分
void (*interop_database_add)interop_database_add添加设备互操作性数据库的动态部分。
bluetooth::avrcp::ServiceInterface* (*get_avrcp_service)get_avrcp_service获取AvrcpTarget服务接口以与Avrcp服务交互
int (*read_local_oob_data)read_local_oob_data读取带外数据
std::string (*get_profiles_version)get_profiles_version获取profiles版本信息
int (*incoming_connection_reply)incoming_connection_reply来电连接回复

上述的这些接口,基本上涉及到的都是蓝牙整个流程调度、蓝牙状态控制的逻辑,基本上对应的就是上层AdapterService;

我们可以基于BT的启动流程,追踪分析底层协议栈的加载和启动流程;

HCI Command

Command

Bluetooth HCI Command - Write Class of Device
    Command Opcode: Write Class of Device (0x0c24)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0010 0100 = Opcode Command Field: Write Class of Device (0x024)
    Parameter Total Length: 3
    Class of Device: 0x360408 (Audio/Video:Hands-free Device - services: Networking Rendering ObjectTransfer Audio)
        0000 10.. = Minor Device Class: Hands-free Device (0x02)
        .... ..00 = Format Type: 0x0
        0... .... .... .... = Major Service Classes: Information: False
        .0.. .... .... .... = Major Service Classes: Telephony: False
        ..1. .... .... .... = Major Service Classes: Audio: True
        ...1 .... .... .... = Major Service Classes: Object Transfer: True
        .... 0... .... .... = Major Service Classes: Capturing: False
        .... .1.. .... .... = Major Service Classes: Rendering: True
        .... ..1. .... .... = Major Service Classes: Networking: True
        .... ...0 .... .... = Major Service Classes: Positioning: False
        .... .... 00.. .... = Major Service Classes: Reserved: 0x0
        .... .... ..0. .... = Major Service Classes: Limited Discoverable Mode: False
        .... .... ...0 0100 = Major Device Class: Audio/Video (0x04)
    [Response in frame: 96]
    [Command-Response Delta: 1.451ms]

我们以Write Class of Device HCI Command为例,说明一下Command Opcode;

Command Opcode分为两个字段:

  • Opcode Group Field(OGF):组域段,高6bit

    • Link Control Commands(0x01):链路控制OGF,也就是控制蓝牙芯片跟remote通信的Command;
    • Link Policy Commands(0x02):链路策略OGF,也就是写一些Policy,比如转换角色等;
    • Host Controller and Baseband Commands(0x03):控制本地芯片和基带的OGF,比如reset 本地芯片;
    • Informational Parameters Commands(0x04):读取信息的OGF,比如读取本地芯片的LMP版本、支持的Command、蓝牙地址信息等;
    • Status Parameters Commands(0x05):状态参数OGF,例如读取RSSI等;
    • Testing commands(0x06):测试命令的OGF,例如让芯片进入测试模式(DUT,Device under test);
    • LE Controller Commands(0x08):BLE(低功耗)命令;
    • Vendor-Specific Debug Commands(0x3F):该OGF用于特定于供应商的调试指令,此部分是vendor定义的,也就是芯片厂商为了扩展core文档的HCI command定义;
  • Opcode Command Field(OCF):指令域段,低10bit,OCF定义众多,在每个OGF下都有一系列的OCF定义;

Parameter Total Length:后续参数的长度;

传统蓝牙HCI Command

OGF(Opcode Group Field)OCF(Opcode Command Field)CommandDesc
Link Control command(0x01):链路控制指令0x0001Inquiry command让BR/EDR芯片进行搜索模式,搜索周边的BR/EDR设备
0x0002Inquiry Cancel command让BR/EDR芯片进入周期搜索状态
0x0003Periodic Inquiry Mode command让BR/EDR芯片进入周期搜索状态
0x0004Exit Periodic Inquiry Mode command让进入周期搜索状态的BR/EDR蓝牙芯片退出周期搜索状态
0x0005Create Connection command根据参数跟特定的蓝牙地址创建连线
0x0006Disconnect command根据参数跟特定的连接句柄断开连接,可以用来断开ACL,也可以用来断开SCO,eSCO等等
0x0008Create Connection Cancel command“连接中”的取消指令
0x0009Accept Connection Request command接受连接请求
0x000AReject Connection Request command拒绝连接请求
0x000BLink Key Request Reply command对方请求linkkey的时候,而我们有此link key,我们回复link key
0x000CLink Key RequestNegative Reply command对方请求linkkey的时候,而我们没有次link key的情形下回复
0x000DPIN Code Request Reply command用于应答芯片发送的pincode申请,并把pincode用于连线
0x000EPIN Code RequestNegative Reply command不能用于pin code连线
0x000FChange Connection Packet Type command改变正在连接的交互封包类型
0x0011Authentication Requested command用于指令连接句柄的两个蓝牙地址进行身份认证
0x0013Set Connection Encryption command建立或者取消连接层次的加密
0x0015Change Connection Link Key command用于指定额链接句柄,生成一组新的link key
0x0017Master Link Key command用于强制主设备使用主设备的临时或半永久link key
0x0019Remote Name Request command获取远端设备名称
0x001ARemote Name Request Cancel command取消获取远端设备名称
0x001BRead Remote Supported Features command获取remote端支持的feature
0x001CRead Remote Extended Features command获取remote端支持的扩展feature
0x001DRead Remote Version Information command获取remote端版本信息,注意此部分是LMP或者LL的版本
0x001FRead Clock Offset command获取时钟偏移
0x0020Read LMP Handle command获取LMP句柄
0x0028Setup Synchronous Connection command建立SCO
0x0029Accept Synchronous Connection Request command接受对方SCO建立请求
0x002AReject SynchronousConnection Request command拒绝对方SCO建立请求
0x002BIO Capability Request Reply command当在SSP配对模式下,响应芯片IO capability的请求
0x002CUser Confirmation Request Reply command当在SSP配对模式下,响应芯片IO User Confirmation的请求
0x002DUser Confirmation Request Negative Reply command当在SSP配对模式下,拒绝芯片IO User Confirmation的请求
0x0034IO Capability RequestNegative Reply command当在SSP配对模式下,拒绝芯片IO capability的请求
Link Policy commands (0x02):链路策略指令0x0001Hold Mode command用于更改链接的行为,并让它将指定的Connection_Handle关联的ACL基带连接放置到Holdmode中
0x0003Sniff Mode command用于改变该链接的行为管理并让它将与指定Connection_Handle关联的ACL基带连接置于嗅探模式
0x0004Exit Sniff Mode command离开嗅探模式
0x0007QoS Setup command用于为Connection_Handle指定服务质量参数
0x0009Role Discovery command查询当前的连线芯片处于什么角色
0x000BSwitch Role command进行角色切换
0x000CRead Link Policy Settings command读取当前connection_handle的连接策略
0x000DWrite Link Policy Settings command改变当前连线的连接策略
0x000ERead Default Link Policy Settings command读取默认的连接策略
0x000FWrite Default Link Policy Settings command改变当前默认的连接策略
0x0010Flow Specification command用于指定通过Connection_Handle标识的ACL Connection_Handle传输的流量的流参数
0x0011Sniff Subrating command为给定链接指定用于嗅探子例程的参数
Control and Baseband commands(0x03):主机控制器与基带指令0x0001Set Event Mask command用于控制HCI为主机生成哪些事件
0x0003Reset commandreset蓝牙芯片(软复位)
0x0005Set Event Filter command设置事件过滤
0x0008Flush command丢弃芯片中正在等待传输的数据
0x0009Read PIN Type command读取PIN 类型
0x000AWrite PIN Type command写PIN类型
0x000DRead Stored Link Key command读取芯片中存储的Link key
0x0011Write Stored Link Key command把link key写入到芯片中
0x0012Delete Stored Link Key command删除芯片中的Link key
0x0013Write Local Name command写入本地名称到芯片中
0x0014Read Local Name command读取芯片的蓝牙名称
0x0015Read Connection Accept Timeout command读取蓝牙连接接收的超时时间
0x0016Write Connection Accept Timeout command写入蓝牙连接接收的超时时间
0x0017Read Page Timeout command读取page的超时时间
0x0018Write Page Timeout command写page的超时时间
0x0019Read Scan Enable command读取scan enable
0x001AWrite Scan Enable command写scan enable
0x001BRead Page Scan Activity command读取Page Scan Activity
0x0023Read Class of Device command读取芯片的cod
0x0024Write Class of Device command把cod写入cod
0x0025Read Voice Setting command读取voice setting
0x0026Write Voice Setting command把voice setting写入到芯片
0x0031Set Controller To Host Flow Control command启动HCI 流控
0x0033Host Buffer Size command把buffer size写入到芯片中
0x0036Read Link Supervision Timeout command读取Link Supervision timeout时间(也就是Link lost时间)
0x0037Write Link Supervision Timeout command把Link Supervision timeout写入到芯片(也就是Link lost时间)
0x0044Read Inquiry Mode command读取芯片的搜索模式
0x0045Write Inquiry Mode command把搜索模式写入到芯片中
0x0051Read Extended Inquiry Response command读取芯片的Extended inquiry response
0x0052Write Extended Inquiry Response command把Extended inquiry response写入到芯片
0x0055Read Simple Pairing Mode command读取芯片是否支持SSP
0x0056Write Simple Pairing Mode command把是否支持SSP写入到蓝牙芯片
Informational Parameters commands(0x04):信息参数指令0x0001Read Local Version Information command读取本地芯片的LMP version
0x0002Read Local Supported Commands command读取本地芯片支持的command
0x0003Read Local Supported Features command读取本地芯片支持的feature
0x0004Read Local Extended Features command读取本地芯片支持的扩展feature
0x0005Read Buffer Size command读取本地芯片的buffer size
0x0009Read BD_ADDR command读取本地芯片的蓝牙地址
0x000ARead Data Block Size command读取本地芯片的block size
0x000B/DRead Local Supported Codecs command读取本地芯片支持的编码格式
0x000CRead Local Simple Pairing Options command读取本地SSP选项
0x000ERead Local Supported Codec Capabilities command读取本地芯片支持的codec capabilities
status parameters commands (0x05):状态参数指令0x0003Read Link Quality command读取连接质量
0x0005Read RSSI command读取接收信号强度
0x0007Read Clock command读取clock
Testing commands (0x06):测试指令0x0001Read Loopback Mode command去读loopback模式
0x0002Write Loopback Mode command写loopback模式
0x0003Enable Device Under Test Mode command使设备进去测试模式

HCI Event

理论上,每一个HCI Command都会对应一个HCI Event,很多HCI Command对应的HCI Event为同一个,Command 和 Event的关系为:多对一的关系;

HCI Event同理也分为BLE专用和传统的HCI Event;

传统蓝牙HCI Event

Event CodeEVENTDesc
0x01Inquiry Complete Event搜索完成
0x02Inquiry Result Event搜索结果
0x03Connection Complete Event连接完成
0x04Connection Request Event连接请求
0x05Disconnection Complete断开完成
0x06Authentication Complete event授权完成
0x07Remote Name Request Complete event获取远端蓝牙名称完成
0x08Encryption Change event加密模式改变完成
0x09Change Connection Link Key Complete event改变连接的link key完成
0x0AMaster Link Key Complete event管理Link key完成
0x0BRead Remote Supported Features Complete event读取远端设备feature完成
0x0CRead Remote VersionInformation Complete event读取远端版本完成
0x0DQoS Setup Complete eventQos setup完成
0x0ECommand Complete event命令完成
0x0FCommand Status event命令状态消息
0x10Hardware Error event蓝牙芯片硬件错误
0x12Role Change event角色切换事件
0x13Number Of Completed Packets event完成封包个数
0x14Mode Change event模式改变
0x15Return Link Keys event返回link key
0x16PIN Code Request eventPIN code请求
0x17Link Key Request eventLink key请求
0x18Link Key Notification eventLink key通知
0x19Loopback Command eventLoopback模式返回的事件
0x1AData Buffer Overflow event芯片buffer爆掉
0x1BMax Slots Change event最大slot变化
0x1CRead Clock Offset Complete event读取时钟偏移完成
0x1DConnection PacketType Changed event连接封包类型变化
0x1EQoS Violation eventQos违规
0x22Inquiry Result with RSSI event带RSSI的搜索结果
0x2CSynchronous Connection Complete eventSCO连接完成
0x2FExtended Inquiry Result event带EIR的搜索结果
0x31IO Capability Request eventIO capability请求
0x32IO Capability Response eventIO capability回复
0x33User Confirmation Request eventSSP配对方式用户确认请求
0x36Simple Pairing Complete eventSSP配对完成

L2CAP信令指令码

CodePrevious Name(Core_4.2)Current Description(Core_5.3)Desc
0x00RESERVEDRESERVED保留
0x01Command rejectL2CAP_COMMAND_REJECT_RSP拒绝命令
0x02Connection requestL2CAP_CONNECTION_REQ连接请求
0x03Connection responseL2CAP_CONNECTION_RSP连接响应
0x04Configure requestL2CAP_CONFIGURATION_REQ配置请求
0x05Configure responseL2CAP_CONFIGURATION_RSP配置响应
0x06Disconnection requestL2CAP_DISCONNECTION_REQ断开请求
0x07Disconnection responseL2CAP_DISCONNECTION_RSP断开响应
0x08Echo requestL2CAP_ECHO_REQ
0x09Echo responseL2CAP_ECHO_RSP
0x0AInformation requestL2CAP_INFORMATION_REQ信息请求
0x0BInformation responseL2CAP_INFORMATION_RSP信息响应