Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ble_app_hrs.c File Reference
#include <stdlib.h>
#include "app_profile.h"
#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_bas.h"
#include "ble_hrs.h"
#include "ble_dis.h"
#include "ble_conn_params.h"
#include "boards.h"
#include "sensorsim.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "device_manager.h"
#include "pstorage.h"
#include "app_trace.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "bsp_btn_ble.h"
#include "app_gpiote.h"

Go to the source code of this file.

Macros

#define IS_SRVC_CHANGED_CHARACT_PRESENT   1
 
#define DEVICE_NAME   "Nordic_HRM"
 
#define MANUFACTURER_NAME   "NordicSemiconductor"
 
#define APP_ADV_INTERVAL   300
 
#define APP_ADV_TIMEOUT_IN_SECONDS   180
 
#define APP_TIMER_PRESCALER   0
 
#define APP_PROFILE_MAX_TIMERS   (4+BSP_APP_TIMERS_NUMBER)
 
#define APP_TIMER_OP_QUEUE_SIZE   4
 
#define BATTERY_LEVEL_MEAS_INTERVAL   APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER)
 
#define MIN_BATTERY_LEVEL   81
 
#define MAX_BATTERY_LEVEL   100
 
#define BATTERY_LEVEL_INCREMENT   1
 
#define HEART_RATE_MEAS_INTERVAL   APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)
 
#define MIN_HEART_RATE   140
 
#define MAX_HEART_RATE   300
 
#define HEART_RATE_INCREMENT   10
 
#define RR_INTERVAL_INTERVAL   APP_TIMER_TICKS(300, APP_TIMER_PRESCALER)
 
#define MIN_RR_INTERVAL   100
 
#define MAX_RR_INTERVAL   500
 
#define RR_INTERVAL_INCREMENT   1
 
#define SENSOR_CONTACT_DETECTED_INTERVAL   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)
 
#define MIN_CONN_INTERVAL   MSEC_TO_UNITS(400, UNIT_1_25_MS)
 
#define MAX_CONN_INTERVAL   MSEC_TO_UNITS(650, UNIT_1_25_MS)
 
#define SLAVE_LATENCY   0
 
#define CONN_SUP_TIMEOUT   MSEC_TO_UNITS(4000, UNIT_10_MS)
 
#define FIRST_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)
 
#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER)
 
#define MAX_CONN_PARAMS_UPDATE_COUNT   3
 
#define SEC_PARAM_BOND   1
 
#define SEC_PARAM_MITM   0
 
#define SEC_PARAM_IO_CAPABILITIES   BLE_GAP_IO_CAPS_NONE
 
#define SEC_PARAM_OOB   0
 
#define SEC_PARAM_MIN_KEY_SIZE   7
 
#define SEC_PARAM_MAX_KEY_SIZE   16
 
#define DEAD_BEEF   0xDEADBEEF
 

Functions

 APP_TIMER_DEF (m_battery_timer_id)
 
 APP_TIMER_DEF (m_heart_rate_timer_id)
 
 APP_TIMER_DEF (m_rr_interval_timer_id)
 
 APP_TIMER_DEF (m_sensor_contact_timer_id)
 
static void battery_level_update (void)
 Function for performing battery measurement and updating the Battery Level characteristic in Battery Service.
 
static void battery_level_meas_timeout_handler (void *p_context)
 Function for handling the Battery measurement timer timeout. More...
 
static void heart_rate_meas_timeout_handler (void *p_context)
 Function for handling the Heart rate measurement timer timeout. More...
 
static void rr_interval_timeout_handler (void *p_context)
 Function for handling the RR interval timer timeout. More...
 
static void sensor_contact_detected_timeout_handler (void *p_context)
 Function for handling the Sensor Contact Detected timer timeout. More...
 
static void timers_init (void)
 Function for the Timer initialization. More...
 
static void gap_params_init (void)
 Function for the GAP initialization. More...
 
static void advertising_stop (void)
 Function for stopping advertising.
 
static void reset_prepare (void)
 Function for preparing for system reset.
 
static void services_init (void)
 Function for initializing services that will be used by the application. More...
 
