Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
120:770f00554b1e
Parent:
119:ee6a53069455
Child:
121:ee02790d00b7
--- a/main.cpp	Thu Apr 30 15:42:41 2015 +0000
+++ b/main.cpp	Mon May 04 17:25:29 2015 +0000
@@ -1,4 +1,6 @@
-#include "main_includes_configs.h"
+//#include "main_app_var_configs.h"
+//#include "main_app_functions.h"
+#include "main_app_var_configs.h"
 
 int main()
 {
@@ -18,7 +20,7 @@
     {    
         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();
 
@@ -50,8 +52,8 @@
         if ( r_stats )
         {
             reset_stats ();
+            stats = true;
             r_stats = false;
-            stats = true;
         }
 
         if ( stats )
@@ -84,24 +86,19 @@
             show_current_time = false;;
         }
         
-        if ( test_flag_unusual ) _this_is_a_only_test ();
-        
-        if( show_sip == true ){
+        if( show_sip ){
+            show_cb_sip ( v_cb );
             show_sip = false;
-            send_msg(":: Sip :: %u", v_cb->size() );
-            for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
-                Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
-                send_msg("ext :: %d -- port :: %d -- timer %d", cb->get_sip_ext(), cb->get_sip_port(), cb->get_timer() );
-            }
         } 
 
         if( pflood == true ) flood();
 
         if( debug_eth ) {
+            send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" );
             debug_eth = false;
-            send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" );
         }
 
+        // chechando se existe um pacote vindo do cbx pendente
         if( status != WAITING ) {
             pkg_wdt = RX_CB_IDLE;
             xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, CB_BUFFER_SIZE );
@@ -199,26 +196,11 @@
 
         if( sizes == true )
         {
+            show_sizes ();
             sizes = 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 );
-            send_msg("RTP_header_New (%u) -- RTP_header_Delete (%u)", rtp_header_new_counter, rtp_header_delete_counter );
-            send_msg("RTP_body_New (%u) -- RTP_body_Delete (%u)", rtp_body_new_counter, rtp_body_delete_counter );
-            send_msg("Call_New (%u) -- Call_Delete (%u)", call_new_counter, call_delete_counter );
-            send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter );
-            send_msg("Memory is %s", ( memory_is_over ) ? "Over" : "Ok" );
-            send_msg("Missed_Pkg :: %d ::", missed_pkg );
-            send_msg("Sizeof Sip :: %u", sizeof( Sip ) );
-            send_msg("Sizeof Call_Box :: %u", sizeof ( Call_Box ) );
-            send_msg("Sizeof VZ_call :: %u", sizeof( VZ_call ) );
-            send_msg("Sizeof RTP :: %u", sizeof( RTP ) );
-            send_msg("Sizeof RTP_Header :: %u", sizeof( RTP_Header ) );
-            send_msg("Sizeof RTP_Body :: %u", sizeof( RTP_Body ) );
-            send_msg("Sizeof Vector :: %u", sizeof( Vector ) );
-            send_msg("Sizeof Timeslice :: %u", sizeof( Timeslice ) );
-            send_msg("Sizeof Watchdog :: %u", sizeof( Watchdog ) );
         }
         
+        // usado pra testes
         if ( registra )
         {
             int internal_ext = 8000;
@@ -228,408 +210,22 @@
             }    
         }
         
+        check_udp_packages_pending ( v_cb );        
+        
+        if( dshow_rtp == true )
         {
-            fd_set fdSet;
-            FD_ZERO(&fdSet);
-
-            for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
-                Call_Box * cb = (Call_Box *)v_cb->get_element( i );
-                FD_SET( cb->get_sip_socket_fd(), &fdSet);
-            }
-            
-            /* Recepcao de pacotes UDP para atualizacao de callboxes */
-            FD_SET( udp_bl_client.get_fd(), &fdSet);
-            
-            /* Recepcao de pacotes UDP para "tickagem" do watchdog */
-            FD_SET( udp_wdt_client.get_fd(), &fdSet);
-            
-            // adiciona o socket de comandos prompt-UDP-ETH
-            FD_SET( udp_client.get_fd(), &fdSet );
-            
-            // adiciona o socket de pedido de clock para o servidor
-            FD_SET( clock_sock.get_fd(), &fdSet );
-            
-            struct timeval t;
-            t.tv_sec = 0;
-            t.tv_usec = 0;
-            int ret = lwip_select( FD_SETSIZE, &fdSet, NULL, NULL, &t );
+            show_rtp ();
+            dshow_rtp = false;
+        }
             
