Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
81:3656f00ab3db
Parent:
78:1353744f01e1
Child:
82:f55d13babca0
--- a/sip.cpp	Tue Dec 16 19:34:30 2014 +0000
+++ b/sip.cpp	Tue Dec 30 12:37:40 2014 +0000
@@ -2,9 +2,8 @@
 
 void Sip::__init_sock__( void ){
     sip_server.set_address( this->server_ip , this->server_port );
-    sock.set_blocking( false, 1 ); // verificar isso com mais cuidado depois
+    sock.set_blocking( false, 5 ); // verificar isso com mais cuidado depois
     sock.bind( this->my_port );
-    //sock.init();
     sock.init();
 }
 
@@ -37,6 +36,7 @@
     status = sip_idle;
     
     __init_sock__();
+    muted = false;
 }
   
 Sip::Sip( int id, uint16_t my_port ){
@@ -128,13 +128,6 @@
         }
     }
     fclose( fp );
-    
-    /*
-    this->my_port = my_port;
-    this->my_rtp_port = fill_random_rtp_port();
-    this->my_ext = id;
-    itoa( this->my_ext, this->my_display, 10 );
-    */
     strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
     strcpy( this->last_invite_tag, "" );
     strcpy( this->last_invite_callid, "");
@@ -142,6 +135,7 @@
     status = sip_idle;
     
     __init_sock__();
