Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
119:ee6a53069455
Parent:
117:e9facba9db27
Child:
121:ee02790d00b7
--- a/call_manager.cpp	Fri Apr 24 13:54:24 2015 +0000
+++ b/call_manager.cpp	Thu Apr 30 15:42:41 2015 +0000
@@ -18,11 +18,8 @@
             if( cb->get_invite_response() == false ){
                 invite_pkg_retry_timer.reset();
                 
-                //cb->set_msg_id( cb->get_msg_id() & ~BIT7 );
-                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                
-                send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __INVITE__, ( char * )data, 
-                    cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
+                send2callboxes( build_cb_package( cb->get_ext(), cb->get_port(), INVITE, ( char * )data, 
+                    cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                     
                 return;
             }
@@ -37,11 +34,8 @@
             if( cb->get_bye_response() == false ){
                 bye_pkg_retry_timer.reset();
                 
-                //cb->set_msg_id( cb->get_msg_id() & ~BIT7 );
-                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                
-                send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, ( char * )data, 
-                    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 * )data, 
+                    cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
                     
                 return;
             }
@@ -53,41 +47,41 @@
     VZ_call * call = NULL;
     for( register int i = 0; i < v_call->size(); i++ ){
         call = ( VZ_call * )v_call->get_element( i );
-        if( call->get_cb_ext() == ext ) return( call );
+        if ( call != NULL )
+        {
+            if( call->get_cb_ext() == ext ) return( call );
+        }
     }
     return( NULL );
 }
 
 /* remove calls por timeout */
