![](/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:
- 99:e80850c51106
- Parent:
- 98:43b45f26b430
- Child:
- 100:09a23fcd3bdf
diff -r 43b45f26b430 -r e80850c51106 main.cpp --- a/main.cpp Sun Jan 11 03:30:47 2015 +0000 +++ b/main.cpp Mon Jan 19 18:00:42 2015 +0000 @@ -14,6 +14,7 @@ #include "prompt.h" #include "configs.h" #include "sdram.h" +#include "fw.h" volatile u16_t lpc_low_level_input_counter = 0; @@ -128,7 +129,12 @@ udp_timer.start(); init_telemetry_handler(); - + + //debug_msg(""); + init_fw_handler(); + //debug_msg(""); + + led2 = 0; init_ranges(); init_refresh(); @@ -153,17 +159,21 @@ 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; + //bool invite_retry_time = false; + Timer invite_retry_timer; + invite_retry_timer.start(); /*------------------------------------------ main loop ---------------------------------------------------------------*/ while( true ) { + reset_leds(); + if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size(); if( sync_timer.read() > 5 ) { @@ -209,14 +219,15 @@ 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_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 ); + 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; } @@ -387,10 +398,10 @@ if( show_sip == true ){ show_sip = false; - debug_msg(":: Sip :: "); + 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 ); - debug_msg("sip ext :: %d -- sip port :: %d", cb->get_sip_ext(), cb->get_sip_port() ); + send_msg("ext :: %d -- port :: %d -- timer %d", cb->get_sip_ext(), cb->get_sip_port(), cb->get_timer() ); } } @@ -458,10 +469,29 @@ if( ext > max_ext ) max_ext = ext; if( ext < min_ext ) min_ext = ext; - - if( debug_cb_rx == true ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type ); + + 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 ) { @@ -589,8 +619,27 @@ // Ckecking and set 'alive' bool question_alive = ( wake_msg[ 5 ] == '?' ); if( wake_msg[ 5 ] == '*' ) wdt.kick(); - - 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() ); + + /* + 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 ) ); @@ -611,12 +660,19 @@ if( reset_cks == true ) { pkg_cksok = 0; pkg_ckserr = 0; + pkg_zero = 0; reset_cks = false; + out_of_range = 0; + missed_pkg = 0; + droped_pkg_to_cb = 0; + cpld_pkg_tx_counter = 0; + cpld_pkg_rx_counter = 0; 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 :: CPLD_RCV :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg, received_cpld_pkg ); + send_msg("PKG_CKS OK: %d :: PKG_CKS ERR: %d :: PKG_0: %d :: Out_of_range: %d :: Miss_Pkg :: %d" + "\n\rTX_MISS :: %d :: TX_Counter :: %d :: RX_Counter :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg, droped_pkg_to_cb, cpld_pkg_tx_counter, cpld_pkg_rx_counter ); pcks_s = false; } @@ -673,7 +729,7 @@ cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); delete( call ); - cb->re_start_timer(); + //cb->re_start_timer(); } } if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from vector call" ); @@ -700,7 +756,7 @@ //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); - cb->re_start_timer(); + //cb->re_start_timer(); } } } @@ -764,38 +820,25 @@ 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__ : { - /* - 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(); - } - */ - telemetry_counter++; 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++; @@ -853,7 +896,7 @@ call->cbx_pkg_idle_timer_reset(); Call_Box * cb = __find_CB__( v_cb, ext ); if( cb != NULL ){ - cb->reset_elapsed_time(); + //cb->reset_elapsed_time(); cb->set_invite_response_ok(); cb->invite_retry_count_reset(); } @@ -864,46 +907,62 @@ break; }// fim switch - { - if( invite_retry_time == true ){ - invite_retry_time = false; + { // 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; - - 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", cb->get_ext() ); + + 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( cb->get_invite_retry_count() == 0 ){ - cb->send_bye(); - - ts->return_timeslice( cb->get_timeslice() ); - cb->set_timeslice( 0x00 ); + } + + 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(); - //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; + 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 ); } - - if( debug_invite ) debug_msg( "-- No audio pkgs --" ); - set_status( cb->status, cb_idle ); - set_status( cb->sip->status, sip_idle ); } } } @@ -922,15 +981,20 @@ uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, tmp, __AUDIO__, length, write_buffer ); - - send2callboxes( pkg2cb ); + + //static uint8_t valerioduto = 0; + //if( valerioduto++ == 2 ){ + // valerioduto = 0; + 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() > 500 ) { + if( timer_sync_refresh.read_ms() > 250 ) { timer_sync_refresh.reset(); static uint8_t mode = TIME_TO_REFRESH; @@ -980,7 +1044,7 @@ ( 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 ); } @@ -1022,7 +1086,7 @@ if( wdt_timer.read() >= 1 ) { //FIXME remove myself //registry_test = true; - invite_retry_time = true; + //invite_retry_time = true; uptime++;