forked from NordicSemiconductor/pc-ble-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
996 lines (853 loc) · 30.8 KB
/
main.c
File metadata and controls
996 lines (853 loc) · 30.8 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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
/**@example examples/heart_rate_collector
*
* @brief Heart Rate Collector Sample Application main file.
*
* This file contains the source code for a sample application that acts as a BLE Central device.
* This application scans for a Heart Rate Sensor device and reads it's heart rate data.
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml
*/
#include "ble.h"
#include "sd_rpc.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define DEFAULT_UART_PORT_NAME "COM1"
#define DEFAULT_BAUD_RATE 1000000 /**< The baud rate to be used for serial communication with nRF5 device. */
#endif
#ifdef __APPLE__
#define DEFAULT_UART_PORT_NAME "/dev/tty.usbmodem00000"
#define DEFAULT_BAUD_RATE 115200 /* 1M baud rate is not supported on MacOS */
#endif
#ifdef __linux__
#define DEFAULT_UART_PORT_NAME "/dev/ttyACM0"
#define DEFAULT_BAUD_RATE 1000000
#endif
enum
{
UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
};
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
#define SCAN_INTERVAL 0x00A0 /**< Determines scan interval in units of 0.625 milliseconds. */
#define SCAN_WINDOW 0x0050 /**< Determines scan window in units of 0.625 milliseconds. */
#define SCAN_TIMEOUT 0x0 /**< Scan timeout between 0x01 and 0xFFFF in seconds, 0x0 disables timeout. */
#define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Determines minimum connection interval in milliseconds. */
#define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Determines maximum connection interval in milliseconds. */
#define SLAVE_LATENCY 0 /**< Slave Latency in number of connection events. */
#define CONNECTION_SUPERVISION_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Determines supervision time-out in units of 10 milliseconds. */
#define TARGET_DEV_NAME "Nordic_HRM" /**< Connect to a peripheral using a given advertising name here. */
#define MAX_PEER_COUNT 1 /**< Maximum number of peer's application intends to manage. */
#define BLE_UUID_HEART_RATE_SERVICE 0x180D /**< Heart Rate service UUID. */
#define BLE_UUID_HEART_RATE_MEASUREMENT_CHAR 0x2A37 /**< Heart Rate Measurement characteristic UUID. */
#define BLE_UUID_CCCD 0x2902
#define BLE_CCCD_NOTIFY 0x01
#define STRING_BUFFER_SIZE 50
typedef struct
{
uint8_t * p_data; /**< Pointer to data. */
uint16_t data_len; /**< Length of data. */
} data_t;
static uint8_t m_connected_devices = 0;
static uint16_t m_connection_handle = 0;
static uint16_t m_service_start_handle = 0;
static uint16_t m_service_end_handle = 0;
static uint16_t m_hrm_char_handle = 0;
static uint16_t m_hrm_cccd_handle = 0;
static bool m_connection_is_in_progress = false;
static adapter_t * m_adapter = NULL;
static uint32_t m_config_id = 1;
static const ble_gap_scan_params_t m_scan_param =
{
1, // Active scanning set.
0, // Selective scanning not set.
#if NRF_SD_BLE_API == 2
NULL, // White-list not set.
#endif
#if NRF_SD_BLE_API >= 3
0, // adv_dir_report not set.
#endif
(uint16_t)SCAN_INTERVAL,
(uint16_t)SCAN_WINDOW,
(uint16_t)SCAN_TIMEOUT
};
static const ble_gap_conn_params_t m_connection_param =
{
(uint16_t)MIN_CONNECTION_INTERVAL,
(uint16_t)MAX_CONNECTION_INTERVAL,
(uint16_t)SLAVE_LATENCY,
(uint16_t)CONNECTION_SUPERVISION_TIMEOUT
};
/* Local function forward declarations */
static uint32_t ble_stack_init();
static uint32_t adv_report_parse(uint8_t type, data_t * p_advdata, data_t * p_typedata);
static bool find_adv_name(const ble_gap_evt_adv_report_t *p_adv_report, const char * name_to_find);
static uint32_t scan_start();
static uint32_t service_discovery_start();
static uint32_t char_discovery_start();
static uint32_t descr_discovery_start();
static uint32_t hrm_cccd_set(uint8_t value);
static void ble_address_to_string_convert(ble_gap_addr_t address, uint8_t * string_buffer);
/**@brief Function for handling error message events from sd_rpc.
*
* @param[in] adapter The transport adapter.
* @param[in] code Error code that the error message is associated with.
* @param[in] message The error message that the callback is associated with.
*/
static void status_handler(adapter_t * adapter, sd_rpc_app_status_t code, const char * message)
{
printf("Status: %d, message: %s\n", (uint32_t)code, message);
fflush(stdout);
}
/**@brief Function for handling the log message events from sd_rpc.
*
* @param[in] adapter The transport adapter.
* @param[in] severity Level of severity that the log message is associated with.
* @param[in] message The log message that the callback is associated with.
*/
static void log_handler(adapter_t * adapter, sd_rpc_log_severity_t severity, const char * message)
{
switch (severity)
{
case SD_RPC_LOG_ERROR:
printf("Error: %s\n", message);
fflush(stdout);
break;
case SD_RPC_LOG_WARNING:
printf("Warning: %s\n", message);
fflush(stdout);
break;
case SD_RPC_LOG_INFO:
printf("Info: %s\n", message);
fflush(stdout);
break;
default:
printf("Log: %s\n", message);
fflush(stdout);
break;
}
}
/**@brief Function called on BLE_GAP_EVT_CONNECTED event.
*
* @details Update connection state and proceed to discovering the peer's GATT services.
*
* @param[in] p_ble_gap_evt GAP event.
*/
static void on_connected(const ble_gap_evt_t * const p_ble_gap_evt)
{
printf("Connection established\n");
fflush(stdout);
m_connected_devices++;
m_connection_handle = p_ble_gap_evt->conn_handle;
m_connection_is_in_progress = false;
service_discovery_start();
}
/**@brief Function called on BLE_GAP_EVT_ADV_REPORT event.
*
* @details Create a connection if received advertising packet corresponds to desired BLE device.
*
* @param[in] p_ble_gap_evt Advertising Report Event.
*/
static void on_adv_report(const ble_gap_evt_t * const p_ble_gap_evt)
{
uint32_t err_code;
uint8_t str[STRING_BUFFER_SIZE] = {0};
// Log the Bluetooth device address of advertisement packet received.
ble_address_to_string_convert(p_ble_gap_evt->params.adv_report.peer_addr, str);
printf("Received advertisement report with device address: 0x%s\n", str);
fflush(stdout);
if (find_adv_name(&p_ble_gap_evt->params.adv_report, TARGET_DEV_NAME))
{
if (m_connected_devices >= MAX_PEER_COUNT || m_connection_is_in_progress)
{
return;
}
err_code = sd_ble_gap_connect(m_adapter,
&(p_ble_gap_evt->params.adv_report.peer_addr),
&m_scan_param,
&m_connection_param
#if NRF_SD_BLE_API >= 5
, m_config_id
#endif
);
if (err_code != NRF_SUCCESS)
{
printf("Connection Request Failed, reason %d\n", err_code);
fflush(stdout);
return;
}
m_connection_is_in_progress = true;
}
}
/**@brief Function called on BLE_GAP_EVT_TIMEOUT event.
*
* @param[in] ble_gap_evt_t Timeout Event.
*/
static void on_timeout(const ble_gap_evt_t * const p_ble_gap_evt)
{
if (p_ble_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
{
m_connection_is_in_progress = false;
}
else if (p_ble_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
{
scan_start();
}
}
/**@brief Function called on BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP event.
*
* @details Update service state and proceed to discovering the service's GATT characteristics.
*
* @param[in] p_ble_gattc_evt Primary Service Discovery Response Event.
*/
static void on_service_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
int count;
int service_index;
const ble_gattc_service_t * service;
printf("Received service discovery response\n");
fflush(stdout);
if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
{
printf("Service discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
fflush(stdout);
return;
}
count = p_ble_gattc_evt->params.prim_srvc_disc_rsp.count;
if (count == 0)
{
printf("Service not found\n");
fflush(stdout);
return;
}
if (count > 1)
{
printf("Warning, discovered multiple primary services. Ignoring all but the first\n");
}
service_index = 0; /* We expect to discover only the Heart Rate service as requested. */
service = &(p_ble_gattc_evt->params.prim_srvc_disc_rsp.services[service_index]);
if (service->uuid.uuid != BLE_UUID_HEART_RATE_SERVICE)
{
printf("Unknown service discovered with UUID: 0x%04X\n", service->uuid.uuid);
fflush(stdout);
return;
}
m_service_start_handle = service->handle_range.start_handle;
m_service_end_handle = service->handle_range.end_handle;
printf("Discovered heart rate service. UUID: 0x%04X, "
"start handle: 0x%04X, end handle: 0x%04X\n",
service->uuid.uuid, m_service_start_handle, m_service_end_handle);
fflush(stdout);
char_discovery_start();
}
/**@brief Function called on BLE_GATTC_EVT_CHAR_DISC_RSP event.
*
* @details Update characteristic state and proceed to discovering the characteristicss descriptors.
*
* @param[in] p_ble_gattc_evt Characteristic Discovery Response Event.
*/
static void on_characteristic_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
int count = p_ble_gattc_evt->params.char_disc_rsp.count;
if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
{
printf("Characteristic discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
fflush(stdout);
return;
}
printf("Received characteristic discovery response, characteristics count: %d\n", count);
fflush(stdout);
for (int i = 0; i < count; i++)
{
printf("Characteristic handle: 0x%04X, UUID: 0x%04X\n",
p_ble_gattc_evt->params.char_disc_rsp.chars[i].handle_decl,
p_ble_gattc_evt->params.char_disc_rsp.chars[i].uuid.uuid);
fflush(stdout);
if (p_ble_gattc_evt->params.char_disc_rsp.chars[i].uuid.uuid ==
BLE_UUID_HEART_RATE_MEASUREMENT_CHAR)
{
m_hrm_char_handle = p_ble_gattc_evt->params.char_disc_rsp.chars[i].handle_decl;
}
}
descr_discovery_start();
}
/**@brief Function called on BLE_GATTC_EVT_DESC_DISC_RSP event.
*
* @details Update CCCD descriptor state and proceed to prompting user to toggle notifications.
*
* @param[in] p_ble_gattc_evt Descriptor Discovery Response Event.
*/
static void on_descriptor_discovery_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
int count = p_ble_gattc_evt->params.desc_disc_rsp.count;
if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
{
printf("Descriptor discovery failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
fflush(stdout);
return;
}
printf("Received descriptor discovery response, descriptor count: %d\n", count);
fflush(stdout);
for (int i = 0; i < count; i++)
{
printf("Descriptor handle: 0x%04X, UUID: 0x%04X\n",
p_ble_gattc_evt->params.desc_disc_rsp.descs[i].handle,
p_ble_gattc_evt->params.desc_disc_rsp.descs[i].uuid.uuid);
fflush(stdout);
if (p_ble_gattc_evt->params.desc_disc_rsp.descs[i].uuid.uuid == BLE_UUID_CCCD)
{
m_hrm_cccd_handle = p_ble_gattc_evt->params.desc_disc_rsp.descs[i].handle;
printf("Press enter to toggle notifications on the HRM characteristic\n");
fflush(stdout);
}
}
}
/**@brief Function called on BLE_GATTC_EVT_WRITE_RSP event.
*
* @param[in] p_ble_gattc_evt Write Response Event.
*/
static void on_write_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
printf("Received write response.\n");
fflush(stdout);
if (p_ble_gattc_evt->gatt_status != NRF_SUCCESS)
{
printf("Error. Write operation failed. Error code 0x%X\n", p_ble_gattc_evt->gatt_status);
fflush(stdout);
}
}
/**@brief Function called on BLE_GATTC_EVT_HVX event.
*
* @details Logs the received heart rate measurement.
*
* @param[in] p_ble_gattc_evt Handle Value Notification/Indication Event.
*/
static void on_hvx(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
if (p_ble_gattc_evt->params.hvx.handle >= m_hrm_char_handle ||
p_ble_gattc_evt->params.hvx.handle <= m_hrm_cccd_handle) // Heart rate measurement.
{
// We know the heart rate reading is encoded as 2 bytes [flag, value].
printf("Received heart rate measurement: %d\n", p_ble_gattc_evt->params.hvx.data[1]);
}
else // Unknown data.
{
printf("Un-parsed data received on handle: %04X\n", p_ble_gattc_evt->params.hvx.handle);
}
fflush(stdout);
}
/**@brief Function called on BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST event.
*
* @details Update GAP connection parameters.
*
* @param[in] p_ble_gap_evt Connection Parameter Update Event.
*/
static void on_conn_params_update_request(const ble_gap_evt_t * const p_ble_gap_evt)
{
uint32_t err_code = sd_ble_gap_conn_param_update(m_adapter, m_connection_handle,
&(p_ble_gap_evt->
params.conn_param_update_request.conn_params));
if (err_code != NRF_SUCCESS)
{
printf("Conn params update failed, err_code %d\n", err_code);
fflush(stdout);
}
}
#if NRF_SD_BLE_API >= 3
/**@brief Function called on BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST event.
*
* @details Replies to an ATT_MTU exchange request by sending an Exchange MTU Response to the client.
*
* @param[in] p_ble_gatts_evt Exchange MTU Request Event.
*/
static void on_exchange_mtu_request(const ble_gatts_evt_t * const p_ble_gatts_evt)
{
uint32_t err_code = sd_ble_gatts_exchange_mtu_reply(
m_adapter,
m_connection_handle,
#if NRF_SD_BLE_API < 5
GATT_MTU_SIZE_DEFAULT);
#else
BLE_GATT_ATT_MTU_DEFAULT);
#endif
if (err_code != NRF_SUCCESS)
{
printf("MTU exchange request reply failed, err_code %d\n", err_code);
fflush(stdout);
}
}
/**@brief Function called on BLE_GATTC_EVT_EXCHANGE_MTU_RSP event.
*
* @details Logs the new BLE server RX MTU size.
*
* @param[in] p_ble_gattc_evt Exchange MTU Response Event.
*/
static void on_exchange_mtu_response(const ble_gattc_evt_t * const p_ble_gattc_evt)
{
uint16_t server_rx_mtu = p_ble_gattc_evt->params.exchange_mtu_rsp.server_rx_mtu;
printf("MTU response received. New ATT_MTU is %d\n", server_rx_mtu);
fflush(stdout);
}
#endif
/**@brief Function for initializing serial communication with the target nRF5 Bluetooth slave.
*
* @param[in] serial_port The serial port the target nRF5 device is connected to.
*
* @return The new transport adapter.
*/
static adapter_t * adapter_init(char * serial_port, uint32_t baud_rate)
{
physical_layer_t * phy;
data_link_layer_t * data_link_layer;
transport_layer_t * transport_layer;
phy = sd_rpc_physical_layer_create_uart(serial_port, baud_rate, SD_RPC_FLOW_CONTROL_NONE,
SD_RPC_PARITY_NONE);
data_link_layer = sd_rpc_data_link_layer_create_bt_three_wire(phy, 100);
transport_layer = sd_rpc_transport_layer_create(data_link_layer, 100);
return sd_rpc_adapter_create(transport_layer);
}
/**@brief Function for converting a BLE address to a string.
*
* @param[in] address Bluetooth Low Energy address.
* @param[out] string_buffer The serial port the target nRF5 device is connected to.
*/
static void ble_address_to_string_convert(ble_gap_addr_t address, uint8_t * string_buffer)
{
const int address_length = 6;
char temp_str[3];
for (int i = address_length - 1; i >= 0; --i)
{
sprintf(temp_str, "%02X", address.addr[i]);
strcat((char *)string_buffer, temp_str);
}
}
/**
* @brief Parses advertisement data, providing length and location of the field in case
* matching data is found.
*
* @param[in] Type of data to be looked for in advertisement data.
* @param[in] Advertisement report length and pointer to report.
* @param[out] If data type requested is found in the data report, type data length and
* pointer to data will be populated here.
*
* @retval NRF_SUCCESS if the data type is found in the report.
* @retval NRF_ERROR_NOT_FOUND if the data type could not be found.
*/
static uint32_t adv_report_parse(uint8_t type, data_t * p_advdata, data_t * p_typedata)
{
uint32_t index = 0;
uint8_t * p_data;
p_data = p_advdata->p_data;
while (index < p_advdata->data_len)
{
uint8_t field_length = p_data[index];
uint8_t field_type = p_data[index + 1];
if (field_type == type)
{
p_typedata->p_data = &p_data[index + 2];
p_typedata->data_len = field_length - 1;
return NRF_SUCCESS;
}
index += field_length + 1;
}
return NRF_ERROR_NOT_FOUND;
}
/**@brief Function for searching a given name in the advertisement packets.
*
* @details Use this function to parse received advertising data and to find a given
* name in them either as 'complete_local_name' or as 'short_local_name'.
*
* @param[in] p_adv_report advertising data to parse.
* @param[in] name_to_find name to search.
* @return true if the given name was found, false otherwise.
*/
static bool find_adv_name(const ble_gap_evt_adv_report_t *p_adv_report, const char * name_to_find)
{
uint32_t err_code;
data_t adv_data;
data_t dev_name;
// Initialize advertisement report for parsing
adv_data.p_data = (uint8_t *)p_adv_report->data;
adv_data.data_len = p_adv_report->dlen;
//search for advertising names
err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
&adv_data,
&dev_name);
if (err_code == NRF_SUCCESS)
{
if (memcmp(name_to_find, dev_name.p_data, dev_name.data_len )== 0)
{
return true;
}
}
else
{
// Look for the short local name if it was not found as complete
err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME,
&adv_data,
&dev_name);
if (err_code != NRF_SUCCESS)
{
return false;
}
if (memcmp(name_to_find, dev_name.p_data, dev_name.data_len )== 0)
{
return true;
}
}
return false;
}
/**@brief Function for initializing the BLE stack.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t ble_stack_init()
{
uint32_t err_code;
uint32_t * app_ram_base = NULL;
#if NRF_SD_BLE_API <= 3
ble_enable_params_t ble_enable_params;
memset(&ble_enable_params, 0, sizeof(ble_enable_params));
#endif
#if NRF_SD_BLE_API == 3
ble_enable_params.gatt_enable_params.att_mtu = GATT_MTU_SIZE_DEFAULT;
#elif NRF_SD_BLE_API < 3
ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
ble_enable_params.gatts_enable_params.service_changed = false;
ble_enable_params.gap_enable_params.periph_conn_count = 1;
ble_enable_params.gap_enable_params.central_conn_count = 1;
ble_enable_params.gap_enable_params.central_sec_count = 1;
ble_enable_params.common_enable_params.p_conn_bw_counts = NULL;
ble_enable_params.common_enable_params.vs_uuid_count = 1;
#endif
#if NRF_SD_BLE_API <= 3
err_code = sd_ble_enable(m_adapter, &ble_enable_params, app_ram_base);
#else
err_code = sd_ble_enable(m_adapter, app_ram_base);
#endif
switch (err_code) {
case NRF_SUCCESS:
break;
case NRF_ERROR_INVALID_STATE:
printf("BLE stack already enabled\n");
fflush(stdout);
break;
default:
printf("Failed to enable BLE stack. Error code: %d\n", err_code);
fflush(stdout);
break;
}
return err_code;
}
/**@brief Set BLE option for the BLE role and connection bandwidth.
*
* @return NRF_SUCCESS on option set successfully, otherwise an error code.
*/
static uint32_t ble_options_set()
{
#if NRF_SD_BLE_API <= 3
ble_opt_t opt;
ble_common_opt_t common_opt;
common_opt.conn_bw.role = BLE_GAP_ROLE_CENTRAL;
common_opt.conn_bw.conn_bw.conn_bw_rx = BLE_CONN_BW_HIGH;
common_opt.conn_bw.conn_bw.conn_bw_tx = BLE_CONN_BW_HIGH;
opt.common_opt = common_opt;
return sd_ble_opt_set(m_adapter, BLE_COMMON_OPT_CONN_BW, &opt);
#else
return NRF_ERROR_NOT_SUPPORTED;
#endif
}
#if NRF_SD_BLE_API >= 5
uint32_t ble_cfg_set(uint8_t conn_cfg_tag)
{
const uint32_t ram_start = 0; // Value is not used by ble-driver
uint32_t error_code;
ble_cfg_t ble_cfg;
// Configure the connection roles.
memset(&ble_cfg, 0, sizeof(ble_cfg));
ble_cfg.gap_cfg.role_count_cfg.periph_role_count = 1;
ble_cfg.gap_cfg.role_count_cfg.central_role_count = 1;
ble_cfg.gap_cfg.role_count_cfg.central_sec_count = 1;
error_code = sd_ble_cfg_set(m_adapter, BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, ram_start);
if (error_code != NRF_SUCCESS)
{
printf("sd_ble_cfg_set() failed when attempting to set BLE_GAP_CFG_ROLE_COUNT. Error code: 0x%02X\n", error_code);
fflush(stdout);
return error_code;
}
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = 150;
error_code = sd_ble_cfg_set(m_adapter, BLE_CONN_CFG_GATT, &ble_cfg, ram_start);
if (error_code != NRF_SUCCESS)
{
printf("sd_ble_cfg_set() failed when attempting to set BLE_CONN_CFG_GATT. Error code: 0x%02X\n", error_code);
fflush(stdout);
return error_code;
}
return NRF_SUCCESS;
}
#endif
/**@brief Start scanning (GAP Discovery procedure, Observer Procedure).
* *
* @return NRF_SUCCESS on successfully initiating scanning procedure, otherwise an error code.
*/
static uint32_t scan_start()
{
uint32_t error_code = sd_ble_gap_scan_start(m_adapter, &m_scan_param);
if (error_code != NRF_SUCCESS)
{
printf("Scan start failed\n");
fflush(stdout);
} else
{
printf("Scan started\n");
fflush(stdout);
}
return error_code;
}
/**@brief Function called upon connecting to BLE peripheral.
*
* @details Initiates primary service discovery.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t service_discovery_start()
{
uint32_t err_code;
uint16_t start_handle = 0x01;
ble_uuid_t srvc_uuid;
printf("Discovering primary services\n");
fflush(stdout);
srvc_uuid.type = BLE_UUID_TYPE_BLE;
srvc_uuid.uuid = BLE_UUID_HEART_RATE_SERVICE;
// Initiate procedure to find the primary BLE_UUID_HEART_RATE_SERVICE.
err_code = sd_ble_gattc_primary_services_discover(m_adapter,
m_connection_handle, start_handle,
&srvc_uuid);
if (err_code != NRF_SUCCESS)
{
printf("Failed to initiate or continue a GATT Primary Service Discovery procedure\n");
fflush(stdout);
}
return err_code;
}
/**@brief Function called upon discovering a BLE peripheral's primary service(s).
*
* @details Initiates service's (m_service) characteristic discovery.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t char_discovery_start()
{
ble_gattc_handle_range_t handle_range;
printf("Discovering characteristics\n");
fflush(stdout);
handle_range.start_handle = m_service_start_handle;
handle_range.end_handle = m_service_end_handle;
return sd_ble_gattc_characteristics_discover(m_adapter, m_connection_handle, &handle_range);
}
/**@brief Function called upon discovering service's characteristics.
*
* @details Initiates heart rate monitor (m_hrm_char_handle) characteristic's descriptor discovery.
*
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t descr_discovery_start()
{
ble_gattc_handle_range_t handle_range;
printf("Discovering characteristic's descriptors\n");
fflush(stdout);
if (m_hrm_char_handle == 0)
{
printf("No heart rate measurement characteristic handle found\n");
fflush(stdout);
return NRF_ERROR_INVALID_STATE;
}
handle_range.start_handle = m_hrm_char_handle;
handle_range.end_handle = m_service_end_handle;
return sd_ble_gattc_descriptors_discover(m_adapter, m_connection_handle, &handle_range);
}
/**@brief Function that write's the HRM characteristic's CCCD.
* *
* @return NRF_SUCCESS on success, otherwise an error code.
*/
static uint32_t hrm_cccd_set(uint8_t value)
{
ble_gattc_write_params_t write_params;
uint8_t cccd_value[2] = {value, 0};
printf("Setting HRM CCCD\n");
fflush(stdout);
if (m_hrm_cccd_handle == 0)
{
printf("Error. No CCCD handle has been found\n");
fflush(stdout);
return NRF_ERROR_INVALID_STATE;
}
write_params.handle = m_hrm_cccd_handle;
write_params.len = 2;
write_params.p_value = cccd_value;
write_params.write_op = BLE_GATT_OP_WRITE_REQ;
write_params.offset = 0;
return sd_ble_gattc_write(m_adapter, m_connection_handle, &write_params);
}
/**@brief Function for handling the Application's BLE Stack events.
*
* @param[in] adapter The transport adapter.
* @param[in] p_ble_evt Bluetooth stack event.
*/
static void ble_evt_dispatch(adapter_t * adapter, ble_evt_t * p_ble_evt)
{
if (p_ble_evt == NULL)
{
printf("Received an empty BLE event\n");
fflush(stdout);
return;
}
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
on_connected(&(p_ble_evt->evt.gap_evt));
break;
case BLE_GAP_EVT_DISCONNECTED:
printf("Disconnected, reason: 0x%02X\n",
p_ble_evt->evt.gap_evt.params.disconnected.reason);
fflush(stdout);
m_connected_devices--;
m_connection_handle = 0;
break;
case BLE_GAP_EVT_ADV_REPORT:
on_adv_report(&(p_ble_evt->evt.gap_evt));
break;
case BLE_GAP_EVT_TIMEOUT:
on_timeout(&(p_ble_evt->evt.gap_evt));
break;
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
on_service_discovery_response(&(p_ble_evt->evt.gattc_evt));
break;
case BLE_GATTC_EVT_CHAR_DISC_RSP:
on_characteristic_discovery_response(&(p_ble_evt->evt.gattc_evt));
break;
case BLE_GATTC_EVT_DESC_DISC_RSP:
on_descriptor_discovery_response(&(p_ble_evt->evt.gattc_evt));
break;
case BLE_GATTC_EVT_WRITE_RSP:
on_write_response(&(p_ble_evt->evt.gattc_evt));
break;
case BLE_GATTC_EVT_HVX:
on_hvx(&(p_ble_evt->evt.gattc_evt));
break;
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
on_conn_params_update_request(&(p_ble_evt->evt.gap_evt));
break;
#if NRF_SD_BLE_API >= 3
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
on_exchange_mtu_request(&(p_ble_evt->evt.gatts_evt));
break;
case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
on_exchange_mtu_response(&(p_ble_evt->evt.gattc_evt));
break;
#endif
default:
printf("Received an un-handled event with ID: %d\n", p_ble_evt->header.evt_id);
fflush(stdout);
break;
}
}
/**@brief Function for application main entry.
*
* @param[in] argc Number of arguments (program expects 0 or 1 arguments).
* @param[in] argv The serial port and baud rate of the target nRF5 device (Optional).
*/
int main(int argc, char * argv[])
{
uint32_t error_code;
char * serial_port = DEFAULT_UART_PORT_NAME;
uint32_t baud_rate = DEFAULT_BAUD_RATE;
uint8_t cccd_value = 0;
if (argc > 2)
{
if (strcmp(argv[2], "1000000") == 0)
{
baud_rate = 1000000;
}
else if (strcmp(argv[2], "115200") == 0)
{
baud_rate = 115200;
}
else
{
printf("Supported baud rate values are: 115200, 1000000\n");
fflush(stdout);
}
}
if (argc > 1)
{
serial_port = argv[1];
}
printf("Serial port used: %s\n", serial_port);
printf("Baud rate used: %d\n", baud_rate);
fflush(stdout);
m_adapter = adapter_init(serial_port, baud_rate);
sd_rpc_log_handler_severity_filter_set(m_adapter, SD_RPC_LOG_INFO);
error_code = sd_rpc_open(m_adapter, status_handler, ble_evt_dispatch, log_handler);
if (error_code != NRF_SUCCESS)
{
printf("Failed to open nRF BLE Driver. Error code: 0x%02X\n", error_code);
fflush(stdout);
return error_code;
}
#if NRF_SD_BLE_API >= 5
ble_cfg_set(m_config_id);
#endif
error_code = ble_stack_init();
if (error_code != NRF_SUCCESS)
{
return error_code;
}
#if NRF_SD_BLE_API < 5
error_code = ble_options_set();
if (error_code != NRF_SUCCESS)
{
return error_code;
}
#endif
error_code = scan_start();
if (error_code != NRF_SUCCESS)
{
return error_code;
}
// Endlessly loop.
for (; ;)
{
char c = (char)getchar();
if (c == 'q' || c == 'Q')
{
error_code = sd_rpc_close(m_adapter);
if (error_code != NRF_SUCCESS)
{
printf("Failed to close nRF BLE Driver. Error code: 0x%02X\n", error_code);
fflush(stdout);
return error_code;
}
printf("Closed\n");
fflush(stdout);
return NRF_SUCCESS;
}
// Toggle notifications on the HRM characteristic every time user input is received.
cccd_value ^= BLE_CCCD_NOTIFY;
hrm_cccd_set(cccd_value);
}
}