Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
30:8dfb6d8de53d
Parent:
29:7246460b73f8
Child:
31:bb5fb28a77df
diff -r 7246460b73f8 -r 8dfb6d8de53d main.cpp
--- a/main.cpp	Wed Sep 24 18:02:18 2014 +0000
+++ b/main.cpp	Thu Sep 25 14:37:02 2014 +0000
@@ -9,19 +9,6 @@
 #include "utils.h"
 #include "flood.h"
 
-/*
-#define set_status(a,b) _set_status(a,b)
-#define _set_status(a,b) { \
- a = b; \
- sprintf( debug_msg, "-- State == %d -- Line %d", a, __LINE__ ); \
- debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \
-}
-#define whereami() _whereami()
-#define _whereami() { \
- sprintf( debug_msg, "-- whereami %d", __LINE__ ); \
- debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \
-}
-*/
 volatile uint8_t status = 0x00;
 
 int main(){
@@ -32,18 +19,13 @@
     EthernetInterface eth;
     
     int eth_status = __init_eth__( &eth );
-    
-    
+        
     if( eth_status ){
         debug_msg("Sem conexao eth");    
     }
-        
-    /* debugs */
-//    debug_server.set_address( "192.168.120.144", 9897 );
-//    debug.bind( 72133 );
-//    debug.init();
     
     Timer t;
+    Timer flood_timer;
     t.start();
           
     /* representa ramal do call box */
@@ -115,15 +97,14 @@
         
         if (pshowcb == 1){
             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 );
-            pshowcb = 0;
+                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 );
+                pshowcb = 0;
             }
         }
         
-        if (pflood == 1) flood();
-               
-            
+        if( pflood == 1 ) flood();               
+        
         if( status != __WAITING__ ){
             __read_cb_buffer__( buffer, buffer_from_cb_ptr );     
             status = __WAITING__;
@@ -131,7 +112,7 @@
             data = __parse_cb_buffer__( &ext, &port, &type, buffer );
             
             if( data != NULL ){                
-                if (debug_alive == 1) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type );
+                if( debug_alive == 1 ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type );
                 if( type != __AUDIO__ ){
                     Call_Box * cb = __find_CB__( v_cb, ext );
                     if( cb != NULL ){
@@ -143,10 +124,10 @@
                                 __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__;
-                                debug_msg("-- Renviado ultimo pkg --");
+                                if( debug_main ) debug_msg("-- Renviado ultimo pkg --");
                             }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){
                                 // ack from CBx
-                                debug_msg("-- ACK do meu pkg --");
+                                if( debug_main ) debug_msg("-- ACK do meu pkg --");
                                 type = __DO_NOTHING__;
                             }
                         }
@@ -156,32 +137,32 @@
         }
 //----------------------
             if( t.read() > 5 ){
-                //static int count = 0;            
+                static int count = 0;            
                 for( register uint8_t i = 0; i < v_cb->size(); i++ ){
                     Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
-                if (debug_alive){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 );}
-                }
+                    if( debug_alive ){ 
+                        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 ); }
+                    }
                 
