1
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
39
40
41
42
43
48
49
50
51
52
53
54
59
60
61
62
63
64
65
66
67
76
77
78
81
82
83
84
91
92
96
97
98
99
100
101
102
103
104
105
106
107
110
111
112
113
114
115
116
117
118
119
123
124
127
128
129
133
134
135
136
137
138
139
140
141
142
143
147
148
149
154
155
156
157
158
159
160
161
162
168
169
170
171
175
176
177
180
181
184
189
190
191
192
197
198
199
200
201
202
203
204
207
208
209
216
217
218
219
223
224
227
228
229
230
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
292
293
294
295
296
300
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
329
330
331
332
333
334
335
336
337
338
339
340
342
343
344
351
353
354
355
356
357
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
404
405
406
417
418
419
420
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
458
459
460
461
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* ... */
#include "esp_log.h"
#include "nvs_flash.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "console/console.h"
#include "services/gap/ble_svc_gap.h"
#include "ble_spp_client.h"
#include "driver/uart.h"10 includes
#define PEER_ADDR_VAL_SIZE 6
static const char *tag = "NimBLE_SPP_BLE_CENT";
static int ble_spp_client_gap_event(struct ble_gap_event *event, void *arg);
QueueHandle_t spp_common_uart_queue = NULL;
void ble_store_config_init(void);
uint16_t attribute_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
static void ble_spp_client_scan(void);
static ble_addr_t connected_addr[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
static void ble_spp_client_write_subscribe(const struct peer *peer)
{
uint8_t value[2];
int rc;
const struct peer_dsc *dsc;
/* ... */
dsc = peer_dsc_find_uuid(peer,
BLE_UUID16_DECLARE(GATT_SPP_SVC_UUID),
BLE_UUID16_DECLARE(GATT_SPP_CHR_UUID),
BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16));
if (dsc == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer lacks a CCCD for the Unread Alert "
"Status characteristic\n");
goto err;
}{...}
value[0] = 1;
value[1] = 0;
rc = ble_gattc_write_flat(peer->conn_handle, dsc->dsc.handle,
value, sizeof value, NULL, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to subscribe to characteristic; "
"rc=%d\n", rc);
goto err;
}{...}
return;
err:
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
static void
ble_spp_client_set_handle(const struct peer *peer)
{
const struct peer_chr *chr;
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(GATT_SPP_SVC_UUID),
BLE_UUID16_DECLARE(GATT_SPP_CHR_UUID));
attribute_handle[peer->conn_handle] = chr->chr.val_handle;
}{ ... }
/* ... */
static void
ble_spp_client_on_disc_complete(const struct peer *peer, int status, void *arg)
{
if (status != 0) {
MODLOG_DFLT(ERROR, "Error: Service discovery failed; status=%d "
"conn_handle=%d\n", status, peer->conn_handle);
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
return;
}{...}
/* ... */
MODLOG_DFLT(INFO, "Service discovery complete; status=%d "
"conn_handle=%d\n", status, peer->conn_handle);
ble_spp_client_set_handle(peer);
ble_spp_client_write_subscribe(peer);
#if CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1
ble_spp_client_scan();
#endif
}{ ... }
/* ... */
static void
ble_spp_client_scan(void)
{
uint8_t own_addr_type;
struct ble_gap_disc_params disc_params;
int rc;
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
return;
}{...}
/* ... */
disc_params.filter_duplicates = 1;
/* ... */
disc_params.passive = 1;
disc_params.itvl = 0;
disc_params.window = 0;
disc_params.filter_policy = 0;
disc_params.limited = 0;
rc = ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params,
ble_spp_client_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error initiating GAP discovery procedure; rc=%d\n",
rc);
}{...}
}{ ... }
/* ... */
static int
ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
{
struct ble_hs_adv_fields fields;
int rc;
int i;
for ( i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
if (memcmp(&connected_addr[i].val, disc->addr.val, PEER_ADDR_VAL_SIZE) == 0) {
MODLOG_DFLT(DEBUG, "Device already connected");
return 0;
}{...}
}{...}
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
return 0;
}{...}
rc = ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data);
if (rc != 0) {
return 0;
}{...}
/* ... */
for (i = 0; i < fields.num_uuids16; i++) {
if (ble_uuid_u16(&fields.uuids16[i].u) == GATT_SPP_SVC_UUID) {
return 1;
}{...}
}{...}
return 0;
}{ ... }
/* ... */
static void
ble_spp_client_connect_if_interesting(const struct ble_gap_disc_desc *disc)
{
uint8_t own_addr_type;
int rc;
if (!ble_spp_client_should_connect(disc)) {
return;
}{...}
#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}{...}
#endif/* ... */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
return;
}{...}
/* ... */
rc = ble_gap_connect(own_addr_type, &disc->addr, 30000, NULL,
ble_spp_client_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
"addr=%s; rc=%d\n",
disc->addr.type, addr_str(disc->addr.val), rc);
return;
}{...}
}{ ... }
/* ... */
static int
ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_DISC:
rc = ble_hs_adv_parse_fields(&fields, event->disc.data,
event->disc.length_data);
if (rc != 0) {
return 0;
}{...}
print_adv_fields(&fields);
ble_spp_client_connect_if_interesting(&event->disc);
return 0;
...
case BLE_GAP_EVENT_LINK_ESTAB:
if (event->link_estab.status == 0) {
MODLOG_DFLT(INFO, "Connection established ");
rc = ble_gap_conn_find(event->link_estab.conn_handle, &desc);
assert(rc == 0);
memcpy(&connected_addr[event->link_estab.conn_handle].val, desc.peer_id_addr.val,
PEER_ADDR_VAL_SIZE);
print_conn_desc(&desc);
MODLOG_DFLT(INFO, "\n");
rc = peer_add(event->link_estab.conn_handle);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to add peer; rc=%d\n", rc);
return 0;
}{...}
rc = peer_disc_all(event->link_estab.conn_handle,
ble_spp_client_on_disc_complete, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}{...}
}{...} else {
MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n",
event->link_estab.status);
ble_spp_client_scan();
}{...}
return 0;
...
case BLE_GAP_EVENT_DISCONNECT:
MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
print_conn_desc(&event->disconnect.conn);
MODLOG_DFLT(INFO, "\n");
memset(&connected_addr[event->disconnect.conn.conn_handle].val, 0, PEER_ADDR_VAL_SIZE);
attribute_handle[event->disconnect.conn.conn_handle] = 0;
peer_delete(event->disconnect.conn.conn_handle);
ble_spp_client_scan();
return 0;
...
case BLE_GAP_EVENT_DISC_COMPLETE:
MODLOG_DFLT(INFO, "discovery complete; reason=%d\n",
event->disc_complete.reason);
return 0;
...
case BLE_GAP_EVENT_NOTIFY_RX:
MODLOG_DFLT(INFO, "received %s; conn_handle=%d attr_handle=%d "
"attr_len=%d\n",
event->notify_rx.indication ?
"indication" :
"notification",
event->notify_rx.conn_handle,
event->notify_rx.attr_handle,
OS_MBUF_PKTLEN(event->notify_rx.om));
/* ... */
return 0;
...
case BLE_GAP_EVENT_MTU:
MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n",
event->mtu.conn_handle,
event->mtu.channel_id,
event->mtu.value);
return 0;
...
default:
return 0;...
}{...}
}{ ... }
static void
ble_spp_client_on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}{ ... }
static void
ble_spp_client_on_sync(void)
{
int rc;
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
ble_spp_client_scan();
}{ ... }
void ble_spp_client_host_task(void *param)
{
ESP_LOGI(tag, "BLE Host Task Started");
nimble_port_run();
nimble_port_freertos_deinit();
}{ ... }
void ble_client_uart_task(void *pvParameters)
{
ESP_LOGI(tag, "BLE client UART task started");
int rc;
int i;
uart_event_t event;
for (;;) {
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
switch (event.type) {
case UART_DATA:
if (event.size) {
uint8_t *temp = NULL;
temp = (uint8_t *)malloc(sizeof(uint8_t) * event.size);
if (temp == NULL) {
ESP_LOGE(tag, "malloc failed,%s L#%d", __func__, __LINE__);
break;
}{...}
memset(temp, 0x0, event.size);
uart_read_bytes(UART_NUM_0, temp, event.size, portMAX_DELAY);
for ( i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
if (attribute_handle[i] != 0) {
rc = ble_gattc_write_flat(i, attribute_handle[i], temp, event.size, NULL, NULL);
if (rc == 0) {
ESP_LOGI(tag, "Write in uart task success!");
}{...} else {
ESP_LOGI(tag, "Error in writing characteristic rc=%d", rc);
}{...}
vTaskDelay(10);
}{...}
}{...}
free(temp);
}{...}
break;...
default:
break;...
}{...}
}{...}
}{...}
vTaskDelete(NULL);
}{ ... }
static void ble_spp_uart_init(void)
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_RTS,
.rx_flow_ctrl_thresh = 122,
.source_clk = UART_SCLK_DEFAULT,
}{...};
uart_driver_install(UART_NUM_0, 4096, 8192, 10, &spp_common_uart_queue, 0);
uart_param_config(UART_NUM_0, &uart_config);
uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
xTaskCreate(ble_client_uart_task, "uTask", 4096, (void *)UART_NUM_0, 8, NULL);
}{ ... }
void
app_main(void)
{
int rc;
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}{...}
ESP_ERROR_CHECK(ret);
ret = nimble_port_init();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", ret);
return;
}{...}
ble_spp_uart_init();
ble_hs_cfg.reset_cb = ble_spp_client_on_reset;
ble_hs_cfg.sync_cb = ble_spp_client_on_sync;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
rc = ble_svc_gap_device_name_set("nimble-ble-spp-client");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(ble_spp_client_host_task);
}{ ... }