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
70
71
72
73
74
79
80
81
82
83
84
85
86
87
88
92
93
94
95
96
97
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
127
128
129
130
131
137
138
139
140
141
142
143
144
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
167
168
169
170
171
172
173
177
178
179
180
181
182
188
189
190
191
192
193
194
195
201
202
203
204
205
206
207
208
209
210
211
212
216
217
218
219
220
221
222
228
229
230
231
232
233
234
235
239
240
241
242
243
244
245
246
247
248
249
250
251
252
256
257
258
259
260
261
262
266
267
268
269
270
271
272
273
274
275
276
280
281
282
283
284
285
286
287
288
289
294
295
296
297
298
299
304
305
306
307
308
309
310
311
315
316
317
318
319
320
321
322
323
327
328
329
332
333
334
335
336
337
338
339
340
345
346
347
348
349
350
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
389
390
391
392
397
398
399
400
401
402
403
404
407
408
409
410
411
418
419
423
424
425
426
429
430
431
432
435
436
437
438
439
440
441
442
443
444
448
449
452
453
454
458
459
460
461
462
463
464
465
466
467
468
472
473
474
479
480
481
482
483
484
485
486
487
488
489
490
493
494
495
500
501
505
506
509
510
511
514
515
516
517
520
521
522
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
551
552
553
556
557
558
559
560
564
565
569
570
573
574
575
578
583
584
585
586
587
588
593
594
595
596
597
598
599
600
601
602
606
610
611
612
619
620
621
622
626
627
630
631
632
633
634
635
636
637
638
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
688
689
690
691
692
693
694
695
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
723
724
725
726
727
728
729
742
743
744
745
746
747
748
749
750
751
758
759
767
773
774
775
776
777
778
779
780
781
782
783
784
785
786
792
793
794
795
796
797
802
803
804
805
806
807
808
809
810
820
821
822
823
824
825
826
827
828
829
830
831
832
833
835
836
837
844
845
849
850
851
852
853
854
855
858
859
860
861
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
905
909
910
911
912
913
914
915
916
920
921
922
926
927
928
929
930
934
935
936
938
939
940
941
942
946
947
948
949
950
951
952
953
954
955
956
957
958
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
990
991
992
993
994
995
996
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1018
1019
1020
1021
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1054
1055
/* ... */
#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 "blecent.h"9 includes
#if CONFIG_EXAMPLE_USE_CI_ADDRESS
#ifdef CONFIG_IDF_TARGET_ESP32
#define TEST_CI_ADDRESS_CHIP_OFFSET (0)
#elif CONFIG_IDF_TARGET_ESP32C2
#define TEST_CI_ADDRESS_CHIP_OFFSET (1)
#elif CONFIG_IDF_TARGET_ESP32C3
#define TEST_CI_ADDRESS_CHIP_OFFSET (2)
#elif CONFIG_IDF_TARGET_ESP32C6
#define TEST_CI_ADDRESS_CHIP_OFFSET (3)
#elif CONFIG_IDF_TARGET_ESP32C5
#define TEST_CI_ADDRESS_CHIP_OFFSET (4)
#elif CONFIG_IDF_TARGET_ESP32H2
#define TEST_CI_ADDRESS_CHIP_OFFSET (5)
#elif CONFIG_IDF_TARGET_ESP32P4
#define TEST_CI_ADDRESS_CHIP_OFFSET (6)
#elif CONFIG_IDF_TARGET_ESP32S3
#define TEST_CI_ADDRESS_CHIP_OFFSET (7)
#elif CONFIG_IDF_TARGET_ESP32C61
#define TEST_CI_ADDRESS_CHIP_OFFSET (8)
#endif/* ... */
#endif
static const ble_uuid_t * remote_svc_uuid =
BLE_UUID128_DECLARE(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
static const ble_uuid_t * remote_chr_uuid =
BLE_UUID128_DECLARE(0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33);
static const char *tag = "NimBLE_BLE_CENT";
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM) > 0
static uint16_t cids[MYNEWT_VAL(BLE_EATT_CHAN_NUM)];
static uint16_t bearers;/* ... */
#endif
void ble_store_config_init(void);
/* ... */
static int
blecent_on_custom_read(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
MODLOG_DFLT(INFO,
"Read complete for the subscribable characteristic; "
"status=%d conn_handle=%d", error->status, conn_handle);
if (error->status == 0) {
MODLOG_DFLT(INFO, " attr_handle=%d value=", attr->handle);
print_mbuf(attr->om);
}{...}
MODLOG_DFLT(INFO, "\n");
return 0;
}{ ... }
/* ... */
static int
blecent_on_custom_write(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
const struct peer_chr *chr;
const struct peer *peer;
int rc;
MODLOG_DFLT(INFO,
"Write to the custom subscribable characteristic complete; "
"status=%d conn_handle=%d attr_handle=%d\n",
error->status, conn_handle, attr->handle);
peer = peer_find(conn_handle);
chr = peer_chr_find_uuid(peer,
remote_svc_uuid,
remote_chr_uuid);
if (chr == NULL) {
MODLOG_DFLT(ERROR,
"Error: Peer doesn't have the custom subscribable characteristic\n");
goto err;
}{...}
rc = ble_gattc_read(conn_handle, chr->chr.val_handle,
blecent_on_custom_read, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR,
"Error: Failed to read the custom subscribable characteristic; "
"rc=%d\n", rc);
goto err;
}{...}
return 0;
err:
return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static int
blecent_on_custom_subscribe(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
const struct peer_chr *chr;
uint8_t value;
int rc;
const struct peer *peer;
MODLOG_DFLT(INFO,
"Subscribe to the custom subscribable characteristic complete; "
"status=%d conn_handle=%d", error->status, conn_handle);
if (error->status == 0) {
MODLOG_DFLT(INFO, " attr_handle=%d value=", attr->handle);
print_mbuf(attr->om);
}{...}
MODLOG_DFLT(INFO, "\n");
peer = peer_find(conn_handle);
chr = peer_chr_find_uuid(peer,
remote_svc_uuid,
remote_chr_uuid);
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't have the subscribable characteristic\n");
goto err;
}{...}
value = 0x19;
rc = ble_gattc_write_flat(conn_handle, chr->chr.val_handle,
&value, sizeof(value), blecent_on_custom_write, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR,
"Error: Failed to write to the subscribable characteristic; "
"rc=%d\n", rc);
goto err;
}{...}
return 0;
err:
return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static void
blecent_custom_gatt_operations(const struct peer* peer)
{
const struct peer_dsc *dsc;
int rc;
uint8_t value[2];
dsc = peer_dsc_find_uuid(peer,
remote_svc_uuid,
remote_chr_uuid,
BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16));
if (dsc == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer lacks a CCCD for the subscribable characteristic\n");
goto err;
}{...}
value[0] = 1;
value[1] = 0;
rc = ble_gattc_write_flat(peer->conn_handle, dsc->dsc.handle,
value, sizeof(value), blecent_on_custom_subscribe, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR,
"Error: Failed to subscribe to the subscribable characteristic; "
"rc=%d\n", rc);
goto err;
}{...}
return;
err:
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static int
blecent_on_subscribe(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
struct peer *peer;
MODLOG_DFLT(INFO, "Subscribe complete; status=%d conn_handle=%d "
"attr_handle=%d\n",
error->status, conn_handle, attr->handle);
peer = peer_find(conn_handle);
if (peer == NULL) {
MODLOG_DFLT(ERROR, "Error in finding peer, aborting...");
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{...}
blecent_custom_gatt_operations(peer);
return 0;
}{ ... }
/* ... */
static int
blecent_on_write(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
MODLOG_DFLT(INFO,
"Write complete; status=%d conn_handle=%d attr_handle=%d\n",
error->status, conn_handle, attr->handle);
/* ... */
const struct peer_dsc *dsc;
uint8_t value[2];
int rc;
const struct peer *peer = peer_find(conn_handle);
dsc = peer_dsc_find_uuid(peer,
BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
BLE_UUID16_DECLARE(BLECENT_CHR_UNR_ALERT_STAT_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(conn_handle, dsc->dsc.handle,
value, sizeof value, blecent_on_subscribe, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to subscribe to characteristic; "
"rc=%d\n", rc);
goto err;
}{...}
return 0;
err:
return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static int
blecent_on_read(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
MODLOG_DFLT(INFO, "Read complete; status=%d conn_handle=%d", error->status,
conn_handle);
if (error->status == 0) {
MODLOG_DFLT(INFO, " attr_handle=%d value=", attr->handle);
print_mbuf(attr->om);
}{...}
MODLOG_DFLT(INFO, "\n");
/* ... */
const struct peer_chr *chr;
uint8_t value[2];
int rc;
const struct peer *peer = peer_find(conn_handle);
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
BLE_UUID16_DECLARE(BLECENT_CHR_ALERT_NOT_CTRL_PT));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Alert "
"Notification Control Point characteristic\n");
goto err;
}{...}
value[0] = 99;
value[1] = 100;
rc = ble_gattc_write_flat(conn_handle, chr->chr.val_handle,
value, sizeof value, blecent_on_write, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to write characteristic; rc=%d\n",
rc);
goto err;
}{...}
return 0;
err:
return ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static void
blecent_read_write_subscribe(const struct peer *peer)
{
const struct peer_chr *chr;
int rc;
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(BLECENT_SVC_ALERT_UUID),
BLE_UUID16_DECLARE(BLECENT_CHR_SUP_NEW_ALERT_CAT_UUID));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Supported New "
"Alert Category characteristic\n");
goto err;
}{...}
rc = ble_gattc_read(peer->conn_handle, chr->chr.val_handle,
blecent_on_read, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to read characteristic; rc=%d\n",
rc);
goto err;
}{...}
return;
err:
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
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);
/* ... */
blecent_read_write_subscribe(peer);
}{ ... }
/* ... */
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 CONFIG_EXAMPLE_USE_CI_ADDRESS
uint32_t *addr_offset;
#endif
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)) {
#if !CONFIG_EXAMPLE_USE_CI_ADDRESS
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]);/* ... */
#else
addr_offset = (uint32_t *)&peer_addr[1];
*addr_offset = atoi(CONFIG_EXAMPLE_PEER_ADDR);
peer_addr[5] = 0xC3;
peer_addr[0] = TEST_CI_ADDRESS_CHIP_OFFSET;/* ... */
#endif
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] == 0x11 ) {
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 CONFIG_EXAMPLE_USE_CI_ADDRESS
uint32_t *addr_offset;
#endif
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)) {
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
#if !CONFIG_EXAMPLE_USE_CI_ADDRESS
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]);/* ... */
#else
addr_offset = (uint32_t *)&peer_addr[1];
*addr_offset = atoi(CONFIG_EXAMPLE_PEER_ADDR);
peer_addr[5] = 0xC3;
peer_addr[0] = TEST_CI_ADDRESS_CHIP_OFFSET;/* ... */
#endif
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) == BLECENT_SVC_ALERT_UUID) {
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;
}{...}
}{ ... }
#if MYNEWT_VAL(BLE_POWER_CONTROL)
static void blecent_power_control(uint16_t conn_handle)
{
int rc;
rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 );
assert (rc == 0);
rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x01, 0x01);
assert (rc == 0);
rc = ble_gap_set_path_loss_reporting_param(conn_handle, 60, 10, 30, 10, 2 );
assert (rc == 0);
rc = ble_gap_set_path_loss_reporting_enable(conn_handle, 0x01);
assert (rc == 0);
}{...}
/* ... */#endif
/* ... */
static int
blecent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
#if MYNEWT_VAL(BLE_HCI_VS)
#if MYNEWT_VAL(BLE_POWER_CONTROL)
struct ble_gap_set_auto_pcl_params params;
#endif/* ... */
#endif
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);
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");
rc = peer_add(event->link_estab.conn_handle);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to add peer; rc=%d\n", rc);
return 0;
}{...}
#if MYNEWT_VAL(BLE_POWER_CONTROL)
blecent_power_control(event->link_estab.conn_handle);
#endif
#if MYNEWT_VAL(BLE_HCI_VS)
#if MYNEWT_VAL(BLE_POWER_CONTROL)
memset(¶ms, 0x0, sizeof(struct ble_gap_set_auto_pcl_params));
params.conn_handle = event->link_estab.conn_handle;
rc = ble_gap_set_auto_pcl_param(¶ms);
if (rc != 0) {
MODLOG_DFLT(INFO, "Failed to send VSC %x \n", rc);
return 0;
}{...}
else {
MODLOG_DFLT(INFO, "Successfully issued VSC , rc = %d \n", rc);
}{...}
#endif/* ... */
/* ... */#endif
#if CONFIG_EXAMPLE_ENCRYPTION
/* ... */
rc = ble_gap_security_initiate(event->link_estab.conn_handle);
if (rc != 0) {
MODLOG_DFLT(INFO, "Security could not be initiated, rc = %d\n", rc);
return ble_gap_terminate(event->link_estab.conn_handle,
BLE_ERR_REM_USER_CONN_TERM);
}{...} else {
MODLOG_DFLT(INFO, "Connection secured\n");
}{...}
#else/* ... */
rc = peer_disc_all(event->link_estab.conn_handle,
blecent_on_disc_complete, NULL);
if(rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}{...}
#endif/* ... */
}{...} 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);
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM) > 0
bearers = 0;
for (int i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_NUM); i++) {
cids[i] = 0;
}{...}
#endif/* ... */
blecent_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_ENC_CHANGE:
MODLOG_DFLT(INFO, "encryption change event; status=%d ",
event->enc_change.status);
rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
assert(rc == 0);
print_conn_desc(&desc);
#if !MYNEWT_VAL(BLE_EATT_CHAN_NUM)
#if CONFIG_EXAMPLE_ENCRYPTION
rc = peer_disc_all(event->enc_change.conn_handle,
blecent_on_disc_complete, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}{...}
#endif/* ... */
/* ... */#endif
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;
...
case BLE_GAP_EVENT_REPEAT_PAIRING:
/* ... */
rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
assert(rc == 0);
ble_store_util_delete_peer(&desc.peer_id_addr);
/* ... */
return BLE_GAP_REPEAT_PAIRING_RETRY;
#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
#if MYNEWT_VAL(BLE_POWER_CONTROL)
case BLE_GAP_EVENT_TRANSMIT_POWER:
MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d "
"phy=%d power_level=%d power_level_flag=%d delta=%d",
event->transmit_power.status,
event->transmit_power.conn_handle,
event->transmit_power.reason,
event->transmit_power.phy,
event->transmit_power.transmit_power_level,
event->transmit_power.transmit_power_level_flag,
event->transmit_power.delta);
return 0;
...
case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d "
"zone_entered =%d",
event->pathloss_threshold.conn_handle,
event->pathloss_threshold.current_path_loss,
event->pathloss_threshold.zone_entered);
return 0;/* ... */
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM) > 0
case BLE_GAP_EVENT_EATT:
int i;
MODLOG_DFLT(INFO, "EATT %s : conn_handle=%d cid=%d",
event->eatt.status ? "disconnected" : "connected",
event->eatt.conn_handle,
event->eatt.cid);
if (event->eatt.status) {
for (i = 0; i < bearers; i++) {
if (cids[i] == event->eatt.cid) {
break;
}{...}
}{...}
while (i < (bearers - 1)) {
cids[i] = cids[i + 1];
i += 1;
}{...}
cids[i] = 0;
return 0;
}{...}
cids[bearers] = event->eatt.cid;
bearers += 1;
if (bearers != MYNEWT_VAL(BLE_EATT_CHAN_NUM)) {
return 0;
}{...}
rc = ble_att_set_default_bearer_using_cid(event->eatt.conn_handle, cids[0]);
if (rc != 0) {
MODLOG_DFLT(INFO, "Cannot set default EATT bearer, rc = %d\n", rc);
return rc;
}{...}
rc = peer_disc_all(event->eatt.conn_handle,
blecent_on_disc_complete, NULL);
if(rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}{...}
#endif/* ... */
return 0;
default:
return 0;...
}{...}
}{ ... }
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);
#if !CONFIG_EXAMPLE_INIT_DEINIT_LOOP
blecent_scan();/* ... */
#endif
}{ ... }
void blecent_host_task(void *param)
{
ESP_LOGI(tag, "BLE Host Task Started");
nimble_port_run();
nimble_port_freertos_deinit();
}{ ... }
#if CONFIG_EXAMPLE_INIT_DEINIT_LOOP
static void stack_init_deinit(void)
{
int rc;
while(1) {
vTaskDelay(1000);
ESP_LOGI(tag, "Deinit host");
rc = nimble_port_stop();
if (rc == 0) {
nimble_port_deinit();
}{...} else {
ESP_LOGI(tag, "Nimble port stop failed, rc = %d", rc);
break;
}{...}
vTaskDelay(1000);
ESP_LOGI(tag, "Init host");
rc = nimble_port_init();
if (rc != ESP_OK) {
ESP_LOGI(tag, "Failed to init nimble %d ", rc);
break;
}{...}
nimble_port_freertos_init(blecent_host_task);
ESP_LOGI(tag, "Waiting for 1 second");
}{...}
}{...}
/* ... */#endif
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) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}{...}
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;
rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
assert(rc == 0);
rc = ble_svc_gap_device_name_set("nimble-blecent");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(blecent_host_task);
#if CONFIG_EXAMPLE_INIT_DEINIT_LOOP
stack_init_deinit();
#endif
#if MYNEWT_VAL(BLE_EATT_CHAN_NUM) > 0
bearers = 0;
for (int i = 0; i < MYNEWT_VAL(BLE_EATT_CHAN_NUM); i++) {
cids[i] = 0;
}{...}
#endif/* ... */
}{ ... }