Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
29:7246460b73f8
Parent:
28:94aec56c6329
Child:
33:735fd60e96d8
--- a/sip.cpp	Tue Sep 23 18:28:37 2014 +0000
+++ b/sip.cpp	Wed Sep 24 18:02:18 2014 +0000
@@ -34,35 +34,24 @@
     
     __init_sock__();
 }
- 
-Sip::Sip(){
-    FILE *fp = NULL;
+  
+Sip::Sip( int id, uint16_t my_port ){
+    FILE * fp = NULL;
+    char inner_tmp[ 20 ];
     
-    fp = fopen( "/qspi/myext.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/myext.txt" );
-        this->id = __MY_EXT__;
-    }else{
-        if( fread( &id, 1, 512, fp ) > 0 ){
-            this->id = __MY_EXT__;
-            debug_msg("Objeto SIP fixado na ext %i", this->id );
-        }else{
-            debug_msg("Failed to read /qspi/myext.txt" );
-            this->id = __MY_EXT__;
-        }
-    }
-    fclose( fp );
+    if( debug_sip ) debug_msg("Building SIP ext :: %d | Port :: %d", id, my_port );
     
+    this->id = id;
+        
     fp = fopen( "/qspi/serverip.txt", "r");
     if( fp == NULL){
-        debug_msg("Failed to open /qspi/serverip.txt" );
+        if( debug_sip ) debug_msg("Failed to open /qspi/serverip.txt" );
         strncpy( this->server_ip, __SERVER_IP__, 20 );    
     }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->server_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado com ip %s", this->server_ip );
+        if( fread( this->server_ip, 1, 20, fp ) > 0 ){
+            if( debug_sip ) debug_msg("Objeto SIP fixado no server ip %s", this->server_ip );
         }else{
-            debug_msg("Failed to read /qspi/serverip.txt" );
+            if( debug_sip ) debug_msg("Failed to read /qspi/serverip.txt" );
             strncpy( this->server_ip, __SERVER_IP__, 20 );
         }
     }
