Bluetooth ATT介紹

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

1 介紹

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格式如下


===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用來聲明一個給定的請求無法完成,並給出原因


Request Opcode In Error   : 發生錯誤的Attribute Opcode
Attribute Handle In Error : Attribute Opcode對應的Attribute Handle, 若無或Opcode不支持, 則使用0x0000
Error Code                : 爲下圖值之一, 若Client無法理解(如ErrorCode爲更新版本中定義), 則認爲是未知原因


4.2 MTU Exchange

4.2.1 Exchange MTU Request

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


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


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列表


讀取所有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


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

Format參數有兩個可能值



當一個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


讀取所有Attributes
- Starting Handle : 0x0001
- Ending Handle   : 0xFFFF

其他規則與Find Information Request相似

4.3.4 Find by Type Value Response


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


規則與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)


查找所有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


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


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


Attribute Handle應該爲一個有效的Handle
否則迴應Error Response<Invalid Handle>

因安全問題無法訪問的迴應同Read By Type Request

4.4.4 Read Response


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

4.4.5 Read Blob Request


用來獲取Handle指定Offset的Attribute Value

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

4.4.6 Read Blob Response


當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

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


4.4.8 Read Multiple Response


4.4.9  Read by Group Type Request

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


4.4.10  Read by Group Type Response


Attribute Data格式如下


 

4.5 Writing Attributes

4.5.1 Write Request

用來寫屬性值


4.5.2 Write Response

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


4.5.3 Write Command

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

4.5.4  Signed Write Command

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


4.6 Queued Writes

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

4.6.1  Prepare Write Request


4.6.2 Prepare Write Response


4.6.3  Execute Write Request


4.6.4 Execute Write Response


4.7 Server Initiated

4.7.1  Handle Value Notification

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


4.7.2  Handle Value Indication

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


4.7.3  Handle Value Confirmation

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


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