![](/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:
- 28:94aec56c6329
- Parent:
- 27:98f824719d1c
- Child:
- 29:7246460b73f8
--- a/sip.cpp Mon Sep 22 21:15:07 2014 +0000 +++ b/sip.cpp Tue Sep 23 18:28:37 2014 +0000 @@ -1,5 +1,6 @@ #include "sip.h" #include "utils.h" +#include "prompt.h" void Sip::__init_sock__( void ){ sip_server.set_address( this->server_ip , this->server_port ); @@ -17,22 +18,7 @@ strncpy( this->server_ip, server_ip, 20 ); this->server_ip[19] = 0; this->server_port = server_port; - - FILE *fp = fopen( "/qspi/myip.txt", "r"); - if (fp == NULL){ - pc.printf("Failed to open /qspi/myip.txt \n\r" ); - 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{ - pc.printf("Failed to read /qspi/myip.txt \n\r" ); - strncpy( this->my_ip, __MY_IP__, 20 ); - } - } - fclose( fp ); - + strncpy( this->my_ip, my_ip, 20 ); this->my_ip[19] = 0; this->my_port = my_port; this->my_rtp_port = fill_random_rtp_port(); @@ -50,66 +36,116 @@ } Sip::Sip(){ - FILE *fp = NULL;/* + FILE *fp = NULL; + + 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 ); - mudar para mudar a ext aqui - fopen( "/qspi/myip.txt", "r"); - if (fp == NULL){ - pc.printf("Failed to open /qspi/myip.txt \n\r" ); + 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{ - pc.printf("Failed to read /qspi/myip.txt \n\r" ); - strncpy( this->my_ip, __MY_IP__, 20 ); - } - } - fclose( fp );*/ - - - this->id = __MY_EXT__; - strncpy( this->server_ip, __SEVER_IP__, 20 ); - this->server_ip[19] = 0; - this->server_port = __SERVER_PORT__; - - fp = fopen( "/qspi/myip.txt", "r"); - if (fp == NULL){ - pc.printf("Failed to open /qspi/myip.txt \n\r" ); - 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{ - pc.printf("Failed to read /qspi/myip.txt \n\r" ); + 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){ - pc.printf("Failed to open /qspi/mysipport.txt \n\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{ - pc.printf("Failed to read /qspi/mysipport.txt \n\r" ); + debug_msg("Failed to read /qspi/mysipport.txt" ); this->my_port = __MY_PORT__; } } fclose( fp ); this->my_rtp_port = fill_random_rtp_port(); - this->my_ext = __MY_EXT__; + + 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 ); + }else{ + debug_msg("Failed to read /qspi/myext.txt" ); + this->my_ext = __MY_EXT__; + } + } + fclose( fp ); + itoa( this->my_ext, this->my_display, 10 ); - this->peer_ext = __PEER_EXT__; - + + 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, ""); @@ -120,33 +156,73 @@ } Sip::Sip( int id, uint16_t my_port ){ + FILE * fp = NULL; this->id = id; - strncpy( this->server_ip, __SEVER_IP__, 20 ); + 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; - this->server_port = __SERVER_PORT__; - FILE *fp = fopen( "/qspi/myip.txt", "r"); + 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){ - pc.printf("Failed to open /qspi/myip.txt \n\r" ); + 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{ - pc.printf("Failed to read /qspi/myip.txt \n\r" ); + 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 ); + 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 ); - 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 = __PEER_EXT__; - strcpy( this->fill_random_aux, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789pP" ); strcpy( this->last_invite_tag, "" ); strcpy( this->last_invite_callid, ""); @@ -157,12 +233,56 @@ } Sip::Sip( int id, uint16_t my_port, int dest_ext ){ + FILE * fp = NULL; this->id = id; - strncpy( this->server_ip, __SEVER_IP__, 20 ); + + 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; - this->server_port = __SERVER_PORT__; - strncpy( this->my_ip, "192.168.120.172", 20 ); + + 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; @@ -184,11 +304,11 @@ void Sip::registry(){ char out[ 1000 ]; - debug_msg("registrando -- %d", this->my_ext ); + if( debug_sip ) debug_msg("Registry %d", this->my_ext ); build_registry_package( buffer ); int sent = sock.sendTo( sip_server, buffer, sizeof( buffer ) ); sock.receiveFrom( sip_server, buffer, sizeof( buffer ) ); - debug_msg(" Recebidos %d bytes para o ext %d", sent, my_ext ); + if( debug_sip ) debug_msg("Received %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){