Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: sip.cpp
- Revision:
- 91:c2a86b1f8aaa
- Parent:
- 89:0fe315117b00
- Child:
- 92:92df17f538a8
diff -r 8e538571f427 -r c2a86b1f8aaa sip.cpp --- a/sip.cpp Wed Jan 07 21:50:46 2015 +0000 +++ b/sip.cpp Thu Jan 08 23:44:08 2015 +0000 @@ -39,6 +39,16 @@ __init_sock__(); muted = false; + + this->invite_pkg_sent = 0; + + call = NULL; + + ok_sent = 0; + waiting = false; + length_muted = 0; + listen_SIP_server_return = 0; + sip_new_counter++; } @@ -140,6 +150,14 @@ __init_sock__(); muted = false; + invite_pkg_sent = 0; + call = NULL; + + ok_sent = 0; + waiting = false; + length_muted = 0; + listen_SIP_server_return = 0; + sip_new_counter++; } @@ -149,35 +167,14 @@ } int Sip::registry(){ - //char out[ 1000 ]; if( debug_sip ) debug_msg("Registry %d", this->my_ext ); build_registry_package( buffer ); - int sent = 0; - - //do{ - //sent = sock.sendTo( sip_server, buffer, 512 ); // > 400 - sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400 - if( debug_sip ) send_msg("sizeof( registry pkg ) :: %d", strlen( buffer ) ); - //}while( sent == 0 ); - - // int rcv = 0; - - //rcv = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); + int sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); // > 400 + + if( debug_sip ) debug_msg("Sent %i of %i bytes to ext %d", sent, strlen( buffer ), my_ext ); - //if( rcv > 0 ) eth_pkg_received++; - -/* - if( sent == -1 || rcv == -1 ){ - //if( debug_sip ) debug_msg("sent -- %d, rcv -- %d", sent, rcv ); - if( debug_sip ) debug_msg("sent -- %d", sent ); - muted = true; - __reconnect__(); - }else{ - muted = false; - } - */ - if( debug_sip ) debug_msg("Sent %i bytes to ext %d", sent, my_ext ); + return( sent ); /* if( decode_gettag( (unsigned char *)buffer, "cseq: ", out ) ){ @@ -195,59 +192,44 @@ } } */ - - return( sent ); } VZ_call * Sip::invite(){ - Timer invite_timer; char callbox_string[ 32 ]; itoa( get_id(), callbox_string, 10 ); - static int invite_pkg_sent; - static VZ_call * call = NULL; + + if( status == sip_on_call ) return call; - if( status != sip_on_call ){ - if( status == sip_idle ){ - int cseq = 0; - - call = NULL; - - build_invite_package( buffer, callbox_string, &cseq ); - invite_pkg_sent = 0; - //do{ - //invite_pkg_sent = sock.sendTo( sip_server, buffer, sizeof( buffer ) ); - invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); - if( debug_invite ) send_msg("sizeof( invite_pkg ) :: %d", strlen( buffer ) ); - //}while( invite_pkg_sent == 0 ); - - if( debug_invite ) debug_msg("Return value for invite pkg %d", invite_pkg_sent ); - - if( debug_invite ) debug_msg( "Inviting\r\nSip::status::%d", status ); - - invite_timer.start(); + else if( status == sip_idle ){ + int cseq = 0; + + call = NULL; + + build_invite_package( buffer, callbox_string, &cseq ); + + this->invite_pkg_sent = 0; + + do{ + this->invite_pkg_sent = sock.sendTo( sip_server, buffer, strlen( buffer ) ); + }while( this->invite_pkg_sent == 0 ); - //sock.set_blocking( false, 3 ); - sock.set_blocking( false, 0 ); - - set_status( status, sip_waiting_trying ); - } - }else{ - debug_msg("Nao deveria estar sendo chamado aqui!!!!!"); - return( NULL ); + if( debug_invite ) debug_msg("Return value for invite pkg %d", invite_pkg_sent ); + + invite_timer.start(); + + set_status( status, sip_waiting_trying ); + + waiting = false; + + ok_sent = 0; + + length_muted = 0; } - uint32_t loop_times = 0; - uint32_t received_loop_times = 0; - Timer check_point_timer; - check_point_timer.start(); - int16_t length = 0; - static int ok_sent = 0; - static bool waiting = false; - static uint16_t length_muted = 0; - if( invite_timer.read() > __INVITE_MAX_WAITING_TIME__ ){ if( debug_invite == true ) send_msg("Invite call timeout :("); - if( ( invite_pkg_sent == -1 ) || length_muted || ok_sent == -1 ){ + + if( ( this->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; @@ -262,93 +244,15 @@ invite_timer.reset(); set_status( status, sip_denied ); send_bye(); + + //if( call != NULL ) delete( call ); + call = NULL; return( NULL ); } - loop_times++; - length = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); - if( length == -1 ){ length_muted++; } - if( length > 0 ){ - eth_pkg_received++; - - check_point_timer.reset(); - received_loop_times++; - buffer[ length ] = 0; - if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ - if( !( strncasecmp( buffer + 12, "ok", strlen("ok") ) ) ){ - // tratar depois o esquema do Cseq :: - //debug_msg( "Cseq == %d", cseq ); - char *ref = strstr( buffer, "audio" ); - if( debug_sip ) debug_msg("ok"); - if( ref != NULL ){ - ref += 6; // audio - ref = strtok( ref, " "); - if( call == NULL ){ - debug_msg(""); - call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); - if( call == NULL ){ - memory_is_over = true; - if( debug_memory ) debug_msg("Ok Call allocation fail"); - } - } - char ans[ 1024 ]; - build_ack_package( ans, ( unsigned char *)buffer ); - - //do{ - ok_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); - if( debug_invite ) send_msg("sizeog( ack_pkg ) :: %d", strlen( ans ) ); - //}while( ok_sent == 0 ); - - if( debug_invite ) 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 ); - } - } - } - if( ( status == sip_trying ) || ( status == sip_ringing ) ){ - if( !( strncasecmp( buffer + 12, "Busy Here", strlen("Busy Here") ) ) ){ - if( debug_invite ) debug_msg("Busy Here"); - send_bye(); - if( call != NULL ) delete( call ); - set_status( status, sip_busy ); - return( NULL ); - }else if( !( strncasecmp( buffer + 12, "Session Progress", strlen("Session Progress") ) ) ){ - char *ref = strstr( buffer, "audio" ); - if( ref != NULL ){ - ref += 6; // strlen( "audio" ) == 6 - ref = strtok( ref, " "); - if( call == NULL ){ - debug_msg(""); - call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); - if( call == NULL ){ - memory_is_over = true; - if( debug_memory ) debug_msg("Session progress allocation fail"); - } - } - waiting = true; - } - } - } - if( status == sip_trying ){ - if( !( strncasecmp( buffer + 12, "ringing", strlen("ringing") ) ) ){ - if( debug_invite ) debug_msg("ringing"); - invite_timer.reset(); - set_status( status, sip_ringing ); - } - } - if( status == sip_waiting_trying ){ - if( !( strncasecmp( buffer + 12, "trying", strlen("trying") ) ) ){ - if( debug_invite ) debug_msg("trying") - invite_timer.reset(); - set_status( status, sip_trying ); - } - } - }else if( ( check_point_timer.read() > 4 ) && ( received_loop_times != 0 ) ){ - if( debug_invite ) send_msg("entrei no loop %d vezes", received_loop_times ); - check_point_timer.reset(); - } - if( waiting == true ){ return( call ); } - return( NULL ); + if( waiting == true ){ return call; } + + else return( NULL ); } char * Sip::build_registry_package( char * buffer ){ @@ -415,7 +319,7 @@ strcat( buffer, ":" ); itoa( server_port, itoa_buffer, 10 ); strcat( buffer, itoa_buffer ); - strcat( buffer, ">;expires=600" ); + strcat( buffer, ">;expires=180" ); //itoa( 1200, itoa_buffer, 10 ); //strcat( buffer, itoa_buffer ); strcat( buffer, "\r\n" ); @@ -1034,52 +938,9 @@ > 0 :: devo remover essa call do vetor de calls */ int Sip::listen_SIP_server( void ){ - //sock.set_blocking( false, 3 ); - sock.set_blocking( false, 0 ); - - int bye_sent = 0; - int options_sent = 0; - - int read = sock.receiveFrom( sip_server, this->buffer, sizeof( this->buffer ) ); - - if( read > 0 ){ - eth_pkg_received++; - - if( !( strncasecmp( buffer, "bye", strlen("bye") ) ) ){ - char ans[ 2000 ]; - build_reply_package( ans, (unsigned char*)buffer ); - - //do{ - //bye_sent = sock.sendTo( sip_server, ans, 768 ); - bye_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); - if( debug_invite ) send_msg("sizeof( bye reply pkg ) :: %d", strlen( ans ) ); - //}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 ); - - //do{ - //options_sent = sock.sendTo( sip_server, ans, 512 ); - options_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); - if( debug_sip ) send_msg( "sizeof( options reply package ) :: %d", strlen( ans ) ); - //}while( options_sent == 0 ); - } - } - - if( read == -1 || bye_sent == -1 || options_sent == -1 ){ - muted = true; - }else{ - muted = false; - } - - return( 0 ); + int ret = listen_SIP_server_return; + listen_SIP_server_return = 0; + return ret; } void Sip::set_ext( int ext ){ @@ -1091,14 +952,10 @@ } void Sip::send_bye( void ){ - //build_bye_package( this->buffer ); - int ret = 0; - //do{ - //ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), sizeof( this->buffer ) ); - ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); - if( debug_sip ) send_msg( "sizeof( bye pkg ) :: %d", strlen( buffer ) ); - - //}while( ret == 0 ); + + 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( ret == -1 ){ muted = true; @@ -1109,13 +966,11 @@ void Sip::send_unregistry_pkg(){ build_unregistry_package( 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 ) ); - ret = sock.sendTo( sip_server, build_bye_package( this->buffer ), strlen( this->buffer ) ); - if( debug_sip ) send_msg("sizeof( unregistry_pkg ) :: %d", strlen( buffer ) ); - //}while( 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; @@ -1215,6 +1070,107 @@ return sock.get_fd(); } -int Sip::udp_drop_once( void ){ - return sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); +int Sip::udp_incomming_pkg( void ){ + Endpoint from; + + int length = sock.receiveFrom( from, buffer, sizeof( buffer ) ); + + if( memcmp( ( ( u8_t * )( &(from._remoteHost) ) ) + 2, ( ( u8_t * )( &(sip_server._remoteHost) ) ) + 2, 6 ) != 0 ) return 0; + + if( length == -1 ){ length_muted++; } + + if( length > 0 ){ + if( sizeof( buffer ) > length ) buffer[ length ] = 0; + + int sip_response = -1; + + // faster than atoi() (: + if( buffer[ 7 ] == ' ' || buffer[ 11 ] == ' ' ){ + sip_response = ( buffer[ 8 ] - '0' ) * 100 + ( buffer[ 9 ] - '0' ) * 10 + ( buffer[ 10 ] - '0' ); + } + + if( ( status == sip_trying ) || ( status == sip_ringing ) || ( status == sip_waiting_trying ) ){ + //if( !( strncasecmp( buffer + 7, " 200 ", 5 ) ) ){ + if( sip_response == 200 ){ // Ok + char *ref = strstr( buffer, "audio" ); + if( debug_invite ) debug_msg("ok"); + 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 ) ); + if( call == NULL ) memory_is_over = true; + } + char ans[ 1024 ]; + build_ack_package( ans, ( unsigned char *)buffer ); + + do{ + //ok_sent = sock.sendTo( sip_server, ans, sizeof( ans ) ); + ok_sent = sock.sendTo( sip_server, ans, strlen( ans ) ); + }while( ok_sent == 0 ); + + if( debug_invite ) debug_msg("Call alocada -- Sip::status::%d :: ", status ); + set_status( status, sip_on_call ); + } + } + } + if( ( status == sip_trying ) || ( status == sip_ringing ) ){ + //if( !( strncmp( buffer + 7, " 4", 2 ) ) ){ + if( sip_response >= 400 && sip_response < 700 ){ + if( debug_invite ) { + buffer[ 11 ] = 0; + debug_msg("Busy Here :: %s", buffer + 8 ); + } + send_bye(); + //if( call != NULL ) delete( call ); + set_status( status, sip_busy ); + return( NULL ); + }else //if( !( strncasecmp( buffer + 7, " 183 ", 5 ) ) ){ // Session in Progress + if( sip_response == 183 ){ + char *ref = strstr( buffer, "audio" ); + if( ref != NULL ){ + ref += 6; // strlen( "audio" ) == 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( call == NULL ) memory_is_over = true; + } + waiting = true; + } + } + } + if( status == sip_trying ){ + //if( !( strncasecmp( buffer + 7, " 180 ", 5 ) ) ){ + if( sip_response == 180 ){ + if( debug_invite ) debug_msg("ringing"); + set_status( status, sip_ringing ); + } + } + 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 ); + } + } + if( status == sip_on_call ){ + if( !( strncasecmp( buffer, "bye ", 4 ) ) ){ + char ans[ 2000 ]; + build_reply_package( ans, (unsigned char*)buffer ); + + 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; +} + +void Sip::reset_call( void ){ + if( call != NULL ) call = NULL; } \ No newline at end of file