static void sensor_simulator_init (void)
 Function for initializing the sensor simulators.
 
static void application_timers_start (void)
 Function for starting application timers.
 
static void on_conn_params_evt (ble_conn_params_evt_t *p_evt)
 Function for handling the Connection Parameters Module. More...
 
static void conn_params_error_handler (uint32_t nrf_error)
 Function for handling a Connection Parameters error. More...
 
static void conn_params_init (void)
 Function for initializing the Connection Parameters module.
 
static void sleep_mode_enter (void)
 Function for putting the chip into sleep mode. More...
 
static void on_adv_evt (ble_adv_evt_t ble_adv_evt)
 Function for handling advertising events. More...
 
static void on_ble_evt (ble_evt_t *p_ble_evt)
 Function for handling the Application's BLE Stack events. More...
 
static void ble_evt_dispatch (ble_evt_t *p_ble_evt)
 Function for dispatching a BLE stack event to all modules with a BLE stack event handler. More...
 
static void sys_evt_dispatch (uint32_t sys_evt)
 Function for dispatching a system event to interested modules. More...
 
void bsp_event_handler (bsp_event_t event)
 Function for handling events from the BSP module. More...
 
static uint32_t device_manager_evt_handler (dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
 Function for handling the Device Manager events. More...
 
static void device_manager_init (bool erase_bonds)
 Function for the Device Manager initialization. More...
 
static void advertising_init (void)
 Function for initializing the Advertising functionality.
 
void app_profile_init (void)
 Initialize profile. Is only called once.
 
void app_profile_on_ble_evt (ble_evt_t *p_ble_evt)
 Handle BLE event. More...
 
void app_profile_on_sys_evt (uint32_t sys_evt)
 Handle system event. More...
 
void app_profile_start (void)
 Start profile. Is called every time profile is activated.
 
void app_profile_stop (void)
 Stop profile. Note: Needs to set the softdevice in a state where it is ok to initialize PRU profile.
 

Variables

static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID
 
static ble_bas_t m_bas
 
static ble_hrs_t m_hrs
 
static bool m_rr_interval_enabled = true
 
static sensorsim_cfg_t m_battery_sim_cfg
 
static sensorsim_state_t m_battery_sim_state
 
static sensorsim_cfg_t m_heart_rate_sim_cfg
 
static sensorsim_state_t m_heart_rate_sim_state
 
static sensorsim_cfg_t m_rr_interval_sim_cfg
 
static sensorsim_state_t m_rr_interval_sim_state
 
static dm_application_instance_t m_app_handle
 
static ble_uuid_t m_adv_uuids []
 

Macro Definition Documentation

#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

Definition at line 43 of file ble_app_hrs.c.

#define DEVICE_NAME   "Nordic_HRM"

Name of device. Will be included in the advertising data.

Definition at line 45 of file ble_app_hrs.c.

#define MANUFACTURER_NAME   "NordicSemiconductor"

Manufacturer. Will be passed to Device Information Service.

Definition at line 46 of file ble_app_hrs.c.

#define APP_ADV_INTERVAL   300

The advertising interval (in units of 0.625 ms. This value corresponds to 25 ms).

Definition at line 47 of file ble_app_hrs.c.

#define APP_ADV_TIMEOUT_IN_SECONDS   180

The advertising timeout in units of seconds.

Definition at line 48 of file ble_app_hrs.c.

#define APP_TIMER_PRESCALER   0

Value of the RTC1 PRESCALER register.

Definition at line 50 of file ble_app_hrs.c.

#define APP_PROFILE_MAX_TIMERS   (4+BSP_APP_TIMERS_NUMBER)

Maximum number of simultaneously created timers.

Definition at line 51 of file ble_app_hrs.c.

#define APP_TIMER_OP_QUEUE_SIZE   4

Size of timer operation queues.

Definition at line 52 of file ble_app_hrs.c.

#define BATTERY_LEVEL_MEAS_INTERVAL   APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER)

Battery level measurement interval (ticks).

Definition at line 54 of file ble_app_hrs.c.

#define MIN_BATTERY_LEVEL   81

Minimum simulated battery level.

Definition at line 55 of file ble_app_hrs.c.

#define MAX_BATTERY_LEVEL   100

Maximum simulated battery level.

Definition at line 56 of file ble_app_hrs.c.

#define BATTERY_LEVEL_INCREMENT   1

Increment between each simulated battery level measurement.

Definition at line 57 of file ble_app_hrs.c.

#define HEART_RATE_MEAS_INTERVAL   APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)

Heart rate measurement interval (ticks).

Definition at line 59 of file ble_app_hrs.c.

#define MIN_HEART_RATE   140

Minimum heart rate as returned by the simulated measurement function.

Definition at line 60 of file ble_app_hrs.c.

#define MAX_HEART_RATE   300

Maximum heart rate as returned by the simulated measurement function.

Definition at line 61 of file ble_app_hrs.c.

#define HEART_RATE_INCREMENT   10

Value by which the heart rate is incremented/decremented for each call to the simulated measurement function.

Definition at line 62 of file ble_app_hrs.c.

#define RR_INTERVAL_INTERVAL   APP_TIMER_TICKS(300, APP_TIMER_PRESCALER)

RR interval interval (ticks).

Definition at line 64 of file ble_app_hrs.c.

#define MIN_RR_INTERVAL   100

Minimum RR interval as returned by the simulated measurement function.

Definition at line 65 of file ble_app_hrs.c.

#define MAX_RR_INTERVAL   500

Maximum RR interval as returned by the simulated measurement function.

Definition at line 66 of file ble_app_hrs.c.

#define RR_INTERVAL_INCREMENT   1

Value by which the RR interval is incremented/decremented for each call to the simulated measurement function.

Definition at line 67 of file ble_app_hrs.c.

#define SENSOR_CONTACT_DETECTED_INTERVAL   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)

Sensor Contact Detected toggle interval (ticks).

Definition at line 69 of file ble_app_hrs.c.

#define MIN_CONN_INTERVAL   MSEC_TO_UNITS(400, UNIT_1_25_MS)

Minimum acceptable connection interval (0.4 seconds).

Definition at line 71 of file ble_app_hrs.c.

#define MAX_CONN_INTERVAL   MSEC_TO_UNITS(650, UNIT_1_25_MS)

Maximum acceptable connection interval (0.65 second).

Definition at line 72 of file ble_app_hrs.c.

#define SLAVE_LATENCY   0

Slave latency.

Definition at line 73 of file ble_app_hrs.c.

#define CONN_SUP_TIMEOUT   MSEC_TO_UNITS(4000, UNIT_10_MS)

Connection supervisory timeout (4 seconds).

Definition at line 74 of file ble_app_hrs.c.

#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).

Definition at line 76 of file ble_app_hrs.c.

#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).

Definition at line 77 of file ble_app_hrs.c.

#define MAX_CONN_PARAMS_UPDATE_COUNT   3

Number of attempts before giving up the connection parameter negotiation.

Definition at line 78 of file ble_app_hrs.c.

#define SEC_PARAM_BOND   1

Perform bonding.

Definition at line 80 of file ble_app_hrs.c.

#define SEC_PARAM_MITM   0

Man In The Middle protection not required.

Definition at line 81 of file ble_app_hrs.c.

#define SEC_PARAM_IO_CAPABILITIES   BLE_GAP_IO_CAPS_NONE

No I/O capabilities.

Definition at line 82 of file ble_app_hrs.c.

#define SEC_PARAM_OOB   0

Out Of Band data not available.

Definition at line 83 of file ble_app_hrs.c.

#define SEC_PARAM_MIN_KEY_SIZE   7

Minimum encryption key size.

Definition at line 84 of file ble_app_hrs.c.

#define SEC_PARAM_MAX_KEY_SIZE   16

Maximum encryption key size.

Definition at line 85 of file ble_app_hrs.c.

#define DEAD_BEEF   0xDEADBEEF

Value used as error code on stack dump, can be used to identify stack location on stack unwind.

Definition at line 87 of file ble_app_hrs.c.

Function Documentation

APP_TIMER_DEF ( m_battery_timer_id  )

Battery timer.

APP_TIMER_DEF ( m_heart_rate_timer_id  )

Heart rate measurement timer.

APP_TIMER_DEF ( m_rr_interval_timer_id  )

RR interval timer.

APP_TIMER_DEF ( m_sensor_contact_timer_id  )

Sensor contact detected timer.

static void battery_level_meas_timeout_handler ( void *  p_context)
static

Function for handling the Battery measurement timer timeout.

This function will be called each time the battery level measurement timer expires.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.

Definition at line 141 of file ble_app_hrs.c.

142 {
143  UNUSED_PARAMETER(p_context);
145 }
static void battery_level_update(void)
Function for performing battery measurement and updating the Battery Level characteristic in Battery ...
Definition: ble_app_hrs.c:115
static void heart_rate_meas_timeout_handler ( void *  p_context)
static

Function for handling the Heart rate measurement timer timeout.

This function will be called each time the heart rate measurement timer expires. It will exclude RR Interval data from every third measurement.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.

Definition at line 156 of file ble_app_hrs.c.

157 {
158  static uint32_t cnt = 0;
159  uint32_t err_code;
160  uint16_t heart_rate;
161 
162  UNUSED_PARAMETER(p_context);
163 
164  heart_rate = (uint16_t)sensorsim_measure(&m_heart_rate_sim_state, &m_heart_rate_sim_cfg);
165 
166  cnt++;
167  err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, heart_rate);
168  if ((err_code != NRF_SUCCESS) &&
169  (err_code != NRF_ERROR_INVALID_STATE) &&
170  (err_code != BLE_ERROR_NO_TX_PACKETS) &&
171  (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
172  )
173  {
174  APP_ERROR_HANDLER(err_code);
175  }
176 
177  // Disable RR Interval recording every third heart rate measurement.
178  // NOTE: An application will normally not do this. It is done here just for testing generation
179  // of messages without RR Interval measurements.
180  m_rr_interval_enabled = ((cnt % 3) != 0);
181 }
static bool m_rr_interval_enabled
Definition: ble_app_hrs.c:92
static ble_hrs_t m_hrs
Definition: ble_app_hrs.c:91
static sensorsim_cfg_t m_heart_rate_sim_cfg
Definition: ble_app_hrs.c:96
static sensorsim_state_t m_heart_rate_sim_state
Definition: ble_app_hrs.c:97
static void rr_interval_timeout_handler ( void *  p_context)
static

Function for handling the RR interval timer timeout.

This function will be called each time the RR interval timer expires.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.

Definition at line 191 of file ble_app_hrs.c.

192 {
193  UNUSED_PARAMETER(p_context);
194 
196  {
197  uint16_t rr_interval;
198 
199  rr_interval = (uint16_t)sensorsim_measure(&m_rr_interval_sim_state,
201  ble_hrs_rr_interval_add(&m_hrs, rr_interval);
202  }
203 }
static sensorsim_cfg_t m_rr_interval_sim_cfg
Definition: ble_app_hrs.c:98
static bool m_rr_interval_enabled
Definition: ble_app_hrs.c:92
static sensorsim_state_t m_rr_interval_sim_state
Definition: ble_app_hrs.c:99
static ble_hrs_t m_hrs
Definition: ble_app_hrs.c:91
static void sensor_contact_detected_timeout_handler ( void *  p_context)
static

Function for handling the Sensor Contact Detected timer timeout.

This function will be called each time the Sensor Contact Detected timer expires.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.

Definition at line 213 of file ble_app_hrs.c.

214 {
215  static bool sensor_contact_detected = false;
216 
217  UNUSED_PARAMETER(p_context);
218 
219  sensor_contact_detected = !sensor_contact_detected;
220  ble_hrs_sensor_contact_detected_update(&m_hrs, sensor_contact_detected);
221 }
static ble_hrs_t m_hrs
Definition: ble_app_hrs.c:91
static void timers_init ( void  )
static

Function for the Timer initialization.

Initializes the timer module. This creates and starts application timers.

Definition at line 228 of file ble_app_hrs.c.

229 {
230  uint32_t err_code;
231 
232  // Create timers.
233  err_code = app_timer_create(&m_battery_timer_id,
234  APP_TIMER_MODE_REPEATED,
236  APP_ERROR_CHECK(err_code);
237 
238  err_code = app_timer_create(&m_heart_rate_timer_id,
239  APP_TIMER_MODE_REPEATED,
241  APP_ERROR_CHECK(err_code);
242 
243  err_code = app_timer_create(&m_rr_interval_timer_id,
244  APP_TIMER_MODE_REPEATED,
246  APP_ERROR_CHECK(err_code);
247 
248  err_code = app_timer_create(&m_sensor_contact_timer_id,
249  APP_TIMER_MODE_REPEATED,
251  APP_ERROR_CHECK(err_code);
252 }
static void sensor_contact_detected_timeout_handler(void *p_context)
Function for handling the Sensor Contact Detected timer timeout.
Definition: ble_app_hrs.c:213
static void rr_interval_timeout_handler(void *p_context)
Function for handling the RR interval timer timeout.
Definition: ble_app_hrs.c:191
static void battery_level_meas_timeout_handler(void *p_context)
Function for handling the Battery measurement timer timeout.
Definition: ble_app_hrs.c:141
static void heart_rate_meas_timeout_handler(void *p_context)
Function for handling the Heart rate measurement timer timeout.
Definition: ble_app_hrs.c:156
static void gap_params_init ( void  )
static

Function for the GAP initialization.

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.

Definition at line 260 of file ble_app_hrs.c.

261 {
262  uint32_t err_code;
263  ble_gap_conn_params_t gap_conn_params;
264  ble_gap_conn_sec_mode_t sec_mode;
265 
266  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
267 
268  err_code = sd_ble_gap_device_name_set(&sec_mode,
269  (const uint8_t *)DEVICE_NAME,
270  strlen(DEVICE_NAME));
271  APP_ERROR_CHECK(err_code);
272 
273  err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT);
274  APP_ERROR_CHECK(err_code);
275 
276  memset(&gap_conn_params, 0, sizeof(gap_conn_params));
277 
278  gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
279  gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
280  gap_conn_params.slave_latency = SLAVE_LATENCY;
281  gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
282 
283  err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
284  APP_ERROR_CHECK(err_code);
285 }
#define DEVICE_NAME
Definition: ble_app_hrs.c:45
#define MAX_CONN_INTERVAL
Definition: ble_app_hrs.c:72
#define SLAVE_LATENCY
Definition: ble_app_hrs.c:73
#define MIN_CONN_INTERVAL
Definition: ble_app_hrs.c:71
#define CONN_SUP_TIMEOUT
Definition: ble_app_hrs.c:74
static void services_init ( void  )
static

Function for initializing services that will be used by the application.

Initialize the Heart Rate, Battery and Device Information services.

Definition at line 332 of file ble_app_hrs.c.

333 {
334  uint32_t err_code;
335  ble_hrs_init_t hrs_init;
336  ble_bas_init_t bas_init;
337  ble_dis_init_t dis_init;
338  uint8_t body_sensor_location;
339 
340  // Initialize Heart Rate Service.
341  body_sensor_location = BLE_HRS_BODY_SENSOR_LOCATION_FINGER;
342 
343  memset(&hrs_init, 0, sizeof(hrs_init));
344 
345  hrs_init.evt_handler = NULL;
346  hrs_init.is_sensor_contact_supported = true;
347  hrs_init.p_body_sensor_location = &body_sensor_location;
348 
349  // Here the sec level for the Heart Rate Service can be changed/increased.
350  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_hrm_attr_md.cccd_write_perm);
351  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.read_perm);
352  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.write_perm);
353 
354  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_init.hrs_bsl_attr_md.read_perm);
355  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_bsl_attr_md.write_perm);
356 
357  err_code = ble_hrs_init(&m_hrs, &hrs_init);
358  APP_ERROR_CHECK(err_code);
359 
360  // Initialize Battery Service.
361  memset(&bas_init, 0, sizeof(bas_init));
362 
363  // Here the sec level for the Battery Service can be changed/increased.
364  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.cccd_write_perm);
365  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_char_attr_md.read_perm);
366  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&bas_init.battery_level_char_attr_md.write_perm);
367 
368  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_init.battery_level_report_read_perm);
369 
370  bas_init.evt_handler = NULL;
371  bas_init.support_notification = true;
372  bas_init.p_report_ref = NULL;
373  bas_init.initial_batt_level = 100;
374 
375  err_code = ble_bas_init(&m_bas, &bas_init);
376  APP_ERROR_CHECK(err_code);
377 
378  // Initialize Device Information Service.
379  memset(&dis_init, 0, sizeof(dis_init));
380 
381  ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, (char *)MANUFACTURER_NAME);
382 
383  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm);
384  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm);
385 
386  err_code = ble_dis_init(&dis_init);
387  APP_ERROR_CHECK(err_code);
388 }
static ble_bas_t m_bas
Definition: ble_app_hrs.c:90
static ble_hrs_t m_hrs
Definition: ble_app_hrs.c:91
#define MANUFACTURER_NAME
Definition: ble_app_hrs.c:46
static void on_conn_params_evt ( ble_conn_params_evt_t *  p_evt)
static

