Bluetooth ATT介紹


1 介紹

ATT,Attribute Protocol,用於發現、讀、寫對端設備的協議(針對BLE設備)

ATT允許設備作為服務端提供擁有關聯值的屬性集
讓作為客戶端的設備來發現、讀、寫這些屬性;同時服務端能主動通知客戶端

ATT定義了兩種角色: 服務端(Server)和客戶端(Client)

ATT中的屬性包含下面三個內容

 - Attribute Type       : 由UUID(Universally Unique IDentifier)來定義 
 - Attribute Handle     : 用來訪問Attribute Value 
 - A set of Permissions : 控制是否該Attribute可讀、可寫、屬性值是否通過加密鏈路發送

一個設備可以同時擁有Server和Client;而一個Server可以支持多個Client

2 詳細內容

Client通過使用ATT PDUs(Protocol Data Unit)來發現Attribute Handles

2.1 Attribute Type

Attribute Type由UUID唯一指定,UUID是一個128-bit值

在使用過程中,為了提高效率,使用的是16-bits Attribute UUID
128-bit UUID        = 16-bit Attribute UUID*2^96 + Bluetooth_Base_UUID
Bluetooth_Base_UUID = 00000000-0000-1000-8000-00805F9B34FB

更簡單的辦法如下(xxxx代表十六進制的16-bit UUID)
0000xxxx-0000-1000-8000-00805F9B34FB

TIP: 16-bit Attribute UUIDs與SDP 16-bit UUIDs使用相同的命名空間

2.2 Attribute Handle

Attribute Handle是由Server分配的一個唯一且非零16-bit值

0x0000 : 保留 
0xFFFF : 最大Attribute Handle

2.3 Attribute Handle Grouping

Grouping是一由高層協議定義的一組屬性,他們位於其他屬性組之前
客戶可以請求第一個和最后一個與屬性組關聯的Handle

2.4 Attribute Value

Attribute Value是一個八位組(長度為8或可變)
當屬性值太長時,可通過多個PDUs發送

When transmitting attribute values in a request, a response, a notification or an indication, 
the attribute value length is not sent in any field of the PDU. 

The length of a variable length field in the PDU is implicitly given by the length of the packet that carries this PDU. 
This implies that: 
a) only one attribute value can be placed in a single request, response, notification or indication unless the attribute values have lengths known by both the server and client, as defined by the attribute type. 
b) This attribute value will always be the only variable length field of a request, response, notification or indication. 
c) The bearer protocol (e.g. L2CAP) preserves datagram boundaries.

2.5 Attribute Permissions

屬性都有一組與之相關聯的Permission Values

一個給定屬性的權限由高層協議定義,並對ATT不可見

當訪問一個安全屬性需要一個認證的鏈路,而Client沒有足夠的權限
Server則響應一個Error Code(Insufficient Authentication)
Client收到Error Code后應該嘗試認證該鏈路,成功后即可訪問該安全屬性

當訪問一個安全屬性需要一個加密的鏈路,而鏈路沒有加密
Server則響應一個Error Response(Insufficient Encryption)
Client收到Error Response后應該嘗試加密該鏈路,成功后即可訪問該安全屬性

當訪問一個安全屬性需要一個加密的鏈路
鏈路有加密,但是對於所需要的安全級別來說加密Key Size太短
Server則響應一個Error Code(Insufficient Encryption Key Size)
Client收到Error Response后應該嘗試使用更長的Key Size加密該鏈路,成功后即可訪問該安全屬性

Attribute Permissions由下列三個權限組合而成

- Access Permissions         : 決定Client是否可讀、寫屬性值 
    ~ Readable 
    ~ Writable 
    ~ Readable and Writable 
- Authentication Permissions : 決定是否需要一個認證的物理鏈路(Authenticated Physical Link) 
    ~ Authentication Required 
    ~ No Authentication Required 
- Authorization Permssions   : 決定Client在訪問屬性值前是否需要授權 
    ~ Authorization Required 
    ~ No Authorization Required

2.6 Control-Point Attribute

不可讀、可寫、可通知(Notified)和可指示(Indicated)的屬性被稱為Control-Point Attribute

高層協議可使用該屬性來使能設備特定過程,比如設備上一個給定過程的命令或指示已經完成

2.7 Protocol Methods

ATT使用Protocol Methods來發現、讀、寫、通知、指示屬性,方法可分為如下幾種

 - Request 
 - Response 
 - Command 
 - Notification 
 - Indication 
 - Confirmation

一些ATT PDUs包含一個認證簽名(Authentication Signature);允許PDU發送端不需要加密

這些方法和符號位被稱為opcode

