Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
109:a5b8264ffbbc
Parent:
108:18a3702650f3
Child:
112:6ae726539ab9
--- a/sip.cpp	Wed Mar 18 13:33:54 2015 +0000
+++ b/sip.cpp	Tue Apr 07 14:27:44 2015 +0000
@@ -11,8 +11,9 @@
 }
 
 void Sip::__reconnect__( void ){
-    __end_sock__();
-    __init_sock__();
+    sock.close(); 
+    sock.init();
+    sock.bind( this->my_port );
 }
 
 Sip::Sip( char * server_ip, int server_port, char * my_ip, int my_port, int my_ext, int peer_ext, int id ){
@@ -23,9 +24,7 @@
     strncpy( this->my_ip, my_ip, 20 );
     this->my_ip[19] = 0;
     this->my_port = my_port;
-    //this->my_rtp_port = fill_random_rtp_port();
-    this->my_rtp_port = 0;
-    if( debug_rtp ) debug_msg("new with rtp port %d ", this->my_rtp_port );
+    this->my_rtp_port = fill_random_rtp_port();
     this->my_ext = my_ext;
     itoa( this->my_ext, this->my_display, 10 );
     this->peer_ext = peer_ext;
@@ -58,8 +57,6 @@
     if( debug_sip ) debug_msg("Building SIP ext :: %d | Port :: %d", id, my_port );
     
     this->id = id;
-    this->my_ext = id;
-    this->my_port = my_port;
         
     fp = fopen( "/qspi/serverip.txt", "r");
     if( fp == NULL){
@@ -123,13 +120,12 @@
     
     this->my_ip[ 15 ] = '\0';
     
+    this->my_port = my_port;
     if( debug_sip ) debug_msg("Fixado na porta %d", this->my_port );
     
+    this->my_rtp_port = fill_random_rtp_port();
+    this->my_ext = id;
     itoa( this->my_ext, this->my_display, 10 );
-    
-    //this->my_rtp_port = fill_random_rtp_port();
-    this->my_rtp_port = 0;
-    if( debug_rtp ) debug_msg("new with rtp port %d ", this->my_rtp_port );
 
     fp = fopen( "/qspi/peerext.txt", "r");
     if( fp == NULL ) {
@@ -173,18 +169,15 @@
 int Sip::registry(){
     build_registry_package( buffer );
     
-    int send = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400
-    if( send != strlen( buffer ) )
-    {
-        if( debug_reconnect ) send_msg("Reconnect SIP -- Registry");
-        if( debug_reconnect ) send_msg("Sent %i of %i bytes", send, strlen( buffer ) );
-        __reconnect__();
-        miss_sip_registry_send_pkg++;
+    int sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400
+
+    if( debug_sip ) send_msg("Registry ( %d, %d ) - Sent %i of %i bytes", this->my_ext, this->my_port , sent, strlen( buffer ) );
+    
+    if( sent != strlen( buffer ) ){
+        sip_socket_send_failure++;
     }
-
-    if( debug_sip ) send_msg("Registry ( %d, %d ) -- Sent %i of %i bytes", this->my_ext, this->my_port, send, strlen( buffer ) );
     
-    return( send );
+    return( sent );
 }
 
 VZ_call * Sip::invite(){
@@ -202,21 +195,19 @@
         
         this->invite_pkg_sent = 0;
         
-        do{
-            this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) );
-        }while( this->invite_pkg_sent == 0 );
-        
-        //FIXME verificar se posso nao usar invite_pkg_sent
-        int send = this->invite_pkg_sent;
-        if( send != strlen( buffer ) )
+        if ( drop_invite_to_ast_pkg )
+        {
+                debug_msg("Droped invite pkg to ast");
+        }
+            else
         {
-            if( debug_reconnect ) send_msg("Reconnect SIP -- Invite");
-            __reconnect__();
-            miss_sip_invite_send_pkg++;
+            do{
+                this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) );
+            }while( this->invite_pkg_sent == 0 );
+            
+            if( debug_invite ) debug_msg("Return value for invite pkg %d", invite_pkg_sent );
         }
         
-        if( debug_invite || debug_reconnect ) send_msg("Return value for invite pkg %d", invite_pkg_sent );
-        
         invite_timer.stop();
         invite_timer.reset();
         invite_timer.start();
@@ -372,7 +363,7 @@
     strcat( header, "." );
     itoa( 0, itoa_buffer, 10 );
     strcat( header, itoa_buffer );
-    strcat( header, "/UDP"  );
+    strcat( header, "/UDP"  ); // isso que dizer qu eue sempre coloco /UDP:192, quando deveria ser /UDP<0x20>192...
     strcat( header, this->my_ip );
     strcat( header, ":" );
     itoa( this->my_port, itoa_buffer, 10 );
@@ -467,10 +458,28 @@
     strcat( body, itoa_buffer );
     strcat( body, "\r\n" );
     strcat( body, "m=audio " );
-    this->my_rtp_port = fill_random_rtp_port();
     itoa( this->my_rtp_port, itoa_buffer, 10 );
-    strcat( body, itoa_buffer );
-    if( debug_rtp ) debug_msg("Invite PKG with rtp port ( %d - %s )", this->my_rtp_port, itoa_buffer );
+    debug_msg("my_rtp_port( %d )", my_rtp_port );
+    if ( chicago )
+    {
+        chicago = false;
+        itoa( ( this->my_rtp_port ) + 19, itoa_buffer, 10 );
+        debug_msg("my_rtp_port( %d )", my_rtp_port + 19 );
+        strcat( body, itoa_buffer );
+    }
+        else
+    {
+        if ( canada )
+        {
+            canada = false;
+            strcat( body, " " );
+        }
+            else
+        {
+            strcat( body, itoa_buffer );
+        }
+    }
+    
     strcat( body, " RTP/AVP " );
     itoa( 8, itoa_buffer, 10 );
     strcat( body, itoa_buffer );
@@ -904,13 +913,9 @@
 int Sip::get_my_rtp_port( void ){
     return this->my_rtp_port;
 }
-int Sip::fill_random_rtp_port ()
-{ 
-    static uint16_t port = 0;
-    port += 2;
-    //port &= 0x7fe; // 0 to 2046, always even
-    port &= 0x0e; // just for testing
-    return port + 16384;
+inline int Sip::fill_random_rtp_port(){  
+    // o range [ 16384 - 32767 ] eh configuravel no server
+    return ( ( this->my_ext + 16384 ) );
 }
  
 void Sip::change_sip_server(){
@@ -963,30 +968,31 @@
 }
 
 void Sip::send_bye( void ){
-    int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
+    
+    int ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
     
     if( debug_sip ) send_msg( "sizeof( bye pkg ) :: %d", strlen( buffer ) );
     
-    if( send != strlen( this->buffer ) )
-    {
-        if( debug_reconnect ) send_msg("Reconnect SIP -- BYE, %d, %d", send, strlen( this->buffer ) );
-        __reconnect__();
-        miss_sip_bye_send_pkg++;
+    if( ret == -1 ){
+        muted = true;
+    }else{
+        muted = false;    
     }
 }
 
 void Sip::send_unregistry_pkg(){
     build_unregistry_package( this->buffer );
     
-    int send = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
-    if( send != strlen( this->buffer ) )
-    {
-        if( debug_reconnect ) send_msg("Reconnect SIP -- Unregistry");
-        __reconnect__();
-        miss_sip_unregistry_send_pkg++;
+    int ret = 0;
+    
+    ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) );
+    if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) );
+    
+    if( ret == -1 ){
+        muted = true;
+    }else{
+        muted = false;    
     }
-                
-    if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) );
 }
 
 char * Sip::build_unregistry_package( char * buffer ){
@@ -1085,15 +1091,9 @@
     
     int length = sock.receiveFrom( from, buffer, sizeof( buffer ) );
     
-    //checa se o pacote foi recebido do antigo host conhecido.
     if( memcmp( ( ( u8_t * )( &(from._remoteHost) ) ) + 2, ( ( u8_t * )( &(sip_server._remoteHost) ) ) + 2, 6 ) != 0 ) return 0;
     
-    if( length == -1 )
-    {
-        if( debug_reconnect ) send_msg("Reconnect SIP -- UDP Incomming");
-        __reconnect__();
-        miss_sip_inc_pkg++;
-    }
+    if( length == -1 ){ length_muted++; }
     
     if( length > 0 ){
         if( sizeof( buffer ) > length ) buffer[ length ] = 0;
@@ -1106,30 +1106,37 @@
         }
         
         if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ 
-            //if( !( strncasecmp( buffer + 7, " 200 ", 5 ) ) ){
-            if( sip_response == 200 ){ // Ok
+            if( sip_response == 200 ){ // Ok    
                 char *ref = strstr( buffer, "audio" );
                 if( debug_invite ) debug_msg("ok");
                 if( ref != NULL ){
+                    if ( drop_ok_pkg )
+                    {
+                        debug_msg("Droped ok pkg received from ast");
+                        return -71;    
+                    }
+                                        
                     ref += 6; // audio 
                     ref = strtok( ref, " ");
                     if( call == NULL ){
                         call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
-                        if( debug_rtp ) debug_msg("Ok invite ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref );
                         if( call == NULL ) memory_is_over = true;
                     }
                     char ans[ 1024 ];
                     build_ack_package( ans, ( unsigned char *)buffer );
                     
-                    int send = sock.sendTo( sip_server, ans, strlen( ans ) );
                     
-                    if( send != strlen( ans ) )
+                    if ( drop_ack_pkg )
                     {
-                        if( debug_reconnect ) send_msg("Reconnect SIP -- Ok -- Call mode on");
-                        __reconnect__();
-                        miss_sip_ok_send_pkg++;
+                        debug_msg("Droped ack pkt to ast");
+                        return -73;
+                    }
+                        else
+                    {
+                        ok_sent = sock.sendTo( sip_server, ans, strlen( ans ) );
                     }
                     
+                    debug_msg("sizeof( ack ) :: %d - strlen( ans ):: %d", ok_sent, strlen( ans ) );
                     if( debug_invite ) debug_msg("Call alocada -- Sip::status::%d :: ", status );
                     set_status( status, sip_on_call );
                 }
@@ -1154,7 +1161,6 @@
                     ref = strtok( ref, " ");
                     if( call == NULL ){
                         this->call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
-                        if( debug_rtp ) debug_msg("Session in Progress ( %d, %d, %d, %d ( %s ) ) ", this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ), ref );
                         if( call == NULL ) memory_is_over = true;
                     }
                     waiting = true;
@@ -1169,6 +1175,7 @@
             }
         }
         if( status == sip_waiting_trying ){
+            //if( !( strncasecmp( buffer + 7, " 100 ", 5 ) ) ){
             if( sip_response == 100 ){
                 if( debug_invite ) debug_msg("trying")
                 set_status( status, sip_trying );
@@ -1179,22 +1186,16 @@
                 char ans[ 2000 ];
                 build_reply_package( ans, (unsigned char*)buffer );
                 
-                int send = sock.sendTo( sip_server, ans, strlen( ans ) );
-                if( send == strlen( ans ) )
-                {
-                    if( debug_reconnect ) send_msg("Reconnect SIP -- RCV BYE from *");
-                    __reconnect__();
-                    miss_sip_rcv_bye_send_pkg++;
-                }
-                
-                if( debug_invite ) debug_msg("sizeof( bye reply pkg ) :: %d\nServer request BYE to %d ext\nBye request received from *", strlen( ans ), my_ext );
-                
-                set_sip_rtp_port( 0 );
+                int bye_sent = sock.sendTo( sip_server, ans, strlen( ans ) );
+                if( debug_invite ) debug_msg("sizeof( bye reply pkg ) :: %d", strlen( ans ) );
+                if( debug_invite ) debug_msg("Server request BYE to %d ext", my_ext );
+                if( debug_invite ) debug_msg("Bye request received from *");
                 
                 listen_SIP_server_return = my_ext;
             }
         }
     }