Function for handling the Connection Parameters Module.

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.
Parameters
[in]p_evtEvent received from the Connection Parameters Module.

Definition at line 450 of file ble_app_hrs.c.

451 {
452  uint32_t err_code;
453 
454  if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
455  {
456  err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
457  APP_ERROR_CHECK(err_code);
458  }
459 }
static uint16_t m_conn_handle
Definition: ble_app_hrs.c:89
static void conn_params_error_handler ( uint32_t  nrf_error)
static

Function for handling a Connection Parameters error.

Parameters
[in]nrf_errorError code containing information about what went wrong.

Definition at line 466 of file ble_app_hrs.c.

467 {
468  APP_ERROR_HANDLER(nrf_error);
469 }
static void sleep_mode_enter ( void  )
static

Function for putting the chip into sleep mode.

Note
This function will not return.

Definition at line 499 of file ble_app_hrs.c.

500 {
501  uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
502  APP_ERROR_CHECK(err_code);
503 
504  // Prepare wakeup buttons.
505  err_code = bsp_btn_ble_sleep_mode_prepare();
506  APP_ERROR_CHECK(err_code);
507 
508  // Go to system-off mode (this function will not return; wakeup will cause a reset).
509  err_code = sd_power_system_off();
510  APP_ERROR_CHECK(err_code);
511 }
static void on_adv_evt ( ble_adv_evt_t  ble_adv_evt)
static

