![](/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:
- 81:3656f00ab3db
- Parent:
- 80:61d61c9eb75c
- Child:
- 82:f55d13babca0
--- a/main.cpp Tue Dec 16 19:34:30 2014 +0000 +++ b/main.cpp Tue Dec 30 12:37:40 2014 +0000 @@ -15,23 +15,19 @@ #include "configs.h" int main(){ - /* inicia o wdt interno */ - wdt.Configure( 40.0 ); + init_wdt(); - /*inicia o wdt externo */ - init_tcp_wdt(); + config_lpc(); start_cpld(); - config_lpc(); - init_fsystem(); short int eth_status = __init_eth__(); if( eth_status ){ send_msg("Cannot connect to eth\n\r"); }else{ - send_msg("Connection eth - ok"); + send_msg("******* Connection eth - ok *******"); init_prompt_eth(); } @@ -71,7 +67,7 @@ /* gerencia a distribuicao de timeslice para os call boxes */ Timeslice * ts = new Timeslice(); - /* tenta instanciar o vetor de call boxes, acende o led 1 antes de encerrar o programa */ + /* instancia o vetor de call boxes // calls */ if( v_cb == NULL ){ while( v_cb == NULL ){ Vector * v_cb = new Vector(); @@ -90,8 +86,6 @@ } } - reset_leds(); - for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ cb_rx_buffer[ i ] = 0; cb_tx_buffer[ i ] = 0; @@ -103,20 +97,26 @@ init_telemetry_handler(); + init_ranges(); + init_refresh(); init_aging(); + //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 before = ( int ) sync_timer.read() % 2; - uint8_t max_registered_cbx = 0; - //bool wake_all = false; + bool wake_all = false; + + uint16_t pkg_wdt = RX_CB_IDLE; + uint16_t eth_wdt = ETH_CONNECT_TIMEOUT; send_msg("Ready"); @@ -128,55 +128,15 @@ sync_timer.reset(); if( debug_cks == true ){ - send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d :: Out_of_range: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range ); + pcks_s = true; } if( debug_alive == true ){ - send_msg("Registered %d ( of %d ) CBx -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx , 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, " :: 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 = true; } - - count++; - if( !( count % 15 ) ){ + + if( !( ++count % 15 ) ){ if( eth_status ){ - wdt.kick(); eth_status = __init_eth__(); if( eth_status ){ if( debug_main ) debug_msg("Cannot connect to eth"); @@ -186,60 +146,17 @@ } } } - if( !( count % 6 ) ){ - //wake_all = true; + //35 sec. + if( ( count > 7 ) && ( wake_all == false ) ){ + wake_all = true; if( debug_wake == true ) send_msg( "Time to wake" ); } - //if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); + if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); } - if( main_test == true ){ - uint8_t test_data[ 1024 ]; - Call_Box * cb = __find_CB__( v_cb, 5514 ); - - strcpy( ( char * )test_data, "ping\r\r\r\n" ); - if( cb != NULL ){ - send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __PROMPT__, - ( char * )test_data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - debug_msg("test_ping"); - } - } - - //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... prompt_process( NULL ); - wdt.kick(); - - if( debug_cpld ) rx = 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; - } - - if( tx ){ - char str[ 1024 ]; - strcpy( str, "TX :: \n\r " ); - for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ - char tmp[ 16 ]; - strcat( str, itoa( cb_tx_buffer[ i ], tmp, 16 ) ); - if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); - - else strcat( str, " " ); - } - send_msg( "%s", str ); - tx = false; - } if( r_stats ){ boot_counter = 0; @@ -254,9 +171,10 @@ 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 Ivite :: %u\n\r Audio :: %u\n\r Telemetry :: %u\n\r Cb_by :: %u\n\r Prompt :: %u\n\r Flood :: %u\n\r Bootloader_cbx :: %u\n\r", + snprintf( str, 200, "\n\rReceived Pkgs::\n\r Boot :: %u\n\r Registry :: %u\n\r Ivite :: %u\n\r Audio :: %u\n\r Telemetry :: %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_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); send_msg( str ); stats =false; @@ -298,20 +216,45 @@ list = false; } - if( reset_cks == true ){ - pkg_cksok = 0; - pkg_ckserr = 0; - reset_cks = false; - pcks_s = true; - } + if( long_list ){ + //FIXME implementar um algoritmo que me mostre o timeout de cada cbx do lado da linha, desse. + 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() * 2 ); i += 2 ){ + ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); + ext_list[ i + 1 ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_elapsed_time(); + } + + qsort( ext_list, v_cb->size(), sizeof( int ) * 2, ls_comp ); - if( pcks_s == true ){ - send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d :: Out_of_range: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range ); - pcks_s = false; + char aux[ 16 ]; + strcpy( str, "\r\n> " ); + for( i = 0; i < ( v_cb->size() * 2 ) - 2; i++ ){ + sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); + strcat( str, aux ); + strcat( str, "\r\n> " ); + } + sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); + strcat( str, aux ); + send_msg( "%s", str ); + } + list = false; } if( pshowcb == true ){ - // por que nao posso colocar isso como list on ? 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 ); @@ -354,13 +297,39 @@ pshowcb = false; } - if( pflood == true ) flood(); + 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__ ); - + + 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 ){ @@ -379,10 +348,42 @@ 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( ( data[ 0 ] & BIT7 ) == ( cb->get_msg_id() ^ BIT7 ) ){ - // ack from CBx - if( debug_main ) debug_msg("-- ACK do meu pkg --"); - type = __DO_NOTHING__; + }else if( ( data[ 0 ] & BIT7 ) == BIT7 ){ + if( debug_main ) debug_msg("Received ack pkg with seq_num %d", data[ 0 ] ); + //algum motivo pra sempre receber alguma coisa com o type == 7 ??? + if( type == 0x07 ){ + 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; + } + 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->set_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 = __DO_NOTHING__; } } } @@ -390,6 +391,27 @@ }else type = __DO_NOTHING__; } + if( main_test == true ){ + // Call_Box * cb = new Call_Box( 5010, 5010 ); + // v_cb->add( cb ); + // cb = new Call_Box( 5011, 5011 ); + // v_cb->add( cb ); + main_test = false; + send_msg("Missed_Pkg :: %d ::", missed_pkg ); + } + + if( reset_cks == true ){ + pkg_cksok = 0; + pkg_ckserr = 0; + reset_cks = false; + pcks_s = true; + } + + if( pcks_s == true ){ + send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d :: Out_of_range: %d :: Missed_Pkg :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg ); + pcks_s = false; + } + if( led_sync_timer.read() > 1 ){ led_sync_timer.reset(); led3 = !led3; @@ -401,15 +423,19 @@ break; case __CB_BYE__ : { + //FIXME como mandar um BYE pro servidor ? 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_msg( " Request bye from CBx " ); + 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() ); @@ -419,19 +445,21 @@ set_status( cb->status, cb_idle ); set_status( cb->sip->status, sip_idle ); - v_call->remove_element( i ); + v_call->remove_element( i ); + + cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); 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 ); - delete( call ); cb->re_start_timer(); } } + if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed" ); + cb->registry(); - } + }else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext ); }break; case __INVITE__ : { @@ -476,7 +504,7 @@ if( debug_main ) debug_msg("Added CBx -- %d", ext ); } cb->registry(); - //FIXME mandar um pacote do tipo 2 pro cbx + //FIXME mandar um pacote do tipo 2 pro cbx precis telemetry_counter++; if( debug_telemetry ) send_msg("::Telemetry from %d - %d::", ext, port ); build_telemetry_report( ext, port, ( char *)data ); @@ -532,6 +560,7 @@ } }break; }// fim switch + for( register uint8_t i = 0; i < v_call->size(); i++ ){ VZ_call * call = ( VZ_call * )v_call->get_element( i ); int length = 0; @@ -550,13 +579,11 @@ }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( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); + //if( v_call->size() == 0 ) refresh( v_cb, buffer, write_buffer, NULL ); + refresh( v_cb, buffer, write_buffer, NULL ); //Fixme pensar melhor nessa parte durante ligacoes, pode complicar com muitos cbx ... - //testar sem os debugs em on, pode fazer a diferenca - if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); + //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 ); @@ -579,19 +606,26 @@ } } + //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); + cb->set_msg_id( 80 ); + + 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 ) ); - cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); - cb->re_start_timer(); + //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 ); - invite_handler( v_call, v_cb, ts, NULL ); + /* 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++ ){ @@ -601,15 +635,53 @@ } } if( ts_reset ){ - debug_msg(""); + 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 ){ + 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 ){ + debug_wdt = false; + if( eth_status == 0 ){ + send_msg( "Wdt last reset: %s - status_eth :: Connected - Extern Wdt idle for :: %3d sec - Rx from CBx idle for :: %3d sec", + ( wdt.WatchdogCausedReset() ) ? "true" : "false", + EXTERN_WDT_IDLE - external_wdt, + RX_CB_IDLE - pkg_wdt + ); + }else{ + send_msg( "Wdt last reset: %s - status_eth :: Disconnected :: since %3d sec - Extern Wdt idle for :: %3d sec - Rx from CBx idle for :: %3d sec", + ( wdt.WatchdogCausedReset() ) ? "true" : "false", + ETH_CONNECT_TIMEOUT - eth_wdt, + EXTERN_WDT_IDLE - external_wdt, + RX_CB_IDLE - pkg_wdt + ); + } + } } } \ No newline at end of file