Bluetooth ATT 介紹

Bluetooth ATT介紹

轉發:http://www.cnblogs.com/hzl6255/p/4141505.html 

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

1 介紹

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

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

ATT中的屬性包含下面三個內容  
- Attribute Type       : 由UUID(Universally Unique IDentifier)來定義  
- Attribute Handle     : 用來訪問Attribute Value  
- A set of Permissions : 控制是否該Attribute可讀、可寫、屬性值是否經過加密鏈路發送
大數據

一個設備能夠同時擁有Server和Client  
而一個Server能夠支持多個Clientui

2 詳細內容

Client經過使用ATT PDUs(Protocol Data Unit)來發現Attribute Handlesthis

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 
url

更簡單的辦法以下(xxxx表明十六進制的16-bit UUID)  
0000xxxx-0000-1000-8000-00805F9B34FBspa

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

Request Opcode In Error   : 發生錯誤的Attribute Opcode  
Attribute Handle In Error : Attribute Opcode對應的Attribute Handle, 若無或Opcode不支持, 則使用0x0000  
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

 
分類:  Bluetooth
相關文章
相關標籤/搜索