voltando a versao de n aberturas e fechamentos de sockets

Dependencies:   EthernetInterface NTPClient mbed-rtos_old mbed

Fork of header_main_public by VZTECH

Revision:
10:22da1a0ac1e1
Parent:
9:ffa64f38ef9c
Child:
12:07fd716e0f35
--- a/main.cpp	Mon Sep 15 17:51:46 2014 +0000
+++ b/main.cpp	Mon Sep 15 20:55:48 2014 +0000
@@ -109,9 +109,9 @@
             
             if( data != NULL ){                
                 if( type == __CB_BYE__ ){
-                    debug_msg("--> %s <--", buffer);
+                    debug_msg( "cbx bye" );
                 }
-                debug_msg("Procurando pelo CBx :: %d -- Type :: %d", ext, type );
+                //debug_msg("Procurando pelo CBx :: %d -- Type :: %d", ext, type );
                 if( type != __AUDIO__ ){
                     Call_Box * cb = __find_CB__( v_cb, ext );
                     if( cb != NULL ){
@@ -138,7 +138,8 @@
             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 );
-                    debug_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 );
+                    /* debug_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 ); */
+                    debug_msg(" Type ::  %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d :: SIP Status -- none", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status );
                 }
                 t.reset();
                 led3 = !led3;
@@ -162,12 +163,18 @@
                             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);
+                            v_call->remove_element( i );
+                            debug_msg("");
+                            if( cb->sip ) set_status( cb->sip->status, sip_idle );
+                            debug_msg("");
                             data[ __TIMESLICE_PLACE__ ] = 0x00;
+                            debug_msg("");
+                            cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) );
+                            debug_msg("");
                             __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 );
+                            debug_msg("");
+                            if( cb->sip ) set_status( cb->sip->status, sip_idle );
                             delete( call );
                         }
                     }
@@ -175,7 +182,7 @@
             }break;
             
             case __INVITE__ : {
-                debug_msg("ok -- recebido o invite pelo menos");
+                debug_msg("ok -- invite received");
                 Call_Box * cb = __find_CB__( v_cb, ext );
                 if( cb == NULL ){
                     debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
@@ -184,7 +191,18 @@
                     set_status( cb->status, cb_idle );
                     cb->registry();
                 }
-                cb->reset_elapsed_time();
+                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 ) );
+                    debug_msg( "-- Negando chamada, uma ja em andamento -- " );    
+                    break;
+                }
                 set_status( cb->status, cb_trying );
                 debug_msg(" Ok temos um cbx encontrado com status :: %d", cb->status );
                 switch( cb->status ){
@@ -200,17 +218,6 @@
                     }break;
                 
                     case cb_trying : {
-                        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 ) );
-                            debug_msg( "-- Negando chamada, uma ja em andamento -- " );    
-                            break;
-                        }
                         data[ 0 ] |= BIT7;
                         cb->set_msg_id( data[ 0 ] );
                         cb->set_timeslice( ts->get_timeslice() );
@@ -220,8 +227,8 @@
                             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);
-                            debug_msg( "-- Trying -- Sem TS -- %s ", write_buffer );
+                            if( cb->sip ) set_status( cb->sip->status, sip_idle);
+                            debug_msg( "-- Trying -- Sem TS -- " );
                         }else{
                             set_status(cb->status,cb_busy);
                             data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
@@ -240,7 +247,7 @@
                                 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 ) );
-                                debug_msg( "-- Trying -- aceitando pedido de invite -- %s ", write_buffer );
+                                debug_msg( "-- Trying -- aceitando pedido de invite -- " );
                                 //debug_pkg( 300, write_buffer );
                             }else{
                                 set_status( cb->status,cb_idle);
@@ -251,7 +258,8 @@
                                 __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, 
                                     ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
                                 debug_msg( "-- Trying -- Negando pedido de invite -- %s ", write_buffer );
-                                set_status( cb->sip->status, sip_idle);
+                                if( cb-> sip) set_status( cb->sip->status, sip_idle);
+                                debug_msg("");
                             }
                         }
                     }break;                    
@@ -322,8 +330,7 @@
                 __send_to_cb__( pkg2cb );
             }
         } 
-        //nessa funcao eu deveria me registar a cada 2 min por exemplo
-        registry_aging( v_cb, buffer, write_buffer );
+        if( v_call->size() ) registry_aging( v_cb, buffer, write_buffer );
         
         int ret = sip_manager( v_cb, v_call, write_buffer );
         if( ret > 0x00 ){
@@ -345,12 +352,12 @@
                     __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, 
                         ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
                     cb->reset_sip();
-                    set_status( cb->sip->status, sip_idle );
+                    if( cb->sip ) set_status( cb->sip->status, sip_idle );
                 }
             }else debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
         }
         
-        call_manager( v_call, v_cb, data, write_buffer, ts );
+        call_manager( v_call, v_cb, buffer, write_buffer, ts );
         
         static uint8_t flag = 0;
         if( v_call->size() == 0 ){
@@ -360,7 +367,7 @@
             if (flag == 1) { flag = 0; }
         }
         /* eu deveria tirar ts que nao constam em chamadas aqui */
-/*        for( register uint8_t i = 0; i < v_cb->size(); i++ ){
+/*         for( register uint8_t i = 0; i < v_cb->size(); i++ ){
             Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
             if( cb->get_timeslice() != 0x00 ){
                 VZ_call * call = __find_Call__( v_call, cb->get_ext() );