/**@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: led_stop(); 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(); 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_masters_store(); APP_ERROR_CHECK(err_code); // 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); break; case BLE_GAP_EVT_TIMEOUT: if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { led_stop(); GPIO_WAKEUP_BUTTON_CONFIG(HR_INC_BUTTON_PIN_NO); GPIO_WAKEUP_BUTTON_CONFIG(HR_DEC_BUTTON_PIN_NO); system_off_mode_enter(); } break; default: break; } APP_ERROR_CHECK(err_code); }
/**@brief Application main function. */ int main(void) { uint32_t err_code; timers_init(); gpiote_init(); buttons_init(); if (is_first_start()) { // The startup was not because of button presses. This is the first start. // Go into System-Off mode. // NOTE: This register cannot be set directly after ble_stack_init() because the SoftDevice // will be enabled. GPIO_WAKEUP_BUTTON_CONFIG(HR_INC_BUTTON_PIN_NO); GPIO_WAKEUP_BUTTON_CONFIG(HR_DEC_BUTTON_PIN_NO); NRF_POWER->SYSTEMOFF = 1; } bond_manager_init(); ble_stack_init(); radio_notification_init(); // Initialize Bluetooth Stack parameters gap_params_init(); advertising_init(); services_init(); conn_params_init(); sec_params_init(); // Actually start advertising advertising_start(); // Enter main loop for (;;) { // Switch to a low power state until an event is available for the application err_code = sd_app_event_wait(); APP_ERROR_CHECK(err_code); } }
void charger_framework_init(void) { //Get Init Charging Status global_device_charging_state = charger_status(); //Start Charger Timer ,when Pluged in. if( global_device_charging_state != NoCharge ) { // charging_flash_battery_start(); show_charging_percentage(&global_device_charging_state); } #ifdef CHARGER_CONNECTED_PIN //Wakeup GPIO_WAKEUP_BUTTON_CONFIG(CHARGER_CONNECTED_PIN); //charger wakeup #endif }
/**@brief Initialize buttons. */ static void buttons_init(void) { // Set Wakeup and Bonds Delete buttons as wakeup sources GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN); GPIO_WAKEUP_BUTTON_CONFIG(BONDMNGR_DELETE_BUTTON_PIN_NO); }
/**@brief Application's BLE Stack event handler. * * @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; 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(); 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_bps_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_masters_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) { nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO); // Go to system-off mode (this function will not return; wakeup will cause a reset) GPIO_WAKEUP_BUTTON_CONFIG(SEND_MEAS_BUTTON_PIN_NO); err_code = sd_power_system_off(); } 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); } break; default: break; } APP_ERROR_CHECK(err_code); }
/**@brief Application's BLE Stack event handler. * * @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 ble_gap_evt_auth_status_t m_auth_status; ble_gap_enc_info_t * p_enc_info; 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; /* YOUR_JOB: Uncomment this part if you are using the app_button module to handle button events (assuming that the button events are only needed in connected state). If this is uncommented out here, 1. Make sure that app_button_disable() is called when handling BLE_GAP_EVT_DISCONNECTED below. 2. Make sure the app_button module is initialized. err_code = app_button_enable(); */ break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO); m_conn_handle = BLE_CONN_HANDLE_INVALID; /* YOUR_JOB: Uncomment this part if you are using the app_button module to handle button events. This should be done to save power when not connected to a peer. err_code = app_button_disable(); */ if (err_code == NRF_SUCCESS) { 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_GATTS_EVT_SYS_ATTR_MISSING: err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); break; case BLE_GAP_EVT_AUTH_STATUS: m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status; break; case BLE_GAP_EVT_SEC_INFO_REQUEST: p_enc_info = &m_auth_status.periph_keys.enc_info; if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div) { err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL); } else { // No keys found for this device err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL); } 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) GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN); err_code = sd_power_system_off(); } break; default: break; } APP_ERROR_CHECK(err_code); }
/**@brief Application's BLE Stack event handler. * * @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_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 start to advertise either, store bonds. err_code = ble_bondmngr_bonded_masters_store(); APP_ERROR_CHECK(err_code); // Stop detecting button presses when not connected err_code = app_button_disable(); APP_ERROR_CHECK(err_code); // Stop detecting button presses when not connected err_code = app_button_disable(); APP_ERROR_CHECK(err_code); err_code = ble_ans_c_service_store(); 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) GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN); 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: break; } APP_ERROR_CHECK(err_code); }
/**@brief Application's BLE Stack event handler. * * @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 ble_gap_evt_auth_status_t m_auth_status; ble_gap_enc_info_t * p_enc_info; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: nrf_gpio_pin_set(LED_OTHER); BLESessionActive = 1; break; case BLE_GAP_EVT_DISCONNECTED: advertising_start(); BLESessionActive = 0; sendRawData = 0; do_post_processing = true; nrf_gpio_pin_clear(LED_OTHER); 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_GATTS_EVT_SYS_ATTR_MISSING: err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); APP_ERROR_CHECK(err_code); break; case BLE_GAP_EVT_AUTH_STATUS: m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status; break; case BLE_GAP_EVT_SEC_INFO_REQUEST: p_enc_info = &m_auth_status.periph_keys.enc_info; if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div) { err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL); APP_ERROR_CHECK(err_code); } else { // No keys found for this device err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL); 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) GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN); err_code = sd_power_system_off(); APP_ERROR_CHECK(err_code); } break; default: break; } APP_ERROR_CHECK(err_code); }
/**@brief Application's BLE Stack event handler. * * @param[in] p_ble_evt Bluetooth stack event. */ 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: adv_led_blink_stop(); if (m_is_link_loss_alerting) { alert_led_blink_stop(); } m_advertising_mode = BLE_NO_ADV; m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; // Start handling button presses err_code = app_button_enable(); // enable the button #if OLED096_Func OLED_ShowString(0,16, (uint8_t*)"BT:CON"); OLED_Refresh_Gram(); #endif break; case BLE_GAP_EVT_DISCONNECTED: if (!m_is_link_loss_alerting) { alert_led_blink_stop(); } 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_masters_store(); APP_ERROR_CHECK(err_code); advertising_start(); #if OLED096_Func OLED_ShowString(0,16, (uint8_t*)"BT:DIS"); OLED_Refresh_Gram(); #endif 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_SLEEP) { m_advertising_mode = BLE_NO_ADV; adv_led_blink_stop(); alert_led_blink_stop(); GPIO_WAKEUP_BUTTON_CONFIG(SIGNAL_ALERT_BUTTON); GPIO_WAKEUP_BUTTON_CONFIG(BONDMNGR_DELETE_BUTTON_PIN_NO); // Go to system-off mode // (this function will not return; wakeup will cause a reset) #if 0 err_code = sd_power_system_off(); #endif #if OLED096_Func OLED_ShowString(0,16, (uint8_t*)"BT:Tout"); OLED_Refresh_Gram(); #endif } 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); break; default: 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 = NRF_SUCCESS; static ble_gap_evt_auth_status_t m_auth_status; ble_gap_enc_info_t * p_enc_info; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: nrf_gpio_pin_set(CONNECTED_LED_PIN_N); nrf_gpio_pin_clear(ADVERTISING_LED_PIN_N); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; err_code = app_button_enable(); break; case BLE_GAP_EVT_DISCONNECTED: nrf_gpio_pin_clear(CONNECTED_LED_PIN_N); m_conn_handle = BLE_CONN_HANDLE_INVALID; err_code = app_button_disable(); if (err_code == NRF_SUCCESS) { 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_GATTS_EVT_SYS_ATTR_MISSING: err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); break; case BLE_GAP_EVT_AUTH_STATUS: m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status; break; case BLE_GAP_EVT_SEC_INFO_REQUEST: p_enc_info = &m_auth_status.periph_keys.enc_info; if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div) { err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL); } else { // No keys found for this device err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL); } 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_N); // Go to system-off mode (this function will not return; wakeup will cause a reset) GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN); uart_tx_str("reset by timeout"); err_code = sd_power_system_off(); } break; case BLE_GATTS_EVT_WRITE: break; case BLE_GAP_EVT_CONN_PARAM_UPDATE: nrf_gpio_pin_toggle (LEDBUTTON_LED_PIN_NO); break; default: break; } APP_ERROR_CHECK(err_code); }