Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
108:18a3702650f3
Parent:
107:cf1e43414adb
Child:
109:a5b8264ffbbc
--- a/main.cpp	Mon Mar 16 14:05:05 2015 +0000
+++ b/main.cpp	Wed Mar 18 13:33:54 2015 +0000
@@ -229,7 +229,7 @@
                 // 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;
+                    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 ) );
                     
@@ -332,10 +332,9 @@
             }
         }//end verificacao e tratamento dos pacotes recebidos via UDP-ETH 
         
+        
         // atualiza o valor do contador max_registered_cbx
         if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size();
-        
-         
 
         // executa a cada 5 segundos comandos e verificacoes ( principalmente debugs )
         if( sync_timer.read() > 5 ) {
@@ -380,9 +379,7 @@
                 wake_all = true;
                 if( debug_wake == true ) send_msg( "Time to wake" );
             }
-        }
-        
-         
+        } 
 
         // zera os contadores de pacotes recebidos na interface CBx->Header
         if( r_stats ) {
@@ -400,8 +397,6 @@
             r_stats = false;
             stats = true;
         }
-        
-         
 
         // exibe o valor dos contadores de pacotes recebidos na interfacao CBx->ETH
         if( stats ) {
@@ -412,8 +407,6 @@
             stats =false;
         }
         
-         
-
         // exibe uma lista ( em ordem crescente e por ramais ) dos cbx que ja se registraram ao menos uma vez
         if( list ) 
         {
@@ -450,9 +443,7 @@
                 send_msg( "%s", str );
             }
             list = false;
-        }
-        
-         
+        }         
         
         // validar na proxima iteracao.
         if( long_list ) 
@@ -670,11 +661,57 @@
             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( cb != NULL ) send_msg("ext :: %d -- port :: %d -- timer %d", cb->get_sip_ext(), cb->get_sip_port(), cb->get_timer() );
             }
         } 
         
+        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 ) - Timer ( %d )", 
+                        cb -> get_ext (),
+                        cb -> get_port (),
+                        cb -> get_sip_ext (), 
+                        cb -> get_sip_port (), 
+                        cb -> get_rtp_port (),
+                        cb -> get_rtp_timer ()
+                    );
+                }
+            }
+        }
          
+        if( dcallshow_rtp == true ){
+            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()
+                    );
+                }
+            }
+        }
 
         // aciona rotina de envio de pacote de flood
         if( pflood == true ) flood();
@@ -821,16 +858,12 @@
             } else type = __DO_NOTHING__;
         }
         
-         
-        
         // exibe uptime atual
         if( show_uptime )
         {
             show_uptime = false;
             send_msg("Uptime: %d", uptime );
-        }
-        
-         
+        } 
 
         // exibe algumas informacoes pertinentes sobre quantidade de variaveis alocadas e tamanho de alguns objetos.
         if( sizes == true ) {
@@ -869,8 +902,6 @@
             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( pcks_s == true ) {
@@ -896,8 +927,6 @@
             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;                                                                                                                 
@@ -960,30 +989,26 @@
                 ( char * )buffer, id++, CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) );    
         }
         
-         
         // toggle nos leds de debug da header.
         if( led_sync_timer.read() >= 1 ) {
             led_sync_timer.reset();
             led3 = !led3;
             CAB_LED = !CAB_LED;
         }
-        
          
         // rotina de teste ( usados durante desenvolvimento
-        /*
         if( main_test )
         {
-            static int n = 0;
-            static int next_ext = 5000;
-            
-            main_test = false;
-            data = buffer;
-            ext = next_ext + ( n * ( ( n % 2 ) ? 1 : -1 ) );
-            n++;
-            port = ext;
-            type = __REGISTRY__;
+            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 -> get_ext () == 5000 ) )
+                {
+                    main_test = false;
+                    cb -> init_rtp_timer ();        
+                }
+            }
         }
-        */
          
         // begin switch para tratemento de pacote baseado no type
         switch( type ) {
@@ -993,6 +1018,7 @@
 
             case __CB_BYE__ : {
                 /***
+                    end call
                     -- Fluxo -- 
                     [ Principio ] 
                         -- Receber um pedido de bye
@@ -1064,6 +1090,10 @@
                               
                             // envia o ack bye depois atualiza o msg_id  
                             cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+                    
+                            cb -> set_rtp_port ( 0 );
+                            
+                            cb -> reset_rtp_timer ();
 
                             delete( call );
                         }
@@ -1100,12 +1130,15 @@
                     if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from inviting queue" );
 
                     cb->registry();
+                    cb -> set_rtp_port ( 0 );
+                    cb -> reset_rtp_timer ();
                 } else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext );
             }
             break;
 
             case __INVITE__ : {
                 /***
+                    init call
                     [ Principio ] 
                         -- Receber um pedido de chamada
                         -- tratar com o cbx o andamento da negociacao SIP ( informando disponibilidade de TS )
@@ -1147,6 +1180,8 @@
                     if( cb->status == cb_idle ){
                         cb->set_invite_response_ok();
                         cb->invite_retry_count_reset();
+                        cb -> reset_rtp_timer ();
+                        cb -> init_rtp_timer ();
                     }
                     invite_handler( v_call, v_cb, ts, cb );
                 }
@@ -1370,11 +1405,19 @@
                 if( retry_invite_pkg >= v_cb->size() ) retry_invite_pkg = 0;
 
                 // procura por CBx em ligaçao sequencialmente
-                register int i = 0;
+                int i = 0;
+                
+                led4 = 1;
                 
-                for( ; i < v_cb->size(); i++ ) {
+                /*
+                // por hipótese esse valor esta sendo corrompido quando recebido UDP
+                for ( ; i < v_cb->size(); i++ )
+                {
+                    {
+                        led4 = 0;
+                    }
+                    
                     Call_Box * cb = (Call_Box * )v_cb->get_element( i );
-                    debug_msg("--%p--", cb );
                     if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ) {
                         if( i > retry_invite_pkg ) {
                             retry_invite_pkg = i;
@@ -1382,7 +1425,7 @@
                         }
                     }
                 }
-                
+    
                 if( i != v_cb->size() ){
                     Call_Box * cb = (Call_Box *)v_cb->get_element( retry_invite_pkg++ );
                     
@@ -1423,11 +1466,10 @@
                         }
                     }
                 }
+                */
             }
         }
