Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
135:2f4290590e51
Parent:
132:05cd37f7e007
--- a/main_app_functions.cpp	Tue Sep 01 19:34:34 2015 +0000
+++ b/main_app_functions.cpp	Thu Sep 10 20:11:35 2015 +0000
@@ -75,28 +75,10 @@
     type = DO_NOTHING;
     
     v_cb = new Vector();
-    if( v_cb == NULL ) {
-        while( v_cb == NULL ) {
-            Vector * v_cb = new Vector();
-            if( sync_timer.read() > 5 ) {
-                if( debug_memory ) vz_printf ("Call_Box vector allocation fail");
-                sync_timer.reset();
-            }
-        }
-    }
     
     if ( debug_app_init ) vz_printf ( "v_cb :: %s", ( v_cb not_eq NULL ) ? "Ok" : "Failure" );
     
     v_call = new Vector();
-    if( v_call == NULL ) {
-        while( v_call == NULL ) {
-            Vector * v_call = new Vector();
-            if( sync_timer.read() > 5 ) {
-                if( debug_memory ) vz_printf ("Call vector allocation fail");
-                sync_timer.reset();
-            }
-        }
-    }
     
     if ( debug_app_init ) vz_printf ( "v_call :: %s", ( v_call not_eq NULL ) ? "Ok" : "Failure" );
     
@@ -236,17 +218,10 @@
 int 
 show_cb_list ( Vector * v_cb )
 {
-    uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb -> size();
-
-    if ( ( max_ext % 2 ) == 0 ) missed_cb ++;
-
-    if ( min_ext % 2 ) missed_cb ++;
-
-    if ( min_ext == 0 && max_ext == 0 ) missed_cb = 0;
-
-    vz_printf ("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- "
+    vz_printf (
+        "Registered %d[ %d ] CBx ( %d - %d ) -- "
         "Remain_timeslices :: %d :: v_call->size() :: %d", 
-        v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, 
+        v_cb->size(), max_registered_cbx, min_ext, max_ext, 
         ts->remain_timeslices(), v_call->size() 
     );
         
@@ -254,7 +229,7 @@
         vz_printf (" %d ", ( ( Call_Box * )v_cb -> get_element( 0 ) ) -> get_ext () );
     } else if( v_cb -> size() > 1 ) {
         char str [ 1024 ];
-        int ext_list [ MAX_CB_IN_A_BRANCH ];
+        int ext_list [ u8_MAX_CB_IN_A_BRANCH ];
         register int i = 0;
         for( ; i < v_cb -> size(); i++ ) {
             ext_list [ i ] = ( ( Call_Box * )v_cb->get_element ( i ) ) -> get_ext ();
@@ -281,16 +256,8 @@
 int 
 show_cb_long_list ( Vector * v_cb, bool show_time, bool show_invite )
 {
-    uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb -> size ();
-
-    if ( ( max_ext % 2 ) == 0 ) missed_cb ++;
-
-    if ( min_ext % 2 ) missed_cb ++;
-
-    if ( min_ext == 0 && max_ext == 0 ) missed_cb = 0;
-
     {
-        int ext_list [ MAX_CB_IN_A_BRANCH ];
+        int ext_list [ u8_MAX_CB_IN_A_BRANCH ];
         
         if ( v_cb -> size () >= 1 )
         {
@@ -301,9 +268,9 @@
             qsort ( ext_list, v_cb -> size (), sizeof ( int ), ls_comp );
         }   
         
-        vz_printf ("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- "
+        vz_printf ("Registered %d[ %d ] CBx ( %d - %d ) -- "
             "Remain_timeslices :: %d :: v_call->size() :: %d", 
-            v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, 
+            v_cb->size(), max_registered_cbx, min_ext, max_ext,
             ts->remain_timeslices(), v_call->size() 
         );
         
@@ -413,7 +380,7 @@
                         char strcat_tmp [ 16 ];
                         if ( get_overflow_times_tmp == 1 ) snprintf ( strcat_tmp, sizeof ( strcat_tmp ) - 1, "1 time ]" );
                         
-                        else snprintf ( strcat_tmp, sizeof ( strcat_tmp ) - 1, "%u times ]", get_overflow_times_tmp );
+                        else snprintf ( strcat_tmp, sizeof ( strcat_tmp ) - 1, "%d times ]", get_overflow_times_tmp );
                         
                         strcat ( get_timer_msg, strcat_tmp );
                     }
@@ -651,7 +618,7 @@
             int fd =  cb -> get_sip_socket_fd ();
             if ( FD_ISSET ( fd, &fdSet ) ) 
             {
-                int rcv = cb -> sip_udp_incomming_pkg ();
+                cb -> sip_udp_incomming_pkg ();
             }
         }
         
@@ -855,7 +822,7 @@
                     
                     if ( ( 
                             (!question_alive) 
-                            && ( cb_new_counter <= MAX_CB_IN_A_BRANCH ) 
+                            && ( cb_new_counter <= u8_MAX_CB_IN_A_BRANCH ) 
                             && ( ( cb_new_counter >=  MIN_CBX_IN_A_BRANCH ) || ( disable_wdt_from_cbx ) ) 
                     ) ) 
                     {
@@ -929,7 +896,7 @@
 {
     vz_printf (":: RTP :: %u", v_cb->size() );
     
-    int ext_list[ MAX_CB_IN_A_BRANCH ];
+    int ext_list[ u8_MAX_CB_IN_A_BRANCH ];
         
     if( v_cb->size() >= 1 ) {
         for( register int i = 0; i < v_cb->size(); i++ )
@@ -1713,12 +1680,11 @@
             char * tmp = call -> get_eth_message ( &length );
             if( tmp not_eq NULL )
             {
-                int cb_port = 0xffff;
                 Call_Box * cb = find_CB ( v_cb, call -> get_cb_ext () );
 
                 if( cb not_eq NULL ) 
                 {
-                    cb_port = cb -> get_port ();
+                    int cb_port = cb -> get_port ();
                     
                     if ( drop_rtp_from_ast_pkg )
                     {
@@ -1740,14 +1706,11 @@
     return ( 0 );    
 }
 
-int wake_up_or_refresh_handler ( Vector * v_cb )
+int 
+wake_up_or_refresh_handler ( Vector * v_cb )
 {
     int return_value = 0;
     
-    // analizar este caso com mais cuidado
-    // dado que vão ter 2 carinhas "perdidos"
-    // perceber a frequencia de envio de pacotes
-    // perceber possiveis gargalos nesses envios
     if( timer_sync_refresh.read_ms () > 250 * 2 )
     {
         timer_sync_refresh.reset ();
@@ -1760,7 +1723,7 @@
             if ( !do_not_refresh ) return_value = refresh ( v_cb );
         } else {
             time_to_mode = TIME_TO_REFRESH;
-            if ( wake_all and ( v_call -> size() == 0 ) ) wake_all_up ( v_cb );
+            if ( wake_all and ( v_call -> size() == 0 ) and cm -> get_cbx_wake_mode () ) wake_all_up ( v_cb );
         }
     }
     
@@ -1770,31 +1733,34 @@
 int check_sip_messages_from_ast ( Vector * v_cb, Vector * v_call )
 {
     // 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 not_eq NULL ) {
-            //if( cb->status == cb_on_call ) // poderia ser if( cb->status not_eq cb_idle ) ???
+    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 not_eq NULL )
+        {
             if ( cb -> get_status () == cb_on_call )
             {
-                buffer[ TIMESLICE_PLACE ] = 0;
+                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 );
+                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 ) );
+                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 ) );
+                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 ) vz_debug ("[%d] Received Bye from *", cb->get_ext () );
+                if( debug_invite ) vz_debug ("[%d] Received Bye from *", cb -> get_ext () );
             }
-        } else if( debug_main ) vz_debug ("[%d] Missed bye request", ext_to__be_removed );
+        } else if( debug_main ) vz_debug ("[%d] Missed bye request", ext_to_be_removed );
     }
     
     return ( 0 );    
@@ -1824,13 +1790,6 @@
     return ( 0 );    
 }
 