-            if ((udp_bl_timer.read() > 30) and (bl_start_flag)) {
-                udp_bl_timer.stop();
-                udp_bl_timer.reset();
-                bl_start_flag = 0;
-                if (bl_ts != 0) {
-                    ts->return_timeslice( bl_ts );
-                    bl_ts = 0;
-                }
-                if (bl_cb != NULL) {
-                    bl_cb->set_timeslice( 0 );
-                    set_status( bl_cb->status, cb_idle );
-                    bl_cb = NULL;
-                }
-            }
-
-            if( ret > 0 )
-            {  
-                for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
-                    Call_Box * cb = (Call_Box *)v_cb->get_element( i );
-                    int fd =  cb->get_sip_socket_fd();
-                    if( FD_ISSET( fd, &fdSet ) ) {
-                        int rcv = cb->sip_udp_incomming_pkg();
-                    }
-                }
-                
-                /* Tratamento dos pacotes de bootloader vindo do servidor */
-                if( FD_ISSET( udp_bl_client.get_fd(), &fdSet ) ) {
-                    if( udp_bl_client.receiveFrom( udp_bl_server, bl_recv_buffer, sizeof( bl_recv_buffer ) ) > 0 ) {
-                        uint16_t cnt = 0;
-                        if (debug_bootloader) {
-                            pc.printf("\r\nPACOTE SRV->HDR {");
-                            for (cnt = 0;cnt < UDP_BL_SIZE;cnt++) {
-                                if ((cnt % 30) == 0) {
-                                    pc.printf("\r\n  ");
-                                    pc.printf(hex16(cnt));
-                                    pc.printf(" : ");
-                                }
-                                pc.printf(hex8(bl_recv_buffer[cnt]));
-                                pc.printf(", ");
-                            }
-                            pc.printf("\r\n}");
-                        }
-                        bl_peer = ((uint8_t)bl_recv_buffer[0])*256 + (uint8_t)bl_recv_buffer[1];
-                        bl_send_buffer[0] = bl_recv_buffer[0];
-                        bl_send_buffer[1] = bl_recv_buffer[1];
-                        for (cnt = 2;cnt < UDP_BL_SIZE;cnt++) {
-                            bl_send_buffer[cnt] = 0;
-                        }
-                        
-                        if ((bl_ts == 0) and !(bl_start_flag)) {
-                            bl_ts = ts->get_timeslice();
-                        }
-                        
-                        /* caso nao haja timeslice disponivel informar servidor */
-                        if ((bl_ts == 0) and !(bl_start_flag)) {
-                            strncpy(bl_send_buffer + 2,"cbxdead\x00",8);
-                            udp_bl_client.sendTo( udp_bl_server, bl_send_buffer, strlen( bl_send_buffer ) );
-                        } else {
-                            
-                            /* pacote para verificar se o callbox esta disponivel */
-                            if (!(strncmp(bl_recv_buffer + 2,"avaiable?",9))) {
-                                bl_start_flag = 1;
-                                udp_bl_timer.start();
-                                udp_bl_timer.reset();
-                                strncpy(bl_send_buffer + 2,"cbxalive\x00",9);
-                                if (debug_bootloader) {
-                                    pc.printf("\r\nPACOTE HDR->SRV {");
-                                    for (cnt = 0;cnt < UDP_BL_SIZE;cnt++) {
-                                        if ((cnt % 30) == 0) {
-                                            pc.printf("\r\n  ");
-                                            pc.printf(hex16(cnt));
-                                            pc.printf(" : ");
-                                        }
-                                        pc.printf(hex8(bl_send_buffer[cnt]));
-                                        pc.printf(", ");
-                                    }
-                                    pc.printf("\r\n}");
-                                }
-                                udp_bl_client.sendTo( udp_bl_server, bl_send_buffer, strlen( bl_send_buffer ) );
-                            }
-                            
-                            /* pacote para indicar o termino do processo de atualizacao */
-                            else if (!(strncmp(bl_recv_buffer + 2,"finished",8))) {
-                                bl_start_flag = 0;
-                                if (bl_ts != 0) {
-                                    ts->return_timeslice( bl_ts );
-                                    bl_ts = 0;
-                                }
-                                if (bl_cb != NULL) {
-                                    bl_cb->set_timeslice( 0 );
-                                    set_status( bl_cb->status, cb_idle );
-                                    bl_cb = NULL;
-                                }
-                            }
-                            
-                            /* pacotes a serem repassados para o callbox */
-                            else if (bl_start_flag) {
-                                uint16_t bl_cnt = 0;
-                                udp_bl_timer.reset();
-                                /* pacote de ERASE enviado para o cbx */
-                                /* neste momento a cabeceira sabe que o servidor se comunicou com o callbox */
-                                if (!(strncmp(bl_recv_buffer + 2,"\x45",1))) {
-                                    bl_cb = __find_CB__( v_cb, bl_peer );
-                                    if (bl_cb != NULL) {
-                                        bl_cb->set_timeslice( bl_ts );
-                                        set_status( bl_cb->status, cb_bootloader );
-                                    }
-                                }
-                                /* pacote de OK enviado para o cbx */
-                                /* neste momento a cabeceira desaloca o timeslice do callbox  */
-                                if (!(strncmp(bl_recv_buffer + 2,"\x4f\x00\x00\x00",4))) {
-                                    ts->return_timeslice( bl_ts );
-                                    bl_ts = 0;
-                                    if (bl_cb != NULL) {
-                                        bl_cb->set_timeslice( 0 );
-                                    }
-                                }
-                                bl_cbx_buffer[0] = bl_ts;
-                                for (bl_cnt = 0; bl_cnt < BL_SIZE; bl_cnt++) {
-                                    bl_cbx_buffer[bl_cnt + 1] = bl_recv_buffer[bl_cnt + 2];
-                                }
-                                if (debug_bootloader) {
-                                    pc.printf("\r\nPACOTE HDR->CBX {");
-                                    for (cnt = 0;cnt < BL_SIZE + 1;cnt++) {
-                                        if ((cnt % 30) == 0) {
-                                            pc.printf("\r\n  ");
-                                            pc.printf(hex16(cnt));
-                                            pc.printf(" : ");
-                                        }
-                                        pc.printf(hex8(bl_cbx_buffer[cnt]));
-                                        pc.printf(", ");
-                                    }
-                                    pc.printf("\r\n}");
-                                }
-                                if (bl_cb != NULL) {
-                                    bl_port = bl_cb->get_port();
-                                } else {
-                                    bl_port = bl_peer;
-                                }
-                                send2callboxes( build_cb_package( bl_peer, bl_port, BOOTLOADER_CBX,bl_cbx_buffer, 0, BL_SIZE + 1, write_buffer) );
-                            }
-                        }
-                    }
-                }
-                
-                // verifica o socket do prompt-UDP-ETH                
-                if( FD_ISSET( udp_client.get_fd(), &fdSet ) ) {
-                    char to_prompt_process[ PROMPT_UDP_COMMAND_SIZE ];
-                    for( register int i = 0; i < PROMPT_UDP_COMMAND_SIZE; i++ ) to_prompt_process[ i ] = 0;
-                    
-                    int prompt_process_msg_rcv = udp_client.receiveFrom( udp_server, to_prompt_process, ( sizeof( to_prompt_process ) - 1 ) );
-                    
-                    to_prompt_process[ prompt_process_msg_rcv ] = 0;
-                    if( prompt_process_msg_rcv == -1 )
-                    {
-                        if( debug_reconnect ) send_msg("Reconnect Prompt Process");
-                        reconnect_udp_prompt_process();
-                        miss_prompt_udp_rcv_pkg++;
-                    } 
-                        else if( prompt_process_msg_rcv > 0 )
-                    {
-                        udp_query = true;
-                        prompt_process( to_prompt_process, prompt_process_msg_rcv );
-                    }
-                } 
-                
-                // verifica o socket do watchdog
-                if( FD_ISSET( udp_wdt_client.get_fd(), &fdSet ) ) {
-                    char wake_msg[ 768 ];
-                    Endpoint udp_wdt_server;
-                    
-                    int wake_msg_rcv = udp_wdt_client.receiveFrom( udp_wdt_server, wake_msg, sizeof( wake_msg ) );
-                    
-                    if( wake_msg_rcv == -1 )
-                    {
-                        if( debug_reconnect ) send_msg("Reconnect Extern wdt");
-                        reconnect_extern_wdt_socket();
-                        miss_wdt_send_pkg++;
-                    } 
-                        else if( wake_msg_rcv > 0 )
-                    {
-                        if( !( strncmp( wake_msg, "alive", 5 ) ) ) {
-                            // Just ckeck but not set 'alive?'
-                            // 'alive*' - force wdt tick right now
-                            // Ckecking and set 'alive'
-                            bool question_alive = ( wake_msg[ 5 ] == '?' );
-                            if( wake_msg[ 5 ] == '*' ) wdt.kick();
-                            
-                            snprintf( wake_msg, ( 768 - 1 ),"wdt:%u,%u,%u,%c,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u:", 
-                                uptime, 
-                                invite_counter, 
-                                external_wdt, 
-                                ( wdt.WatchdogCausedReset() ) ? '1' : '0', 
-                                cb_new_counter, 
-                                v_cb->size(),
-                                ts->remain_timeslices(), 
-                                sip_socket_send_failure, 
-                                v_call->size(),
-                                pkg_cksok,
-                                pkg_ckserr,
-                                pkg_zero,
-                                out_of_range,
-                                missed_pkg,
-                                delayed_pkg_to_cb,
-                                cpld_pkg_tx_counter,
-                                cpld_pkg_rx_counter,
-                                eth_wdt,
-                                pkg_wdt,
-                                miss_fw_send_pkg,
-                                miss_prompt_udp_send_pkg,
-                                miss_sip_registry_send_pkg,
-                                miss_sip_invite_send_pkg,
-                                miss_sip_bye_send_pkg,
-                                miss_sip_unregistry_send_pkg,
-                                miss_sip_ok_send_pkg,
-                                miss_sip_rcv_bye_send_pkg,
-                                miss_wdt_send_pkg,
-                                miss_prompt_udp_send_pkg,
-                                miss_ftp_udp_send_pkg,
-                                miss_prompt_udp_rcv_pkg,
-                                miss_closk_send_pkg
-                            );
-            
-                            wake_msg[ 768 - 1 ] = 0;
-                            int send = udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) );
-                            if( send != strlen( wake_msg ) )
-                            {
-                                if( debug_reconnect ) debug_msg("Reconnect Extern wdt (%d, %d)", send, strlen( wake_msg ) );
-                                reconnect_extern_wdt_socket();
-                                miss_wdt_send_pkg++;
-                            }
-        
-                            if( ( (!question_alive) && ( cb_new_counter <= MAX_CB_IN_A_BRANCH ) && ( cb_new_counter >= 2 ) ) ) {
-                                external_wdt = EXTERN_WDT_IDLE;
-                            }
-                        } else if( !( strncmp( wake_msg, "reset", 5 ) ) ) {
-                            external_wdt = 0;
-        
-                            sprintf( wake_msg, "rst:%u:", uptime );
-                            int send = udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) );
-                            if( send != strlen( wake_msg ) )
-                            {
-                                if( debug_reconnect ) send_msg("Reconnect Extern wdt");
-                                reconnect_extern_wdt_socket();
-                                miss_wdt_send_pkg++;
-                            }
-                        }
-                    }
-                }
-                
-                if( FD_ISSET( clock_sock.get_fd(), &fdSet ) )
-                {
-                    update_clock ();
-                }
-            }
+        // usado pra test
+        if ( frtp )
+        {
+            fuck_rtp ( v_cb );
+            frtp = false;
         }
         