+    
     return length;
 }
 
@@ -1208,7 +1209,46 @@
 
 int Sip::get_sip_rtp_port( void ){ return my_rtp_port; }
 
-void Sip::set_sip_rtp_port ( int new_rtp_port )
+void Sip::set_sip_rtp_port ( int new_my_rtp_port )
+{ 
+    this->my_rtp_port = new_my_rtp_port; 
+    debug_msg( "this->my_rtp_port ( %d ) = new_my_rtp_port( %d )", this->my_rtp_port, new_my_rtp_port );
+}
+
+int Sip::print_yourself ( void )
 {
-    this->my_rtp_port = new_rtp_port;
+    send_msg ("");
+    send_msg ("Values ::\r\n");
+    send_msg("id :: %d", id );
+    send_msg("server_ip :: %s", server_ip );
+    send_msg("server_port :: %d", server_port );
+    send_msg("my_ip :: %s", my_ip );
+    send_msg("my_port :: %d", my_port );
+    send_msg("my_ext :: %d", my_ext );
+    send_msg("my_rtp_port :: %d", my_rtp_port );
+    send_msg("my_display :: %s", my_display );
+    send_msg("peer_ext :: %d", peer_ext );
+    send_msg("fill_random_aux :: %s", fill_random_aux );
+    send_msg("last_invite_tag :: %s", last_invite_tag );
+    send_msg("last_invite_callid :: %s", last_invite_callid );
+    send_msg("SVNREV :: %s", SVNREV );
+    // o.O
+    send_msg("char buffer[ 1024 ] :: %p", ( void * ) buffer );
+    
+    //FIXME esse tipo de tag existe como metalinguagem ???
+    //TOTHINK o que fazer pra entender melhor o estado dos sockets
+    send_msg("UDPSocket sock :: %p", ( void * ) &sock );
+    send_msg("Endpoint sip_server :: %p", ( void * ) &sip_server );
+    
+    send_msg("muted :: %s", ( muted ) ? "true" : "false" );
+    send_msg("invite_pkg_sent :: %d", invite_pkg_sent );
+    send_msg("call :: %p", ( void * ) call );
+    send_msg("invite_timer :: %d", ( int ) invite_timer.read () );
+    send_msg("ok_sent :: %d", ok_sent );
+    send_msg("waiting ::  %s", ( waiting ) ? "true" : "false" );
+    send_msg("length_muted :: %d",length_muted );
+    send_msg("listen_SIP_server_return :: %d", listen_SIP_server_return );
+    send_msg ("");
+
+    return( sizeof( Sip ) );    
 }
\ No newline at end of file