Function for handling advertising events.

This function will be called for advertising events which are passed to the application.

Parameters
[in]ble_adv_evtAdvertising event.

Definition at line 520 of file ble_app_hrs.c.

521 {
522  uint32_t err_code;
523 
524  switch (ble_adv_evt)
525  {
526  case BLE_ADV_EVT_FAST:
527  err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
528  APP_ERROR_CHECK(err_code);
529  break;
530  case BLE_ADV_EVT_IDLE:
532  break;
533  default:
534  break;
535  }
536 }
static void sleep_mode_enter(void)
Function for putting the chip into sleep mode.
Definition: ble_app_hrs.c:499
static void on_ble_evt ( ble_evt_t *  p_ble_evt)
static

Function for handling the Application's BLE Stack events.

Parameters
[in]p_ble_evtBluetooth stack event.

Definition at line 543 of file ble_app_hrs.c.

544 {
545  uint32_t err_code;
546 
547  switch (p_ble_evt->header.evt_id)
548  {
549  case BLE_GAP_EVT_CONNECTED:
550  err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
551  APP_ERROR_CHECK(err_code);
552  m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
553  break;
554 
555  case BLE_GAP_EVT_DISCONNECTED:
556  m_conn_handle = BLE_CONN_HANDLE_INVALID;
557  break;
558 
559  default:
560  // No implementation needed.
561  break;
562  }
563 }
static uint16_t m_conn_handle
Definition: ble_app_hrs.c:89
static void ble_evt_dispatch ( ble_evt_t *  p_ble_evt)
static

