1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
124
125
126
127
128
129
130
131
132
133
134
135
136
140
141
142
143
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
173
174
175
176
177
178
183
184
191
192
193
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
262
263
264
265
266
267
268
269
270
271
272
273
284
285
286
287
291
292
293
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
334
335
336
337
341
342
343
344
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
367
368
369
370
371
372
373
374
375
376
377
378
379
380
385
386
/* ... */
#include "gap.h"
#include "common.h"
#include "gatt_svc.h"
inline static void format_addr(char *addr_str, uint8_t addr[]);
static void print_conn_desc(struct ble_gap_conn_desc *desc);
static void start_advertising(void);
static void set_random_addr(void);
static int gap_event_handler(struct ble_gap_event *event, void *arg);
static uint8_t own_addr_type;
static uint8_t addr_val[6] = {0};
static uint8_t esp_uri[] = {BLE_GAP_URI_PREFIX_HTTPS, '/', '/', 'e', 's', 'p', 'r', 'e', 's', 's', 'i', 'f', '.', 'c', 'o', 'm'};
inline static void format_addr(char *addr_str, uint8_t addr[]) {
sprintf(addr_str, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1],
addr[2], addr[3], addr[4], addr[5]);
}{ ... }
static void print_conn_desc(struct ble_gap_conn_desc *desc) {
char addr_str[18] = {0};
ESP_LOGI(TAG, "connection handle: %d", desc->conn_handle);
format_addr(addr_str, desc->our_id_addr.val);
ESP_LOGI(TAG, "device id address: type=%d, value=%s",
desc->our_id_addr.type, addr_str);
format_addr(addr_str, desc->peer_id_addr.val);
ESP_LOGI(TAG, "peer id address: type=%d, value=%s", desc->peer_id_addr.type,
addr_str);
ESP_LOGI(TAG,
"conn_itvl=%d, conn_latency=%d, supervision_timeout=%d, "
"encrypted=%d, authenticated=%d, bonded=%d\n",
desc->conn_itvl, desc->conn_latency, desc->supervision_timeout,
desc->sec_state.encrypted, desc->sec_state.authenticated,
desc->sec_state.bonded);
}{ ... }
static void set_random_addr(void) {
int rc = 0;
ble_addr_t addr;
rc = ble_hs_id_gen_rnd(0, &addr);
assert(rc == 0);
rc = ble_hs_id_set_rnd(addr.val);
assert(rc == 0);
}{ ... }
static void start_advertising(void) {
int rc = 0;
const char *name;
struct ble_hs_adv_fields adv_fields = {0};
struct ble_hs_adv_fields rsp_fields = {0};
struct ble_gap_adv_params adv_params = {0};
adv_fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
name = ble_svc_gap_device_name();
adv_fields.name = (uint8_t *)name;
adv_fields.name_len = strlen(name);
adv_fields.name_is_complete = 1;
adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
adv_fields.tx_pwr_lvl_is_present = 1;
adv_fields.appearance = BLE_GAP_APPEARANCE_GENERIC_TAG;
adv_fields.appearance_is_present = 1;
adv_fields.le_role = BLE_GAP_LE_ROLE_PERIPHERAL;
adv_fields.le_role_is_present = 1;
rc = ble_gap_adv_set_fields(&adv_fields);
if (rc != 0) {
ESP_LOGE(TAG, "failed to set advertising data, error code: %d", rc);
return;
}{...}
rsp_fields.device_addr = addr_val;
rsp_fields.device_addr_type = own_addr_type;
rsp_fields.device_addr_is_present = 1;
rsp_fields.uri = esp_uri;
rsp_fields.uri_len = sizeof(esp_uri);
rsp_fields.adv_itvl = BLE_GAP_ADV_ITVL_MS(500);
rsp_fields.adv_itvl_is_present = 1;
rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
if (rc != 0) {
ESP_LOGE(TAG, "failed to set scan response data, error code: %d", rc);
return;
}{...}
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
adv_params.itvl_min = BLE_GAP_ADV_ITVL_MS(500);
adv_params.itvl_max = BLE_GAP_ADV_ITVL_MS(510);
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params,
gap_event_handler, NULL);
if (rc != 0) {
ESP_LOGE(TAG, "failed to start advertising, error code: %d", rc);
return;
}{...}
ESP_LOGI(TAG, "advertising started!");
}{ ... }
/* ... */
static int gap_event_handler(struct ble_gap_event *event, void *arg) {
int rc = 0;
struct ble_gap_conn_desc desc;
switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
ESP_LOGI(TAG, "connection %s; status=%d",
event->connect.status == 0 ? "established" : "failed",
event->connect.status);
if (event->connect.status == 0) {
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
if (rc != 0) {
ESP_LOGE(TAG,
"failed to find connection by handle, error code: %d",
rc);
return rc;
}{...}
print_conn_desc(&desc);
struct ble_gap_upd_params params = {.itvl_min = desc.conn_itvl,
.itvl_max = desc.conn_itvl,
.latency = 3,
.supervision_timeout =
desc.supervision_timeout}{...};
rc = ble_gap_update_params(event->connect.conn_handle, ¶ms);
if (rc != 0) {
ESP_LOGE(
TAG,
"failed to update connection parameters, error code: %d",
rc);
return rc;
}{...}
}{...}
else {
start_advertising();
}{...}
return rc;
...
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGI(TAG, "disconnected from peer; reason=%d",
event->disconnect.reason);
start_advertising();
return rc;
...
case BLE_GAP_EVENT_CONN_UPDATE:
ESP_LOGI(TAG, "connection updated; status=%d",
event->conn_update.status);
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
if (rc != 0) {
ESP_LOGE(TAG, "failed to find connection by handle, error code: %d",
rc);
return rc;
}{...}
print_conn_desc(&desc);
return rc;
...
case BLE_GAP_EVENT_ADV_COMPLETE:
ESP_LOGI(TAG, "advertise complete; reason=%d",
event->adv_complete.reason);
start_advertising();
return rc;
...
case BLE_GAP_EVENT_NOTIFY_TX:
if ((event->notify_tx.status != 0) &&
(event->notify_tx.status != BLE_HS_EDONE)) {
ESP_LOGI(TAG,
"notify event; conn_handle=%d attr_handle=%d "
"status=%d is_indication=%d",
event->notify_tx.conn_handle, event->notify_tx.attr_handle,
event->notify_tx.status, event->notify_tx.indication);
}{...}
return rc;
...
case BLE_GAP_EVENT_SUBSCRIBE:
ESP_LOGI(TAG,
"subscribe event; conn_handle=%d attr_handle=%d "
"reason=%d prevn=%d curn=%d previ=%d curi=%d",
event->subscribe.conn_handle, event->subscribe.attr_handle,
event->subscribe.reason, event->subscribe.prev_notify,
event->subscribe.cur_notify, event->subscribe.prev_indicate,
event->subscribe.cur_indicate);
rc = gatt_svr_subscribe_cb(event);
if (rc == BLE_ATT_ERR_INSUFFICIENT_AUTHEN) {
return ble_gap_security_initiate(event->subscribe.conn_handle);
}{...}
return rc;
...
case BLE_GAP_EVENT_MTU:
ESP_LOGI(TAG, "mtu update event; conn_handle=%d cid=%d mtu=%d",
event->mtu.conn_handle, event->mtu.channel_id,
event->mtu.value);
return rc;
...
case BLE_GAP_EVENT_ENC_CHANGE:
if (event->enc_change.status == 0) {
ESP_LOGI(TAG, "connection encrypted!");
}{...} else {
ESP_LOGE(TAG, "connection encryption failed, status: %d",
event->enc_change.status);
}{...}
return rc;
...
case BLE_GAP_EVENT_REPEAT_PAIRING:
rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
if (rc != 0) {
ESP_LOGE(TAG, "failed to find connection, error code %d", rc);
return rc;
}{...}
ble_store_util_delete_peer(&desc.peer_id_addr);
/* ... */
ESP_LOGI(TAG, "repairing...");
return BLE_GAP_REPEAT_PAIRING_RETRY;
...
case BLE_GAP_EVENT_PASSKEY_ACTION:
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
struct ble_sm_io pkey = {0};
pkey.action = event->passkey.params.action;
pkey.passkey = 100000 + esp_random() % 900000;
ESP_LOGI(TAG, "enter passkey %" PRIu32 " on the peer side",
pkey.passkey);
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
if (rc != 0) {
ESP_LOGE(TAG,
"failed to inject security manager io, error code: %d",
rc);
return rc;
}{...}
}{...}
return rc;...
}{...}
return rc;
}{ ... }
void adv_init(void) {
int rc = 0;
char addr_str[18] = {0};
set_random_addr();
rc = ble_hs_util_ensure_addr(1);
if (rc != 0) {
ESP_LOGE(TAG, "device does not have any available bt address!");
return;
}{...}
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
ESP_LOGE(TAG, "failed to infer address type, error code: %d", rc);
return;
}{...}
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
if (rc != 0) {
ESP_LOGE(TAG, "failed to copy device address, error code: %d", rc);
return;
}{...}
format_addr(addr_str, addr_val);
ESP_LOGI(TAG, "device address: %s", addr_str);
start_advertising();
}{ ... }
bool is_connection_encrypted(uint16_t conn_handle) {
int rc = 0;
struct ble_gap_conn_desc desc;
rc = ble_gap_conn_find(conn_handle, &desc);
if (rc != 0) {
ESP_LOGE(TAG, "failed to find connection by handle, error code: %d",
rc);
return false;
}{...}
return desc.sec_state.encrypted;
}{ ... }
int gap_init(void) {
int rc = 0;
ble_svc_gap_init();
rc = ble_svc_gap_device_name_set(DEVICE_NAME);
if (rc != 0) {
ESP_LOGE(TAG, "failed to set device name to %s, error code: %d",
DEVICE_NAME, rc);
return rc;
}{...}
return rc;
}{ ... }