GATT(Generic Attribute Profile),描述了一種使用ATT的服務(wù)框架 該框架定義了服務(wù)(Server)和服務(wù)屬性(characteristic)的過程(Procedure)及格式 Procedure定義了characteristic的發(fā)現(xiàn)、讀、寫、通知(Notifing)、指示(Indicating) 及配置characteristic的廣播 GATT可以被Application或其他Profile使用 其協(xié)議棧如下圖 --------------- --------------- | Application | <----> | Application | --------------- --------------- | Attribute | | Attribute | | Protocol | <----> | Protocol | --------------- --------------- | L2CAP | <----> | L2CAP | --------------- --------------- | Controller | <----> | Controller | --------------- --------------- GATT可以配置為如下兩種角色(Role) - Client : 命令、請求發(fā)起方 - Server : 命令、請求接收方 角色配置實(shí)例如下 _________ / ======= \ / __________\ | ___________ | Request /\ | |Computer | | ----------> / / | |(Client) | | <---------- / / Sensor(Server) | |_________| | Response / / \=____________/ \/ / ''''''''''' \ / ::::::::::::: \ (_________________) Computer是一個(gè)溫度服務(wù)客戶端, Sensor是溫度服務(wù)服務(wù)器 Computer向Sensor發(fā)起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是強(qiáng)制的 - TX order : GATT中的多字節(jié)字段,采用Least Significant octet先發(fā)送(Little Endian) 2 內(nèi)容 2.1 Configured Broadcast對于LE物理鏈路,在Server廣播模式過程中 Client通過Configured Broadcast告知Server應(yīng)該在advertising data加入Characteristic Value 方法是Client設(shè)置指定bit位 廣播頻率則是Service、Characteristic行為定義的一部分 2.2 GATT Profile HierarchyGATT指定了數(shù)據(jù)交互的結(jié)構(gòu)(Structure) 這個(gè)結(jié)構(gòu)體定義了一些基本元素,如Service、Characteristic 這些元素存在于Attribute中 
GATT中最上層是Profile,Profile由一個(gè)或多個(gè)服務(wù)(Service)組成 服務(wù)是由Characteristics組成,或是其他服務(wù)的引用(Reference) Characteristic包含一個(gè)值(Value),可能包含該Value的相關(guān)信息 2.2.1 ServiceService是[數(shù)據(jù)]和與之關(guān)聯(lián)的[完成某個(gè)特定功能的行為]/[特性]的集合 在GATT中,一個(gè)服務(wù)由服務(wù)定義(Service Defintion)來實(shí)現(xiàn) 一個(gè)服務(wù)定義可能包含引用服務(wù)(Reference Service)、強(qiáng)制Characteristic和可選Characteristic 為了實(shí)現(xiàn)舊版本的兼容,新版本中服務(wù)定義只能增加新的引用服務(wù)或可選Characteristic 新版本中的服務(wù)定義禁止從舊的服務(wù)定義中改變行為 Server有兩類 - Primary Service : 擁有基本功能的服務(wù),可被其他服務(wù)包含,可以通過Primary Service Discovery過程來發(fā)現(xiàn) - Secondary Service : 僅用來被Primary/Other Secondary Service、高層協(xié)議引用的服務(wù) 判讀一個(gè)服務(wù)是Primary or Secondary Service可通過高層協(xié)議強(qiáng)制規(guī)定 2.2.2 Included Service一個(gè)Included Service是一種引用已存在服務(wù)的方法, 具體辦法為在服務(wù)定義的開始加上Included Service的引用, 這樣整個(gè)Included Service定義成為新服務(wù)定義的一部分 2.2.3 CharacteristicCharacteristic由Characteristic Definition定義, 包含一個(gè)Characteristic聲明、Characteristic屬性、值、值的描述(Optional) 3 Service Interoperability Requirements 3.1 Service Definition服務(wù)定義(Service Definition)包含一個(gè)服務(wù)申明(Service Declaration) 可能包含Include Definitions和Characteristic Definitions 在下一個(gè)服務(wù)申明前或到達(dá)Maximum Attribute Handle時(shí)結(jié)束 服務(wù)定義在服務(wù)端上基于Attribute Handle順序呈現(xiàn) 服務(wù)定義中的Include Definitions和Characteristic Definitions被認(rèn)為是服務(wù)的一部分 服務(wù)定義中的順序?yàn)? 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 |
規(guī)則如下: - 當(dāng)多個(gè)服務(wù)存在時(shí) 使用16-bit Bluetooth UUID服務(wù)定義的服務(wù)應(yīng)該分組(如按順序排列) 同理,使用128-bit UUID的服務(wù)定義的服務(wù)也分組 - 一個(gè)設(shè)備或高層協(xié)議可能有多個(gè)服務(wù)定義,同時(shí)多個(gè)服務(wù)定義含有相同的Service UUID - 服務(wù)端的所有Attributes應(yīng)該包含一個(gè)服務(wù)聲明或存在一個(gè)服務(wù)定義 - 服務(wù)端的服務(wù)定義可能無序;Client不應(yīng)該認(rèn)為服務(wù)端的服務(wù)定義一定是有序的 3.2 Include Definition一個(gè)Include Definition只包含一個(gè)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 |
其中,僅當(dāng)UUID是16-bit Bluetooth UUID時(shí)才存在 如果一個(gè)Service的Include Definition(A)是引用其他Server的Include Definition(B) 那么Include Definition(B)不應(yīng)該引用Include Definition(A),否則就是循環(huán)引用(Circular Reference) 當(dāng)一個(gè)Client檢測到循環(huán)引用或detects nested include declarations to a greater level than it expects Client應(yīng)當(dāng)終止本次通信(ATT Bearer) 3.3 Characteristic DefinitionCharacteristic Definition包含如下內(nèi)容 - Characteristic Declaration : First - Characteristic Value declaration : Second - Characteristic Descriptor Declarations(Optional) : Last(含多個(gè)時(shí)順序不關(guān)緊要) Characteristic Definitions在服務(wù)端以Attribute Handle排序 以上每個(gè)Declaration包含在一個(gè)單獨(dú)的Attribute中 3.3.1 Characteristic DeclarationCharacteristic 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 |
一個(gè)Service可能含有相同Characteristic UUID的Characteristic Definitions 在一個(gè)服務(wù)定義中,一些強(qiáng)制的Characteristics應(yīng)該位于Include Declarations之后、其他可選characteristics之前 而Client不應(yīng)該認(rèn)為Characteristic是有序的 同時(shí),16-bit Bluetooth UUIDs和128-bit Bluetooth UUIDs應(yīng)該分別分組 Characteristic Properties以bit位的方式?jīng)Q定如下內(nèi)容 - Characteristic Value的使用方式 - Characteristic Descriptors的訪問方式 Characteristic Properties bit field的詳細(xì)內(nèi)容如下 Notice: 多個(gè)比特位可以同時(shí)設(shè)置 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 DeclarationsCharacteristic Descriptor Declarations包含了Characteristic Value相關(guān)信息 GATT定義了一系列的標(biāo)準(zhǔn)Characteristic Descriptors供高層協(xié)議使用 高層協(xié)議也可以定義協(xié)議相關(guān)的Characteristic Descriptors Characteristic Descriptors在服務(wù)端上是無序的,Client不應(yīng)該理所當(dāng)然 Characteristic Descriptors Declarations Permissions由高層協(xié)議定義或協(xié)議相關(guān)的 Client不應(yīng)該理所當(dāng)然地認(rèn)為是可讀的 Characteristic Descriptor Declarations包括如下內(nèi)容(詳細(xì)見規(guī)范) - 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 TypesAttribute 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 OverviewGATT中定義了11項(xiàng)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 每個(gè)Feature都有對應(yīng)的過程和子過程,這些過程描述了如何使用ATT來實(shí)現(xiàn)各自的功能 4.2 Feature Support and Procedure Mapping詳細(xì)的對應(yīng)列表如下表 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 MTUClient使用該子過程來設(shè)置適配雙方均支持的最大ATT_MTU 在BR/EDR物理鏈路中不應(yīng)該使用該過程,而應(yīng)該使用L2CAP Channel Configuration Procedures 該過程對應(yīng)于ATT的MTU Exchange Request/Response 見<Bluetooth ATT介紹 - 4.2 MTU Exchange> 4.4 Primary Service DiscoveryClient使用該過程來發(fā)現(xiàn)服務(wù)端的Primary Services 一旦發(fā)現(xiàn)服務(wù)存在,可通過其他過程來訪問Primary Services的附加信息(關(guān)聯(lián)主服務(wù)和次服務(wù)) 可使用的其他過程包括Characteristic Discovery和Relationship Discovery 該過程包括兩個(gè)子過程: - Discover All Primary Services - Discover Primary Services by Service UUID 在BR/EDR物理鏈路上則使用SDP service discovery來發(fā)現(xiàn)服務(wù) 4.4.1 Discover All Primary ServicesClient使用該子過程來發(fā)現(xiàn)服務(wù)端的所有Primary Services 該子過程使用ATT的Read By Group Type Request,同時(shí)設(shè)置如下參數(shù) - Starting Handle : 0x0001 - Ending Handle : 0xFFFF - Attribute Type : UUID for <Primary Service> 可能的回應(yīng)有 - Read By Group Type Response - Error Response Read By Group Type Response返回三元組列表 三元組包括 - Attribute Handle : 服務(wù)聲明的Handle - End Group Handle : 服務(wù)定義中最后一個(gè)Attribute的Handle - Attribute Value : 服務(wù)端支持的服務(wù)的Service UUID 當(dāng)收到Error Response<Error Code: Attribute Not Found>時(shí) 則表明該過程已經(jīng)完成 當(dāng)Client找到自己所需要的服務(wù)時(shí),可以終止該過程 Note: 3.1中已指出Service Declaration是可讀,并且不需要認(rèn)證或授權(quán) 因此權(quán)限相關(guān)的錯誤不會發(fā)生
下圖是一個(gè)實(shí)例圖 
4.4.2 Discover Primary Service by Service UUID當(dāng)Client只知道Service UUID時(shí),可以使用該子過程來發(fā)現(xiàn)對應(yīng)的主服務(wù) 該子過程使用ATT的Find By Type Value Request,同時(shí)設(shè)置參數(shù)如下 - Starting Handle : 0x0001 - Ending Handle : 0xFFFF - Attribute Value : 16-bit Bluetooth UUID or 128-bit UUID - Attribute Type : UUID for <Primary Service> 可能的回應(yīng)有 - Find By Type Value Response - Error Response Find By Type Value Response返回Attribute Handle ranges列表 Attribute Handle range即服務(wù)定義的Starting Handle和Ending Handle 如果Attribute Handle range中的End Found Handle不是0xFFFF 那么Client將會再請求一次Req 同時(shí)將Starting Handle設(shè)置為收到的最后一個(gè)Attribute Handle+1 終止規(guī)則和權(quán)限問題同Discover All Primary Services 下圖是一個(gè)實(shí)例圖 
4.5 Relationship DiscoveryClient使用該過程來發(fā)現(xiàn)和其他服務(wù)的服務(wù)關(guān)系 4.5.1 Find Include ServicesClient使用該子過程來發(fā)現(xiàn)一個(gè)服務(wù)定義包含的服務(wù)申明 該子過程使用ATT的Read By Type Request,同時(shí)設(shè)置參數(shù)如下 - Starting Handle : 所要查找服務(wù)的Starting Handle - Ending Handle : 所要查找服務(wù)的Ending Handle - Attribute Type : UUID for <Include> 可能的回應(yīng)有 - Find By Type Response - Error Response Find By Type Response返回[Attribute Handle, Attribute Value]集合對 Attribute Value由所包含服務(wù)申明的Attribute Handle和End Group Handle組成 當(dāng)UUID為16-bit Bluetooth UUID時(shí),那么它也將包含在Rsp中 該Req應(yīng)該被再次請求,同時(shí)設(shè)置Starting Handle為為收到的最后一個(gè)Attribute Handle+1 當(dāng)Rsp中包含的服務(wù)申明中Attribute Handle等于Req的Ending Handle時(shí), 該子過程被認(rèn)為完成(當(dāng)然Attribute Not Found-Error Rsp也是) 當(dāng)Include Service使用128-bit UUID時(shí) 使用Read Request來獲取Include Service UUID 其中Attribute Handle參數(shù)設(shè)置為Include Service的Attribute Handle 權(quán)限規(guī)則同上面 下圖是一個(gè)實(shí)例圖
|