-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmain.c
More file actions
744 lines (609 loc) · 18.4 KB
/
main.c
File metadata and controls
744 lines (609 loc) · 18.4 KB
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* Copyright (c) 2014-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ble.h>
#include <ble_gap.h>
#include <bm/bm_buttons.h>
#include <hal/nrf_gpio.h>
#include <nrf_soc.h>
#include <bm/bluetooth/ble_conn_params.h>
#include <bm/bluetooth/ble_db_discovery.h>
#include <bm/bluetooth/ble_scan.h>
#include <bm/bluetooth/peer_manager/nrf_ble_lesc.h>
#include <bm/bluetooth/peer_manager/peer_manager.h>
#include <bm/bluetooth/peer_manager/peer_manager_handler.h>
#include <bm/bluetooth/peer_manager/peer_manager_types.h>
#include <bm/bluetooth/ble_gq.h>
#include <bm/bluetooth/services/ble_bas_client.h>
#include <bm/bluetooth/services/ble_hrs_client.h>
#include <bm/bluetooth/services/uuid.h>
#include <bm/softdevice_handler/nrf_sdh.h>
#include <bm/softdevice_handler/nrf_sdh_ble.h>
#include <bm/softdevice_handler/nrf_sdh_soc.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/sys/atomic.h>
#include <board-config.h>
LOG_MODULE_REGISTER(sample, CONFIG_SAMPLE_BLE_HRS_CENTRAL_LOG_LEVEL);
/* Structure used to identify the heart rate client module. */
BLE_HRS_CLIENT_DEF(ble_hrs_client);
/* Battery service client instance. */
BLE_BAS_CLIENT_DEF(ble_bas_client);
/* Gatt queue instance. */
BLE_GQ_DEF(ble_gq);
/* DB discovery module instance. */
BLE_DB_DISCOVERY_DEF(ble_db_disc);
/* Scanning module instance. */
BLE_SCAN_DEF(ble_scan);
/* Current connection handle. */
static uint16_t conn_handle;
/* True if allow list has been temporarily disabled. */
static bool allow_list_disabled;
/* Central connections. */
static atomic_t central_conn;
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
uint8_t target_periph_addr[BLE_GAP_ADDR_LEN] = {
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 40) & 0xff,
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 32) & 0xff,
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 24) & 0xff,
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 16) & 0xff,
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 8) & 0xff,
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR) & 0xff,
};
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
static uint32_t active_conn_count(atomic_t *conn)
{
uint32_t set_flag_count = 0;
for (uint32_t i = 0; i < CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++) {
if (atomic_test_bit(conn, i)) {
set_flag_count += 1;
}
}
return set_flag_count;
}
static uint32_t scan_start(bool erase_bonds);
static void db_disc_handler(struct ble_db_discovery *db_discovery,
struct ble_db_discovery_evt *evt)
{
ble_hrs_on_db_disc_evt(&ble_hrs_client, evt);
ble_bas_on_db_disc_evt(&ble_bas_client, evt);
}
static void pm_evt_handler(const struct pm_evt *evt)
{
pm_handler_on_pm_evt(evt);
pm_handler_disconnect_on_sec_failure(evt);
pm_handler_flash_clean(evt);
switch (evt->evt_id) {
case PM_EVT_PEERS_DELETE_SUCCEEDED:
scan_start(false);
break;
default:
break;
}
}
static void on_ble_evt(const ble_evt_t *ble_evt, void *ctx)
{
uint32_t nrf_err;
const ble_gap_evt_t *gap_evt = &ble_evt->evt.gap_evt;
int idx = nrf_sdh_ble_idx_get(ble_evt->evt.gap_evt.conn_handle);
switch (ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
LOG_INF("Connected");
conn_handle = ble_evt->evt.gap_evt.conn_handle;
nrf_err = ble_db_discovery_start(&ble_db_disc, ble_evt->evt.gap_evt.conn_handle);
if (nrf_err != 0) {
LOG_ERR("db discovery start failed, nrf_error %#x", nrf_err);
}
if (ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_CENTRAL) {
atomic_set_bit(¢ral_conn, idx);
}
if (active_conn_count(¢ral_conn) < CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT) {
scan_start(false);
}
nrf_gpio_pin_write(BOARD_PIN_LED_1, BOARD_LED_ACTIVE_STATE);
break;
case BLE_GAP_EVT_DISCONNECTED:
LOG_INF("Disconnected, reason %#x", gap_evt->params.disconnected.reason);
if (conn_handle == gap_evt->conn_handle) {
conn_handle = BLE_CONN_HANDLE_INVALID;
}
atomic_clear_bit(¢ral_conn, idx);
if (active_conn_count(¢ral_conn) < CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT) {
scan_start(false);
}
nrf_gpio_pin_write(BOARD_PIN_LED_1, !BOARD_LED_ACTIVE_STATE);
break;
case BLE_GAP_EVT_TIMEOUT:
if (gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) {
LOG_INF("Connection Request timed out");
}
break;
case BLE_GATTC_EVT_TIMEOUT:
LOG_INF("GATT Client Timeout");
nrf_err = sd_ble_gap_disconnect(ble_evt->evt.gattc_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (nrf_err) {
LOG_ERR("Failed to disconnect, nrf_error %#x", nrf_err);
}
break;
case BLE_GATTS_EVT_TIMEOUT:
LOG_INF("GATT Server Timeout");
nrf_err = sd_ble_gap_disconnect(ble_evt->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (nrf_err) {
LOG_ERR("Failed to disconnect, nrf_error %#x", nrf_err);
}
break;
default:
break;
}
}
NRF_SDH_BLE_OBSERVER(sdh_ble, on_ble_evt, NULL, USER_LOW);
static uint32_t peer_manager_init(void)
{
uint32_t nrf_err;
ble_gap_sec_params_t sec_param = {
.bond = 1,
.mitm = 0,
.lesc = 1,
.keypress = 0,
.io_caps = BLE_GAP_IO_CAPS_NONE,
.oob = 0,
.min_key_size = 7,
.max_key_size = 16,
.kdist_own.enc = 1,
.kdist_own.id = 1,
.kdist_peer.enc = 1,
.kdist_peer.id = 1,
};
nrf_err = pm_init();
if (nrf_err) {
LOG_ERR("PM init failed, nrf_error %#x", nrf_err);
return nrf_err;
}
nrf_err = pm_sec_params_set(&sec_param);
if (nrf_err) {
LOG_ERR("Failed to set PM sec params, nrf_error %#x", nrf_err);
return nrf_err;
}
nrf_err = pm_register(pm_evt_handler);
if (nrf_err) {
LOG_ERR("PM register failed, nrf_error %#x", nrf_err);
return nrf_err;
}
return NRF_SUCCESS;
}
static uint32_t delete_bonds(void)
{
uint32_t nrf_err;
LOG_INF("Erase bonds!");
nrf_err = pm_peers_delete();
if (nrf_err) {
LOG_ERR("Failed to delete bonds, nrf_error %#x", nrf_err);
return nrf_err;
}
return NRF_SUCCESS;
}
static void allow_list_disable(void)
{
if (!allow_list_disabled) {
LOG_INF("allow list temporarily disabled");
allow_list_disabled = true;
ble_scan_stop(&ble_scan);
scan_start(false);
}
}
static void button_handler_allow_list_off(uint8_t pin, uint8_t action)
{
if (action != BM_BUTTONS_PRESS) {
return;
}
allow_list_disable();
}
static void button_handler_disconnect(uint8_t pin, uint8_t action)
{
if (conn_handle == BLE_CONN_HANDLE_INVALID || action != BM_BUTTONS_PRESS) {
return;
}
uint32_t nrf_err = sd_ble_gap_disconnect(conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (nrf_err != 0) {
LOG_ERR("ble gap disconnect failed, nrf_error %#x", nrf_err);
}
}
static void hrs_c_evt_handler(struct ble_hrs_client *hrs, const struct ble_hrs_client_evt *evt)
{
uint32_t nrf_err;
switch (evt->evt_type) {
case BLE_HRS_CLIENT_EVT_DISCOVERY_COMPLETE:
LOG_INF("Heart rate service discovered");
nrf_err = ble_hrs_client_handles_assign(hrs, evt->conn_handle,
&evt->discovery_complete.handles);
if (nrf_err != 0) {
LOG_ERR("ble_hrs_client_handles_assign failed, nrf_error %#x", nrf_err);
}
/* Heart rate service discovered. Enable notification of Heart Rate Measurement. */
nrf_err = ble_hrs_client_hrm_notif_enable(hrs);
if (nrf_err != 0) {
LOG_ERR("ble_hrs_client_hrm_notif_enable failed, nrf_error %#x", nrf_err);
}
break;
case BLE_HRS_CLIENT_EVT_HRM_NOTIFICATION:
LOG_INF("Heart Rate %d.", evt->hrm_notification.hr_value);
if (evt->hrm_notification.rr_intervals_cnt != 0) {
uint32_t rr_avg = 0;
for (uint32_t i = 0; i < evt->hrm_notification.rr_intervals_cnt; i++) {
rr_avg += evt->hrm_notification.rr_intervals[i];
}
rr_avg = rr_avg / evt->hrm_notification.rr_intervals_cnt;
LOG_INF("rr_interval (avg) %d", rr_avg);
}
break;
default:
LOG_WRN("Unhandled HRS event %d", evt->evt_type);
break;
}
}
static uint32_t hrs_c_init(void)
{
uint32_t nrf_err;
struct ble_hrs_client_config hrs_client_cfg = {
.evt_handler = hrs_c_evt_handler,
.gatt_queue = &ble_gq,
.db_discovery = &ble_db_disc
};
nrf_err = ble_hrs_client_init(&ble_hrs_client, &hrs_client_cfg);
if (nrf_err) {
LOG_ERR("Failed to init HRS client, nrf_error %#x", nrf_err);
}
return nrf_err;
}
static void bas_client_evt_handler(struct ble_bas_client *bas_client,
const struct ble_bas_client_evt *bas_client_evt)
{
uint32_t nrf_err;
switch (bas_client_evt->evt_type) {
case BLE_BAS_CLIENT_EVT_DISCOVERY_COMPLETE:
nrf_err = ble_bas_client_handles_assign(
bas_client, bas_client_evt->conn_handle,
&bas_client_evt->discovery_complete.handles);
if (nrf_err) {
LOG_ERR("Failed to assign handles, nrf_error %#x", nrf_err);
}
/** Battery service discovered. Enable notification of Battery Level. */
LOG_DBG("Battery Service discovered. Reading battery level.");
nrf_err = ble_bas_client_bl_read(bas_client);
if (nrf_err) {
LOG_ERR("Failed to read battery level characteristic, nrf_error %#x",
nrf_err);
}
LOG_DBG("Enabling Battery Level Notification.");
nrf_err = ble_bas_client_bl_notif_enable(bas_client);
if (nrf_err) {
LOG_ERR("Failed to enable battery level characteristic notifications, "
"nrf_error %#x",
nrf_err);
}
break;
case BLE_BAS_CLIENT_EVT_BATT_NOTIFICATION:
LOG_INF("Battery Level received %d %%.", bas_client_evt->battery.level);
break;
case BLE_BAS_CLIENT_EVT_BATT_READ_RESP:
LOG_INF("Battery Level Read as %d %%.", bas_client_evt->battery.level);
break;
case BLE_BAS_CLIENT_EVT_ERROR:
LOG_ERR("Battery Service client error, nrf_error %#x",
bas_client_evt->error.reason);
break;
default:
break;
}
}
static uint32_t bas_c_init(void)
{
struct ble_bas_client_config bas_client_config = {.evt_handler = bas_client_evt_handler,
.gatt_queue = &ble_gq,
.db_discovery = &ble_db_disc};
return ble_bas_client_init(&ble_bas_client, &bas_client_config);
}
static uint32_t db_discovery_init(void)
{
uint32_t nrf_err;
struct ble_db_discovery_config db_init = {0};
db_init.evt_handler = db_disc_handler;
db_init.gatt_queue = &ble_gq;
nrf_err = ble_db_discovery_init(&ble_db_disc, &db_init);
if (nrf_err) {
LOG_ERR("db discovery init failed, nrf_error %#x", nrf_err);
}
return nrf_err;
}
static void peer_list_get(uint16_t *peers, uint32_t *size)
{
uint16_t peer_id;
uint32_t peers_to_copy;
peers_to_copy = (*size < BLE_GAP_WHITELIST_ADDR_MAX_COUNT)
? *size
: BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
peer_id = pm_next_peer_id_get(PM_PEER_ID_INVALID);
*size = 0;
while ((peer_id != PM_PEER_ID_INVALID) && (peers_to_copy--)) {
peers[(*size)++] = peer_id;
peer_id = pm_next_peer_id_get(peer_id);
}
}
static uint32_t allow_list_load(void)
{
uint32_t nrf_err;
uint16_t peers[8];
uint32_t peer_cnt;
memset(peers, PM_PEER_ID_INVALID, sizeof(peers));
peer_cnt = (sizeof(peers) / sizeof(*peers));
peer_list_get(peers, &peer_cnt);
nrf_err = pm_allow_list_set(peers, peer_cnt);
if (nrf_err) {
return nrf_err;
}
nrf_err = pm_device_identities_list_set(peers, peer_cnt);
if (nrf_err != NRF_ERROR_NOT_SUPPORTED) {
return nrf_err;
}
return NRF_SUCCESS;
}
static uint32_t on_allow_list_req(void)
{
uint32_t nrf_err;
ble_gap_addr_t allow_list_addrs[8] = {0};
ble_gap_irk_t allow_list_irks[8] = {0};
uint32_t addr_cnt = (sizeof(allow_list_addrs) / sizeof(ble_gap_addr_t));
uint32_t irk_cnt = (sizeof(allow_list_irks) / sizeof(ble_gap_irk_t));
nrf_err = allow_list_load();
if (nrf_err) {
return nrf_err;
}
nrf_err = pm_allow_list_get(allow_list_addrs, &addr_cnt, allow_list_irks, &irk_cnt);
if (nrf_err) {
return nrf_err;
}
if (((addr_cnt == 0) && (irk_cnt == 0)) || (allow_list_disabled)) {
/* Don't use allow list.*/
nrf_err = ble_scan_params_set(&ble_scan, NULL);
if (nrf_err) {
return nrf_err;
}
}
return NRF_SUCCESS;
}
static uint32_t scan_start(bool erase_bonds)
{
uint32_t nrf_err;
if (erase_bonds) {
/* Scan is started by the PM_EVT_PEERS_DELETE_SUCCEEDED event. */
delete_bonds();
} else {
nrf_err = ble_scan_start(&ble_scan);
if (nrf_err) {
LOG_ERR("ble_scan_start failed, nrf_error %#x", nrf_err);
return nrf_err;
}
}
return NRF_SUCCESS;
}
static void conn_params_evt_handler(const struct ble_conn_params_evt *evt)
{
switch (evt->evt_type) {
case BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED:
LOG_INF("GATT ATT MTU on connection %#x changed to %d", evt->conn_handle,
evt->att_mtu);
break;
case BLE_CONN_PARAMS_EVT_DATA_LENGTH_UPDATED:
LOG_INF("Data length for connection %#x updated to %d", evt->conn_handle,
evt->data_length.rx);
break;
default:
LOG_WRN("unhandled conn params event %d", evt->evt_type);
break;
}
}
static void scan_evt_handler(const struct ble_scan_evt *scan_evt)
{
uint32_t nrf_err;
switch (scan_evt->evt_type) {
case BLE_SCAN_EVT_NOT_FOUND:
/* ignore */
break;
case BLE_SCAN_EVT_ALLOW_LIST_REQUEST:
on_allow_list_req();
allow_list_disabled = false;
LOG_INF("Allow list request");
break;
case BLE_SCAN_EVT_CONNECTING_ERROR:
nrf_err = scan_evt->connecting_err.reason;
LOG_INF("Scan connecting error");
break;
case BLE_SCAN_EVT_SCAN_TIMEOUT:
LOG_INF("Scan timed out");
scan_start(false);
break;
case BLE_SCAN_EVT_FILTER_MATCH:
LOG_INF("Scan filter match");
break;
case BLE_SCAN_EVT_ALLOW_LIST_ADV_REPORT:
LOG_INF("Allow list advertise report");
break;
case BLE_SCAN_EVT_CONNECTED: {
const ble_gap_evt_connected_t *p_connected = scan_evt->connected.connected;
LOG_INF("Connecting to target %02x%02x%02x%02x%02x%02x",
p_connected->peer_addr.addr[0], p_connected->peer_addr.addr[1],
p_connected->peer_addr.addr[2], p_connected->peer_addr.addr[3],
p_connected->peer_addr.addr[4], p_connected->peer_addr.addr[5]);
} break;
default:
LOG_WRN("Unhandled scan event %d", scan_evt->evt_type);
break;
}
}
static uint32_t gatt_init(void)
{
uint32_t nrf_err = ble_conn_params_evt_handler_set(conn_params_evt_handler);
if (nrf_err) {
LOG_ERR("ble_conn_params_evt_handler_set failed, nrf_error %#x", nrf_err);
}
return nrf_err;
}
static uint32_t scan_init(void)
{
uint32_t nrf_err;
struct ble_scan_config scan_cfg = {
.scan_params = {
.active = 0x01,
.interval = BLE_GAP_SCAN_INTERVAL_US_MIN * 6,
.window = BLE_GAP_SCAN_WINDOW_US_MIN * 6,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
.timeout = BLE_GAP_SCAN_TIMEOUT_UNLIMITED,
.scan_phys = BLE_GAP_PHY_AUTO,
},
.conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT,
.connect_if_match = true,
.conn_cfg_tag = CONFIG_NRF_SDH_BLE_CONN_TAG,
.evt_handler = scan_evt_handler,
};
nrf_err = ble_scan_init(&ble_scan, &scan_cfg);
if (nrf_err) {
LOG_ERR("nrf_ble_scan_init failed, nrf_error %#x", nrf_err);
}
struct ble_scan_filter_data filter_data = {
.uuid_filter.uuid = {
.uuid = BLE_UUID_HEART_RATE_SERVICE,
.type = BLE_UUID_TYPE_BLE,
},
};
nrf_err = ble_scan_filter_add(&ble_scan, BLE_SCAN_UUID_FILTER, &filter_data);
if (nrf_err) {
LOG_ERR("nrf_ble_scan_filter_add uuid failed, nrf_error %#x", nrf_err);
}
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_NAME)
filter_data.name_filter.name = CONFIG_SAMPLE_TARGET_PERIPHERAL_NAME;
nrf_err = ble_scan_filter_add(&ble_scan, BLE_SCAN_NAME_FILTER, &filter_data);
if (nrf_err) {
LOG_ERR("nrf_ble_scan_filter_add name failed, nrf_error %#x", nrf_err);
}
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_NAME */
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
filter_data.addr_filter.addr = target_periph_addr;
nrf_err = ble_scan_filter_add(&ble_scan, BLE_SCAN_ADDR_FILTER, &filter_data);
if (nrf_err) {
LOG_ERR("nrf_ble_scan_filter_add address failed, nrf_error %#x", nrf_err);
}
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
nrf_err = ble_scan_filters_enable(&ble_scan, BLE_SCAN_UUID_FILTER |
BLE_SCAN_NAME_FILTER |
BLE_SCAN_ADDR_FILTER, false);
if (nrf_err) {
LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err);
}
return NRF_SUCCESS;
}
int main(void)
{
int err;
uint32_t nrf_err;
ble_gap_conn_sec_mode_t device_name_write_sec;
static struct bm_buttons_config configs[] = {
{
.pin_number = BOARD_PIN_BTN_0,
.active_state = BM_BUTTONS_ACTIVE_LOW,
.pull_config = BM_BUTTONS_PIN_PULLUP,
.handler = button_handler_allow_list_off,
},
{
.pin_number = BOARD_PIN_BTN_1,
.active_state = BM_BUTTONS_ACTIVE_LOW,
.pull_config = BM_BUTTONS_PIN_PULLUP,
.handler = button_handler_disconnect,
},
};
LOG_INF("BLE HRS Central sample started.");
nrf_gpio_cfg_output(BOARD_PIN_LED_0);
nrf_gpio_cfg_output(BOARD_PIN_LED_1);
nrf_gpio_pin_write(BOARD_PIN_LED_0, !BOARD_LED_ACTIVE_STATE);
nrf_gpio_pin_write(BOARD_PIN_LED_1, !BOARD_LED_ACTIVE_STATE);
err = bm_buttons_init(configs, ARRAY_SIZE(configs), BM_BUTTONS_DETECTION_DELAY_MIN_US);
if (err) {
LOG_ERR("Failed to initialize buttons, err %d", err);
goto idle;
}
err = bm_buttons_enable();
if (err) {
LOG_ERR("Failed to enable buttons, err %d", err);
goto idle;
}
const bool erase_bonds = bm_buttons_is_pressed(BOARD_PIN_BTN_1);
err = nrf_sdh_enable_request();
if (err) {
LOG_ERR("Failed to enable SoftDevice, err %d", err);
goto idle;
}
LOG_INF("SoftDevice enabled");
err = nrf_sdh_ble_enable(CONFIG_NRF_SDH_BLE_CONN_TAG);
if (err) {
LOG_ERR("Failed to enable BLE, err %d", err);
goto idle;
}
LOG_INF("Bluetooth enabled");
BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&device_name_write_sec);
nrf_err = sd_ble_gap_device_name_set(&device_name_write_sec, CONFIG_SAMPLE_BLE_DEVICE_NAME,
strlen(CONFIG_SAMPLE_BLE_DEVICE_NAME));
if (nrf_err) {
LOG_ERR("Failed to set device name, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = gatt_init();
if (nrf_err) {
LOG_ERR("Failed to initialize gatt, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = peer_manager_init();
if (nrf_err) {
LOG_ERR("Failed to initialize peer manager, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = db_discovery_init();
if (nrf_err) {
LOG_ERR("Failed to initialize db discovery, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = hrs_c_init();
if (nrf_err) {
LOG_ERR("Failed to initialize HRS Client, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = bas_c_init();
if (nrf_err) {
LOG_ERR("Failed to initialize BAS Client, nrf_error %#x", nrf_err);
goto idle;
}
nrf_err = scan_init();
if (nrf_err) {
LOG_ERR("Failed to initialize scan library, nrf_error %#x", nrf_err);
goto idle;
}
nrf_gpio_pin_write(BOARD_PIN_LED_0, BOARD_LED_ACTIVE_STATE);
LOG_INF("BLE HRS Central sample initialized");
scan_start(erase_bonds);
idle:
while (true) {
#if defined(CONFIG_PM_LESC)
(void)nrf_ble_lesc_request_handler();
#endif
log_flush();
k_cpu_idle();
}
}