现在的位置: 首页 > 综合 > 正文

FTS抓包看蓝牙的SDP整个过程

2019年04月25日 ⁄ 综合 ⁄ 共 4445字 ⁄ 字号 评论关闭

1.概述

  SDP是蓝牙的Service Discovery Protocol,用来发现远程设备能够提供的Service。它只负责发现对方支持的Service,不负责Service的具体实现。

2.背景概念
   SDP中的每一个Service用ServiceRecord来表示(具有唯一的32bit的Handle),每一个ServiceRecord由若干ServiceAttribute组成,ServiceAttribute由Attribute ID和Attribute Valuel两个部分。
   通过SDP request可以访问这些record,以此来获取设备的服务信息。
   Data Element是SDP中使用的一种数据结构,如下:
FTS抓包看蓝牙的SDP整个过程
具体的Type和Size Index可以查询Spec p1580和p1581。Data element可以用来表示attribute ID,Attribute range,Attribute value。
  每一个Service record至少包含两种attribute:
1.ServiceRecordHandle (Attribute ID = 0x0000)
2.ServiceClassIDList (Attribute ID = 0x0001)
ServiceRecordHandle是一个32位整型,唯一标识某一SDP中的Service record
ServiceClassIDList表示该Service record所属的Service Classes
传输的request/resposne有如下的结构:
FTS抓包看蓝牙的SDP整个过程

3.关于SDP的一些Frame---本地hsot进行request,远端slave进行resposne
   首先是本地的Host进行一系列的SDP_ServiceSearchAttributeRequest,远端Slave进行resposne。这个request的说明如下:
FTS抓包看蓝牙的SDP整个过程
ServiceSearchPattern指定了要寻找的Service record
AttributeIDList表明了要寻找的Service record中的某些Attributes
下面我们可以通过一些Frame来具体看看这个request。

Frame91:SDP_ServiceSearchAttributeRequest
00001001 00000000 00010100 00000000 00010100 00000000 01000100 00000000 00000110 00000000
00000000
 00000000 00001111 00110101 00000011 00011001 00000001 00000000 00000011
11111000
 00110101 00000101 00001010 00000000 00000000 11111111 11111111 00000000
最前面四个字节是HCI层的包装
Connection Handle: 00001001 00000000 = 0x9 = 9
Total Length: 00010100 00000000 = 0x0014 = 20
下面两个字节是L2CAP层的包装
PDU Length: 00010100 00000000 = 0x0014 = 20
Channel ID: 01000100 00000000 = 0x0044
下面是SDP的部分:
PDU ID: 是SDP_ServiceSearchAttributeRequest
Transaction ID: 0x0000
Parameter Length: 0x000f = 15  //这里需要注意的是,SDP是MSB!!!
后面是SDP request的parameter部分
ServiceSearchPattern:这部分是一系列的Data Element,参考Data element解释如下:
00110101:00110 = 6 表示数据部分是Data element sequence  101 = 5表示下面的一个字节表示该Data Element的大小
00000011 = 3表示该Data Element大小为3个字节
00011001(Data element sequence中的第一个Data element): 00011 = 3 表示是UUID  001 = 1表示2 bytes
00000001 00000000 = 0x0100 表示实际的UUID:查询UUDI值,表示L2CAP
Max Amount of Attribute Data to Return:0x03f8 =  1016
List of Requested Attributes: 这又是一个Data element sequence,分析如下:
00110101: 00110 = 6表示数据部分是Data element sequence 101 = 5表示下面的一个字节表示该Data Element的大小
00000101 = 5表示该Data Element大小为5个字节
00001010: 00001 = 1表示Unsigned Integer 010 = 2表示4 bytes 
这样表示的就是一个attribute ID range
00000000 00000000 11111111 11111111:表示的就是从0x0000到0xffff的attribute ID
Bytes for continuation length: 0 
注:这个request的目的应该是搜索L2CAP Service的所有Attributes。PS:不知道哪个2B搞的协议,需要弄得这么复杂吗??

Frame92:SDP_ServiceSearchAttributeResponse 
远端slave的resposne,携带参数如下:
AttributeListsByteCount,
AttributeLists,
ContinuationState
L2CAP显示的PDU的长度是256,数据茫茫。。。还是挑一些重点看看吧。
PDU ID: 00000111 = 0x07 SDP_ServiceSearchAttributeResponse
Transaction ID: 0x0000
Attribute List Byte Count: 246  //PS:靠,attributeLists有这么多字节
AttributeListsByteCount = 00000000 11110110 = 0x00f6 = 246
看一下AttributeLists的开头3个字节:00110110 00000011 00111101
00110110:00110表示是Data element sequence 110 = 6表示下面两个字节表示该Data Element的大小
00000011 00111101 = 0x33d = 829 //这么多字节是必须用到ContinuationState了
随便看几个Attribute吧:
第一个AttributeList:
前三个字节是00110110 00000000 01001110 解析如下(PS:写解析软件的公司真牛逼):
00110110: 00110表示是Data element sequence 110 = 6表示下面两个字节表示该Data Element的大小
00000000 01001110 = 0x004e = 78  //第一个AttributeList有78个字节
第一个Attribute:00001001 00000000 00000000
00001表示Unsigned Integer 001表示2个字节 (Attribute ID是16位的)
Attribute ID为00000000 00000000,表示ServiceRecordHandle
Attribute value为0x00010000
再看一个Attribute
00001001 00000000 00000001 00110101 00000110 00011001 00010001
00010010
 00011001 00010010 00000011 
00001001:无符号整数,2个字节
00000000 00000001 = 0x01:Attribute ID = 0x01:是ServiceClassIDList属性
00110101 00000110:UUID,6个字节
00011001 00010001 00010010:UUID,2个字节,Attribute
value为0x1112
00011001 00010010 00000011:UUID,2个字节,Atribute value为0x1203
其余的attribute不看了,太多了,哪个Sb制定的协议!!
最后的ContinuationState:00000000 11110110 = 0x00f6  //这次response不足以把所有的attribute传回,0x00f6会用在下一次的SDP_ServiceSearchAttributerequest中。如果足以传回所有的attribute,则ContinuationState为1个字节=0.
注:以上是一个SDP_ServiceSearchAttributeRequest和一个SDP_ServiceSearchAttributeResponse。下面又是一组request和resposne。

Frame100:SDP_ServiceSearchAttributeRequest
相比于上一个SDP_ServiceSearchAttributeRequest不同的是,transaction ID为0x0001, Continuation length bytes为0x00f6,是上次的resposne返回的,说明这次的request是要求对方继续发送上次没有发送完的response。
直到frame128才完成所有的response,在这个过程中,远端的slave也开始向本地的Host发送SDP request。

3.远端slave向本地的Host发送requsst,本地的Host进行resposne
Frame110:SDP_ServiceSearchAttributeRequest
远端的slave在Frame110时就已经向本地Host发送SDP request,可以看出,双方的SDP request和resposne是穿插的,并不是一定要等一方完成后另一方才能进行SDP request。
Frame110是关于pnp Information的requset,本地的Host也在Frame121向远端slave发送了这个SDP requset。

4.总结:
    SDP用于发现对方能够提供的服务,通信方式为SDP requset和SDP resposne。从抓取的包来看,两端的SDP Service 都会向对方发送SDP requset。需要注意在SDP中是MSB的字节顺序,与L2CAP和HCI不同,另外,需要注意Data Element,每个Attribute的ID和Value都是由它来表示的。
PS:我只想说,制定这种协议的人可以去死了。

抱歉!评论已关闭.