6/22/2021

ESP32 BT(Bluetooth) Host Stack 정리

1. BT의 Host Stack 구조 

BT의 Host Stack과 Profile의 전부 파악하기 힘들므로, 각각의 기능에 따라 분리하여 각 항목별로 Stack 구조파악을 하자.

ESP32 BT Stack 동작과 비교 


1.1 BT Audio기반 Stack 구조

  • Bluetooth Classic Stack
  1. Profiles: GAP, A2DP(SNK), AVRCP(CT)
  2. Protocols: L2CAP, SDP, AVDTP, AVCTP 
Bluetooth의 경우 중요한것은 Profile들이며, 대부분 Protocol들이 지원하므로, 관련부분은 참조만 하자.
더불어 Profile의 지원여부들을 확인하자.
https://www.espressif.com/sites/default/files/documentation/esp32_bluetooth_architecture_en.pdf


  • L2CAP(Logical Link Control and Adaptation Protocol)
OSI Layer 2 계층이며, Packet 재전송과 Flow Control 이외 Packet 분할과 재조립부터 다양한 기능들을 제공해주고 있으며, 상위 Protocol Multiplexing 도 제공 
  1. Basic L2CAP Mode
  2. Flow Control Mode
  3. Retransmission Mode
  4. Enhanced Retransmission Mode
  5. Streaming Mode
  6. LE Credit-Based Flow Control Mode

  • SDP(Service Discovery Protocol)
L2CAP기반에 주 역할은 상대의 Service를 찾는것이며, Server 와 Client형태로 구성이되어 Server는 각 Service들을 유지하고 있다.

  • GAP(General Access Profile) 
GAP의 경우, description의 mode와 device discoverability, connection and security 의 절차를 제공한다고 한다.
GAP의 경우 기본으로 사용되므로 사용방법을 알아두며, GATT를 사용시에도 사용되므로 중요.

  • A2DP (Advanced Audio Distribution Profile)
BT에서 Audio를 사용한다면, A2DP일 것이며, ACL Channel를 통해 제공하며, SRC -> SINK형태로 Audio를 제공 

AVRCP(Audio/Video Remote Control Profile)를 보면 대충 Remocon 기능으로 동작될 것 같으며, CT(Controller) 와 TG(Target)으로 동작되어진다고 한다.

https://www.espressif.com/sites/default/files/documentation/esp32_bluetooth_architecture_en.pdf


1.2 BT의 GAP/GATT 기반의 Stack 구조 


GAP(General Access Profile)
GATT(Generic Attribute Profile) 
ATT(Attribute Protocol)
SM(Security Manager)
L2CAP(Logical Link Control and Adaption Protocol)


https://smartits.tistory.com/18?category=693936


https://igotit.tistory.com/entry/BLE-Protocol-Stack-Bluetooth-Low-Energy



  • BLE의 GAP(General Access Profile)
BLE와 BT에서 GAP의 동작이 다르지는 않을 것이라고 생각되지만, 일단 아래의 BLE기반이라고 하므로 추후에 비교분석을 별도로 해야함

  • BLE의 GAP의 역할 
  1. Broadcaster : Advertising Packet을 보내는 역할로 Observer가 이를 찾아주길 바란다.
  2. Observer: Broadcaster의 정보를 찾고 App에게 이를 보고는하지만 연결은 못함
  3. Peripheral: connectable advertising packets 이용하여 advertise하고 연결가능하며 slave로 연결동작 
  4. Central: Peripheral과 연결가능하며, Master로 동작 

GAP의 Status이며, GATT를 연결하여 동작하면 쉽게 이해할 수 있다. 

  • Device-A 에서는 GAP 기반의 GATT Server 의 동작  
  1. BT Device-A(미밴드) Advertising Packet를 보내 다른 BT Device-B(핸드폰)들이 발견해주기를 바란다.
  2. BT Device-A는 GATT Server를 동작하고 있으며, BT Device-B가 Peripheral(Slave)연결가능하며 연결진행 
  3. 연결된 후 BT Device-A는 GATT Server로 Master(Central)로 동작 


  • GAP의 상태로 상위에서 Slave 와 Master까지 표시 

http://software-dl.ti.com/lprf/simplelink_cc2640r2_sdk/1.35.00.33/exports/docs/ble5stack/ble_user_guide/html/ble-stack/gap.html


핸드폰(Client)에서 Device(Server)로 연결한 후 각 Characteristic 값들을 변경하여 반영한다.
아래는 단방향이지만, 양방향으로 가능 (Notify Server-> Client)



2.  BT의 Stack 의 동작 및 구현정보

현재 Audio 관련부분은 경험이 없으므로, 생략하며, GAP 과 GATT Service기반으로 간단히 설명하고 정리한다.


2.1 GGS(GAP GATT Service) 동작 및 확인

Device에서 GATT Server로 Service를 구현하여, 핸드폰에서 이 Device를 발견하고 연결한 후 Characteristic Value를 변경한다.

  

  • GATT(General Attribute Profile) Client

http://software-dl.ti.com/lprf/simplelink_cc2640r2_sdk/1.35.00.33/exports/docs/ble5stack/ble_user_guide/html/ble-stack/gatt.html#gap-gatt-service-ggs


  • GATT Client-> Server 연결 후 Server 값들 확인 
아래의 16bite UUID의 값은 역할이 정해져 있으므로, 각 값은 반드시 정확한 의미를 알고 사용해야한다.

    Type             UUID       Allocated for 
//GATT Service 등록시 Primary or Secondary Service 설정 
//Service 등록되면 Characteristic 설정하여 값 이름 과 값 설정 등등
GATT Declarations 0x2800   Primary Service
GATT Declarations 0x2801   Secondary Service
GATT Declarations 0x2802   Include
GATT Declarations 0x2803   Characteristic

