1
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
52
53
54
55
56
57
58
59
60
61
65
68
69
70
71
72
73
74
75
76
77
81
82
83
84
85
89
90
91
92
93
94
95
100
101
102
107
108
109
110
111
112
113
114
117
118
119
120
121
128
129
133
134
135
136
139
140
141
142
145
146
147
148
149
150
151
152
153
154
158
159
162
163
164
168
169
170
171
172
173
174
175
176
177
178
182
183
184
189
190
191
192
193
194
195
196
197
198
201
202
203
204
205
206
207
210
211
212
215
216
217
218
221
222
223
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
253
254
255
258
259
260
261
262
263
264
265
268
269
270
273
278
279
280
281
282
283
288
289
290
291
292
293
294
295
296
297
301
305
306
307
314
315
316
317
321
322
325
326
327
328
329
330
331
332
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
393
394
395
396
397
398
399
400
401
402
409
410
418
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
445
446
447
448
449
450
451
452
453
461
462
463
464
465
466
467
468
469
470
471
472
473
474
476
477
478
479
480
481
482
483
484
485
486
492
493
494
496
497
498
499
500
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
537
538
539
540
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* ... */
#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 "ble_cts_cent.h"
#include "services/cts/ble_svc_cts.h"10 includes
static const char *tag = "NimBLE_CTS_CENT";
static int ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
static char *day_of_week[7] = {
"Unknown"
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
}{...};
void ble_store_config_init(void);
static void ble_cts_cent_scan(void);
void printtime(struct ble_svc_cts_curr_time ctime) {
ESP_LOGI(tag, "Date : %d/%d/%d %s", ctime.et_256.d_d_t.d_t.day,
ctime.et_256.d_d_t.d_t.month,
ctime.et_256.d_d_t.d_t.year,
day_of_week[ctime.et_256.d_d_t.day_of_week]);
ESP_LOGI(tag, "hours : %d minutes : %d ",
ctime.et_256.d_d_t.d_t.hours,
ctime.et_256.d_d_t.d_t.minutes);
ESP_LOGI(tag, "seconds : %d\n", ctime.et_256.d_d_t.d_t.seconds);
ESP_LOGI(tag, "fractions : %d\n", ctime.et_256.fractions_256);
}{ ... }
/* ... */
static int
ble_cts_cent_on_read(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg)
{
struct ble_svc_cts_curr_time ctime;
MODLOG_DFLT(INFO, "Read Current time complete; status=%d conn_handle=%d\n",
error->status, conn_handle);
if (error->status == 0) {
MODLOG_DFLT(INFO, " attr_handle=%d value=\n", attr->handle);
print_mbuf(attr->om);
}{...}
else {
goto err;
}{...}
MODLOG_DFLT(INFO, "\n");
ble_hs_mbuf_to_flat(attr->om, &ctime, sizeof(ctime), NULL);
printtime(ctime);
return 0;
err:
return ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}{ ... }
/* ... */
static int
ble_cts_cent_read_time(const struct peer *peer)
{
/* ... */
const struct peer_chr *chr;
int rc;
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(BLE_SVC_CTS_UUID16),
BLE_UUID16_DECLARE(BLE_SVC_CTS_CHR_UUID16_CURRENT_TIME));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the CTS "
" characteristic\n");
goto err;
}{...}
rc = ble_gattc_read(peer->conn_handle, chr->chr.val_handle,
ble_cts_cent_on_read, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to read 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
ble_cts_cent_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);
/* ... */
ble_cts_cent_read_time(peer);
}{ ... }
/* ... */
static void
ble_cts_cent_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,
ble_cts_cent_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_ble_cts_cent_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 + 1] == 0x03) {
int temp = 2;
while(temp < disc->data[offset + 1]) {
if(disc->data[offset + temp] == 0x05 &&
disc->data[offset + temp + 1] == 0x18) {
return 1;
}{...}
temp += 2;
}{...}
}{...}
offset += ad_struct_len + 1;
}{...} while ( offset < disc->length_data );
return 0;
}{...}
/* ... */#else
static int
ble_cts_cent_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)) {
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;
}{...}
}{...}
/* ... */
for (i = 0; i < fields.num_uuids16; i++) {
if (ble_uuid_u16(&fields.uuids16[i].u) == BLE_SVC_CTS_UUID16) {
return 1;
}{...}
}{...}
return 0;
}{ ... }
#endif/* ... */
/* ... */
static void
ble_cts_cent_connect_if_interesting(void *disc)
{
uint8_t own_addr_type;
int rc;
ble_addr_t *addr;
#if CONFIG_EXAMPLE_EXTENDED_ADV
if (!ext_ble_cts_cent_should_connect((struct ble_gap_ext_disc_desc *)disc)) {
return;
}{...}
#else/* ... */
if (!ble_cts_cent_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,
ble_cts_cent_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
ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
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);
ble_cts_cent_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 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,
ble_cts_cent_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);
ble_cts_cent_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);
ble_cts_cent_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 CONFIG_EXAMPLE_ENCRYPTION
rc = peer_disc_all(event->connect.conn_handle,
ble_cts_cent_on_disc_complete, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Failed to discover services; rc=%d\n", rc);
return 0;
}{...}
#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;
#if CONFIG_EXAMPLE_EXTENDED_ADV...
case BLE_GAP_EVENT_EXT_DISC:
ext_print_adv_report(&event->disc);
ble_cts_cent_connect_if_interesting(&event->disc);
return 0;/* ... */
#endif
default:
return 0;...
}{...}
}{ ... }
static void
ble_cts_cent_on_reset(int reason)
{
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
}{ ... }
static void
ble_cts_cent_on_sync(void)
{
int rc;
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
ble_cts_cent_scan();
}{ ... }
void ble_cts_cent_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) {
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
return;
}{...}
ble_hs_cfg.reset_cb = ble_cts_cent_on_reset;
ble_hs_cfg.sync_cb = ble_cts_cent_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-cts-cent");
assert(rc == 0);
ble_store_config_init();
nimble_port_freertos_init(ble_cts_cent_host_task);
}{ ... }