/**@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: nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); m_conn_handle = BLE_CONN_HANDLE_INVALID; // Since we are not in a connection and have not started advertising, store bonds. err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); advertising_start(); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); // 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); } break; default: // No implementation needed. break; } }
/**@brief ANT CHANNEL_CLOSED event handler. */ static void on_ant_evt_channel_closed(void) { #ifdef BONDING_ENABLE uint32_t err_code; uint32_t count; // We do not have any activity on the radio at this time, so it is safe to store bonds err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); err_code = pstorage_access_status_get(&count); if ((err_code == NRF_SUCCESS) && (count == 0)) #endif // BONDING_ENABLE { storage_access_complete_handler(); } }
static void btle_handler(ble_evt_t *p_ble_evt) { /* Library service handlers */ #if NEED_BOND_MANAGER /* disabled by default */ ble_bondmngr_on_ble_evt(p_ble_evt); #endif #if SDK_CONN_PARAMS_MODULE_ENABLE ble_conn_params_on_ble_evt(p_ble_evt); #endif /* Custom event handler */ switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: { Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; nRF51Gap::getInstance().setConnectionHandle(handle); const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; nRF51Gap::getInstance().processConnectionEvent(handle, static_cast<Gap::addr_type_t>(peer->addr_type), peer->addr, params); break; } case BLE_GAP_EVT_DISCONNECTED: { Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; // Since we are not in a connection and have not started advertising, // store bonds nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); #if NEED_BOND_MANAGER /* disabled by default */ ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); #endif Gap::DisconnectionReason_t reason; switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; break; case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; break; case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: reason = Gap::CONN_INTERVAL_UNACCEPTABLE; break; default: /* Please refer to the underlying transport library for an * interpretion of this reason's value. */ reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); break; } nRF51Gap::getInstance().processDisconnectionEvent(handle, reason); break; } case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { ble_gap_sec_params_t sec_params = {0}; sec_params.timeout = 30; /*< Timeout for Pairing Request or * Security Request (in seconds). */ sec_params.bond = 1; /**< Perform bonding. */ sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; sec_params.oob = CFG_BLE_SEC_PARAM_OOB; sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE; sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE; ASSERT_STATUS_RET_VOID(sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(), BLE_GAP_SEC_STATUS_SUCCESS, &sec_params)); } break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT); } break; case BLE_GATTC_EVT_TIMEOUT: case BLE_GATTS_EVT_TIMEOUT: // Disconnect on GATT Server and Client timeout events. // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); break; default: break; } nRF51GattServer::getInstance().hwCallback(p_ble_evt); }
static void btle_handler(ble_evt_t *p_ble_evt) { /* Library service handlers */ #if NEED_BOND_MANAGER /* disabled by default */ ble_bondmngr_on_ble_evt(p_ble_evt); #endif ble_conn_params_on_ble_evt(p_ble_evt); /* Custom event handler */ switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: { Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; nRF51Gap::getInstance().setConnectionHandle(handle); nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, handle); break; } case BLE_GAP_EVT_DISCONNECTED: { Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; // Since we are not in a connection and have not started advertising, // store bonds nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); #if NEED_BOND_MANAGER /* disabled by default */ ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); #endif nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, handle); break; } case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { ble_gap_sec_params_t sec_params = {0}; sec_params.timeout = 30; /*< Timeout for Pairing Request or * Security Request (in seconds). */ sec_params.bond = 1; /**< Perform bonding. */ sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; sec_params.oob = CFG_BLE_SEC_PARAM_OOB; sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE; sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE; ASSERT_STATUS_RET_VOID( sd_ble_gap_sec_params_reply(nRF51Gap::getInstance(). getConnectionHandle(), BLE_GAP_SEC_STATUS_SUCCESS, &sec_params)); } break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT); } break; case BLE_GATTC_EVT_TIMEOUT: case BLE_GATTS_EVT_TIMEOUT: // Disconnect on GATT Server and Client timeout events. // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); break; default: break; } nRF51GattServer::getInstance().hwCallback(p_ble_evt); }
*/ static void on_ble_evt(ble_evt_t * p_ble_evt) { uint32_t err_code; static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO); nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO); // Start handling button presses err_code = app_button_enable(); APP_ERROR_CHECK(err_code); m_advertising_mode = BLE_NO_ADV; m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); if (!m_is_link_loss_alerting) { nrf_gpio_pin_clear(ALERT_LEVEL_MILD_LED_PIN_NO); nrf_gpio_pin_clear(ALERT_LEVEL_HIGH_LED_PIN_NO); } m_conn_handle = BLE_CONN_HANDLE_INVALID; // Since we are not in a connection and have not started advertising, store bonds. err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); // Stop detecting button presses when not connected. err_code = app_button_disable(); APP_ERROR_CHECK(err_code); advertising_start(); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { if (m_advertising_mode == BLE_SLEEP) { m_advertising_mode = BLE_NO_ADV; nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); nrf_gpio_pin_clear(ADV_WHITELIST_LED_PIN_NO); nrf_gpio_pin_clear(ADV_INTERVAL_SLOW_LED_PIN_NO); // Configure buttons with sense level low as wakeup source. nrf_gpio_cfg_sense_input(SIGNAL_ALERT_BUTTON, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); nrf_gpio_cfg_sense_input(BONDMNGR_DELETE_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); // 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); } else { advertising_start(); } } break; case BLE_GATTC_EVT_TIMEOUT: case BLE_GATTS_EVT_TIMEOUT: // Disconnect on GATT Server and Client timeout events. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); APP_ERROR_CHECK(err_code); break; default: // No implementation needed. break; }
*/ 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: nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); // Start detecting button presses err_code = app_button_enable(); APP_ERROR_CHECK(err_code); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); m_conn_handle = BLE_CONN_HANDLE_INVALID; m_hts_meas_ind_conf_pending = false; // Stop detecting button presses when not connected err_code = app_button_disable(); APP_ERROR_CHECK(err_code); // Since we are not in a connection and have not started advertising, store bonds err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); advertising_start(); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); // Configure buttons with sense level low as wakeup source. nrf_gpio_cfg_sense_input(SEND_MEAS_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); nrf_gpio_cfg_sense_input(BONDMNGR_DELETE_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); // 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); } break; case BLE_GATTS_EVT_TIMEOUT: if (p_ble_evt->evt.gatts_evt.params.timeout.src == BLE_GATT_TIMEOUT_SRC_PROTOCOL) { err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); APP_ERROR_CHECK(err_code); } break; default: // No implementation needed. 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 = NRF_SUCCESS; static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); err_code = app_button_enable(); m_advertising_mode = BLE_NO_ADVERTISING; m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; break; case BLE_GAP_EVT_AUTH_STATUS: apple_notification_setup(); break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); m_conn_handle = BLE_CONN_HANDLE_INVALID; // Stop detecting button presses when not connected. err_code = app_button_disable(); APP_ERROR_CHECK(err_code); // Since we are not in a connection and have not start to advertise either, store bonds. err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); err_code = ble_ans_c_service_store(); APP_ERROR_CHECK(err_code); advertising_start(); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { if (m_advertising_mode == BLE_FAST_ADVERTISING) { advertising_start(); } else { nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); m_advertising_mode = BLE_NO_ADVERTISING; // Go to system-off mode (function will not return; wakeup will cause a reset). nrf_gpio_cfg_sense_input(WAKEUP_BUTTON_PIN, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); nrf_gpio_cfg_sense_input(BONDMNGR_DELETE_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); err_code = sd_power_system_off(); } } break; case BLE_GATTC_EVT_TIMEOUT: case BLE_GATTS_EVT_TIMEOUT: // Disconnect on GATT Server and Client timeout events. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); break; default: //No implementation needed break; } APP_ERROR_CHECK(err_code); }
/**@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; static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: led_stop(); nrf_gpio_pin_set(CONNECTED_LED_PIN_NO); //GreenLED m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; // Initialize the current heart rate to the average of max and min values. So that // everytime a new connection is made, the heart rate starts from the same value. m_cur_heart_rate = (MAX_HEART_RATE + MIN_HEART_RATE) / 2; // Start timers used to generate battery and HR measurements. application_timers_start(); // Start handling button presses err_code = app_button_enable(); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_DISCONNECTED: // Since we are not in a connection and have not started advertising, store bonds err_code = ble_bondmngr_bonded_centrals_store(); APP_ERROR_CHECK(err_code); nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); //GreenLED m_storage_in_progress = true; // @note Flash access may not be complete on return of this API. System attributes are now // stored to flash when they are updated to ensure flash access on disconnect does not // result in system powering off before data was successfully written. // Go to system-off mode, should not return from this function, wakeup will trigger // a reset. system_off_mode_enter(); break; case BLE_GAP_EVT_SEC_PARAMS_REQUEST: err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_params); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { led_stop(); nrf_gpio_cfg_sense_input(HR_INC_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); nrf_gpio_cfg_sense_input(HR_DEC_BUTTON_PIN_NO, BUTTON_PULL, NRF_GPIO_PIN_SENSE_LOW); system_off_mode_enter(); } break; default: // No implementation needed. break; } }