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 VZTECH

Revision:
8:e3bfe62a477e
Parent:
7:019b08223b87
Child:
9:ffa64f38ef9c
diff -r 019b08223b87 -r e3bfe62a477e main.cpp
--- a/main.cpp	Sat Sep 13 19:21:48 2014 +0000
+++ b/main.cpp	Mon Sep 15 15:13:52 2014 +0000
@@ -7,8 +7,6 @@
 #include "debug.h"
 #include "utils.h"
 
-//#define debug_telos
-
 /*
 #define set_status(a,b) _set_status(a,b)
 #define _set_status(a,b) { \
@@ -32,7 +30,10 @@
     
     /* referencia e inicio da conexao com a ethernet */
     EthernetInterface eth;
-    __init_eth__( &eth );
+    volatile int eth_status = __init_eth__( &eth );
+    if( eth_status ){
+        debug_msg("Falha ao conectar com a Eth");    
+    }
         
     /* debugs */
 //    debug_server.set_address( "192.168.120.144", 9897 );
@@ -106,24 +107,8 @@
             data = __parse_cb_buffer__( &ext, &port, &type, buffer );
             
             if( data != NULL ){                
-                //if( type == __CB_BYE__ ){
-                //    debug_msg("--> %s <--", buffer);
-                //}
-                //debug_msg("Procurando pelo CBx :: %d -- Type :: %d", ext, type );
+                debug_msg("Procurando pelo CBx :: %d -- Type :: %d", ext, type );
                 if( type != __AUDIO__ ){
-#ifdef debug_telos
-                        debug_msg("");
-                        debug_32( data );
-                        debug_msg("");
-                        __send_to_cb__( buffer );
-                        debug_msg("");
-                        type = __DO_NOTHING__;
-                        debug_msg("");
-                        continue;
-                        debug_msg("");
-                }
-#endif
-#ifndef debug_telos
                     Call_Box * cb = __find_CB__( v_cb, ext );
                     if( cb != NULL ){
                         cb->reset_elapsed_time();
@@ -133,26 +118,24 @@
                                 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 ) );
+                                cb->set_msg_id( 0x11 );
                                 type = __DO_NOTHING__;
                                 debug_msg("-- Renviado ultimo pkg --");
                             }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){
                                 // ack from CBx
                                 debug_msg("-- ACK do meu pkg --");
                                 type = __DO_NOTHING__;
-                                cb->reset_elapsed_time();
-                                if( type == __REGISTRY__ ) cb->set_msg_id( cb->get_msg_id() + 10 );
                             }
                         }
                     }
                 }
-#endif                    
             }else 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 );
-                    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 );
                 }
                 t.reset();
                 led3 = !led3;
@@ -214,17 +197,27 @@
                     }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() );
                         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);
-                            debug_msg( "-- Trying -- Sem TS -- %s ", write_buffer );
+                            debug_msg( "-- Trying -- Sem TS -- " );
                         }else{
                             set_status(cb->status,cb_busy);
                             data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
@@ -243,7 +236,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);
@@ -253,8 +246,9 @@
                                 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 ) );
-                                debug_msg( "-- Trying -- Negando pedido de invite -- %s ", write_buffer );
+                                debug_msg( "-- Trying -- Negando pedido de invite -- " );
                                 set_status( cb->sip->status, sip_idle);
+                                debug_msg( "-- Trying -- Negando pedido de invite -- " );
                             }
                         }
                     }break;                    
@@ -272,7 +266,7 @@
                     cb->reset_elapsed_time();
                     debug_msg(" reset_elapsed_time CBx -- %d", ext );
                 }else{
-                    debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
+                    debug_msg("criando CBx :: %d -- Type :: %d", ext, type );
                     cb = new Call_Box( ext, port );
                     v_cb->add( cb );
                     debug_msg(" Criado CBx -- %d", ext );
@@ -285,7 +279,19 @@
                     ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
                 }break;
             case __TELEMETRY__ : {
-                    debug_msg("Leitura da bateria :: ( Id : %x, MSB : %x, LSB : %x )", data[ 0 ], data[ 1 ], data[ 2 ] );
+                    debug_msg("Ext :: %d ::", ext );
+                    //TODO implementar que quando, a header recebe um pacote de telemetria
+                    //verifica se o call box esta alocado, caso contratio, manda um pacote
+                    //pro cbx pedindo esse registro.
+                    /*
+                    data[ 0 ] |= BIT7 ;
+                    Call_Box * cb = __find_CB__( v_cb, ext );
+                    if( cb == NULL ){
+                        __send_to_cb__( __build_cb_package__( ext, port, __REGISTRY__, 
+                            ( char * )buffer, 0, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
+                    }
+                    debug_msg("fim telemetry");
+                    */
                 }break;
             case __AUDIO__ : {
                 Call_Box * cb;
@@ -297,7 +303,7 @@
                     //debug_pkg( 242, data );
                     //char * pkg = call->build_eth_package( oitocentos );
                     //char * pkg = call->build_eth_package( data );
-                    char * pkg = call->build_eth_package( data + 2 );
+                    char * pkg = call->build_eth_package( data );
                     call->send_message( pkg );
                     cb = __find_CB__( v_cb, ext );
                     if( cb != NULL ) cb->reset_elapsed_time();
@@ -305,8 +311,8 @@
                     debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
                 }
             }break;
-        }// fim switch
-#ifndef debug_telos        
+        }// fim switch 
+        //if( type != __DO_NOTHING__ ) debug_msg( "sai do switch" );
         for( register int i = 0; i < v_call->size(); i++ ){
             VZ_call * call = ( VZ_call * )v_call->get_element( i );
             int length = 0;
@@ -326,9 +332,7 @@
                 __send_to_cb__( pkg2cb );
             }
         } 
-#endif        
-        //nessa funcao eu deveria me registar a cada 2 min por exemplo
-        registry_aging( v_cb, data, write_buffer );
+        registry_aging( v_cb, buffer, write_buffer );
         
         int ret = sip_manager( v_cb, v_call, write_buffer );
         if( ret > 0x00 ){
@@ -347,6 +351,7 @@
                             delete( call );
                         }
                     }
+                    cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) );
                     __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();
@@ -355,7 +360,7 @@
             }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 ){