-        if ( cogumelo )
-        {
-            cogumelo = false;
-            for( register int i = 0; i < v_cb->size(); i++ )
-            {
-                Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
-                if ( cb )
-                {
-                       set_status( cb->status, cb_on_call );
-                       set_status( cb->sip->status, sip_on_call );
-                }
-                    else
-                {
-                    debug_msg("%d congumelo missed", i );    
-                }
-            }
-            
-        }
-        
-        if ( gnomo )
-        {
-            gnomo = false;
-            for( register int i = 0; i < v_cb->size(); i++ )
-            {
-                Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
-                if ( cb )
-                {
-                       set_status( cb->status, cb_idle );
-                       set_status( cb->sip->status, sip_idle );
-                }
-                    else
-                {
-                    debug_msg("%d gnomo missed", i );    
-                }
-            }
-            
-        }
-        
-        
-        if( dshow_rtp == true ){
-            dshow_rtp = false;
-            send_msg(":: RTP :: %u", v_cb->size() );
-            
-            int ext_list[ MAX_CB_IN_A_BRANCH ];
-                
-            if( v_cb->size() >= 1 ) {
-                for( register int i = 0; i < v_cb->size(); i++ )
-                {
-                    ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext();
-                }
-                qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp );
-            }   
-            
-            for ( register uint8_t i = 0; i < v_cb->size(); i++ )
-            {
-                Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] );
-                if( cb != NULL )
-                {
-                    send_msg("CBX ( %d,  %d ) - SIP  ( %d,  %d ) - RTP ( %d )", 
-                        cb -> get_ext (),
-                        cb -> get_port (),
-                        cb -> get_sip_ext (), 
-                        cb -> get_sip_port (), 
-                        cb -> get_rtp_port ()
-                    );
-                }
-            }
-        }
-        
-        if( malasia )
-        {
-            malasia = false;
-            // conclusao, mudar o nro nao impala, talvez mudar o nro no pacote ...
-            Call_Box * cb = __find_CB__( v_cb, 5016 );
-            if( cb != NULL )
-            {
-                cb -> set_rtp_port ( cb -> get_rtp_port () * 10 + 1 );
-                send_msg("CBX ( %d,  %d ) - SIP  ( %d,  %d ) - RTP ( %d )", 
-                cb -> get_ext (),
-                cb -> get_port (),
-                cb -> get_sip_ext (), 
-                cb -> get_sip_port (), 
-                cb -> get_rtp_port ()
-                );
-            }    
-        }
-        
-        if( york )
-        {
-            york = false;
-            Call_Box * cb = __find_CB__( v_cb, 5016 );
-            if( cb != NULL )
-            {
-                cb -> set_rtp_port ( 80 );
-                send_msg("CBX ( %d,  %d ) - SIP  ( %d,  %d ) - RTP ( %d )", 
-                cb -> get_ext (),
-                cb -> get_port (),
-                cb -> get_sip_ext (), 
-                cb -> get_sip_port (), 
-                cb -> get_rtp_port ()
-                );
-            }    
-        }
-        
-        if ( frtp )
-        {
-            frtp = false;
-            Call_Box * cb = __find_CB__( v_cb, frtp_target );
-            if ( cb != NULL )
-            {
-                cb -> set_rtp_port ( -1008789032 );
-            }
-                else
-            {
-                debug_msg("frtp fail");
-            }
-            
-        }
-        
+        // usado pra test
         if ( rescue_rtp )
         {
             rescue_rtp = false;
@@ -645,823 +241,115 @@
             
         }
         