-
-void show_lpc_low_level_input_counter ( void )
-{
-    vz_printf ("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter );
-    lpc_low_level_input_counter = 0;
-}
-
 int show_wdt_status ( void )
 {
     if( eth_status == 0 ) {
@@ -2020,6 +1979,51 @@
     return 0;
 }
 
+Call_Box * 
+try_add_new_cbx ( Vector * v_cb, const int ext )
+{
+    if ( v_cb not_eq NULL )
+    {
+        if ( v_cb -> size () < u8_MAX_CB_IN_A_BRANCH )
+        {
+            if ( debug_main ) vz_debug ( "[%d] Adding Cbx", ext );
+            
+            Call_Box * cb = new Call_Box ( ext, ext );
+    
+            if ( cb == NULL ) { memory_is_over = true; } 
+            
+            else
+            {
+                int pair_ext = ( ( ext % 2 ) == 0 ) ? ext + 1 : ext - 1;
+                
+                Call_Box * pair_cb = find_CB ( v_cb, pair_ext );
+                
+                if ( pair_cb not_eq NULL )
+                {
+                    int pair_ret = cb -> set_pair_cbx ( pair_cb );
+                    
+                    if ( pair_ret == 0 ) {
+                        if ( debug_pair ) vz_debug ( "[%d] vinculado com [%d]", ext, pair_ext );
+                        
+                        pair_cb -> set_pair_cbx ( cb );
+                    } else if ( pair_ret == -3 ) { 
+                        if ( debug_pair ) vz_debug ( "[%d] par sobrescrito com [%d]", ext, pair_ext );
+                    }
+
+                } else if ( debug_pair ) vz_debug ( "[%d] Par nao encontrado", ext )
+                                            
+                v_cb -> add ( cb );
+                
+                if ( debug_main ) vz_debug ( "[%d] Added CBx", ext );
+                
+                return ( cb );
+            }
+        }
+    }
+    
+    return ( NULL );
+}
+
 void update_all_cb_timer ( Vector * v_cb )
 {
     if ( v_cb == NULL ) return;
@@ -2215,16 +2219,16 @@
 {
     if ( wake_msg == NULL ) return ( NULL );
     
-    snprintf( wake_msg, length,"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,%u,%u:", 
+    snprintf ( wake_msg, length,"wdt:%u,%u,%u,%c,%u,%d,%u,%u,%d,%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(),
+        v_cb -> size (),
         ts->remain_timeslices(), 
         sip_socket_send_failure, 
-        v_call->size(),
+        v_call -> size (),
         pkg_cksok,
         pkg_ckserr,
         pkg_zero,