-        
-        //
-        
+            
         /***
             [ Principio ]
                 -- Para cada CBx em ligaçao
@@ -1442,27 +1484,29 @@
                             - Caso nao seja encontrado
                                 - Exibe msg de erro condicionado a debug
         ***/
-        
+       
         for( register uint8_t i = 0; i < v_call->size(); i++ ) {
-            //debug_msg("");
             VZ_call * call = ( VZ_call * )v_call->get_element( i );
-            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();
-
-                    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("received missed package  from CBx :: %i -- Type :: %i", ext, type );
+            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();
+    
+                        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("received missed package  from CBx :: %i -- Type :: %i", ext, type );
+                }
             }
         }
-        //
+    
         /***
             [ Principio ]
                 -- Verificar se já se passou determinada unidade de tempo
@@ -1474,22 +1518,21 @@
         if( timer_sync_refresh.read_ms() > 250 )
         {
             timer_sync_refresh.reset();
-
-            static uint8_t mode = TIME_TO_REFRESH;
-            
-            if( mode == TIME_TO_REFRESH )
+            static uint8_t time_to_mode = TIME_TO_REFRESH;
+            if( time_to_mode == TIME_TO_REFRESH )
             {
-                mode = TIME_TO_WAKE_UP;
+                time_to_mode = TIME_TO_WAKE_UP;
                 refresh( v_cb );
             }
                 else
             {
-                mode = TIME_TO_REFRESH;
+                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 ?
+        // end call
         int ext_to__be_removed = sip_manager( v_cb );
         if( ext_to__be_removed > 0x00 ) {
             Call_Box * cb = __find_CB__( v_cb, ext_to__be_removed );
@@ -1511,6 +1554,10 @@
                     }
 
                     cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+                    
+                    cb -> set_rtp_port ( 0 );
+                    
+                    cb -> reset_rtp_timer ();
 
                     send2callboxes( build_cb_package( ext, port, __CB_BYE__,
                         ( char * )buffer, cb->get_msg_id(), CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) );
@@ -1518,16 +1565,14 @@
                     if( debug_invite ) debug_msg("Received Bye from *");
                 }
             } else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d", ext );
-        }
+        }        
 
         /* Verifica andamento de ligações para eventualmente encerra-las por timeout */
-        call_manager( v_call, v_cb, buffer, write_buffer, ts );
+        call_manager( v_call, v_cb, ts );
 
-        
         /* tratamento de pedidos de ligação já em andamento com 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;
@@ -1538,26 +1583,39 @@
                     break;
                 }
             }
-            if( ts_reset ) {
-                if( debug_invite ) debug_msg("Resetando TS");
-                ts->reset();
+            if ( ts_reset )
+            {
+                if ( debug_invite ) debug_msg ("Resetando TS");
+                ts -> reset ();
             }
         }
-
+        
+        /* 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 */
         tx_buffer_ring_buffer_handler();
 
-        
         // atribui o valor default para a variavel type
         type = __DO_NOTHING__;
-
         
         // verifica o status da conexao ETH, caso exteja conectado ( == 0 ) atribui o valor maximo ao contador de controle
         if( eth_status == 0 ) eth_wdt = ETH_CONNECT_TIMEOUT;
         
-        
-        
         // responsavel por atualizar os contadores de controle utilizados para tickar o wdt
         // e tickar o wdt
         if( wdt_timer.read() >= 1 ) {
@@ -1576,8 +1634,7 @@
             if( eth_wdt && external_wdt && pkg_wdt ){
                 wdt.kick();
             }
-        }
-        
+        }     
         
         // exibe informacoes referentes ao wdt, ultimo reset, status da eth, tempo em segundos desde o ultimo tick dowdt externo, 
         // tempo em segundos desde o ultimo pacote recebido da interface fibra
@@ -1604,9 +1661,6 @@
                           RX_CB_IDLE
                         );
             }
-        }
-        
-        // atribui o valor default ao ponteiro de dados
-        data = NULL;
+        }        
     }//fim while ( main loop )
 }
\ No newline at end of file