H_U32 _StorageCount(void) { H_U32 count = 0; // H_U32 retval = 0; // Request clearing of blocks pstorage_access_status_get(&count); return count; }
/**@brief Function to start scanning. */ static void scan_start(void) { ble_gap_whitelist_t whitelist; ble_gap_addr_t * p_whitelist_addr[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; ble_gap_irk_t * p_whitelist_irk[BLE_GAP_WHITELIST_IRK_MAX_COUNT]; uint32_t err_code; uint32_t count; // Verify if there is any flash access pending, if yes delay starting scanning until // it's complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_memory_access_in_progress = true; return; } // Initialize whitelist parameters. whitelist.addr_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; whitelist.irk_count = 0; whitelist.pp_addrs = p_whitelist_addr; whitelist.pp_irks = p_whitelist_irk; // Request creating of whitelist. err_code = dm_whitelist_create(&m_dm_app_id,&whitelist); APP_ERROR_CHECK(err_code); if (((whitelist.addr_count == 0) && (whitelist.irk_count == 0)) || (m_scan_mode != BLE_WHITELIST_SCAN) || (m_whitelist_temporarily_disabled)) { // No devices in whitelist, hence non selective performed. m_scan_param.active = 0; // Active scanning set. m_scan_param.selective = 0; // Selective scanning not set. m_scan_param.interval = SCAN_INTERVAL;// Scan interval. m_scan_param.window = SCAN_WINDOW; // Scan window. m_scan_param.p_whitelist = NULL; // No whitelist provided. m_scan_param.timeout = 0x0000; // No timeout. } else { // Selective scanning based on whitelist first. m_scan_param.active = 0; // Active scanning set. m_scan_param.selective = 1; // Selective scanning not set. m_scan_param.interval = SCAN_INTERVAL;// Scan interval. m_scan_param.window = SCAN_WINDOW; // Scan window. m_scan_param.p_whitelist = &whitelist; // Provide whitelist. m_scan_param.timeout = 0x001E; // 30 seconds timeout. } err_code = sd_ble_gap_scan_start(&m_scan_param); APP_ERROR_CHECK(err_code); err_code = bsp_indication_set(BSP_INDICATE_SCANNING); APP_ERROR_CHECK(err_code); }
static bool flash_busy(void) { uint32_t q_count, err_code; err_code = pstorage_access_status_get(&q_count); APP_ERROR_CHECK(err_code); if (q_count != 0) { return true; } else { return false; } }
/**@brief Function for dispatching a system event to interested modules. * * @details This function is called from the System event interrupt handler after a system * event has been received. * * @param[in] sys_evt System stack event. */ static void sys_evt_dispatch(uint32_t sys_evt) { pstorage_sys_event_handler(sys_evt); uint32_t count; uint32_t err_code; // Check if storage access is in progress. err_code = pstorage_access_status_get(&count); if ((err_code == NRF_SUCCESS) && (count == 0)) { storage_access_complete_handler(); } }
/**@breif Function to start scanning. */ static void scan_start(void) { uint32_t err_code; uint32_t count; // Verify if there is any flash access pending, if yes delay starting scanning until // it's complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_memory_access_in_progress = true; return; } err_code = sd_ble_gap_scan_start(&m_scan_param); APP_ERROR_CHECK(err_code); }
/**@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(); } }
/**@brief Function for putting the chip in System OFF Mode */ static void system_off_mode_enter(void) { uint32_t err_code; uint32_t count; // Verify if there is any flash access pending, if yes delay starting advertising until // it's complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_memory_access_in_progress = true; return; } err_code = sd_power_system_off(); APP_ERROR_CHECK(err_code); }
/**@brief Function for starting advertising. */ static void advertising_start(void) { uint32_t err_code; uint32_t count; // Verify if there is any flash access pending, if yes delay starting advertising until // it's complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_memory_access_in_progress = true; return; } err_code = sd_ble_gap_adv_start(&m_adv_params); APP_ERROR_CHECK(err_code); nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO); }
/**@brief Function for dispatching a system event to interested modules. * * @details This function is called from the System event interrupt handler after a system * event has been received. * * @param[in] sys_evt System stack event. */ static void sys_evt_dispatch(uint32_t sys_evt) { uint32_t err_code; uint32_t count; pstorage_sys_event_handler(sys_evt); // Verify if BLE bond data storage access is in progress or not. if (m_app_initialized == true) { err_code = pstorage_access_status_get(&count); if ((err_code == NRF_SUCCESS) && (count == 0)) { storage_access_complete_handler(); } } else { m_app_initialized = true; } }
/**@brief Application main function. */ int main(void) { // Initialize peripherals leds_init(); timers_init(); buttons_init(); // Initialize S310 SoftDevice ble_ant_stack_init(); // Initialize Bluetooth stack parameters. gap_params_init(); advertising_init(); services_init(); conn_params_init(); // Initialize ANT HRM recieve channel. ant_hrm_rx_init(); #ifdef BONDING_ENABLE uint32_t err_code; uint32_t count; // Initialize device manager. device_manager_init(); err_code = pstorage_access_status_get(&count); if ((err_code == NRF_SUCCESS) && (count == 0)) #endif // BONDING_ENABLE { storage_access_complete_handler(); } // Enter main loop. for (;;) { power_manage(); } }
/**@brief Function for starting advertising. */ static void advertising_start(void) { uint32_t err_code; uint32_t count; // Verify if there is any flash access pending, if yes delay starting advertising until // its complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_advertising_mode |= PS_MEMORY_ACCESS_IN_PROGRESS; return; } err_code = sd_ble_gap_adv_start(&m_adv_params); APP_ERROR_CHECK(err_code); err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); APP_ERROR_CHECK(err_code); }
/**@brief Function for starting advertising. */ static void advertising_start(void) { uint32_t err_code; ble_gap_adv_params_t adv_params; ble_gap_whitelist_t whitelist; uint32_t count; // Verify if there is any flash access pending, if yes delay starting advertising until // it's complete. err_code = pstorage_access_status_get(&count); APP_ERROR_CHECK(err_code); if (count != 0) { m_memory_access_in_progress = true; return; } // Initialize advertising parameters with defaults values memset(&adv_params, 0, sizeof(adv_params)); adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND; adv_params.p_peer_addr = NULL; adv_params.fp = BLE_GAP_ADV_FP_ANY; adv_params.p_whitelist = NULL; // Configure advertisement according to current advertising state switch (m_advertising_mode) { case BLE_NO_ADV: m_advertising_mode = BLE_FAST_ADV_WHITELIST; // fall through. case BLE_FAST_ADV_WHITELIST: { ble_gap_addr_t * p_whitelist_addr[BLE_GAP_WHITELIST_ADDR_MAX_COUNT]; ble_gap_irk_t * p_whitelist_irk[BLE_GAP_WHITELIST_IRK_MAX_COUNT]; whitelist.addr_count = BLE_GAP_WHITELIST_ADDR_MAX_COUNT; whitelist.irk_count = BLE_GAP_WHITELIST_IRK_MAX_COUNT; whitelist.pp_addrs = p_whitelist_addr; whitelist.pp_irks = p_whitelist_irk; err_code = dm_whitelist_create(&m_app_handle, &whitelist); APP_ERROR_CHECK(err_code); if ((whitelist.addr_count != 0) || (whitelist.irk_count != 0)) { adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ; adv_params.p_whitelist = &whitelist; advertising_init(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED); m_advertising_mode = BLE_FAST_ADV; } else { m_advertising_mode = BLE_SLOW_ADV; } adv_params.interval = APP_ADV_INTERVAL_FAST; adv_params.timeout = APP_FAST_ADV_TIMEOUT; err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_WHITELIST); APP_ERROR_CHECK(err_code); break; } case BLE_FAST_ADV: advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE); adv_params.interval = APP_ADV_INTERVAL_FAST; adv_params.timeout = APP_FAST_ADV_TIMEOUT; m_advertising_mode = BLE_SLOW_ADV; err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); APP_ERROR_CHECK(err_code); break; case BLE_SLOW_ADV: advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE); adv_params.interval = APP_ADV_INTERVAL_SLOW; adv_params.timeout = APP_SLOW_ADV_TIMEOUT; m_advertising_mode = BLE_SLEEP; err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING_SLOW); APP_ERROR_CHECK(err_code); break; default: // No implementation needed. break; } // Start advertising. err_code = sd_ble_gap_adv_start(&adv_params); APP_ERROR_CHECK(err_code); }
uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode) { uint32_t err_code; ble_gap_adv_params_t adv_params; m_adv_mode_current = advertising_mode; uint32_t count = 0; // Verify if there are any pending flash operations. If so, delay starting advertising until // the flash operations are complete. err_code = pstorage_access_status_get(&count); if (err_code == NRF_ERROR_INVALID_STATE) { // Pstorage is not initialized, i.e. not in use. count = 0; } else if (err_code != NRF_SUCCESS) { return err_code; } if (count != 0) { m_advertising_start_pending = true; return NRF_SUCCESS; } // Fetch the peer address. ble_advertising_peer_address_clear(); if ( ((m_adv_modes_config.ble_adv_directed_enabled) && m_adv_mode_current == BLE_ADV_MODE_DIRECTED) ||((m_adv_modes_config.ble_adv_directed_slow_enabled) && m_adv_mode_current == BLE_ADV_MODE_DIRECTED_SLOW)) { if (m_evt_handler != NULL) { m_peer_addr_reply_expected = true; m_evt_handler(BLE_ADV_EVT_PEER_ADDR_REQUEST); } else { m_peer_addr_reply_expected = false; } } // If a mode is disabled, continue to the next mode. I.e fast instead of direct, slow instead of fast, idle instead of slow. if ( (m_adv_mode_current == BLE_ADV_MODE_DIRECTED) &&(!m_adv_modes_config.ble_adv_directed_enabled || !peer_address_exists(m_peer_address.addr))) { m_adv_mode_current = BLE_ADV_MODE_DIRECTED_SLOW; } if ( (m_adv_mode_current == BLE_ADV_MODE_DIRECTED_SLOW) &&(!m_adv_modes_config.ble_adv_directed_slow_enabled || !peer_address_exists(m_peer_address.addr))) { m_adv_mode_current = BLE_ADV_MODE_FAST; } if (!m_adv_modes_config.ble_adv_fast_enabled && m_adv_mode_current == BLE_ADV_MODE_FAST) { m_adv_mode_current = BLE_ADV_MODE_SLOW; } if (!m_adv_modes_config.ble_adv_slow_enabled && m_adv_mode_current == BLE_ADV_MODE_SLOW) { m_adv_mode_current = BLE_ADV_MODE_IDLE; m_adv_evt = BLE_ADV_EVT_IDLE; } // Fetch the whitelist. if ( (m_evt_handler != NULL) && (m_adv_mode_current == BLE_ADV_MODE_FAST || m_adv_mode_current == BLE_ADV_MODE_SLOW) && (m_adv_modes_config.ble_adv_whitelist_enabled) && (!m_whitelist_temporarily_disabled)) { m_whitelist_reply_expected = true; m_evt_handler(BLE_ADV_EVT_WHITELIST_REQUEST); } else { m_whitelist_reply_expected = false; } // Initialize advertising parameters with default values. memset(&adv_params, 0, sizeof(adv_params)); adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND; adv_params.p_peer_addr = NULL; adv_params.fp = BLE_GAP_ADV_FP_ANY; adv_params.p_whitelist = NULL; // Set advertising parameters and events according to selected advertising mode. switch (m_adv_mode_current) { case BLE_ADV_MODE_DIRECTED: LOG("[ADV]: Starting direct advertisement.\r\n"); adv_params.p_peer_addr = &m_peer_address; // Directed advertising. adv_params.type = BLE_GAP_ADV_TYPE_ADV_DIRECT_IND; adv_params.timeout = 0; adv_params.interval = 0; m_adv_evt = BLE_ADV_EVT_DIRECTED; break; case BLE_ADV_MODE_DIRECTED_SLOW: LOG("[ADV]: Starting direct advertisement.\r\n"); adv_params.p_peer_addr = &m_peer_address; // Directed advertising. adv_params.type = BLE_GAP_ADV_TYPE_ADV_DIRECT_IND; adv_params.timeout = m_adv_modes_config.ble_adv_directed_slow_timeout; adv_params.interval = m_adv_modes_config.ble_adv_directed_slow_interval; m_adv_evt = BLE_ADV_EVT_DIRECTED_SLOW; break; case BLE_ADV_MODE_FAST: adv_params.timeout = m_adv_modes_config.ble_adv_fast_timeout; adv_params.interval = m_adv_modes_config.ble_adv_fast_interval; if ( whitelist_has_entries(&m_whitelist) && m_adv_modes_config.ble_adv_whitelist_enabled && !m_whitelist_temporarily_disabled) { adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ; adv_params.p_whitelist = &m_whitelist; m_advdata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED; err_code = ble_advdata_set(&m_advdata, NULL); if(err_code != NRF_SUCCESS) { return err_code; } m_adv_evt = BLE_ADV_EVT_FAST_WHITELIST; LOG("[ADV]: Starting fast advertisement with whitelist.\r\n"); } else { m_adv_evt = BLE_ADV_EVT_FAST; LOG("[ADV]: Starting fast advertisement.\r\n"); } break; case BLE_ADV_MODE_SLOW: adv_params.interval = m_adv_modes_config.ble_adv_slow_interval; adv_params.timeout = m_adv_modes_config.ble_adv_slow_timeout; if ( whitelist_has_entries(&m_whitelist) && m_adv_modes_config.ble_adv_whitelist_enabled && !m_whitelist_temporarily_disabled) { adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ; adv_params.p_whitelist = &m_whitelist; m_advdata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED; err_code = ble_advdata_set(&m_advdata, NULL); if(err_code != NRF_SUCCESS) { return err_code; } m_adv_evt = BLE_ADV_EVT_SLOW_WHITELIST; LOG("[ADV]: Starting slow advertisement with whitelist.\r\n"); } else { m_adv_evt = BLE_ADV_EVT_SLOW; LOG("[ADV]: Starting slow advertisement.\r\n"); } break; default: break; } if (m_adv_mode_current != BLE_ADV_MODE_IDLE) { err_code = sd_ble_gap_adv_start(&adv_params); if(err_code != NRF_SUCCESS) { return err_code; } } if (m_evt_handler != NULL) { m_evt_handler(m_adv_evt); } return NRF_SUCCESS; }