Function for dispatching a BLE stack event to all modules with a BLE stack event handler.

This function is called from the BLE Stack event interrupt handler after a BLE stack event has been received.

Parameters
[in]p_ble_evtBluetooth stack event.

Definition at line 573 of file ble_app_hrs.c.

574 {
575  dm_ble_evt_handler(p_ble_evt);
576  ble_hrs_on_ble_evt(&m_hrs, p_ble_evt);
577  ble_bas_on_ble_evt(&m_bas, p_ble_evt);
578  ble_conn_params_on_ble_evt(p_ble_evt);
579  bsp_btn_ble_on_ble_evt(p_ble_evt);
580  on_ble_evt(p_ble_evt);
581  ble_advertising_on_ble_evt(p_ble_evt);
582 }
static ble_bas_t m_bas
Definition: ble_app_hrs.c:90
static ble_hrs_t m_hrs
Definition: ble_app_hrs.c:91
static void on_ble_evt(ble_evt_t *p_ble_evt)
Function for handling the Application's BLE Stack events.
Definition: ble_app_hrs.c:543
static void sys_evt_dispatch ( uint32_t  sys_evt)
static

Function for dispatching a system event to interested modules.

This function is called from the System event interrupt handler after a system event has been received.

Parameters
[in]sys_evtSystem stack event.

