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
39
40
41
42
43
44
45
46
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
88
89
90
93
94
95
99
100
101
102
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
129
130
131
132
137
138
139
140
141
142
143
144
147
148
149
150
151
158
159
163
164
165
166
168
169
170
171
172
173
183
184
187
188
189
190
191
192
193
194
195
196
200
201
204
205
206
210
211
212
213
214
215
216
217
218
219
220
224
225
226
231
232
233
234
235
236
237
238
241
242
243
244
245
246
247
250
251
252
254
255
256
257
260
261
262
267
268
269
270
271
272
273
274
279
280
281
282
283
284
285
286
287
290
291
292
299
300
301
302
306
307
308
309
310
311
312
313
314
315
318
319
320
321
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
369
370
371
372
373
374
375
376
377
381
382
383
384
385
389
395
396
397
398
399
400
401
402
403
404
405
406
407
421
422
423
424
425
430
435
436
437
442
449
451
452
453
454
455
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
524
525
526
527
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* ... */
#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 "phy_cent.h"9 includes
static const char *tag = "NimBLE_BLE_PHY_CENT";
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
static ble_addr_t conn_addr;
static void blecent_scan(void);
static uint8_t s_current_phy;
void ble_store_config_init(void);
/* ... */
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", error->status,
conn_handle);
if (error->status == 0) {
MODLOG_DFLT(INFO, " attr_handle=%d", attr->handle);
}{...}
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
MODLOG_DFLT(INFO, "\n");
return 0;
}{ ... }
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;
int len = 1000;
uint8_t value[len];
int rc;
struct os_mbuf *txom;
const struct peer *peer = peer_find(conn_handle);
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(LE_PHY_UUID16),
BLE_UUID16_DECLARE(LE_PHY_CHR_UUID16));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Alert "
"Notification Control Point characteristic\n");
goto err;
}{...}
for (int i = 0; i < len; i++) {
value[i] = i;
}{...}
txom = ble_hs_mbuf_from_flat(&value, len);
if (!txom) {
MODLOG_DFLT(ERROR, "Insufficient memory");
goto err;
}{...}
rc = ble_gattc_write_long(conn_handle, chr->chr.val_handle, 0,
txom, 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(const struct peer *peer)
{
const struct peer_chr *chr;
int rc;
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(LE_PHY_UUID16),
BLE_UUID16_DECLARE(LE_PHY_CHR_UUID16));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Supported "
"LE PHY 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(peer);
}{ ... }
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
if (rc == 0) {
MODLOG_DFLT(INFO, "Default LE PHY set successfully; tx_phy = %d, rx_phy = %d",
tx_phys_mask, rx_phys_mask);
}{...} else {
MODLOG_DFLT(ERROR, "Failed to set default LE PHY");
}{...}
}{ ... }
/* ... */
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);
}{...}
}{ ... }
/* ... */
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] == 0xAB && disc->data[offset + 3] == 0xF2 ) {
return 1;
}{...}
}{...}
offset += ad_struct_len + 1;
}{...} while ( offset < disc->length_data );
return 0;
}{ ... }
/* ... */
static void
blecent_connect_if_interesting(void *disc)
{
uint8_t own_addr_type;
int rc;
ble_addr_t *addr;
if (!ext_blecent_should_connect((struct ble_gap_ext_disc_desc *)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;
}{...}
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
memcpy(&conn_addr, addr, sizeof(conn_addr));
MODLOG_DFLT(INFO, "Attempting to connect to : %x %x %x %x %x %x with type %d \n",
conn_addr.val[5],conn_addr.val[4], conn_addr.val[3], conn_addr.val[2], conn_addr.val[1], conn_addr.val[0],
conn_addr.type);
/* ... */
rc = ble_gap_connect(own_addr_type, &conn_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",
conn_addr.type, addr_str(conn_addr.val), rc);
return;
}{...}
}{ ... }
/* ... */
static int
blecent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_LINK_ESTAB:
if (event->link_estab.status == 0) {
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on 1M Phy");
break;
...
case BLE_HCI_LE_PHY_2M_PREF_MASK:
case BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on 2M Phy");
break;
...
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on Coded Phy");
break;...
}{...}
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;
}{...}
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;
}{...}
}{...} 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);
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
s_current_phy = BLE_HCI_LE_PHY_2M_PREF_MASK;
break;
...
case BLE_HCI_LE_PHY_2M_PREF_MASK:
s_current_phy = BLE_HCI_LE_PHY_CODED_PREF_MASK;
break;
...
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
return 0;...
}{...}
vTaskDelay(200);
if (s_current_phy == BLE_HCI_LE_PHY_CODED_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on Coded PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, BLE_HCI_LE_PHY_CODED_PREF_MASK,
NULL, NULL, NULL, blecent_gap_event, NULL);
}{...}
else if (s_current_phy == BLE_HCI_LE_PHY_2M_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on 2M PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, (BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK),
NULL, NULL, NULL, blecent_gap_event, NULL);
}{...}
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_EXT_DISC:
ext_print_adv_report(&event->disc);
blecent_connect_if_interesting(&event->disc);
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 ii, rc;
uint8_t all_phy;
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
if (s_current_phy != BLE_HCI_LE_PHY_1M_PREF_MASK) {
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
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]);
for(ii = 0 ;ii < 6; ii++)
conn_addr.val[ii] = peer_addr[ii];
conn_addr.type = 0;
vTaskDelay(300);
if (s_current_phy == BLE_HCI_LE_PHY_2M_PREF_MASK)
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK ;
ble_gap_ext_connect(0, &conn_addr, 30000, s_current_phy,
NULL, NULL, NULL, blecent_gap_event, NULL);
}{...}
}{...}
else {
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();
if (ret != ESP_OK) {
MODLOG_DFLT(ERROR, "Failed to init nimble %d \n", 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("blecent-phy");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(blecent_host_task);
}{ ... }