//설명으로 옵션으로 설정
GATT Descriptor 0x2900 Characteristic Extended Properties
GATT Descriptor 0x2901 Characteristic User Description
GATT Descriptor 0x2902 Client Characteristic Configuration
GATT Descriptor 0x2903 Server Characteristic Configuration
GATT Descriptor 0x2904 Characteristic Presentation Format
GATT Descriptor 0x2905 Characteristic Aggregate Format
GATT Descriptor 0x2906 Valid Range
GATT Descriptor 0x2907 External Report Reference
GATT Descriptor 0x2908 Report Reference
GATT Descriptor 0x2909 Number of Digitals
GATT Descriptor 0x290A Value Trigger Setting

//GATT 기본서비스로 Device Name 설정 및 기타기능설정 
GATT Service 0x1800 Generic Access

//GATT 기본서비스 속성으로 Service 동작방식제어
GATT Service 0x1801 Generic Attribute

// Device Information Service로 아래에 있는 것처럼 Device 관련된 정보출력
GATT Service 0x180A Device Information

// Heart Rate Service (Notify를 이용하여 각 정보제공)
GATT Service 0x180D Heart Rate

// Battery Service 
GATT Service 0x180F Battery

BT관련정보들 


** 주의해야할 것은 Handle 과 UUID를 혼동하지말자.
http://software-dl.ti.com/lprf/simplelink_cc2640r2_sdk/1.35.00.33/exports/docs/ble5stack/ble_user_guide/html/ble-stack/gatt.html#gap-gatt-service-ggs


UUID의 경우 16bit 이외에도 128bit 생성하여 Custom UUID로 사용가능하며, 이 값도 설정가능 


  • GATT Service 구현시 
  1. ATT(Attribute Protocol):  Server or Client 설정 및 각 Type 과 권한설정 
  2. GATT(General Attribute Profile) : 아래 기능들을 설정하여 구현 
    1. Services:  Server에서 동작할 Service ( Service UUID 찾기)
    2. Characteristics: Service에서 사용되는 값 정의  및 설정 등 (Char UUID 찾기)
    3. Descriptor: 관련설명 명시 

https://www.novelbits.io/bluetooth-gatt-services-characteristics/

  • GAP/GATT Service에서 가장 기본예제 (Heart Rate)
아래 사이트를 찾아봐도 쉽게 다양한 자료를 검색가능하며,  핸드폰으로 Device의 GATT Server로 접속하여 Hear Rate 정보를 받는다.


https://www.cypress.com/file/179851/download


  • BLE GATT(Generic Attribute Profile) 구현 및 테스트 예제 
아래의 사이트가 가장도움이 되었으며, 쉽게 Android에서 nRF Connect를 이용하여 테스트가 가능하였다.

아래사이트 반드시 참조


  • GATT의 Device Info or Battery Service 구현할 경우 예제 
각 필요한 GATT의 Profile 내부에서 사용되는 Service확인 
  1. GATT 사용될 Service의 UUID
  2. 상위 Service의 Characteristics의 UUID를 찾아 정의하고 값 설정 
  3. Description도 필요하다면 설정 

/* Device Information Service UUID */
#define GATT_DEVICE_INFO_UUID                   0x180A

/* Device Information Service's Characteristics  */
#define GATT_MANUFACTURER_NAME_UUID             0x2A29
#define GATT_MODEL_NUMBER_UUID                  0x2A24
#define GATT_SERIAL_NUMBER_UUID                 0x2A25
#define GATT_HARDWARE_REVISION_UUID             0x2A27
#define GATT_FIRMWARE_REVISION_UUID             0x2A26
/* Heart-rate configuration  Service */ 
#define GATT_HRS_UUID                           0x180D

/* Heart-rate configuration 's Characteristics  */ 
#define GATT_HRS_MEASUREMENT_UUID               0x2A37
#define GATT_HRS_BODY_SENSOR_LOC_UUID           0x2A38
/* Battery Service UUID */
#define GATT_BATTERY_SERVICE_UUID               0x180F

/*  Battery Service 's Characteristics  */ 
#define GATT_BATTERY_LEVEL_UUID                 0x2A19
//#define GATT_BATTERY_LEVEL_STATE_UUID           0x2A1B
//#define GATT_BATTERY_POWER_STATE_UUID           0x2A1A
 
구현하고자 하는 Service의 Spec을 아래에서 찾은 후 확인

3.  ESP32 OS 관련정보 

ESP32 기본개발환경은 expressif가 제공을 해주고 있으며, VSCode로 연결하여 Download도 가능하다.
기본으로 FreeRTOS를 사용하고 있으며, 많은 예제를 제공을 해주고 있다. 

  • ESP32 개발환경 문서 
  • ESP32 개발환경소스 


3.1 AWS FreeRTOS

AWS FreeRTOS는 AWS가 주관으로 FreeRTOS에서 부족한 Library와 버그들을 수정을해가면서 제공을 해주고 있다. 
만약 ESP32 혹은 다른 MCU기반으로 AWS FreeRTOS기반으로 연결하여 쉽게 테스트는 가능하지만, 현재 테스트 결과 ESP32의 경우 
Heap 관련부분에 좀 문제가 존재하지만, 추후에 이 부분은 보강이 될것이라고 생각된다.

  • AWS FreeRTOS
AWS FreeRTOS 문서

AWS FreeRTOS 소스


3.2 Zephyr

  • Zephyr
ESP32도 Zepheyr가 제공되기 시작했지만, 아직 테스트를 해보지는 못함