![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: main.cpp
- Revision:
- 115:a1e1e2e60a2c
- Parent:
- 114:472502b31a12
- Child:
- 116:39a41ebb675c
diff -r 472502b31a12 -r a1e1e2e60a2c main.cpp --- a/main.cpp Mon Apr 20 14:40:09 2015 +0000 +++ b/main.cpp Thu Apr 23 13:57:54 2015 +0000 @@ -160,7 +160,6 @@ uint16_t pkg_wdt = RX_CB_IDLE; uint16_t eth_wdt = ETH_CONNECT_TIMEOUT; - uint32_t uptime = 0; led1 = 0; send_msg("Ready"); @@ -174,32 +173,21 @@ debug_uart3 = false; Timer test_timer; + + reset_leds(); /*------------------------------------------ main loop ---------------------------------------------------------------*/ - while( true ) { - reset_leds(); - // need be removed; - + while( true ) + { if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size(); - if( sync_timer.read() > 5 ) { + if( sync_timer.read() > 5 ) + { sync_timer.reset(); - if( debug_cks == true ) { - pcks_s = true; - } + if( debug_cks == true ) { pcks_s = true; } - if( debug_alive == true ) { - pshowcb = true; - } - - - { - // debug_msg("Enviei"); - // send2callboxes( __build_cb_package__( 5000, 5000, __INVITE__, - // ( char * )buffer, 0x12, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - // v_cb -> add ( new Call_Box ( 5010, 5010 ) ); - } + if( debug_alive == true ) { pshowcb = true; } if( !( ++count % 15 ) ) { if( eth_status ) { @@ -240,15 +228,38 @@ stats = true; } - if( stats ) { + if( stats ) + { char str[ 200 ]; - snprintf( str, 200, "\n\rReceived Pkgs::\n\r Boot :: %u\n\r Registry :: %u\n\r Invite :: %u\n\r Audio :: %u\n\r Telemetry :: %u\n\r CB_stats :: %u\n\r CB_bye :: %u\n\r Prompt :: %u\n\r Flood :: %u\n\r Bootloader_cbx :: %u\n\r", - boot_counter, registry_counter, invite_counter, audio_counter, telemetry_counter, cb_stats_counter, cb_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); + snprintf( str, 200, "" + "\n\rReceived Pkgs::\n\r " + "Boot :: %u\n\r " + "Registry :: %u\n\r " + "Invite :: %u\n\r " + "Audio :: %u\n\r " + "Telemetry :: %u\n\r " + "CB_stats :: %u\n\r " + "CB_bye :: %u\n\r " + "Prompt :: %u\n\r " + "Flood :: %u\n\r " + "Bootloader_cbx :: %u\n\r", + boot_counter, + registry_counter, + invite_counter, + audio_counter, + telemetry_counter, + cb_stats_counter, + cb_bye_counter, + prompt_counter, + flood_counter, + bootloader_cbx_counter + ); send_msg( str ); stats =false; } - if( list ) { + if( list ) + { uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); if( ( max_ext % 2 ) == 0 ) missed_cb++; @@ -400,7 +411,8 @@ long_list = false; } - if( pshowcb == true ) { + if( pshowcb == true ) + { send_msg("Registered %d ( of %d ) CBx ( %d - %d ) -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, ts->remain_timeslices(), v_call->size() ); for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); @@ -511,29 +523,6 @@ missed_pkg--; xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); - /* - { - uint16_t e, p; - e = buffer[0]; - e <<= 8; - e |= buffer[1]; - p = buffer[2]; - p <<= 8; - p |= buffer[3]; - //if (e != (p+100)) { - if( ( buffer[0] == 0x14 ) && ( buffer[1] == 0x0a ) ) { - int i; - char s[400], ss[4];; - strcpy(s,"OPS-PKG:"); - for (i=0; i<40; i++) { - sprintf(ss," %02x",buffer[i]); - strcat(s,ss); - } - debug_msg( s ); - } - } - */ - if( debug_cpld ) { rx = true; tx = true; @@ -555,16 +544,15 @@ data = __parse_vz_pkg__( &ext, &port, &type, buffer ); - if( data != NULL ) { + if( data != NULL ) + { if( min_ext == 0 ) min_ext = ext; if( ext > max_ext ) max_ext = ext; if( ext < min_ext ) min_ext = ext; - if( debug_fw ){ - fw_cbx_pkg( ext, port, ( char *)buffer ); - } + if( debug_fw ){ fw_cbx_pkg( ext, port, ( char *)buffer ); } if( type == __TELEMETRY__ ) telemetry_counter++; @@ -595,8 +583,7 @@ switch( type ) { case __INVITE__ : { - if( debug_main || debug_invite ) debug_msg("Invite Ack from %d on msg_id %d", ext, cb->get_msg_id() ); - //cb->first_invite_response_ok(); + if( debug_main || debug_invite ) debug_msg("Invite Ack from %d on msg_id %d -- Cbx seqNum %d", ext, cb->get_msg_id(), data[ 0 ] ); break; } case __CB_BYE__ : { @@ -612,7 +599,7 @@ if( debug_main || debug_aging ) debug_msg("ACK from %d in pkg :: %d :: type %d", ext, cb->get_msg_id(), type ); } } - if( type != __REGISTRY__ && type != __CB_BYE__ ) type = __DO_NOTHING__; + if( type != __REGISTRY__ && type != __CB_BYE__ && type != __INVITE__ ) type = __DO_NOTHING__; if( type == __CB_BYE__ ){ VZ_call * call = __find_Call__( v_call, ext ); if( call != NULL ){ @@ -629,7 +616,8 @@ } else type = __DO_NOTHING__; } - if( main_test == true ) { + if( main_test == true ) + { main_test = false; send_msg("CB_New (%u) -- CB_Delete (%u)", cb_new_counter, cb_delete_counter ); send_msg("SIP_New (%u) -- SIP_Delete (%u)", sip_new_counter, sip_delete_counter ); @@ -647,35 +635,7 @@ send_msg("Sizeof Vector :: %u", sizeof( Vector ) ); send_msg("Sizeof Timeslice :: %u", sizeof( Timeslice ) ); send_msg("Sizeof Watchdog :: %u", sizeof( Watchdog ) ); - //VZ_call * call = new VZ_call( 1234, 9876, 413, 12093 ); - //v_call -> add ( call ); } - - //static int wdt_count = 0; - /* - if( registry_test == true ) { - registry_test = false; - - if( wdt_count++ > 5 || v_cb->size() < 66 ) { - wdt_count = 0; - - if( type == __DO_NOTHING__ ) { - if( next_value < ( 5002 + __MAX_CB_IN_A_BRANCH__ - 2 ) ) { - //type = __REGISTRY__; - data = buffer; - ext = next_value; - port = next_value++; - } else { - if( nex_test_registry > ( 5002 + __MAX_CB_IN_A_BRANCH__ - 1 - 2 ) ) nex_test_registry = 5002; - //type = __REGISTRY__; - data = buffer; - ext = nex_test_registry; - port = nex_test_registry++; - } - } - } - } - */ if ( registra ) { @@ -1638,9 +1598,27 @@ pcks_s = true; } + // exibe os contadores de cks de pacotes recebidos na interface CBx->Header, e outros contadores relacionados if( pcks_s == true ) { - send_msg("PKG_CKS OK: %d :: PKG_CKS ERR: %d :: PKG_0: %d :: Out_of_range: %d :: Miss_Pkg :: %d" - "\n\r> TX_delayed :: %d :: TX_Counter :: %d :: RX_Counter :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg, delayed_pkg_to_cb, cpld_pkg_tx_counter, cpld_pkg_rx_counter ); + send_msg( + "\r\n" + " PKG_CKS OK: %d ( %003.2f )\r\n" + " PKG_CKS ERR: %d ( %003.2f )\r\n" + " PKG_0: %d ( %003.2f )\r\n" + " Out_of_range: %d ( %003.2f )\r\n" + " Miss_Pkg: %d\r\n" + " TX_delayed: %d\n\r" + " TX_Counter: %d\n\r" + " RX_Counter: %d\r\n", + pkg_cksok, ( cpld_pkg_rx_counter == 0 ) ? 0.00 : ( ( double ) pkg_cksok / cpld_pkg_rx_counter ) * 100, + pkg_ckserr, ( cpld_pkg_rx_counter == 0 ) ? 0.00 : ( ( double ) pkg_ckserr / cpld_pkg_rx_counter ) * 100, + pkg_zero, ( cpld_pkg_rx_counter == 0 ) ? 0.00 : ( ( double ) pkg_zero / cpld_pkg_rx_counter ) * 100, + out_of_range, ( cpld_pkg_rx_counter == 0 ) ? 0.00 : ( ( double ) out_of_range / cpld_pkg_rx_counter ) * 100, + missed_pkg, + delayed_pkg_to_cb, + cpld_pkg_tx_counter, + cpld_pkg_rx_counter + ); pcks_s = false; } @@ -1770,12 +1748,13 @@ case __INVITE__ : { if( drop_invite_pkg ){ - debug_msg("Dropando invite pck msg id :: %d", data[ 0 ] ); + debug_msg("Dropando invite pck from %d msg id :: %d", ext, data[ 0 ] ); break; } invite_counter++; if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); Call_Box * cb = __find_CB__( v_cb, ext ); + if( cb == NULL ) { if( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { if( debug_main ) debug_msg( "Adding CBx :: %i", ext ); @@ -1789,12 +1768,26 @@ } } } - if( cb != NULL ) { + + if( cb != NULL ) + { + cb->invite_retry_count_reset(); + cb->set_msg_id( data[ 0 ] ); - if( cb->status == cb_idle ){ - cb->set_invite_response_ok(); - cb->invite_retry_count_reset(); + + if( cb->status == cb_idle ){ + cb->set_invite_response_ok(); + } else { + data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); + + cb->set_invite_response_pending(); + + cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); + + send2callboxes( __build_cb_package__( ext, port, __INVITE__, + ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); } + invite_handler( v_call, v_cb, ts, cb ); } } @@ -1846,7 +1839,6 @@ case __BOOTLOADER_CBX__ : { bootloader_cbx_counter++; - //int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); } break; @@ -1893,33 +1885,37 @@ break; case __AUDIO__ : { audio_counter++; + if ( received_audio_from_cb ) { + debug_msg("audio pkg from %d", ext ); + } + VZ_call * call = __find_Call__( v_call, ext ); if( call != NULL ) { - if ( drop_rtp_from_cbx_pkg ) - { + if ( drop_rtp_from_cbx_pkg ){ led2 = !led2; break; - } - else - { + } else { char * pkg = call->build_eth_package( data + 2 ); call->send_message( pkg ); call->cbx_pkg_idle_timer_reset(); - Call_Box * cb = __find_CB__( v_cb, ext ); - if( cb != NULL ){ - cb->set_invite_response_ok(); - cb->invite_retry_count_reset(); - } } - } else { - if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); + } + + Call_Box * cb = __find_CB__( v_cb, ext ); + if( cb != NULL ){ + if ( cb->get_invite_response() == false ) { + cb->set_invite_response_ok(); + cb->invite_retry_count_reset(); + } else { + if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); + } } } break; }// fim switch { // rajada - if( invite_retry_timer.read_ms() > 30 ) + if( invite_retry_timer.read_ms() > 20 ) { invite_retry_timer.reset(); @@ -1955,31 +1951,47 @@ cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_invite ) debug_msg("resend invite OK to Cbx : ( %d, %d )", cb->get_ext(), cb->get_port() ); - } - - if( cb->get_invite_retry_count() == 0 ) + } + + if ( __find_Call__( v_call, cb->get_ext () ) == NULL ) { - cb->send_bye(); - - ts->return_timeslice( cb->get_timeslice() ); - cb->set_timeslice( 0x00 ); - - cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); - - for( register uint8_t i = 0; i < v_call->size(); i++ ) + + if ( ( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) ) ) { - VZ_call * call = ( VZ_call * )v_call->get_element( i ); - if( call->get_cb_ext() == cb->get_ext() ) + const int rtp_tmp_length = 172; + char rtp_src_tmp_write_buffer [ __CB_BUFFER_SIZE__ ]; + uint8_t rtp_dst_tmp_write_buffer [ __CB_BUFFER_SIZE__ ]; + + for ( register int i = 0; i < rtp_tmp_length; i++ ) rtp_src_tmp_write_buffer [ i ] = 'U'; // 0x55 + + send2callboxes( __build_cb_package__( cb->get_ext (), cb->get_port (), __AUDIO__, + rtp_src_tmp_write_buffer, __AUDIO__, rtp_tmp_length, rtp_dst_tmp_write_buffer ) ); + } else { + if ( cb->get_invite_retry_count() == 0 ) { - v_call->remove_element( i ); - if( call != NULL ) delete( call ); - break; + cb->send_bye(); + + ts->return_timeslice( cb->get_timeslice() ); + cb->set_timeslice( 0x00 ); + + cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); + + for ( register uint8_t i = 0; i < v_call->size(); i++ ) + { + VZ_call * call = ( VZ_call * )v_call->get_element( i ); + if( call->get_cb_ext() == cb->get_ext() ) + { + v_call->remove_element( i ); + if( call != NULL ) delete( call ); + break; + } + } + + if( debug_invite ) debug_msg( "-- No audio pkgs --" ); + set_status( cb->status, cb_idle ); + set_status( cb->sip->status, sip_idle ); } } - - if( debug_invite ) debug_msg( "-- No audio pkgs --" ); - set_status( cb->status, cb_idle ); - set_status( cb->sip->status, sip_idle ); } } } @@ -2028,12 +2040,6 @@ } } - - //refresh( v_cb, buffer, write_buffer, NULL ); - - //Fixme pensar melhor nessa parte durante ligacoes, pode complicar com muitos cbx ... - //if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); - // check sip messages only for cbx in call ? int ext_to__be_removed = sip_manager( v_cb ); if( ext_to__be_removed > 0x00 ) { @@ -2110,11 +2116,6 @@ } */ - //invite_pgk_retry_manager( v_call, v_cb, buffer, write_buffer ); - - /* escuta se existe algum procedimento de gravação de cbx */ - //bl_handler(); - /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */ tx_buffer_ring_buffer_handler();