![](/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:
- 2014-11-13
- Revision:
- 70:714c33487aae
- Parent:
- 69:65665afbad5d
- Child:
- 71:757fa1ef37c6
File content as of revision 70:714c33487aae:
#include "bits.h" #include "utils.h" #include "configs.h" #include "UART3Interrupt.h" #include "parallelcpld.h" #include "prompt.h" #include "debug.h" #include "utils.h" #include "flood.h" #include "bootloader_cbx_handler.h" volatile uint8_t status = 0x00; int main(){ wdt.Configure( 40.0 ); start_cpld(); config_lpc(); init_fsystem(); //FIXMEj pensar melhor em como saber se estou conectado na eth //int eth_status = __init_eth__( ð ); int eth_status = __init_eth__(); if( eth_status ){ send_msg("Cannot connect to eth\n\r"); }else{ send_msg("Connection eth - ok"); init_prompt(); } Timer t; t.start(); //Timer udp_timer; /* 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 reconhecidos 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(); /* tenta instanciar o vetor de call boxes, acende o led 1 antes de encerrar o programa */ reset_leds(); if( v_cb == NULL ){ led1 = 0; while( v_cb == NULL ){ Vector * v_cb = new Vector(); led1 = led1; if( t.read() > 5 ){ send_msg("Erro ao alocar o vetor de CBx"); t.reset(); } } }else if( v_call == NULL ){ led2 = 0; led1 = 1; while( v_call == NULL ){ Vector * v_call = new Vector(); if( t.read() > 5 ){ send_msg("Erro ao alocar o vetor de Calls"); t.reset(); } } } 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(); static uint8_t count = 0; int bl_ret = init_bl_handler(); if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); uint8_t before = t.read(); send_msg("Ready"); /*------------------------------------------------ main loop -------------------------------------------------------------------------*/ while( true ){ //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... prompt_process( NULL ); wdt.kick(); 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 ) % 20 ) ) 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 ) % 20 ) ) strcat( str, "\n\r " ); else strcat( str, " " ); } send_msg( "%s", str ); tx = false; } 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; 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", 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; } if( list ){ send_msg("Registred %d CBx", v_cb->size() ); if( v_cb->size() == 0 ){ send_msg("{-/-}"); }else if( v_cb->size() == 1 ){ Call_Box * cb = ( Call_Box * )v_cb->get_element( 0 ); send_msg(" %d ", cb->get_ext() ); }else{ char str[ 1024 ]; int ext_list[ 128 ]; Call_Box * cb = NULL; register int i = 0; for( ; i < v_cb->size(); i++ ){ cb = ( Call_Box * )v_cb->get_element( i ); ext_list[ i ] = cb->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 ) % 8 ) ) strcat( str, "\r\n " ); } sprintf( aux, "%i ", ext_list[ i ] ); strcat( str, aux ); send_msg( "%s", str ); } list = false; } if (pcks_s == 1){ send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); pcks_s =0; } if( reset_cks ){ pkg_cksok = 0; pkg_ckserr = 0; send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); reset_cks = false; } if (pshowcb == 1){ send_msg("Registred %d CBx", 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("Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d :: SIP Status %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status, cb->sip->status ); } pshowcb = 0; } /* if( cb_status ){ cb_status = false; send_msg("Registred %d CBx", v_cb->size() ); char str[ 1024 ]; if( v_cb->size() == 0 ){ send_msg("{-/-}"); }else if( v_cb->size() == 1 ){ Call_Box * cb = ( Call_Box * )v_cb->get_element( 0 ); send_msg(" %i ", cb->get_ext() ); }else{ register uint8_t i; char aux[ 16 ]; Call_Box * cb = NULL; for( i = 0; i < v_cb->size() - 1; i++ ){ cb = ( Call_Box * )v_cb->get_element( i ); sprintf( aux, "%i, ", cb->get_ext() ); strcat( str, aux ); if( ( i != 0 ) && !( ( i + 1 ) % 8 ) ) strcat( str, "\n\r " ); } cb = ( Call_Box * )v_cb->get_element( i ); sprintf( aux, "%i ", cb->get_ext() ); strcat( str, aux ); send_msg( "%s", str ); } } */ if( pflood == 1 ) flood(); if( status != __WAITING__ ){ xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); __read_cb_buffer__( buffer, buffer_from_cb_ptr ); status = __WAITING__; data = __parse_vz_pkg__( &ext, &port, &type, buffer ); if( data != NULL ){ if( debug_alive == 1 && debug_cb_rx ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type ); if( type != __AUDIO__ ){ Call_Box * cb = __find_CB__( v_cb, ext ); if( cb != NULL ){ cb->reset_elapsed_time(); if( ( data[ 0 ] ^ cb->get_msg_id() ) == BIT7 ){ // some pkg reply if( ( data[ 0 ] | BIT7 ) == cb->get_msg_id() ){ if( type == __BOOT__ ) type = __REGISTRY__; __send_to_cb__( __build_cb_package__( ext, port, type, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); type = __DO_NOTHING__; if( debug_main ) debug_msg("-- Renviado ultimo pkg --"); }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){ // ack from CBx if( debug_main ) debug_msg("-- ACK do meu pkg --"); type = __DO_NOTHING__; } } } } }else type = __DO_NOTHING__; } //---------------------- uint8_t now = t.read(); if( before != now ){ led3 = !led3; before = now; } if( t.read() > 5 ){ //static int test_ext = 5010; //static int test_port = 5010; //send_msg("from_eth = %s - tcp_session = %s", ( from_eth ) ? "true" : "false", ( tcp_session ) ? "true" : "false" ); if( debug_alive ){ send_msg("Registred %d CBx", 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("Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d :: SIP Status %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status, cb->sip->status ); } } /* if( debug_alive == 1 ){ if( debug_main ) send_msg("Status - alive "); //uint32_t errper = 100*(pkg_ckserr/(pkg_ckserr + pkg_cksok)); //pc.printf("\n\r Error = %d", errper); } */ t.reset(); CAB_LED = !CAB_LED; if( debug_cks ) send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); //<><><><><><><><><><> count++; if( !( count % 15 ) ){ if( eth_status ){ wdt.kick(); //eth_status = __init_eth__( ð ); 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(); } } } if( main_test ){ data = buffer; //ext = test_ext++ + ( ( test_ext % 2 ) ? 100 : 50 ); //port = test_port++; //type = __REGISTRY__; type = __TELEMETRY__; ext = 5108; port = 5008; uint8_t var = 0xAA; for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__; i++ ){ data[ i ] = var++; if( var == 0xFF ) var = 0xAA; } for( register uint16_t i = __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__; i < __CB_BUFFER_SIZE__; i++ ){ data[ i ] = 0x00; } main_test = false; } /* if( count == 5 * 2 ){ //debug_msg("Colocar simulacoes de cbx aqui"); data = buffer; ext = 891; port = 5001; type = __INVITE__; debug_msg("registro enviado"); }*/ } //---------------------- switch( type ){ case __DO_NOTHING__ :{} break; case __CB_BYE__ : { cb_bye_counter++; Call_Box * cb = __find_CB__( v_cb, ext ); if( cb != NULL ){ data[ 0 ] |= BIT7; cb->set_msg_id( data[ 0 ] ); if( debug_main ) debug_msg( " Request bye from CBx " ); 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 ){ cb->send_bye(); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); set_status( cb->status, cb_idle ); v_call->remove_element( i ); set_status( cb->sip->status, sip_idle); data[ __TIMESLICE_PLACE__ ] = 0x00; __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); set_status( cb->sip->status, sip_idle ); delete( call ); cb->re_start_timer(); } } } }break; case __INVITE__ : { invite_counter++; if( debug_main ) debug_msg("Request Invite received from Cbx %i", ext); Call_Box * cb = __find_CB__( v_cb, ext ); if( cb == NULL ){ if( debug_main ) debug_msg("Adding CBx :: %i", ext ); cb = new Call_Box( ext, port ); v_cb->add( cb ); set_status( cb->status, cb_idle ); cb->registry(); } cb->reset_elapsed_time(); if( v_call->size() != 0 ){ data[ 0 ] |= BIT7; cb->set_msg_id( data[ 0 ] ); set_status(cb->status,cb_idle); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg( "denying call - another call box on call" ); break; } set_status( cb->status, cb_trying ); if( debug_main ) debug_msg("Request invite cbx status :: %d", cb->status ); //FIXMEj essa logica deve estar la embaixo, fora do switch maior, a ideia eh // quando receber um pedido de invite, apenas manda o invite pro server // dai rodar, ver se alguem mandou alguma coisa e voltar a tratar com o // * ver se ja esta tudo ok, a ideia é fazer esse pedido de invite funcionar // assincrono. switch( cb->status ){ case cb_on_call : { // a priori, nao fazer nada //data[ __TIMESLICE_PLACE__ ] = 0x00; //__send_to_cb__( __build_cb_package__( ext, port, __BYE__, // ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); }break; case cb_ringing : { // tratar sip 2 }break; case cb_trying : { data[ 0 ] |= BIT7; cb->set_msg_id( data[ 0 ] ); cb->set_timeslice( ts->get_timeslice() ); if( cb->get_timeslice() == 0x00 ){ //sem timeslice pra tratar com o * set_status(cb->status,cb_idle); data[ __TIMESLICE_PLACE__ ] = 0x00; __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); set_status( cb->sip->status, sip_idle); if( debug_main ) debug_msg( "-- Trying -- without TS -- %s ", write_buffer ); }else{ set_status(cb->status,cb_busy); data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); VZ_call * call = cb->invite(); if( call != NULL ){ v_call->add( call ); set_status(cb->status,cb_on_call); if( debug_main ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg( "-- Trying -- accepting call request --" ); }else{ set_status( cb->status,cb_idle); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; if( debug_main ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg( "-- Trying -- denying call request --" ); set_status( cb->sip->status, sip_idle); } } }break; case cb_busy : { set_status( cb->status,cb_idle); ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); data[ __TIMESLICE_PLACE__ ] = 0x00; if( debug_main ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); if( debug_main ) debug_msg( "-- Trying -- denying call request << Busy Here >> --" ); set_status( cb->sip->status, sip_idle ); }break; } }break; case __REGISTRY__ : { registry_counter++; Call_Box * cb = NULL; data[ 0 ] |= BIT7 ; cb = __find_CB__( v_cb, ext ); if( cb != NULL ){ cb->reset_elapsed_time(); if( debug_main ) debug_msg(" reset_elapsed_time CBx -- %d", ext ); }else{ if( debug_main ) debug_msg("Adding Cbx :: %d -- Type :: %d", ext, type ); cb = new Call_Box( ext, port ); v_cb->add( cb ); if( debug_main ) debug_msg("Added CBx -- %d", ext ); } if( debug_main ) debug_msg("::registered %d - %d::", ext, port ); cb->registry(); }break; case __BOOT__ : { boot_counter++; __send_to_cb__( __build_cb_package__( ext, port, __REGISTRY__, ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); }break; case __TELEMETRY__ : { telemetry_counter++; if( debug_telemetry ) send_msg("::telemetry from %d - %d::", ext, port ); build_telemetry_report( ext, port, ( char *)data ); }break; case __BOOTLOADER_CBX__ : { bootloader_cbx_counter++; int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); }break; case __PROMPT__ : { prompt_counter++; send_msg( "Prompt pkg from ( %i, %i ) ::", ext, port ); /* if( tcp_session ){ // udp ? char tmp[ 255 ]; sprintf( tmp, "Prompt pkg from ( %i, %i ) :: ", ext, port ); tcp_client.send_all( tmp, strlen( tmp ) ); } */ for( register int i = 0; i < 32; i++ ){ pc.printf("%3i ", data[ i ] ); if( i == 15 ) pc.printf( "\r\n" ); } 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++; Call_Box * cb; VZ_call * call = NULL; cb = NULL; call = __find_Call__( v_call, ext ); if( call != NULL ){ char * pkg = call->build_eth_package( data + 2 ); call->send_message( pkg ); cb = __find_CB__( v_cb, ext ); if( cb != NULL ) cb->reset_elapsed_time(); }else{ if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); } }break; }// fim switch for( register int 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 ){ Call_Box * cb = NULL; int cb_port = 0xffff; cb = __find_CB__( v_cb, call->get_cb_ext() ); if( cb != NULL ){ cb_port = cb->get_port(); }else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, tmp, __AUDIO__, length, write_buffer ); __send_to_cb__( pkg2cb ); } } if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); int ret = sip_manager( v_cb ); if( ret > 0x00 ){ Call_Box * cb = __find_CB__( v_cb, ret ); if( cb != NULL ){ if( cb->status == cb_on_call ){ ts->return_timeslice( cb->get_timeslice() ); cb->set_timeslice( 0x00 ); set_status(cb->status,cb_idle); buffer[ __TIMESLICE_PLACE__ ] = 0x00; cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); 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() == ret ){ v_call->remove_element( i ); delete( call ); } } __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); //FIXMEj talvez eu nao precise dessa linha mudar o set_status( cb->sip->status, sip_idle ); pra ver o que acontece //cb->reset_sip(); set_status( cb->sip->status, sip_idle ); cb->re_start_timer(); } }else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d -- Type :: %d", ext, type ); } call_manager( v_call, v_cb, buffer, write_buffer, ts ); static uint8_t flag = 0; if( v_call->size() == 0 ){ if (flag == 0) { flag = 1; } ts->reset(); }else{ if (flag == 1) { flag = 0; } } bl_handler(); type = __DO_NOTHING__; } }