Bluetooth GATT介紹

1. 介紹

GATT(Generic Attribute Profile),描述了一種使用ATT的服務框架
該框架定義了服務(Server)和服務屬性(characteristic)的過程(Procedure)及格式
Procedure定義了characteristic的發現、讀、寫、通知(Notifing)、指示(Indicating)
及配置characteristic的廣播

GATT可以被Application或其他Profile使用
其協議棧如下圖
---------------        ---------------
| Application | <----> | Application |
---------------        ---------------
|  Attribute  |        | Attribute   |
|  Protocol   | <----> | Protocol    |
---------------        ---------------
|    L2CAP    | <----> |    L2CAP    |
---------------        ---------------
|  Controller | <----> |  Controller |
---------------        ---------------

GATT可以配置爲如下兩種角色(Role)
- Client : 命令、請求發起方
- Server : 命令、請求接收方

角色配置實例如下

     _________ 
    / ======= \ 
   / __________\ 
  | ___________ |     Request      /\ 
  | |Computer | |   ---------->   / / 
  | |(Client) | |   <----------  / / Sensor(Server) 
  | |_________| |     Response  / / 
  \=____________/               \/ 
  / """"""""""" \  
 / ::::::::::::: \   
(_________________) 

Computer是一個溫度服務客戶端, Sensor是溫度服務服務器
Computer向Sensor發起Procedure來讀Sensor的值

GATT對下層的需求如下
- Physical Link : 使用GAP Channel Establishment建立的ATT Bearer
- GATT Role     : 不依賴於Coontroller角色(Master/Slave)
- Security      : 對於LE,Security Features(Authorization、Authentication、Encryption)是可選的
                  對於BR/EDR, Encryption是強制的
- TX order      : GATT中的多字節字段,採用Least Significant octet先發送(Little Endian)

2 內容

2.1 Configured Broadcast

對於LE物理鏈路,在Server廣播模式過程中
Client通過Configured Broadcast告知Server應該在advertising data加入Characteristic Value

方法是Client設置指定bit位
廣播頻率則是Service、Characteristic行爲定義的一部分

2.2 GATT Profile Hierarchy

GATT指定了數據交互的結構(Structure)
這個結構體定義了一些基本元素,如Service、Characteristic
這些元素存在於Attribute中


GATT中最上層是Profile,Profile由一個或多個服務(Service)組成
服務是由Characteristics組成,或是其他服務的引用(Reference)
Characteristic包含一個值(Value),可能包含該Value的相關信息

2.2.1 Service

Service是[數據]和與之關聯的[完成某個特定功能的行爲]/[特性]的集合
在GATT中,一個服務由服務定義(Service Defintion)來實現
一個服務定義可能包含引用服務(Reference Service)、強制Characteristic和可選Characteristic

爲了實現舊版本的兼容,新版本中服務定義只能增加新的引用服務或可選Characteristic
新版本中的服務定義禁止從舊的服務定義中改變行爲

Server有兩類
- Primary Service   : 擁有基本功能的服務,可被其他服務包含,可以通過Primary Service Discovery過程來發現
- Secondary Service : 僅用來被Primary/Other Secondary Service、高層協議引用的服務

判讀一個服務是Primary or Secondary Service可通過高層協議強制規定

2.2.2 Included Service

一個Included Service是一種引用已存在服務的方法,
具體辦法爲在服務定義的開始加上Included Service的引用,
這樣整個Included Service定義成爲新服務定義的一部分

2.2.3 Characteristic

Characteristic由Characteristic Definition定義,
包含一個Characteristic聲明、Characteristic屬性、值、值的描述(Optional)

3 Service Interoperability Requirements

3.1 Service Definition

服務定義(Service Definition)包含一個服務申明(Service Declaration)
    可能包含Include Definitions和Characteristic Definitions
在下一個服務申明前或到達Maximum Attribute Handle時結束
服務定義在服務端上基於Attribute Handle順序呈現

服務定義中的Include Definitions和Characteristic Definitions被認爲是服務的一部分
服務定義中的順序爲
Service Declaration ~ Include Definitions(>=0) ~ Characteristic Definitions(>=0)

Service Declaration如下

Attribute Handle Attribute Type Attribute Value Attribute Permission
0xNNNN 0x2800 – UUID for <Primary Service>
0x2801 – UUID for <Secondary Service>
16-bit Bluetooth UUID
128-bit UUID for Service
Read Only,
No Authentication,
No Authorization

規則如下:
- 當多個服務存在時
      使用16-bit Bluetooth UUID服務定義的服務應該分組(如按順序排列)
      同理,使用128-bit UUID的服務定義的服務也分組
- 一個設備或高層協議可能有多個服務定義,同時多個服務定義含有相同的Service UUID
- 服務端的所有Attributes應該包含一個服務聲明或存在一個服務定義
- 服務端的服務定義可能無序;Client不應該認爲服務端的服務定義一定是有序的

3.2 Include Definition

一個Include Definition只包含一個Include Declaration

Include Declaration如下

Attribute Handle Attribute Type Attribute Value Attribute Permission
0xNNNN 0x2802 – UUID
for<Include>
Included Service
Attribute Handle
End Group
Handle
Service UUID Read Only,
No Authentication,
No Authorization

其中,僅當UUID是16-bit Bluetooth UUID時才存在

如果一個Service的Include Definition(A)是引用其他Server的Include Definition(B)
那麼Include Definition(B)不應該引用Include Definition(A),否則就是循環引用(Circular Reference)

當一個Client檢測到循環引用或detects nested include declarations to a greater level than it expects
Client應當終止本次通信(ATT Bearer)

3.3 Characteristic Definition

Characteristic Definition包含如下內容
- Characteristic Declaration                       : First
- Characteristic Value declaration                 : Second
- Characteristic Descriptor Declarations(Optional) : Last(含多個時順序不關緊要)

Characteristic Definitions在服務端以Attribute Handle排序

以上每個Declaration包含在一個單獨的Attribute中

3.3.1 Characteristic Declaration

Characteristic Declaration如下

Attribute Handle Attribute Type Attribute Value Attribute Permission
0xNNNN 0x2803
– UUID for
Characteristic
Characteristic
Properties
Characteristic
Value
Attribute Handle
Characteristic
UUID
Read Only,
No Authentication,
No Authorization

其中,Attribute Value只讀
Attribute Value字段如下

Attribute Value Size Description
Characteristic Properties 1 octets Bit field of characteristic properties
Characteristic
Value Handle
2 octets Handle of the Attribute containing the value of this characteristic
Characteristic UUID 2/16 octets 16-bit Bluetooth UUID or 128-bit UUID for Characteristic Value

一個Service可能含有相同Characteristic UUID的Characteristic Definitions

在一個服務定義中,一些強制的Characteristics應該位於Include Declarations之後、其他可選characteristics之前
而Client不應該認爲Characteristic是有序的
同時,16-bit Bluetooth UUIDs和128-bit Bluetooth UUIDs應該分別分組

Characteristic Properties以bit位的方式決定如下內容
- Characteristic Value的使用方式
- Characteristic Descriptors的訪問方式

Characteristic Properties bit field的詳細內容如下
Notice: 多個比特位可以同時設置

Properties Value Description
Broadcast 0x01 If set, permits broadcasts of the Characteristic Value
using Characteristic Configuration Descriptor
Read 0x02 If set, permits reads of the Characteristic
Value using procedures defined in Section 4.8
Write Without
Response
0x04 If set, permit writes of the Characteristic Value
without response using procedures
defined in Section 4.9.1
Write 0x08 If set, permits writes of the Characteristic Value
with response using procedures
defined in Section 4.9.3 or Section 4.9.4
Notify 0x10 If set, permits notifications of a Characteristic Value
without acknowledgement using the procedure
defined in Section 4.10
Indicate 0x20 If set, permits indications of a Characteristic Value
with acknowledgement using the procedure
defined in Section 4.11
Authenticated
Signed Writes
0x40 If set, permits signed writes to the Characteristic Value
using the procedure defined in Section 4.9.2
Extended
Properties
0x80 If set, additional characteristic properties are
defined in the Characteristic Extended Properties Descriptor
defined in Section 3.3.3.1

3.3.2 Characteristic Value Declaration

Characteristic Value Declaration如下

Attribute Handle Attribute Type Attribute Value Attribute Permissions
0xNNNN 0xuuuu – 16-bit Bluetooth UUID
or
128-bit UUID for Characteristic UUID
Characteristic Value Higher layer profile
or
implementation specific

3.3.3 Characteristic Descriptor Declarations

Characteristic Descriptor Declarations包含了Characteristic Value相關信息

GATT定義了一系列的標準Characteristic Descriptors供高層協議使用
高層協議也可以定義協議相關的Characteristic Descriptors

Characteristic Descriptors在服務端上是無序的,Client不應該理所當然
Characteristic Descriptors Declarations Permissions由高層協議定義或協議相關的
Client不應該理所當然地認爲是可讀的

Characteristic Descriptor Declarations包括如下內容(詳細見規範)

- Characteristic Extended Properties
- Characteristic User Description
- Client Characteristic Configuration
- Server Characteristic Configuration
- Characteristic Presentation Format
- Characteristic Aggregate Format

3.4 Summary of GATT Profile Attribute Types

Attribute Type UUID Description
<Primary Service> 0x2800 Primary Service Declaration
<Secondary Service> 0x2801 Secondary Service Declaration
<Include> 0x2802 Include Declaration
<Characteristic> 0x2803 Characteristic Declaration
<Characteristic Extended Properties> 0x2900 Characteristic Extended Properties
<Characteristic User Description> 0x2901 Characteristic User Description Descriptor
<Client Characteristic Configuration> 0x2902 Client Characteristic Configuration Descriptor
<Server Characteristic Configuration> 0x2903 Server Characteristic Configuration Descriptor
<Characteristic Format> 0x2904 Characteristic Format Descriptor
<Characteristic Aggregate Format> 0x2905 Characteristic Aggregate Format Descriptor

4. GATT Feature Requirements

4.1 Overview

GATT中定義了11項Feature
1.  Server Configuration
2.  Primary Service Discovery
3.  Relationship Discovery
4.  Characteristic Discovery
5.  Characteristic Descriptor Discovery
6.  Reading a Characteristic Value
7.  Writing a Characteristic Value
8.  Notification of a Characteristic Value
9.  Indication of a Characteristic Value
10. Reading a Characteristic Descriptor
11. Writing a Characteristic Descriptor

每個Feature都有對應的過程和子過程,這些過程描述瞭如何使用ATT來實現各自的功能

4.2 Feature Support and Procedure Mapping

詳細的對應列表如下表

No

Feature

Sub-Procedure

Ref

Client
Support

Server
Support

1

Server Configuration

Exchange MTU

4.3.1

O

O

2

Primary Service

Discovery

Discover All Primary Services

4.4.1

O

M

Discover Primary Services By
Service UUID

4.4.2

O

M

3

Relationship Discovery

Find Included Services

4.5.1

O

M

4

Characteristic
Discovery

Discover All Characteristic of a Service

4.6.1

O

M

Discover Characteristic by UUID

4.6.2

O

M

5

Characteristic
Descriptor Discovery

Discover All Characteristic Descriptors

4.7.1

O

M

6

Characteristic

Value Read

Read Characteristic Value

4.8.1

O

M

Read Using Characteristic UUID

4.8.1

O

M

Read Long Characteristic Values

4.8.2

O

O

Read Multiple Characteristic Values

4.8.3

O

O

7

Characteristic

Value Write

Write Without Response

4.9.1

O

C.1

Signed Write Without Response

4.9.2

O

O

Write Characteristic Value

4.9.3

O

C.2

Write Long Characteristic Values

4.9.4

O

O

Characteristic Value Reliable Writes

4.9.5

O

O

8

Characteristic

Value Notification

Notifications

4.10.1

O

O

9

Characteristic

Value Indication

Indications

4.11.1

M

C.3

10

Characteristic
Descriptor Value Read

Read Characteristic Descriptors

4.12.1

O

O

Read Long Characteristic Descriptors

4.12.2

O

O

11

Characteristic
Descriptor Value Write

Write Characteristic Descriptors

4.12.3

O

O

Write Long Characteristic Descriptors

4.12.4

O

O

C1: Write Without Response is mandatory if Signed Write Without Response is supported

otherwise optional

C2: Write Characteristic Value is mandatory if Write Long Characteristic Values is supported

otherwise optional

C3: If Service Change Characteristic is present, this feature is mandatory, otherwise optional.

4.3 Server Configuration

該過程可被Client用來配置Attribute Protocol的MTU大小

4.3.1 Exchange MTU

Client使用該子過程來設置適配雙方均支持的最大ATT_MTU

在BR/EDR物理鏈路中不應該使用該過程,而應該使用L2CAP Channel Configuration Procedures

該過程對應於ATT的MTU Exchange Request/Response
見<Bluetooth ATT介紹 - 4.2 MTU Exchange>

4.4 Primary Service Discovery

Client使用該過程來發現服務端的Primary Services
一旦發現服務存在,可通過其他過程來訪問Primary Services的附加信息(關聯主服務和次服務)
可使用的其他過程包括Characteristic Discovery和Relationship Discovery

該過程包括兩個子過程:
- Discover All Primary Services
- Discover Primary Services by Service UUID

在BR/EDR物理鏈路上則使用SDP service discovery來發現服務

4.4.1 Discover All Primary Services

Client使用該子過程來發現服務端的所有Primary Services

該子過程使用ATT的Read By Group Type Request,同時設置如下參數
- Starting Handle : 0x0001
- Ending Handle   : 0xFFFF
- Attribute Type  : UUID for <Primary Service>

可能的迴應有
- Read By Group Type Response
- Error Response

Read By Group Type Response返回三元組列表
三元組包括
- Attribute Handle : 服務聲明的Handle
- End Group Handle : 服務定義中最後一個Attribute的Handle
- Attribute Value  : 服務端支持的服務的Service UUID

當收到Error Response<Error Code: Attribute Not Found>時
則表明該過程已經完成

當Client找到自己所需要的服務時,可以終止該過程

Note: 3.1中已指出Service Declaration是可讀,並且不需要認證或授權
         因此權限相關的錯誤不會發生

下圖是一個實例圖


4.4.2 Discover Primary Service by Service UUID

當Client只知道Service UUID時,可以使用該子過程來發現對應的主服務

該子過程使用ATT的Find By Type Value Request,同時設置參數如下
- Starting Handle : 0x0001
- Ending Handle   : 0xFFFF
- Attribute Value : 16-bit Bluetooth UUID or 128-bit UUID
- Attribute Type  : UUID for <Primary Service>

可能的迴應有
- Find By Type Value Response
- Error Response

Find By Type Value Response返回Attribute Handle ranges列表
Attribute Handle range即服務定義的Starting HandleEnding Handle
如果Attribute Handle range中的End Found Handle不是0xFFFF
那麼Client將會再請求一次Req
同時將Starting Handle設置爲收到的最後一個Attribute Handle+1

終止規則和權限問題同Discover All Primary Services

下圖是一個實例圖


4.5 Relationship Discovery

Client使用該過程來發現和其他服務的服務關係

4.5.1 Find Include Services

Client使用該子過程來發現一個服務定義包含的服務申明

該子過程使用ATT的Read By Type Request,同時設置參數如下
- Starting Handle : 所要查找服務的Starting Handle
- Ending Handle   : 所要查找服務的Ending Handle
- Attribute Type  : UUID for <Include>

可能的迴應有
- Find By Type Response
- Error Response

Find By Type Response返回[Attribute Handle, Attribute Value]集合對
Attribute Value由所包含服務申明的Attribute HandleEnd Group Handle組成
當UUID爲16-bit Bluetooth UUID時,那麼它也將包含在Rsp中

該Req應該被再次請求,同時設置Starting Handle爲爲收到的最後一個Attribute Handle+1

當Rsp中包含的服務申明中Attribute Handle等於Req的Ending Handle時,
該子過程被認爲完成(當然Attribute Not Found-Error Rsp也是)

當Include Service使用128-bit UUID時
使用Read Request來獲取Include Service UUID
其中Attribute Handle參數設置爲Include Service的Attribute Handle

權限規則同上面

下圖是一個實例圖


========================================================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章