Select one of the symbols to view example projects that use it.
 
Outline
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "ble_compatibility_test.h"
#include "esp_gatt_common_api.h"
#define DEBUG_ON
#define EXAMPLE_DEBUG
#define EXAMPLE_DEBUG
#define EXAMPLE_TAG
#define PROFILE_NUM
#define PROFILE_APP_IDX
#define ESP_APP_ID
#define SAMPLE_DEVICE_NAME
#define SVC_INST_ID
#define GATTS_EXAMPLE_CHAR_VAL_LEN_MAX
#define LONG_CHAR_VAL_LEN
#define SHORT_CHAR_VAL_LEN
#define GATTS_NOTIFY_FIRST_PACKET_LEN_MAX
#define PREPARE_BUF_MAX_SIZE
#define CHAR_DECLARATION_SIZE
#define ADV_CONFIG_FLAG
#define SCAN_RSP_CONFIG_FLAG
adv_config_done
gatt_db_handle_table
prepare_type_env_t
prepare_write_env
service_uuid
adv_data
scan_rsp_data
adv_params
gatts_profile_inst
heart_rate_profile_tab
GATTS_SERVICE_UUID_TEST
CHAR_1_SHORT_WR
CHAR_2_LONG_WR
CHAR_3_SHORT_NOTIFY
primary_service_uuid
character_declaration_uuid
character_client_config_uuid
character_user_description
char_prop_notify
char_prop_read_write
char1_name
char2_name
char3_name
char_ccc
char_value
gatt_db
show_bonded_devices()
remove_all_bonded_devices()
gap_event_handler(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *)
example_prepare_write_event_env(esp_gatt_if_t, prepare_type_env_t *, esp_ble_gatts_cb_param_t *)
long_write
example_exec_write_event_env(prepare_type_env_t *, esp_ble_gatts_cb_param_t *)
gatts_profile_event_handler(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *)
gatts_event_handler(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesble_compatibility_test samplemain/ble_compatibility_test.c
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ /******************************************************************************** * * This file is for gatt server. It can send adv data, and get connected by client. * *********************************************************************************//* ... */ #include <inttypes.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" #include "esp_gap_ble_api.h" #include "esp_gatts_api.h" #include "esp_bt_main.h" #include "esp_bt_device.h" #include "ble_compatibility_test.h" #include "esp_gatt_common_api.h"14 includes #define DEBUG_ON 0 #if DEBUG_ON #define EXAMPLE_DEBUG ESP_LOGI #else #define EXAMPLE_DEBUG( tag, format, ... ) #endif #define EXAMPLE_TAG "BLE_COMP" #define PROFILE_NUM 1 #define PROFILE_APP_IDX 0 #define ESP_APP_ID 0x55 #define SAMPLE_DEVICE_NAME "BLE_COMP_TEST" #define SVC_INST_ID 0 /* The max length of characteristic value. When the gatt client write or prepare write, * the data length must be less than GATTS_EXAMPLE_CHAR_VAL_LEN_MAX. *//* ... */ #define GATTS_EXAMPLE_CHAR_VAL_LEN_MAX 500 #define LONG_CHAR_VAL_LEN 500 #define SHORT_CHAR_VAL_LEN 10 #define GATTS_NOTIFY_FIRST_PACKET_LEN_MAX 20 #define PREPARE_BUF_MAX_SIZE 1024 #define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) #define ADV_CONFIG_FLAG (1 << 0) #define SCAN_RSP_CONFIG_FLAG (1 << 1)14 defines static uint8_t adv_config_done = 0; uint16_t gatt_db_handle_table[HRS_IDX_NB]; typedef struct { uint8_t *prepare_buf; int prepare_len; }{ ... } prepare_type_env_t; static prepare_type_env_t prepare_write_env; //#define CONFIG_SET_RAW_ADV_DATA #ifdef CONFIG_SET_RAW_ADV_DATA static uint8_t raw_adv_data[] = { /* Flags */ 0x02, ESP_BLE_AD_TYPE_FLAG, 0x06, /* TX Power */ 0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xeb, /* Service UUID */ 0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00, /* Device Name */ 0x0E, ESP_BLE_AD_TYPE_NAME_CMPL, 'B', 'L', 'E', '_', 'C', 'O', 'M', 'P', '_', 'T', 'E', 'S', 'T' }{...}; static uint8_t raw_scan_rsp_data[] = { /* Flags */ 0x02, ESP_BLE_AD_TYPE_FLAG, 0x06, /* TX Power */ 0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xeb, /* Service UUID */ 0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00 }{...}; /* ... */ #else static uint8_t service_uuid[16] = { /* LSB <--------------------------------------------------------------------------------> MSB */ //first uuid, 16bit, [12],[13] is the value 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, }{...}; /* The length of adv data must be less than 31 bytes */ static esp_ble_adv_data_t adv_data = { .set_scan_rsp = false, .include_name = true, .include_txpower = true, .min_interval = 0x20, .max_interval = 0x40, .appearance = 0x00, .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN, .p_manufacturer_data = NULL, //test_manufacturer, .service_data_len = 0, .p_service_data = NULL, .service_uuid_len = sizeof(service_uuid), .p_service_uuid = service_uuid, .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT), }{...}; // scan response data static esp_ble_adv_data_t scan_rsp_data = { .set_scan_rsp = true, .include_name = true, .include_txpower = true, .min_interval = 0x20, .max_interval = 0x40, .appearance = 0x00, .manufacturer_len = 0, //TEST_MANUFACTURER_DATA_LEN, .p_manufacturer_data = NULL, //&test_manufacturer[0], .service_data_len = 0, .p_service_data = NULL, .service_uuid_len = 16, .p_service_uuid = service_uuid, .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT), }{...}; /* ... */#endif /* CONFIG_SET_RAW_ADV_DATA */ static esp_ble_adv_params_t adv_params = { .adv_int_min = 0x40, .adv_int_max = 0x40, .adv_type = ADV_TYPE_IND, .own_addr_type = BLE_ADDR_TYPE_PUBLIC, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, }{...}; struct gatts_profile_inst { esp_gatts_cb_t gatts_cb; uint16_t gatts_if; uint16_t app_id; uint16_t conn_id; uint16_t service_handle; esp_gatt_srvc_id_t service_id; uint16_t char_handle; esp_bt_uuid_t char_uuid; esp_gatt_perm_t perm; esp_gatt_char_prop_t property; uint16_t descr_handle; esp_bt_uuid_t descr_uuid; }{ ... }; 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); /* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */ static struct gatts_profile_inst heart_rate_profile_tab[PROFILE_NUM] = { [PROFILE_APP_IDX] = { .gatts_cb = gatts_profile_event_handler, .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ }{...}, }{...}; /* Service */ static const uint16_t GATTS_SERVICE_UUID_TEST = 0x00FF; static const uint16_t CHAR_1_SHORT_WR = 0xFF01; static const uint16_t CHAR_2_LONG_WR = 0xFF02; static const uint16_t CHAR_3_SHORT_NOTIFY = 0xFF03; static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE; static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE; static const uint16_t character_client_config_uuid = ESP_GATT_UUID_CHAR_CLIENT_CONFIG; static const uint16_t character_user_description = ESP_GATT_UUID_CHAR_DESCRIPTION; static const uint8_t char_prop_notify = ESP_GATT_CHAR_PROP_BIT_NOTIFY; static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_READ; static const uint8_t char1_name[] = "Char_1_Short_WR"; static const uint8_t char2_name[] = "Char_2_Long_WR"; static const uint8_t char3_name[] = "Char_3_Short_Notify"; static const uint8_t char_ccc[2] = {0x00, 0x00}; static const uint8_t char_value[4] = {0x11, 0x22, 0x33, 0x44}; /* Full Database Description - Used to add attributes into the database */ static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] = { // Service Declaration [IDX_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, sizeof(uint16_t), sizeof(GATTS_SERVICE_UUID_TEST), (uint8_t *)&GATTS_SERVICE_UUID_TEST}{...}}{...}, /* Characteristic Declaration */ [IDX_CHAR_A] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}{...}}{...}, /* Characteristic Value */ [IDX_CHAR_VAL_A] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&CHAR_1_SHORT_WR, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE | ESP_GATT_PERM_READ_ENC_MITM, SHORT_CHAR_VAL_LEN, sizeof(char_value), (uint8_t *)char_value}{...}}{...}, /* Characteristic User Descriptor */ [IDX_CHAR_CFG_A] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_user_description, ESP_GATT_PERM_READ, sizeof(char1_name), sizeof(char1_name), (uint8_t *)char1_name}{...}}{...}, /* Characteristic Declaration */ [IDX_CHAR_B] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}{...}}{...}, /* Characteristic Value */ [IDX_CHAR_VAL_B] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&CHAR_2_LONG_WR, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, LONG_CHAR_VAL_LEN, sizeof(char_value), (uint8_t *)char_value}{...}}{...}, /* Characteristic User Descriptor */ [IDX_CHAR_CFG_B] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_user_description, ESP_GATT_PERM_READ, sizeof(char2_name), sizeof(char2_name), (uint8_t *)char2_name}{...}}{...}, /* Characteristic Declaration */ [IDX_CHAR_C] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_notify}{...}}{...}, /* Characteristic Value */ [IDX_CHAR_VAL_C] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&CHAR_3_SHORT_NOTIFY, 0, LONG_CHAR_VAL_LEN, sizeof(char_value), (uint8_t *)char_value}{...}}{...}, /* Characteristic User Descriptor */ [IDX_CHAR_CFG_C] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_user_description, ESP_GATT_PERM_READ, sizeof(char3_name), sizeof(char3_name), (uint8_t *)char3_name}{...}}{...}, /* Characteristic Client Configuration Descriptor */ [IDX_CHAR_CFG_C_2] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(char_ccc), (uint8_t *)char_ccc}{...}}{...}, }{...}; static void show_bonded_devices(void) { int dev_num = esp_ble_get_bond_device_num(); if (dev_num == 0) { ESP_LOGI(EXAMPLE_TAG, "Bonded devices number zero\n"); return; }{...} esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); if (!dev_list) { ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n"); return; }{...} esp_ble_get_bond_device_list(&dev_num, dev_list); EXAMPLE_DEBUG(EXAMPLE_TAG, "Bonded devices number : %d\n", dev_num); EXAMPLE_DEBUG(EXAMPLE_TAG, "Bonded devices list : %d\n", dev_num); for (int i = 0; i < dev_num; i++) { #if DEBUG_ON ESP_LOG_BUFFER_HEX(EXAMPLE_TAG, (void *)dev_list[i].bd_addr, sizeof(esp_bd_addr_t)); #endif }{...} free(dev_list); }{ ... } static void __attribute__((unused)) remove_all_bonded_devices(void) { int dev_num = esp_ble_get_bond_device_num(); if (dev_num == 0) { ESP_LOGI(EXAMPLE_TAG, "Bonded devices number zero\n"); return; }{...} esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); if (!dev_list) { ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n"); return; }{...} esp_ble_get_bond_device_list(&dev_num, dev_list); for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); }{...} free(dev_list); }{ ... } static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { switch (event) { #ifdef CONFIG_SET_RAW_ADV_DATA case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: adv_config_done &= (~ADV_CONFIG_FLAG); if (adv_config_done == 0){ esp_ble_gap_start_advertising(&adv_params); }{...} break;... case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT: adv_config_done &= (~SCAN_RSP_CONFIG_FLAG); if (adv_config_done == 0){ esp_ble_gap_start_advertising(&adv_params); }{...} break;/* ... */ #else case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: adv_config_done &= (~ADV_CONFIG_FLAG); if (adv_config_done == 0){ esp_ble_gap_start_advertising(&adv_params); }{...} break;... case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: adv_config_done &= (~SCAN_RSP_CONFIG_FLAG); if (adv_config_done == 0){ esp_ble_gap_start_advertising(&adv_params); }{...} break;/* ... */ #endif case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: /* advertising start complete event to indicate advertising start successfully or failed */ if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(EXAMPLE_TAG, "advertising start failed"); }{...}else{ ESP_LOGI(EXAMPLE_TAG, "(0) ***** advertising start successfully ***** "); } break;... case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(EXAMPLE_TAG, "Advertising stop failed"); }{...} else { ESP_LOGI(EXAMPLE_TAG, "Stop adv successfully"); }{...} break;... case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: EXAMPLE_DEBUG(EXAMPLE_TAG, "update connection params status = %d, conn_int = %d, latency = %d, timeout = %d", param->update_conn_params.status, param->update_conn_params.conn_int, param->update_conn_params.latency, param->update_conn_params.timeout); break;... case ESP_GAP_BLE_PASSKEY_REQ_EVT: /* passkey request event */ EXAMPLE_DEBUG(EXAMPLE_TAG, "ESP_GAP_BLE_PASSKEY_REQ_EVT"); //esp_ble_passkey_reply(heart_rate_profile_tab[HEART_PROFILE_APP_IDX].remote_bda, true, 0x00); break; ... case ESP_GAP_BLE_NC_REQ_EVT: /* The app will receive this event when the IO has DisplayYesNO capability and the peer device IO also has DisplayYesNo capability. show the passkey number to the user to confirm it with the number displayed by peer device. *//* ... */ ESP_LOGI(EXAMPLE_TAG, "ESP_GAP_BLE_NC_REQ_EVT, the passkey Notify number:%" PRIu32, param->ble_security.key_notif.passkey); break;... case ESP_GAP_BLE_SEC_REQ_EVT: /* send the positive(true) security response to the peer device to accept the security request. If not accept the security request, should send the security response with negative(false) accept value*//* ... */ esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); break;... case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: ///the app will receive this evt when the IO has Output capability and the peer device IO has Input capability. ///show the passkey number to the user to input it in the peer device. ESP_LOGI(EXAMPLE_TAG, "The passkey notify number:%06" PRIu32, param->ble_security.key_notif.passkey); break;... case ESP_GAP_BLE_KEY_EVT: //shows the ble key info share with peer device to the user. EXAMPLE_DEBUG(EXAMPLE_TAG, "key type = %s", esp_key_type_to_str(param->ble_security.ble_key.key_type)); break;... case ESP_GAP_BLE_AUTH_CMPL_EVT: { esp_bd_addr_t bd_addr; memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); EXAMPLE_DEBUG(EXAMPLE_TAG, "remote BD_ADDR: %08x%04x",\ (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3], (bd_addr[4] << 8) + bd_addr[5]); EXAMPLE_DEBUG(EXAMPLE_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type); if (param->ble_security.auth_cmpl.success){ ESP_LOGI(EXAMPLE_TAG, "(1) ***** pair status = success ***** "); }{...} else { ESP_LOGI(EXAMPLE_TAG, "***** pair status = fail, reason = 0x%x *****", param->ble_security.auth_cmpl.fail_reason); }{...} show_bonded_devices(); break; }{...} ... case ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT: { EXAMPLE_DEBUG(EXAMPLE_TAG, "ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT status = %d", param->remove_bond_dev_cmpl.status); #if DEBUG_ON ESP_LOG_BUFFER_HEX(EXAMPLE_TAG, (void *)param->remove_bond_dev_cmpl.bd_addr, sizeof(esp_bd_addr_t)); #endif EXAMPLE_DEBUG(EXAMPLE_TAG, "------------------------------------"); break; }{...} ... default: break;... }{...} }{ ... } void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param) { EXAMPLE_DEBUG(EXAMPLE_TAG, "prepare write, handle = %d, value len = %d", param->write.handle, param->write.len); esp_gatt_status_t status = ESP_GATT_OK; if (param->write.offset > PREPARE_BUF_MAX_SIZE) { status = ESP_GATT_INVALID_OFFSET; }{...} else if ((param->write.offset + param->write.len) > PREPARE_BUF_MAX_SIZE) { status = ESP_GATT_INVALID_ATTR_LEN; }{...} if (status == ESP_GATT_OK && prepare_write_env->prepare_buf == NULL) { prepare_write_env->prepare_buf = (uint8_t *)malloc(PREPARE_BUF_MAX_SIZE * sizeof(uint8_t)); prepare_write_env->prepare_len = 0; if (prepare_write_env->prepare_buf == NULL) { ESP_LOGE(EXAMPLE_TAG, "%s, Gatt_server prep no mem", __func__); status = ESP_GATT_NO_RESOURCES; }{...} }{...} /*send response when param->write.need_rsp is true */ if (param->write.need_rsp){ esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t)); if (gatt_rsp != NULL){ gatt_rsp->attr_value.len = param->write.len; gatt_rsp->attr_value.handle = param->write.handle; gatt_rsp->attr_value.offset = param->write.offset; gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); if (response_err != ESP_OK){ ESP_LOGE(EXAMPLE_TAG, "Send response error"); }{...} free(gatt_rsp); }{...}else{ ESP_LOGE(EXAMPLE_TAG, "%s, malloc failed, and no resource to send response", __func__); status = ESP_GATT_NO_RESOURCES; } }{...} if (status != ESP_GATT_OK){ return; }{...} memcpy(prepare_write_env->prepare_buf + param->write.offset, param->write.value, param->write.len); prepare_write_env->prepare_len += param->write.len; }{ ... } uint8_t long_write[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param){ if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC && prepare_write_env->prepare_buf){ if(prepare_write_env->prepare_len == 256) { bool long_write_success = true; for(uint16_t i = 0; i < prepare_write_env->prepare_len; i ++) { if(prepare_write_env->prepare_buf[i] != long_write[i%16]) { long_write_success = false; break; }{...} }{...} if(long_write_success) { ESP_LOGI(EXAMPLE_TAG, "(4) ***** long write success ***** "); }{...} }{...} }{...}else{ ESP_LOGI(EXAMPLE_TAG,"ESP_GATT_PREP_WRITE_CANCEL"); } if (prepare_write_env->prepare_buf) { free(prepare_write_env->prepare_buf); prepare_write_env->prepare_buf = NULL; }{...} prepare_write_env->prepare_len = 0; }{ ... } 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) { switch (event) { case ESP_GATTS_REG_EVT:{ esp_err_t set_dev_name_ret = esp_ble_gap_set_device_name(SAMPLE_DEVICE_NAME); if (set_dev_name_ret){ ESP_LOGE(EXAMPLE_TAG, "set device name failed, error code = %x", set_dev_name_ret); }{...} #ifdef CONFIG_SET_RAW_ADV_DATA esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data)); if (raw_adv_ret){ ESP_LOGE(EXAMPLE_TAG, "config raw adv data failed, error code = %x ", raw_adv_ret); }{...} adv_config_done |= ADV_CONFIG_FLAG; esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw(raw_scan_rsp_data, sizeof(raw_scan_rsp_data)); if (raw_scan_ret){ ESP_LOGE(EXAMPLE_TAG, "config raw scan rsp data failed, error code = %x", raw_scan_ret); }{...} adv_config_done |= SCAN_RSP_CONFIG_FLAG;/* ... */ #else //config adv data esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data); if (ret){ ESP_LOGE(EXAMPLE_TAG, "config adv data failed, error code = %x", ret); }{...} adv_config_done |= ADV_CONFIG_FLAG; //config scan response data ret = esp_ble_gap_config_adv_data(&scan_rsp_data); if (ret){ ESP_LOGE(EXAMPLE_TAG, "config scan response data failed, error code = %x", ret); }{...} adv_config_done |= SCAN_RSP_CONFIG_FLAG;/* ... */ #endif esp_err_t create_attr_ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, SVC_INST_ID); if (create_attr_ret){ ESP_LOGE(EXAMPLE_TAG, "create attr table failed, error code = %x", create_attr_ret); }{...} }{...} break;... case ESP_GATTS_READ_EVT: //ESP_LOGE(EXAMPLE_TAG, "ESP_GATTS_READ_EVT, handle=0x%d, offset=%d", param->read.handle, param->read.offset); if(gatt_db_handle_table[IDX_CHAR_VAL_A] == param->read.handle) { ESP_LOGE(EXAMPLE_TAG, "(2) ***** read char1 ***** "); }{...} if(gatt_db_handle_table[IDX_CHAR_VAL_B] == param->read.handle) { ESP_LOGE(EXAMPLE_TAG, "(5) ***** read char2 ***** "); }{...} break;... case ESP_GATTS_WRITE_EVT: if (!param->write.is_prep){ // the data length of gattc write must be less than GATTS_EXAMPLE_CHAR_VAL_LEN_MAX. if (gatt_db_handle_table[IDX_CHAR_CFG_C_2] == param->write.handle && param->write.len == 2){ uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0]; uint8_t notify_data[2]; notify_data[0] = 0xAA; notify_data[1] = 0xBB; if (descr_value == 0x0001){ //the size of notify_data[] need less than MTU size esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gatt_db_handle_table[IDX_CHAR_VAL_C], sizeof(notify_data), notify_data, false); ESP_LOGI(EXAMPLE_TAG, "(6) ***** send notify AA BB ***** "); }{...}else if (descr_value == 0x0002){ //the size of indicate_data[] need less than MTU size esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, gatt_db_handle_table[IDX_CHAR_VAL_C], sizeof(notify_data), notify_data, true); }{...} else if (descr_value == 0x0000){ ESP_LOGI(EXAMPLE_TAG, "notify/indicate disable "); }{...}else{ ESP_LOGE(EXAMPLE_TAG, "unknown descr value"); ESP_LOG_BUFFER_HEX(EXAMPLE_TAG, param->write.value, param->write.len); } }{...} if(gatt_db_handle_table[IDX_CHAR_VAL_A] == param->write.handle && param->write.len == 2) { uint8_t write_data[2] = {0x88, 0x99}; if(memcmp(write_data, param->write.value, param->write.len) == 0) { ESP_LOGI(EXAMPLE_TAG, "(3)***** short write success ***** "); }{...} }{...} /* send response when param->write.need_rsp is true*/ if (param->write.need_rsp){ esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL); }{...} }{...}else{ /* handle prepare write */ example_prepare_write_event_env(gatts_if, &prepare_write_env, param); } break;... case ESP_GATTS_EXEC_WRITE_EVT: // the length of gattc prepare write data must be less than GATTS_EXAMPLE_CHAR_VAL_LEN_MAX. ESP_LOGI(EXAMPLE_TAG, "ESP_GATTS_EXEC_WRITE_EVT, Length=%d", prepare_write_env.prepare_len); example_exec_write_event_env(&prepare_write_env, param); break;... case ESP_GATTS_MTU_EVT: EXAMPLE_DEBUG(EXAMPLE_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu); break;... case ESP_GATTS_CONF_EVT: EXAMPLE_DEBUG(EXAMPLE_TAG, "ESP_GATTS_CONF_EVT, status = %d", param->conf.status); break;... case ESP_GATTS_START_EVT: EXAMPLE_DEBUG(EXAMPLE_TAG, "SERVICE_START_EVT, status %d, service_handle %d", param->start.status, param->start.service_handle); break;... case ESP_GATTS_CONNECT_EVT: ESP_LOGI(EXAMPLE_TAG, "ESP_GATTS_CONNECT_EVT, conn_id = %d", param->connect.conn_id); /* start security connect with peer device when receive the connect event sent by the master */ esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM); break;... case ESP_GATTS_DISCONNECT_EVT: ESP_LOGI(EXAMPLE_TAG, "ESP_GATTS_DISCONNECT_EVT, reason = %d", param->disconnect.reason); esp_ble_gap_start_advertising(&adv_params); break;... case ESP_GATTS_CREAT_ATTR_TAB_EVT:{ if (param->add_attr_tab.status != ESP_GATT_OK){ ESP_LOGE(EXAMPLE_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status); }{...} else if (param->add_attr_tab.num_handle != HRS_IDX_NB){ ESP_LOGE(EXAMPLE_TAG, "create attribute table abnormally, num_handle (%d) \ doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB); }{...} else { ESP_LOGI(EXAMPLE_TAG, "create attribute table successfully, the number handle = %d",param->add_attr_tab.num_handle); memcpy(gatt_db_handle_table, param->add_attr_tab.handles, sizeof(gatt_db_handle_table)); esp_ble_gatts_start_service(gatt_db_handle_table[IDX_SVC]); }{...} break; }{...} default: break;... }{...} }{ ... } static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { /* If event is register event, store the gatts_if for each profile */ if (event == ESP_GATTS_REG_EVT) { if (param->reg.status == ESP_GATT_OK) { heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if = gatts_if; }{...} else { ESP_LOGE(EXAMPLE_TAG, "reg app failed, app_id %04x, status %d", param->reg.app_id, param->reg.status); return; }{...} }{...} do { int idx; for (idx = 0; idx < PROFILE_NUM; idx++) { /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ if (gatts_if == ESP_GATT_IF_NONE || gatts_if == heart_rate_profile_tab[idx].gatts_if) { if (heart_rate_profile_tab[idx].gatts_cb) { heart_rate_profile_tab[idx].gatts_cb(event, gatts_if, param); }{...} }{...} }{...} }{...} while (0); }{ ... } void app_main(void) { esp_err_t ret; /* Initialize NVS. */ ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); }{...} ESP_ERROR_CHECK( ret ); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg); if (ret) { ESP_LOGE(EXAMPLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret) { ESP_LOGE(EXAMPLE_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_init(); if (ret) { ESP_LOGE(EXAMPLE_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_enable(); if (ret) { ESP_LOGE(EXAMPLE_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_ble_gatts_register_callback(gatts_event_handler); if (ret){ ESP_LOGE(EXAMPLE_TAG, "gatts register error, error code = %x", ret); return; }{...} ret = esp_ble_gap_register_callback(gap_event_handler); if (ret){ ESP_LOGE(EXAMPLE_TAG, "gap register error, error code = %x", ret); return; }{...} ret = esp_ble_gatts_app_register(ESP_APP_ID); if (ret){ ESP_LOGE(EXAMPLE_TAG, "gatts app register error, error code = %x", ret); return; }{...} esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(33); if (local_mtu_ret){ ESP_LOGE(EXAMPLE_TAG, "set local MTU failed, error code = %x", local_mtu_ret); }{...} /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/ esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND; //bonding with peer device after authentication esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT; //set the IO capability to No output No input uint8_t key_size = 16; //the key size should be 7~16 bytes uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; uint32_t passkey = 123456; esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t)); /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you, and the response key means which key you can distribute to the Master; If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you, and the init key means which key you can distribute to the slave. *//* ... */ esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.