Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
126:1f90756250fb
Parent:
124:c1b6c893e1c3
Child:
127:e7160a463b6c
diff -r 8ff4dc96ad58 -r 1f90756250fb main.cpp
--- a/main.cpp	Wed May 13 21:18:12 2015 +0000
+++ b/main.cpp	Wed May 20 19:50:31 2015 +0000
@@ -17,6 +17,20 @@
     
     while( true )
     {    
+        prompt_process( NULL, 0 );
+        
+        if ( show_last_rx )
+        {
+            show_last_rx_pkg_from_cbx ();
+            show_last_rx = false;    
+        }
+        
+        if ( show_last_tx )
+        {
+            show_last_tx_pkg_from_cbx ();
+            show_last_tx = false;    
+        }
+        
         if ( show_hello_status )
         {
             show_hello_status_function ();
@@ -40,7 +54,7 @@
             }
             
             //35 sec.
-            if( ( count > 7 ) && ( wake_all == false ) ) {
+            if( ( count > 7 ) and ( wake_all == false ) ) {
                 wake_all = true;
                 if( debug_wake == true ) send_msg( "Time to wake" );
             }
@@ -55,8 +69,6 @@
             send_msg("wake_all_up status :: %s", ( wake_all ) ? "Enable" : "Disable" );
             show_wake_all_up_status = false;    
         }
-
-        prompt_process( NULL, 0 );
         
         check_clock ();
 
@@ -116,11 +128,35 @@
             send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" );
             debug_eth = false;
         }
+        
+        
 
         // chechando se existe um pacote vindo do cbx pendente
         if( status != WAITING ) 
         {
-            process_received_pkg_from_cbx ();    
+            process_received_pkg_from_cbx ();
+        } 
+            else if ( test_ts and test_ts_timer.read_ms () > 10 )
+        {
+            if ( v_cb -> size () not_eq 0 )
+            {
+                Call_Box * cb = ( Call_Box * ) v_cb -> get_element ( 0 );
+                if ( cb not_eq NULL )
+                {
+                    ext = cb -> get_ext ();
+                    port = cb -> get_port ();     
+                } 
+            }
+                else
+            {
+                ext = 5000;
+                port = 5000;
+            }
+                
+            type = INVITE;
+            data = buffer;
+            
+            test_ts_timer.reset ();
         }
 
         if( sizes == true )
@@ -281,29 +317,30 @@
         }
             
         // usado pra test