-        if( america )
-        {
-            america = false;
-            Call_Box * cb = __find_CB__( v_cb, 5016 );
-            if( cb != NULL )
-            {
-                cb -> set_rtp_port ( 8929415 );
-                send_msg("CBX ( %d,  %d ) - SIP  ( %d,  %d ) - RTP ( %d )", 
-                cb -> get_ext (),
-                cb -> get_port (),
-                cb -> get_sip_ext (), 
-                cb -> get_sip_port (), 
-                cb -> get_rtp_port ()
-                );
-            }    
-        }
-        
-        //begin debug_print
         if ( print_v_cb )
         {
+            v_cb->print_yourself ();
             print_v_cb = false;
-            v_cb->print_yourself ();
         }
         
         if ( print_v_call )
         {
-            print_v_call = false;
             v_call->print_yourself ();
+            print_v_call = false;         
         }
         
         if ( print_cb_var )
         {
+            show_cb_content ();
             print_cb_var = false;
-            Call_Box * cb = __find_CB__ ( v_cb, print_this_cb );
-            if ( cb != NULL )
-            {
-                cb -> print_yourself ();
-            }
-                else
-            {
-                if( print_values )
-                {
-                    debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-                }
-                    else 
-                {
-                    send_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-                }
-            }
         }
         
         if ( print_cb_all )
         {
+            show_cb_content_all ();
             print_cb_all = false;
-            
-            if ( v_cb -> size () == 0 )
-            {
-                send_msg("known CBx :: 0");
-            }
-            
-            for ( register uint8_t i = 0; i < v_cb->size(); i++ )
-            {
-                Call_Box * cb = (Call_Box *)v_cb->get_element( i );
-                        
-                if ( cb != NULL )
-                {
-                    cb -> print_yourself ();
-                }
-                    else
-                {
-                    if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", i );
-                }
-            }
         }
         
         if ( print_hex_cb_var )
         {
+            show_hex_cb_content ();
             print_hex_cb_var = false;
-            uint8_t * ptr = ( uint8_t * ) __find_CB__ ( v_cb, print_hex_this_cb );
-    
-            if ( ptr != NULL )
-            {
-                send_msg ("Values :: %p\r\n", ( void *) ptr );            
-                for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                {
-                    if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                    
-                    if ( ( i % 32 ) == 0 )
-                    {
-                        if( i != 0 )
-                        {
-                            if( debug_uart3 ) pc.printf("\n\r> ");
-                        }  
-                            else
-                        {
-                            if( debug_uart3 ) pc.printf(" ");
-                        }
-                    }        
-                        else
-                    {
-                        if( debug_uart3 ) pc.printf(" ");
-                    }
-                }
-                if ( debug_uart3 ) pc.printf("\n\r> ");
-                
-                if ( tcp_session ) {
-                    char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ];
-                    
-                    for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0;
-                    
-                    uint8_t * ptr = ( uint8_t * ) __find_CB__ ( v_cb, print_hex_this_cb );
-                    
-                    for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                    {
-                        char tmp[ 16 ];
-                        sprintf( tmp, "%x ", *ptr++ );  
-                        strcat( aux, tmp );
-                    }
-                    
-                    strcat( aux, "\n\r\0" );
-                    tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) );
-                    tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) );
-                }
-            }
-                else
-            {
-                if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-            }
         }
         
         if ( print_hex_cb_all )
         {
+            show_hex_cb_content_all ();
             print_hex_cb_all = false;
-            
-            if ( v_cb -> size () == 0 )
-            {
-                send_msg("known CBx :: 0");    
-            }
-            
-            for ( register uint8_t j = 0; j < v_cb->size(); j++ )
-            {
-                uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
-                            
-                if ( ptr != NULL )
-                {
-                    send_msg ("Values :: %p\r\n", ( void *) ptr );
-                    for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                    {
-                        if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                        
-                        if ( ( i % 32 ) == 0 )
-                        {
-                            if( i != 0 )
-                            {
-                                if( debug_uart3 ) pc.printf("\n\r> ");
-                            }  
-                                else
-                            {
-                                if( debug_uart3 ) pc.printf(" ");
-                            }
-                        }        
-                            else
-                        {
-                            if( debug_uart3 ) pc.printf(" ");
-                        }
-                    }
-                    if ( debug_uart3 ) pc.printf("\n\r> ");
-                    
-                    if ( tcp_session ) {
-                        char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ];
-                        
-                        for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0;
-                        
-                        uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
-                        
-                        for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                        {
-                            char tmp[ 16 ];
-                            sprintf( tmp, "%x ", *ptr++ );  
-                            strcat( aux, tmp );
-                        }
-                        
-                        strcat( aux, "\n\r\0" );
-                        tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) );
-                        tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) );
-                    }
-                }
-                    else
-                {
-                    if ( print_values ) 
-                    {
-                        debug_msg ("Objeto CBx ( %d ) nao encontrado", j );
-                    }
-                        else 
-                    {
-                        send_msg ("Objeto CBx ( %d ) nao encontrado", j );
-                    }
-                }
-            }
         }
          
