@ -83,59 +83,30 @@ static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGT
/**@brief NUS UUID. */
static ble_uuid_t const m_nus_uuid = { . uuid = BLE_UUID_NUS_SERVICE , . type = NUS_SERVICE_UUID_TYPE } ;
/**@brief Function for handling asserts in the SoftDevice.
*
* @ details This function is called in case of an assert in the SoftDevice .
*
* @ warning This handler is only an example and is not meant for the final product . You need to analyze
* how your product is supposed to react in case of assert .
* @ warning On assert from the SoftDevice , the system can only recover on reset .
*
* @ param [ in ] line_num Line number of the failing assert call .
* @ param [ in ] p_file_name File name of the failing assert call .
*/
void assert_nrf_callback ( uint16_t line_num , const uint8_t * p_file_name ) { app_error_handler ( 0xDEADBEEF , line_num , p_file_name ) ; }
/**@brief Function for handling the Nordic UART Service Client errors.
*
* @ param [ in ] nrf_error Error code containing information about what went wrong .
*/
static void nus_error_handler ( uint32_t nrf_error ) { APP_ERROR_HANDLER ( nrf_error ) ; }
/**@brief Function to start scanning. */
static void scan_start ( void ) {
void assert_nrf_callback ( uint16_t line_num , const uint8_t * p_file_name ) { app_error_handler ( 0xDEADBEEF , line_num , p_file_name ) ; }
static void nus_error_handler ( uint32_t nrf_error ) { APP_ERROR_HANDLER ( nrf_error ) ; }
static void scan_start ( void ) {
ret_code_t ret ;
ret = nrf_ble_scan_start ( & m_scan ) ;
APP_ERROR_CHECK ( ret ) ;
ret = bsp_indication_set ( BSP_INDICATE_SCANNING ) ;
APP_ERROR_CHECK ( ret ) ;
}
/**@brief Function for handling Scanning Module events.
*/
static void scan_evt_handler ( scan_evt_t const * p_scan_evt ) {
ret_code_t err_code ;
switch ( p_scan_evt - > scan_evt_id ) {
case NRF_BLE_SCAN_EVT_CONNECTING_ERROR : {
err_code = p_scan_evt - > params . connecting_err . err_code ;
APP_ERROR_CHECK ( err_code ) ;
} break ;
case NRF_BLE_SCAN_EVT_CONNECTED : {
ble_gap_evt_connected_t const * p_connected = p_scan_evt - > params . connected . p_connected ;
/ / Scan is automatically stopped by the connection .
NRF_LOG_INFO ( " Connecting to target %02x%02x%02x%02x%02x%02x " , p_connected - > peer_addr . addr [ 0 ] , p_connected - > peer_addr . addr [ 1 ] , p_connected - > peer_addr . addr [ 2 ] , p_connected - > peer_addr . addr [ 3 ] , p_connected - > peer_addr . addr [ 4 ] , p_connected - > peer_addr . addr [ 5 ] ) ;
} break ;
case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT : {
NRF_LOG_INFO ( " Scan timed out. " ) ;
scan_start ( ) ;
} break ;
default :
break ;
}
@ -148,7 +119,7 @@ static char m_target_periph_name1[20] = "M1001"; /**< Name of the device we tr
static char m_target_periph_name2 [ 20 ] = " iflytop " ; /**< Name of the device we try to connect to. This name is searched in the scan report data*/
static char m_target_periph_name3 [ 20 ] = " ADS1293 " ; /**< Name of the device we try to connect to. This name is searched in the scan report data*/
static char m_target_periph_name4 [ 20 ] = " IFLYTOP " ; /**< Name of the device we try to connect to. This name is searched in the scan report data*/
static char m_target_periph_name5 [ 20 ] = " M1003 " ; /**< Name of the device we try to connect to. This name is searched in the scan report data*/
static char m_target_periph_name5 [ 20 ] = " M1003 " ; /**< Name of the device we try to connect to. This name is searched in the scan report data*/
static void scan_init ( void ) {
ret_code_t err_code ;
@ -164,6 +135,7 @@ static void scan_init(void) {
/ / err_code = nrf_ble_scan_filter_set ( & m_scan , SCAN_UUID_FILTER , & m_nus_uuid ) ;
/ / APP_ERROR_CHECK ( err_code ) ;
nrf_ble_scan_all_filter_remove ( & m_scan ) ;
APP_ERROR_CHECK ( nrf_ble_scan_filter_set ( & m_scan , SCAN_NAME_FILTER , m_target_periph_name0 ) ) ;
APP_ERROR_CHECK ( nrf_ble_scan_filter_set ( & m_scan , SCAN_NAME_FILTER , m_target_periph_name1 ) ) ;
@ -172,29 +144,11 @@ static void scan_init(void) {
APP_ERROR_CHECK ( nrf_ble_scan_filter_set ( & m_scan , SCAN_NAME_FILTER , m_target_periph_name4 ) ) ;
APP_ERROR_CHECK ( nrf_ble_scan_filter_set ( & m_scan , SCAN_NAME_FILTER , m_target_periph_name5 ) ) ;
/ / err_code = nrf_ble_scan_filters_enable ( & m_scan , NRF_BLE_SCAN_NAME_FILTER , false ) ;
/ / APP_ERROR_CHECK ( err_code ) ;
err_code = nrf_ble_scan_filters_enable ( & m_scan , NRF_BLE_SCAN_NAME_FILTER , false ) ;
APP_ERROR_CHECK ( err_code ) ;
}
/**@brief Function for handling database discovery events.
*
* @ details This function is a callback function to handle events from the database discovery module .
* Depending on the UUIDs that are discovered , this function forwards 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 ) { ble_nus_c_on_db_disc_evt ( & m_ble_nus_c , p_evt ) ; }
/**@brief Function for handling characters received by the Nordic UART Service (NUS).
*
* @ details This function takes a list of characters of length data_len and prints the characters out on UART .
* If @ ref ECHOBACK_BLE_UART_DATA is set , the data is sent back to sender .
*/
static void app_uart_force_put ( uint8_t data ) {
ret_code_t ret_val ;
do {
@ -266,18 +220,7 @@ void uart_event_handle(app_uart_evt_t* p_event) {
case APP_UART_DATA_READY :
UNUSED_VARIABLE ( app_uart_get ( & data_array [ data_array_index ] ) ) ;
data_array_index + + ;
if ( data_array_index > = ( m_ble_nus_max_data_len ) ) {
NRF_LOG_DEBUG ( " Ready to send data over BLE NUS " ) ;
NRF_LOG_HEXDUMP_DEBUG ( data_array , data_array_index ) ;
do {
ret_val = ble_nus_c_string_send ( & m_ble_nus_c , data_array , data_array_index ) ;
if ( ( ret_val ! = NRF_ERROR_INVALID_STATE ) & & ( ret_val ! = NRF_ERROR_RESOURCES ) ) {
APP_ERROR_CHECK ( ret_val ) ;
}
} while ( ret_val = = NRF_ERROR_RESOURCES ) ;
if ( data_array_index > BLE_NUS_MAX_DATA_LEN ) {
data_array_index = 0 ;
}
break ;
@ -602,16 +545,21 @@ static void idle_state_handle(void) {
}
APP_TIMER_DEF ( m_uart_send_tmr ) ; / / × ´ Ì ¬ » ú Ç ý ¶ ¯ ¶ ¨ Ê ± Æ ÷
static void process_uart_rx_packet ( uint8_t * data , size_t len ) {
ret_code_t ret_val ;
do {
ret_val = ble_nus_c_string_send ( & m_ble_nus_c , data , len ) ;
if ( ( ret_val ! = NRF_ERROR_INVALID_STATE ) & & ( ret_val ! = NRF_ERROR_RESOURCES ) ) {
APP_ERROR_CHECK ( ret_val ) ;
}
} while ( ret_val = = NRF_ERROR_RESOURCES ) ;
}
static void uart_send_tmr_cb ( void * p_context ) { / /
static uint32_t last_data_array_index ;
ret_code_t ret_val ;
if ( data_array_index ! = 0 & & data_array_index = = last_data_array_index ) {
do {
ret_val = ble_nus_c_string_send ( & m_ble_nus_c , data_array , data_array_index ) ;
if ( ( ret_val ! = NRF_ERROR_INVALID_STATE ) & & ( ret_val ! = NRF_ERROR_RESOURCES ) ) {
APP_ERROR_CHECK ( ret_val ) ;
}
} while ( ret_val = = NRF_ERROR_RESOURCES ) ;
process_uart_rx_packet ( data_array , data_array_index ) ;
data_array_index = 0 ;
last_data_array_index = 0 ;
}