Skip to content

Commit c48cdf6

Browse files
committed
Updated example to be SDK v15.0.0 compatible (Keil, GCC and SES)
1 parent 847a722 commit c48cdf6

10 files changed

+10789
-3578
lines changed

main.c

+84-67
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
2+
* Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
33
*
44
* All rights reserved.
55
*
@@ -74,20 +74,20 @@
7474
#include "sensorsim.h"
7575
#include "ble_conn_state.h"
7676
#include "nrf_ble_gatt.h"
77+
#include "nrf_ble_qwr.h"
78+
#include "nrf_pwr_mgmt.h"
7779

7880
#include "nrf_log.h"
7981
#include "nrf_log_ctrl.h"
8082
#include "nrf_log_default_backends.h"
8183
#include "ble_cus.h"
8284

83-
#define APP_FEATURE_NOT_SUPPORTED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 /**< Reply when unsupported features are requested. */
84-
8585
#define DEVICE_NAME "Nordic_Template" /**< Name of device. Will be included in the advertising data. */
8686
#define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */
8787
#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. */
8988

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. */
9191
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
9292

9393
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */
@@ -113,8 +113,9 @@
113113
#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
114114

115115

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.*/
118119
BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
119120

120121
APP_TIMER_DEF(m_notification_timer_id);
@@ -198,7 +199,7 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
198199
{
199200
// Run garbage collection on the flash.
200201
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)
202203
{
203204
// Retry.
204205
}
@@ -213,12 +214,6 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
213214
advertising_start(false);
214215
} break;
215216

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-
222217
case PM_EVT_PEER_DATA_UPDATE_FAILED:
223218
{
224219
// Assert.
@@ -247,6 +242,8 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
247242
case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
248243
case PM_EVT_PEER_DELETE_SUCCEEDED:
249244
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.
250247
case PM_EVT_SERVICE_CHANGED_IND_SENT:
251248
case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
252249
default:
@@ -341,6 +338,19 @@ static void gatt_init(void)
341338
}
342339

343340

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+
344354
/**@brief Function for handling the YYY Service events.
345355
* YOUR_JOB implement a service handler function depending on the event the service you are using can generate
346356
*
@@ -410,20 +420,48 @@ static void on_cus_evt(ble_cus_t * p_cus_service,
410420
*/
411421
static void services_init(void)
412422
{
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);
416432

417433
// Initialize CUS Service init structure to zero.
418-
memset(&cus_init, 0, sizeof(cus_init));
419434
cus_init.evt_handler = on_cus_evt;
420435

421436
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.cccd_write_perm);
422437
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.read_perm);
423438
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cus_init.custom_value_char_attr_md.write_perm);
424439

425440
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+
*/
427465
}
428466

429467

@@ -556,18 +594,18 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
556594
{
557595
case BLE_GAP_EVT_DISCONNECTED:
558596
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.
561598
break;
562599

563600
case BLE_GAP_EVT_CONNECTED:
564601
NRF_LOG_INFO("Connected.");
565602
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
566603
APP_ERROR_CHECK(err_code);
567604
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);
568607
break;
569608

570-
#if defined(S132)
571609
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
572610
{
573611
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)
579617
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
580618
APP_ERROR_CHECK(err_code);
581619
} break;
582-
#endif
583620

584621
case BLE_GATTC_EVT_TIMEOUT:
585622
// 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)
597634
APP_ERROR_CHECK(err_code);
598635
break;
599636

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-
634637
default:
635638
// No implementation needed.
636639
break;
@@ -768,7 +771,7 @@ static void advertising_init(void)
768771

769772
init.config.ble_adv_fast_enabled = true;
770773
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;
772775

773776
init.evt_handler = on_adv_evt;
774777

@@ -778,6 +781,7 @@ static void advertising_init(void)
778781
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
779782
}
780783

784+
781785
/**@brief Function for initializing buttons and leds.
782786
*
783787
* @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)
787791
ret_code_t err_code;
788792
bsp_event_t startup_event;
789793

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);
791795
APP_ERROR_CHECK(err_code);
792796

793797
err_code = bsp_btn_ble_init(NULL, &startup_event);
@@ -808,23 +812,37 @@ static void log_init(void)
808812
}
809813

810814

811-
/**@brief Function for the Power manager.
815+
/**@brief Function for initializing power management.
812816
*/
813-
static void power_manage(void)
817+
static void power_management_init(void)
814818
{
815-
ret_code_t err_code = sd_app_evt_wait();
819+
ret_code_t err_code;
820+
err_code = nrf_pwr_mgmt_init();
816821
APP_ERROR_CHECK(err_code);
817822
}
818823

819824

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+
820838
/**@brief Function for starting advertising.
821839
*/
822840
static void advertising_start(bool erase_bonds)
823841
{
824842
if (erase_bonds == true)
825843
{
826844
delete_bonds();
827-
// Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED evetnt
845+
// Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED event
828846
}
829847
else
830848
{
@@ -840,10 +858,12 @@ static void advertising_start(bool erase_bonds)
840858
int main(void)
841859
{
842860
bool erase_bonds;
861+
843862
// Initialize.
844863
log_init();
845864
timers_init();
846865
buttons_leds_init(&erase_bonds);
866+
power_management_init();
847867
ble_stack_init();
848868
gap_params_init();
849869
gatt_init();
@@ -861,10 +881,7 @@ int main(void)
861881
// Enter main loop.
862882
for (;;)
863883
{
864-
if (NRF_LOG_PROCESS() == false)
865-
{
866-
power_manage();
867-
}
884+
idle_state_handle();
868885
}
869886
}
870887

0 commit comments

Comments
 (0)