Skip to content

Commit 1c3dd23

Browse files
committed
Merge branch 'feature/btdm_ble_spp_docs' into 'master'
component/bt: add readme for ble spp demo See merge request !1748
2 parents 429371c + c49a07e commit 1c3dd23

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed
Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,80 @@
1-
ESP-IDF SPP GATT CLIENT demo
2-
========================
1+
# ESP-IDF SPP GATT CLIENT demo
32

3+
## 1. Overview
4+
5+
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.
6+
7+
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.
8+
9+
## 2. Packet Structure
10+
11+
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.
12+
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.
13+
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.
14+
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.
15+
The phone APP need to check the structure of the packet if it want to communicate with the ble_spp_server demo.
16+
17+
## 3. Software Description
18+
19+
The application 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).
20+
21+
### 3.1 Initialization
22+
23+
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.
24+
25+
### 3.2 Event Processing
26+
27+
The spp server has two main event processing functions for BLE event:
28+
29+
* static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
30+
* static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
31+
32+
The spp client has two main event processing function for BLE event:
33+
34+
* esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
35+
* void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param);
36+
37+
These are some queues and tasks used by SPP application:
38+
39+
Queues:
40+
41+
* spp_uart_queue - Uart data messages received from the Uart
42+
* cmd_cmd_queue - commands received from the client
43+
* cmd_heartbeat_queue - heartbeat received, if supported
44+
45+
Tasks:
46+
47+
* uart_task - processs Uart
48+
* spp_cmd_task - process command messages, the commands and processing were defined by customer
49+
* spp_heartbeat_task - if heartbeat is supported, the task will send a heatbeat packet to the remote device
50+
51+
### 3.3 Sending Data Wirelessly
52+
53+
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.
54+
55+
### 3.4 Receiving Data Wirelessly
56+
57+
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:
58+
59+
case ESP_GATTS_WRITE_EVT:
60+
...
61+
if(res == SPP_IDX_SPP_DATA_RECV_VAL){
62+
uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len);
63+
}
64+
...
65+
break;
66+
67+
### 3.5 GATT Server Attribute Table
68+
69+
charactertistic|UUID|Permissions
70+
:-:|:-:|:-:
71+
SPP_DATA_RECV_CHAR|0xABF1|READ&WRITE_NR
72+
SPP_DATA_NOTIFY_CHAR|0xABF2|READ&NOTIFY
73+
SPP_COMMAND_CHAR|0xABF3|READ&WRITE_NR
74+
SPP_STATUS_CHAR|0xABF4|READ & NOTIFY
75+
SPP_HEARTBEAT_CHAR|0xABF5|READ&WRITE_NR&NOTIFY
76+
77+
## 4. How to run these demos
78+
79+
Compile and download each application to the ESP32. The spp cilent will auto connect to the spp server, do service search, exchange MTU size and register notification.
80+
if you input data to the Uart terminal, it will be print in the remote device Uart terminal.
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
ESP-IDF GATT SERVER SPP demo
2-
===============================================
3-
4-
This is the demo for user to realization BLE Serial Port Profile.
1+
## ESP-IDF GATT SERVER SPP demo
52

3+
For description of this application please refer to [ESP-IDF GATT CLIENT SPP demo](../ble_spp_client/README.md)

0 commit comments

Comments
 (0)