-        if( flood_bug_pkg ){
+        if ( flood_bug_pkg ){
             static int id = 0x10;
             if( id < 10 ) id = 0x0b;
             send2callboxes( build_cb_package( 5828, 5123, REGISTRY,
                 ( char * )buffer, id++, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );    
         }
         
-        if( led_sync_timer.read() > 1 ) {
+        if ( led_sync_timer.read() > 1 ) {
             led_sync_timer.reset();
             led3 = !led3;
             CAB_LED = !CAB_LED;
         }
-
-        switch( type ) {
-            case DO_NOTHING :
-            {}
+        
+        switch( type ) 
+        {
+            case DO_NOTHING :{}
             break;
 
-            case CB_BYE : {
+            case CB_BYE : 
+            {
                 cb_bye_counter++;
                 Call_Box * cb = find_CB( v_cb, ext );
                 if( cb != NULL ) {
-                    if( debug_invite || debug_main ) debug_msg("[%d] Bye pkg - msg_id %d e pkg_id %d", ext, cb->get_msg_id(), data[ 0 ] );
+                    if( debug_invite or debug_main ) debug_msg("[%d] Bye pkg - msg_id %d e pkg_id %d", ext, cb->get_msg_id(), data[ 0 ] );
                     
                     bool already_removed = true;
                     
@@ -313,12 +350,12 @@
                         data[ TIMESLICE_PLACE ] = 0;
                     
                         send2callboxes( build_cb_package( ext, port, CB_BYE,
-                            ( char * )data, data[ 0 ] |= BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                            ( char * )data, data[ 0 ] or_eq BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                     
                         ts->return_timeslice( cb->call_end () );
                     }
     
-                    if( already_removed ) if( debug_main || debug_invite ) debug_msg( "[%d] Already removed from inviting queue", ext );
+                    if( already_removed ) if( debug_main or debug_invite ) debug_msg( "[%d] Already removed from inviting queue", ext );
                     
                     already_removed = true;
                     
@@ -334,7 +371,7 @@
                             v_call->remove_element( i );
 
                             send2callboxes( build_cb_package( ext, port, CB_BYE,
-                                ( char * )data, data[ 0 ] |= BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                                ( char * )data, data[ 0 ] or_eq BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                             
                             delete( call );
                             
@@ -342,23 +379,25 @@
                         }
                     }
    
-                    if( already_removed ) if( debug_main || debug_invite ) debug_msg( "[%d] Already removed from vector call", ext );
+                    if( already_removed ) if( debug_main or debug_invite ) debug_msg( "[%d] Already removed from vector call", ext );
 
                     cb -> registry();
                     
-                } else if( debug_invite || debug_main ) debug_msg("[%d] Bye from who ?", ext );
+                } else if ( debug_invite or debug_main ) debug_msg("[%d] Bye from who ?", ext );
             }
             break;
 
-            case INVITE : {
-                if( drop_invite_pkg ){
+            case INVITE : 
+            {
+                if ( drop_invite_pkg )
+                {
                     debug_msg("[%d] Dropando invite pck - msg id :: %d", ext, data[ 0 ] );
                     break;
                 }
                 
                 invite_counter++;
                 
-                if( debug_invite ) debug_msg("[%d] Invite request", ext);
+                if ( debug_invite ) debug_msg ("[%d] Invite request", ext );
                 
                 Call_Box * cb = find_CB ( v_cb, ext );
               
@@ -366,13 +405,13 @@
                 {
                     if ( v_cb -> size () < MAX_CB_IN_A_BRANCH )
                     {
-                        if ( debug_main ) debug_msg ( "[%d] Adding CBx", ext );
+                        if ( debug_main or debug_invite ) debug_msg ( "[%d] Adding CBx", ext );
                         cb = new Call_Box ( ext, port );
 
                         if ( cb == NULL )
                         {
                             memory_is_over = true;
-                            if ( debug_memory ) debug_msg ("[%d] Invite allocation cb fail", ext );
+                            if ( debug_memory or debug_invite ) debug_msg ("[%d] Invite allocation cb fail", ext );
                         } else {
                             v_cb->add ( cb );
                         }
@@ -387,18 +426,22 @@
                     
                     if ( cb -> get_status () == cb_idle ) { 
                         cb -> call_config ();
+                        if ( test_ts ) debug_msg ("TST::IDLE");
                     } else {
                         data [ TIMESLICE_PLACE ] = cb -> get_timeslice ();
                         
                         cb -> set_invite_response_pending ();    
-                            
-                        send2callboxes( build_cb_package( ext, port, INVITE, 
+                        
+                        if ( test_ts ) debug_msg ("TST::%u ", data [ TIMESLICE_PLACE ]);
+                        
+                        send2callboxes ( build_cb_package ( ext, port, INVITE, 
                             ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );    
                     }
                     
                     invite_handler ( v_call, v_cb, ts, cb );
                 }
             }
+            
             break;
             
             case REGISTRY : {
@@ -424,7 +467,11 @@
                 
                 if ( debug_main ) debug_msg("[%d %d] Registered", ext, port );
 
-                if ( cb != NULL ) cb->registry();
+                int registry_ret = -1;
+                
+                if ( cb != NULL ) registry_ret = cb -> registry ();
+                
+                if ( ( registry_ret > 0 ) and eth_status ) try_reconnect_with_eth (); 
             }
             break;
             
@@ -434,13 +481,13 @@
                     send_msg("[%d %d] Boot pkg -- pkg-id %d", ext, port, data[ 0 ] );    
                 }
                 send2callboxes( build_cb_package( ext, port, REGISTRY,
-                    ( char * )data, data[ 0 ] | BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                    ( char * )data, data[ 0 ] bitor BIT7, CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
             }
             break;
             
             case FW : {
-                if( debug_fw_print ) send_msg("[%d %d]::FW pkg::", ext, port );
-                fw_cbx_pkg( ext, port, ( char *)buffer );
+                if ( debug_fw_print ) send_msg ("[%d %d]::FW pkg::", ext, port );
+                fw_cbx_pkg ( ext, ( char *) buffer );
             }
             break;
 
@@ -502,13 +549,16 @@
                     }
                 }
 
-                if( cb!= NULL ) cb->registry();
+                if( cb!= NULL ) cb -> registry ();
 
-                if( xstrmatch( ( uint8_t * )data, ( uint8_t * )"ping" ) ) {
+//                if( ( xstrmatch( ( uint8_t * )data, ( uint8_t * )"ping" ) ) or xstrmatch ( ( uint8_t * )data, ( uint8_t * )"pong" ) ) 
+                if ( ( strncasecmp ( ( const char * )data, "ping", 4 ) == 0 ) or ( strncasecmp ( ( const char * )data, "pong", 4 ) == 0 ) )
+                {
                     if( debug_ping ) send_msg( "[%d %d] Prompt pkg :: Ping", ext, port );
-                } else {
+                } 
+                    else 
+                {
                     prompt_counter++;
-                    //fixme isso nao poderia ser resolvido com um sendmsg ?
                     send_msg( "[%i, %i] Prompt pkg::", ext, port );
                     for( register uint8_t i = 0; i < 32; i++ ) {
                         if( debug_uart3 ) pc.printf("%c", data[ i ] );
@@ -533,26 +583,30 @@
                     debug_msg("[%d] audio pkg", ext );    
                 }
                 
-                VZ_call * call = __find_Call__( v_call, ext );
-                if( call != NULL ) {
-                    if ( drop_rtp_from_cbx_pkg ){
+                VZ_call * call = find_Call ( v_call, ext );
+                if ( call != NULL )
+                {
+                    if ( drop_rtp_from_cbx_pkg )
+                    {
                         led2 = !led2;
                         break;
                     } else {
-                        char * pkg = call->build_eth_package( data + 2 );
-                        call->send_message( pkg );
-                        call->cbx_pkg_idle_timer_reset();
+                        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 ){
-                    if ( cb->get_invite_response() == false ) {
-                        cb->set_invite_response_ok();
-                        cb->invite_retry_count_reset();
-                    } else {
-                        if( debug_main ) debug_msg("[%d] received missed package", ext );       
-                    }
+                if ( cb != NULL )
+                {
+                    if ( cb -> get_invite_response() == false )
+                    {
+                        cb -> set_invite_response_ok ();
+                        cb -> invite_retry_count_reset ();
+                    } 
+                } else {
+                    if( debug_main ) debug_msg("[%d] received missed package", ext );
                 }
             }
             break;