-                if (debug_alive == 1){
-                debug_msg("Status - alive ");
-                //uint32_t errper = 100*(pkg_ckserr/(pkg_ckserr + pkg_cksok));
-                //pc.printf("\n\r Error = %d", errper);
+                if( debug_alive == 1 ){
+                    if( debug_main ) debug_msg("Status - alive ");
+                    //uint32_t errper = 100*(pkg_ckserr/(pkg_ckserr + pkg_cksok));
+                    //pc.printf("\n\r Error = %d", errper);
                 }
                 t.reset();
                 led3 = !led3;
                 CAB_LED = !CAB_LED;
                 
-                
-                /*
                 if( count  == 4 ){
-                    data = buffer;
-                    for( register int i = 0; i < __TELEMETRY_SIZE__; i++ ) data[ i ] = (uint8_t)0x03 + i;
-                    type = __TELEMETRY__;
-                    pc.printf("\n\rMontando telemetry pkg\n\r");
-                    ext = 1234;
+                    Call_Box * cb = new Call_Box( 854, 0x1011 );
+                    v_cb->add( cb );
                 }
                 count++;
-                */
+                
+                if( count  == 41 ){
+                    Call_Box * cb = new Call_Box( 854, 0x1011 );
+                    v_cb->add( cb );
+                }
             }
 //----------------------
         switch( type ){
@@ -193,7 +174,7 @@
                 if( cb != NULL ){
                     data[ 0 ] |= BIT7;
                     cb->set_msg_id( data[ 0 ] );
-                    debug_msg( " Request bye from CBx " );
+                    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 ){
@@ -216,10 +197,10 @@
             }break;
             
             case __INVITE__ : {
-                debug_msg("ok -- recebido o invite pelo menos");
+                if( debug_main ) debug_msg("Request Invite received from Cbx %i", ext);
                 Call_Box * cb = __find_CB__( v_cb, ext );
                 if( cb == NULL ){
-                    debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
+                    debug_msg("Adding CBx :: %i", ext );
                     cb = new Call_Box( ext, port );
                     v_cb->add( cb );
                     set_status( cb->status, cb_idle );
@@ -234,11 +215,11 @@
                     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 -- " );    
+                    if( debug_main ) debug_msg( "denying call - another call box on call" );    
                     break;
                 }
                 set_status( cb->status, cb_trying );
-                debug_msg("Request invite cbx status :: %d", cb->status );
+                if( debug_main ) debug_msg("Request invite cbx status :: %d", cb->status );
                 switch( cb->status ){
                     case cb_on_call : {
                         // a priori, nao fazer nada
@@ -262,7 +243,7 @@
                             __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 -- without TS -- %s ", write_buffer );
                         }else{
                             set_status(cb->status,cb_busy);
                             data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
@@ -281,8 +262,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_pkg( 300, write_buffer );
+                                debug_msg( "-- Trying -- accepting call request --" );
                             }else{
                                 set_status( cb->status,cb_idle);
                                 ts->return_timeslice( cb->get_timeslice() );
@@ -291,7 +271,7 @@
                                 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 -- denying call request --" );
                                 set_status( cb->sip->status, sip_idle);
                             }
                         }
@@ -301,10 +281,10 @@
                         ts->return_timeslice( cb->get_timeslice() );
                         cb->set_timeslice( 0x00 );
                         data[ __TIMESLICE_PLACE__ ] = 0x00;
-                        debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
+                        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 ) );
-                        debug_msg( "-- Trying -- Negando pedido de invite << Busy Here >> --" );
+                        if( debug_main ) debug_msg( "-- Trying -- denying call request << Busy Here >> --" );
                         set_status( cb->sip->status, sip_idle );
                     }break;
                 }                            
@@ -316,14 +296,14 @@
                 cb = __find_CB__( v_cb, ext );
                 if( cb != NULL ){
                     cb->reset_elapsed_time();
-                    debug_msg(" reset_elapsed_time CBx -- %d", ext );
+                    if( debug_main ) debug_msg(" reset_elapsed_time CBx -- %d", ext );
                 }else{
-                    debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
+                    if( debug_main ) debug_msg("Adding Cbx :: %d -- Type :: %d", ext, type );
                     cb = new Call_Box( ext, port );
                     v_cb->add( cb );
-                    debug_msg(" Criado CBx -- %d", ext );
+                    if( debug_main ) debug_msg("Added CBx -- %d", ext );
                 }
-                debug_msg("::Registrado %d - %d::", ext, port );
+                if( debug_main ) debug_msg("::registered %d - %d::", ext, port );
                 cb->registry();
                 }break;
             case __BOOT__ : {
@@ -342,12 +322,13 @@
                 cb = NULL;
                 call = __find_Call__( v_call, ext );
                 if( call != NULL ){
-                    char * pkg = call->build_eth_package( data );
+                    //char * pkg = call->build_eth_package( data );
+                    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{
-                    debug_msg("received missed package  from CBx :: %i -- Type :: %i", ext, type );
+                    if( debug_main ) debug_msg("received missed package  from CBx :: %i -- Type :: %i", ext, type );
                 }
             }break;
         }// fim switch
@@ -362,7 +343,7 @@
                 
                 if( cb != NULL ){
                     cb_port = cb->get_port();
-                }else debug_msg("received missed package  from CBx :: %i -- Type :: %i", ext, type );
+                }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 );
@@ -370,7 +351,6 @@
                 __send_to_cb__( pkg2cb );
             }
         }
-        ///debug_msg("");
         if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer );
         
         int ret = sip_manager( v_cb, v_call, write_buffer );
@@ -396,7 +376,7 @@
                     set_status( cb->sip->status, sip_idle );
                     cb->re_start_timer();
                 }
-            }else debug_msg("Nao encontrado CBx :: %d -- Type :: %d", ext, type );
+            }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 );
@@ -408,18 +388,6 @@
         }else {
             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++ ){
-            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() );
-                if( call == NULL ){
-                    ts->return_timeslice( cb->get_timeslice() );
-                    cb->set_timeslice( 0x00 );
-                    set_status(cb->status,cb_idle);
-                }
-            }
-        }
-*/      type = __DO_NOTHING__;
+        type = __DO_NOTHING__;
     }
 }
\ No newline at end of file