@@ -71,119 +60,14 @@
     
     fp = fopen( "/qspi/serverport.txt", "r");
     if( fp == NULL ){
-        debug_msg("Failed to open /qspi/serverport.txt" );
+        if( debug_sip ) debug_msg("Failed to open /qspi/serverport.txt" );
         this->server_port = __SERVER_PORT__;
     }else{
-        if( fread( &server_port, 1, 512, fp ) > 0 ){
-            this->server_port = __SERVER_PORT__;
-            debug_msg("Objeto SIP fixado na server port %i", this->server_port );
-        }else{
-            debug_msg("Failed to read /qspi/serverport.txt" );
-            this->server_port = __SERVER_PORT__;
-        }
-    }
-    fclose( fp );
-    
-    fp = fopen( "/qspi/myip.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/myip.txt" );
-        strncpy( this->my_ip, __MY_IP__, 20 );    
-    }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->my_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado no ip %s", this->my_ip );
-        }else{
-            debug_msg("Failed to read /qspi/myip.txt" );
-            strncpy( this->my_ip, __MY_IP__, 20 );
-        }
-    }
-    fclose( fp );
-    this->my_ip[19] = 0;
-    
-    fp = fopen( "/qspi/mysipport.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/mysipport.txt" );
-        this->my_port = __MY_PORT__;
-    }else{
-        if( fread( &my_port, 1, 32, fp ) > 0 ){
-            debug_msg("Objeto SIP fixado na porta sip %i", this->my_port );
-        }else{
-            debug_msg("Failed to read /qspi/mysipport.txt" );
-            this->my_port = __MY_PORT__;
-        }
-    }
-    fclose( fp );
-    
-    this->my_rtp_port = fill_random_rtp_port();
-    
-    fp = fopen( "/qspi/myext.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/myext.txt" );
-        this->my_ext = __MY_EXT__;
-    }else{
-        if( fread( &my_ext, 1, 32, fp ) > 0 ){
-            debug_msg("Objeto SIP fixado ext %i", this->my_ext );
+        if( fread( (void *)inner_tmp, 1, 20, fp ) > 0 ){
+            server_port = atoi( inner_tmp );
+            if( debug_sip ) debug_msg("Objeto SIP fixado no server port %d", this->server_port );
         }else{
-            debug_msg("Failed to read /qspi/myext.txt" );
-            this->my_ext = __MY_EXT__;
-        }
-    }
-    fclose( fp );
-    
-    itoa( this->my_ext, this->my_display, 10 );
-    
-    fp = fopen( "/qspi/peerext.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/peerext.txt" );
-        this->peer_ext = __PEER_EXT__;
-    }else{
-        if( fread( &peer_ext, 1, 32, fp ) > 0 ){
-            debug_msg("Objeto SIP fixado no server ext %i", this->peer_ext );
-        }else{
-            debug_msg("Failed to read /qspi/peerext.txt" );
-            this->peer_ext = __PEER_EXT__;
-        }
-    }
-    fclose( fp );
-    
-    strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
-    strcpy( this->last_invite_tag, "" );
-    strcpy( this->last_invite_callid, "");
-    strcpy( this->SVNREV, "COLA" );
-    status = sip_idle;
-    
-    __init_sock__();
-}
- 
-Sip::Sip( int id, uint16_t my_port ){
-    FILE * fp = NULL;
-    this->id = id;
-    fp = fopen( "/qspi/serverip.txt", "r");
-    if( fp == NULL){
-        debug_msg("Failed to open /qspi/serverip.txt" );
-        strncpy( this->server_ip, __SERVER_IP__, 20 );    
-    }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->server_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado com ip %s", this->server_ip );
-        }else{
-            debug_msg("Failed to read /qspi/serverip.txt" );
-            strncpy( this->server_ip, __SERVER_IP__, 20 );
-        }
-    }
-    fclose( fp );
-    this->server_ip[19] = 0;
-    
-    fp = fopen( "/qspi/serverport.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/serverport.txt" );
-        this->server_port = __SERVER_PORT__;
-    }else{
-        if( fread( &server_port, 1, 512, fp ) > 0 ){
-            this->server_port = __SERVER_PORT__;
-            debug_msg("Objeto SIP fixado na server port %i", this->server_port );
-        }else{
-            debug_msg("Failed to read /qspi/serverport.txt" );
+            if( debug_sip ) debug_msg("Failed to read /qspi/serverport.txt" );
             this->server_port = __SERVER_PORT__;
         }
     }
@@ -191,14 +75,13 @@
     
     fp = fopen( "/qspi/myip.txt", "r");
     if (fp == NULL){
-        debug_msg("Failed to open /qspi/myip.txt" );
+        if( debug_sip ) debug_msg("Failed to open /qspi/myip.txt" );
         strncpy( this->my_ip, __MY_IP__, 20 );    
     }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->my_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado no ip %s", this->my_ip );
+        if( fread( this->my_ip, 1, 20, fp ) > 0 ){
+            if( debug_sip ) debug_msg("Objeto SIP fixado no ip %s", this->my_ip );
         }else{
-            debug_msg("Failed to read /qspi/myip.txt" );
+            if( debug_sip ) debug_msg("Failed to read /qspi/myip.txt" );
             strncpy( this->my_ip, __MY_IP__, 20 );
         }
     }
@@ -209,20 +92,27 @@
     this->my_rtp_port = fill_random_rtp_port();
     this->my_ext = id;
     itoa( this->my_ext, this->my_display, 10 );
+    
     fp = fopen( "/qspi/peerext.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/peerext.txt" );
+    if( fp == NULL ) {
+        if( debug_sip ) debug_msg("Failed to open /qspi/peerext.txt" );
         this->peer_ext = __PEER_EXT__;
-    }else{
-        if( fread( &peer_ext, 1, 32, fp ) > 0 ){
-            debug_msg("Objeto SIP fixado no server ext %i", this->peer_ext );
-        }else{
-            debug_msg("Failed to read /qspi/peerext.txt" );
+    } else {
+        if( fread( (void *)inner_tmp, 1, 32, fp ) > 0 ) {
+            this->peer_ext = atoi( inner_tmp );
+            if( debug_sip ) debug_msg("Objeto SIP fixado no server ext %d", this->peer_ext );
+        } else {
+            if( debug_sip ) debug_msg("Failed to read /qspi/peerext.txt" );
             this->peer_ext = __PEER_EXT__;
         }
     }
     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, "");
