Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: sip.cpp
- Revision:
- 77:e8c0253b57bc
- Parent:
- 76:9f5f99dd895a
- Child:
- 78:1353744f01e1
--- a/sip.cpp Tue Nov 25 17:37:56 2014 +0000 +++ b/sip.cpp Tue Nov 25 18:43:01 2014 +0000 @@ -182,20 +182,19 @@ itoa( get_id(), callbox_string, 10 ); if( status != sip_on_call ){ - if( status == sip_idle ){ - int cseq = 0; + 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( debug_sip ) debug_msg( "Inviting" ); - if( debug_sip ) debug_msg( "Sip::status::%d", status ); - t.start(); - - sock.set_blocking( false, 3 ); - set_status( status, sip_waiting_trying ); - } - }else return( NULL ); + 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 ); + t.start(); + + sock.set_blocking( false, 5 ); int loop_times = 0; int received_loop_times = 0; @@ -205,92 +204,98 @@ int length = 0; bool waiting = false; VZ_call * call = NULL; - //while( t.read() < __INVITE_MAX_WAITING_TIME__ ){ - if( 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 ); + 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( ( 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_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 ) || ( 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") ) ) ){ - //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_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 ) && ( received_loop_times != 0 ) ){ - send_msg(" %d bytes lidos ", length ); - reconnect.reset(); } - if( waiting == true ){ - return( call ); - - /*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( 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(); } - //} - // - //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 ); + delete( call ); return( NULL ); } @@ -1100,12 +1105,4 @@ 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