-        //begin Sip debug print
+        if ( print_sip_var )
         {
-            if ( print_sip_var )
-            {
-                print_sip_var = false;
-                Call_Box * cb = __find_CB__ ( v_cb, print_this_sip );
-                if ( cb != NULL )
-                {
-                    if( ( cb -> sip ) != NULL )
-                    {
-                        cb -> sip -> print_yourself ();
-                    }
-                        else
-                    {
-                        if( print_values )
-                        {
-                            debug_msg("Sip param of %d equals NULL", print_this_sip );
-                        }
-                            else 
-                        {
-                            send_msg("Sip param of %d equals NULL", print_this_sip );
-                        }
-                    }
-                }
-                    else
-                {
-                    if ( print_values ) 
-                    {
-                        debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-                    }
-                        else 
-                    {
-                        send_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-                    }                }
-            }
-            
-            if ( print_sip_all )
-            {
-                print_sip_all = false;
-                
-                if ( v_cb -> size () == 0 )
-                {
-                    send_msg("known CBx :: 0");
-                }
-                
-                for ( register uint8_t i = 0; i < v_cb->size(); i++ )
-                {
-                    Call_Box * cb = (Call_Box *)v_cb->get_element( i );
-                            
-                    if ( cb != NULL )
-                    {
-                        if( ( cb -> sip ) != NULL )
-                        {
-                            cb -> sip -> print_yourself ();
-                        }
-                            else
-                        {
-                            if( print_values )
-                            {
-                                debug_msg("Sip param of %d equals NULL", print_this_sip );
-                            }
-                                else 
-                            {
-                                send_msg("Sip param of %d equals NULL", print_this_sip );
-                            }
-                        }
-                    }
-                        else
-                    {
-                        if( print_values )
-                        {
-                            debug_msg("CBx %d not found", print_this_sip );
-                        }
-                            else 
-                        {
-                            send_msg("CBx %d not found", print_this_sip );
-                        }
-                    }
-                }
-            }
-            
-            if ( print_hex_sip_var )
-            {
-                uint8_t * ptr = NULL;
-                
-                print_hex_sip_var = false;
-                Call_Box * cb = __find_CB__ ( v_cb, print_hex_this_sip );
-                if( cb != NULL )
-                {
-                    if( ( cb -> sip ) != NULL )    
-                    ptr = ( uint8_t * ) cb -> sip;
-                }
+            show_cB_sip ();
+            print_sip_var = false;
+        }
         
-                if ( ptr != NULL )
-                {
-                    send_msg ("Values :: %p\r\n", ( void *) ptr );
-                    for ( register int i = 0; i < sizeof( Sip ); i++ )
-                    {
-                        if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                        
-                        if ( ( i % 32 ) == 0 )
-                        {
-                            if( i != 0 )
-                            {
-                                if( debug_uart3 ) pc.printf("\n\r> ");
-                            }  
-                                else
-                            {
-                                if( debug_uart3 ) pc.printf(" ");
-                            }
-                        }        
-                            else
-                        {
-                            if( debug_uart3 ) pc.printf(" ");
-                        }
-                    }
-                    if ( debug_uart3 ) pc.printf("\n\r> ");
-                    
-                    if ( tcp_session ) {
-                        char aux[ ( 32 * 3 ) + 5 ];
-                        
-                        for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
-                        
-                        ptr = NULL;
-                
-                        print_hex_sip_var = false;
-                        Call_Box * cb = __find_CB__ ( v_cb, print_hex_this_sip );
-                        if( cb != NULL )
-                        {
-                            if( ( cb -> sip ) != NULL )    
-                            ptr = ( uint8_t * ) cb -> sip;
-                        }
-                
-                        if ( ptr != NULL )
-                        {
-                            bool finished = false;
-                            volatile int i = 0;
-                            int count = 0;
-                            Timer dont_overwrite_tcp_buffer;
-                            dont_overwrite_tcp_buffer.reset ();
-                            dont_overwrite_tcp_buffer.start ();
-                            
-                            while ( !finished )
-                            {
-                                if( dont_overwrite_tcp_buffer.read_ms () >= 500 )
-                                {
-                                    dont_overwrite_tcp_buffer.reset();
-                                    for ( ; i < sizeof( Sip ); i++ )
-                                    {
-                                        char tmp[ 16 ];
-                                        sprintf( tmp, "%x ", *ptr++ );  
-                                        strcat( aux, tmp );
-                                        count++;
-                                        
-                                        if ( count >= 32  )
-                                        {
-                                            count = 0;
-                                            i++;
-                                            break;
-                                        }
-                                    }                                
-                                    strcat ( aux, "\n\r\0" );
-                                    tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
-                                    if ( i >= sizeof ( Sip ) ) finished = true;
-                                    strcpy ( aux, "\0" );
-                                }
-                            }
-                            dont_overwrite_tcp_buffer.reset();
-                            dont_overwrite_tcp_buffer.stop();
-                            tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
-                        }
-                    }
-                }
-                    else
-                {
-                    if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb );
-                }
-            }
-            /*
-            if ( print_hex_cb_all )
-            {
-                print_hex_cb_all = false;
-                
-                if ( v_cb -> size () == 0 )
-                {
-                    send_msg("known CBx :: 0");    
-                }
-                
-                for ( register uint8_t j = 0; j < v_cb->size(); j++ )
-                {
-                    uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
-                                
-                    if ( ptr != NULL )
-                    {
-                        send_msg ("Values :: %p\r\n", ( void *) ptr );
-                        for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                        {
-                            if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                            
-                            if ( ( i % 32 ) == 0 )
-                            {
-                                if( i != 0 )
-                                {
-                                    if( debug_uart3 ) pc.printf("\n\r> ");
-                                }  
-                                    else
-                                {
-                                    if( debug_uart3 ) pc.printf(" ");
-                                }
-                            }        
-                                else
-                            {
-                                if( debug_uart3 ) pc.printf(" ");
-                            }
-                        }
-                        if ( debug_uart3 ) pc.printf("\n\r> ");
-                        
-                        if ( tcp_session ) {
-                            char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ];
-                            
-                            for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0;
-                            
-                            uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j );
-                            
-                            for ( register int i = 0; i < sizeof( Call_Box ); i++ )
-                            {
-                                char tmp[ 16 ];
-                                sprintf( tmp, "%x ", *ptr++ );  
-                                strcat( aux, tmp );
-                            }
-                            
-                            strcat( aux, "\n\r\0" );
-                            tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) );
-                            tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) );
-                        }
-                    }
-                        else
-                    {
-                        if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", j );
-                    }
-                }
-            }
-            */
-        }//end Sip debug print
+        if ( print_sip_all )
+        {
+            show_cB_sip_all ();
+            print_sip_all = false;
+        }
+        
+        if ( print_hex_sip_var )
+        {
+            show_cB_hex_sip ();
+            print_hex_sip_var = false;
+        }
          
         if( dcallshow_rtp == true ){
+            show_rtp_on_call ();
             dcallshow_rtp = false;
-            send_msg(":: CAll RTP :: %u", v_call->size() );
-            for( register uint8_t i = 0; i < v_call->size(); i++ ) {
-                VZ_call * call = ( VZ_call * )v_call->get_element( i );
-                if( call != NULL )
-                {
-                    send_msg("CBX ( %d,  %d ) - Server ( %d,  %d )", 
-                        call->get_cb_ext(),
-                        call->get_cb_port(), 
-                        call->get_rtp_server_ext(), 
-                        call->get_rtp_server_port()
-                    );
-                }
-            }
         }
         
         if ( print_call_var )
         {
+            show_call ();
             print_call_var = false;
-            VZ_call * call = __find_Call__( v_call, print_this_call );
-            if ( call != NULL )
-            {
-                send_msg ("Values :: %p\r\n", ( void *) call );
-                call -> print_yourself ();
-            }
-                else
-            {            
-                if( print_values )
-                {
-                    debug_msg("Call %d not found", print_this_call );
-                }
-                    else 
-                {
-                    send_msg("Call %d not found", print_this_call );
-                }
-            }
+            
         }
         
         if ( print_hex_call_var )
         {   
+            show_hex_call ();
             print_hex_call_var = false;
-            VZ_call * call = __find_Call__( v_call, print_hex_this_call );
-            
-            uint8_t * ptr = NULL;
-            if( call != NULL )
-            {
-                ptr = ( uint8_t * ) call;
-            }
-    
-            if ( ptr != NULL )
-            {
-                send_msg ("Values :: %p\r\n", ( void *) ptr );
-                for ( register int i = 0; i < sizeof( VZ_call ); i++ )
-                {
-                    if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                    
-                    if ( ( i % 32 ) == 0 )
-                    {
-                        if( i != 0 )
-                        {
-                            if( debug_uart3 ) pc.printf("\n\r> ");
-                        }  
-                            else
-                        {
-                            if( debug_uart3 ) pc.printf(" ");
-                        }
-                    }        
-                        else
-                    {
-                        if( debug_uart3 ) pc.printf(" ");
-                    }
-                }
-                if ( debug_uart3 ) pc.printf("\n\r> ");
-                
-                if ( tcp_session ) {
-                    char aux[ ( 32 * 3 ) + 5 ];
-                    
-                    for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
-                    
-                    ptr = NULL;
-            
-                    VZ_call * call = __find_Call__( v_call, print_hex_this_call );
-                    
-                    uint8_t * ptr = NULL;
-                    if( call != NULL )
-                    {
-                        ptr = ( uint8_t * ) call;
-                    }
-            
-                    if ( ptr != NULL )
-                    {
-                        bool finished = false;
-                        volatile int i = 0;
-                        int count = 0;
-                        Timer dont_overwrite_tcp_buffer;
-                        dont_overwrite_tcp_buffer.reset ();
-                        dont_overwrite_tcp_buffer.start ();
-                        
-                        while ( !finished )
-                        {
-                            if( dont_overwrite_tcp_buffer.read_ms () >= 20 )
-                            {
-                                dont_overwrite_tcp_buffer.reset();
-                                for ( ; i < sizeof( VZ_call ); i++ )
-                                {
-                                    char tmp[ 16 ];
-                                    sprintf( tmp, "%x ", *ptr++ );  
-                                    strcat( aux, tmp );
-                                    count++;
-                                    
-                                    if ( count >= 32  )
-                                    {
-                                        count = 0;
-                                        i++;
-                                        break;
-                                    }
-                                }                                
-                                strcat ( aux, "\n\r\0" );
-                                tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
-                                if ( i >= sizeof ( VZ_call ) ) finished = true;
-                                strcpy ( aux, "\0" );
-                            }
-                        }
-                        dont_overwrite_tcp_buffer.reset();
-                        dont_overwrite_tcp_buffer.stop();
-                        tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
-                    }
-                }
-            }
-                else
-            {   
-                if( print_values )
-                {
-                    debug_msg("Call %d not found", print_this_call );
-                }
-                    else 
-                {
-                    send_msg("Call %d not found", print_this_call );
-                }
-            }
         }
         
         
         if ( print_hex_rtp_var )
         {   
+            show_cb_rtp ();
             print_hex_rtp_var = false;
-            VZ_call * call = __find_Call__( v_call, print_hex_this_rtp );
-            
-            uint8_t * ptr = NULL;
-            if( call != NULL )
-            {
-                ptr = ( uint8_t * ) call -> check_rtp ();
-            }
-    
-            if ( ptr != NULL )
-            {
-                send_msg ("Values :: %p\r\n", ( void *) ptr );
-                for ( register int i = 0; i < sizeof( RTP ); i++ )
-                {
-                    if( debug_uart3 ) pc.printf("%x", *ptr++ );
-                    
-                    if ( ( i % 32 ) == 0 )
-                    {
-                        if( i != 0 )
-                        {
-                            if( debug_uart3 ) pc.printf("\n\r> ");
-                        }  
-                            else
-                        {
-                            if( debug_uart3 ) pc.printf(" ");
-                        }
-                    }        
-                        else
-                    {
-                        if( debug_uart3 ) pc.printf(" ");
-                    }
-                }
-                if ( debug_uart3 ) pc.printf("\n\r> ");
-                
-                if ( tcp_session ) {
-                    char aux[ ( 32 * 3 ) + 5 ];
-                    
-                    for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0;
-                    
-                    ptr = NULL;
-            
-                    VZ_call * call = __find_Call__( v_call, print_hex_this_rtp );
-                    
-                    uint8_t * ptr = NULL;
-                    if( call != NULL )
-                    {
-                        ptr = ( uint8_t * ) call -> check_rtp ();
-                    }
-            
-                    if ( ptr != NULL )
-                    {
-                        bool finished = false;
-                        volatile int i = 0;
-                        int count = 0;
-                        Timer dont_overwrite_tcp_buffer;
-                        dont_overwrite_tcp_buffer.reset ();
-                        dont_overwrite_tcp_buffer.start ();
-                        
-                        while ( !finished )
-                        {
-                            if( dont_overwrite_tcp_buffer.read_ms () >= 20 )
-                            {
-                                dont_overwrite_tcp_buffer.reset();
-                                for ( ; i < sizeof( RTP ); i++ )
-                                {
-                                    char tmp[ 16 ];
-                                    sprintf( tmp, "%x ", *ptr++ );  
-                                    strcat( aux, tmp );
-                                    count++;
-                                    
-                                    if ( count >= 32  )
-                                    {
-                                        count = 0;
-                                        i++;
-                                        break;
-                                    }
-                                }                                
-                                strcat ( aux, "\n\r\0" );
-                                tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) );
-                                if ( i >= sizeof ( RTP ) ) finished = true;
-                                strcpy ( aux, "\0" );
-                            }
-                        }
-                        dont_overwrite_tcp_buffer.reset();
-                        dont_overwrite_tcp_buffer.stop();
-                        tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) );
-                    }
-                }
-            }
-                else
-            {   
-                if( print_values )
-                {
-                    debug_msg("Call %d not found", print_this_rtp );
-                }
-                    else 
-                {
-                    send_msg("Call %d not found", print_this_rtp );
-                }
-            }
         }
         
         if ( print_rtp_var )
         {
+            show_cb_hex_rtp ();
             print_rtp_var = false;
-            VZ_call * call = __find_Call__( v_call, print_this_rtp );
-            if ( call != NULL )
-            {
-                if ( call -> check_rtp () != NULL )
-                {
-                    send_msg ("Values :: %p\r\n", ( void *) call -> check_rtp () );
-                    call -> rtp_print_yourself ();
-                }
-                    else
-                {
-                    if( print_values )
-                    {
-                        debug_msg("Call->RTP %d not found", print_this_rtp );
-                    }
-                        else 
-                    {
-                        send_msg("Call->RTP %d not found", print_this_rtp );
-                    }       
-                }
-            }
-                else
-            {            
-                if( print_values )
-                {
-                    debug_msg("Call->RTP %d not found", print_this_rtp );
-                }
-                    else 
-                {
-                    send_msg("Call->RTP %d not found", print_this_rtp );
-                }
-            }
-        }
-        
-        //end debug_print
-
-        if( reset_cks == true ) {
-            pkg_cksok = 0;
-            pkg_ckserr = 0;
-            pkg_zero = 0;
-            reset_cks = false;
-            out_of_range = 0;
-            missed_pkg = 0;
-            delayed_pkg_to_cb = 0;
-            cpld_pkg_tx_counter = 0;
-            cpld_pkg_rx_counter = 0;
-            pcks_s = true;
         }
 