@@ -231,73 +121,7 @@
     
     __init_sock__();
 }
- 
-Sip::Sip( int id, uint16_t my_port, int dest_ext  ){
-    FILE * fp = NULL;
-    this->id = id;
-    
-    fp = fopen( "/qspi/serverip.txt", "r");
-    if( fp == NULL){
-        debug_msg("Failed to open /qspi/serverip.txt" );
-        strncpy( this->server_ip, __SERVER_IP__, 20 );    
-    }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->server_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado com ip %s", this->server_ip );
-        }else{
-            debug_msg("Failed to read /qspi/serverip.txt" );
-            strncpy( this->server_ip, __SERVER_IP__, 20 );
-        }
-    }
-    fclose( fp );
-    this->server_ip[19] = 0;
-    
-    fp = fopen( "/qspi/serverport.txt", "r");
-    if( fp == NULL ){
-        debug_msg("Failed to open /qspi/serverport.txt" );
-        this->server_port = __SERVER_PORT__;
-    }else{
-        if( fread( &server_port, 1, 512, fp ) > 0 ){
-            this->server_port = __SERVER_PORT__;
-            debug_msg("Objeto SIP fixado na server port %i", this->server_port );
-        }else{
-            debug_msg("Failed to read /qspi/serverport.txt" );
-            this->server_port = __SERVER_PORT__;
-        }
-    }
-    fclose( fp );
-    
-    fp = fopen( "/qspi/myip.txt", "r");
-    if (fp == NULL){
-        debug_msg("Failed to open /qspi/myip.txt" );
-        strncpy( this->my_ip, __MY_IP__, 20 );    
-    }else{
-        if( fread( this->buffer, 1, 512, fp ) > 0 ){
-            strncpy( this->my_ip, this->buffer, 20 );
-            debug_msg("Objeto SIP fixado no ip %s", this->my_ip );
-        }else{
-            debug_msg("Failed to read /qspi/myip.txt" );
-            strncpy( this->my_ip, __MY_IP__, 20 );
-        }
-    }
-    fclose( fp );
-    this->my_ip[19] = 0;
-    
-    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 );
-    this->peer_ext = dest_ext;
- 
-    strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" );
-    strcpy( this->last_invite_tag, "" );
-    strcpy( this->last_invite_callid, "");
-    strcpy( this->SVNREV, "COLA" );
-    status = sip_idle;
-    
-    __init_sock__();
-}
- 
+
 Sip::~Sip(){
     __end_sock__();
 }
@@ -306,7 +130,7 @@
     char out[ 1000 ];
     if( debug_sip ) debug_msg("Registry %d", this->my_ext );
     build_registry_package( buffer );
-    int sent = sock.sendTo( sip_server, buffer, sizeof( buffer ) );
+    int sent = sock.sendTo( sip_server, buffer, 512 ); // > 400
     sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
     if( debug_sip ) debug_msg("Received %i bytes to ext %d", sent, my_ext );
     if( decode_gettag( (unsigned char *)buffer, "cseq: ", out ) ){
@@ -318,7 +142,6 @@
             }
         }
     }
