-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
781 lines (633 loc) · 26.5 KB
/
main.c
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
/* Copyright (c) 2014 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.
*
*/
/** @file
*
* @defgroup ble_sdk_app_template_main main.c
* @{
* @ingroup ble_sdk_app_template
* @brief Template project main file.
*
* This file contains a template for creating a new application. It has the code necessary to wakeup
* from button, advertise, get a connection restart advertising on disconnect and if no new
* connection created go back to system-off mode.
* It can easily be used as a starting point for creating a new application, the comments identified
* with 'YOUR_JOB' indicates where and how you can customize.
*/
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "ble.h"
#include "ble_hci.h"
#include "ble_srv_common.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "boards.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "device_manager.h"
#include "pstorage.h"
#include "app_trace.h"
#include "bsp.h"
#include "bsp_btn_ble.h"
#include "our_service.h"
#include "app_uart.h"
#include "nrf_delay.h"
/*UART buffer size. */
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 1
#define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/
#define CENTRAL_LINK_COUNT 0 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings*/
#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
#define DEVICE_NAME "SensorWeb" /**< Name of device. Will be included in the advertising data. */
#define MANUFACTURER_NAME "Mozilla" /**< Manufacturer. Will be passed to Device Information Service. */
#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 25 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout in units of seconds. */
#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(200, UNIT_1_25_MS) /**< Maximum acceptable connection interval (0.2 second). */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */
#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER)/**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
#define SEC_PARAM_BOND 1 /**< Perform bonding. */
#define SEC_PARAM_MITM 0 /**< Man In The Middle protection not required. */
#define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_NONE /**< No I/O capabilities. */
#define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */
#define SEC_PARAM_MIN_KEY_SIZE 7 /**< Minimum encryption key size. */
#define SEC_PARAM_MAX_KEY_SIZE 16 /**< Maximum encryption key size. */
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
static dm_application_instance_t m_app_handle; /**< Application identifier allocated by device manager */
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
// Storage for our various characteristics.
static uint8_t our_public_key[32];
static uint8_t client_public_key[32];
static uint8_t ssid[33]; // +1 for terminating null
static uint8_t password[65]; // +1 for terminating null
// FROM_SERVICE_TUTORIAL: Declare a service structure for our application
ble_os_t m_our_service = {
.conn_handle = BLE_CONN_HANDLE_INVALID,
.service_handle = 0,
.char_our_public_key = {
.uuid = 0x0001,
.write_only = false,
.value_len = sizeof(our_public_key),
.value = our_public_key,
},
.char_client_public_key = {
.uuid = 0x0002,
.write_only = true,
.value_len = sizeof(client_public_key),
.value = client_public_key,
},
.char_ssid = {
.uuid = 0x0003,
.write_only = true,
.value_len = sizeof(ssid),
.value = ssid,
},
.char_password = {
.uuid = 0x0004,
.write_only = true,
.value_len = sizeof(password),
.value = password,
},
};
/**@brief Callback function for asserts in the SoftDevice.
*
* @details This function will be called in case of an assert in the SoftDevice.
*
* @warning This handler is an example only and does not fit a final product. You need to analyze
* how your product is supposed to react in case of Assert.
* @warning On assert from the SoftDevice, the system can only recover on reset.
*
* @param[in] line_num Line number of the failing ASSERT call.
* @param[in] file_name File name of the failing ASSERT call.
*/
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
app_error_handler(DEAD_BEEF, line_num, p_file_name);
}
/**@brief Function for the Timer initialization.
*
* @details Initializes the timer module. This creates and starts application timers.
*/
static void timers_init(void)
{
// Initialize timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
}
/**@brief Function for the GAP initialization.
*
* @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
* device including the device name, appearance, and the preferred connection parameters.
*/
static void gap_params_init(void)
{
uint32_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *)DEVICE_NAME,
strlen(DEVICE_NAME));
APP_ERROR_CHECK(err_code);
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
// FROM_SERVICE_TUTORIAL: Add code to initialize the services used by the application.
our_service_init(&m_our_service);
}
/**@brief Function for handling the Connection Parameters Module.
*
* @details This function will be called for all events in the Connection Parameters Module which
* are passed to the application.
* @note All this function does is to disconnect. This could have been done by simply
* setting the disconnect_on_fail config parameter, but instead we use the event
* handler mechanism to demonstrate its use.
*
* @param[in] p_evt Event received from the Connection Parameters Module.
*/
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{
uint32_t err_code;
if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
{
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
APP_ERROR_CHECK(err_code);
}
}
/**@brief Function for handling a Connection Parameters error.
*
* @param[in] nrf_error Error code containing information about what went wrong.
*/
static void conn_params_error_handler(uint32_t nrf_error)
{
APP_ERROR_HANDLER(nrf_error);
}
/**@brief Function for initializing the Connection Parameters module.
*/
static void conn_params_init(void)
{
uint32_t err_code;
ble_conn_params_init_t cp_init;
memset(&cp_init, 0, sizeof(cp_init));
cp_init.p_conn_params = NULL;
cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
cp_init.disconnect_on_fail = false;
cp_init.evt_handler = on_conn_params_evt;
cp_init.error_handler = conn_params_error_handler;
err_code = ble_conn_params_init(&cp_init);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for starting timers.
*/
static void application_timers_start(void)
{
// OUR_JOB: Step 3.I, Start our timer
// To update temperature only when in a connection then don't call app_timer_start() here, but in on_ble_evt().
}
/**@brief Function for putting the chip into sleep mode.
*
* @note This function will not return.
*/
static void sleep_mode_enter(void)
{
uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
APP_ERROR_CHECK(err_code);
// Prepare wakeup buttons.
err_code = bsp_btn_ble_sleep_mode_prepare();
APP_ERROR_CHECK(err_code);
// Go to system-off mode (this function will not return; wakeup will cause a reset).
err_code = sd_power_system_off();
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling advertising events.
*
* @details This function will be called for advertising events which are passed to the application.
*
* @param[in] ble_adv_evt Advertising event.
*/
static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
uint32_t err_code;
switch (ble_adv_evt)
{
case BLE_ADV_EVT_FAST:
err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
APP_ERROR_CHECK(err_code);
break;
case BLE_ADV_EVT_IDLE:
sleep_mode_enter();
break;
default:
break;
}
}
/**@brief Function for handling the Application's BLE Stack events.
*
* @param[in] p_ble_evt Bluetooth stack event.
*/
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
uint32_t err_code;
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
printf("BLE_GAP_EVT_CONNECTED\r\n");
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
sd_ble_gap_rssi_start(m_conn_handle, 1, 0);
break;
case BLE_GAP_EVT_DISCONNECTED:
printf("BLE_GAP_EVT_DISCONNECTED\r\n");
m_conn_handle = BLE_CONN_HANDLE_INVALID;
break;
case BLE_GAP_EVT_RSSI_CHANGED:
printf("BLE_GAP_EVT_RSSI_CHANGED rssi = %d\r\n",
p_ble_evt->evt.gap_evt.params.rssi_changed.rssi);
// The highest RSSI that seems to be reported is -48. That means
// that the phone is within about a foot.
if (p_ble_evt->evt.gap_evt.params.rssi_changed.rssi < -48) {
printf("Disconnecting due to RSSI too weak\r\n");
if (m_conn_handle != BLE_CONN_HANDLE_INVALID) {
sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
}
}
break;
default:
// No implementation needed.
break;
}
}
/**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
*
* @details This function is called from the BLE Stack event interrupt handler after a BLE stack
* event has been received.
*
* @param[in] p_ble_evt Bluetooth stack event.
*/
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
dm_ble_evt_handler(p_ble_evt);
ble_conn_params_on_ble_evt(p_ble_evt);
bsp_btn_ble_on_ble_evt(p_ble_evt);
on_ble_evt(p_ble_evt);
ble_advertising_on_ble_evt(p_ble_evt);
// OUR_JOB: Step 3.C, Call ble_our_service_on_ble_evt() to do housekeeping of ble connections related to our service and characteristic
ble_our_service_on_ble_evt(&m_our_service, p_ble_evt);
}
/**@brief Function for dispatching a system event to interested modules.
*
* @details This function is called from the System event interrupt handler after a system
* event has been received.
*
* @param[in] sys_evt System stack event.
*/
static void sys_evt_dispatch(uint32_t sys_evt)
{
pstorage_sys_event_handler(sys_evt);
ble_advertising_on_sys_evt(sys_evt);
}
/**@brief Function for initializing the BLE stack.
*
* @details Initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(void)
{
uint32_t err_code;
nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
// Initialize the SoftDevice handler module.
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
ble_enable_params_t ble_enable_params;
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);
APP_ERROR_CHECK(err_code);
//Check the ram settings against the used number of links
CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
// Enable BLE stack.
//ble_enable_params.gatts_enable_params.attr_tab_size = 1024;
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
err_code = softdevice_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for handling events from the BSP module.
*
* @param[in] event Event generated by button press.
*/
void bsp_event_handler(bsp_event_t event)
{
uint32_t err_code;
switch (event)
{
case BSP_EVENT_SLEEP:
sleep_mode_enter();
break;
case BSP_EVENT_DISCONNECT:
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
break;
case BSP_EVENT_WHITELIST_OFF:
err_code = ble_advertising_restart_without_whitelist();
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
break;
default:
break;
}
}
/**@brief Function for handling the Device Manager events.
*
* @param[in] p_evt Data associated to the device manager event.
*/
static uint32_t device_manager_evt_handler(dm_handle_t const * p_handle,
dm_event_t const * p_event,
ret_code_t event_result)
{
APP_ERROR_CHECK(event_result);
#ifdef BLE_DFU_APP_SUPPORT
if (p_event->event_id == DM_EVT_LINK_SECURED)
{
app_context_load(p_handle);
}
#endif // BLE_DFU_APP_SUPPORT
return NRF_SUCCESS;
}
/**@brief Function for the Device Manager initialization.
*
* @param[in] erase_bonds Indicates whether bonding information should be cleared from
* persistent storage during initialization of the Device Manager.
*/
static void device_manager_init(bool erase_bonds)
{
uint32_t err_code;
dm_init_param_t init_param = {.clear_persistent_data = erase_bonds};
dm_application_param_t register_param;
// Initialize persistent storage module.
err_code = pstorage_init();
APP_ERROR_CHECK(err_code);
err_code = dm_init(&init_param);
APP_ERROR_CHECK(err_code);
memset(®ister_param.sec_param, 0, sizeof(ble_gap_sec_params_t));
register_param.sec_param.bond = SEC_PARAM_BOND;
register_param.sec_param.mitm = SEC_PARAM_MITM;
register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES;
register_param.sec_param.oob = SEC_PARAM_OOB;
register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
register_param.evt_handler = device_manager_evt_handler;
register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;
err_code = dm_register(&m_app_handle, ®ister_param);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing the Advertising functionality.
*/
static void advertising_init(void)
{
uint32_t err_code;
ble_advdata_t advdata;
// Build advertising data struct to pass into ble_advertising_init().
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
ble_adv_modes_config_t options = {0};
options.ble_adv_fast_enabled = BLE_ADV_FAST_ENABLED;
options.ble_adv_fast_interval = APP_ADV_INTERVAL;
options.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS;
// OUR_JOB: Create a scan response packet and include the list of UUIDs
err_code = ble_advertising_init(&advdata, NULL, &options, on_adv_evt, NULL);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for initializing buttons and leds.
*
* @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
*/
static void buttons_leds_init(bool * p_erase_bonds)
{
bsp_event_t startup_event;
uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,
APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
bsp_event_handler);
APP_ERROR_CHECK(err_code);
err_code = bsp_btn_ble_init(NULL, &startup_event);
APP_ERROR_CHECK(err_code);
*p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
}
/**@brief Function for the Power manager.
*/
static void power_manage(void)
{
uint32_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
}
/**
* @brief UART events handler.
*/
static void uart_events_handler(app_uart_evt_t * p_event)
{
switch (p_event->evt_type)
{
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
/**
* @brief UART initialization.
* Just the usual way. Nothing special here
*/
static void uart_config(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_events_handler,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
bool erase_bonds;
uart_config();
log_uart_init();
printf("\033[2J\033[;HCharacteristic Tutorial, Completed Code\r\n");
// Initialize.
timers_init();
buttons_leds_init(&erase_bonds);
ble_stack_init();
device_manager_init(erase_bonds);
gap_params_init();
services_init();
advertising_init();
conn_params_init();
#if 0
static uint8_t const aes_secret[16] = {
0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4,
0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37,
};
static char const msg[16] = "This is a test.";
nrf_ecb_hal_data_t ecb;
memcpy(ecb.key, aes_secret, sizeof(ecb.key));
memcpy(ecb.cleartext, msg, sizeof(ecb.cleartext));
memset(ecb.ciphertext, 0, sizeof(ecb.ciphertext));
err_code = sd_ecb_block_encrypt(&ecb);
printf("sd_ecb_block_encrypt returned %lu\r\n", err_code);
printhex("aes secret", ecb.key, sizeof(ecb.key));
printhex(" cleartext", ecb.cleartext, sizeof(ecb.cleartext));
printhex("ciphertext", ecb.ciphertext, sizeof(ecb.ciphertext));
memcpy(ecb.cleartext, ecb.ciphertext, sizeof(ecb.cleartext));
memset(ecb.ciphertext, 0, sizeof(ecb.ciphertext));
err_code = sd_ecb_block_encrypt(&ecb);
printf("sd_ecb_block_encrypt returned %lu\r\n", err_code);
printhex("aes secret", ecb.key, sizeof(ecb.key));
printhex(" cleartext", ecb.cleartext, sizeof(ecb.cleartext));
printhex("ciphertext", ecb.ciphertext, sizeof(ecb.ciphertext));
//nrf_delay_ms(1000);
#endif
#if 0
// Start the random number generator. We don't initialise a noise
// source here because we don't need one for testing purposes.
// Real applications should of course use a proper noise source.
RNG.begin("TestBLE 1.0", 950);
#if 1
uint8_t servicePrivateKey[32] = {0x68, 0xD6, 0x31, 0x9D, 0xE5, 0x0E, 0xE1, 0x08,
0x33, 0x20, 0xC8, 0xFC, 0x1E, 0x1E, 0xB1, 0x8E,
0x54, 0x4C, 0x80, 0x87, 0x24, 0x03, 0xE7, 0x43,
0x72, 0xC0, 0x49, 0x2F, 0xDD, 0xF8, 0x75, 0x43};
uint8_t servicePublicKey[32] = {0xD9, 0x2D, 0x7A, 0x3F, 0xEE, 0x2F, 0x67, 0x5F,
0x47, 0x5F, 0xC4, 0x52, 0xA6, 0x81, 0xA9, 0x25,
0x5C, 0x21, 0x50, 0x55, 0x37, 0x02, 0xE5, 0xF1,
0x27, 0xD8, 0x37, 0x62, 0x3D, 0x4C, 0x81, 0x35};
#else
uint8_t servicePrivateKey[32];
uint8_t servicePublicKey[32];
Curve25519::dh1(servicePublicKey, servicePrivateKey);
#endif
printhex("Service Private Key", servicePrivateKey, 32);
printhex("Service Public Key", servicePublicKey, 32);
#if 1
uint8_t clientPrivateKey[32] = {0xC0, 0xA9, 0xB3, 0x04, 0xB1, 0xFB, 0x77, 0xCC,
0xA7, 0xFB, 0xD3, 0xD8, 0x8D, 0x0D, 0x1A, 0xB2,
0x7D, 0xFA, 0x2D, 0x53, 0xD9, 0x03, 0x29, 0x78,
0x25, 0x8C, 0x81, 0x77, 0x2C, 0xB4, 0x7E, 0x49};
uint8_t clientPublicKey[32] = {0x5C, 0xFF, 0xD4, 0xB3, 0xE8, 0x39, 0x1E, 0x6E,
0xC6, 0x30, 0x70, 0xC1, 0xA5, 0x5C, 0xB2, 0x82,
0xBD, 0x6E, 0x3C, 0x7D, 0x28, 0xBF, 0xA2, 0x7B,
0x09, 0x5E, 0xE6, 0x9E, 0xA4, 0x26, 0xB2, 0x6D};
#else
uint8_t clientPrivateKey[32];
uint8_t clientPublicKey[32];
Curve25519::dh1(clientPublicKey, clientPrivateKey);
#endif
nrf_delay_ms(1000);
printhex("Client Private Key", clientPrivateKey, 32);
printhex("Client Public Key", clientPublicKey, 32);
uint8_t serviceSharedSecret[32];
uint8_t clientSharedSecret[32];
memcpy(clientSharedSecret, clientPublicKey, sizeof(clientSharedSecret));
Curve25519::dh2(clientSharedSecret, servicePrivateKey);
memcpy(serviceSharedSecret, servicePublicKey, sizeof(serviceSharedSecret));
Curve25519::dh2(serviceSharedSecret, clientPrivateKey);
printhex("Client Shared Secret", clientSharedSecret, 32);
printhex("Service Shared Secret", serviceSharedSecret, 32);
uint8_t ssid[32];
uint8_t password[64];
//RNG.rand(ssid, sizeof(ssid));
//RNG.rand(password, sizeof(password));
memset(ssid, 0xAA, sizeof(ssid));
strcpy((char *)ssid, "My-SSID");
memset(password, 0xAA, sizeof(password));
strcpy((char *)password, "password");
AES128 aes128;
aes128.setKey(clientSharedSecret, 16);
uint8_t ssid_enc[32];
uint8_t password_enc[32];
aes128.encryptBlock(ssid_enc, ssid);
aes128.encryptBlock(password_enc, password);
uint8_t ssid_dec[32];
uint8_t password_dec[32];
#if 1
aes128_decrypt(clientSharedSecret, ssid_dec, ssid_enc, 16);
aes128_decrypt(clientSharedSecret, password_dec, password_enc, 16);
#else
aes128.decryptBlock(ssid_dec, ssid_enc);
aes128.decryptBlock(password_dec, password_enc);
#endif
printhex("SSID org", ssid, 16);
printhex("SSID dec", ssid_dec, 16);
printhex("SSID enc", ssid_enc, 16);
printhex("password org", password, 16);
printhex("password dec", password_dec, 16);
printhex("password enc", password_enc, 16);
printf("SSID = '%s' password = '%s'\r\n", (const char *)ssid_dec, (const char *)password_dec);
#endif
// Start execution.
application_timers_start();
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
power_manage();
}
}
/**
* @}
*/