/**@brief Function for handling events from the BSP module. * * @param[in] event Event generated by button press. */ static void bsp_event_handler(bsp_event_t event) { ret_code_t err_code; switch (event) { case BSP_EVENT_KEY_0: NRF_LOG_PRINTF("BSP_EVENT_KEY_0\n"); err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, 87); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_PACKETS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } break; case BSP_EVENT_KEY_1: NRF_LOG_PRINTF("BSP_EVENT_KEY_1\n"); break; default: break; } }
static void sensors_callback(sensors_evt_t evt, sensors_data_t *data) { switch (evt) { case SI7020_TEMP_MEAS_EVT: NRF_LOG_PRINTF("[APPL] Temperature measurement, %.2f.\r\n", data->temperature); // bluetooth_update_temperature(data->temperature); if (m_adv_state == W_ADV_STATE_IDLE) { advertising_init(); advertising_start(); } break; case SI7020_HUM_MEAS_EVT: NRF_LOG_PRINTF("[APP] T=%d.%02d°C, RH=%d.%02d%%\r\n", (uint32_t)data->temperature, (uint32_t)(100.0 * data->temperature) - 100 * (uint32_t)data->temperature, (uint32_t)data->humidity, (uint32_t)(100.0 * data->humidity) - 100 * (uint32_t)data->humidity); // bluetooth_update_temperature(data->temperature); // bluetooth_update_humidity(data->humidity); if (m_adv_state == W_ADV_STATE_IDLE) { advertising_init(); advertising_start(); } break; } }
/**@brief Function for printing an iOS notification. * * @param[in] p_notif Pointer to the iOS notification. */ static void notif_print(ble_ancs_c_evt_notif_t * p_notif) { NRF_LOG("\r\nNotification\r\n"); NRF_LOG_PRINTF("Event: %s\r\n", lit_eventid[p_notif->evt_id]); NRF_LOG_PRINTF("Category ID: %s\r\n", lit_catid[p_notif->category_id]); NRF_LOG_PRINTF("Category Cnt:%u\r\n", (unsigned int) p_notif->category_count); NRF_LOG_PRINTF("UID: %u\r\n", (unsigned int) p_notif->notif_uid); NRF_LOG("Flags:\r\n"); if(p_notif->evt_flags.silent == 1) { NRF_LOG(" Silent\r\n"); } if(p_notif->evt_flags.important == 1) { NRF_LOG(" Important\r\n"); } if(p_notif->evt_flags.pre_existing == 1) { NRF_LOG(" Pre-existing\r\n"); } if(p_notif->evt_flags.positive_action == 1) { NRF_LOG(" Positive Action\r\n"); } if(p_notif->evt_flags.negative_action == 1) { NRF_LOG(" Positive Action\r\n"); } }
/**@brief Function for handling BLE Stack events involving peripheral applications. Manages the * LEDs used to report the status of the peripheral applications. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_peripheral_evt(ble_evt_t * p_ble_evt) { uint32_t err_code; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: NRF_LOG_PRINTF("Peripheral connected\r\n"); LEDS_OFF(PERIPHERAL_ADVERTISING_LED); LEDS_ON(PERIPHERAL_CONNECTED_LED); break; //BLE_GAP_EVT_CONNECTED case BLE_GAP_EVT_DISCONNECTED: NRF_LOG_PRINTF("Peripheral disconnected\r\n"); LEDS_OFF(PERIPHERAL_CONNECTED_LED); break;//BLE_GAP_EVT_DISCONNECTED case BLE_EVT_USER_MEM_REQUEST: err_code = sd_ble_user_mem_reply(p_ble_evt->evt.gap_evt.conn_handle, NULL); APP_ERROR_CHECK(err_code); break;//BLE_EVT_USER_MEM_REQUEST case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: { ble_gatts_rw_authorize_reply_params_t auth_reply; if(p_ble_evt->evt.gatts_evt.params.authorize_request.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID) { if ((p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ) || (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) || (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)) { if (p_ble_evt->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE; } else { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ; } auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED; err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gap_evt.conn_handle,&auth_reply); APP_ERROR_CHECK(err_code); } } }break;//BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST default: // No implementation needed. break; } }
/**@brief Handles events coming from Running Speed and Cadence central module. */ static void rscs_c_evt_handler(ble_rscs_c_t * p_rscs_c, ble_rscs_c_evt_t * p_rscs_c_evt) { switch (p_rscs_c_evt->evt_type) { case BLE_RSCS_C_EVT_DISCOVERY_COMPLETE: { ret_code_t err_code; NRF_LOG_PRINTF("Running Speed and Cadence service discovered\r\n"); // Initiate bonding. err_code = pm_link_secure(p_rscs_c->conn_handle, false); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } // Running speed cadence service discovered. Enable notifications. err_code = ble_rscs_c_rsc_notif_enable(p_rscs_c); APP_ERROR_CHECK(err_code); } break; // BLE_RSCS_C_EVT_DISCOVERY_COMPLETE: case BLE_RSCS_C_EVT_RSC_NOTIFICATION: { uint32_t err_code; ble_rscs_meas_t rscs_measurment; NRF_LOG_PRINTF("Speed = %d\r\n", p_rscs_c_evt->params.rsc.inst_speed); rscs_measurment.is_running = p_rscs_c_evt->params.rsc.is_running; rscs_measurment.is_inst_stride_len_present = p_rscs_c_evt->params.rsc.is_inst_stride_len_present; rscs_measurment.is_total_distance_present = p_rscs_c_evt->params.rsc.is_total_distance_present; rscs_measurment.inst_stride_length = p_rscs_c_evt->params.rsc.inst_stride_length; rscs_measurment.inst_cadence = p_rscs_c_evt->params.rsc.inst_cadence; rscs_measurment.inst_speed = p_rscs_c_evt->params.rsc.inst_speed; rscs_measurment.total_distance = p_rscs_c_evt->params.rsc.total_distance; err_code = ble_rscs_measurement_send(&m_rscs, &rscs_measurment); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } } break; // BLE_RSCS_C_EVT_RSC_NOTIFICATION default: // No implementation needed. break; } }
/**@brief Function for printing iOS notification attribute data. * * @param[in] p_attr Pointer to an iOS notification attribute. * @param[in] p_ancs_attr_list Pointer to a list of attributes. Each entry in the list stores a pointer to its attribute data, which is to be printed. */ static void notif_attr_print(ble_ancs_c_evt_notif_attr_t * p_attr) { if (p_attr->attr_len != 0) { NRF_LOG_PRINTF("%s: %s\r\n", lit_attrid[p_attr->attr_id], p_attr->p_attr_data); } else if (p_attr->attr_len == 0) { NRF_LOG_PRINTF("%s: (N/A)\r\n", lit_attrid[p_attr->attr_id]); } }
int main(void) { ret_code_t err_code; err_code = NRF_LOG_INIT(); APP_ERROR_CHECK(err_code); NRF_LOG_PRINTF("[APP]: Multilink Example\r\n"); leds_init(); APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL); buttons_init(); ble_stack_init(); db_discovery_init(); lbs_c_init(); // Start scanning for peripherals and initiate connection to devices which // advertise. scan_start(); // Turn on the LED to signal scanning. LEDS_ON(CENTRAL_SCANNING_LED); for (;;) { // Wait for BLE events. power_manage(); } }
/**@brief Function for handling database discovery events. * * @details This function is callback function to handle events from the database discovery module. * Depending on the UUIDs that are discovered, this function should forward the events * to their respective services. * * @param[in] p_event Pointer to the database discovery event. */ static void db_disc_handler(ble_db_discovery_evt_t * p_evt) { NRF_LOG_PRINTF("[APP]: call to ble_lbs_on_db_disc_evt for instance %d and link 0x%x!\r\n", p_evt->conn_handle, p_evt->conn_handle); ble_lbs_on_db_disc_evt(&m_ble_lbs_c[p_evt->conn_handle], p_evt); }
/** * @brief Function for main application entry. */ int main(void) { LEDS_CONFIGURE(BSP_LED_0_MASK); LEDS_OFF(BSP_LED_0_MASK); adc_config(); UNUSED_RETURN_VALUE(NRF_LOG_INIT()); NRF_LOG_PRINTF("ADC example\r\n"); while (true) { APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE)); uint32_t i; for (i = 0; i < ADC_BUFFER_SIZE; i++) { // manually trigger ADC conversion nrf_drv_adc_sample(); // enter into sleep mode __SEV(); __WFE(); __WFE(); nrf_delay_ms(100); LEDS_INVERT(BSP_LED_0_MASK); } } }
int main(void) { // Enable the constant latency sub power mode to minimize the time it takes // for the SPIS peripheral to become active after the CSN line is asserted // (when the CPU is in sleep mode). NRF_POWER->TASKS_CONSTLAT = 1; LEDS_CONFIGURE(BSP_LED_0_MASK); LEDS_OFF(BSP_LED_0_MASK); APP_ERROR_CHECK(NRF_LOG_INIT()); NRF_LOG_PRINTF("SPIS example\n"); nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE); spis_config.csn_pin = SPIS_CS_PIN; APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler)); while(1) { memset(m_rx_buf, 0, m_length); spis_xfer_done = false; APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, m_tx_buf, m_length, m_rx_buf, m_length)); while (!spis_xfer_done) { __WFE(); } LEDS_INVERT(BSP_LED_0_MASK); } }
/**@brief Function for handling File Data Storage events. * * @param[in] p_evt Peer Manager event. * @param[in] cmd */ static void fds_evt_handler(fds_evt_t const * const p_fds_evt) { if (p_fds_evt->id == FDS_EVT_GC) { NRF_LOG_PRINTF("GC completed\n"); } }
/** * @brief SPIS user event handler. * * @param event */ void spis_event_handler(nrf_drv_spis_event_t event) { if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) { spis_xfer_done = true; NRF_LOG_PRINTF(" Transfer completed. Received: %s\n",m_rx_buf); } }
/**@brief Handles events coming from the Heart Rate central module. */ static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt) { switch (p_hrs_c_evt->evt_type) { case BLE_HRS_C_EVT_DISCOVERY_COMPLETE: { ret_code_t err_code; NRF_LOG_PRINTF("Heart rate service discovered\r\n"); // Initiate bonding. err_code = pm_link_secure(p_hrs_c->conn_handle, false); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(p_hrs_c); APP_ERROR_CHECK(err_code); } break; // BLE_HRS_C_EVT_DISCOVERY_COMPLETE case BLE_HRS_C_EVT_HRM_NOTIFICATION: { ret_code_t err_code; NRF_LOG_PRINTF("Heart Rate = %d\r\n", p_hrs_c_evt->params.hrm.hr_value); err_code = ble_hrs_heart_rate_measurement_send(&m_hrs, p_hrs_c_evt->params.hrm.hr_value); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_NO_TX_BUFFERS) && (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING) ) { APP_ERROR_HANDLER(err_code); } } break; // BLE_HRS_C_EVT_HRM_NOTIFICATION default: // No implementation needed. break; } }
/**@brief Function for handling BLE Stack events common to both the central and peripheral roles. * @param[in] conn_handle Connection Handle. * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_evt(uint16_t conn_handle, ble_evt_t * p_ble_evt) { ret_code_t err_code; uint8_t passkey[BLE_GAP_PASSKEY_LEN + 1]; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_SEC_PARAMS_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n"); break; case BLE_GAP_EVT_PASSKEY_DISPLAY: memcpy(passkey, p_ble_evt->evt.gap_evt.params.passkey_display.passkey, BLE_GAP_PASSKEY_LEN); passkey[BLE_GAP_PASSKEY_LEN] = 0x00; NRF_LOG_PRINTF("BLE_GAP_EVT_PASSKEY_DISPLAY: passkey=%s match_req=%d\n", passkey, p_ble_evt->evt.gap_evt.params.passkey_display.match_request); err_code = sd_ble_gap_auth_key_reply(conn_handle, BLE_GAP_AUTH_KEY_TYPE_PASSKEY, NULL); APP_ERROR_CHECK(err_code); /* Due to DRGN-7235, dhkey_reply() must come after auth_key_reply() */ APP_ERROR_CHECK_BOOL(m_dhkey_req); err_code = sd_ble_gap_lesc_dhkey_reply(conn_handle, &m_lesc_dhkey); APP_ERROR_CHECK(err_code); m_dhkey_req = 0; break; case BLE_GAP_EVT_AUTH_KEY_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_AUTH_KEY_REQUEST\n"); break; case BLE_GAP_EVT_LESC_DHKEY_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n"); err_code = ecc_p256_shared_secret_compute(&m_lesc_sk.sk[0], &p_ble_evt->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer->pk[0], &m_lesc_dhkey.key[0]); APP_ERROR_CHECK(err_code); m_dhkey_req = 1; break; case BLE_GAP_EVT_AUTH_STATUS: NRF_LOG_PRINTF("BLE_GAP_EVT_AUTH_STATUS: status=0x%x bond=0x%x lv4: %d kdist_own:0x%x kdist_peer:0x%x\n", p_ble_evt->evt.gap_evt.params.auth_status.auth_status, p_ble_evt->evt.gap_evt.params.auth_status.bonded, p_ble_evt->evt.gap_evt.params.auth_status.sm1_levels.lv4, *((uint8_t *)&p_ble_evt->evt.gap_evt.params.auth_status.kdist_own), *((uint8_t *)&p_ble_evt->evt.gap_evt.params.auth_status.kdist_peer)); break; default: // No implementation needed. break; } }
/**@brief Function for handling File Data Storage events. * * @param[in] p_evt Peer Manager event. * @param[in] cmd */ static void fds_evt_handler(ret_code_t result, fds_cmd_id_t cmd, fds_record_id_t record_id, fds_record_key_t record_key) { if (cmd == FDS_CMD_GC) { NRF_LOG_PRINTF("GC completed\n"); } }
uint32_t bsp_indication_text_set(bsp_indication_t indicate, char const * p_text) { uint32_t err_code = bsp_indication_set(indicate); #ifdef BSP_UART_SUPPORT NRF_LOG_PRINTF("%s", p_text); #endif // BSP_UART_SUPPORT return err_code; }
/**@brief Handles events coming from the LED Button central module. * * @param[in] p_lbs_c The instance of LBS_C that triggered the event. * @param[in] p_lbs_c_evt The LBS_C event. */ static void lbs_c_evt_handler(ble_lbs_c_t * p_lbs_c, ble_lbs_c_evt_t * p_lbs_c_evt) { const uint16_t conn_handle = p_lbs_c_evt->conn_handle; switch (p_lbs_c_evt->evt_type) { case BLE_LBS_C_EVT_DISCOVERY_COMPLETE: { ret_code_t err_code; NRF_LOG_PRINTF("[APP]: LED Button service discovered on conn_handle 0x%x\r\n", conn_handle); err_code = app_button_enable(); APP_ERROR_CHECK(err_code); // LED Button service discovered. Enable notification of Button. err_code = ble_lbs_c_button_notif_enable(p_lbs_c); APP_ERROR_CHECK(err_code); } break; // BLE_LBS_C_EVT_DISCOVERY_COMPLETE case BLE_LBS_C_EVT_BUTTON_NOTIFICATION: { NRF_LOG_PRINTF("[APP]: Link 0x%x, Button state changed on peer to 0x%x\r\n", conn_handle, p_lbs_c_evt->params.button.button_state); if (p_lbs_c_evt->params.button.button_state) { LEDS_ON(LEDBUTTON_LED); } else { LEDS_OFF(LEDBUTTON_LED); } } break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION default: // No implementation needed. break; } }
/** * @brief ADC interrupt handler. */ static void adc_event_handler(nrf_drv_adc_evt_t const * p_event) { if (p_event->type == NRF_DRV_ADC_EVT_DONE) { uint32_t i; for (i = 0; i < p_event->data.done.size; i++) { NRF_LOG_PRINTF("Current sample value: %d\r\n", p_event->data.done.p_buffer[i]); } } }
/**@brief Function for handling BLE Stack events involving peripheral applications. Manages the * LEDs used to report the status of the peripheral applications. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_peripheral_evt(ble_evt_t * p_ble_evt) { switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: NRF_LOG_PRINTF("Peripheral connected\r\n"); LEDS_OFF(PERIPHERAL_ADVERTISING_LED); LEDS_ON(PERIPHERAL_CONNECTED_LED); break; case BLE_GAP_EVT_DISCONNECTED: NRF_LOG_PRINTF("Peripheral disconnected\r\n"); LEDS_OFF(PERIPHERAL_CONNECTED_LED); break; default: // No implementation needed. break; } }
/** * @brief ADC interrupt handler. * Prints ADC results on hardware UART and over BLE via the NUS service. */ static void adc_event_handler(nrf_drv_adc_evt_t const * p_event) { uint8_t adc_result[ADC_BUFFER_SIZE*2]; if (p_event->type == NRF_DRV_ADC_EVT_DONE) { adc_event_counter++; NRF_LOG_PRINTF(" adc event counter: %d\r\n", adc_event_counter); for (uint32_t i = 0; i < p_event->data.done.size; i++) { NRF_LOG_PRINTF("ADC value channel %d: %d\r\n", (i % number_of_adc_channels), p_event->data.done.p_buffer[i]); adc_result[(i*2)] = p_event->data.done.p_buffer[i] >> 8; adc_result[(i*2)+1] = p_event->data.done.p_buffer[i]; } if(ADC_BUFFER_SIZE <= 10) { ble_nus_string_send(&m_nus, &adc_result[0], ADC_BUFFER_SIZE*2); } APP_ERROR_CHECK(nrf_drv_adc_buffer_convert(adc_buffer,ADC_BUFFER_SIZE)); LEDS_INVERT(BSP_LED_3_MASK); }
int main(void) { ret_code_t err_code; bool erase_bonds; err_code = NRF_LOG_INIT(); APP_ERROR_CHECK(err_code); NRF_LOG_PRINTF("Relay Example\r\n"); APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL); buttons_leds_init(&erase_bonds); if (erase_bonds) { NRF_LOG_PRINTF("Bonds erased!\r\n"); } ble_stack_init(); peer_manager_init(erase_bonds); db_discovery_init(); hrs_c_init(); rscs_c_init(); gap_params_init(); conn_params_init(); services_init(); advertising_init(); adv_scan_start(); for (;;) { // Wait for BLE events. power_manage(); } }
int main(void) { ret_code_t err_code; bool erase_bonds; err_code = NRF_LOG_INIT(); APP_ERROR_CHECK(err_code); NRF_LOG_PRINTF("Relay Example\r\n"); APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL); buttons_leds_init(&erase_bonds); if (erase_bonds == true) { NRF_LOG("Bonds erased!\r\n"); } ble_stack_init(); peer_manager_init(erase_bonds); db_discovery_init(); hrs_c_init(); rscs_c_init(); gap_params_init(); conn_params_init(); services_init(); advertising_init(); /** Start scanning for peripherals and initiate connection to devices which * advertise Heart Rate or Running speed and cadence UUIDs. */ scan_start(); // Turn on the LED to signal scanning. LEDS_ON(CENTRAL_SCANNING_LED); // Start advertising. err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); for (;;) { // Wait for BLE events. power_manage(); } }
/**@brief Function to start scanning. */ static void scan_start(void) { ret_code_t err_code; err_code = sd_ble_gap_scan_stop(); // It is okay to ignore this error since we are stopping the scan anyway. if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } NRF_LOG_PRINTF("[APP]: start scanning for device name %s\r\n", m_target_periph_name); err_code = sd_ble_gap_scan_start(&m_scan_param); APP_ERROR_CHECK(err_code); }
/**@brief Function for handling events from the button handler module. * * @param[in] pin_no The pin that the event applies to. * @param[in] button_action The button action (press/release). */ static void button_event_handler(uint8_t pin_no, uint8_t button_action) { uint32_t err_code; switch (pin_no) { case LEDBUTTON_BUTTON_PIN: err_code = led_status_send_to_all(button_action); if (err_code == NRF_SUCCESS) { NRF_LOG_PRINTF("LBS write LED state %d\r\n", button_action); } break; default: APP_ERROR_HANDLER(pin_no); break; } }
/**@brief Function for handling the Current Time Service errors. * * @param[in] p_evt Event received from the Current Time Service client. */ static void current_time_print(ble_cts_c_evt_t * p_evt) { NRF_LOG("\nCurrent Time:\n"); NRF_LOG("\nDate:\n"); NRF_LOG_PRINTF("\tDay of week %s\n", day_of_week[p_evt-> params. current_time. exact_time_256. day_date_time. day_of_week]); if (p_evt->params.current_time.exact_time_256.day_date_time.date_time.day == 0) { NRF_LOG("\tDay of month Unknown\n"); } else { NRF_LOG_PRINTF("\tDay of month %i\n", p_evt->params.current_time.exact_time_256.day_date_time.date_time.day); } NRF_LOG_PRINTF("\tMonth of year %s\n", month_of_year[p_evt->params.current_time.exact_time_256.day_date_time.date_time.month]); if (p_evt->params.current_time.exact_time_256.day_date_time.date_time.year == 0) { NRF_LOG("\tYear Unknown\n"); } else { NRF_LOG_PRINTF("\tYear %i\n", p_evt->params.current_time.exact_time_256.day_date_time.date_time.year); } NRF_LOG("\nTime:\n"); NRF_LOG_PRINTF("\tHours %i\n", p_evt->params.current_time.exact_time_256.day_date_time.date_time.hours); NRF_LOG_PRINTF("\tMinutes %i\n", p_evt->params.current_time.exact_time_256.day_date_time.date_time.minutes); NRF_LOG_PRINTF("\tSeconds %i\n", p_evt->params.current_time.exact_time_256.day_date_time.date_time.seconds); NRF_LOG_PRINTF("\tFractions %i/256 of a second\n", p_evt->params.current_time.exact_time_256.fractions256); NRF_LOG("\nAdjust reason:\n"); NRF_LOG_PRINTF("\tDaylight savings %x\n", p_evt->params.current_time.adjust_reason.change_of_daylight_savings_time); NRF_LOG_PRINTF("\tTime zone %x\n", p_evt->params.current_time.adjust_reason.change_of_time_zone); NRF_LOG_PRINTF("\tExternal update %x\n", p_evt->params.current_time.adjust_reason.external_reference_time_update); NRF_LOG_PRINTF("\tManual update %x\n", p_evt->params.current_time.adjust_reason.manual_time_update); }
/**@brief Function for handling BLE Stack events concerning central applications. * * @details This function keeps the connection handles of central applications up-to-date. It * parses scanning reports, initiating a connection attempt to peripherals when a * target UUID is found, and manages connection parameter update requests. Additionally, * it updates the status of LEDs used to report central applications activity. * * @note Since this function updates connection handles, @ref BLE_GAP_EVT_DISCONNECTED events * should be dispatched to the target application before invoking this function. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_evt(const ble_evt_t * const p_ble_evt) { // For readability. const ble_gap_evt_t * const p_gap_evt = &p_ble_evt->evt.gap_evt; switch (p_ble_evt->header.evt_id) { // Upon connection, check which peripheral has connected, initiate DB // discovery, update LEDs status and resume scanning if necessary. case BLE_GAP_EVT_CONNECTED: { uint32_t err_code; NRF_LOG_PRINTF("[APP]: link 0x%x established, start discovery on it\r\n", p_gap_evt->conn_handle); APP_ERROR_CHECK_BOOL(p_gap_evt->conn_handle < TOTAL_LINK_COUNT); err_code = ble_lbs_c_handles_assign(&m_ble_lbs_c[p_gap_evt->conn_handle], p_gap_evt->conn_handle, NULL); APP_ERROR_CHECK(err_code); err_code = ble_db_discovery_start(&m_ble_db_discovery[p_gap_evt->conn_handle], p_gap_evt->conn_handle); if (err_code != NRF_ERROR_BUSY) { APP_ERROR_CHECK(err_code); } // Update LEDs status, and check if we should be looking for more // peripherals to connect to. LEDS_ON(CENTRAL_CONNECTED_LED); if (ble_conn_state_n_centrals() == CENTRAL_LINK_COUNT) { LEDS_OFF(CENTRAL_SCANNING_LED); } else { // Resume scanning. LEDS_ON(CENTRAL_SCANNING_LED); scan_start(); } } break; // BLE_GAP_EVT_CONNECTED // Upon disconnection, reset the connection handle of the peer which disconnected, update // the LEDs status and start scanning again. case BLE_GAP_EVT_DISCONNECTED: { uint32_t central_link_cnt; // Number of central links. NRF_LOG_PRINTF("LBS central link 0x%x disconnected (reason: %d)\r\n", p_gap_evt->conn_handle, p_gap_evt->params.disconnected.reason); uint32_t err_code = app_button_disable(); APP_ERROR_CHECK(err_code); // Start scanning scan_start(); // Update LEDs status. LEDS_ON(CENTRAL_SCANNING_LED); central_link_cnt = ble_conn_state_n_centrals(); if (central_link_cnt == 0) { LEDS_OFF(CENTRAL_CONNECTED_LED); } } break; // BLE_GAP_EVT_DISCONNECTED case BLE_GAP_EVT_ADV_REPORT: on_adv_report(p_ble_evt); break; // BLE_GAP_ADV_REPORT case BLE_GAP_EVT_TIMEOUT: { // We have not specified a timeout for scanning, so only connection attemps can timeout. if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) { APPL_LOG("[APPL]: Connection Request timed out.\r\n"); } } break; // BLE_GAP_EVT_TIMEOUT case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: { // Accept parameters requested by peer. ret_code_t err_code; err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle, &p_gap_evt->params.conn_param_update_request.conn_params); APP_ERROR_CHECK(err_code); } break; // BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST default: // No implementation needed. break; } }
void ble_ans_c_on_db_disc_evt(ble_ans_c_t * p_ans, const ble_db_discovery_evt_t * p_evt) { ble_ans_c_evt_t evt; memset(&evt, 0, sizeof(ble_ans_c_evt_t)); evt.conn_handle = p_evt->conn_handle; evt.evt_type = BLE_ANS_C_EVT_DISCOVERY_FAILED; // Check if the Alert Notification Service was discovered. if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE && p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_ALERT_NOTIFICATION_SERVICE && p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE) { // Find the characteristics inside the service. for (uint8_t i = 0; i < p_evt->params.discovered_db.char_count; i++) { const ble_gatt_db_char_t * p_char = &(p_evt->params.discovered_db.charateristics[i]); switch (p_char->characteristic.uuid.uuid) { case BLE_UUID_ALERT_NOTIFICATION_CONTROL_POINT_CHAR: NRF_LOG_PRINTF("[ANS] Found Ctrlpt \n\r"); char_set(&evt.data.service.alert_notif_ctrl_point, &p_char->characteristic); break; case BLE_UUID_UNREAD_ALERT_CHAR: NRF_LOG_PRINTF("[ANS] Found Unread Alert \n\r"); char_set(&evt.data.service.unread_alert_status, &p_char->characteristic); char_cccd_set(&evt.data.service.unread_alert_cccd, p_char->cccd_handle); break; case BLE_UUID_NEW_ALERT_CHAR: NRF_LOG_PRINTF("[ANS] Found New Alert \n\r"); char_set(&evt.data.service.new_alert, &p_char->characteristic); char_cccd_set(&evt.data.service.new_alert_cccd, p_char->cccd_handle); break; case BLE_UUID_SUPPORTED_UNREAD_ALERT_CATEGORY_CHAR: NRF_LOG_PRINTF("[ANS] Found supported unread alert category \n\r"); char_set(&evt.data.service.suported_unread_alert_cat, &p_char->characteristic); break; case BLE_UUID_SUPPORTED_NEW_ALERT_CATEGORY_CHAR: NRF_LOG_PRINTF("[ANS] Found supported new alert category \n\r"); char_set(&evt.data.service.suported_new_alert_cat, &p_char->characteristic); break; default: // No implementation needed. break; } } if (is_valid_ans_srv_discovered(&evt.data.service)) { evt.evt_type = BLE_ANS_C_EVT_DISCOVERY_COMPLETE; } } p_ans->evt_handler(&evt); }
/**@brief Function for handling BLE Stack events concerning central applications. * * @details This function keeps the connection handles of central applications up-to-date. It * parses scanning reports, initiating a connection attempt to peripherals when a target UUID * is found, and manages connection parameter update requests. Additionally, it updates the status * of LEDs used to report central applications activity. * * @note Since this function updates connection handles, @ref BLE_GAP_EVT_DISCONNECTED events * should be dispatched to the target application before invoking this function. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_central_evt(const ble_evt_t * const p_ble_evt) { const ble_gap_evt_t * const p_gap_evt = &p_ble_evt->evt.gap_evt; switch (p_ble_evt->header.evt_id) { /** Upon connection, check which peripheral has connected (HR or RSC), initiate DB * discovery, update LEDs status and resume scanning if necessary. */ case BLE_GAP_EVT_CONNECTED: { uint32_t err_code; /** If no Heart Rate sensor or RSC sensor is currently connected, try to find them on this peripheral*/ if (m_conn_handle_hrs_c == BLE_CONN_HANDLE_INVALID) { NRF_LOG_PRINTF("try to find HRS or RSC on conn_handle 0x%x\r\n", p_gap_evt->conn_handle); APP_ERROR_CHECK_BOOL(p_gap_evt->conn_handle < CENTRAL_LINK_COUNT + PERIPHERAL_LINK_COUNT); err_code = ble_db_discovery_start(&m_ble_db_discovery[p_gap_evt->conn_handle], p_gap_evt->conn_handle); APP_ERROR_CHECK(err_code); } /** Update LEDs status, and check if we should be looking for more * peripherals to connect to. */ LEDS_ON(CENTRAL_CONNECTED_LED); if (ble_conn_state_n_centrals() == CENTRAL_LINK_COUNT) { LEDS_OFF(CENTRAL_SCANNING_LED); } else { // Resume scanning. LEDS_ON(CENTRAL_SCANNING_LED); scan_start(); } } break; // BLE_GAP_EVT_CONNECTED /** Upon disconnection, reset the connection handle of the peer which disconnected, update * the LEDs status and start scanning again. */ case BLE_GAP_EVT_DISCONNECTED: { uint8_t n_centrals; if (p_gap_evt->conn_handle == m_conn_handle_hrs_c) { NRF_LOG_PRINTF("HRS central disconnected (reason: %d)\r\n", p_gap_evt->params.disconnected.reason); m_conn_handle_hrs_c = BLE_CONN_HANDLE_INVALID; } if (m_conn_handle_hrs_c == BLE_CONN_HANDLE_INVALID) { // Start scanning scan_start(); // Update LEDs status. LEDS_ON(CENTRAL_SCANNING_LED); } n_centrals = ble_conn_state_n_centrals(); if (n_centrals == 0) { LEDS_OFF(CENTRAL_CONNECTED_LED); } } break; // BLE_GAP_EVT_DISCONNECTED case BLE_GAP_EVT_ADV_REPORT: { uint32_t err_code; if (strlen(m_target_periph_name) != 0) { if (find_adv_name(&p_gap_evt->params.adv_report, m_target_periph_name)) { // Initiate connection. err_code = sd_ble_gap_connect(&p_gap_evt->params.adv_report.peer_addr, &m_scan_param, &m_connection_param); if (err_code != NRF_SUCCESS) { APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n", err_code); } } } else { /** We do not want to connect to two peripherals offering the same service, so when * a UUID is matched, we check that we are not already connected to a peer which * offers the same service. */ if (find_adv_uuid(&p_gap_evt->params.adv_report, BLE_UUID_HEART_RATE_SERVICE)&& (m_conn_handle_hrs_c == BLE_CONN_HANDLE_INVALID)) { // Initiate connection. err_code = sd_ble_gap_connect(&p_gap_evt->params.adv_report.peer_addr, &m_scan_param, &m_connection_param); if (err_code != NRF_SUCCESS) { APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n", err_code); } } } } break; // BLE_GAP_ADV_REPORT case BLE_GAP_EVT_TIMEOUT: { // We have not specified a timeout for scanning, so only connection attemps can timeout. if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) { APPL_LOG("[APPL]: Connection Request timed out.\r\n"); } } break; // BLE_GAP_EVT_TIMEOUT case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: { // Accept parameters requested by peer. ret_code_t err_code; err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle, &p_gap_evt->params.conn_param_update_request.conn_params); APP_ERROR_CHECK(err_code); } break; // BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST 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; //lint -save -e438 // Last value assigned to variable 'err_code' not used switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_DISCONNECTED: { NRF_LOG_PRINTF("[APP]: %s: BLE_GAP_EVT_DISCONNECTED\r\n", __func__); m_conn_handle = BLE_CONN_HANDLE_INVALID; /*check if the last connected peer had not used MITM, if so, delete its bond information*/ if (peer_to_be_deleted != PM_PEER_ID_INVALID) { ret_code_t ret_val = pm_peer_delete(peer_to_be_deleted); APP_ERROR_CHECK(ret_val); APP_LOG("Collector's bond deleted\r\n"); peer_to_be_deleted = PM_PEER_ID_INVALID; } // advertising_init(); // TODO: Check if this is correct, shouldn't be needed nrf_gpio_pin_clear(LED_1); }break;//BLE_GAP_EVT_DISCONNECTED case BLE_GAP_EVT_CONNECTED: { NRF_LOG_PRINTF("[APP]: %s: BLE_GAP_EVT_CONNECTED\r\n", __func__); peer_to_be_deleted = PM_PEER_ID_INVALID; m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; // Start Security Request timer. err_code = app_timer_start(m_sec_req_timer_id, SECURITY_REQUEST_DELAY, NULL); APP_ERROR_CHECK(err_code); }break;//BLE_GAP_EVT_CONNECTED case BLE_GATTS_EVT_TIMEOUT: NRF_LOG_PRINTF("[APP]: %s: BLE_GATTS_EVT_TIMEOUT\r\n", __func__); // Disconnect on GATT Server timeout events. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); APP_ERROR_CHECK(err_code); break;//BLE_GATTS_EVT_TIMEOUT case BLE_GAP_EVT_SEC_PARAMS_REQUEST: NRF_LOG_DEBUG("BLE_GAP_EVT_SEC_PARAMS_REQUEST\r\n"); break;//BLE_GAP_EVT_SEC_PARAMS_REQUEST case BLE_GAP_EVT_PASSKEY_DISPLAY: { NRF_LOG_PRINTF("[APP]: %s: BLE_GAP_EVT_PASSKEY_DISPLAY\r\n", __func__); char passkey[PASSKEY_LENGTH+1]; memcpy(passkey,p_ble_evt->evt.gap_evt.params.passkey_display.passkey,PASSKEY_LENGTH); passkey[PASSKEY_LENGTH] = 0; // Don't send delayed Security Request if security procedure is already in progress. err_code = app_timer_stop(m_sec_req_timer_id); APP_ERROR_CHECK(err_code); NRF_LOG_PRINTF("Passkey: %s\r\n",passkey); }break;//BLE_GAP_EVT_PASSKEY_DISPLAY case BLE_EVT_USER_MEM_REQUEST: NRF_LOG_PRINTF("[APP]: %s: BLE_EVT_USER_MEM_REQUEST\r\n", __func__); err_code = sd_ble_user_mem_reply(m_conn_handle, NULL); APP_ERROR_CHECK(err_code); break;//BLE_EVT_USER_MEM_REQUEST case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: { NRF_LOG_PRINTF("[APP]: %s: BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST\r\n", __func__); ble_gatts_rw_authorize_reply_params_t auth_reply; if(p_ble_evt->evt.gatts_evt.params.authorize_request.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID) { if ((p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ) || (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) || (p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)) { if (p_ble_evt->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE; } else { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ; } auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED; err_code = sd_ble_gatts_rw_authorize_reply(m_conn_handle,&auth_reply); APP_ERROR_CHECK(err_code); } } }break;//BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST default: // No implementation needed. break; } }
/**@brief Function for handling Peer Manager events. * * @param[in] p_evt Peer Manager event. */ static void pm_evt_handler(pm_evt_t const * p_evt) { ret_code_t err_code; switch(p_evt->evt_id) { case PM_EVT_BONDED_PEER_CONNECTED: NRF_LOG_PRINTF("PM_EVT_BONDED_PEER_CONNECTED: peer_id=%d\n", p_evt->peer_id); err_code = pm_peer_rank_highest(p_evt->peer_id); NRF_LOG_PRINTF("peer_rank err_code: %d\r\n", err_code); break; case PM_EVT_CONN_SEC_START: NRF_LOG_PRINTF("PM_EVT_CONN_SEC_START: peer_id=%d\n", p_evt->peer_id); break; case PM_EVT_CONN_SEC_SUCCEEDED: NRF_LOG_PRINTF("PM_EVT_CONN_SEC_SUCCEEDED Role: %d. conn_handle: %d, Procedure: %d\r\n", ble_conn_state_role(p_evt->conn_handle), p_evt->conn_handle, p_evt->params.conn_sec_succeeded.procedure); err_code = pm_peer_rank_highest(p_evt->peer_id); NRF_LOG_PRINTF("peer_rank err_code: %d\r\n", err_code); break; case PM_EVT_CONN_SEC_FAILED: NRF_LOG_PRINTF("PM_EVT_CONN_SEC_FAILED: peer_id=%d, error=%d\n", p_evt->peer_id, p_evt->params.conn_sec_failed.error); switch (p_evt->params.conn_sec_failed.error) { case PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING: // Rebond if one party has lost its keys. err_code = pm_conn_secure(p_evt->conn_handle, true); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } break; default: break; } break; case PM_EVT_CONN_SEC_CONFIG_REQ: { // Reject pairing request from an already bonded peer. pm_conn_sec_config_t conn_sec_config = {.allow_repairing = false}; pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config); } case PM_EVT_STORAGE_FULL: // Run garbage collection on the flash. err_code = fds_gc(); if (err_code == FDS_ERR_BUSY || err_code == FDS_ERR_NO_SPACE_IN_QUEUES) { // Retry. } break; case PM_EVT_ERROR_UNEXPECTED: // Assert. APP_ERROR_CHECK(p_evt->params.error_unexpected.error); break; case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: NRF_LOG_PRINTF("PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: peer_id=%d data_id=0x%x action=0x%x\n", p_evt->peer_id, p_evt->params.peer_data_update_succeeded.data_id, p_evt->params.peer_data_update_succeeded.action); break; case PM_EVT_PEER_DATA_UPDATE_FAILED: // Assert. APP_ERROR_CHECK_BOOL(false); break; case PM_EVT_PEER_DELETE_SUCCEEDED: break; case PM_EVT_PEER_DELETE_FAILED: // Assert. APP_ERROR_CHECK(p_evt->params.peer_delete_failed.error); break; case PM_EVT_PEERS_DELETE_SUCCEEDED: adv_scan_start(); break; case PM_EVT_PEERS_DELETE_FAILED: // Assert. APP_ERROR_CHECK(p_evt->params.peers_delete_failed_evt.error); break; case PM_EVT_LOCAL_DB_CACHE_APPLIED: break; case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED: // The local database has likely changed, send service changed indications. pm_local_database_has_changed(); break; case PM_EVT_SERVICE_CHANGED_IND_SENT: break; case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED: break; } } /**@brief Handles events coming from the Heart Rate central module. */ static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt) { switch (p_hrs_c_evt->evt_type) { case BLE_HRS_C_EVT_DISCOVERY_COMPLETE: { if (m_conn_handle_hrs_c == BLE_CONN_HANDLE_INVALID) { ret_code_t err_code; m_conn_handle_hrs_c = p_hrs_c_evt->conn_handle; NRF_LOG_PRINTF("HRS discovered on conn_handle 0x%x\r\n", m_conn_handle_hrs_c); err_code = ble_hrs_c_handles_assign(p_hrs_c, m_conn_handle_hrs_c, &p_hrs_c_evt->params.peer_db); APP_ERROR_CHECK(err_code); // Initiate bonding. err_code = pm_conn_secure(m_conn_handle_hrs_c, false); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(p_hrs_c); APP_ERROR_CHECK(err_code); } } break; // BLE_HRS_C_EVT_DISCOVERY_COMPLETE case BLE_HRS_C_EVT_HRM_NOTIFICATION: { NRF_LOG_PRINTF("Heart Rate = %d\r\n", p_hrs_c_evt->params.hrm.hr_value); } break; // BLE_HRS_C_EVT_HRM_NOTIFICATION default: // No implementation needed. break; } } /**@brief Function for searching a given name in the advertisement packets. * * @details Use this function to parse received advertising data and to find a given * name in them either as 'complete_local_name' or as 'short_local_name'. * * @param[in] p_adv_report advertising data to parse. * @param[in] name_to_find name to search. * @return true if the given name was found, false otherwise. */ static bool find_adv_name(const ble_gap_evt_adv_report_t *p_adv_report, const char * name_to_find) { uint32_t err_code; data_t adv_data; data_t dev_name; // Initialize advertisement report for parsing adv_data.p_data = (uint8_t *)p_adv_report->data; adv_data.data_len = p_adv_report->dlen; //search for advertising names err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, &adv_data, &dev_name); if (err_code == NRF_SUCCESS) { if(memcmp(name_to_find, dev_name.p_data, dev_name.data_len )== 0) { return true; } } else { // Look for the short local name if it was not found as complete err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, &adv_data, &dev_name); if (err_code != NRF_SUCCESS) { return false; } if(memcmp(m_target_periph_name, dev_name.p_data, dev_name.data_len )== 0) { return true; } } return false; }