Definition at line 592 of file ble_app_hrs.c.

593 {
594  pstorage_sys_event_handler(sys_evt);
595  ble_advertising_on_sys_evt(sys_evt);
596 }
void bsp_event_handler ( bsp_event_t  event)

Function for handling events from the BSP module.

Parameters
[in]eventEvent generated by button press.

Definition at line 604 of file ble_app_hrs.c.

605 {
606  uint32_t err_code;
607  switch (event)
608  {
609  case BSP_EVENT_SLEEP:
611  break;
612 
613  case BSP_EVENT_DISCONNECT:
614  err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
615  if (err_code != NRF_ERROR_INVALID_STATE)
616  {
617  APP_ERROR_CHECK(err_code);
618  }
619  break;
620 
621  case BSP_EVENT_WHITELIST_OFF:
622  err_code = ble_advertising_restart_without_whitelist();
623  if (err_code != NRF_ERROR_INVALID_STATE)
624  {
625  APP_ERROR_CHECK(err_code);
626  }
627  break;
628 
629  default:
630  break;
631  }
632 }
static uint16_t m_conn_handle
Definition: ble_app_hrs.c:89
static void sleep_mode_enter(void)
Function for putting the chip into sleep mode.
Definition: ble_app_hrs.c:499
static uint32_t device_manager_evt_handler ( dm_handle_t const *  p_handle,
dm_event_t const *  p_event,
ret_code_t  event_result 
)
static

