voltando a versao de n aberturas e fechamentos de sockets

Dependencies:   EthernetInterface NTPClient mbed-rtos_old mbed

Fork of header_main_public by VZTECH

Revision:
3:cd9148672e25
Parent:
1:a1758104fa1d
Child:
4:de46f0d9b14d
--- a/sip.cpp	Wed Sep 10 14:32:19 2014 +0000
+++ b/sip.cpp	Wed Sep 10 18:21:48 2014 +0000
@@ -197,7 +197,9 @@
     reconnect.start();
     static int id_ = 0;
     int length = 0;
-    bool print = false;
+    bool waiting = false;
+    
+    VZ_call * call = NULL;
     while( t.read() < __INVITE_MAX_WAITING_TIME__ ){    
         loop_times++;        
         length = sock.receiveFrom( sip_server, buffer_eth_invite, sizeof( buffer_eth_invite ) );
@@ -217,7 +219,7 @@
                     if( ref != NULL ){
                         ref += 6; // audio 
                         ref = strtok( ref, " ");
-                        VZ_call * call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) );
+                        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_eth_invite );
                         sock.sendTo( sip_server, ans, sizeof( ans ) );
@@ -227,6 +229,18 @@
                     }
                 }
             }
+            if( ( status == sip_trying ) || ( status == sip_ringing ) ){            
+                if( !( strncasecmp( buffer_eth_invite + 12, "Session Progress", strlen("Session Progress") ) ) ){
+                    char *ref = strstr( buffer_eth_invite, "audio" );
+                    debug_msg( "Trocando dados de audio -- musica de espera com o CBx" );
+                    if( ref != NULL ){
+                        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 ) );
+                        waiting = true;
+                    }
+                }
+            }
             if( status == sip_trying ){
                 if( !( strncasecmp( buffer_eth_invite + 12, "ringing", strlen("ringing") ) ) ){
                     debug_msg("ringing");
@@ -250,15 +264,22 @@
             reconnect.reset();
             //sock.sendTo( sip_server, buffer, sizeof( buffer ) );
             led4 = !led4;
-            print = true;
         }
-        if( print ) debug_msg( "Lidos : %d --", length );
+        if( waiting == true ){
+            char * tmp = call->get_eth_message( &length );
+            if( tmp != NULL ){
+                uint8_t * pkg2cb = __build_cb_package__( this->my_ext, this->my_port, __AUDIO__, 
+                    tmp, __AUDIO__, length, (uint8_t *)buffer );
+                __send_to_cb__( pkg2cb );
+            }
+        }
     }
     
     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);
     }
+    delete( call );
     return( NULL );
 }