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_box.cpp	Fri Apr 24 13:54:24 2015 +0000
+++ b/call_box.cpp	Thu Apr 30 15:42:41 2015 +0000
@@ -14,16 +14,15 @@
     if( sip == NULL )
     {
         memory_is_over = true;
-        if( debug_memory ) debug_msg("Sip allocation fail");
+        if( debug_memory ) debug_msg("[%d] Sip allocation fail", this->ext );
     }
     
     next_aging_type = __CYCLES__;
-    seconds.start();
     invite_response = true;
-    //invite_first_response = true;
     bye_response = true;
     invite_retry_count = MAX_INVITE_RETRY;
     cb_new_counter++;
+    invite_try_number = 0;
 }
 
 int Call_Box::get_status( void ){ return this->status; }
@@ -50,6 +49,7 @@
 }
 
 int Call_Box::get_port( void ){ return( this->port ); }
+
 int Call_Box::get_ext( void ){ return( this->ext ); }
 
 float Call_Box::get_elapsed_time( void ){ return( this->t.read_ms() ); }
@@ -60,79 +60,38 @@
     next_aging_type = __CYCLES__;
 }
 
-bool Call_Box::is_timeout( void ){
-    if( seconds.read() > __RESQUEST_TIME__ ){
-        seconds.reset();
-        return( t.read() > __TIME_TO_REGISTER__ ) ? true : false;
-    }else return false;
-}
-
-bool Call_Box::has_next_try( void ){
-    return( ( t.read() < __TIMEOUT__ ) ? true : false );
-}
-
 void Call_Box::registry( void ){
-    if( this->sip == NULL ){
+    if ( drop_registry_pkg )
+    {
+        send_msg("[%d] Dropando registry pck", this->ext );
+        return;
+    }    
+    
+    if( this->sip != NULL )
+    {
+        if( sip->registry() > 0x00 ) reset_elapsed_time();
+        if( debug_aging ) send_msg( "[%d] Registered - ( %f )", this->ext, __TIMEOUT__ - t.read() );
+    
+    } else {
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
         deleted_sip++;
-        this->sip = new Sip( ext, port );
-        if( sip == NULL ){
-            memory_is_over = true;
-            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
-            return;
-        }
-    
-        if( sip->registry() > 0x00 ) reset_elapsed_time();
-        if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() );
-        
-        delete( sip );
-        this->sip = NULL;
-    }else{
-        if( sip->registry() > 0x00 ) reset_elapsed_time();
-        if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() );
     }
 }
 