Function for handling the Device Manager events.

Parameters
[in]p_handleDevice manager handle
[in]p_eventDevice manager event
[out]event_resultResult
Returns
error code

Definition at line 642 of file ble_app_hrs.c.

645 {
646  APP_ERROR_CHECK(event_result);
647 
648  return NRF_SUCCESS;
649 }
static void device_manager_init ( bool  erase_bonds)
static

Function for the Device Manager initialization.

Parameters
[in]erase_bondsIndicates whether bonding information should be cleared from persistent storage during initialization of the Device Manager.

Definition at line 657 of file ble_app_hrs.c.

658 {
659  uint32_t err_code;
660  dm_init_param_t init_param = {.clear_persistent_data = erase_bonds};
661  dm_application_param_t register_param;
662 
663  // Initialize persistent storage module.
664  err_code = pstorage_init();
665  APP_ERROR_CHECK(err_code);
666 
667  err_code = dm_init(&init_param);
668  APP_ERROR_CHECK(err_code);
669 
670  memset(&register_param.sec_param, 0, sizeof(ble_gap_sec_params_t));
671 
672  register_param.sec_param.bond = SEC_PARAM_BOND;
673  register_param.sec_param.mitm = SEC_PARAM_MITM;
674  register_param.sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES;
675  register_param.sec_param.oob = SEC_PARAM_OOB;
676  register_param.sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
677  register_param.sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
678  register_param.evt_handler = device_manager_evt_handler;
679  register_param.service_type = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;
680 
681  err_code = dm_register(&m_app_handle, &register_param);
682  APP_ERROR_CHECK(err_code);
683 }
#define SEC_PARAM_MITM
Definition: ble_app_hrs.c:81
#define SEC_PARAM_MAX_KEY_SIZE
Definition: ble_app_hrs.c:85
#define SEC_PARAM_IO_CAPABILITIES
Definition: ble_app_hrs.c:82
#define SEC_PARAM_MIN_KEY_SIZE
Definition: ble_app_hrs.c:84
static dm_application_instance_t m_app_handle
Definition: ble_app_hrs.c:106
#define SEC_PARAM_BOND
Definition: ble_app_hrs.c:80
static uint32_t device_manager_evt_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
Function for handling the Device Manager events.
Definition: ble_app_hrs.c:642
#define SEC_PARAM_OOB
Definition: ble_app_hrs.c:83
void app_profile_on_ble_evt ( ble_evt_t *  p_ble_evt)