-void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){
-    for( register uint8_t i = 0; i < v_call->size(); i++ ){
+void call_manager( Vector * v_call, Vector * v_cb, Timeslice * ts )
+{
+    //FixME pode dar problema por conta do static
+    static uint8_t data[ CB_BUFFER_SIZE ], write_buffer[ CB_BUFFER_SIZE ];
+    
+    for( register uint8_t i = 0; i < v_call->size(); i++ )
+    {
         VZ_call * call = ( VZ_call * )v_call->get_element( i );
-        if( call->is_timetofinish() || call->cbx_pkg_is_idle() ){
-            
+        if( call->is_timetofinish() || call->cbx_pkg_is_idle() )
+        {
             if( debug_invite ){
                 if( call->cbx_pkg_is_idle() ){ 
-                    if( debug_invite ) debug_msg("Call timeout: no pkg");
+                    if( debug_invite ) debug_msg("[%d] Call timeout: no pkg", call->get_cb_ext () );
                 } else { 
-                    if( debug_invite ) debug_msg("Call timeout"); 
+                    if( debug_invite ) debug_msg("[%d] Call timeout", call->get_cb_ext () ); 
                 }
             }
             v_call->remove_element( i );
             Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() );
             if( cb != NULL ){ 
-                cb->status = cb_idle;
-                ts->return_timeslice( cb->get_timeslice() );
-                cb->set_timeslice( 0x00 );
-                data[ __TIMESLICE_PLACE__ ] = 0x00;
-//                cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, 
-                    ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-                cb->send_bye();
-                set_status( cb->sip->status, sip_idle );
-                //cb->re_start_timer();
-                cb -> reset_cb_status();
-                //cb -> set_rtp_port ( 0 );
-                //cb -> reset_rtp_timer ();
+                data[ TIMESLICE_PLACE ] = 0;
+                
+                send2callboxes( build_cb_package( cb->get_ext(), cb->get_port(), CB_BYE, 
+                    ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                
+                ts->return_timeslice( cb->call_end () );
             }
             delete( call );    
         }
@@ -95,150 +89,139 @@
 }
 
 void invite_handler( Vector * v_call, Vector * v_cb, Timeslice * ts, Call_Box * inviting_cb ){
-    uint8_t data[ __CB_BUFFER_SIZE__ ];
-    uint8_t write_buffer[ __CB_BUFFER_SIZE__ ];
-    
-    if( inviting_cb != NULL ){
-        //if( inviting_cb->status != cb_on_call ){
-            
-        //}
-        if( inviting_cb->status == cb_idle ){            
-            inviting_cb->cb_set_status( cb_trying );
-            if( debug_invite ) debug_msg("Request invite cbx status :: cb_trying" );
-        }
-    }
+    if ( ( v_call != NULL ) && ( v_cb != NULL ) && ( ts != NULL ) )
+    {
+        uint8_t data[ CB_BUFFER_SIZE ];
+        uint8_t write_buffer[ CB_BUFFER_SIZE ];
     
-    for( register uint8_t i = 0; i < v_cb->size(); i++ ){
-        Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
-        if( cb->get_status() == cb_idle || cb->get_status() == cb_on_call ) continue;
-        
-        int ext = cb->get_ext();
-        int port = cb->get_port();
+        if( inviting_cb != NULL ){
+            if( inviting_cb->status == cb_idle ){            
+                inviting_cb->cb_set_status( cb_trying );
+                if( debug_invite ) debug_msg("[%d] Request invite <cb_idle,cb_trying>", inviting_cb->get_ext ()  );
+            }
+        }
         
-        VZ_call * call = NULL;
-
-        switch( cb->status ){            
-            case cb_trying : {
-                uint8_t temp = cb->get_msg_id();
-                cb->set_msg_id( temp |= BIT7 );
-                cb->set_timeslice( ts->get_timeslice() );
-                if( cb->get_timeslice() == 0x00 ){
-                    
-                    cb->cb_set_status( cb_idle );
+        for( register uint8_t i = 0; i < v_cb->size(); i++ )
+        {
+            Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
+            
+            if ( cb == NULL ) continue;
+            
+            if( cb->get_status() == cb_idle || cb->get_status() == cb_on_call ) continue;
+            
+            int ext = cb->get_ext();
+            int port = cb->get_port();
+            
+            VZ_call * call = NULL;
+    
+            switch( cb->status ){            
+                case cb_trying : {                
+                    uint8_t timeslice = ts->get_timeslice ();
                     
-                    data[ __TIMESLICE_PLACE__ ] = 0x00;
-      
-                    send2callboxes( __build_cb_package__( ext, port, __INVITE__, 
-                        ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-      
-                    set_status( cb->sip->status, sip_idle );
-                    
-                    //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                    cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                     
-                    if( debug_invite ) debug_msg( "-- Trying -- without TS --");
-                    
-                    return;
-                }else{
-                    data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
-                    
-                    cb->cb_set_status( cb_ringing );
-                    
-                    cb->set_invite_response_pending();                    
+                    if( timeslice == 0 ){
+                        
+                        data[ TIMESLICE_PLACE ] = 0x00;
+          
+                        send2callboxes( build_cb_package( ext, port, INVITE, 
+                            ( char * )data, ( cb->get_msg_id() | BIT7 ), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                         
+                        cb->call_end ();
+                         
+                        if( debug_invite ) debug_msg( "[%d] Trying without TS <cb_trying,cb_idle>", ext );
+                        
+                        return;
+                    }else{
+                        
+                        data[ TIMESLICE_PLACE ] = timeslice;
+                        
+                        cb->call_init ( timeslice );
+                            
+                        send2callboxes( build_cb_package( ext, port, INVITE, 
+                            ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                        
+                        if( debug_invite ) debug_msg("[%d] TS::%d <cb_trying,cb_ringing>", ext, data[ TIMESLICE_PLACE ] );
+                    }
+                }break;
+                case cb_ringing : {
+                    call = cb->invite();
                     
-                    //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) );
-                    cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
+                    if( cb->get_sip_status() == sip_busy ){
+                        cb->cb_set_status( cb_busy );
+                    }else if( call != NULL ){
+                        if( __find_Call__( v_call, call->get_cb_ext() ) == NULL ) v_call -> add( call );
                         
-                    send2callboxes( __build_cb_package__( ext, port, __INVITE__, 
-                        ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-                    
-                    if( debug_invite ) debug_msg("-- %d on TS::%d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
-                }
-            }break;
-            case cb_ringing : {
-                call = cb->invite();
-                
-                if( cb->get_sip_status() == sip_busy ){
-                    cb->cb_set_status( cb_busy );
-                }else if( call != NULL ){
-                    if( __find_Call__( v_call, call->get_cb_ext() ) == NULL ) v_call -> add( call );
-                    
-                    call->init_cbx_pkg_idle_timer();
-                    
-                    if( cb->get_sip_status() == sip_on_call ){
+                        call->init_cbx_pkg_idle_timer();
+                        
+                        if( cb->get_sip_status() == sip_on_call ){
                         
-                        set_status( cb->status, cb_on_call );
+                            data[ TIMESLICE_PLACE ] = cb->get_timeslice();  
+                            
+                            send2callboxes( build_cb_package( ext, port, INVITE, ( char * )data, 
+                                cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                            
+                            cb->call_confirmed ();    
                     
-                        data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();  
-                        
-                        //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                        cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                        
-                        send2callboxes( __build_cb_package__( ext, port, __INVITE__, ( char * )data, 
-                            cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-                        //debug_msg("send ok liga o mic");                        
-                            
-                        cb->set_invite_response_pending();
-                        
-                        if( debug_invite ) debug_msg( "-- ringing -- accepting call request --" );
+                            if( debug_invite ) debug_msg( "[%d] Ringing call ok <cb_ringing,cb_on_call>", ext );
+                        }
+                    }else{
+                        if( cb->get_sip_status() == sip_denied )
+                        {
+                            cb->cb_set_status( cb_denied );
+                            if( debug_invite ) debug_msg( "[%d] call denied <sip_denied>::<cb_ringing,cb_denied>", ext );
+                        } else {
+                            if ( cb->time_to_retry () )
+                            {
+                                if ( cb->sip != NULL )
+                                {
+                                    if ( cb->sip->get_status () == sip_waiting_trying )
+                                    {
+                                        if ( debug_invite ) debug_msg ("[%4i %3i] Resend invite to *", ext, cb -> get_invite_try_number ()  );
+                                        cb->retry_send_invite_pkg_to_ast ();    
+                                    }
+                                }
+                            }    
+                        }
                     }
-                }else{
-                    if( cb->get_sip_status() == sip_denied ) cb->cb_set_status( cb_denied );
-                }
-            }break;
-            case cb_busy : {
-                set_status( cb->status, cb_idle );
-                ts->return_timeslice( cb->get_timeslice() );
-                cb->set_timeslice( 0x00 );
-                data[ __TIMESLICE_PLACE__ ] = 0x00;
-                if( debug_invite ) debug_msg("-- %d on %d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
-                
-                //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                
-                send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, 
-                    ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-                
-                if( debug_invite ) debug_msg( "-- Trying -- denying call request << Busy Here >> --" );
+                }break;
+                case cb_busy : {
+                    data[ TIMESLICE_PLACE ] = 0;
+                    
+                    send2callboxes( build_cb_package( ext, port, CB_BYE, 
+                        ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                    
+                    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] Busy Here <cb_busy,cb_idle>", ext );
+                }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;
+                case cb_denied : {
+                    data[ TIMESLICE_PLACE ] = 0;
+                    
+                    send2callboxes( build_cb_package( ext, port, CB_BYE, 
+                        ( char * )data, cb->msg_id_update (), CB_BUFFER_SIZE - VZ_HEADER_OFFSET, write_buffer ) );
+                        
+                    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;
+                        }
                     }
-                }
-        
-                cb->send_bye();
-                set_status( cb->sip->status, sip_idle );
-                //cb->re_start_timer();
-            }break;
-            case cb_denied : {
-                ts->return_timeslice( cb->get_timeslice() );
-                cb->set_timeslice( 0x00 );
-                data[ __TIMESLICE_PLACE__ ] = 0x00;
-                if( debug_invite ) debug_msg("-- %d on %d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
-                
-                //cb->set_msg_id( ( cb->get_msg_id() &  ~BIT7 ) + 1 );
-                cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
-                
-                send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, 
-                    ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-                    
-                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;
-                    }
-                }
-                //cb->re_start_timer();
-                if( debug_invite ) debug_msg( "-- Trying -- denying call request --" );
-                set_status( cb->status, cb_idle );
-                set_status( cb->sip->status, sip_idle );
-            }break;
+                    ts->return_timeslice( cb->call_end () );
+    
+                    if( debug_invite ) debug_msg( "[%d] call denied <cb_denied,cb_idle>", ext );
+                }break;
+            }
         }
     }
 }
\ No newline at end of file