-void Call_Box::unregistry( void ){
-    if( this->sip == NULL ){ 
-        deleted_sip++;
-        this->sip = new Sip( ext, port );
-        if( sip == NULL ){
-            memory_is_over = true;
-            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
-            return;    
-        }
-        sip->send_unregistry_pkg();
-    
-        delete( sip );
-        this->sip = NULL;
-    }else{
-        sip->send_unregistry_pkg();
-    }
-}
-
-VZ_call * Call_Box::invite( void ){
-    //t.stop();
+VZ_call * Call_Box::invite ( void )
+{
     VZ_call * call = NULL;
     
-    if( this->sip == NULL ){
-        deleted_sip++;
-        this->sip = new Sip( ext, port );
+    if( this->sip != NULL )
+    {
+        call = sip->invite();
+    } else {   
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
         
-        if( sip != NULL ){
-            memory_is_over = true;
-            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
-            return( NULL );
-        }else{
-            call = sip->invite();
-            
-            delete( sip );
-            this->sip = NULL;
-        }
-    }else{   
-        call = sip->invite();
+        deleted_sip++;
     }
     
-    if( debug_cb ) debug_msg("Call returned value :: %p ", (void * )call );
+    if( debug_cb ) send_msg("[%d] Call returned value :: %p ", this->ext, (void * )call );
     if( call == NULL ) t.start();
     return( call );
 }
@@ -143,27 +102,17 @@
     > 0 :: devo remover essa call do vetor de calls 
 */
 int Call_Box::listen_SIP_server( void ){
-    if( this->sip == NULL ){
+    if( this->sip == NULL )
+    {
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
+        
         deleted_sip++;
-        if( status == cb_on_call || status == cb_idle ){
-            this->sip = new Sip( ext, port );
-            
-            if( sip == NULL ){
-                memory_is_over = true;
-                if( debug_memory ) debug_msg("Call_Box cb allocation fail");
-                return( -1 );
-            }else{
-                int out_read = sip->listen_SIP_server();
-                
-                delete( sip );
-                this->sip = NULL;
-                return( out_read );
-            }
-        }else return( 0 );
-    }else{
+        
+        return ( -3 );
+    } else {
         if( status == cb_on_call || status == cb_idle ){
             return ( sip->listen_SIP_server() );
-        } else return( 0 );
+        } else return ( 0 );
     }
 }
 
@@ -177,42 +126,28 @@
 }
 uint8_t Call_Box::get_timeslice( void ){ return( this->timeslice ); }
 
-//void Call_Box::set_sip( Sip * sip ){
-//    this->sip = sip;    
-//}
-
 void Call_Box::send_bye( void ){ 
-    if( this->sip == NULL ){
-        deleted_sip++;
-        this->sip = new Sip( ext, port );
+    if( this->sip == NULL )
+    {
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
         
-        if( sip == NULL ){
-            memory_is_over = true;
-            if( debug_memory ) debug_msg("Call_Box cb allocation fail");
-            return;
-        }else{
-        
-            sip->send_bye();
-            
-            delete( sip );
-            sip = NULL;
-        }
-    }else{
+        deleted_sip++;
+    } else {
         sip->send_bye();
     }
 }
 
 void Call_Box::reset_sip( void ){
-    debug_msg("reset_sip");
+    debug_msg("[%d] reset_sip", this->ext );
     
     if( this->sip != NULL )
     {
          delete( this->sip );
-         debug_msg("deletei");
+         debug_msg("[%d] deletei", this->ext );
     }
     
     this->sip = new Sip( ext, port );
-    debug_msg( "sobrescrevi" );
+    debug_msg( "[%d ]sobrescrevi", this->ext );
     
     deleted_sip++;
     
@@ -223,74 +158,63 @@
     if( this->sip != NULL ){
         this->sip->status = status;
     }else{
-        if( debug_cb) debug_msg("Sip equals NULL o.O");
+        if( debug_cb) debug_msg("[%d] Sip equals NULL o.O", this->ext );
         deleted_sip++;
-        debug_msg("Sip equals NULL o.O");
+        debug_msg("[%d] Sip equals NULL o.O", this->ext );
     }        
 }
 
-void Call_Box::re_start_timer( void ){
-    t.start();    
-}
-
-int8_t Call_Box::get_next_aging_type( void ){
-    next_aging_type--;
-    
-    if( ( next_aging_type % 2 ) == 0  || ( next_aging_type > __CYCLES__ - 15 ) ) return( REQUEST_REGISTRY );
-    
-    else if( ( next_aging_type % 7 ) == 0 ) return( REQUEST_PEND );
-    
-    else return( REQUEST_PING );
+void Call_Box::set_invite_response_ok ( void )
+{ 
+    this->invite_response = true; 
+    if ( debug_invite ) send_msg ("[%d] Invite response :: ok", this->ext );
 }
 
-void Call_Box::set_invite_response_ok( void ){ this->invite_response = true; }
+void Call_Box::set_invite_response_pending( void ){ this->invite_response = false; }
 
-//void Call_Box::set_first_invite_response_ok( void ){ this->invite_first_response = true; 
-//    debug_msg("");
-//}
-
-void Call_Box::set_invite_response_pending( void ){ this->invite_response = false; }
 bool Call_Box::get_invite_response( void ){ return( this->invite_response ); }
 
-//void Call_Box::set_first_invite_response_pending( void ){ this->first_invite_response = false; }
-//bool Call_Box::get_first_invite_response( void ){ return( this->first_invite_response ); }
+void Call_Box::invite_retry_count_reset( void ){ invite_retry_count = MAX_INVITE_RETRY; }
 
-
-void Call_Box::invite_retry_count_reset( void ){ invite_retry_count = MAX_INVITE_RETRY; }
-uint8_t Call_Box::get_invite_retry_count( void ){ 
+uint16_t Call_Box::get_invite_retry_count( void ){ 
     return ( invite_retry_count ) ? invite_retry_count-- : 0;
 }
 
 void Call_Box::set_bye_response_ok( void ){ this->bye_response = true; }
+
 void Call_Box::set_bye_response_pending( void ){ this->bye_response = false; }
+
 bool Call_Box::get_bye_response( void ){ return( this->bye_response ); }
 
 
-int Call_Box::get_sip_socket_fd( void ){
-    return( sip_socket_fd );
-}
+int Call_Box::get_sip_socket_fd ( void ) { return( sip_socket_fd ); }
 
 int Call_Box::sip_udp_incomming_pkg( void ){
     return sip->udp_incomming_pkg();
 }
 
-void Call_Box::reset_cb_status( void ){
-    if( sip != NULL ) sip->reset_call();
-}
+void Call_Box::reset_cb_status ( void ) { if( sip != NULL ) sip->reset_call(); }
 
 int Call_Box::get_sip_ext( void ){ return this->sip->get_ext(); }
+
 int Call_Box::get_sip_port( void ){ return this->sip->get_port(); }
 
-int Call_Box::get_timer( void ){ return t.read(); }
+int Call_Box::get_timer ( void )
+{
+    int now = t.read ();
+    if( now >= 9999 || now <= 0 ) t.reset ();
+    return t.read (); 
+}
 
 int Call_Box::get_rtp_port ( void )
 {
     if( this->sip != NULL ){
         return this->sip->get_sip_rtp_port();
     }else{
-        if( debug_cb) debug_msg("Sip equals NULL o.O");
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
+        
         deleted_sip++;
-        debug_msg("Sip equals NULL o.O");
+        
         return -1;
     }
 }
@@ -300,12 +224,33 @@
     if( this->sip != NULL ){
         this->sip->set_sip_rtp_port( new_rtp_port );
     }else{
-        if( debug_cb) debug_msg("Sip equals NULL o.O");
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
+        
         deleted_sip++;
-        debug_msg("Sip equals NULL o.O");
     }
 }
 
+void Call_Box::init_rtp_timer ( void )
+{
+    this->rtp_timer.start();   
+}
+
+void Call_Box::reset_rtp_timer ( void )
+{
+    this->rtp_timer.stop();
+    this->rtp_timer.reset();
+}
+
+bool Call_Box::is_rtp_timer_timeout ( void )
+{
+    return ( rtp_timer.read () > RTP_REQUEST_PORT_TIMEOUT ) ? true : false;  
+}
+
+int Call_Box::get_rtp_timer ( void )
+{
+    return ( rtp_timer.read () );
+}
+
 int Call_Box::print_yourself ( void )
 {   
     send_msg ("");
@@ -347,30 +292,115 @@
     send_msg ("invite_response :: %s", ( invite_response ) ? "true" : "false" );
     send_msg ("invite_retry_count :: %u", invite_retry_count );
     send_msg ("bye_response :: %s", ( bye_response ) ? "true" : "false" );
-    send_msg ("seconds :: %d", ( int )seconds.read () );
     send_msg ("sip_socket_fd :: %d", sip_socket_fd );
     send_msg ("");
     
     return ( sizeof( Call_Box ) );
 }
- 
-void Call_Box::init_rtp_timer ( void )
+
+int Call_Box::call_init ( int timeslice )
 {
-    this->rtp_timer.start();   
-}
+    invite_timer.reset ();
  
-void Call_Box::reset_rtp_timer ( void )
-{
-    this->rtp_timer.stop();
-    this->rtp_timer.reset();
+    invite_timer.start ();
+    
+    this->timeslice = timeslice;
+    
+    this->status = cb_ringing;
+    
+    set_invite_response_pending ();            
+    
+    invite_try_number = 0;
+
+    msg_id_update ();
+    
+    return 0;    
 }
- 
-bool Call_Box::is_rtp_timer_timeout ( void )
+
+int Call_Box::call_end ( bool send_bye_to_ast )
 {
-    return ( rtp_timer.read () > RTP_REQUEST_PORT_TIMEOUT ) ? true : false;  
+    uint8_t timeslice = this->timeslice;
+    
+    this->timeslice = 0;
+    
+    this->status = cb_idle;
+    
+    if ( sip != NULL ) {
+        sip -> sip_set_status ( sip_idle );
+    } else {
+        if( debug_cb ) debug_msg("[%d] Sip equals NULL o.O", this->ext );
+    }
+    
+    set_rtp_port ( 0 );
+    
+    reset_rtp_timer ();
+    
+    if ( send_bye_to_ast )
+    {
+        if ( timeslice != 0 ) send_bye ();
+    }
+    
+    reset_cb_status ();
+    
+    invite_timer.stop ();
+    
+    invite_timer.reset ();
+    
+    invite_try_number = 0;
+    
+    msg_id_update ();
+    
+    return timeslice;
 }
- 
-int Call_Box::get_rtp_timer ( void )
+
+int Call_Box::call_confirmed ( void )
+{
+    set_invite_response_pending ();
+    
+    this->status = cb_on_call;
+    
+    msg_id_update ();
+    
+    return 0;
+}
+
+uint8_t Call_Box::msg_id_update ( void )
+{    
+    this->msg_id = ( ( this->msg_id + 1 ) & ~BIT7 ) ? msg_id : 0x11;
+    
+    return this->msg_id;
+}
+
+void Call_Box::call_config ( void )
 {
-    return ( rtp_timer.read () );
+    set_invite_response_ok (); 
+    reset_rtp_timer ();
+    init_rtp_timer ();    
+}
+
+bool Call_Box::time_to_retry ( void )
+{
+    if ( invite_timer.read_ms () > 1000 ){
+        invite_timer.reset ();
+        return true;
+    } else {
+        return false;    
+    }
+}
+
+uint8_t Call_Box::get_invite_try_number ( void ) {return invite_try_number; }
+
+int Call_Box::retry_send_invite_pkg_to_ast ( void )
+{   
+    if( this->sip != NULL )
+    {
+        invite_try_number++;
+        return (  sip -> retry_send_last_invite_pkg_to_ast () );
+    } else {
+        debug_msg("[%d] Eu definitivamente nao deveria estar aqui!!!", this->ext );
+        
+        deleted_sip++;
+        
+        return ( 0 );
+    }
 }
\ No newline at end of file