2017年2月27日 星期一

BLE ATT, GATT protocol 解析

markdown GATT,也就是 Generic Attribute Profile 的縮寫,是 Bluetooth Low Energy 裝置用來傳送資料的 protocol,它包含了所謂 Service 和 Characteristics 的概念在其中,並且建構在 Attribute Protocol(ATT) 的 protocol 之上 Attribute Protocol ---- ATT 定義了名為 Attribute 的結構,包含三個元素 * 16-bits handle * UUID(官方已有定義的為16-bits, 自定義的為 128-bits) 定義了 attribute type * Attribute value, 就是一 byte array, 其意義由 UUID 決定 ATT 單只是定義了這個結構,並未定義任何 UUID,這留給 GATT 來定義. 結構中 value 的長度取決於 UUID,傳送資料時並不會包含 value 的長度訊息 ATT server 維護 Attribute 資訊,ATT client 則否 Generic Attribute Profile ---- GATT 定義了許多的 Attribute,並且提出了 Service 和 Characteristics 的概念,由 Attribute 組成 Characteristics,再由多個 Characteristics 組成 Service,多個 Service 則組成 Profile ![Attribute structure](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGhB8leQjh7r9NZ0x-nHWAb4a-EcOlX5SPwxT8jYPWnywR2uBdagnJcPNa-QxEFbTAiq0gsLMIpVK-lJlmWfKUZdF1axGBWurcqUaOQGGd-yNW1bpMFY5tvrl8oe_K9NsJt5MmnuvguBV9/s1600/microcontrollers_GattStructure.png) 之前提到 ATT server 維護 Attribute 資訊,當 BLE 裝置連上後,可以跟 server 查詢其上面所有的 Attribute,client 可以得到一個 Attribute 列表 ![Attribute sample](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2iW68RFmDnZ0ANgHjcD8lGArmTYGcbb266QGxDm8EYNP9b8Njg7vTODpPB6LZnaN-dNV4JfJjsI2zm1sfknnUfhdL6pGnFTSnS4t0cINRLgc4Oo_NJ95iyVqlAoeKxrC-YiiBLIE0BxJW/s1600/%25E7%25B4%25A2%25E5%25BC%2595.png) GATT 定義了一個 Service 總是從 UUID 0x2800 開始的,從 0x2800 開始的所有 Attribute 都屬於該 service,直到下一個 0x2800 為止 0x2800 除了表示 Service 的起點,這個 Attribute 的 value 也定義了該 Service 是哪種 Service,例如 value = 0x180D 的話就表示這是一個 Heart Rate Service 一個 Service 總是包含了一個以上的 Characteristics,GATT 也定義了特定的 Attribute 來表示 Service 中有哪些 Characteristics ![GATT sample](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrZxUrwqFxwr0FhzmABicDyKVrN46acZ19_E-6e9dTutWC9qcKyTGbliekYIRsyvQeVnzEx8WSVaHexqE_Zy4dJdScXlr3QyvCehJVjK7Fg7w1lKxi13E-sb6DnAzFkT9WBdPNsocATY7H/s1600/table.png =800x*) UUID = 0x2803 的 Attribute 表示這個 Service 有一個 Characteristic,而是甚麼 Characteristics,它的值又在哪邊,則寫在此 Attribute 的 value 中,如上圖所示,第二列表示 Heart Rate Service 有一個 Characteristic,這個 Characteristic 的值在 Handle = 0x000E,UUID = 0x2A37 的 Attribute 中,也就是第三列,UUID 0x2A37 也是 BLE 標準預定義好的,表示 Heart Rate Measurement Characteristic,所以,實際上最少會用兩個 Attribute 來表示一個 Characteristic 這種設計方式非常的彈性,Client 可以知道有多少 Service 存在,Service 中又有多少 Characteristics,而不一定要確切知道這些 Service 和 Characteristic 是甚麼,BLE 裝置之間就以它們認識的 Service 互相溝通就好,而之後要新增新的 Characteristic和 Service,也不需要改變既有的 client 程式

沒有留言:

張貼留言