1
6
7
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
41
42
43
44
45
46
47
48
49
52
53
60
61
62
63
64
65
66
67
71
77
78
79
83
84
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
119
120
123
124
125
126
127
128
129
130
131
132
133
134
135
140
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
161
162
163
164
165
166
173
174
178
179
180
181
184
185
186
187
190
191
192
193
194
195
196
197
198
199
203
204
207
208
209
213
214
215
216
217
218
219
220
221
222
223
227
228
229
234
235
236
237
238
239
240
241
242
245
246
247
248
249
250
251
252
255
256
258
259
260
261
264
265
266
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
291
292
293
296
297
298
299
300
301
302
303
304
307
308
309
311
316
317
318
319
320
325
326
327
328
329
330
331
332
333
334
338
342
343
344
351
352
353
354
358
359
362
363
364
365
366
367
368
369
370
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
401
402
403
404
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
429
430
431
432
433
434
438
439
440
441
442
446
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
474
480
481
482
484
485
486
487
488
501
502
503
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
540
541
542
543
544
545
546
547
548
549
550
551
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* ... */
#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 "coc_blecent.h"9 includes
static const char *tag = "NimBLE_BLE_CENT_L2CAP_COC";
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
void ble_store_config_init(void);
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) >= 1
#define COC_BUF_COUNT (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
#define L2CAP_COC_UUID 0x1812
#define MTU 512
static uint16_t conn_handle_coc ;
static os_membuf_t sdu_coc_mem[OS_MEMPOOL_SIZE(COC_BUF_COUNT, MTU * 2)];
static struct os_mempool sdu_coc_mbuf_mempool;
static struct os_mbuf_pool sdu_os_mbuf_pool;
static int blecent_l2cap_coc_event_cb(struct ble_l2cap_event *event, void *arg);
struct ble_l2cap_chan *coc_chan = NULL;
/* ... */
static void
blecent_l2cap_coc_send_data(struct ble_l2cap_chan *chan)
{
struct os_mbuf *sdu_rx_data;
int rc = 0;
int len = 512;
uint8_t value[len];
for (int i = 0; i < len; i++) {
value[i] = i;
}{...}
do {
sdu_rx_data = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
if (sdu_rx_data == NULL) {
vTaskDelay(10 / portTICK_PERIOD_MS);
sdu_rx_data = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
}{...}
}{...} while (sdu_rx_data == NULL);
os_mbuf_append(sdu_rx_data, value, len);
print_mbuf_data(sdu_rx_data);
rc = ble_l2cap_send(chan, sdu_rx_data);
while (rc == BLE_HS_ESTALLED) {
vTaskDelay(100 / portTICK_PERIOD_MS);
rc = ble_l2cap_send(chan, sdu_rx_data);
}{...}
if (rc == 0) {
MODLOG_DFLT(INFO, "Data sent successfully");
}{...} else {
MODLOG_DFLT(INFO, "Data sending failed, rc = %d", rc);
os_mbuf_free_chain(sdu_rx_data);
}{...}
}{ ... }
/* ... */
static void
blecent_l2cap_coc_on_disc_complete(const struct peer *peer, int status, void *arg)
{
uint16_t psm = 0x1002;
struct os_mbuf *sdu_rx;
sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
ble_l2cap_connect(conn_handle_coc, psm, MTU, sdu_rx, blecent_l2cap_coc_event_cb,
NULL);
}{ ... }
/* ... */
static int
blecent_l2cap_coc_event_cb(struct ble_l2cap_event *event, void *arg)
{
struct ble_l2cap_chan_info chan_info;
switch (event->type) {
case BLE_L2CAP_EVENT_COC_CONNECTED:
if (event->connect.status) {
console_printf("LE COC error: %d\n", event->connect.status);
return 0;
}{...}
if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) {
assert(0);
}{...}
console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x,"
" scid: 0x%04x, ""dcid: 0x%04x, our_mps: %d, our_mtu: %d,"
" peer_mps: %d, peer_mtu: %d\n",
event->connect.conn_handle, event->connect.chan,
chan_info.psm, chan_info.scid, chan_info.dcid,
chan_info.our_l2cap_mtu, chan_info.our_coc_mtu,
chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu);
coc_chan = event->connect.chan;
return 0;
...
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
console_printf("LE CoC disconnected, chan: %p\n",
event->disconnect.chan);
return 0;
...
default:
return 0;...
}{...}
}{ ... }
static void
blecent_l2cap_coc_mem_init(void)
{
int rc;
rc = os_mempool_init(&sdu_coc_mbuf_mempool, COC_BUF_COUNT, MTU, sdu_coc_mem,
"coc_sdu_pool");
assert(rc == 0);
rc = os_mbuf_pool_init(&sdu_os_mbuf_pool, &sdu_coc_mbuf_mempool, MTU,
COC_BUF_COUNT);
assert(rc == 0);
}{ ... }
#endif/* ... */
/* ... */
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) < 1
static void
blecent_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);
/* ... */
}{...}
/* ... */#endif
/* ... */
static void
blecent_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,
blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error initiating GAP discovery procedure; rc=%d\n",
rc);
}{...}
}{ ... }
/* ... */
#if CONFIG_EXAMPLE_EXTENDED_ADV
static int
ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
{
int offset = 0;
int ad_struct_len = 0;
if (disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
return 0;
}{...}
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&peer_addr[5], &peer_addr[4], &peer_addr[3],
&peer_addr[2], &peer_addr[1], &peer_addr[0]);
if (memcmp(peer_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
return 0;
}{...}
}{...}
/* ... */
do {
ad_struct_len = disc->data[offset];
if (!ad_struct_len) {
break;
}{...}
if (disc->data[offset] == 0x03 && disc->data[offset + 1] == 0x03) {
if ( disc->data[offset + 2] == 0x18 && disc->data[offset + 3] == 0x12 ) {
return 1;
}{...}
}{...}
offset += ad_struct_len + 1;
}{...} while ( offset < disc->length_data );
return 0;
}{...}
/* ... */#else
static int
blecent_should_connect(const struct ble_gap_disc_desc *disc)
{
struct ble_hs_adv_fields fields;
int rc;
int i;
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;
}{...}
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
MODLOG_DFLT(INFO, "Peer address from menuconfig:%s", CONFIG_EXAMPLE_PEER_ADDR);
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&peer_addr[5], &peer_addr[4], &peer_addr[3],
&peer_addr[2], &peer_addr[1], &peer_addr[0]);
if (memcmp(peer_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
return 0;
}{...}
}{...}
/* ... */
for (i = 0; i < fields.num_uuids16; i++) {
if (ble_uuid_u16(&fields.uuids16[i].u) == 0x1812) {
return 1;
}{...}
}{...}
return 0;
}{ ... }
/* ... */#endif
/* ... */
static void
blecent_connect_if_interesting(void *disc)
{
uint8_t own_addr_type;
int rc;
ble_addr_t *addr;
#if CONFIG_EXAMPLE_EXTENDED_ADV
if (!ext_blecent_should_connect((struct ble_gap_ext_disc_desc *)disc)) {
return;
}{...}
#else/* ... */
if (!blecent_should_connect((struct ble_gap_disc_desc *)disc)) {
return;
}{...}
#endif/* ... */
#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;
}{...}
/* ... */
#if CONFIG_EXAMPLE_EXTENDED_ADV
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
#else
addr = &((struct ble_gap_disc_desc *)disc)->addr;
#endif
rc = ble_gap_connect(own_addr_type, addr, 30000, NULL,
blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
"addr=%s; rc=%d\n",
addr->type, addr_str(addr->val), rc);
return;
}{...}
}{ ... }
/* ... */
static int
blecent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
int rc;
peer_disc_fn *disc_cb = NULL;
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);
blecent_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);
print_conn_desc(&desc);
MODLOG_DFLT(INFO, "\n");
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) >= 1
conn_handle_coc = event->link_estab.conn_handle;
disc_cb = blecent_l2cap_coc_on_disc_complete;/* ... */
#else
disc_cb = blecent_on_disc_complete;
#endif
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,
disc_cb, 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);
blecent_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");
peer_delete(event->disconnect.conn.conn_handle);
blecent_scan();
return 0;
...
case BLE_GAP_EVENT_DISC_COMPLETE:
MODLOG_DFLT(INFO, "discovery complete; reason=%d\n",
event->disc_complete.reason);
return 0;
#if CONFIG_EXAMPLE_EXTENDED_ADV...
case BLE_GAP_EVENT_EXT_DISC:
ext_print_adv_report(&event->disc);
blecent_connect_if_interesting(&event->disc);
return 0;/* ... */
#endif
default:
return 0;...
}{...}
}{ ... }
void
ble_coc_cent_task(void *pvParameters)
{
while (1) {
if (coc_chan) {
for (int i = 0; i < 5; i++) {
blecent_l2cap_coc_send_data(coc_chan);
vTaskDelay(500 / portTICK_PERIOD_MS);
}{...}
coc_chan = NULL;
}{...}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}{...}
}{ ... }
static void
blecent_on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}{ ... }
static void
blecent_on_sync(void)
{
int rc;
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
blecent_scan();
}{ ... }
void blecent_host_task(void *param)
{
ESP_LOGI(tag, "BLE Host Task Started");
nimble_port_run();
nimble_port_freertos_deinit();
}{ ... }
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();
assert(ret == 0);
ble_hs_cfg.reset_cb = blecent_on_reset;
ble_hs_cfg.sync_cb = blecent_on_sync;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) >= 1
blecent_l2cap_coc_mem_init();
xTaskCreate(ble_coc_cent_task, "ble_coc_cent_task", 4096, NULL, 10, NULL);/* ... */
#endif
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
rc = ble_svc_gap_device_name_set("blecent-l2coc");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(blecent_host_task);
}{ ... }