主要介绍下蓝牙协议栈(bluetooth stack)低功耗蓝牙搜索广播的流程以及协议栈的实现流程,BLE scan flow
btsnoop以及流程在资料中的......\STM32_UBUNTU_BLUETOOTH\2-蓝牙资料\蓝牙协议分析\BLE搜索广播.log
本专栏文章我们会以连载的方式持续更新,本专栏计划更新内容如下:
第一篇:蓝牙综合介绍 ,主要介绍蓝牙的一些概念,产生背景,发展轨迹,市面蓝牙介绍,以及蓝牙开发板介绍。
第二篇:Transport层介绍,主要介绍蓝牙协议栈跟蓝牙芯片之前的硬件传输协议,比如基于UART的H4,H5,BCSP,基于USB的H2等
第三篇:传统蓝牙controller介绍,主要介绍传统蓝牙芯片的介绍,包括射频层(RF),基带层(baseband),链路管理层(LMP)等
第四篇:传统蓝牙host介绍,主要介绍传统蓝牙的协议栈,比如HCI,L2CAP,SDP,RFCOMM,HFP,SPP,HID,AVDTP,AVCTP,A2DP,AVRCP,OBEX,PBAP,MAP等等一系列的协议吧。
第五篇:低功耗蓝牙controller介绍,主要介绍低功耗蓝牙芯片,包括物理层(PHY),链路层(LL)
第六篇:低功耗蓝牙host介绍,低功耗蓝牙协议栈的介绍,包括HCI,L2CAP,ATT,GATT,SM等
第七篇:蓝牙芯片介绍,主要介绍一些蓝牙芯片的初始化流程,基于HCI vendor command的扩展
第八篇:附录,主要介绍以上常用名词的介绍以及一些特殊流程的介绍等。
另外,开发板如下所示,对于想学习蓝牙协议栈的最好人手一套。以便更好的学习蓝牙协议栈,相信我,学完这一套视频你将拥有修改任何协议栈的能力(比如Linux下的bluez,Android下的bluedroid)。
-------------------------------------------------------------------------------------------------------------------------
CSDN学院链接(进入选择你想要学习的课程):https://edu.csdn.net/lecturer/5352?spm=1002.2001.3001.4144
蓝牙交流扣扣群:970324688
Github代码:https://github.com/sj15712795029/bluetooth_stack
入手开发板:https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-22329603896.18.5aeb41f973iStr&id=622836061708
蓝牙学习目录:https://blog.csdn.net/XiaoXiaoPengBo/article/details/107727900
--------------------------------------------------------------------------------------------------------------------------
整个流程如下(注意在初始化的时候有省略的步骤,只列出初始化的关键步骤)
注意我们在截图初始化的时候只是截取了两个command以及event,一个是跟传统蓝牙有差别的set event mask,一个是write le host supported,其他可以参照传统蓝牙的初始化,步骤整理如下:
步骤1)发送设置事件掩码的command(set event mask)以及收到commnd complete event
步骤2)发送设置支持BLE的command(write le host support)收到command complete event
步骤3)发送设置BLE scan参数的command(LE set scan param)
步骤4)发送BLE搜索使能的command(LE set scan enable)
步骤5)收到步骤3)4)的command complete
步骤6)解析BLE广播event的数据包
步骤7)发送结束搜索的command(LE set scan enable)以及收到commnd complete event
下面我们就详细说下每个步骤
① 发送设置事件掩码的command(set event mask)
首先我们来看下这个command格式,OGF=3,OCF=1
一共8byte的参数,一共有62个mask,这个command就是掩码,决定芯片是否要上报给协议栈这个event,此部分我们传统蓝牙跟BLE蓝牙发送的参数有差别,截图code如下:
可以看到就差了bit61,也就是LE Meta Event,此部分很多BLE的event都会通过这个上来
我们来看下btsnoop
② 收到command complete event
此event我们在传统蓝牙HCI章节中多次讲到不再重复
我们来看下btsnoop
① 发送设置支持BLE的command(write le host support)
首先我们来看下command的格式,OGF=3,OCF=0x6d
可以看到如果要使能BLE,那么LE SUPPORTED HOST要设置为0x01,SIMULTANEOUS LE HOST只能设置为0,其他用在扩展使用。
我们来看下btsnoop
② command complete event
此event我们在传统蓝牙HCI章节中多次讲到不再重复
我们来看下btsnoop
首先我们来看下命令格式(OGF=8,OCF=0xb)
下面来具体说下参数的意义:
LE_Scan_Type:分主动扫描跟被动扫描
区别主要有几个
一般被动扫描用于确定从机不会发送扫描响应,只会发送31byte的广播数据
而主动扫描用于不确定从机是否有额外的数据,所以要额外发起扫描请求来接受更多的广播的数据
注意:主动扫描的扫描请求以及扫描响应也是广播封包
LE_Scan_Window跟LE_Scan_Interval:
看以上图你就懂这两个参数了
我们来看下btsnoop
我们来看下command格式(OGF=8,OCF=0xc)
我们看下btsnoop
这个command我们已经在传统蓝牙HCI介绍了,我们直接贴下btsnoop
此部分的event code是0x3e(LE Meta event)
其中Subevent_Code = 2就是LE广播封包的event
我们首先来看下参数
这是一个关键的event,我们来详细说下广播数据,其他参数自己体会
广播参数格式如下:
一个1byte的length,n byte的type,后面跟的是这个item的广播数据,符合L T V格式
L:length T:Type V:value
其中Type跟HCI EIR基本一样,在文档CSS_V9中,想详细看的可以看下
Data Type Value | Data Type Name | Reference for Definition |
---|---|---|
0x01 | «Flags» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.3 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.3 and 18.1 (v4.0)Core Specification Supplement, Part A, section 1.3 |
0x02 | «Incomplete List of 16-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.1 and 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x03 | «Complete List of 16-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.1 and 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x04 | «Incomplete List of 32-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, section 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x05 | «Complete List of 32-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, section 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x06 | «Incomplete List of 128-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.1 and 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x07 | «Complete List of 128-bit Service Class UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.1 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.1 and 18.2 (v4.0)Core Specification Supplement, Part A, section 1.1 |
0x08 | «Shortened Local Name» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.2 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.2 and 18.4 (v4.0)Core Specification Supplement, Part A, section 1.2 |
0x09 | «Complete Local Name» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.2 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.2 and 18.4 (v4.0)Core Specification Supplement, Part A, section 1.2 |
0x0A | «Tx Power Level» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.5 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.5 and 18.3 (v4.0)Core Specification Supplement, Part A, section 1.5 |
0x0D | «Class of Device» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.6 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.5 and 18.5 (v4.0)Core Specification Supplement, Part A, section 1.6 |
0x0E | «Simple Pairing Hash C» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.6 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.5 and 18.5 (v4.0) |
0x0E | «Simple Pairing Hash C-192» | Core Specification Supplement, Part A, section 1.6 |
0x0F | «Simple Pairing Randomizer R» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.6 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.5 and 18.5 (v4.0) |
0x0F | «Simple Pairing Randomizer R-192» | Core Specification Supplement, Part A, section 1.6 |
0x10 | «Device ID» | Device ID Profile v1.3 or later |
0x10 | «Security Manager TK Value» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.7 and 18.6 (v4.0)Core Specification Supplement, Part A, section 1.8 |
0x11 | «Security Manager Out of Band Flags» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.6 and 18.7 (v4.0)Core Specification Supplement, Part A, section 1.7 |
0x12 | «Slave Connection Interval Range» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.8 and 18.8 (v4.0)Core Specification Supplement, Part A, section 1.9 |
0x14 | «List of 16-bit Service Solicitation UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.9 and 18.9 (v4.0)Core Specification Supplement, Part A, section 1.10 |
0x15 | «List of 128-bit Service Solicitation UUIDs» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.9 and 18.9 (v4.0)Core Specification Supplement, Part A, section 1.10 |
0x16 | «Service Data» | Bluetooth Core Specification:Vol. 3, Part C, sections 11.1.10 and 18.10 (v4.0) |
0x16 | «Service Data - 16-bit UUID» | Core Specification Supplement, Part A, section 1.11 |
0x17 | «Public Target Address» | Bluetooth Core Specification:Core Specification Supplement, Part A, section 1.13 |
0x18 | «Random Target Address» | Bluetooth Core Specification:Core Specification Supplement, Part A, section 1.14 |
0x19 | «Appearance» | Bluetooth Core Specification:Core Specification Supplement, Part A, section 1.12 |
0x1A | «Advertising Interval» | Bluetooth Core Specification:Core Specification Supplement, Part A, section 1.15 |
0x1B | «LE Bluetooth Device Address» | Core Specification Supplement, Part A, section 1.16 |
0x1C | «LE Role» | Core Specification Supplement, Part A, section 1.17 |
0x1D | «Simple Pairing Hash C-256» | Core Specification Supplement, Part A, section 1.6 |
0x1E | «Simple Pairing Randomizer R-256» | Core Specification Supplement, Part A, section 1.6 |
0x1F | «List of 32-bit Service Solicitation UUIDs» | Core Specification Supplement, Part A, section 1.10 |
0x20 | «Service Data - 32-bit UUID» | Core Specification Supplement, Part A, section 1.11 |
0x21 | «Service Data - 128-bit UUID» | Core Specification Supplement, Part A, section 1.11 |
0x22 | «LE Secure Connections Confirmation Value» | Core Specification Supplement Part A, Section 1.6 |
0x23 | «LE Secure Connections Random Value» | Core Specification Supplement Part A, Section 1.6 |
0x24 | «URI» | Bluetooth Core Specification:Core Specification Supplement, Part A, section 1.18 |
0x25 | «Indoor Positioning» | Indoor Positioning Service v1.0 or later |
0x26 | «Transport Discovery Data» | Transport Discovery Service v1.0 or later |
0x27 | «LE Supported Features» | Core Specification Supplement, Part A, Section 1.19 |
0x28 | «Channel Map Update Indication» | Core Specification Supplement, Part A, Section 1.20 |
0x29 | «PB-ADV» | Mesh Profile Specification Section 5.2.1 |
0x2A | «Mesh Message» | Mesh Profile Specification Section 3.3.1 |
0x2B | «Mesh Beacon» | Mesh Profile Specification Section 3.9 |
0x2C | «BIGInfo» | |
0x2D | «Broadcast_Code» | |
0x3D | «3D Information Data» | 3D Synchronization Profile, v1.0 or later |
0xFF | «Manufacturer Specific Data» | Bluetooth Core Specification:Vol. 3, Part C, section 8.1.4 (v2.1 + EDR, 3.0 + HS and 4.0)Vol. 3, Part C, sections 11.1.4 and 18.11 (v4.0)Core Specification Supplement, Part A, section 1.4 |
我们来分析一组raw data让你加深理解,方便自己写code的时候解析广播数据
Raw data为:02 01 06 03 03 00 FF 04 08 42 4c 45 03 19 80 00
02 -》此个item的length为2
01-》Type为1,也就是«Flags»
06 -》 flag的值
03 -》此个item的length为3
03 -》«Complete List of 16-bit Service Class UUIDs»
00 FF -》UUID的值
04 -》此个item的length为4
08 -》«Shortened Local Name»
42 4c 45 -》 名字为BLE
03 -》此个item的length为3
19 -》«Appearance»
80 00 -》 «Appearance»的值
LE set scan enable我们已经在步骤4)说明了,我们直接贴下btsnoop