1
1
/**
2
- * Copyright (c) 2014 - 2017 , Nordic Semiconductor ASA
2
+ * Copyright (c) 2014 - 2018 , Nordic Semiconductor ASA
3
3
*
4
4
* All rights reserved.
5
5
*
74
74
#include "sensorsim.h"
75
75
#include "ble_conn_state.h"
76
76
#include "nrf_ble_gatt.h"
77
+ #include "nrf_ble_qwr.h"
78
+ #include "nrf_pwr_mgmt.h"
77
79
78
80
#include "nrf_log.h"
79
81
#include "nrf_log_ctrl.h"
80
82
#include "nrf_log_default_backends.h"
81
83
#include "ble_cus.h"
82
84
83
- #define APP_FEATURE_NOT_SUPPORTED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 /**< Reply when unsupported features are requested. */
84
-
85
85
#define DEVICE_NAME "Nordic_Template" /**< Name of device. Will be included in the advertising data. */
86
86
#define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */
87
87
#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 187.5 ms). */
88
- #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout in units of seconds. */
89
88
90
- #define APP_BLE_OBSERVER_PRIO 1 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
89
+ #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
90
+ #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
91
91
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
92
92
93
93
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */
113
113
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
114
114
115
115
116
- NRF_BLE_GATT_DEF (m_gatt ); /**< GATT module instance. */
117
- BLE_CUS_DEF (m_cus );
116
+ NRF_BLE_GATT_DEF (m_gatt );
117
+ NRF_BLE_QWR_DEF (m_qwr ); /**< GATT module instance. */
118
+ BLE_CUS_DEF (m_cus ); /**< Context for the Queued Write module.*/
118
119
BLE_ADVERTISING_DEF (m_advertising ); /**< Advertising module instance. */
119
120
120
121
APP_TIMER_DEF (m_notification_timer_id );
@@ -198,7 +199,7 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
198
199
{
199
200
// Run garbage collection on the flash.
200
201
err_code = fds_gc ();
201
- if (err_code == FDS_ERR_BUSY || err_code == FDS_ERR_NO_SPACE_IN_QUEUES )
202
+ if (err_code == FDS_ERR_NO_SPACE_IN_QUEUES )
202
203
{
203
204
// Retry.
204
205
}
@@ -213,12 +214,6 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
213
214
advertising_start (false);
214
215
} break ;
215
216
216
- case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED :
217
- {
218
- // The local database has likely changed, send service changed indications.
219
- pm_local_database_has_changed ();
220
- } break ;
221
-
222
217
case PM_EVT_PEER_DATA_UPDATE_FAILED :
223
218
{
224
219
// Assert.
@@ -247,6 +242,8 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
247
242
case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED :
248
243
case PM_EVT_PEER_DELETE_SUCCEEDED :
249
244
case PM_EVT_LOCAL_DB_CACHE_APPLIED :
245
+ case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED :
246
+ // This can happen when the local DB has changed.
250
247
case PM_EVT_SERVICE_CHANGED_IND_SENT :
251
248
case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED :
252
249
default :
@@ -341,6 +338,19 @@ static void gatt_init(void)
341
338
}
342
339
343
340
341
+ /**@brief Function for handling Queued Write Module errors.
342
+ *
343
+ * @details A pointer to this function will be passed to each service which may need to inform the
344
+ * application about an error.
345
+ *
346
+ * @param[in] nrf_error Error code containing information about what went wrong.
347
+ */
348
+ static void nrf_qwr_error_handler (uint32_t nrf_error )
349
+ {
350
+ APP_ERROR_HANDLER (nrf_error );
351
+ }
352
+
353
+
344
354
/**@brief Function for handling the YYY Service events.
345
355
* YOUR_JOB implement a service handler function depending on the event the service you are using can generate
346
356
*
@@ -410,20 +420,48 @@ static void on_cus_evt(ble_cus_t * p_cus_service,
410
420
*/
411
421
static void services_init (void )
412
422
{
413
- /* YOUR_JOB: Add code to initialize the services used by the application.*/
414
- ret_code_t err_code ;
415
- ble_cus_init_t cus_init ;
423
+ ret_code_t err_code ;
424
+ nrf_ble_qwr_init_t qwr_init = {0 };
425
+ ble_cus_init_t cus_init = {0 };
426
+
427
+ // Initialize Queued Write Module.
428
+ qwr_init .error_handler = nrf_qwr_error_handler ;
429
+
430
+ err_code = nrf_ble_qwr_init (& m_qwr , & qwr_init );
431
+ APP_ERROR_CHECK (err_code );
416
432
417
433
// Initialize CUS Service init structure to zero.
418
- memset (& cus_init , 0 , sizeof (cus_init ));
419
434
cus_init .evt_handler = on_cus_evt ;
420
435
421
436
BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cus_init .custom_value_char_attr_md .cccd_write_perm );
422
437
BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cus_init .custom_value_char_attr_md .read_perm );
423
438
BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cus_init .custom_value_char_attr_md .write_perm );
424
439
425
440
err_code = ble_cus_init (& m_cus , & cus_init );
426
- APP_ERROR_CHECK (err_code );
441
+ APP_ERROR_CHECK (err_code );
442
+
443
+ /* YOUR_JOB: Add code to initialize the services used by the application.
444
+ ble_xxs_init_t xxs_init;
445
+ ble_yys_init_t yys_init;
446
+
447
+ // Initialize XXX Service.
448
+ memset(&xxs_init, 0, sizeof(xxs_init));
449
+
450
+ xxs_init.evt_handler = NULL;
451
+ xxs_init.is_xxx_notify_supported = true;
452
+ xxs_init.ble_xx_initial_value.level = 100;
453
+
454
+ err_code = ble_bas_init(&m_xxs, &xxs_init);
455
+ APP_ERROR_CHECK(err_code);
456
+
457
+ // Initialize YYY Service.
458
+ memset(&yys_init, 0, sizeof(yys_init));
459
+ yys_init.evt_handler = on_yys_evt;
460
+ yys_init.ble_yy_initial_value.counter = 0;
461
+
462
+ err_code = ble_yy_service_init(&yys_init, &yy_init);
463
+ APP_ERROR_CHECK(err_code);
464
+ */
427
465
}
428
466
429
467
@@ -556,18 +594,18 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
556
594
{
557
595
case BLE_GAP_EVT_DISCONNECTED :
558
596
NRF_LOG_INFO ("Disconnected." );
559
- err_code = bsp_indication_set (BSP_INDICATE_IDLE );
560
- APP_ERROR_CHECK (err_code );
597
+ // LED indication will be changed when advertising starts.
561
598
break ;
562
599
563
600
case BLE_GAP_EVT_CONNECTED :
564
601
NRF_LOG_INFO ("Connected." );
565
602
err_code = bsp_indication_set (BSP_INDICATE_CONNECTED );
566
603
APP_ERROR_CHECK (err_code );
567
604
m_conn_handle = p_ble_evt -> evt .gap_evt .conn_handle ;
605
+ err_code = nrf_ble_qwr_conn_handle_assign (& m_qwr , m_conn_handle );
606
+ APP_ERROR_CHECK (err_code );
568
607
break ;
569
608
570
- #if defined(S132 )
571
609
case BLE_GAP_EVT_PHY_UPDATE_REQUEST :
572
610
{
573
611
NRF_LOG_DEBUG ("PHY update request." );
@@ -579,7 +617,6 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
579
617
err_code = sd_ble_gap_phy_update (p_ble_evt -> evt .gap_evt .conn_handle , & phys );
580
618
APP_ERROR_CHECK (err_code );
581
619
} break ;
582
- #endif
583
620
584
621
case BLE_GATTC_EVT_TIMEOUT :
585
622
// Disconnect on GATT Client timeout event.
@@ -597,40 +634,6 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
597
634
APP_ERROR_CHECK (err_code );
598
635
break ;
599
636
600
- case BLE_EVT_USER_MEM_REQUEST :
601
- err_code = sd_ble_user_mem_reply (p_ble_evt -> evt .gattc_evt .conn_handle , NULL );
602
- APP_ERROR_CHECK (err_code );
603
- break ;
604
-
605
- case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST :
606
- {
607
- ble_gatts_evt_rw_authorize_request_t req ;
608
- ble_gatts_rw_authorize_reply_params_t auth_reply ;
609
-
610
- req = p_ble_evt -> evt .gatts_evt .params .authorize_request ;
611
-
612
- if (req .type != BLE_GATTS_AUTHORIZE_TYPE_INVALID )
613
- {
614
- if ((req .request .write .op == BLE_GATTS_OP_PREP_WRITE_REQ ) ||
615
- (req .request .write .op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW ) ||
616
- (req .request .write .op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL ))
617
- {
618
- if (req .type == BLE_GATTS_AUTHORIZE_TYPE_WRITE )
619
- {
620
- auth_reply .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE ;
621
- }
622
- else
623
- {
624
- auth_reply .type = BLE_GATTS_AUTHORIZE_TYPE_READ ;
625
- }
626
- auth_reply .params .write .gatt_status = APP_FEATURE_NOT_SUPPORTED ;
627
- err_code = sd_ble_gatts_rw_authorize_reply (p_ble_evt -> evt .gatts_evt .conn_handle ,
628
- & auth_reply );
629
- APP_ERROR_CHECK (err_code );
630
- }
631
- }
632
- } break ; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST
633
-
634
637
default :
635
638
// No implementation needed.
636
639
break ;
@@ -768,7 +771,7 @@ static void advertising_init(void)
768
771
769
772
init .config .ble_adv_fast_enabled = true;
770
773
init .config .ble_adv_fast_interval = APP_ADV_INTERVAL ;
771
- init .config .ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS ;
774
+ init .config .ble_adv_fast_timeout = APP_ADV_DURATION ;
772
775
773
776
init .evt_handler = on_adv_evt ;
774
777
@@ -778,6 +781,7 @@ static void advertising_init(void)
778
781
ble_advertising_conn_cfg_tag_set (& m_advertising , APP_BLE_CONN_CFG_TAG );
779
782
}
780
783
784
+
781
785
/**@brief Function for initializing buttons and leds.
782
786
*
783
787
* @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
@@ -787,7 +791,7 @@ static void buttons_leds_init(bool * p_erase_bonds)
787
791
ret_code_t err_code ;
788
792
bsp_event_t startup_event ;
789
793
790
- err_code = bsp_init (BSP_INIT_LED | BSP_INIT_BUTTONS , bsp_event_handler );
794
+ err_code = bsp_init (BSP_INIT_LEDS | BSP_INIT_BUTTONS , bsp_event_handler );
791
795
APP_ERROR_CHECK (err_code );
792
796
793
797
err_code = bsp_btn_ble_init (NULL , & startup_event );
@@ -808,23 +812,37 @@ static void log_init(void)
808
812
}
809
813
810
814
811
- /**@brief Function for the Power manager .
815
+ /**@brief Function for initializing power management .
812
816
*/
813
- static void power_manage (void )
817
+ static void power_management_init (void )
814
818
{
815
- ret_code_t err_code = sd_app_evt_wait ();
819
+ ret_code_t err_code ;
820
+ err_code = nrf_pwr_mgmt_init ();
816
821
APP_ERROR_CHECK (err_code );
817
822
}
818
823
819
824
825
+ /**@brief Function for handling the idle state (main loop).
826
+ *
827
+ * @details If there is no pending log operation, then sleep until next the next event occurs.
828
+ */
829
+ static void idle_state_handle (void )
830
+ {
831
+ if (NRF_LOG_PROCESS () == false)
832
+ {
833
+ nrf_pwr_mgmt_run ();
834
+ }
835
+ }
836
+
837
+
820
838
/**@brief Function for starting advertising.
821
839
*/
822
840
static void advertising_start (bool erase_bonds )
823
841
{
824
842
if (erase_bonds == true)
825
843
{
826
844
delete_bonds ();
827
- // Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED evetnt
845
+ // Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED event
828
846
}
829
847
else
830
848
{
@@ -840,10 +858,12 @@ static void advertising_start(bool erase_bonds)
840
858
int main (void )
841
859
{
842
860
bool erase_bonds ;
861
+
843
862
// Initialize.
844
863
log_init ();
845
864
timers_init ();
846
865
buttons_leds_init (& erase_bonds );
866
+ power_management_init ();
847
867
ble_stack_init ();
848
868
gap_params_init ();
849
869
gatt_init ();
@@ -861,10 +881,7 @@ int main(void)
861
881
// Enter main loop.
862
882
for (;;)
863
883
{
864
- if (NRF_LOG_PROCESS () == false)
865
- {
866
- power_manage ();
867
- }
884
+ idle_state_handle ();
868
885
}
869
886
}
870
887
0 commit comments