![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
voltando a versao de n aberturas e fechamentos de sockets data 19/09
Dependencies: EthernetInterface NTPClient mbed-rtos mbed EALib
Fork of header_main_publish by
Diff: main.cpp
- Revision:
- 2:93bec7313ccc
- Parent:
- 1:a1758104fa1d
- Child:
- 3:cd9148672e25
diff -r a1758104fa1d -r 93bec7313ccc main.cpp --- a/main.cpp Wed Sep 10 12:50:57 2014 +0000 +++ b/main.cpp Wed Sep 10 14:32:19 2014 +0000 @@ -7,9 +7,6 @@ #include "debug.h" #include "utils.h" -// for aditional debugs -#define on_work - /* #define set_status(a,b) _set_status(a,b) #define _set_status(a,b) { \ @@ -36,12 +33,11 @@ __init_eth__( ð ); /* debugs */ - debug_server.set_address( "192.168.120.144", 9897 ); - debug.bind( 72133 ); - debug.init(); +// debug_server.set_address( "192.168.120.144", 9897 ); +// debug.bind( 72133 ); +// debug.init(); Timer t; - Timer reboot; t.start(); /* representa ramal do call box */ @@ -99,36 +95,8 @@ } } reset_leds(); - reboot.start(); debug_msg( " Ready " ); - /* - char test_buffer[400]; - UDPSocket test_sock; - Endpoint test_server; - test_server.set_address( "192.168.120.144" , 12345 ); - test_sock.set_blocking( false, 1 ); - test_sock.bind( 78965 ); - test_sock.init(); - int n = 0; - strcpy( test_buffer, "a"); - test_sock.sendTo( test_server, test_buffer, sizeof( test_buffer ) ); - Timer reconnect; - reconnect.start(); - */ while( true ){ - /* - n = test_sock.receiveFrom( test_server, test_buffer, sizeof( test_buffer ) ); - if( n > 0 ){ - test_sock.sendTo( test_server, test_buffer, n ); - reconnect.reset(); - }else if( reconnect.read() > 1 ){ - test_sock.close(); - test_sock.bind( 78965 ); - test_sock.init(); - reconnect.reset(); - test_sock.sendTo( test_server, test_buffer, n ); - led4 = !led4; - }*/ if( status != __WAITING__ ){ __read_cb_buffer__( buffer, buffer_from_cb_ptr ); status = __WAITING__; @@ -164,7 +132,8 @@ if( data == NULL ) type = __DO_NOTHING__; */ } -//---------------------- +//---------------------- +/* if( t.read() > 5 ){ for( register uint8_t i = 0; i < v_cb->size(); i++ ){ Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); @@ -173,22 +142,7 @@ t.reset(); led3 = !led3; } - if( reboot.read() > 180 ){ - for( register uint8_t i = 0; i < v_cb->size(); i++ ){ - Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); - cb->set_timeslice( 0x00 ); - set_status(cb->status,cb_idle); - set_status( cb->sip->status, sip_idle ); - debug_msg( "Erased >> Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status ); - } - for( register uint8_t i = 0; i < v_call->size(); i++ ){ - VZ_call * call = ( VZ_call * )v_call->get_element( i ); - v_call->remove_element( i ); - delete( call ); - } - reboot.reset(); - led4 = !led4; - } +*/ //---------------------- switch( type ){ case __DO_NOTHING__ :{} @@ -220,73 +174,6 @@ case __INVITE__ : { debug_msg("ok -- recebido o invite pelo menos"); - #ifndef on_work - // invite de uma ligacao em andamento ? - Call_Box * cb = __find_CB__( v_cb, ext ); - if( cb != NULL ){ - cb->reset_elapsed_time(); - if( cb->status == cb_on_call ){ - VZ_call * vz = __find_Call__( v_call, cb->get_ext() ); - if( vz != NULL ) break; - ts->return_timeslice( cb->get_timeslice() ); - cb->set_timeslice( 0x00 ); - set_status(cb->status,cb_idle); - } - }else{ - cb = new Call_Box( ext, port ); - v_cb->add( cb ); - } - if( cb->status != cb_on_call ) set_status(cb->status,cb_ringing); - if( data[ __TIMESLICE_PLACE__ ] == __CB_BYE__ ){ - if( cb->status == cb_on_call ){ - for( register uint8_t i = 0; i < v_call->size(); i++ ){ - if( ( (VZ_call *)v_call->get_element( i ) )->get_cb_ext() == ext ){ - v_call->remove_element( i ); - ts->return_timeslice( cb->get_timeslice() ); - cb->set_timeslice( 0x00 ); - set_status(cb->status,cb_idle); - cb->send_bye(); - break; - } - } - } - } - if( cb->status == cb_ringing ){ - data[ 0 ] |= BIT7; - cb->set_msg_id( data[ 0 ] ); - cb->set_timeslice( ts->get_timeslice() ); - if( cb->get_timeslice() == 0x00 ){ - set_status(cb->status,cb_idle); - data[ __CLOCK_SYNC_SIZE__ ] = 0x00; - __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, - ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - break; - } - if( cb->status == cb_ringing ){ - data[ __CLOCK_SYNC_SIZE__ ] = 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 ) ); - - VZ_call * call = cb->invite(); - - if( call != NULL ){ - v_call->add( call ); - set_status(cb->status,cb_on_call); - data[ __CLOCK_SYNC_SIZE__ ] = cb->get_timeslice(); - __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, - ( char * )data, cb->get_msg_id()+1, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - }else{ - set_status(cb->status,cb_idle); - ts->return_timeslice( cb->get_timeslice() ); - data[ __CLOCK_SYNC_SIZE__ ] = 0x00; - __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, - ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); - } - } - } - }break; - #endif - #ifdef on_work Call_Box * cb = __find_CB__( v_cb, ext ); if( cb == NULL ){ cb = new Call_Box( ext, port ); @@ -360,19 +247,26 @@ }break; } }break; - #endif case __REGISTRY__ : { - Call_Box * cb; + Call_Box * cb_master; + Call_Box * cb_slave; + data[ 0 ] |= BIT7; - cb = __find_CB__( v_cb, ext ); - if( cb != NULL ){ - cb->reset_elapsed_time(); + cb_master = __find_CB__( v_cb, ext ); + cb_slave = __find_CB__( v_cb, ext+1 ); + if( cb_master != NULL ){ + cb_master->reset_elapsed_time(); + }else if( cb_slave != NULL ){ + cb_slave->reset_elapsed_time(); }else{ - cb = new Call_Box( ext, port ); - v_cb->add( cb ); + cb_master = new Call_Box( ext, port ); + cb_slave = new Call_Box( ext + 1, port + 1 ); + v_cb->add( cb_master ); + v_cb->add( cb_slave ); } - cb->registry(); + cb_master->registry(); + cb_slave->registry(); }break; case __BOOT__ : __send_to_cb__( __build_cb_package__( ext, port, __REGISTRY__, @@ -425,7 +319,10 @@ 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 ); - v_call->remove_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 ) );