-    debug_msg("");
 }
 
 VZ_call * Sip::invite(){
@@ -337,8 +160,8 @@
     build_invite_package( buffer, callbox_string, &cseq );
     sock.sendTo( sip_server, buffer, sizeof( buffer ) );
  
-    debug_msg( "Inicio do invite" );
-    debug_msg( "Sip::status::%d", status );
+    if( debug_sip ) debug_msg( "Inviting" );
+    if( debug_sip ) debug_msg( "Sip::status::%d", status );
     t.start();
 
     sock.set_blocking( false, 5 );
@@ -347,7 +170,7 @@
     int received_loop_times = 0;
     Timer reconnect;
     reconnect.start();
-    static int id_ = 0;
+    //static int id_ = 0;
     int length = 0;
     bool waiting = false;
     
@@ -357,16 +180,16 @@
         length = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) );
         if( length > 0 ){
             reconnect.reset();
-            debug_msg("Pacotes recebidos :: %d", id_ );
+            //debug_msg("Pacotes recebidos :: %d", id_ );
             buffer[ length ] = 0;
             received_loop_times++;
             if( ( status == sip_trying ) || ( status == sip_ringing ) ){            
                 if( !( strncasecmp( buffer + 12, "ok", strlen("ok") ) ) ){
                     // tratar depois o esquema do Cseq ::
-                    debug_msg( "Cseq == %d", cseq );
+                    //debug_msg( "Cseq == %d", cseq );
                     set_status(status, sip_on_call);
                     char *ref = strstr( buffer, "audio" );
-                    debug_msg("ok");
+                    if( debug_sip ) debug_msg("ok");
                     if( ref != NULL ){
                         ref += 6; // audio 
                         ref = strtok( ref, " ");
@@ -374,7 +197,7 @@
                         char ans[ 1024 ];
                         build_ack_package( ans, ( unsigned char *)buffer );
                         sock.sendTo( sip_server, ans, sizeof( ans ) );
-                        debug_msg("Call alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
+                        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 );
                     }
@@ -382,7 +205,7 @@
             }
             if( ( status == sip_trying ) || ( status == sip_ringing ) ){            
                 if( !( strncasecmp( buffer + 12, "Busy Here", strlen("Busy Here") ) ) ){
-                    debug_msg("Busy Here");
+                    if( debug_sip ) debug_msg("Busy Here");
                     send_bye();
                     delete( call );
                     set_status( status, sip_busy );
@@ -391,7 +214,7 @@
             }
             if( ( status == sip_trying ) || ( status == sip_ringing ) ){            
                 if( !( strncasecmp( buffer + 12, "Session Progress", strlen("Session Progress") ) ) ){
-                    debug_msg( "Trocando dados de audio -- musica de espera com o CBx" );
+                    //debug_msg( "Trocando dados de audio -- musica de espera com o CBx" );
                     char *ref = strstr( buffer, "audio" );
                     if( ref != NULL ){
                         ref += 6; // audio 
@@ -403,14 +226,14 @@
             }
             if( status == sip_trying ){
                 if( !( strncasecmp( buffer + 12, "ringing", strlen("ringing") ) ) ){
-                    debug_msg("ringing");
+                    if( debug_sip ) debug_msg("ringing");
                     t.reset();
                     set_status( status, sip_ringing );
                 }
             }
             if( status == sip_waiting_trying ){
                 if( !( strncasecmp( buffer + 12, "trying", strlen("trying") ) ) ){
-                    debug_msg("trying")
+                    if( debug_sip ) debug_msg("trying")
                     t.reset();
                     set_status( status, sip_trying );
                 }
@@ -420,7 +243,7 @@
             sock.bind( this->my_port );
             sock.init();
             sock.set_blocking( false, 1 );
-            debug_msg("Reconnect");
+            if( debug_sip ) debug_msg("Reconnect");
             reconnect.reset();
             //sock.sendTo( sip_server, buffer, sizeof( buffer ) );
             led4 = !led4;
@@ -435,7 +258,7 @@
         }
     }
     
-    debug_msg("Call nao alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
+    if( debug_sip ) debug_msg("Call nao alocada -- Sip::status::%d -- loop_times :: %d :: received_loop_times :: %d", status, loop_times, received_loop_times );
     if( t.read() > __INVITE_MAX_WAITING_TIME__ ){
         set_status(status, sip_denied);
         send_bye();
@@ -1137,11 +960,11 @@
             char ans[ 2000 ];
             build_reply_package( ans, (unsigned char*)buffer );
             sock.sendTo( sip_server, ans, sizeof( ans ) );  
-            debug_msg(" Server request BYE to %d ext", my_ext );
+            if( debug_sip ) debug_msg(" Server request BYE to %d ext", my_ext );
             return( this->my_ext );
         }else if( !( strncasecmp( buffer, "options", strlen("options") ) ) ){
             char ans[ 2000 ];
-            debug_msg(" Options received ");
+            if( debug_sip ) debug_msg(" Options received ");
             build_reply_package( ans, (unsigned char*)buffer );
             sock.sendTo( sip_server, ans, sizeof( ans ) );
             //debug_msg("");