Handle BLE event.

Parameters
[in]p_ble_evtBLE event to handle.

Definition at line 726 of file ble_app_hrs.c.

727 {
728  ble_evt_dispatch(p_ble_evt);
729 }
static void ble_evt_dispatch(ble_evt_t *p_ble_evt)
Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
Definition: ble_app_hrs.c:573
void app_profile_on_sys_evt ( uint32_t  sys_evt)

Handle system event.

Parameters
sys_evtSystem event to handle.

Definition at line 731 of file ble_app_hrs.c.

732 {
733  sys_evt_dispatch(sys_evt);
734 }
static void sys_evt_dispatch(uint32_t sys_evt)
Function for dispatching a system event to interested modules.
Definition: ble_app_hrs.c:592

Variable Documentation

uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID
static

Handle of the current connection.

Definition at line 89 of file ble_app_hrs.c.

ble_bas_t m_bas
static

Structure used to identify the battery service.

Definition at line 90 of file ble_app_hrs.c.

ble_hrs_t m_hrs
static

Structure used to identify the heart rate service.

Definition at line 91 of file ble_app_hrs.c.

bool m_rr_interval_enabled = true
static

Flag for enabling and disabling the registration of new RR interval measurements (the purpose of disabling this is just to test sending HRM without RR interval data.

Definition at line 92 of file ble_app_hrs.c.

sensorsim_cfg_t m_battery_sim_cfg
static

Battery Level sensor simulator configuration.

Definition at line 94 of file ble_app_hrs.c.

sensorsim_state_t m_battery_sim_state
static

Battery Level sensor simulator state.

Definition at line 95 of file ble_app_hrs.c.

sensorsim_cfg_t m_heart_rate_sim_cfg
static

Heart Rate sensor simulator configuration.

Definition at line 96 of file ble_app_hrs.c.

sensorsim_state_t m_heart_rate_sim_state
static

Heart Rate sensor simulator state.

Definition at line 97 of file ble_app_hrs.c.

sensorsim_cfg_t m_rr_interval_sim_cfg
static

RR Interval sensor simulator configuration.

Definition at line 98 of file ble_app_hrs.c.

sensorsim_state_t m_rr_interval_sim_state
static

RR Interval sensor simulator state.

Definition at line 99 of file ble_app_hrs.c.

dm_application_instance_t m_app_handle
static

Application identifier allocated by device manager

Definition at line 106 of file ble_app_hrs.c.

ble_uuid_t m_adv_uuids[]
static
Initial value:
= {{BLE_UUID_HEART_RATE_SERVICE, BLE_UUID_TYPE_BLE},
{BLE_UUID_BATTERY_SERVICE, BLE_UUID_TYPE_BLE},
{BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE}}

Universally unique service identifiers.

Definition at line 108 of file ble_app_hrs.c.