![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: sip.cpp
- Revision:
- 78:1353744f01e1
- Parent:
- 77:e8c0253b57bc
- Child:
- 81:3656f00ab3db
--- a/sip.cpp Tue Nov 25 18:43:01 2014 +0000 +++ b/sip.cpp Tue Dec 16 12:13:01 2014 +0000 @@ -148,23 +148,18 @@ __end_sock__(); } -void Sip::registry(){ +int Sip::registry(){ 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 ) ); - /* - if( sent == -1 || rcv == -1 ){ - debug_msg(""); - __init_eth__(); - debug_msg(""); - } - */ + if( sent == -1 || rcv == -1 ){ __reconnect__(); } - if( debug_sip ) debug_msg("Received %i bytes to ext %d", sent, my_ext ); + + 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){ @@ -174,128 +169,111 @@ } } } + return( sent ); } VZ_call * Sip::invite(){ - Timer t; + Timer invite_timer; char callbox_string[ 32 ]; itoa( get_id(), callbox_string, 10 ); if( status != sip_on_call ){ - set_status( status, sip_waiting_trying ); - }else return( NULL ); - - int cseq = 0; - - build_invite_package( buffer, callbox_string, &cseq ); - sock.sendTo( sip_server, buffer, sizeof( buffer ) ); + if( status == sip_idle ){ + int cseq = 0; - if( debug_sip ) debug_msg( "Inviting" ); - if( debug_sip ) debug_msg( "Sip::status::%d", status ); - t.start(); - - sock.set_blocking( false, 5 ); + build_invite_package( buffer, callbox_string, &cseq ); + sock.sendTo( sip_server, buffer, sizeof( buffer ) ); + + if( debug_sip ) debug_msg( "Inviting" ); + if( debug_sip ) debug_msg( "Sip::status::%d", status ); + invite_timer.start(); + + sock.set_blocking( false, 3 ); + set_status( status, sip_waiting_trying ); + } + }else{ + debug_msg("Nao deveria estar sendo chamado aqui!!!!!"); + return( NULL ); + } - int loop_times = 0; - int received_loop_times = 0; - Timer reconnect; - reconnect.start(); - //static int id_ = 0; - int length = 0; - bool waiting = false; + uint32_t loop_times = 0; + uint32_t received_loop_times = 0; + Timer check_point_timer; + check_point_timer.start(); + uint16_t length = 0; + static bool waiting = false; VZ_call * call = NULL; - while( t.read() < __INVITE_MAX_WAITING_TIME__ ){ - loop_times++; - length = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); - if( length > 0 ){ - reconnect.reset(); - //debug_msg("Pacotes recebidos :: %d", id_ ); - buffer[ length ] = 0; - received_loop_times++; - 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 ); - set_status(status, sip_on_call); - char *ref = strstr( buffer, "audio" ); - if( debug_sip ) 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 ) ); - char ans[ 1024 ]; - build_ack_package( ans, ( unsigned char *)buffer ); - sock.sendTo( sip_server, ans, sizeof( ans ) ); - 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 ); - } + + if( invite_timer.read() > __INVITE_MAX_WAITING_TIME__ ){ + if( debug_invite ) debug_msg("Timeout do pedido"); + invite_timer.stop(); + invite_timer.reset(); + set_status( status, sip_denied ); // timeout invite package + send_bye(); + return( NULL ); + } + loop_times++; + length = sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); + if( length > 0 ){ + 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 ) 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 ) ); + 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 ); } } - if( ( status == sip_trying ) || ( status == sip_ringing ) ){ - if( !( strncasecmp( buffer + 12, "Busy Here", strlen("Busy Here") ) ) ){ - if( debug_sip ) debug_msg("Busy Here"); - send_bye(); - delete( call ); - set_status( status, sip_busy ); - return( NULL ); - } - } - 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" ); - char *ref = strstr( buffer, "audio" ); - 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 + 12, "ringing", strlen("ringing") ) ) ){ - if( debug_sip ) debug_msg("ringing"); - t.reset(); - set_status( status, sip_ringing ); + } + if( ( status == sip_trying ) || ( status == sip_ringing ) ){ + if( !( strncasecmp( buffer + 12, "Busy Here", strlen("Busy Here") ) ) ){ + if( debug_sip ) debug_msg("Busy Here"); + send_bye(); + 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 ) call = new VZ_call( this->my_ext, this->my_rtp_port, this->peer_ext, atoi( ref ) ); + waiting = true; } } - if( status == sip_waiting_trying ){ - if( !( strncasecmp( buffer + 12, "trying", strlen("trying") ) ) ){ - if( debug_sip ) debug_msg("trying") - t.reset(); - set_status( status, sip_trying ); - } - } - }else if( reconnect.read() > 4 ){ - //sock.close(); - //sock.bind( this->my_port ); - //sock.init(); - //sock.set_blocking( false, 1 ); - //if( debug_sip ) debug_msg("Reconnect"); - if( received_loop_times != 0 ){ - if( debug_sip ) send_msg(" %d bytes lidos ", length ); - reconnect.reset(); - } - //sock.sendTo( sip_server, buffer, sizeof( buffer ) ); - //led4 = !led4; } - 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 ); - send2callboxes( pkg2cb ); + if( status == sip_trying ){ + if( !( strncasecmp( buffer + 12, "ringing", strlen("ringing") ) ) ){ + if( debug_sip ) 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_sip ) 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 ); } - 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(); - } - delete( call ); return( NULL ); } @@ -320,7 +298,6 @@ strcat( buffer, ";branch=" ); strcat( buffer, branch ); strcat( buffer, "\r\nFrom: " ); - // mudar isso aqui pra algo mais compreensivel pra cc itoa( my_ext, itoa_buffer, 10 ); strcat( buffer, itoa_buffer ); strcat( buffer, " <sip:" ); @@ -364,7 +341,7 @@ strcat( buffer, ":" ); itoa( server_port, itoa_buffer, 10 ); strcat( buffer, itoa_buffer ); - strcat( buffer, ">;expires=120" ); + strcat( buffer, ">;expires=600" ); //itoa( 1200, itoa_buffer, 10 ); //strcat( buffer, itoa_buffer ); strcat( buffer, "\r\n" ); @@ -1105,4 +1082,12 @@ strcat( buffer, "\r\n\r\n" ); buffer[ SIP_MAXMSGSIZE - 1 ] = 0; return buffer; +} + +int Sip::get_status( void ){ + return this->status; +} + +void Sip::sip_set_status( uint8_t status ){ + this->status = status; } \ No newline at end of file