-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathBluetoothSerial.cpp
248 lines (214 loc) · 5.92 KB
/
BluetoothSerial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2018 Evandro Luis Copercini
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
#include "BluetoothSerial.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_bt_device.h"
#include "esp_spp_api.h"
#define SPP_SERVER_NAME "ESP32_SPP_SERVER"
#define SPP_TAG "BluetoothSerial"
#define QUEUE_SIZE 256
uint32_t client;
xQueueHandle SerialQueueBT;
static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_NONE;
static const esp_spp_role_t role_slave = ESP_SPP_ROLE_SLAVE;
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
switch (event)
{
case ESP_SPP_INIT_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT");
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
esp_spp_start_srv(sec_mask, role_slave, 0, SPP_SERVER_NAME);
break;
case ESP_SPP_DISCOVERY_COMP_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_DISCOVERY_COMP_EVT");
break;
case ESP_SPP_OPEN_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT");
break;
case ESP_SPP_CLOSE_EVT:
client = 0;
ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT");
break;
case ESP_SPP_START_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_START_EVT");
break;
case ESP_SPP_CL_INIT_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_CL_INIT_EVT");
break;
case ESP_SPP_DATA_IND_EVT:
ESP_LOGV(SPP_TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
//esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len); //for low level debug
if (SerialQueueBT != 0){
for (int i = 0; i < param->data_ind.len; i++)
xQueueSend(SerialQueueBT, param->data_ind.data + i, (TickType_t)0);
}
else {
ESP_LOGE(SPP_TAG, "SerialQueueBT ERROR");
}
break;
case ESP_SPP_CONG_EVT:
ESP_LOGI(SPP_TAG, "ESP_SPP_CONG_EVT");
break;
case ESP_SPP_WRITE_EVT:
ESP_LOGV(SPP_TAG, "ESP_SPP_WRITE_EVT");
break;
case ESP_SPP_SRV_OPEN_EVT:
client = param->open.handle;
ESP_LOGI(SPP_TAG, "ESP_SPP_SRV_OPEN_EVT");
break;
default:
break;
}
}
static bool _init_bt(const char *deviceName)
{
if (!btStarted() && !btStart()){
ESP_LOGE(SPP_TAG, "%s initialize controller failed\n", __func__);
return false;
}
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED){
if (esp_bluedroid_init()) {
ESP_LOGE(SPP_TAG, "%s initialize bluedroid failed\n", __func__);
return false;
}
}
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED){
if (esp_bluedroid_enable()) {
ESP_LOGE(SPP_TAG, "%s enable bluedroid failed\n", __func__);
return false;
}
}
if (esp_spp_register_callback(esp_spp_cb) != ESP_OK){
ESP_LOGE(SPP_TAG, "%s spp register failed\n", __func__);
return false;
}
if (esp_spp_init(esp_spp_mode) != ESP_OK){
ESP_LOGE(SPP_TAG, "%s spp init failed\n", __func__);
return false;
}
SerialQueueBT = xQueueCreate(QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue
if (SerialQueueBT == NULL){
ESP_LOGE(SPP_TAG, "%s Queue creation error\n", __func__);
return false;
}
esp_bt_dev_set_device_name(deviceName);
return true;
}
static bool _stop_bt()
{
if (btStarted()){
esp_bluedroid_disable();
esp_bluedroid_deinit();
btStop();
}
return true;
}
/*
* Serial Bluetooth Arduino
*
* */
BluetoothSerial::BluetoothSerial()
{
local_name = "ESP32"; //default bluetooth name
}
BluetoothSerial::~BluetoothSerial(void)
{
_stop_bt();
}
bool BluetoothSerial::begin(String localName)
{
if (localName.length()){
local_name = localName;
}
return _init_bt(local_name.c_str());
}
int BluetoothSerial::available(void)
{
if (!client || SerialQueueBT == NULL){
return 0;
}
return uxQueueMessagesWaiting(SerialQueueBT);
}
int BluetoothSerial::peek(void)
{
if (available()){
if (!client || SerialQueueBT == NULL){
return 0;
}
uint8_t c;
if (xQueuePeek(SerialQueueBT, &c, 0)){
return c;
}
}
return -1;
}
bool BluetoothSerial::hasClient(void)
{
if (client)
return true;
return false;
}
int BluetoothSerial::read(void)
{
if (available()){
if (!client || SerialQueueBT == NULL){
return 0;
}
uint8_t c;
if (xQueueReceive(SerialQueueBT, &c, 0)){
return c;
}
return 0;
}
}
size_t BluetoothSerial::write(uint8_t c)
{
if (client){
uint8_t buffer[1];
buffer[0] = c;
esp_spp_write(client, 1, buffer);
return 1;
}
return -1;
}
size_t BluetoothSerial::write(const uint8_t *buffer, size_t size)
{
if (client){
esp_spp_write(client, size, (uint8_t *)buffer);
}
return size;
}
void BluetoothSerial::flush()
{
if (client){
int qsize = available();
uint8_t buffer[qsize];
esp_spp_write(client, qsize, buffer);
}
}
void BluetoothSerial::end()
{
_stop_bt();
}
#endif