-        // exibe os contadores de cks de pacotes recebidos na interface CBx->Header, e outros contadores relacionados
+        if( reset_cks == true )
+        {
+            reset_stats_cks ();
+            pcks_s = true;
+            reset_cks = false;
+        }
+
         if( pcks_s == true ) {
-            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 
-            );
+            show_cb_stats ();
             pcks_s = false;
         }
         
-        // zera os contadores de pacotes que resultaram em falha de envio vio ETH
-        if( reset_missed_send_udp_pkg ){        
-            miss_fw_send_pkg = 0;                                                                                                                 
-            miss_prompt_udp_send_pkg = 0;                                                                                                         
-            miss_sip_registry_send_pkg = 0;                                                                                                       
-            miss_sip_invite_send_pkg = 0;                                                                                                         
-            miss_sip_bye_send_pkg = 0;                                                                                                            
-            miss_sip_unregistry_send_pkg = 0;                                                                                                     
-            miss_sip_ok_send_pkg = 0;                                                                                                             
-            miss_sip_rcv_bye_send_pkg = 0;
-            miss_wdt_send_pkg = 0;
-            miss_prompt_udp_send_pkg = 0;
-            miss_ftp_udp_send_pkg = 0;
-            miss_prompt_udp_rcv_pkg = 0;
+        
+        if( reset_missed_send_udp_pkg )
+        {
+            reset_missed_send_udp ();
+            missed_send_udp_pkg = true;
             reset_missed_send_udp_pkg = false;
-            missed_send_udp_pkg = true;
         }
         
-        // exibe estatisticas de pacotes que resultaram em falha de envio na interface ETH
+        
         if( missed_send_udp_pkg )
         {
-            send_msg(   "\r\nMissed pkgs ::\r\n "
-                        "FW: %d\r\n "
-                        "Prompt UDP %d\r\n "
-                        "Registry %d\r\n "
-                        "Invite %d\r\n "
-                        "Bye %d\r\n "
-                        "Unregistry %d\r\n "
-                        "UDP incoming ( invite ans ) %d\r\n "
-                        "UDP incoming ( bye from * ) %d\r\n "
-                        "Wdt [ alive | rst ] %d\r\n "
-                        "Rcv prompt %d\r\n "
-                        "[ debug | send ]_msg %d\r\n "
-                        "RTP %d\r\n", 
-                miss_fw_send_pkg, 
-                miss_prompt_udp_send_pkg, 
-                miss_sip_registry_send_pkg, 
-                miss_sip_invite_send_pkg, 
-                miss_sip_bye_send_pkg, 
-                miss_sip_unregistry_send_pkg, 
-                miss_sip_ok_send_pkg, 
-                miss_sip_rcv_bye_send_pkg, 
-                miss_wdt_send_pkg, 
-                miss_prompt_udp_send_pkg,
-                miss_prompt_udp_rcv_pkg,
-                miss_ftp_udp_send_pkg 
-            );
-            
+            show_missed_send_udp_pkg ();
             missed_send_udp_pkg = false;
         }
 
@@ -1581,6 +469,7 @@
                 }
             }
             break;
