--- C:/Users/GrzegorzN1/Desktop/main.c Thu Mar 22 16:25:09 2018 +++ C:/Users/GrzegorzN1/eclipse-workspace/fault_test/src/main.c Tue May 29 14:55:55 2018 @@ -132,12 +132,14 @@ #define OSTIMER_WAIT_FOR_QUEUE 2 /**< Number of ticks to wait for the timer queue to be ready */ +#define SECURITY_REQUEST_DELAY APP_TIMER_TICKS(1500) /**< Delay after connection until security request is sent, if necessary (ticks). */ BLE_BAS_DEF(m_bas); /**< Battery service instance. */ BLE_HRS_DEF(m_hrs); /**< Heart rate service instance. */ NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */ NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/ BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */ +APP_TIMER_DEF(m_sec_req_timer_id); /**< Security request timer. The timer lets us start pairing request if one does not arrive from the Central. */ static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ static bool m_rr_interval_enabled = true; /**< 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. */ @@ -396,6 +398,34 @@ ble_hrs_sensor_contact_detected_update(&m_hrs, sensor_contact_detected); } +/**@brief Function for handling the security request timer time-out. + * + * @details This function is called each time the security request timer expires. + * + * @param[in] p_context Pointer used for passing context information from the + * app_start_timer() call to the time-out handler. + */ +static void sec_req_timeout_handler(void * p_context) +{ + ret_code_t ret; + pm_conn_sec_status_t status; + + if (m_conn_handle != BLE_CONN_HANDLE_INVALID) + { + ret = pm_conn_sec_status_get(m_conn_handle, &status); + APP_ERROR_CHECK(ret); + + // If the link is still not secured by the peer, initiate security procedure. + if (!status.encrypted) + { + ret = pm_conn_secure(m_conn_handle, false); + if (ret != NRF_ERROR_INVALID_STATE) + { + APP_ERROR_CHECK(ret); + } + } + } +} /**@brief Function for the Timer initialization. * @@ -437,6 +467,12 @@ { APP_ERROR_HANDLER(NRF_ERROR_NO_MEM); } + + // Create security request timer. + err_code = app_timer_create(&m_sec_req_timer_id, + APP_TIMER_MODE_SINGLE_SHOT, + sec_req_timeout_handler); + APP_ERROR_CHECK(err_code); } @@ -735,6 +771,8 @@ APP_ERROR_CHECK(err_code); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle); + APP_ERROR_CHECK(err_code); + err_code = app_timer_start(m_sec_req_timer_id, SECURITY_REQUEST_DELAY, NULL); APP_ERROR_CHECK(err_code); break;