+    muted = false;
 }
 
 Sip::~Sip(){
@@ -152,17 +146,32 @@
     char out[ 1000 ];
     if( debug_sip ) debug_msg("Registry %d", this->my_ext );
     build_registry_package( buffer );
-    int sent = sock.sendTo( sip_server, buffer, 512 ); // > 400
-    int rcv = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
+    
+    int sent = 0;
+    
+    do{
+        sent = sock.sendTo( sip_server, buffer, 512 ); // > 400
+    }while( sent == 0 );
+    
+    int rcv = 0;
+  
+    rcv = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
 
     if( sent == -1 || rcv == -1 ){
+        debug_msg("");
+        muted = true;
         __reconnect__();
+    }else{
+        muted = false;
     }
     
     if( debug_sip ) debug_msg("Sent %i bytes to ext %d", sent, my_ext );
     if( decode_gettag( (unsigned char *)buffer, "cseq: ", out ) ){
         if(strlen( out ) > 7){
             if(strcasecmp("options",out+strlen(out)-7) == 0){
+                // esse teste nao esta mais em uso, uma vez que na 
+                // configuracao atual do asterisk nao existe
+                // mais o envio desse pacote
                 char ans[ 2000 ];
                 build_reply_package( ans, (unsigned char*)buffer );
                 sock.sendTo( sip_server, ans, sizeof( 512 ) );
@@ -176,19 +185,25 @@
     Timer invite_timer;
     char callbox_string[ 32 ];
     itoa( get_id(), callbox_string, 10 );
- 
+    static int invite_pkg_sent;
     if( status != sip_on_call ){
         if( status == sip_idle ){
             int cseq = 0;
  
             build_invite_package( buffer, callbox_string, &cseq );
-            sock.sendTo( sip_server, buffer, sizeof( buffer ) );
+            invite_pkg_sent = 0;
+            do{
+                invite_pkg_sent = sock.sendTo( sip_server, buffer, sizeof( buffer ) );
+            }while( invite_pkg_sent == 0 );
+            
+            if( debug_sip ) debug_msg("Return value for invite pkg %d", invite_pkg_sent );
          
-            if( debug_sip ) debug_msg( "Inviting" );
-            if( debug_sip ) debug_msg( "Sip::status::%d", status );
+            if( debug_sip ) debug_msg( "Inviting\r\nSip::status::%d", status );
+            
             invite_timer.start();
         
             sock.set_blocking( false, 3 );
+            
             set_status( status, sip_waiting_trying );
         }          
     }else{
@@ -200,20 +215,33 @@
     uint32_t received_loop_times = 0;
     Timer check_point_timer;
     check_point_timer.start();
-    uint16_t length = 0;
+    int16_t length = 0;
+    static int ok_sent = 0;
     static bool waiting = false;
+    static uint16_t length_muted = 0;
     VZ_call * call = NULL;
     
     if( invite_timer.read() > __INVITE_MAX_WAITING_TIME__ ){
+        if( ( invite_pkg_sent == -1 ) || length_muted || ok_sent == -1 ){
+            muted = true;
+            if( debug_invite || debug_muted ) debug_msg("RX from eth having situation, length_muted( %d ) - ok_sent( %d )", length_muted, ok_sent );
+            ok_sent = 0;
+            length_muted = 0;
+            __reconnect__();
+        }else{
+            muted = false;    
+        }
+        
         if( debug_invite ) debug_msg("Timeout do pedido");
         invite_timer.stop();
         invite_timer.reset();
-        set_status( status, sip_denied ); // timeout invite package
+        set_status( status, sip_denied );
         send_bye();
         return( NULL );
     }
     loop_times++;
     length = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
+    if( length == -1 ){ length_muted++; }
     if( length > 0 ){
         check_point_timer.reset();
         received_loop_times++;
@@ -230,7 +258,11 @@
                     if( call == NULL ) call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
                     char ans[ 1024 ];
                     build_ack_package( ans, ( unsigned char *)buffer );
-                    sock.sendTo( sip_server, ans, sizeof( ans ) );
+                    
+                    do{
+                        ok_sent = sock.sendTo( sip_server, ans, sizeof( ans ) );
+                    }while( ok_sent == 0 );
+                    
                     if( debug_sip ) debug_msg("Call alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
                     set_status( status, sip_on_call );
                     return( call );
@@ -272,6 +304,7 @@
         if( debug_invite ) send_msg("entrei no loop %d vezes", received_loop_times );
         check_point_timer.reset();
     }
+    
     if( waiting == true ){ return( call ); }
     
     return( NULL );
@@ -961,26 +994,45 @@
 */
 int Sip::listen_SIP_server( void ){
     sock.set_blocking( false, 1 );
-    //debug_msg("");
-    //uint8_t read = sock.receiveFrom( sip_server, this->buffer, sizeof( this->buffer ) );
-    uint8_t read = sock.receiveFrom( sip_server, this->buffer, sizeof( this->buffer ) );
+    
+    int bye_sent = 0;
+    int options_sent = 0;
+    
+    int read = 0;
+    
+    read = sock.receiveFrom( sip_server, this->buffer, sizeof( this->buffer ) );
+    
     if( read > 0 ){
-        //debug_msg( "-- SIP recebeu algo --" );
         if( !( strncasecmp( buffer, "bye", strlen("bye") ) ) ){
             char ans[ 2000 ];
             build_reply_package( ans, (unsigned char*)buffer );
-            //sock.sendTo( sip_server, ans, sizeof( ans ) );  
-            sock.sendTo( sip_server, ans, 768 );  
+            
+            do{
+                bye_sent = sock.sendTo( sip_server, ans, 768 );
+            }while( bye_sent == 0 );  
+            
             if( debug_sip ) debug_msg("Server request BYE to %d ext", my_ext );
+            
+            if( debug_invite ) debug_msg("Bye request received from *");
+            
             return( this->my_ext );
         }else if( !( strncasecmp( buffer, "options", strlen("options") ) ) ){
             char ans[ 2000 ];
             if( debug_sip ) debug_msg("Options received ");
             build_reply_package( ans, (unsigned char*)buffer );
-            //sock.sendTo( sip_server, ans, sizeof( ans ) );
-            sock.sendTo( sip_server, ans, 512 );
+            
+            do{
+                options_sent = sock.sendTo( sip_server, ans, 512 );
+            }while( options_sent == 0 );
         }
     }
+    
+    if( read == -1 || bye_sent == -1 || options_sent == -1 ){
+        muted = true;
+    }else{
+        muted = false;    
+    }
+    
     return( 0 ); 
 }
 
@@ -994,13 +1046,31 @@
 
 void Sip::send_bye( void ){
     build_bye_package( this->buffer );
-    sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );
+    int ret = 0;
+    do{
+        ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );    
+    }while( ret == 0 );
+    
+    if( ret == -1 ){
+        muted = true;
+    }else{
+        muted = false;    
+    }
 }
 
 void Sip::send_unregistry_pkg(){
     build_unregistry_package( this->buffer );
-    for( register int i = 0; i < 7; i++ )
-        sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );
+    //for( register int i = 0; i < 7; i++ )
+    int ret = 0;
+    do{
+        ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) );
+    }while( ret == 0 );
+    
+    if( ret == -1 ){
+        muted = true;
+    }else{
+        muted = false;    
+    }
 }
 
 char * Sip::build_unregistry_package( char * buffer ){
@@ -1068,13 +1138,6 @@
     itoa( server_port, itoa_buffer, 10 );
     strcat( buffer, itoa_buffer );
     strcat( buffer, ">;expires=0\r\n" );
-    /*
-    strcat( buffer, ">;expires=" );
-    itoa( 0, itoa_buffer, 10 );
-    strcat( buffer, itoa_buffer );
-    strcat( buffer, "\r\n" );
-    */
-    
     strcat( buffer, "User-Agent: VZtech/pabxdriver-" );
     strcat( buffer, SVNREV );
     strcat( buffer, "\r\nContent-Length: 0\r\n" );
@@ -1090,4 +1153,9 @@
 
 void Sip::sip_set_status( uint8_t status ){
     this->status = status;    
+}
+
+void Sip::sip_check_muted( void ){
+    if( muted == true ) __reconnect__();
+    if( debug_muted ) send_msg("For some reason sip muted, reconnected");
 }
\ No newline at end of file