2.8 Exchanging MTU Size

ATT_MTU定義了Client和Server之間數據包的最大值;其默認值由高層協議來定義

Client和Server可通過Exchange MTU Request and Response PDUs來交換最大數據包
然后均使用交換值中的最小值進行通信

同時作為Server和Client的設備應該使用相同的Client Rx MTU和Server Rx MTU

每個ATT Bearer均有其ATT_MTU;當一個設備擁有多個ATT Bearer時,不同ATT Bearer的ATT_MTU可能不同

2.9 Long Attribute Value

在單一數據包中可發送最長的屬性大小為[ATT_MTU –1] octets;在一個Attribute PDU中至少包含Attribute Opcode

屬性值比[ATT_MTU-1] octets大時屬性稱為Long Attribute,對於Long Attribute

 - 使用Read Blob Request來讀取整個屬性(Attribute > [ATT_MTU-1] octets) 
 - 使用Prepare Write Request和Execute Write Request來寫整個屬性(Attribute > [ATT_MTU-3] octets)

ATT無法決定屬性值是否大於[ATT_MTU] octets,但是高層協議Can Tell,最大的屬性值為512 octets

2.10 Atomic Operation

Server應該將Client的每個請求或命令視為不受影像的原子操作
如果一個鏈路由於某種原因斷開,高層協議應當對屬性值得修改負責

Long Attribute不能被單一的原子操作讀、寫

3 Attribute PDU

Attribute PDUs有六種類型

 - Requests      : Client->Server, 請求回應 
 - Responses     : Server->Client, 響應請求. 
 - Commands      : Client->Server, 命令 
 - Notifications : Server->Client, 服務端通知 
 - Indications   : Server->Client, 請求確認 
 - Confirmations : Client->Server, Ind確認

Server至少能處理以下請求

 - Find Information Request 
 - Read Reques

Attribute PDU格式如下

image

Authentication Signature Flag取值如下

 - 1: PDU包含Authentication Signature(12 octets), X為13 
 - 0: PDU不包含Authentication Signature, X為1

當Attribute PDU包含Authentication Signature時,則該PDU不應該通過加密鏈路傳輸

Command Flag取值如下

 - 1: PDU為一個Command

注意: 只有Write Command可能包含一個Authentication Signature

ATT是一種Sequential Protocol,這意味着在執行下一個動作前應該得到相應的回應
ATT將Request-Response和Indication-Confirmation Pair看出一個單一的事務(Transaction)

4 Attribute Protocol PDU

4.1 Error Handing

Error Response用來聲明一個給定的請求無法完成,並給出原因

ErrorResponse

Error Code取值如下, 若Client無法理解(如ErrorCode為更新版本中定義), 則認為是未知原因

ErrorCode

4.2 MTU Exchange

4.2.1 Exchange MTU Request

Client使用MTU Exchange Request來告知所支持的最大接收MTU size
同時請求Server回應Server所支持的最大接收MTU size

image

Client Rx MTU>= default ATT_MTU;
該請求在一個連接中僅發送一次,Client Rx MTU應當設置為Client所能接收ATT PDU的最大值

4.2.2 Exchange MTU Response

Server使用Exchange MTU Response來回應來自Client的Exchange MTU Request

image

Server Rx MTU>= default ATT_MTU;Server Rx MTU應當設置為Server所能接收ATT PDU的最大值
當完成Req-Rsp后,Server和Client將ATT_MTU均設置為Client Rx MTU和Server Rx MTU中的較小值
ATT_MTU生效時機

 - Server : Response發送后、其他ATT PDU發送前 
 - Client : Response接收后、其他ATT PDU發送前

當其中任一值小於默認ATT_MTU大小時認為該值不正確,此時應當將ATT_MTU設置為默認值
當一個設備同時作為Client和Server,不同角色時ATT_MTU值應當相同(詳細規則可參看規范)

4.3 Find Information

4.3.1 Find Information Request

Find Information Request被用來獲取與Attribute Handles相關聯的類型
Client使用該請求來發現Server上的Attribute-Type列表

image

讀取所有Attributes

 - Starting Handle : 0x0001 
 - Ending Handle   : 0xFFFF

規則

 - Starting Handle <= Ending Handle : Response PDU或Error Response<Attribute Not Found> 
 - Starting Handle  > Ending Handle : Server回應Error Response<Invalid Handle> 
 - Starting Handle  = 0x0000        : Server回應Error Response<Invalid Handle>

Server不應該回應以下Error Code的Error Response

 - <Insufficient Authentication> 
 - <Insufficient Authorization> 
 - <Insufficient Encryption Key Size> 
 - <Application Error>

4.3.2 Find Information Response

