Bluetooth ATT介绍
閱讀目錄
- 1 介紹
- 2 詳細(xì)內(nèi)容
- 3 Attribute PDU
- 4 Attribute Protocol PDU
回到頂部
1 介紹
ATT,Attribute Protocol,用于發(fā)現(xiàn)、讀、寫對端設(shè)備的協(xié)議(針對BLE設(shè)備)
ATT允許設(shè)備作為服務(wù)端提供擁有關(guān)聯(lián)值的屬性集
讓作為客戶端的設(shè)備來發(fā)現(xiàn)、讀、寫這些屬性;同時服務(wù)端能主動通知客戶端
ATT定義了兩種角色: 服務(wù)端(Server)和客戶端(Client)
ATT中的屬性包含下面三個內(nèi)容
- Attribute Type : 由UUID(Universally Unique IDentifier)來定義 - Attribute Handle : 用來訪問Attribute Value - A set of Permissions : 控制是否該Attribute可讀、可寫、屬性值是否通過加密鏈路發(fā)送一個設(shè)備可以同時擁有Server和Client;而一個Server可以支持多個Client
回到頂部
2 詳細(xì)內(nèi)容
Client通過使用ATT PDUs(Protocol Data Unit)來發(fā)現(xiàn)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代表十六進(jìn)制的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 Handle2.3 Attribute Handle Grouping
Grouping是一由高層協(xié)議定義的一組屬性,他們位于其他屬性組之前
客戶可以請求第一個和最后一個與屬性組關(guān)聯(lián)的Handle
2.4 Attribute Value
Attribute Value是一個八位組(長度為8或可變)
當(dāng)屬性值太長時,可通過多個PDUs發(fā)送
2.5 Attribute Permissions
屬性都有一組與之相關(guān)聯(lián)的Permission Values
一個給定屬性的權(quán)限由高層協(xié)議定義,并對ATT不可見
當(dāng)訪問一個安全屬性需要一個認(rèn)證的鏈路,而Client沒有足夠的權(quán)限
Server則響應(yīng)一個Error Code(Insufficient Authentication)
Client收到Error Code后應(yīng)該嘗試認(rèn)證該鏈路,成功后即可訪問該安全屬性
當(dāng)訪問一個安全屬性需要一個加密的鏈路,而鏈路沒有加密
Server則響應(yīng)一個Error Response(Insufficient Encryption)
Client收到Error Response后應(yīng)該嘗試加密該鏈路,成功后即可訪問該安全屬性
當(dāng)訪問一個安全屬性需要一個加密的鏈路
鏈路有加密,但是對于所需要的安全級別來說加密Key Size太短
Server則響應(yīng)一個Error Code(Insufficient Encryption Key Size)
Client收到Error Response后應(yīng)該嘗試使用更長的Key Size加密該鏈路,成功后即可訪問該安全屬性
Attribute Permissions由下列三個權(quán)限組合而成
- Access Permissions : 決定Client是否可讀、寫屬性值 ~ Readable ~ Writable ~ Readable and Writable - Authentication Permissions : 決定是否需要一個認(rèn)證的物理鏈路(Authenticated Physical Link) ~ Authentication Required ~ No Authentication Required - Authorization Permssions : 決定Client在訪問屬性值前是否需要授權(quán) ~ Authorization Required ~ No Authorization Required2.6 Control-Point Attribute
不可讀、可寫、可通知(Notified)和可指示(Indicated)的屬性被稱為Control-Point Attribute
高層協(xié)議可使用該屬性來使能設(shè)備特定過程,比如設(shè)備上一個給定過程的命令或指示已經(jīng)完成
2.7 Protocol Methods
ATT使用Protocol Methods來發(fā)現(xiàn)、讀、寫、通知、指示屬性,方法可分為如下幾種
- Request - Response - Command - Notification - Indication - Confirmation一些ATT PDUs包含一個認(rèn)證簽名(Authentication Signature);允許PDU發(fā)送端不需要加密
這些方法和符號位被稱為opcode
2.8 Exchanging MTU Size
ATT_MTU定義了Client和Server之間數(shù)據(jù)包的最大值;其默認(rèn)值由高層協(xié)議來定義
Client和Server可通過Exchange MTU Request and Response PDUs來交換最大數(shù)據(jù)包
然后均使用交換值中的最小值進(jìn)行通信
同時作為Server和Client的設(shè)備應(yīng)該使用相同的Client Rx MTU和Server Rx MTU
每個ATT Bearer均有其ATT_MTU;當(dāng)一個設(shè)備擁有多個ATT Bearer時,不同ATT Bearer的ATT_MTU可能不同
2.9 Long Attribute Value
在單一數(shù)據(jù)包中可發(fā)送最長的屬性大小為[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,但是高層協(xié)議Can Tell,最大的屬性值為512 octets
2.10 Atomic Operation
Server應(yīng)該將Client的每個請求或命令視為不受影像的原子操作
如果一個鏈路由于某種原因斷開,高層協(xié)議應(yīng)當(dāng)對屬性值得修改負(fù)責(zé)
Long Attribute不能被單一的原子操作讀、寫
回到頂部
3 Attribute PDU
Attribute PDUs有六種類型
- Requests : Client->Server, 請求回應(yīng) - Responses : Server->Client, 響應(yīng)請求. - Commands : Client->Server, 命令 - Notifications : Server->Client, 服務(wù)端通知 - Indications : Server->Client, 請求確認(rèn) - Confirmations : Client->Server, Ind確認(rèn)Server至少能處理以下請求
- Find Information Request - Read RequesAttribute PDU格式如下
Authentication Signature Flag取值如下
- 1: PDU包含Authentication Signature(12 octets), X為13 - 0: PDU不包含Authentication Signature, X為1當(dāng)Attribute PDU包含Authentication Signature時,則該PDU不應(yīng)該通過加密鏈路傳輸
Command Flag取值如下
- 1: PDU為一個Command注意: 只有Write Command可能包含一個Authentication Signature
ATT是一種Sequential Protocol,這意味著在執(zhí)行下一個動作前應(yīng)該得到相應(yīng)的回應(yīng)
ATT將Request-Response和Indication-Confirmation Pair看出一個單一的事務(wù)(Transaction)
回到頂部
4 Attribute Protocol PDU
4.1 Error Handing
Error Response用來聲明一個給定的請求無法完成,并給出原因
Error Code取值如下, 若Client無法理解(如ErrorCode為更新版本中定義), 則認(rèn)為是未知原因
?
4.2 MTU Exchange
4.2.1 Exchange MTU Request
Client使用MTU Exchange Request來告知所支持的最大接收MTU size
同時請求Server回應(yīng)Server所支持的最大接收MTU size
Client Rx MTU>= default ATT_MTU;
該請求在一個連接中僅發(fā)送一次,Client Rx MTU應(yīng)當(dāng)設(shè)置為Client所能接收ATT PDU的最大值
4.2.2 Exchange MTU Response
Server使用Exchange MTU Response來回應(yīng)來自Client的Exchange MTU Request
Server Rx MTU>= default ATT_MTU;Server Rx MTU應(yīng)當(dāng)設(shè)置為Server所能接收ATT PDU的最大值
當(dāng)完成Req-Rsp后,Server和Client將ATT_MTU均設(shè)置為Client Rx MTU和Server Rx MTU中的較小值
ATT_MTU生效時機
當(dāng)其中任一值小于默認(rèn)ATT_MTU大小時認(rèn)為該值不正確,此時應(yīng)當(dāng)將ATT_MTU設(shè)置為默認(rèn)值
當(dāng)一個設(shè)備同時作為Client和Server,不同角色時ATT_MTU值應(yīng)當(dāng)相同(詳細(xì)規(guī)則可參看規(guī)范)
4.3 Find Information
4.3.1 Find Information Request
Find Information Request被用來獲取與Attribute Handles相關(guān)聯(lián)的類型
Client使用該請求來發(fā)現(xiàn)Server上的Attribute-Type列表
讀取所有Attributes
- Starting Handle : 0x0001 - Ending Handle : 0xFFFF規(guī)則
- Starting Handle <= Ending Handle : Response PDU或Error Response<Attribute Not Found> - Starting Handle > Ending Handle : Server回應(yīng)Error Response<Invalid Handle> - Starting Handle = 0x0000 : Server回應(yīng)Error Response<Invalid Handle>Server不應(yīng)該回應(yīng)以下Error Code的Error Response
- <Insufficient Authentication> - <Insufficient Authorization> - <Insufficient Encryption Key Size> - <Application Error>4.3.2 Find Information Response
Response PDU中應(yīng)當(dāng)包含完整的Handle-UUID對;這意味著Handle-UUID對應(yīng)該在單一的回應(yīng)報文中;同時Response PDU按照Handle的升序回應(yīng)
Format參數(shù)有兩個可能值
當(dāng)一個Response PDU無法裝下所有的Handle-UUID對時;Client以新的Starting Handle發(fā)起另一個Find Information Request來獲取未回應(yīng)的信息
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其他規(guī)則與Find Information Request相似
4.3.4 Find by Type Value Response
Handles Information List包含一個或多個Handle Information列表
規(guī)則與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 : 0xFFFFTIP : 所有的Attribute Type通過128-bit UUID進(jìn)行比較,即使提供的是16-bit UUID
規(guī)則
- Starting Handle <= Ending Handle : Response PDU或Error Response<Attribute Not Found> - Starting Handle > Ending Handle : Server回應(yīng)Error Response<Invalid Handle> - Starting Handle = 0x0000 : Server回應(yīng)Error Response<Invalid Handle>該報文中Attribute Type的長度應(yīng)當(dāng)相同,否則需要重新發(fā)起另一次請求
?
?
?
?
?
?
當(dāng)Client因為安全原因無法訪問時,Server應(yīng)該回應(yīng)Error Response
- <Insufficient Authentication> - <Insufficient Authorization> - <Insufficient Encryption Key Size> - <Insufficient Encryption>一些其他權(quán)限原因?qū)е聼o法讀時,則回應(yīng)
- <Read Not Permitted>4.4.2 Read By Type Response
Attribute Data Field字段是Attribute Handle-Value列表
Read By Type Response包含完整的Handle-Value對;這意味著Handle-Value對應(yīng)該在單一的回應(yīng)報文中;同時Response PDU按照Handle的升序回應(yīng)
Handle-Value對的最大長度為255 octets,即Length參數(shù);故最大的Attribute Value為(Length-2)=253 octets
在回應(yīng)的Handle-Value列表中
- Attribute Value > (ATT_MTU-4)/253 octets部分 : Client重新請求Read Blob Request來獲取 - Attribute Value <= (ATT_MTU-4)/253 octets部分 : Rsp PDU中回應(yīng)4.4.3 Read Request
Read Request用來請求Attribute Value
Attribute Handle應(yīng)該為一個有效的Handle;否則回應(yīng)Error Response<Invalid Handle>;因安全問題無法訪問的回應(yīng)同Read By Type Request
?
4.4.4 Read Response
當(dāng)Attribute Value長度超過(ATT_MTU-1)時,前(ATT_MTU-1) octets應(yīng)該被回應(yīng);Client發(fā)起Read Blob Request來獲取剩下數(shù)據(jù)
4.4.5 Read Blob Request
用來獲取Handle指定Offset的Attribute Value
NOTICE: 內(nèi)容太多,筆者無法一一細(xì)述,僅給出概要,詳細(xì)信息請參考規(guī)范,下同
4.4.6 Read Blob Response
當(dāng)Value Offset等于Attribute Value的長度時,回應(yīng)0;當(dāng)Attribute Value大于(Value Offset + ATT_MTU-1)時
回應(yīng)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方式入隊,然后再一個原子操作中進(jìn)行
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發(fā)送Attribute Value的通知
4.7.2? Handle Value Indication
Server可以發(fā)送Attribute Value的指示(Indication)
4.7.3? Handle Value Confirmation
用來回應(yīng)Handle Value Indication,確認(rèn)Ind已接收
總結(jié)
以上是生活随笔為你收集整理的Bluetooth ATT介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ATT Protocol 属性协议
- 下一篇: Bluetooth GAP介绍