+            
             case REGISTRY : {
                 registry_counter++;
                 Call_Box * cb = __find_CB__( v_cb, ext );
@@ -1607,6 +496,7 @@
                 if( cb != NULL ) cb->registry();
             }
             break;
+            
             case BOOT : {
                 boot_counter++;
                 if( debug_boot == true ){
@@ -1705,6 +595,7 @@
                 }
             }
             break;
+            
             case AUDIO : {
                 audio_counter++;
                 if ( received_audio_from_cb ) {
@@ -1736,199 +627,29 @@
             break;
         }// fim switch
         
-        { // rajada
-            if( invite_retry_timer.read_ms() >= 20 )
-            {
-                invite_retry_timer.reset();
-
-                static int retry_invite_pkg = 0;
-                bool need_retry = false;
-                Call_Box * cb = NULL;
+        // rajada
+        if( invite_retry_timer.read_ms() >= 20 )
+        {
+            invite_ack_to_cb_handler ( v_cb, v_call );
             
-                for ( register int i = 0; i < v_cb->size(); i++ )
-                {
-                    retry_invite_pkg++;
-                    if ( retry_invite_pkg >= v_cb->size() ) retry_invite_pkg = 0;
-                    
-                    cb = (Call_Box * )v_cb->get_element( i );
-                    if ( cb != NULL )
-                    {
-                        if ( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) )
-                        {
-                            need_retry = true;
-                            break;
-                        }
-                    }
-                }
-                
-                if ( need_retry )
-                {
-                    if( cb->get_invite_response() == false )
-                    {
-                        cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+            invite_retry_timer.reset();
+        }    
+    
+        check_audio_from_ast ( v_cb, v_call );        
 
-                        buffer[ TIMESLICE_PLACE ] = cb->get_timeslice();
-                        
-                        send2callboxes( build_cb_package( cb->get_ext(), cb->get_port(), INVITE, ( char * )buffer,
-                           cb->get_msg_id(), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+        wake_up_or_refresh_handler ( v_cb );
 
-                        if( debug_invite ) debug_msg("[%d] resend invite OK to Cbx : ( %d,  %d )", cb->get_ext(), cb->get_ext(), cb->get_port() );
-                    } 
-                    
-                    if ( __find_Call__( v_call, cb->get_ext () ) == NULL )
-                    {
-                    
-                        if ( ( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) ) )
-                        {
-                                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_MSG_SIZE; 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_MSG_SIZE, rtp_dst_tmp_write_buffer ) );
-                        } else {
-                            if ( cb->get_invite_retry_count() == 0 )
-                            {
-                                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+        check_sip_messages_from_ast ( v_cb, v_call );
         
-                                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;
-                                    }
-                                }
-                                
-                                ts->return_timeslice( cb->call_end () );
-                                
-                                if( debug_invite ) debug_msg( "[%d]-- No audio pkgs --", cb->get_ext () );
-                            }
-                        }
-                    }
-                }
-            }          
-        } // fim rajada
-
-        for( register uint8_t i = 0; i < v_call->size(); i++ ) {
-            VZ_call * call = ( VZ_call * )v_call->get_element( i );
-            if ( call != NULL )
-            {
-                int length = 0;
-                char * tmp = call->get_eth_message( &length );
-                if( tmp != NULL ) {
-                    int cb_port = 0xffff;
-                    Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() );
-    
-                    if( cb != NULL ) {
-                        cb_port = cb->get_port();
-                        
-                        if ( drop_rtp_from_ast_pkg )
-                        {
-                            led1 = !led1;
-                        }
-                            else
-                        {
-                            uint8_t * pkg2cb = build_cb_package( call->get_cb_ext(), cb_port, AUDIO,
-                                tmp, AUDIO, length, write_buffer );
-                                
-                            send2callboxes( pkg2cb );
-                        }
-                        
-                    } else if( debug_main ) debug_msg("[%d] received missed package -- Type :: %i", ext, type );
-                }
-            }
-        }
-
-        if( timer_sync_refresh.read_ms() > 250 ) {
-            timer_sync_refresh.reset();
-
-            static uint8_t time_to_mode = TIME_TO_REFRESH;
-
-            if ( time_to_mode == TIME_TO_REFRESH ){
-                time_to_mode = TIME_TO_WAKE_UP;
-                refresh ( v_cb );
-            } else {
-                time_to_mode = TIME_TO_REFRESH;
-                if ( wake_all ) if ( v_call->size() == 0 ) wake_all_up ( v_cb );
-            }
-        }
-
-        // check sip messages only for cbx in call ?
-        int ext_to__be_removed = sip_manager( v_cb );
-        if( ext_to__be_removed > 0 ) {
-            Call_Box * cb = __find_CB__( v_cb, ext_to__be_removed );
-            if( cb != NULL ) {
-                //if( cb->status == cb_on_call ) // poderia ser if( cb->status != cb_idle ) ???
-                if( cb->status == cb_on_call )
-                {
-                    buffer[ TIMESLICE_PLACE ] = 0;
-
-                    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() == ext_to__be_removed ) {
-                            v_call->remove_element( i );
-                            delete( call );
-                        }
-                    }
-                    
-                    ts->return_timeslice( cb->call_end ( false ) );
-
-                    send2callboxes( build_cb_package( cb->get_ext (), cb->get_port (), CB_BYE,
-                        ( char * )buffer, cb->get_msg_id(), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
-                    
-                    if( debug_invite ) debug_msg("[%d] Received Bye from *", cb->get_ext () );
-                }
-            } else if( debug_main ) debug_msg("[%d] Missed bye request", ext_to__be_removed );
-        }
-
         /* Verifica andamento de ligações para eventualmente encerra-las por timeout */
         call_manager( v_call, v_cb, ts );
 