image

Response PDU中應當包含完整的Handle-UUID對;這意味着Handle-UUID對應該在單一的回應報文中;同時Response PDU按照Handle的升序回應

Format參數有兩個可能值

image

image

當一個Response PDU無法裝下所有的Handle-UUID對時;Client以新的Starting Handle發起另一個Find Information Request來獲取未回應的信息

4.3.3 Find By Type Value Request

該Req用來獲取指定16-bit UUID Attribute Type和Attribute Value的Attribute Handles

image

讀取所有Attributes

- Starting Handle : 0x0001 
- Ending Handle   : 0xFFFF

其他規則與Find Information Request相似

4.3.4 Find by Type Value Response

image

Handles Information List包含一個或多個Handle Information列表

image

規則與Find Information Response相似

4.4 Reading Attributes

4.4.1 Read By Type Request

Read By Type Request用來獲取Attribute Values(Client已知Attribute Type但不知Attribute Handle)

image

查找所有Attributes

 - Starting Handle : 0x0001 
 - Ending Handle   : 0xFFFF

TIP : 所有的Attribute Type通過128-bit UUID進行比較,即使提供的是16-bit UUID

規則

 - Starting Handle <= Ending Handle : Response PDU或Error Response<Attribute Not Found> 
 - Starting Handle  > Ending Handle : Server回應Error Response<Invalid Handle> 
 - Starting Handle  = 0x0000        : Server回應Error Response<Invalid Handle>

該報文中Attribute Type的長度應當相同,否則需要重新發起另一次請求

當Client因為安全原因無法訪問時,Server應該回應Error Response

 - <Insufficient Authentication> 
 - <Insufficient Authorization> 
 - <Insufficient Encryption Key Size> 
 - <Insufficient Encryption>

一些其他權限原因導致無法讀時,則回應

 - <Read Not Permitted>

4.4.2 Read By Type Response

image

Attribute Data Field字段是Attribute Handle-Value列表

image

Read By Type Response包含完整的Handle-Value對;這意味着Handle-Value對應該在單一的回應報文中;同時Response PDU按照Handle的升序回應

Handle-Value對的最大長度為255 octets,即Length參數;故最大的Attribute Value為(Length-2)=253 octets

在回應的Handle-Value列表中

 - Attribute Value  > (ATT_MTU-4)/253 octets部分 : Client重新請求Read Blob Request來獲取 
 - Attribute Value <= (ATT_MTU-4)/253 octets部分 : Rsp PDU中回應

4.4.3 Read Request

Read Request用來請求Attribute Value

image

Attribute Handle應該為一個有效的Handle;否則回應Error Response<Invalid Handle>;因安全問題無法訪問的回應同Read By Type Request

4.4.4 Read Response

image

當Attribute Value長度超過(ATT_MTU-1)時,前(ATT_MTU-1) octets應該被回應;Client發起Read Blob Request來獲取剩下數據

4.4.5 Read Blob Request

image

用來獲取Handle指定Offset的Attribute Value

NOTICE: 內容太多,筆者無法一一細述,僅給出概要,詳細信息請參考規范,下同

4.4.6 Read Blob Response

R

當Value Offset等於Attribute Value的長度時,回應0;當Attribute Value大於(Value Offset + ATT_MTU-1)時
回應Attribute Value從Value Offset開始的(ATT_MTU-1)個octets

4.4.7 Read Multiple Request

用來請求兩個或更多的屬性集的值

image

4.4.8 Read Multiple Response

image

4.4.9  Read by Group Type Request

用來請求已知Attribute Type的屬性值

123

4.4.10  Read by Group Type Response

image

Attribute Data格式如下

image 

4.5 Writing Attributes

4.5.1 Write Request

用來寫屬性值

image

4.5.2 Write Response

用來通知Client屬性值已成功寫入

image

4.5.3 Write Command

用來寫屬性值,通常為Control-Point Attribute

image

4.5.4  Signed Write Command

用來寫屬性值,包含一個Authentication Signature,通常為Control-Point Attribute

image

4.6 Queued Writes

將多個屬性值寫操作以FIFO方式入隊,然后再一個原子操作中進行

4.6.1  Prepare Write Request

image

4.6.2 Prepare Write Response

image

4.6.3  Execute Write Request

image

4.6.4 Execute Write Response

image 

4.7 Server Initiated

4.7.1  Handle Value Notification

Server可以在任何時候向Client發送Attribute Value的通知

image

4.7.2  Handle Value Indication

Server可以發送Attribute Value的指示(Indication)

image

4.7.3  Handle Value Confirmation

用來回應Handle Value Indication,確認Ind已接收

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM