int main(void) { uint32_t err_code; bool erase_bonds; // Initialize. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); buttons_leds_init(&erase_bonds); uart_init(); printf("Relay Example\r\n"); ble_stack_init(); device_manager_init(erase_bonds); db_discovery_init(); hrs_c_init(); rscs_c_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); // Start scanning for peripherals and initiate connection // with devices that advertise Heart Rate UUID. scan_start(); // Start advertising. err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); for (;; ) { power_manage(); } }
int main(void) { uint32_t err_code; APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),NULL); APP_ERROR_CHECK(err_code); leds_init(); timers_init(); uart_init(); ble_stack_init(); device_manager_init(); db_discovery_init(); uart_c_init(); printf("Scanning ...\r\n"); // Start scanning for peripherals and initiate connection // with devices that advertise NUS UUID. scan_start(); for (;;) { power_manage(); } }
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(); } }
int main(void) { gpio_init(); uart_init(); timers_init(); __LOG("Timers initialized."); ble_stack_init(); //device_manager_init(erase_bonds); gap_params_init(); services_init(); advertising_init(); __LOG("Radio initialized."); conn_params_init(); storage_init(); contacts_init(); __LOG("Application modules initialized"); // Start execution. timers_start(); advertising_start(); __LOG("ADV Started"); scan_start(); __LOG("Scan Started"); // Enter main loop. for (;;) { power_manage(); } }
int main(void) { bool erase_bonds; // Initialize. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); buttons_leds_init(&erase_bonds); uart_init(); printf("Battery collector example\r\n"); ble_stack_init(); device_manager_init(erase_bonds); db_discovery_init(); //hrs_c_init(); bas_c_init(); pwm_init(); // Start scanning for peripherals and initiate connection // with devices that advertise Heart Rate UUID. scan_start(); for (;; ) { power_manage(); } }
static void on_write(ble_iqo_t * p_iqo, ble_evt_t * p_ble_evt) { ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; if (p_evt_write->handle == p_iqo->iqo_identify_handle.value_handle) { memcpy(&(iqo_tgt_identify.id[0]), p_evt_write->data, p_evt_write->len); iqo_tgt_identify.id[p_evt_write->len] = 0; } else if (p_evt_write->handle == p_iqo->iqo_cmd_handle.value_handle) { memcpy((uint8_t *)&iqo_tgt_cmd, p_evt_write->data, p_evt_write->len); if (iqo_tgt_cmd.cmd == 0) { // Connect to peer scan_start(); } else if (iqo_tgt_cmd.cmd == 1) { // Disconnect from peer disconnect_peer(); } else if (iqo_tgt_cmd.cmd == 2) { // Set WiFi SSID intermcu_notify(NRF_WIFI_SSID_NOTIFY, (p_evt_write->data + 1), (p_evt_write->len - 1)); } else if (iqo_tgt_cmd.cmd == 3) { // Set WiFi Password intermcu_notify(NRF_WIFI_PSWD_NOTIFY, (p_evt_write->data + 1), (p_evt_write->len - 1)); } else if (iqo_tgt_cmd.cmd == 4) { // Reset RT5350 trigger_rt5350_reset(); } } }
static void scan_process_msg_idle(void *p_handle, os_msg_t *p_msg) { scan_priv_t *p_scan_priv = (scan_priv_t *)p_handle; scan_data_t *p_scan_data = p_scan_priv->p_priv_data; MT_ASSERT(p_msg != NULL); switch(p_msg->content) { case SCAN_CMD_START_SCAN: scan_start(p_scan_priv, p_msg); log_perf(LOG_AP_SCAN, PERF_BEGIN,0,0); p_scan_data->state = SCAN_GET_TP; log_perf(LOG_AP_SCAN, PERF_GET_TP_BEGIN,0,0);//start scan log_perf(LOG_AP_SCAN, PERF_GET_ONETP_BEGIN,0,0); //The implement policy may override message process of scan process if(p_scan_priv->p_policy != NULL) { scan_policy_t *p_policy = p_scan_priv->p_policy; #if 0 //for specified bouquet id if(p_policy->require_extend_table(p_policy,DVB_TABLE_ID_BAT) && p_policy->get_bouquet_id_list != NULL) { p_scan_data->p_rt_data->bouquet_num = p_policy->get_bouquet_id_list(p_policy, p_scan_data->p_rt_data->bouquet_id); } #else #endif p_policy->msg_proc(p_policy->p_priv_data, p_msg); } break; default: break; } }
/**@brief Running Speed and Cadence Collector Handler. */ static void rscs_c_evt_handler(ble_rscs_c_t * p_rsc_c, ble_rscs_c_evt_t * p_rsc_c_evt) { uint32_t err_code; switch (p_rsc_c_evt->evt_type) { case BLE_RSCS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_rscs_c_rsc_notif_enable(p_rsc_c); APP_ERROR_CHECK(err_code); printf("Running Speed and Cadence service discovered \r\n"); if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } break; case BLE_RSCS_C_EVT_RSC_NOTIFICATION: { printf("\r\n"); APPL_LOG("[APPL]: RSC Measurement received %d \r\n", p_rsc_c_evt->params.rsc.inst_speed); printf("Speed = %d\r\n", p_rsc_c_evt->params.rsc.inst_speed); ble_rscs_meas_t rscs_measurment; rscs_measurment.is_inst_stride_len_present = p_rsc_c_evt->params.rsc.is_inst_stride_len_present; rscs_measurment.is_total_distance_present = p_rsc_c_evt->params.rsc.is_total_distance_present; rscs_measurment.is_running = p_rsc_c_evt->params.rsc.is_running; rscs_measurment.inst_stride_length = p_rsc_c_evt->params.rsc.inst_stride_length; rscs_measurment.inst_cadence = p_rsc_c_evt->params.rsc.inst_cadence; rscs_measurment.inst_speed = p_rsc_c_evt->params.rsc.inst_speed; rscs_measurment.total_distance = p_rsc_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; } default: break; } }
// Initialize touch input void touch_init(uint32_t channel_mask) { // Turn on clock gating for TSI and configure touch input SIM_SCGC5 |= SIM_SCGC5_TSI_MASK; TSI0_GENCS |= (TSI_GENCS_ESOR_MASK // Enable end of scan interrupt | TSI_GENCS_MODE(0) // Capactive sensing | TSI_GENCS_REFCHRG(4) // Reference charge 4 uA | TSI_GENCS_DVOLT(0) // Voltage rails | TSI_GENCS_EXTCHRG(7) // External osc charge | TSI_GENCS_PS(4) // Prescalar divide by 4 | TSI_GENCS_NSCN(11) // Scans per electrode | TSI_GENCS_TSIIEN_MASK // Input interrupt enable | TSI_GENCS_STPE_MASK // Enable in STOP mode ); TSI0_GENCS |= TSI_GENCS_TSIEN_MASK; // Enable touch I/O pins for Freedom board, and configure // the scan sequence PORTB_PCR16 = PORT_PCR_MUX(0); // PTB16 as touch channel 9 PORTB_PCR17 = PORT_PCR_MUX(0); // PTB17 as touch channel 10 // Read initial (baseline) values for each enabled channel int i, first_channel = 0; enable_mask = channel_mask; for(i=15; i>=0; i--) { if((1 << i) & enable_mask) { scan_start(i); while(!(TSI0_GENCS & TSI_GENCS_EOSF_MASK)) // Wait until done ; base_counts[i] = scan_data(); first_channel = i; } } // Enable TSI interrupts and start the first scan enable_irq(INT_TSI0); scan_start(first_channel); }
void test_begin(void) { NRF_LOG_INFO("Preparing the test.\r\n"); NRF_LOG_FLUSH(); switch (m_gap_role) { default: // If no connection was established, the role won't be either. // In this case, start both advertising and scanning. advertising_start(); scan_start(); break; case BLE_GAP_ROLE_PERIPH: advertising_start(); break; case BLE_GAP_ROLE_CENTRAL: scan_start(); break; } }
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 for handling the Application's system events. * * @param[in] sys_evt system event. */ static void on_sys_evt(uint32_t sys_evt) { switch(sys_evt) { case NRF_EVT_FLASH_OPERATION_SUCCESS: case NRF_EVT_FLASH_OPERATION_ERROR: if (m_memory_access_in_progress) { m_memory_access_in_progress = false; scan_start(); } break; default: // No implementation needed. break; } }
int main(void) { // Initialization of various modules. uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 }; APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code); APP_ERROR_CHECK(err_code); app_trace_init(); APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); APP_GPIOTE_INIT(1); err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),NULL); APP_ERROR_CHECK(err_code); printf("Heart rate collector example\r\n"); ble_stack_init(); device_manager_init(); db_discovery_init(); hrs_c_init(); bas_c_init(); // Start scanning for peripherals and initiate connection // with devices that advertise Heart Rate UUID. scan_start(); for (;; ) { power_manage(); } }
/**@brief Function for application main entry. */ int main(void) { // Initialization of various modules. debug_init(); leds_init(); buttons_init(); ble_stack_init(); client_handling_init(); device_manager_init(); // Start scanning for devices. scan_start(); for (;;) { power_manage(); } }
void ble_central_mode_init(void) { // app_trace_init(); // APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); // APP_GPIOTE_INIT(1); // err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),NULL); // APP_ERROR_CHECK(err_code); // printf("Heart rate collector example\r\n"); ble_stack_init(); device_manager_init(); db_discovery_init(); hrs_c_init(); bas_c_init(); // Start scanning for peripherals and initiate connection // with devices that advertise Heart Rate UUID. scan_start(); }
/**@brief Heart Rate Collector Handler. */ static void hrs_c_evt_handler(ble_hrs_c_t * p_hrs_c, ble_hrs_c_evt_t * p_hrs_c_evt) { uint32_t err_code; switch (p_hrs_c_evt->evt_type) { case BLE_HRS_C_EVT_DISCOVERY_COMPLETE: // Initiate bonding. err_code = dm_security_setup_req(&m_dm_device_handle); 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); printf("Heart rate service discovered \r\n"); if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } break; case BLE_HRS_C_EVT_HRM_NOTIFICATION: { APPL_LOG("[APPL]: HR Measurement received %d \r\n", p_hrs_c_evt->params.hrm.hr_value); 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; } default: break; } }
/**@brief Function for application main entry. */ int main(void) { // Initialization of various modules. app_trace_init(); LEDS_CONFIGURE(LEDS_MASK); LEDS_OFF(LEDS_MASK); buttons_init(); ble_stack_init(); client_handling_init(); device_manager_init(); // Start scanning for devices. scan_start(); for (;;) { power_manage(); } }
/**@brief Function for disabling the use of whitelist for scanning. */ static void whitelist_disable(void) { uint32_t err_code; if ((m_scan_mode == BLE_WHITELIST_SCAN) && !m_whitelist_temporarily_disabled) { m_whitelist_temporarily_disabled = true; err_code = sd_ble_gap_scan_stop(); if (err_code == NRF_SUCCESS) { scan_start(); } else if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } } m_whitelist_temporarily_disabled = true; }
static SPIBoolean switch_callback (const AccessibleKeystroke *key, void *user_data) { static SPIBoolean is_down = FALSE; if (key->type == SPI_KEY_RELEASED) { g_print ("switch up\n"); is_down = FALSE; scan_stop (key->timestamp); } else if (!is_down) { g_print ("switch down\n"); is_down = TRUE; scan_start (key->timestamp); } /* catch the first, ignore the rest (will repeat) until keyrelease */ return FALSE; }
void test_terminate(void) { m_run_test = false; m_notif_enabled = false; m_mtu_exchanged = false; m_conn_interval_configured = false; if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { NRF_LOG_INFO("Disconnecting...\r\n"); ret_code_t err_code; err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); if (err_code != NRF_SUCCESS) { NRF_LOG_ERROR("sd_ble_gap_disconnect() failed: 0x%0x.\r\n", err_code); } } else { if (m_board_role == BOARD_TESTER) { m_print_menu = true; } if (m_board_role == BOARD_DUMMY) { if (m_gap_role == BLE_GAP_ROLE_PERIPH) { advertising_start(); } else { scan_start(); } } } }
/**@brief Function for initiating advertising and scanning. */ static void adv_scan_start(void) { ret_code_t err_code; uint32_t count; //check if there are no flash operations in progress err_code = fs_queued_op_count_get(&count); APP_ERROR_CHECK(err_code); if(count == 0) { // 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); } }
static void *scan_thread_func(void *data) { (void)data; pthread_mutex_lock(&scan_mutex); memset(&status, 0, sizeof(scan_status_t)); status.active = true; status.start_time = time(NULL); pthread_mutex_unlock(&scan_mutex); db_simple_exec("BEGIN TRANSACTION", NULL); scan(); db_simple_exec("COMMIT TRANSACTION", NULL); pthread_mutex_lock(&scan_mutex); thread_running = false; status.active = false; status.end_time = time(NULL); if (restart) { restart = 0; interrupted = 0; pthread_mutex_unlock(&scan_mutex); scan_start(); return NULL; } pthread_mutex_unlock(&scan_mutex); /** @todo FIXME */ if (interrupted) { exit(0); } return NULL; }
int main(int argc, char* argv[]) { musicd_start_time = time(NULL); config_init(); config_set_hook("log-level", log_level_changed); config_set_hook("log-time-format", log_time_format_changed); config_set("log-level", "debug"); config_set_hook("directory", directory_changed); config_set("config", "~/.musicd.conf"); config_set("directory", "~/.musicd"); config_set("bind", "any"); config_set("port", "6800"); config_set_hook("image-prefix", scan_image_prefix_changed); config_set("image-prefix", "front,cover,jacket"); config_set("server-name", "musicd server"); if (config_load_args(argc, argv)) { musicd_log(LOG_FATAL, "main", "invalid command line arguments"); print_usage(argv[0]); return -1; } if (config_get_value("help")) { print_usage(argv[0]); return 0; } if (config_get_value("version")) { print_version(); return 0; } if (!config_to_bool("no-config") && config_load_file(config_to_path("config"))) { musicd_log(LOG_FATAL, "main", "could not read config file"); return -1; } /* Reload command line arguments - this is because the config file might have * overwritten them, and the command line has the highest priority. */ config_load_args(argc, argv); confirm_directory(); musicd_log(LOG_INFO, "main", "musicd version %s", MUSICD_VERSION_STRING); srand(time(NULL)); av_register_all(); avcodec_register_all(); av_lockmgr_register(&musicd_av_lockmgr); av_log_set_level(AV_LOG_QUIET); if (db_open()) { musicd_log(LOG_FATAL, "library", "can't open database"); return -1; } if (library_open()) { musicd_log(LOG_FATAL, "main", "could not open library"); return -1; } if (cache_open()) { musicd_log(LOG_FATAL, "main", "could not open cache"); return -1; } if (server_start()) { musicd_log(LOG_FATAL, "main", "could not start server"); return -1; } signal(SIGUSR1, start_scan_signal); scan_start(); while (1) { sleep(1); } return 0; }
/**@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; const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_ADV_REPORT: { data_t adv_data; data_t type_data; // Initialize advertisement report for parsing. adv_data.p_data = (uint8_t *)p_gap_evt->params.adv_report.data; adv_data.data_len = p_gap_evt->params.adv_report.dlen; err_code = adv_report_parse(BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, &adv_data, &type_data); if (err_code != NRF_SUCCESS) { // Compare short local name in case complete name does not match. err_code = adv_report_parse(BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE, &adv_data, &type_data); } // Verify if short or complete name matches target. if (err_code == NRF_SUCCESS) { uint16_t extracted_uuid; // UUIDs found, look for matching UUID for (uint32_t u_index = 0; u_index < (type_data.data_len/UUID16_SIZE); u_index++) { UUID16_EXTRACT(&extracted_uuid,&type_data.p_data[u_index * UUID16_SIZE]); APPL_LOG("\t[APPL]: %x\r\n",extracted_uuid); if(extracted_uuid == TARGET_UUID) { // Stop scanning. err_code = sd_ble_gap_scan_stop(); if (err_code != NRF_SUCCESS) { APPL_LOG("[APPL]: Scan stop failed, reason %d\r\n", err_code); } err_code = bsp_indication_set(BSP_INDICATE_IDLE); APP_ERROR_CHECK(err_code); m_scan_param.selective = 0; // Initiate connection. err_code = sd_ble_gap_connect(&p_gap_evt->params.adv_report.peer_addr, &m_scan_param, &m_connection_param); m_whitelist_temporarily_disabled = false; if (err_code != NRF_SUCCESS) { APPL_LOG("[APPL]: Connection Request Failed, reason %d\r\n", err_code); } break; } } } break; } case BLE_GAP_EVT_TIMEOUT: if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN) { APPL_LOG("[APPL]: Scan timed out.\r\n"); scan_start(); } else if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN) { APPL_LOG("[APPL]: Connection Request timed out.\r\n"); } break; case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: // Accepting parameters requested by peer. 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; default: break; } }
/**@brief Callback handling device manager events. * * @details This function is called to notify the application of device manager events. * * @param[in] p_handle Device Manager Handle. For link related events, this parameter * identifies the peer. * @param[in] p_event Pointer to the device manager event. * @param[in] event_status Status of the event. */ static ret_code_t device_manager_event_handler(const dm_handle_t * p_handle, const dm_event_t * p_event, const ret_code_t event_result) { uint32_t err_code; switch (p_event->event_id) { case DM_EVT_CONNECTION: { APPL_LOG("[APPL]: >> DM_EVT_CONNECTION\r\n"); #ifdef ENABLE_DEBUG_LOG_SUPPORT ble_gap_addr_t * peer_addr; peer_addr = &p_event->event_param.p_gap_param->params.connected.peer_addr; #endif // ENABLE_DEBUG_LOG_SUPPORT APPL_LOG("[APPL]:[%02X %02X %02X %02X %02X %02X]: Connection Established\r\n", peer_addr->addr[0], peer_addr->addr[1], peer_addr->addr[2], peer_addr->addr[3], peer_addr->addr[4], peer_addr->addr[5]); err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); APP_ERROR_CHECK(err_code); m_conn_handle = p_event->event_param.p_gap_param->conn_handle; m_dm_device_handle = (*p_handle); // Discover peer's services. err_code = ble_db_discovery_start(&m_ble_db_discovery, p_event->event_param.p_gap_param->conn_handle); APP_ERROR_CHECK(err_code); m_peer_count++; if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } APPL_LOG("[APPL]: << DM_EVT_CONNECTION\r\n"); break; } case DM_EVT_DISCONNECTION: { APPL_LOG("[APPL]: >> DM_EVT_DISCONNECTION\r\n"); memset(&m_ble_db_discovery, 0 , sizeof (m_ble_db_discovery)); err_code = bsp_indication_set(BSP_INDICATE_IDLE); APP_ERROR_CHECK(err_code); if (m_peer_count == MAX_PEER_COUNT) { scan_start(); } m_peer_count--; APPL_LOG("[APPL]: << DM_EVT_DISCONNECTION\r\n"); break; } case DM_EVT_SECURITY_SETUP: { APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); // Slave securtiy request received from peer, if from a non bonded device, // initiate security setup, else, wait for encryption to complete. err_code = dm_security_setup_req(&m_dm_device_handle); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); break; } case DM_EVT_SECURITY_SETUP_COMPLETE: { APPL_LOG("[APPL]: >> DM_EVT_SECURITY_SETUP_COMPLETE\r\n"); // Heart rate service discovered. Enable notification of Heart Rate Measurement. err_code = ble_hrs_c_hrm_notif_enable(&m_ble_hrs_c); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]: << DM_EVT_SECURITY_SETUP_COMPLETE\r\n"); break; } case DM_EVT_LINK_SECURED: APPL_LOG("[APPL]: >> DM_LINK_SECURED_IND\r\n"); APPL_LOG("[APPL]: << DM_LINK_SECURED_IND\r\n"); break; case DM_EVT_DEVICE_CONTEXT_LOADED: APPL_LOG("[APPL]: >> DM_EVT_LINK_SECURED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_LOADED\r\n"); break; case DM_EVT_DEVICE_CONTEXT_STORED: APPL_LOG("[APPL]: >> DM_EVT_DEVICE_CONTEXT_STORED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_STORED\r\n"); break; case DM_EVT_DEVICE_CONTEXT_DELETED: APPL_LOG("[APPL]: >> DM_EVT_DEVICE_CONTEXT_DELETED\r\n"); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]: << DM_EVT_DEVICE_CONTEXT_DELETED\r\n"); break; default: break; } return NRF_SUCCESS; }
/**@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; } }
int main(void) { log_init(); leds_init(); timer_init(); counter_init(); buttons_init(); ble_stack_init(); gap_params_init(); conn_params_init(); gatt_init(); advertising_data_set(); server_init(); client_init(); // Default ATT MTU size and connection interval are set at compile time. gatt_mtu_set(m_test_params.att_mtu); // Data Length Extension (DLE) is on by default. // Enable the Connection Event Length Extension. conn_evt_len_ext_set(m_test_params.conn_evt_len_ext_enabled); NRF_LOG_INFO("ATT MTU example started.\r\n"); NRF_LOG_INFO("Press button 3 on the board connected to the PC.\r\n"); NRF_LOG_INFO("Press button 4 on other board.\r\n"); NRF_LOG_FLUSH(); board_role_select(); if (m_board_role == BOARD_TESTER) { m_print_menu = true; } if (m_board_role == BOARD_DUMMY) { advertising_start(); scan_start(); } // Enter main loop. NRF_LOG_DEBUG("Entering main loop.\r\n"); for (;;) { if (m_print_menu) { menu_print(); } if (is_test_ready()) { m_run_test = true; test_run(); } if (!NRF_LOG_PROCESS()) { wait_for_event(); } } }
void start_scan_signal(int signum) { (void)signum; musicd_log(LOG_INFO, "main", "caught USR1, starting scan"); scan_start(); }
/**@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 Callback handling device manager events. * * @details This function is called to notify the application of device manager events. * * @param[in] p_handle Device Manager Handle. For link related events, this parameter * identifies the peer. * @param[in] p_event Pointer to the device manager event. * @param[in] event_status Status of the event. */ static api_result_t device_manager_event_handler(const dm_handle_t * p_handle, const dm_event_t * p_event, const api_result_t event_result) { uint32_t err_code; switch(p_event->event_id) { case DM_EVT_CONNECTION: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_CONNECTION\r\n", p_handle->connection_id); #ifdef ENABLE_DEBUG_LOG_SUPPORT ble_gap_addr_t * p_peer_addr; p_peer_addr = &p_event->event_param.p_gap_param->params.connected.peer_addr; #endif // ENABLE_DEBUG_LOG_SUPPORT APPL_LOG("[APPL]:[%02X %02X %02X %02X %02X %02X]: Connection Established\r\n", p_peer_addr->addr[0], p_peer_addr->addr[1], p_peer_addr->addr[2], p_peer_addr->addr[3], p_peer_addr->addr[4], p_peer_addr->addr[5]); APPL_LOG("\r\n"); APPL_LOG("[APPL]:[CI 0x%02X]: Requesting GATT client create\r\n", p_handle->connection_id); err_code = client_handling_create(p_handle, p_event->event_param.p_gap_param->conn_handle); APP_ERROR_CHECK(err_code); m_peer_count++; if (m_peer_count < MAX_PEER_COUNT) { scan_start(); } APPL_LOG("[APPL]:[0x%02X] << DM_EVT_CONNECTION\r\n", p_handle->connection_id); break; case DM_EVT_DISCONNECTION: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DISCONNECTION\r\n", p_handle->connection_id); err_code = client_handling_destroy(p_handle); APP_ERROR_CHECK(err_code); if (m_peer_count == MAX_PEER_COUNT) { scan_start(); } m_peer_count--; APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DISCONNECTION\r\n", p_handle->connection_id); break; case DM_EVT_SECURITY_SETUP: { dm_handle_t handle = (*p_handle); APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); // Slave securtiy request received from peer, if from a non bonded device, // initiate security setup, else, wait for encryption to complete. err_code = dm_security_setup_req(&handle); APP_ERROR_CHECK(err_code); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP\r\n", p_handle->connection_id); break; } case DM_EVT_SECURITY_SETUP_COMPLETE: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_SECURITY_SETUP_COMPLETE, result 0x%08X\r\n", p_handle->connection_id, event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_SECURITY_SETUP_COMPLETE\r\n", p_handle->connection_id); break; case DM_EVT_LINK_SECURED: APPL_LOG("[APPL]:[0x%02X] >> DM_LINK_SECURED_IND, result 0x%08X\r\n", p_handle->connection_id, event_result); APPL_LOG("[APPL]:[0x%02X] << DM_LINK_SECURED_IND\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_LOADED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_LINK_SECURED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_LOADED\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_STORED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DEVICE_CONTEXT_STORED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_STORED\r\n", p_handle->connection_id); break; case DM_EVT_DEVICE_CONTEXT_DELETED: APPL_LOG("[APPL]:[0x%02X] >> DM_EVT_DEVICE_CONTEXT_DELETED\r\n", p_handle->connection_id); APP_ERROR_CHECK(event_result); APPL_LOG("[APPL]:[0x%02X] << DM_EVT_DEVICE_CONTEXT_DELETED\r\n", p_handle->connection_id); break; default: break; } // Relay the event to client handling module. err_code = client_handling_dm_event_handler(p_handle, p_event, event_result); APP_ERROR_CHECK(err_code); return NRF_SUCCESS; }