![](/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:
- 78:1353744f01e1
- Parent:
- 77:e8c0253b57bc
- Child:
- 79:9bc12aa305a9
--- a/main.cpp Tue Nov 25 18:43:01 2014 +0000 +++ b/main.cpp Tue Dec 16 12:13:01 2014 +0000 @@ -14,34 +14,29 @@ #include "prompt.h" #include "configs.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__(); - + short int eth_status = __init_eth__(); if( eth_status ){ send_msg("Cannot connect to eth\n\r"); }else{ send_msg("Connection eth - ok"); - init_prompt(); + init_prompt_eth(); } - Timer t; - t.start(); - //Timer udp_timer; + + 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; + int port = 0; + /* buffer para onde se copia os dados vindos do cb para tratameno interno */ uint8_t buffer[ __CB_BUFFER_SIZE__ ]; @@ -68,61 +63,154 @@ /* 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 ){ + if( sync_timer.read() > 5 ){ send_msg("Erro ao alocar o vetor de CBx"); - t.reset(); + sync_timer.reset(); } } }else if( v_call == NULL ){ - led2 = 0; - led1 = 1; while( v_call == NULL ){ Vector * v_call = new Vector(); - if( t.read() > 5 ){ + if( sync_timer.read() > 5 ){ send_msg("Erro ao alocar o vetor de Calls"); - t.reset(); + sync_timer.reset(); } } } + reset_leds(); + 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(); + init_aging(); + static uint8_t count = 0; - int bl_ret = init_bl_handler(); + + short int bl_ret = init_bl_handler(); if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); - uint8_t before = t.read(); + //uint8_t before = ( int ) sync_timer.read() % 2; + + uint8_t max_registered_cbx = 0; + + //bool wake_all = false; send_msg("Ready"); - /*------------------------------------------------ main loop -------------------------------------------------------------------------*/ + /*------------------------------------------ main loop ---------------------------------------------------------------*/ while( true ){ + 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 ){ + send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d", pkg_cksok, pkg_ckserr, pkg_zero ); + } + + 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 ); + } + } + + count++; + if( !( count % 15 ) ){ + if( eth_status ){ + wdt.kick(); + 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(); + } + } + } + if( !( count % 6 ) ){ + //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( 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 ) % 20 ) ) strcat( str, "\n\r " ); + if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); else strcat( str, " " ); } @@ -136,7 +224,7 @@ 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 " ); + if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); else strcat( str, " " ); } @@ -166,30 +254,33 @@ } 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{ + 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[ 128 ]; - Call_Box * cb = NULL; + int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; register int i = 0; for( ; i < v_cb->size(); i++ ){ - cb = ( Call_Box * )v_cb->get_element( i ); - ext_list[ i ] = cb->get_ext(); + 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 " ); + 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 " ); + if( ( i != 0 ) && !( ( i + 1 ) % 16 ) ) strcat( str, "\r\n> " ); } sprintf( aux, "%i ", ext_list[ i ] ); strcat( str, aux ); @@ -198,80 +289,88 @@ 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 == true ){ + pkg_cksok = 0; + pkg_ckserr = 0; + reset_cks = false; + pcks_s = true; } - 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( pcks_s == true ){ + send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d", pkg_cksok, pkg_ckserr, pkg_zero ); + pcks_s = false; } - if (pshowcb == 1){ - send_msg("Registred %d CBx", v_cb->size() ); + 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 ); - 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 ); + 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 = 0; + pshowcb = false; } - /* - 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( pflood == true ) 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__; - __read_cb_buffer__( buffer, cb_rx_buffer ); + xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); 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( 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_cb_rx == true ) 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__; - send2callboxes( __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 ) ){ + if( ( data[ 0 ] & BIT7 ) >= 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( ( data[ 0 ] & BIT7 ) == ( cb->get_msg_id() ^ BIT7 ) ){ // ack from CBx if( debug_main ) debug_msg("-- ACK do meu pkg --"); type = __DO_NOTHING__; @@ -281,89 +380,13 @@ } }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++; - 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;*/ - Call_Box * cb = new Call_Box( ext, port ); - if( cb != NULL ){ - v_cb->add( cb ); - cb->set_timeslice( ts->get_timeslice() ); - VZ_call * call = new VZ_call( ext, port, 913, 10112 ); - v_call->add( call ); - } - } - - /* - if( count == 5 * 2 ){ - //debug_msg("Colocar simulacoes de cbx aqui"); - data = buffer; - ext = 891; - port = 5001; - type = __INVITE__; - debug_msg("registro enviado"); - }*/ - } -//---------------------- + + if( led_sync_timer.read() > 1 ){ + led_sync_timer.reset(); + led3 = !led3; + CAB_LED = !CAB_LED; + } + switch( type ){ case __DO_NOTHING__ :{} break; @@ -379,152 +402,73 @@ 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 ); + data[ __TIMESLICE_PLACE__ ] = 0x00; + set_status( cb->status, cb_idle ); + set_status( cb->sip->status, sip_idle ); + v_call->remove_element( i ); - set_status( cb->sip->status, sip_idle); - data[ __TIMESLICE_PLACE__ ] = 0x00; + send2callboxes( __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 ); + + cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); + delete( call ); cb->re_start_timer(); } } + cb->registry(); } }break; case __INVITE__ : { invite_counter++; - if( debug_main ) debug_msg("Request Invite received from Cbx %i", ext); - /*procura pelo cbx usando como chave o ramal( ext ) - caso nao encontre, cria o objeto e coloca na logica*/ + if( debug_invite ) 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 ); + 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(); - /* restricao de que so podemos fazer 1 call por vez */ - /* - 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; - send2callboxes( __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; - //send2callboxes( __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; - send2callboxes( __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(); - send2callboxes( __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__ ] ); - send2callboxes( __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__ ] ); - send2callboxes( __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__ ] ); - send2callboxes( __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; - } + cb->registry(); + cb->set_msg_id( data[ 0 ] ); + invite_handler( v_call, v_cb, ts, cb ); }break; case __REGISTRY__ : { registry_counter++; - Call_Box * cb = NULL; + Call_Box * cb = __find_CB__( v_cb, ext ); - 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 ); + if( cb == NULL ){ + if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); 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 ); + if( debug_main ) debug_msg("Registered %d - %d", ext, port ); + cb->registry(); + }break; case __BOOT__ : { boot_counter++; send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, - ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); + ( 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 ){ + if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); + cb = new Call_Box( ext, port ); + v_cb->add( cb ); + if( debug_main ) debug_msg("Added CBx -- %d", ext ); + } + cb->registry(); telemetry_counter++; - if( debug_telemetry ) send_msg("::telemetry from %d - %d::", ext, port ); + if( debug_telemetry ) send_msg("::Telemetry from %d - %d::", ext, port ); build_telemetry_report( ext, port, ( char *)data ); }break; @@ -534,106 +478,124 @@ }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 ) ); + Call_Box * cb = __find_CB__( v_cb, ext ); + if( cb == NULL ){ + if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); + cb = new Call_Box( ext, port ); + v_cb->add( cb ); + if( debug_main ) debug_msg("Added CBx -- %d", ext ); } - */ - for( register int i = 0; i < 32; i++ ){ - pc.printf("%3i ", data[ i ] ); - if( i == 15 ) pc.printf( "\r\n" ); - } - pc.printf("\n\r> "); + + cb->registry(); - 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> " ) ); + 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++ ){ + pc.printf("%c", 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 ); + VZ_call * 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 ); + Call_Box * 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 ); + if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); } }break; }// fim switch - for( register int i = 0; i < v_call->size(); i++ ){ + 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 ){ - Call_Box * cb = NULL; int cb_port = 0xffff; - cb = __find_CB__( v_cb, call->get_cb_ext() ); + Call_Box * 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__, + + uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, tmp, __AUDIO__, length, write_buffer ); - send2callboxes( pkg2cb ); + send2callboxes( pkg2cb ); + }else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); } } + //if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); + + //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 ); - int ret = sip_manager( v_cb ); - if( ret > 0x00 ){ - Call_Box * cb = __find_CB__( v_cb, ret ); + // 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 ); - set_status(cb->status,cb_idle); buffer[ __TIMESLICE_PLACE__ ] = 0x00; - cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); + + 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() == ret ){ + if( call->get_cb_ext() == ext_to__be_removed ){ v_call->remove_element( i ); delete( call ); } } + send2callboxes( __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->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); + cb->re_start_timer(); } - }else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d -- Type :: %d", ext, type ); + }else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d", ext ); } 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; } + invite_handler( v_call, v_cb, ts, NULL ); + + 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 ){ + ts_reset = false; + break; + } + } + if( ts_reset ){ + debug_msg(""); + ts->reset(); + } } - */ - + bl_handler(); + tx_buffer_ring_buffer_handler(); type = __DO_NOTHING__;