![](/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: main.cpp
- Revision:
- 116:39a41ebb675c
- Parent:
- 115:a1e1e2e60a2c
- Child:
- 117:e9facba9db27
diff -r a1e1e2e60a2c -r 39a41ebb675c main.cpp --- a/main.cpp Thu Apr 23 13:57:54 2015 +0000 +++ b/main.cpp Thu Apr 23 20:24:09 2015 +0000 @@ -1,12 +1,10 @@ -#include "telemetry.h" #include "eth.h" #include "bits.h" -#include "UART3Interrupt.h" // for RXBuffer[ __CB_BUFFER_SIZE__ ]; +#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" @@ -15,7 +13,6 @@ #include "configs.h" #include "sdram.h" #include "fw.h" -#include "bootloader.h" volatile u16_t lpc_low_level_input_counter = 0; int main() @@ -49,7 +46,6 @@ send_msg("******* Connection eth - ok *******"); init_prompt_eth(); init_external_wdt(); - init_bl(); } Timer sync_timer, led_sync_timer; @@ -62,16 +58,16 @@ int port = 0; /* buffer para onde se copia os dados vindos do cb para tratameno interno */ - uint8_t buffer[ __CB_BUFFER_SIZE__ ]; + 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__ ]; + 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__ ]; + uint8_t cb_rx_buffer[ CB_BUFFER_SIZE ]; /* referencia para os dados contidos no pacote, sem o header */ uint8_t * data = NULL; @@ -101,7 +97,10 @@ sync_timer.reset(); } } - } else if( v_call == NULL ) { + } + + // aloca o vetor de calls + if( v_call == NULL ) { while( v_call == NULL ) { Vector * v_call = new Vector(); if( sync_timer.read() > 5 ) { @@ -121,7 +120,7 @@ if( debug_memory ) debug_msg("Call vector allocation fail"); } - for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { + for( register uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) { cb_rx_buffer[ i ] = 0; cb_tx_buffer[ i ] = 0; } @@ -130,19 +129,13 @@ 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(); @@ -198,7 +191,6 @@ if( debug_main ) debug_msg("Connection eth - ok"); init_prompt_eth(); init_external_wdt(); - init_bl(); } } } @@ -208,10 +200,17 @@ wake_all = true; if( debug_wake == true ) send_msg( "Time to wake" ); } + + // enable na variavel que exibe lista com estatisticas de pacotes que falharam ao serem enviados via interface eth + if( debug_missed ) + { + missed_send_udp_pkg = true; + } + } //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... - prompt_process( NULL ); + prompt_process( NULL, 0 ); if( r_stats ) { boot_counter = 0; @@ -224,6 +223,7 @@ flood_counter = 0; bootloader_cbx_counter = 0; cb_stats_counter = 0; + fw_counter = 0; r_stats = false; stats = true; } @@ -242,6 +242,7 @@ "CB_bye :: %u\n\r " "Prompt :: %u\n\r " "Flood :: %u\n\r " + "Flood :: %u\n\r " "Bootloader_cbx :: %u\n\r", boot_counter, registry_counter, @@ -251,7 +252,8 @@ cb_stats_counter, cb_bye_counter, prompt_counter, - flood_counter, + flood_counter, + flood_counter, bootloader_cbx_counter ); send_msg( str ); @@ -518,20 +520,15 @@ if( status != __WAITING__ ) { pkg_wdt = RX_CB_IDLE; - xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); + xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, CB_BUFFER_SIZE ); status = __WAITING__; missed_pkg--; - xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); - - if( debug_cpld ) { - rx = true; - tx = true; - } + xmemcpy( buffer, cb_rx_buffer, CB_BUFFER_SIZE ); if( rx ) { char str[ 1024 ]; strcpy( str, "RX :: \n\r " ); - for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { + 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 " ); @@ -542,7 +539,7 @@ rx = false; } - data = __parse_vz_pkg__( &ext, &port, &type, buffer ); + data = parse_vz_pkg ( &ext, &port, &type, buffer ); if( data != NULL ) { @@ -576,8 +573,8 @@ 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 ) ); + 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 ] ); @@ -616,9 +613,9 @@ } else type = __DO_NOTHING__; } - if( main_test == true ) + if( sizes == true ) { - main_test = false; + sizes = 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 ); @@ -628,6 +625,7 @@ 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 Call_Box :: %u", sizeof ( Call_Box ) ); 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 ) ); @@ -1621,12 +1619,63 @@ ); pcks_s = false; } + + // zera os contadores de pacotes que resultaram em falha de envio vio ETH + if( reset_missed_send_udp_pkg ){ + miss_fw_send_pkg = 0; + miss_prompt_udp_send_pkg = 0; + miss_sip_registry_send_pkg = 0; + miss_sip_invite_send_pkg = 0; + miss_sip_bye_send_pkg = 0; + miss_sip_unregistry_send_pkg = 0; + miss_sip_ok_send_pkg = 0; + miss_sip_rcv_bye_send_pkg = 0; + miss_wdt_send_pkg = 0; + miss_prompt_udp_send_pkg = 0; + miss_ftp_udp_send_pkg = 0; + miss_prompt_udp_rcv_pkg = 0; + reset_missed_send_udp_pkg = false; + missed_send_udp_pkg = true; + } + + // exibe estatisticas de pacotes que resultaram em falha de envio na interface ETH + if( missed_send_udp_pkg ) + { + send_msg( "\r\nMissed pkgs ::\r\n " + "FW: %d\r\n " + "Prompt UDP %d\r\n " + "Registry %d\r\n " + "Invite %d\r\n " + "Bye %d\r\n " + "Unregistry %d\r\n " + "UDP incoming ( invite ans ) %d\r\n " + "UDP incoming ( bye from * ) %d\r\n " + "Wdt [ alive | rst ] %d\r\n " + "Rcv prompt %d\r\n " + "[ debug | send ]_msg %d\r\n " + "RTP %d\r\n", + miss_fw_send_pkg, + miss_prompt_udp_send_pkg, + miss_sip_registry_send_pkg, + miss_sip_invite_send_pkg, + miss_sip_bye_send_pkg, + miss_sip_unregistry_send_pkg, + miss_sip_ok_send_pkg, + miss_sip_rcv_bye_send_pkg, + miss_wdt_send_pkg, + miss_prompt_udp_send_pkg, + miss_prompt_udp_rcv_pkg, + miss_ftp_udp_send_pkg + ); + + missed_send_udp_pkg = 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 ) ); + send2callboxes( build_cb_package( 5828, 5123, __REGISTRY__, + ( char * )buffer, id++, CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); } if( led_sync_timer.read() > 1 ) { @@ -1634,38 +1683,6 @@ 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__ : @@ -1696,18 +1713,18 @@ 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 ) ); + 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 ); + cb -> set_rtp_port ( 0 ); + + cb -> reset_rtp_timer (); + delete( call ); - //cb->re_start_timer(); } } if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from vector call" ); @@ -1728,13 +1745,10 @@ 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 ) ); + 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(); } } } @@ -1742,6 +1756,8 @@ if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from inviting queue" ); cb->registry(); + cb -> set_rtp_port ( 0 ); + cb -> reset_rtp_timer (); } else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext ); } break; @@ -1752,7 +1768,7 @@ break; } invite_counter++; - if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); + if( debug_invite ) debug_msg("Invite request from %i", ext); Call_Box * cb = __find_CB__( v_cb, ext ); if( cb == NULL ) { @@ -1776,7 +1792,9 @@ cb->set_msg_id( data[ 0 ] ); if( cb->status == cb_idle ){ - cb->set_invite_response_ok(); + cb -> set_invite_response_ok(); + cb -> reset_rtp_timer (); + cb -> init_rtp_timer (); } else { data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); @@ -1784,8 +1802,8 @@ cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); - send2callboxes( __build_cb_package__( ext, port, __INVITE__, - ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); + send2callboxes( build_cb_package( ext, port, __INVITE__, + ( char * )data, cb->get_msg_id(), CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); } invite_handler( v_call, v_cb, ts, cb ); @@ -1820,14 +1838,8 @@ 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 ); + send2callboxes( build_cb_package( ext, port, __REGISTRY__, + ( char * )data, data[ 0 ] | BIT7, CB_BUFFER_SIZE - __VZ_HEADER_OFFSET__, write_buffer ) ); } break; @@ -1846,16 +1858,15 @@ 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( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { + 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 ); + } } } @@ -1874,8 +1885,8 @@ if( debug_uart3 ) pc.printf("\n\r> "); if( tcp_session ) { - char aux[ __CB_BUFFER_SIZE__ + 3 ]; - strncpy( aux, (char * )data, __CB_BUFFER_SIZE__ ); + 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> " ) ); @@ -1947,8 +1958,8 @@ 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 ) ); + 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() ); } @@ -1959,12 +1970,12 @@ if ( ( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) ) ) { const int rtp_tmp_length = 172; - char rtp_src_tmp_write_buffer [ __CB_BUFFER_SIZE__ ]; - uint8_t rtp_dst_tmp_write_buffer [ __CB_BUFFER_SIZE__ ]; + char rtp_src_tmp_write_buffer [ CB_BUFFER_SIZE ]; + uint8_t rtp_dst_tmp_write_buffer [ CB_BUFFER_SIZE ]; for ( register int i = 0; i < rtp_tmp_length; i++ ) rtp_src_tmp_write_buffer [ i ] = 'U'; // 0x55 - send2callboxes( __build_cb_package__( cb->get_ext (), cb->get_port (), __AUDIO__, + send2callboxes( build_cb_package( cb->get_ext (), cb->get_port (), __AUDIO__, rtp_src_tmp_write_buffer, __AUDIO__, rtp_tmp_length, rtp_dst_tmp_write_buffer ) ); } else { if ( cb->get_invite_retry_count() == 0 ) @@ -1999,44 +2010,45 @@ 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 ( call != NULL ) + { + 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; + static uint8_t time_to_mode = TIME_TO_REFRESH; - if( mode == TIME_TO_REFRESH ) { - mode = TIME_TO_WAKE_UP; - refresh( v_cb, buffer, write_buffer, NULL ); + if ( time_to_mode == TIME_TO_REFRESH ){ + time_to_mode = TIME_TO_WAKE_UP; + refresh ( v_cb ); } else { - mode = TIME_TO_REFRESH; - if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); + time_to_mode = TIME_TO_REFRESH; + if ( wake_all ) if ( v_call->size() == 0 ) wake_all_up ( v_cb ); } } @@ -2061,13 +2073,14 @@ } } - //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); + + cb -> set_rtp_port ( 0 ); + + cb -> reset_rtp_timer (); - //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 ) ); + 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(); @@ -2076,7 +2089,7 @@ } /* Verifica andamento de ligações para eventualmente encerra-las por timeout */ - call_manager( v_call, v_cb, buffer, write_buffer, ts ); + call_manager( v_call, v_cb, ts ); /* tratamento de pedidos de ligação */ invite_handler( v_call, v_cb, ts, NULL ); @@ -2098,7 +2111,6 @@ } /* rotina "zeradora" de portas RTP */ - /* for ( register uint8_t i = 0; i < v_cb->size(); i++ ) { Call_Box * cb = (Call_Box *) v_cb->get_element (i); @@ -2106,7 +2118,7 @@ { if ( ( cb->get_status () == cb_idle ) && ( cb->get_sip_status () == sip_idle ) && ( cb -> is_rtp_timer_timeout () ) ) { - if ( debug_rtp ) debug_msg( "%d rtp reset", cb -> get_ext () ); + if ( debug_reset_rtp ) debug_msg( "%d rtp reset", cb -> get_ext () ); set_status ( cb -> status, cb_idle ); set_status ( cb->sip->status, sip_idle ); cb -> reset_rtp_timer (); @@ -2114,7 +2126,6 @@ } } } - */ /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */ tx_buffer_ring_buffer_handler();