![](/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
main.cpp
- Committer:
- klauss
- Date:
- 2015-04-07
- Revision:
- 109:a5b8264ffbbc
- Parent:
- 108:18a3702650f3
- Child:
- 111:c0833f2455ed
File content as of revision 109:a5b8264ffbbc:
#include "telemetry.h" #include "eth.h" #include "bits.h" #include "UART3Interrupt.h" // for RXBuffer[ __CB_BUFFER_SIZE__ ]; #include "utils.h" #include "parallelcpld.h" #include "debug.h" #include "flood.h" #include "bootloader_cbx.h" #include "call_manager.h" #include "call_box_manager.h" #include "sip_manager.h" #include "shared_variables.h" #include "prompt.h" #include "configs.h" #include "sdram.h" #include "fw.h" volatile u16_t lpc_low_level_input_counter = 0; int main() { init_wdt(); config_lpc(); start_cpld(); debug_uart3 = true; if( sdram_init() == 1 ) { send_msg("******* Failed to initialize SDRAM *******"); return 1; } else { send_msg("******* Success to initialize SDRAM *******"); } init_fsystem(); { char s[ 32 ]; mbed_mac_address( s ); send_msg( "::Mac::%02x:%02x:%02x:%02x:%02x:%02x:: ", s[0],s[1],s[2],s[3],s[4],s[5] ); } short int eth_status = __init_eth__(); if( eth_status ) { send_msg("******* Cannot connect to eth *******"); } else { send_msg("******* Connection eth - ok *******"); init_prompt_eth(); init_external_wdt() } Timer sync_timer, led_sync_timer; sync_timer.start(), led_sync_timer.start(); /* representa ramal do call box */ int ext = 0; /* representa porta do call box */ int port = 0; /* buffer para onde se copia os dados vindos do cb para tratameno interno */ uint8_t buffer[ __CB_BUFFER_SIZE__ ]; /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; /* ponteiro que aponta para os dados vindo do CPLD */ uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; /* Armazena o ultimo pacote recebido dos CBx */ uint8_t cb_rx_buffer[ __CB_BUFFER_SIZE__ ]; /* referencia para os dados contidos no pacote, sem o header */ uint8_t * data = NULL; /* gerencia o tipo do pacote para providenciar tratamento adequado */ volatile uint8_t type = __DO_NOTHING__; /* representa a lista dos Call Boxes atualmente recfonhecidos pela cabeceira */ Vector * v_cb = new Vector(); /* representa a lista de ligacoes ativas na cabeceira */ Vector * v_call = new Vector(); /* gerencia a distribuicao de timeslice para os call boxes */ Timeslice * ts = new Timeslice(); if( ts == NULL ) { memory_is_over = true; if( debug_memory ) debug_msg("TS allocation fail"); } /* instancia o vetor de call boxes // calls */ if( v_cb == NULL ) { while( v_cb == NULL ) { Vector * v_cb = new Vector(); if( sync_timer.read() > 5 ) { send_msg("Erro ao alocar o vetor de CBx"); sync_timer.reset(); } } } else if( v_call == NULL ) { while( v_call == NULL ) { Vector * v_call = new Vector(); if( sync_timer.read() > 5 ) { send_msg("Erro ao alocar o vetor de Calls"); sync_timer.reset(); } } } if( v_cb == NULL ) { memory_is_over = true; if( debug_memory ) debug_msg("Call_Box vector allocation fail"); } if( v_call == NULL ) { memory_is_over = true; if( debug_memory ) debug_msg("Call vector allocation fail"); } for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { cb_rx_buffer[ i ] = 0; cb_tx_buffer[ i ] = 0; } reset_leds(); udp_timer.start(); init_telemetry_handler(); //debug_msg(""); init_fw_handler(); //debug_msg(""); led2 = 0; init_ranges(); init_refresh(); init_aging(); init_sync_refresh(); //init_invite_pgk_retry_manager(); //void init_bye_pgk_retry_manager(); static uint8_t count = 0; //short int bl_ret = init_bl_handler(); //if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); uint8_t max_registered_cbx = 0; bool wake_all = false; uint16_t pkg_wdt = RX_CB_IDLE; uint16_t eth_wdt = ETH_CONNECT_TIMEOUT; uint32_t uptime = 0; led1 = 0; send_msg("Ready"); //bool registry_test = false; //static int next_value = 5002; //static int nex_test_registry = next_value; //bool invite_retry_time = false; Timer invite_retry_timer; invite_retry_timer.start(); debug_uart3 = false; Timer test_timer; /*------------------------------------------ main loop ---------------------------------------------------------------*/ while( true ) { reset_leds(); // need be removed; if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size(); if( sync_timer.read() > 5 ) { sync_timer.reset(); if( debug_cks == true ) { pcks_s = true; } if( debug_alive == true ) { pshowcb = true; } { // debug_msg("Enviei"); // send2callboxes( __build_cb_package__( 5000, 5000, __INVITE__, // ( char * )buffer, 0x12, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); // v_cb -> add ( new Call_Box ( 5010, 5010 ) ); } if( !( ++count % 15 ) ) { if( eth_status ) { eth_status = __init_eth__(); if( eth_status ) { if( debug_main ) debug_msg("Cannot connect to eth"); } else { if( debug_main ) debug_msg("Connection eth - ok"); init_prompt_eth(); init_external_wdt() } } } //35 sec. if( ( count > 7 ) && ( wake_all == false ) ) { wake_all = true; if( debug_wake == true ) send_msg( "Time to wake" ); } } //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... prompt_process( NULL ); if( r_stats ) { boot_counter = 0; registry_counter = 0; invite_counter = 0; audio_counter = 0; telemetry_counter = 0; cb_bye_counter = 0; prompt_counter = 0; flood_counter = 0; bootloader_cbx_counter = 0; cb_stats_counter = 0; r_stats = false; stats = true; } if( stats ) { char str[ 200 ]; snprintf( str, 200, "\n\rReceived Pkgs::\n\r Boot :: %u\n\r Registry :: %u\n\r Invite :: %u\n\r Audio :: %u\n\r Telemetry :: %u\n\r CB_stats :: %u\n\r CB_bye :: %u\n\r Prompt :: %u\n\r Flood :: %u\n\r Bootloader_cbx :: %u\n\r", boot_counter, registry_counter, invite_counter, audio_counter, telemetry_counter, cb_stats_counter, cb_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); send_msg( str ); stats =false; } if( list ) { uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); if( ( max_ext % 2 ) == 0 ) missed_cb++; if( min_ext % 2 ) missed_cb++; if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; send_msg("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, ts->remain_timeslices(), v_call->size() ); if( v_cb->size() == 1 ) { send_msg(" %d ", ( ( Call_Box * )v_cb->get_element( 0 ) )->get_ext() ); } else if( v_cb->size() > 1 ) { char str[ 1024 ]; int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; register int i = 0; for( ; i < v_cb->size(); i++ ) { ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); } qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); char aux[ 16 ]; strcpy( str, "\r\n> " ); for( i = 0; i < v_cb->size() - 1; i++ ) { sprintf( aux, "%i, ", ext_list[ i ] ); strcat( str, aux ); if( ( i != 0 ) && !( ( i + 1 ) % 16 ) ) strcat( str, "\r\n> " ); } sprintf( aux, "%i ", ext_list[ i ] ); strcat( str, aux ); send_msg( "%s", str ); } list = false; } if( long_list ) { uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); if( ( max_ext % 2 ) == 0 ) missed_cb++; if( min_ext % 2 ) missed_cb++; if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; { int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; if( v_cb->size() >= 1 ) { for( register int i = 0; i < v_cb->size(); i++ ) { ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); } qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); } send_msg("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, ts->remain_timeslices(), v_call->size() ); for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); if( cb != NULL ) { char cb_status[ 32 ]; char cb_sip_status[ 32 ]; switch( cb->status ) { case cb_idle : { strcpy( cb_status, "cb_idle" ); break; } case cb_ringing : { strcpy( cb_status, "cb_ringing" ); break; } case cb_trying : { strcpy( cb_status,"cb_trying" ); break; } case cb_on_call : { strcpy( cb_status, "cb_on_call" ); break; } case cb_busy : { strcpy( cb_status, "cb_busy" ); break; } case cb_denied : { strcpy( cb_status, "cb_denied" ); break; } } switch( cb->sip->status ) { case sip_idle : { strcpy( cb_sip_status, "sip_idle" ); break; } case sip_waiting_trying : { strcpy( cb_sip_status, "sip_waiting_trying" ); break; } case sip_trying : { strcpy( cb_sip_status, "sip_trying" ); break; } case sip_ringing : { strcpy( cb_sip_status, "sip_ringing" ); break; } case sip_busy : { strcpy( cb_sip_status, "sip_busy" ); break; } case sip_ok : { strcpy( cb_sip_status, "sip_ok" ); break; } case sip_on_call : { strcpy( cb_sip_status, "sip_on_call" ); break; } case sip_denied : { strcpy( cb_sip_status, "sip_denied" ); break; } } char cbx_to_string[ 254 ]; char aux[ 16 ]; strcpy( cbx_to_string, "Ext :: " ); itoa( cb->get_ext(), aux , 10 ); strcat( cbx_to_string, aux ); strcat( cbx_to_string, " :: Port :: " ); itoa( cb->get_port(), aux , 10 ); strcat( cbx_to_string, aux ); strcat( cbx_to_string, " :: Status -- " ); strcat( cbx_to_string, cb_status ); strcat( cbx_to_string, " - " ); strcat( cbx_to_string, cb_sip_status ); if( cb->get_timeslice() != 0 ) { strcat( cbx_to_string, " -- on TimeSlice :: " ); itoa( cb->get_timeslice(), aux , 10 ); strcat( cbx_to_string, aux ); } send_msg( cbx_to_string ); } } } long_list = false; } if( pshowcb == true ) { send_msg("Registered %d ( of %d ) CBx ( %d - %d ) -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, ts->remain_timeslices(), v_call->size() ); for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); char cb_status[ 32 ]; char cb_sip_status[ 32 ]; switch( cb->status ) { case cb_idle : { strcpy( cb_status, "cb_idle" ); break; } case cb_ringing : { strcpy( cb_status, "cb_ringing" ); break; } case cb_trying : { strcpy( cb_status,"cb_trying" ); break; } case cb_on_call : { strcpy( cb_status, "cb_on_call" ); break; } case cb_busy : { strcpy( cb_status, "cb_busy" ); break; } case cb_denied : { strcpy( cb_status, "cb_denied" ); break; } } switch( cb->sip->status ) { case sip_idle : { strcpy( cb_sip_status, "sip_idle" ); break; } case sip_waiting_trying : { strcpy( cb_sip_status, "sip_waiting_trying" ); break; } case sip_trying : { strcpy( cb_sip_status, "sip_trying" ); break; } case sip_ringing : { strcpy( cb_sip_status, "sip_ringing" ); break; } case sip_busy : { strcpy( cb_sip_status, "sip_busy" ); break; } case sip_ok : { strcpy( cb_sip_status, "sip_ok" ); break; } case sip_on_call : { strcpy( cb_sip_status, "sip_on_call" ); break; } case sip_denied : { strcpy( cb_sip_status, "sip_denied" ); break; } } char cbx_to_string[ 254 ]; char aux[ 16 ]; strcpy( cbx_to_string, "Ext :: " ); itoa( cb->get_ext(), aux , 10 ); strcat( cbx_to_string, aux ); strcat( cbx_to_string, " :: Port :: " ); itoa( cb->get_port(), aux , 10 ); strcat( cbx_to_string, aux ); strcat( cbx_to_string, " :: Status -- " ); strcat( cbx_to_string, cb_status ); strcat( cbx_to_string, " - " ); strcat( cbx_to_string, cb_sip_status ); if( cb->get_timeslice() != 0 ) { strcat( cbx_to_string, " -- on TimeSlice :: " ); itoa( cb->get_timeslice(), aux , 10 ); strcat( cbx_to_string, aux ); } send_msg( cbx_to_string ); } pshowcb = false; } if( show_sip == true ){ show_sip = false; send_msg(":: Sip :: %u", v_cb->size() ); for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); send_msg("ext :: %d -- port :: %d -- timer %d", cb->get_sip_ext(), cb->get_sip_port(), cb->get_timer() ); } } if( pflood == true ) flood(); if( debug_eth ) { debug_eth = false; send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" ); } if( status != __WAITING__ ) { pkg_wdt = RX_CB_IDLE; xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); status = __WAITING__; missed_pkg--; xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); /* { uint16_t e, p; e = buffer[0]; e <<= 8; e |= buffer[1]; p = buffer[2]; p <<= 8; p |= buffer[3]; //if (e != (p+100)) { if( ( buffer[0] == 0x14 ) && ( buffer[1] == 0x0a ) ) { int i; char s[400], ss[4];; strcpy(s,"OPS-PKG:"); for (i=0; i<40; i++) { sprintf(ss," %02x",buffer[i]); strcat(s,ss); } debug_msg( s ); } } */ if( debug_cpld ) { rx = true; tx = true; } if( rx ) { char str[ 1024 ]; strcpy( str, "RX :: \n\r " ); for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { char tmp[ 16 ]; strcat( str, itoa( cb_rx_buffer[ i ], tmp, 16 ) ); if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); else strcat( str, " " ); } send_msg( "%s", str ); rx = false; } data = __parse_vz_pkg__( &ext, &port, &type, buffer ); if( data != NULL ) { if( min_ext == 0 ) min_ext = ext; if( ext > max_ext ) max_ext = ext; if( ext < min_ext ) min_ext = ext; if( debug_fw ){ fw_cbx_pkg( ext, port, ( char *)buffer ); } if( type == __TELEMETRY__ ) telemetry_counter++; if( type != __AUDIO__ ) { if( type == __TELEMETRY__ || type == __CB_STATS__ || type == __FW1__ || type == __FW2__ || type == __FW3__ || type == __FW4__ || type == __FW5__ || type == __FW6__ ) type = __FW__; if( debug_cb_rx == true ){ send_msg("Pkg from CBx :: ( %d, %d ) -- Type :: %d", ext, port, type ); } Call_Box * cb = __find_CB__( v_cb, ext ); if( cb != NULL ) { if( data[ 0 ] & BIT7 ) { if( type == __BOOT__ ) { send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); } else { if( debug_main ) debug_msg("Received ack pkg with seq_num %d", data[ 0 ] ); switch( type ) { case __INVITE__ : { if( debug_main || debug_invite ) debug_msg("Invite Ack from %d on msg_id %d", ext, cb->get_msg_id() ); //cb->first_invite_response_ok(); break; } case __CB_BYE__ : { if( debug_main || debug_invite ) debug_msg("BYE Ack from %d on msg_id %d", ext, cb->get_msg_id() ); cb->set_bye_response_ok(); break; } case __REGISTRY__ : { if( debug_main || debug_aging ) debug_msg("Registry ACK from %d in pkg :: %d", ext, cb->get_msg_id() ); break; } default : { if( debug_main || debug_aging ) debug_msg("ACK from %d in pkg :: %d :: type %d", ext, cb->get_msg_id(), type ); } } if( type != __REGISTRY__ && type != __CB_BYE__ ) type = __DO_NOTHING__; if( type == __CB_BYE__ ){ VZ_call * call = __find_Call__( v_call, ext ); if( call != NULL ){ if( call->get_elapsed_time() < 120000 ){ if( debug_invite ) debug_msg("%d ack bye ignored", ext ); type = __DO_NOTHING__; } } } } } } } } else type = __DO_NOTHING__; } if( main_test == true ) { main_test = false; send_msg("CB_New (%u) -- CB_Delete (%u)", cb_new_counter, cb_delete_counter ); send_msg("SIP_New (%u) -- SIP_Delete (%u)", sip_new_counter, sip_delete_counter ); send_msg("RTP_header_New (%u) -- RTP_header_Delete (%u)", rtp_header_new_counter, rtp_header_delete_counter ); send_msg("RTP_body_New (%u) -- RTP_body_Delete (%u)", rtp_body_new_counter, rtp_body_delete_counter ); send_msg("Call_New (%u) -- Call_Delete (%u)", call_new_counter, call_delete_counter ); send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter ); send_msg("Memory is %s", ( memory_is_over ) ? "Over" : "Ok" ); send_msg("Missed_Pkg :: %d ::", missed_pkg ); send_msg("Sizeof Sip :: %u", sizeof( Sip ) ); send_msg("Sizeof VZ_call :: %u", sizeof( VZ_call ) ); send_msg("Sizeof RTP :: %u", sizeof( RTP ) ); send_msg("Sizeof RTP_Header :: %u", sizeof( RTP_Header ) ); send_msg("Sizeof RTP_Body :: %u", sizeof( RTP_Body ) ); send_msg("Sizeof Vector :: %u", sizeof( Vector ) ); send_msg("Sizeof Timeslice :: %u", sizeof( Timeslice ) ); send_msg("Sizeof Watchdog :: %u", sizeof( Watchdog ) ); VZ_call * call = new VZ_call( 1234, 9876, 413, 12093 ); v_call -> add ( call ); } //static int wdt_count = 0; /* if( registry_test == true ) { registry_test = false; if( wdt_count++ > 5 || v_cb->size() < 66 ) { wdt_count = 0; if( type == __DO_NOTHING__ ) { if( next_value < ( 5002 + __MAX_CB_IN_A_BRANCH__ - 2 ) ) { //type = __REGISTRY__; data = buffer; ext = next_value; port = next_value++; } else { if( nex_test_registry > ( 5002 + __MAX_CB_IN_A_BRANCH__ - 1 - 2 ) ) nex_test_registry = 5002; //type = __REGISTRY__; data = buffer; ext = nex_test_registry; port = nex_test_registry++; } } } } */ { fd_set fdSet; FD_ZERO(&fdSet); for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *)v_cb->get_element( i ); FD_SET( cb->get_sip_socket_fd(), &fdSet); } FD_SET( udp_wdt_client.get_fd(), &fdSet); struct timeval t; t.tv_sec = 0; t.tv_usec = 0; int ret = lwip_select( FD_SETSIZE, &fdSet, NULL, NULL, &t ); if(ret > 0 ) { for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *)v_cb->get_element( i ); int fd = cb->get_sip_socket_fd(); if( FD_ISSET( fd, &fdSet ) ) { int rcv = cb->sip_udp_incomming_pkg(); } } if( FD_ISSET( udp_wdt_client.get_fd(), &fdSet ) ) { char wake_msg[ 768 ]; Endpoint udp_wdt_server; if( udp_wdt_client.receiveFrom( udp_wdt_server, wake_msg, sizeof( wake_msg ) ) > 0 ){ if( !( strncmp( wake_msg, "alive", 5 ) ) ) { // Just ckeck but not set 'alive?' // 'alive*' - force wdt tick right now // Ckecking and set 'alive' bool question_alive = ( wake_msg[ 5 ] == '?' ); if( wake_msg[ 5 ] == '*' ) wdt.kick(); /* uint8_t registered_cbx = v_cb->size(); for( register int i = 0; i < v_cb->size(); i++ ){ Call_Box * cb = (Call_Box * )v_cb->get_element( i ); if( cb->is_timeout() ){ if( registered_cbx ) registered_cbx--; } } */ snprintf( wake_msg, 48,"wdt:%u,%u,%u,%c,%u,%u,%u,%u,%u:", uptime, invite_counter, external_wdt, ( wdt.WatchdogCausedReset() ) ? '1' : '0', cb_new_counter, v_cb->size(), ts->remain_timeslices(), sip_socket_send_failure, v_call->size() // registered_cbx ); wake_msg[ 768 - 1 ] = 0; udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) ); if( ( (!question_alive) && ( cb_new_counter <= __MAX_CB_IN_A_BRANCH__ ) && ( cb_new_counter >= 2 ) ) ) { external_wdt = EXTERN_WDT_IDLE; } } else if( !( strncmp( wake_msg, "reset", 5 ) ) ) { external_wdt = 0; sprintf( wake_msg, "rst:%u:", uptime ); udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) ); } } } } } if( dshow_rtp == true ){ dshow_rtp = false; send_msg(":: RTP :: %u", v_cb->size() ); int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; if( v_cb->size() >= 1 ) { for( register int i = 0; i < v_cb->size(); i++ ) { ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); } qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); } for ( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = __find_CB__( v_cb, ext_list[ i ] ); if( cb != NULL ) { send_msg("CBX ( %d, %d ) - SIP ( %d, %d ) - RTP ( %d )", cb -> get_ext (), cb -> get_port (), cb -> get_sip_ext (), cb -> get_sip_port (), cb -> get_rtp_port () ); } } } if( malasia ) { malasia = false; // conclusao, mudar o nro nao impala, talvez mudar o nro no pacote ... Call_Box * cb = __find_CB__( v_cb, 5016 ); if( cb != NULL ) { cb -> set_rtp_port ( cb -> get_rtp_port () * 10 + 1 ); send_msg("CBX ( %d, %d ) - SIP ( %d, %d ) - RTP ( %d )", cb -> get_ext (), cb -> get_port (), cb -> get_sip_ext (), cb -> get_sip_port (), cb -> get_rtp_port () ); } } if( york ) { york = false; Call_Box * cb = __find_CB__( v_cb, 5016 ); if( cb != NULL ) { cb -> set_rtp_port ( 80 ); send_msg("CBX ( %d, %d ) - SIP ( %d, %d ) - RTP ( %d )", cb -> get_ext (), cb -> get_port (), cb -> get_sip_ext (), cb -> get_sip_port (), cb -> get_rtp_port () ); } } if ( frtp ) { frtp = false; Call_Box * cb = __find_CB__( v_cb, frtp_target ); if ( cb != NULL ) { cb -> set_rtp_port ( -1008789032 ); } else { debug_msg("frtp fail"); } } if ( rescue_rtp ) { rescue_rtp = false; Call_Box * cb = __find_CB__( v_cb, rescue_rtp_target ); if ( cb != NULL ) { cb -> set_rtp_port ( rescue_rtp_value ); } else { debug_msg("rescue rtp fail"); } } if( america ) { america = false; Call_Box * cb = __find_CB__( v_cb, 5016 ); if( cb != NULL ) { cb -> set_rtp_port ( 8929415 ); send_msg("CBX ( %d, %d ) - SIP ( %d, %d ) - RTP ( %d )", cb -> get_ext (), cb -> get_port (), cb -> get_sip_ext (), cb -> get_sip_port (), cb -> get_rtp_port () ); } } //begin debug_print if ( print_v_cb ) { print_v_cb = false; v_cb->print_yourself (); } if ( print_v_call ) { print_v_call = false; v_call->print_yourself (); } if ( print_cb_var ) { print_cb_var = false; Call_Box * cb = __find_CB__ ( v_cb, print_this_cb ); if ( cb != NULL ) { cb -> print_yourself (); } else { if( print_values ) { debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } else { send_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } } } if ( print_cb_all ) { print_cb_all = false; if ( v_cb -> size () == 0 ) { send_msg("known CBx :: 0"); } for ( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *)v_cb->get_element( i ); if ( cb != NULL ) { cb -> print_yourself (); } else { if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", i ); } } } if ( print_hex_cb_var ) { print_hex_cb_var = false; uint8_t * ptr = ( uint8_t * ) __find_CB__ ( v_cb, print_hex_this_cb ); if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ]; for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0; uint8_t * ptr = ( uint8_t * ) __find_CB__ ( v_cb, print_hex_this_cb ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); } strcat( aux, "\n\r\0" ); tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) ); tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); } } else { if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } } if ( print_hex_cb_all ) { print_hex_cb_all = false; if ( v_cb -> size () == 0 ) { send_msg("known CBx :: 0"); } for ( register uint8_t j = 0; j < v_cb->size(); j++ ) { uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j ); if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ]; for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0; uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); } strcat( aux, "\n\r\0" ); tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) ); tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); } } else { if ( print_values ) { debug_msg ("Objeto CBx ( %d ) nao encontrado", j ); } else { send_msg ("Objeto CBx ( %d ) nao encontrado", j ); } } } } //begin Sip debug print { if ( print_sip_var ) { print_sip_var = false; Call_Box * cb = __find_CB__ ( v_cb, print_this_sip ); if ( cb != NULL ) { if( ( cb -> sip ) != NULL ) { cb -> sip -> print_yourself (); } else { if( print_values ) { debug_msg("Sip param of %d equals NULL", print_this_sip ); } else { send_msg("Sip param of %d equals NULL", print_this_sip ); } } } else { if ( print_values ) { debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } else { send_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } } } if ( print_sip_all ) { print_sip_all = false; if ( v_cb -> size () == 0 ) { send_msg("known CBx :: 0"); } for ( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *)v_cb->get_element( i ); if ( cb != NULL ) { if( ( cb -> sip ) != NULL ) { cb -> sip -> print_yourself (); } else { if( print_values ) { debug_msg("Sip param of %d equals NULL", print_this_sip ); } else { send_msg("Sip param of %d equals NULL", print_this_sip ); } } } else { if( print_values ) { debug_msg("CBx %d not found", print_this_sip ); } else { send_msg("CBx %d not found", print_this_sip ); } } } } if ( print_hex_sip_var ) { uint8_t * ptr = NULL; print_hex_sip_var = false; Call_Box * cb = __find_CB__ ( v_cb, print_hex_this_sip ); if( cb != NULL ) { if( ( cb -> sip ) != NULL ) ptr = ( uint8_t * ) cb -> sip; } if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( Sip ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( 32 * 3 ) + 5 ]; for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0; ptr = NULL; print_hex_sip_var = false; Call_Box * cb = __find_CB__ ( v_cb, print_hex_this_sip ); if( cb != NULL ) { if( ( cb -> sip ) != NULL ) ptr = ( uint8_t * ) cb -> sip; } if ( ptr != NULL ) { bool finished = false; volatile int i = 0; int count = 0; Timer dont_overwrite_tcp_buffer; dont_overwrite_tcp_buffer.reset (); dont_overwrite_tcp_buffer.start (); while ( !finished ) { if( dont_overwrite_tcp_buffer.read_ms () >= 500 ) { dont_overwrite_tcp_buffer.reset(); for ( ; i < sizeof( Sip ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); count++; if ( count >= 32 ) { count = 0; i++; break; } } strcat ( aux, "\n\r\0" ); tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) ); if ( i >= sizeof ( Sip ) ) finished = true; strcpy ( aux, "\0" ); } } dont_overwrite_tcp_buffer.reset(); dont_overwrite_tcp_buffer.stop(); tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) ); } } } else { if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", print_this_cb ); } } /* if ( print_hex_cb_all ) { print_hex_cb_all = false; if ( v_cb -> size () == 0 ) { send_msg("known CBx :: 0"); } for ( register uint8_t j = 0; j < v_cb->size(); j++ ) { uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j ); if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( sizeof( Call_Box ) * 3 ) + 3 ]; for ( register int i = 0; i < ( sizeof( Call_Box ) * 3 ) + 3 ; i++ ) aux [ i ] = 0; uint8_t * ptr = ( uint8_t * ) v_cb->get_element( j ); for ( register int i = 0; i < sizeof( Call_Box ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); } strcat( aux, "\n\r\0" ); tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) ); tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); } } else { if ( print_values ) debug_msg ("Objeto CBx ( %d ) nao encontrado", j ); } } } */ }//end Sip debug print if( dcallshow_rtp == true ){ dcallshow_rtp = false; send_msg(":: CAll RTP :: %u", v_call->size() ); for( register uint8_t i = 0; i < v_call->size(); i++ ) { VZ_call * call = ( VZ_call * )v_call->get_element( i ); if( call != NULL ) { send_msg("CBX ( %d, %d ) - Server ( %d, %d )", call->get_cb_ext(), call->get_cb_port(), call->get_rtp_server_ext(), call->get_rtp_server_port() ); } } } if ( print_call_var ) { print_call_var = false; VZ_call * call = __find_Call__( v_call, print_this_call ); if ( call != NULL ) { send_msg ("Values :: %p\r\n", ( void *) call ); call -> print_yourself (); } else { if( print_values ) { debug_msg("Call %d not found", print_this_call ); } else { send_msg("Call %d not found", print_this_call ); } } } if ( print_hex_call_var ) { print_hex_call_var = false; VZ_call * call = __find_Call__( v_call, print_hex_this_call ); uint8_t * ptr = NULL; if( call != NULL ) { ptr = ( uint8_t * ) call; } if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( VZ_call ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( 32 * 3 ) + 5 ]; for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0; ptr = NULL; VZ_call * call = __find_Call__( v_call, print_hex_this_call ); uint8_t * ptr = NULL; if( call != NULL ) { ptr = ( uint8_t * ) call; } if ( ptr != NULL ) { bool finished = false; volatile int i = 0; int count = 0; Timer dont_overwrite_tcp_buffer; dont_overwrite_tcp_buffer.reset (); dont_overwrite_tcp_buffer.start (); while ( !finished ) { if( dont_overwrite_tcp_buffer.read_ms () >= 20 ) { dont_overwrite_tcp_buffer.reset(); for ( ; i < sizeof( VZ_call ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); count++; if ( count >= 32 ) { count = 0; i++; break; } } strcat ( aux, "\n\r\0" ); tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) ); if ( i >= sizeof ( VZ_call ) ) finished = true; strcpy ( aux, "\0" ); } } dont_overwrite_tcp_buffer.reset(); dont_overwrite_tcp_buffer.stop(); tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) ); } } } else { if( print_values ) { debug_msg("Call %d not found", print_this_call ); } else { send_msg("Call %d not found", print_this_call ); } } } if ( print_hex_rtp_var ) { print_hex_rtp_var = false; VZ_call * call = __find_Call__( v_call, print_hex_this_rtp ); uint8_t * ptr = NULL; if( call != NULL ) { ptr = ( uint8_t * ) call -> check_rtp (); } if ( ptr != NULL ) { send_msg ("Values :: %p\r\n", ( void *) ptr ); for ( register int i = 0; i < sizeof( RTP ); i++ ) { if( debug_uart3 ) pc.printf("%x", *ptr++ ); if ( ( i % 32 ) == 0 ) { if( i != 0 ) { if( debug_uart3 ) pc.printf("\n\r> "); } else { if( debug_uart3 ) pc.printf(" "); } } else { if( debug_uart3 ) pc.printf(" "); } } if ( debug_uart3 ) pc.printf("\n\r> "); if ( tcp_session ) { char aux[ ( 32 * 3 ) + 5 ]; for ( register int i = 0; i < ( 32 * 3 ) + 5 ; i++ ) aux [ i ] = 0; ptr = NULL; VZ_call * call = __find_Call__( v_call, print_hex_this_rtp ); uint8_t * ptr = NULL; if( call != NULL ) { ptr = ( uint8_t * ) call -> check_rtp (); } if ( ptr != NULL ) { bool finished = false; volatile int i = 0; int count = 0; Timer dont_overwrite_tcp_buffer; dont_overwrite_tcp_buffer.reset (); dont_overwrite_tcp_buffer.start (); while ( !finished ) { if( dont_overwrite_tcp_buffer.read_ms () >= 20 ) { dont_overwrite_tcp_buffer.reset(); for ( ; i < sizeof( RTP ); i++ ) { char tmp[ 16 ]; sprintf( tmp, "%x ", *ptr++ ); strcat( aux, tmp ); count++; if ( count >= 32 ) { count = 0; i++; break; } } strcat ( aux, "\n\r\0" ); tcp_client.send_all ( ( char *)aux, strlen( (char * )aux ) ); if ( i >= sizeof ( RTP ) ) finished = true; strcpy ( aux, "\0" ); } } dont_overwrite_tcp_buffer.reset(); dont_overwrite_tcp_buffer.stop(); tcp_client.send_all ( "\r\n> ", strlen( "\r\n> " ) ); } } } else { if( print_values ) { debug_msg("Call %d not found", print_this_rtp ); } else { send_msg("Call %d not found", print_this_rtp ); } } } if ( print_rtp_var ) { print_rtp_var = false; VZ_call * call = __find_Call__( v_call, print_this_rtp ); if ( call != NULL ) { if ( call -> check_rtp () != NULL ) { send_msg ("Values :: %p\r\n", ( void *) call -> check_rtp () ); call -> rtp_print_yourself (); } else { if( print_values ) { debug_msg("Call->RTP %d not found", print_this_rtp ); } else { send_msg("Call->RTP %d not found", print_this_rtp ); } } } else { if( print_values ) { debug_msg("Call->RTP %d not found", print_this_rtp ); } else { send_msg("Call->RTP %d not found", print_this_rtp ); } } } //end debug_print if( reset_cks == true ) { pkg_cksok = 0; pkg_ckserr = 0; pkg_zero = 0; reset_cks = false; out_of_range = 0; missed_pkg = 0; delayed_pkg_to_cb = 0; cpld_pkg_tx_counter = 0; cpld_pkg_rx_counter = 0; pcks_s = true; } if( pcks_s == true ) { send_msg("PKG_CKS OK: %d :: PKG_CKS ERR: %d :: PKG_0: %d :: Out_of_range: %d :: Miss_Pkg :: %d" "\n\r> TX_delayed :: %d :: TX_Counter :: %d :: RX_Counter :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg, delayed_pkg_to_cb, cpld_pkg_tx_counter, cpld_pkg_rx_counter ); pcks_s = false; } if( flood_bug_pkg ){ static int id = 0x10; if( id < 10 ) id = 0x0b; send2callboxes( __build_cb_package__( 5828, 5123, __REGISTRY__, ( char * )buffer, id++, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); } if( led_sync_timer.read() > 1 ) { led_sync_timer.reset(); led3 = !led3; CAB_LED = !CAB_LED; } static int test_ext = 5102; static int test_port = 5102; if ( tango ) { if ( v_cb -> size () >= 27 ) { tango = false; test_timer.start (); } ext = test_ext++; port = test_port++; type = __REGISTRY__; data = buffer; } if( v_cb -> size () >= 27 ) { if( ( test_timer.read_ms () > 500 ) && ( type == __DO_NOTHING__ ) ) { if( test_ext >= ( 5102 + 26 ) ) test_ext = 5102; if( test_port >= ( 5102 + 26 ) ) test_port = 5102; ext = test_ext++; port = test_port++; type = __REGISTRY__; data = buffer; test_timer.reset (); } } switch( type ) { case __DO_NOTHING__ : {} break; case __CB_BYE__ : { cb_bye_counter++; Call_Box * cb = __find_CB__( v_cb, ext ); if( cb != NULL ) { if( debug_invite || debug_main ) debug_msg("Received bye pkg with msg_id %d e pkg_id %d", cb->get_msg_id(), data[ 0 ] ); data[ 0 ] |= BIT7; cb->set_msg_id( data[ 0 ] ); if( debug_main || debug_invite ) debug_msg( "Request bye from CBx " ); bool already_removed = true; for( register uint8_t i = 0; i < v_call->size(); i++ ) { VZ_call * call = (VZ_call *)v_call->get_element( i ); if( call->get_cb_ext() == ext ) { already_removed = false; cb->send_bye(); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; set_status( cb->status, cb_idle ); set_status( cb->sip->status, sip_idle ); v_call->remove_element( i ); //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); // envia o ack bye depois atualiza o msg_id cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); delete( call ); //cb->re_start_timer(); } } if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from vector call" ); //ok, mas nem sempre o cbx "entrou em call for( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *)v_cb->get_element( i ); if( cb->get_ext() == ext ) { already_removed = true; if( cb->get_status() != cb_idle ) { already_removed = false; cb->send_bye(); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; set_status( cb->status, cb_idle ); set_status( cb->sip->status, sip_idle ); send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); //cb->re_start_timer(); } } } if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from inviting queue" ); cb->registry(); } else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext ); } break; case __INVITE__ : { if( drop_invite_pkg ){ debug_msg("Dropando invite pck msg id :: %d", data[ 0 ] ); break; } invite_counter++; if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); Call_Box * cb = __find_CB__( v_cb, ext ); if( cb == NULL ) { if( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { if( debug_main ) debug_msg( "Adding CBx :: %i", ext ); cb = new Call_Box( ext, port ); if( cb == NULL ) { memory_is_over = true; if( debug_memory ) debug_msg("Invite allocation cb fail"); } else { v_cb->add( cb ); } } } if( cb != NULL ) { cb->set_msg_id( data[ 0 ] ); if( cb->status == cb_idle ){ cb->set_invite_response_ok(); cb->invite_retry_count_reset(); } invite_handler( v_call, v_cb, ts, cb ); } } break; case __REGISTRY__ : { registry_counter++; Call_Box * cb = __find_CB__( v_cb, ext ); if( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { if( cb == NULL ) { if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); cb = new Call_Box( ext, port ); if( cb == NULL ) { memory_is_over = true; if( debug_memory ) debug_msg("Registry cb allocation fail"); } else { v_cb->add( cb ); if( debug_main ) debug_msg("Added CBx -- %d", ext ); } } } if( debug_main ) debug_msg("Registered %d - %d", ext, port ); if( cb != NULL ) cb->registry(); } break; case __BOOT__ : { boot_counter++; if( debug_boot == true ){ send_msg("Rcv boot pkg from (%d, %d) pkg-id %d", ext, port, data[ 0 ] ); } send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, ( char * )data, data[ 0 ] | BIT7, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); } break; case __TELEMETRY__ : { if( debug_telemetry ) send_msg("::Telemetry from %d - %d::", ext, port ); build_telemetry_report( ext, port, ( char *)data ); } break; case __FW__ : { if( debug_fw_print ) send_msg("::FW pkg from %d - %d::", ext, port ); fw_cbx_pkg( ext, port, ( char *)buffer ); } break; case __BOOTLOADER_CBX__ : { bootloader_cbx_counter++; //int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); } break; case __PROMPT__ : { Call_Box * cb = __find_CB__( v_cb, ext ); if( cb == NULL ) { // send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, // ( char * )data, ( ( data[ 0 ] & ~BIT7 ) + 1 ), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); cb = new Call_Box( ext, port ); if( cb == NULL ){ memory_is_over = true; }else{ v_cb->add( cb ); if( debug_main ) debug_msg("Added CBx -- %d", ext ); } } if( cb!= NULL ) cb->registry(); if( xstrmatch( ( uint8_t * )data, ( uint8_t * )"ping" ) ) { if( debug_ping ) send_msg( "Prompt pkg from ( %i, %i ) :: Ping", ext, port ); } else { prompt_counter++; //fixme isso nao poderia ser resolvido com um sendmsg ? send_msg( "Prompt pkg from ( %i, %i ) ::", ext, port ); for( register uint8_t i = 0; i < 32; i++ ) { if( debug_uart3 ) pc.printf("%c", data[ i ] ); if( i == 15 ) if( debug_uart3 ) pc.printf( "\r\n" ); } if( debug_uart3 ) pc.printf("\n\r> "); if( tcp_session ) { char aux[ __CB_BUFFER_SIZE__ + 3 ]; strncpy( aux, (char * )data, __CB_BUFFER_SIZE__ ); strcat( aux, "\n\r\0" ); tcp_client.send_all( ( char *)data, strlen( (char * )data ) ); tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); } } } break; case __AUDIO__ : { audio_counter++; VZ_call * call = __find_Call__( v_call, ext ); if( call != NULL ) { char * pkg = call->build_eth_package( data + 2 ); call->send_message( pkg ); call->cbx_pkg_idle_timer_reset(); Call_Box * cb = __find_CB__( v_cb, ext ); if ( drop_rtp_from_cbx_pkg ) { led2 = !led2; break; } if( cb != NULL ){ //cb->reset_elapsed_time(); cb->set_invite_response_ok(); cb->invite_retry_count_reset(); } } else { if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); } } break; }// fim switch { // rajada //if( invite_retry_time == true ){ // invite_retry_time = false; if( invite_retry_timer.read_ms() > 30 ) { invite_retry_timer.reset(); static int retry_invite_pkg = 0; if( retry_invite_pkg >= v_cb->size() ) retry_invite_pkg = 0; register int i = 0; for( ; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box * )v_cb->get_element( i ); if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ) { if( i > retry_invite_pkg ) { retry_invite_pkg = i; break; } } } if( i != v_cb->size() ){ Call_Box * cb = (Call_Box *)v_cb->get_element( retry_invite_pkg++ ); if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ) { if( cb->get_invite_response() == false ) { //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); buffer[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __INVITE__, ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_invite ) debug_msg("resend invite OK to Cbx : ( %d, %d )", cb->get_ext(), cb->get_port() ); } if( cb->get_invite_retry_count() == 0 ) { cb->send_bye(); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); for( register uint8_t i = 0; i < v_call->size(); i++ ) { VZ_call * call = ( VZ_call * )v_call->get_element( i ); if( call->get_cb_ext() == cb->get_ext() ) { v_call->remove_element( i ); if( call != NULL ) delete( call ); break; } } if( debug_invite ) debug_msg( "-- No audio pkgs --" ); set_status( cb->status, cb_idle ); set_status( cb->sip->status, sip_idle ); } } } } } // fim rajada for( register uint8_t i = 0; i < v_call->size(); i++ ) { VZ_call * call = ( VZ_call * )v_call->get_element( i ); int length = 0; char * tmp = call->get_eth_message( &length ); if( tmp != NULL ) { int cb_port = 0xffff; Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); if( cb != NULL ) { cb_port = cb->get_port(); if ( drop_rtp_from_ast_pkg ) { led1 = !led1; } else { uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, tmp, __AUDIO__, length, write_buffer ); send2callboxes( pkg2cb ); } } else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); } } //if( v_call->size() == 0 ) refresh( v_cb, buffer, write_buffer, NULL ); if( timer_sync_refresh.read_ms() > 250 ) { timer_sync_refresh.reset(); static uint8_t mode = TIME_TO_REFRESH; if( mode == TIME_TO_REFRESH ) { mode = TIME_TO_WAKE_UP; refresh( v_cb, buffer, write_buffer, NULL ); } else { mode = TIME_TO_REFRESH; if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); } } //refresh( v_cb, buffer, write_buffer, NULL ); //Fixme pensar melhor nessa parte durante ligacoes, pode complicar com muitos cbx ... //if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); // check sip messages only for cbx in call ? int ext_to__be_removed = sip_manager( v_cb ); if( ext_to__be_removed > 0x00 ) { Call_Box * cb = __find_CB__( v_cb, ext_to__be_removed ); if( cb != NULL ) { if( cb->status == cb_on_call ) { ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); buffer[ __TIMESLICE_PLACE__ ] = 0x00; set_status( cb->status, cb_idle ); set_status( cb->sip->status, sip_idle ); for( register uint8_t i = 0; i < v_call->size(); i++ ) { VZ_call * call = ( VZ_call * )v_call->get_element( i ); if( call->get_cb_ext() == ext_to__be_removed ) { v_call->remove_element( i ); delete( call ); } } //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); //debug_msg("msg_id -- %d", cb->get_msg_id() ); send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_invite ) debug_msg("Received Bye from *"); //cb->re_start_timer(); } } else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d", ext ); } /* Verifica andamento de ligações para eventualmente encerra-las por timeout */ call_manager( v_call, v_cb, buffer, write_buffer, ts ); /* tratamento de pedidos de ligação */ invite_handler( v_call, v_cb, ts, NULL ); /* rotina de verificação de TS's perdidos */ if( ( v_call->size() == 0 ) && ( ts->remain_timeslices() != __MAX_TIMESLICES__ ) ) { bool ts_reset = true; for( register uint8_t i = 0; i < v_cb->size(); i++ ) { if ( ( ( Call_Box * )v_cb->get_element( i ) )->get_status() != cb_idle && ( ( Call_Box * )v_cb->get_element( i ) )->get_sip_status() != sip_idle ) { ts_reset = false; break; } } if( ts_reset ) { if( debug_invite ) debug_msg("Resetando TS"); ts->reset(); } } //invite_pgk_retry_manager( v_call, v_cb, buffer, write_buffer ); /* escuta se existe algum procedimento de gravação de cbx */ //bl_handler(); /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */ tx_buffer_ring_buffer_handler(); type = __DO_NOTHING__; if( eth_status == 0 ) eth_wdt = ETH_CONNECT_TIMEOUT; if( wdt_timer.read() >= 1 ) { //FIXME remove myself //registry_test = true; //invite_retry_time = true; uptime++; wdt_timer.reset(); if( wdt_show ) debug_wdt = true; if( external_wdt ) external_wdt--; if( pkg_wdt ) pkg_wdt--; if( eth_wdt ) eth_wdt--; if( eth_wdt && external_wdt && pkg_wdt ){ wdt.kick(); } } if( debug_wdt ) { send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter ); lpc_low_level_input_counter = 0; debug_wdt = false; if( eth_status == 0 ) { send_msg( "Wdt last reset: %s - status_eth :: Connected - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )", ( wdt.WatchdogCausedReset() ) ? "true" : "false", EXTERN_WDT_IDLE - external_wdt, EXTERN_WDT_IDLE, RX_CB_IDLE - pkg_wdt, RX_CB_IDLE ); } else { send_msg( "Wdt last reset: %s - status_eth :: Disconnected :: since %3d sec - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )", ( wdt.WatchdogCausedReset() ) ? "true" : "false", ETH_CONNECT_TIMEOUT - eth_wdt, EXTERN_WDT_IDLE - external_wdt, EXTERN_WDT_IDLE, RX_CB_IDLE - pkg_wdt, RX_CB_IDLE ); } } } }