You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
7.9 KiB

  1. | Supported Targets | ESP32 | ESP32-C3 |
  2. | ----------------- | ----- | -------- |
  3. # ESP-IDF SPP GATT CLIENT demo
  4. In Bluetooth classic (BR/EDR) systems, a Serial Port Profile (SPP) is an adopted profile defined by the Bluetooth Special Interest Group (SIG) used to emulate a serial port connection over a Bluetooth wireless connection. For BLE systems, an adopted SPP profile over BLE is not defined, thus emulation of a serial port must be implemented as a vendor-specific custom profile.
  5. This reference design consists of two Demos, the ble spp server and ble spp client that run on their respective endpoints. These devices connect and exchange data wirelessly with each other. This capability creates a virtual serial link over the air. Each byte input can be sent and received by both the server and client. The spp server is implemented as the [ble_spp_server](../ble_spp_server) demo while the spp client is implemented as the [ble_spp_client](../ble_spp_client) demo. Espressif designed the BLE SPP applications to use the UART transport layer but you could adapt this design to work with other serial protocols, such as SPI.
  6. This vendor-specific custom profile is implemented in [spp_client_demo.c](../ble_spp_client/main/spp_client_demo.c) and [spp_server_demo.c](../ble_spp_server/main/ble_spp_server_demo.c).
  7. ## Using Examples
  8. ### Initialization
  9. Both the server and client will first initialize the uart and ble. The server demo will set up the serial port service with standard GATT and GAP services in the attribute server. The client demo will scan the ble broadcast over the air to find the spp server.
  10. ### Event Processing
  11. The spp server has two main event processing functions for BLE event:
  12. ```c
  13. void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
  14. void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
  15. ```
  16. The spp client has two main event processing functions for BLE event:
  17. ```c
  18. esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
  19. void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param);
  20. ```
  21. These are some queues and tasks used by SPP application:
  22. Queues:
  23. * spp_uart_queue - Uart data messages received from the Uart
  24. * cmd_cmd_queue - commands received from the client
  25. * cmd_heartbeat_queue - heartbeat received, if supported
  26. Tasks:
  27. * `uart_task` - process Uart
  28. * `spp_cmd_task` - process command messages, the commands and processing were defined by customer
  29. * `spp_heartbeat_task` - if heartbeat is supported, the task will send a heatbeat packet to the remote device
  30. ### Packet Structure
  31. After the Uart received data, the data will be posted to Uart task. Then, in the UART_DATA event, the raw data may be retrieved. The max length is 120 bytes every time.
  32. If you run the ble spp demo with two ESP32 chips, the MTU size will be exchanged for 200 bytes after the ble connection is established, so every packet can be send directly.
  33. If you only run the ble_spp_server demo, and it was connected by a phone, the MTU size may be less than 123 bytes. In such a case the data will be split into fragments and send in turn.
  34. In every packet, we add 4 bytes to indicate that this is a fragment packet. The first two bytes contain "##" if this is a fragment packet, the third byte is the total number of the packets, the fourth byte is the current number of this packet.
  35. The phone APP need to check the structure of the packet if it want to communicate with the ble_spp_server demo.
  36. ### Sending Data Wirelessly
  37. The client will be sending WriteNoRsp packets to the server. The server side sends data through notifications. When the Uart receives data, the Uart task places it in the buffer. If the size of the data is larger than (MTU size - 3), the data will be split into packets and send in turn.
  38. ### Receiving Data Wirelessly
  39. The server will receive this data in the ESP_GATTS_WRITE_EVT event and send data to the Uart terminal by `uart_wrire_bytes` function. For example:
  40. case ESP_GATTS_WRITE_EVT:
  41. ...
  42. if(res == SPP_IDX_SPP_DATA_RECV_VAL){
  43. uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len);
  44. }
  45. ...
  46. break;
  47. ### GATT Server Attribute Table
  48. charactertistic|UUID|Permissions
  49. :-:|:-:|:-:
  50. SPP_DATA_RECV_CHAR|0xABF1|READ&WRITE_NR
  51. SPP_DATA_NOTIFY_CHAR|0xABF2|READ&NOTIFY
  52. SPP_COMMAND_CHAR|0xABF3|READ&WRITE_NR
  53. SPP_STATUS_CHAR|0xABF4|READ & NOTIFY
  54. SPP_HEARTBEAT_CHAR|0xABF5|READ&WRITE_NR&NOTIFY
  55. ### Build and Flash
  56. Build each project and flash it to the board, then run monitor tool to view serial output:
  57. ```
  58. idf.py -p PORT flash monitor
  59. ```
  60. (To exit the serial monitor, type ``Ctrl-]``.)
  61. See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
  62. ## Example Output
  63. The spp cilent will auto connect to the spp server, do service search, exchange MTU size and register notification.
  64. ### Client
  65. ```
  66. I (2894) GATTC_SPP_DEMO: ESP_GATTC_CONNECT_EVT: conn_id=0, gatt_if = 3
  67. I (2894) GATTC_SPP_DEMO: REMOTE BDA:
  68. I (2904) GATTC_SPP_DEMO: 00 00 00 00 00 00
  69. I (2904) GATTC_SPP_DEMO: EVT 2, gattc if 3
  70. I (3414) GATTC_SPP_DEMO: EVT 7, gattc if 3
  71. I (3414) GATTC_SPP_DEMO: ESP_GATTC_SEARCH_RES_EVT: start_handle = 40, end_handle = 65535, UUID:0xabf0
  72. I (3424) GATTC_SPP_DEMO: EVT 6, gattc if 3
  73. I (3424) GATTC_SPP_DEMO: SEARCH_CMPL: conn_id = 0, status 0
  74. I (3464) GATTC_SPP_DEMO: EVT 18, gattc if 3
  75. I (3464) GATTC_SPP_DEMO: +MTU:200
  76. I (3464) GATTC_SPP_DEMO: attr_type = PRIMARY_SERVICE,attribute_handle=40,start_handle=40,end_handle=65535,properties=0x0,uuid=0xabf0
  77. I (3474) GATTC_SPP_DEMO: attr_type = CHARACTERISTIC,attribute_handle=42,start_handle=0,end_handle=0,properties=0x6,uuid=0xabf1
  78. I (3484) GATTC_SPP_DEMO: attr_type = CHARACTERISTIC,attribute_handle=44,start_handle=0,end_handle=0,properties=0x12,uuid=0xabf2
  79. I (3494) GATTC_SPP_DEMO: attr_type = DESCRIPTOR,attribute_handle=45,start_handle=0,end_handle=0,properties=0x0,uuid=0x2902
  80. I (3504) GATTC_SPP_DEMO: attr_type = CHARACTERISTIC,attribute_handle=47,start_handle=0,end_handle=0,properties=0x6,uuid=0xabf3
  81. I (3524) GATTC_SPP_DEMO: attr_type = CHARACTERISTIC,attribute_handle=49,start_handle=0,end_handle=0,properties=0x12,uuid=0xabf4
  82. I (3534) GATTC_SPP_DEMO: attr_type = DESCRIPTOR,attribute_handle=50,start_handle=0,end_handle=0,properties=0x0,uuid=0x2902
  83. I (3544) GATTC_SPP_DEMO: Index = 2,UUID = 0xabf2, handle = 44
  84. I (3554) GATTC_SPP_DEMO: EVT 38, gattc if 3
  85. I (3554) GATTC_SPP_DEMO: Index = 2,status = 0,handle = 44
  86. I (3594) GATTC_SPP_DEMO: EVT 9, gattc if 3
  87. I (3594) GATTC_SPP_DEMO: ESP_GATTC_WRITE_DESCR_EVT: status =0,handle = 45
  88. I (3654) GATTC_SPP_DEMO: Index = 5,UUID = 0xabf4, handle = 49
  89. I (3654) GATTC_SPP_DEMO: EVT 38, gattc if 3
  90. I (3654) GATTC_SPP_DEMO: Index = 5,status = 0,handle = 49
  91. I (3684) GATTC_SPP_DEMO: EVT 9, gattc if 3
  92. I (3684) GATTC_SPP_DEMO: ESP_GATTC_WRITE_DESCR_EVT: status =0,handle = 50
  93. I (16904) GATTC_SPP_DEMO: EVT 10, gattc if 3
  94. I (16904) GATTC_SPP_DEMO: ESP_GATTC_NOTIFY_EVT
  95. I (16904) GATTC_SPP_DEMO: +NOTIFY:handle = 44,length = 22
  96. ```
  97. ### Server
  98. ```
  99. I (4452) GATTS_SPP_DEMO: EVT 14, gatts if 3
  100. I (4452) GATTS_SPP_DEMO: event = e
  101. I (5022) GATTS_SPP_DEMO: EVT 4, gatts if 3
  102. I (5022) GATTS_SPP_DEMO: event = 4
  103. I (5152) GATTS_SPP_DEMO: EVT 2, gatts if 3
  104. I (5152) GATTS_SPP_DEMO: event = 2
  105. I (5152) GATTS_SPP_DEMO: ESP_GATTS_WRITE_EVT : handle = 5
  106. I (5242) GATTS_SPP_DEMO: EVT 2, gatts if 3
  107. I (5242) GATTS_SPP_DEMO: event = 2
  108. I (5242) GATTS_SPP_DEMO: ESP_GATTS_WRITE_EVT : handle = 10
  109. I (18462) GATTS_SPP_DEMO: EVT 5, gatts if 3
  110. I (18462) GATTS_SPP_DEMO: event = 5
  111. I (27652) GATTS_SPP_DEMO: EVT 2, gatts if 3
  112. I (27652) GATTS_SPP_DEMO: event = 2
  113. I (27652) GATTS_SPP_DEMO: ESP_GATTS_WRITE_EVT : handle = 2
  114. ```
  115. if you input data to the Uart terminal, it will be printed in the remote device Uart terminal.