Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
112:6ae726539ab9
Parent:
111:c0833f2455ed
Child:
113:db67ae00550e
--- a/main.cpp	Tue Apr 07 18:26:21 2015 +0000
+++ b/main.cpp	Mon Apr 13 12:16:21 2015 +0000
@@ -16,7 +16,6 @@
 #include "sdram.h"
 #include "fw.h"
 #include "bootloader.h"
-
 volatile u16_t lpc_low_level_input_counter = 0;
 
 int main()
@@ -648,8 +647,8 @@
             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 );
+            //VZ_call * call = new VZ_call( 1234, 9876, 413, 12093 );
+            //v_call -> add ( call );
         }
 
         //static int wdt_count = 0;
@@ -678,6 +677,15 @@
         }
         */
         
+        if ( registra )
+        {
+            int internal_ext = 8000;
+            registra = false;
+            for( register uint8_t i = 0; i < 4; i++ ) {
+                v_cb -> add ( new Call_Box ( internal_ext, internal_ext++ ) );
+            }    
+        }
+        
         {
             fd_set fdSet;
             FD_ZERO(&fdSet);
@@ -751,6 +759,45 @@
             }
         }
         
+        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() );
@@ -1807,8 +1854,8 @@
                 Call_Box * cb = __find_CB__( v_cb, ext );
 
                 if( cb == NULL ) {
-//                    send2callboxes( __build_cb_package__( ext, port, __REGISTRY__,
-//                        ( char * )data, ( ( data[ 0 ] &  ~BIT7 ) + 1 ), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
+                    send2callboxes( __build_cb_package__( ext, port, __REGISTRY__,
+                        ( char * )data, ( ( data[ 0 ] &  ~BIT7 ) + 1 ), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
 
                     if( debug_main ) debug_msg("Adding Cbx :: %d", ext );
                     cb = new Call_Box( ext, port );
@@ -1848,22 +1895,22 @@
                 audio_counter++;
                 VZ_call * call = __find_Call__( v_call, ext );
                 if( call != NULL ) {
-                    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 ( drop_rtp_from_cbx_pkg )
                     {
                         led2 = !led2;
                         break;
                     }
-                    
-                    if( cb != NULL ){
-                        //cb->reset_elapsed_time();                        
-                        cb->set_invite_response_ok();
-                        cb->invite_retry_count_reset();
-                    } 
+                        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 );
                 }
@@ -1872,64 +1919,70 @@
         }// fim switch
         
         { // rajada
-            //if( invite_retry_time == true ){
-            //    invite_retry_time = false;
-            if( invite_retry_timer.read_ms() > 30 ) {
+            if( invite_retry_timer.read_ms() > 30 )
+            {
                 invite_retry_timer.reset();
-                static int retry_invite_pkg = 0;
 
-                if( retry_invite_pkg >= v_cb->size() ) retry_invite_pkg = 0;
-
-                register int i = 0;
-                for( ; i < v_cb->size(); i++ ) {
-                    Call_Box * cb = (Call_Box * )v_cb->get_element( i );
-                    if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ) {
-                        if( i > retry_invite_pkg ) {
-                            retry_invite_pkg = i;
+                static int retry_invite_pkg = 0;
+                bool need_retry = false;
+                Call_Box * cb = NULL;
+            
+                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( i != v_cb->size() ){
-                    Call_Box * cb = (Call_Box *)v_cb->get_element( retry_invite_pkg++ );
-    
-                    if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ) {
-                        if( cb->get_invite_response() == false ) {
-                            //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                            cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+                if ( need_retry )
+                {
+                    if( cb->get_invite_response() == false )
+                    {
+                        cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
 
-                            buffer[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
+                        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 ) );
 
-                            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 ) );
-                            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 ) {
-                            cb->send_bye();
+                        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 )
+                    {
+                        cb->send_bye();
 
-                            ts->return_timeslice( cb->get_timeslice() );
-                            cb->set_timeslice( 0x00 );
+                        ts->return_timeslice( cb->get_timeslice() );
+                        cb->set_timeslice( 0x00 );
 
-                            //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                            cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+                        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;
-                                }
+                        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 );
                     }
                 }
-            }
+            }          
         } // fim rajada
 
         for( register uint8_t i = 0; i < v_call->size(); i++ ) {
@@ -2037,6 +2090,25 @@
                 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_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 );           
+                }
+            }
+        }
+        */
 
         //invite_pgk_retry_manager( v_call, v_cb, buffer, write_buffer );