一、介绍和概述
1、ATT(attribute protocol)有两种角色的设备,分别是server和client;
server向client开放一套attribute集合及其相关的值,server开放的attribute可以被client执行discovered、read、written,server也可以向client设备发送notification和indication
2、attribute有3个properties,分别是:attribute type、attribute handle、permissions
(1)attribute type(即UUID):作用是让client理解这个attribute的含义,在Bluetooth SIG assigned numbers中有定义和含义说明,当然也可以使用Bluetooth SIG assigned numbers定义之外的值,一个server上的多个attribute可能有相同的attribute type
(2)attribute handle:即attribute的唯一标识符,client可以检索server中attributes的handle,可以使用handle对attribute进行read和write请求,也可以通过handle确定notification或者indication中对应的attribute。
(3)permissions:即attribute的权限,用来表示attribute是否可以被读写,是否需要加密
3、client可以发送attribute protocol request给server,server需要对收到的所有request做出回复;一个设备可以同时是client和server,但是一个设备只能有一个server,一个server可以有多个service,一个server可以被多个client连接
4、Notification和indication是server主动发送attribute值给client的手段
二、基础概念:
1、Attribute Type
当attribute UUID包含在ATT PDU中时,所有32位的UUID必须转换为128位的UUID
2、Attribute Handle
Attribute handle是一个16bit的非零值,最大值是0xFFFF,这个值是server自己分配的,不可以重复使用,attribute是按照attribute handle进行排序的
注意:当一个ATT Bearer处于active时,attribute可以被添加和删除,一个attribute删除后,它对应的attribute handle不可以被其他attribute使用
3、Attribute Handle Grouping
Grouping is defined by a specific attribute placed at the beginning of a range of
other attributes that are grouped with that attribute, as defined by a higher layer
specification. Clients can request the first and last handles associated with a
group of attributes.
4、Attribute Value
Attribute value是一个字节数组,长度可能是固定的也可能是可变的,例如:可能是1个字节的值、或者4个字节的int、或者可变长度的字符串。一个attribute包含的attribute value长度可能大于一个PDU的长度,所以会分成多个PDU传输。Attribute value数组的编码由attribute type决定
在request、response、notification、indication中传输attribute value时,PDU中时没有attribute value length这个字段的,attribute value length是可以通过PDU数据包的长度计算出来的:
(1)在一个request、response、notification、indication中只能有一个attribute value,除非server和client都可以通过attribute type确定attribute value的长度。
(2)在一个request、response、notification、indication中只有attribute value字段的长度是可变的
(3)bearer protocol (e.g. L2CAP)会保留数据包的边界
注意:有些response包含多个attribute value,但是这些attribute value的长度都是attribute type定义好的固定长度。
5、Attribute Permissions
Attribute permissions是access permissions、encryption permissions、authentication permissions和authorization permissions的组合
(1)access permissions用来表示attribute是否允许client进行读写,取值包括:Readable、Writeable、Readable and writable;
(2)encryption permissions取值包括:Encryption required、No encryption required
(3)authentication permissions用来表示当client访问attribute value时是否需要一个已认证的物理链路,同样也表示当server向client发送notification和indication时是否需要一个已认证的物理链路,取值包括:Authentication Required、No Authentication Required
(4)authorization permissions用来表示当client访问attribute value时是否需要授权,取值包括:Authorization Required、No Authorization Required
注意:
(5)如果server设置authentication permissions为Authentication Required,但是client的链路没有通过认证,当client访问attribute value时,会收到错误码:«Insufficient Authentication»,此时client需要进行链路认证,认证成功后才能访问attribute value
(6)如果server设置encryption permissions为Encryption required,但是client的链路没有加密,当client访问attribute value时,会收到错误码:«Insufficient Encryption»,此时client需要进行链路加密,加密成功后才能访问attribute value
(7)如果server设置encryption permissions为Encryption required,client的链路已经加密,但是encryption key size小于要求的长度,当client访问attribute value时,会收到错误码:«Insufficient Encryption Key Size»,此时client需要用符合长度要求的link key进行加密,加密成功后才能访问attribute value
6、Control-Point Attributes
control-point attributes指的是不能read,但是能written、notified或者indicated的attributes,用处:例如client向server写入一个command,或者server程序执行完时给client一个通知
7、Protocol Methods
Method分为:request、response、command、notification、indication或者confirmation;
8、Exchanging MTU Size
ATT_MTU表示的是client和server之间每个数据包的最大长度,ATT的上层协议会定义一个default ATT_MTU value。
Client和server可以使用Exchange MTU Request和Response PDU来交换自己的MTU size,然后client和server会使用其中最小的值作为交互用的MTU size
A device that is acting as a server and client at the same time shall use the same value for Client Rx MTU and Server Rx MTU.
9、Long Attribute Values
一个数据包最大可以发送的attribute长度是ATT_MTU-1个字节,至少Attribute Opcode要占一个字节,如果attribute value的长度大于ATT_MTU-1个字节,则称为Long Attribute
read长度大于ATT_MTU-1个字节Attribute,需要使用read blob request,使用read request可能读取到前面的ATT_MTU-1个字节
write长度大于ATT_MTU-3个字节Attribute,使用prepare write request和execute write request,使用write request可能写入前面的ATT_MTU-3个字节
在ATT协议中无法确定一个attribute的长度是否大约ATT_MTU-3,在上层协议有规定
attribute value最大长度是512个字节