-        /* tratamento de pedidos de ligação */
         invite_handler( v_call, v_cb, ts, NULL );
 
-        /* rotina de verificação de TS's perdidos */
-        if( ( v_call->size() == 0 ) && ( ts->remain_timeslices() != __MAX_TIMESLICES__ ) ) {
-            bool ts_reset = true;
-            for( register uint8_t i = 0; i < v_cb->size(); i++ ) {
-                uint16_t cb_status = ((Call_Box *)v_cb->get_element( i ))->get_status();
-                uint16_t cb_sip_status = ((Call_Box *)v_cb->get_element( i ))->get_sip_status();
-                
-                if  ((cb_status != cb_idle) and (cb_sip_status != sip_idle)) {
-                    ts_reset = false;
-                    break;
-                }
-                
-            }
-            if( ts_reset && !bl_start_flag )
-            {
-                if( debug_invite ) debug_msg("Resetando TS");
-                ts->reset();
-            }
-        }
+        check_for_runaways_ts ( v_cb, ts );
         
-        /* rotina "zeradora" de portas RTP */
-        for ( register uint8_t i = 0; i < v_cb->size(); i++ )
-        {
-            Call_Box * cb = (Call_Box *) v_cb->get_element (i);
-            if ( cb != NULL )
-            {
-                if (  ( cb->get_status () == cb_idle ) && ( cb->get_sip_status () == sip_idle ) && ( cb -> is_rtp_timer_timeout () ) )
-                {
-                    if ( debug_reset_rtp ) debug_msg( "[%d] rtp reset", cb -> get_ext () );
-                    set_status ( cb -> status, cb_idle );
-                    set_status ( cb->sip->status, sip_idle );
-                    cb -> reset_rtp_timer ();
-                    cb -> set_rtp_port ( 0 );           
-                }
-            }
-        }
-
-        /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */
+        check_for_unwanted_rtp_ports ( v_cb );
+        
         tx_buffer_ring_buffer_handler();
 
         type = DO_NOTHING;
@@ -1936,50 +657,15 @@
         if( eth_status == 0 ) eth_wdt = ETH_CONNECT_TIMEOUT;
 
         if( wdt_timer.read() >= 1 ) {
-            //FIXME remove myself
-            //registry_test = true;
-            //invite_retry_time = true;
-
-            uptime++;
-            
-            current_time++;
-
+            wdt_update ();
             wdt_timer.reset();
-
-            if( wdt_show ) debug_wdt = true;
-
-            if( external_wdt ) external_wdt--;
-            if( pkg_wdt ) pkg_wdt--;
-            if( eth_wdt ) eth_wdt--;
-
-            if( eth_wdt && external_wdt && pkg_wdt ){
-                wdt.kick();
-            }
         }
 
-        if( debug_wdt ) {
-            send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter );
-            lpc_low_level_input_counter = 0;
-
+        if( debug_wdt )
+        {
+            show_wdt_status ();
             debug_wdt = false;
-            if( eth_status == 0 ) {
-                send_msg( "Wdt last reset: %s - status_eth :: Connected - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )",
-                          ( wdt.WatchdogCausedReset() ) ? "true" : "false",
-                          EXTERN_WDT_IDLE - external_wdt,
-                          EXTERN_WDT_IDLE,
-                          RX_CB_IDLE - pkg_wdt,
-                          RX_CB_IDLE
-                        );
-            } else {
-                send_msg( "Wdt last reset: %s - status_eth :: Disconnected :: since %3d sec - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )",
-                          ( wdt.WatchdogCausedReset() ) ? "true" : "false",
-                          ETH_CONNECT_TIMEOUT - eth_wdt,
-                          EXTERN_WDT_IDLE - external_wdt,
-                          EXTERN_WDT_IDLE,
-                          RX_CB_IDLE - pkg_wdt,
-                          RX_CB_IDLE
-                        );
-            }
+            
         }
-    }
-}
\ No newline at end of file
+    }// fim while
+}// fim main \o/
\ No newline at end of file