Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
109:a5b8264ffbbc
Parent:
105:a930035b6556
Child:
113:db67ae00550e
--- a/vz_protocol.cpp	Wed Mar 18 13:33:54 2015 +0000
+++ b/vz_protocol.cpp	Tue Apr 07 14:27:44 2015 +0000
@@ -40,79 +40,76 @@
     fclose( end_ext );
 }
 
-uint8_t * parse_vz_pkg( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ){
+uint8_t * __parse_vz_pkg__( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ){
     uint8_t p_lsb, p_msb;
     uint8_t e_lsb, e_msb;
     
     if( ( ext && port && type && cb_buffer ) == NULL ) return( NULL );
-
-    //tratamento dos pacotes do tipo flood logo na chegada
-    {    
-        if( cb_buffer[ 6 ] == __FLOOD__ ){
-            flood_counter++;
-            static uint8_t flood_cnt = 0;
-            static uint8_t first_run_flag = 0;
-            static Timer t;
-            if (first_run_flag == 0) {
-                flood_cnt = cb_buffer[0];
-                first_run_flag++;
-                t.start();
-            } else {
-                if (cb_buffer[0] == 0x00){
-                    if( debug_uart3 ) pc.printf("%d", t.read_us());
-                    if( tcp_session && !udp_query ){
+    
+    if( cb_buffer[ 6 ] == __FLOOD__ ){
+        flood_counter++;
+        static uint8_t flood_cnt = 0;
+        static uint8_t first_run_flag = 0;
+        static Timer t;
+        if (first_run_flag == 0) {
+            flood_cnt = cb_buffer[0];
+            first_run_flag++;
+            t.start();
+        } else {
+            if (cb_buffer[0] == 0x00){
+                if( debug_uart3 ) pc.printf("%d", t.read_us());
+                if( tcp_session ){
+                    char tmp[ 16 ];
+                    sprintf( tmp, "%d", t.read_us() );
+                    tcp_client.send_all( tmp, strlen( tmp ) );
+                }
+            }
+            flood_cnt++;
+            while (flood_cnt != cb_buffer[0]) {
+                if( debug_uart3 ) pc.printf("0");
+                flood_cnt++;
+                if( tcp_session ){
+                    tcp_client.send_all( "0", strlen( "0" ) );
+                }
+            }
+            int i;
+            int ok = 1;
+            uint8_t cmp;
+            for (i=0, cmp=cb_buffer[0]; (i<300) && (ok==1); i++, cmp++) {
+                if (i != 6) {
+                    if (cb_buffer[i] != cmp) ok = 0;
+                }
+            }
+            if( ok ){
+                if( debug_uart3 ) pc.printf("+");
+                if( tcp_session ){
+                    tcp_client.send_all( "+", strlen( "+" ) );
+                }
+            }else{
+                if( debug_uart3 ) pc.printf("%02x",cb_buffer[0] );
+                if( debug_uart3 ) pc.printf("#");
+                if( debug_uart3 ) pc.printf("\n\r");
+                if( tcp_session ){
+                    char tmp[ 16 ];
+                    sprintf( tmp, "%02x#\n\r", cb_buffer[ 0 ] );
+                    tcp_client.send_all( tmp, strlen( tmp ) );
+                }
+                for( int i = 0; i < 300 ; i++ ){
+                    if( debug_uart3 ) pc.printf("%x", cb_buffer[ i ]);
+                    if( tcp_session ){
                         char tmp[ 16 ];
-                        sprintf( tmp, "%d", t.read_us() );
+                        sprintf( tmp, "%x", cb_buffer[ i ] );
                         tcp_client.send_all( tmp, strlen( tmp ) );
                     }
                 }
-                flood_cnt++;
-                while (flood_cnt != cb_buffer[0]) {
-                    if( debug_uart3 ) pc.printf("0");
-                    flood_cnt++;
-                    if( tcp_session && !udp_query ){
-                        tcp_client.send_all( "0", strlen( "0" ) );
-                    }
-                }
-                int i;
-                int ok = 1;
-                uint8_t cmp;
-                for (i=0, cmp=cb_buffer[0]; (i<300) && (ok==1); i++, cmp++) {
-                    if (i != 6) {
-                        if (cb_buffer[i] != cmp) ok = 0;
-                    }
-                }
-                if( ok ){
-                    if( debug_uart3 ) pc.printf("+");
-                    if( tcp_session && !udp_query ){
-                        tcp_client.send_all( "+", strlen( "+" ) );
-                    }
-                }else{
-                    if( debug_uart3 ) pc.printf("%02x",cb_buffer[0] );
-                    if( debug_uart3 ) pc.printf("#");
-                    if( debug_uart3 ) pc.printf("\n\r");
-                    if( tcp_session && !udp_query ){
-                        char tmp[ 16 ];
-                        sprintf( tmp, "%02x#\n\r", cb_buffer[ 0 ] );
-                        tcp_client.send_all( tmp, strlen( tmp ) );
-                    }
-                    for( int i = 0; i < 300 ; i++ ){
-                        if( debug_uart3 ) pc.printf("%x", cb_buffer[ i ]);
-                        if( tcp_session && !udp_query ){
-                            char tmp[ 16 ];
-                            sprintf( tmp, "%x", cb_buffer[ i ] );
-                            tcp_client.send_all( tmp, strlen( tmp ) );
-                        }
-                    }
-                }
             }
-            return( NULL );
         }
-    }//fim tratamento do flood
+        return( NULL );
+    }
     
     uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ];
     
-    uint16_t cc_calculated = __checksum__( cb_buffer, CB_BUFFER_SIZE );
+    uint16_t cc_calculated = __checksum__( cb_buffer, __CB_BUFFER_SIZE__ );
     
     if( cc != cc_calculated ){
         if( debug_cks_err ) send_msg( "Received PKG, but -- CKS ERROR: %d ( %d )", cc_calculated, cc );
@@ -136,6 +133,10 @@
             return( NULL );
         }
         
+        //if( 
+        //( ( cb_buffer[ 0 ] == cb_buffer[ 2 ] ) || ( cb_buffer[ 1 ] == cb_buffer[ 3 ] ) ) ) 
+        //debug_msg("[0](%u),[1](%u),[2](%u),[3](%u)", cb_buffer[ 0 ], cb_buffer[ 1 ], cb_buffer[ 2 ], cb_buffer[ 3 ] );
+        
         p_msb = cb_buffer[ 2 ];
         p_lsb = cb_buffer[ 3 ];
         *port = p_msb << 8 | p_lsb; 
@@ -149,14 +150,7 @@
         return( cb_buffer + __VZ_HEADER_OFFSET__ );    
     }
 }
-uint8_t * build_cb_package( int ext, int port, uint8_t type, char * cb_buffer, uint8_t seq_num, int length,  uint8_t * pkg ){    
-    //checa se o fuffer de src e dst dos dados estao alocados
-    if( ( cb_buffer == NULL ) || ( pkg == NULL ) ) return NULL;
-    
-    //apaga as posicoes de memoria onde serah montado o pacote
-    for( register int i = 0; i < CB_BUFFER_SIZE; i++ ) pkg[ i ] = 0;
-    
-    //coloca ramal ( MSB ), ramal ( LSB ), porta ( MSB ), porta ( LSB ) e tipo nas posicoes destino
+uint8_t * __build_cb_package__( int ext, int port, uint8_t type, char * cb_buffer, uint8_t seq_num, int length,  uint8_t * pkg ){
     pkg[ 0 ] = ( uint8_t )( ( ext & 0xFF00 ) >> 8 );                                                                                      
     pkg[ 1 ] = ( uint8_t )( ext & 0x00FF );                                                                                                                                                                                                                                 
     pkg[ 2 ] = ( uint8_t )( ( port & 0xFF00 ) >> 8 );                      
@@ -164,95 +158,61 @@
                                                                                                                                         
     pkg[ 6 ] = type;
     
-    //preenche os dados de acordo com o tipo do pacote transmitido
-    size_t fill = __VZ_HEADER_OFFSET__;
-    switch( type )
-    {
-        case __AUDIO__  :{
-                for( register int i = __VZ_HEADER_OFFSET__; i < length + __VZ_HEADER_OFFSET__; i++ )
-                    pkg[ i ] = ( uint8_t )cb_buffer[ i - __VZ_HEADER_OFFSET__ ];
-                
-                fill = length + __VZ_HEADER_OFFSET__;            
-                break;
-        }
-        case __INVITE__ :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
-                fill = __TIMESLICE_PLACE__ + 1;
-                break;
-        }
+    size_t fill = 0x00;
+    
+    if( type == __AUDIO__ ){
+        for( register int i = __VZ_HEADER_OFFSET__; i < length + __VZ_HEADER_OFFSET__; i++ )
+            pkg[ i ] = ( uint8_t )cb_buffer[ i - __VZ_HEADER_OFFSET__ ];
+        fill = length + __VZ_HEADER_OFFSET__;
+    }else if( type == __INVITE__ ){
+        pkg[ 7 ] = seq_num;
+        __print_clock__( pkg + 8 );
+        
+        pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
+        fill = __TIMESLICE_PLACE__ + 1;
+    }else if( type == __REGISTRY__ || type == __BOOT__ ){
+        pkg[ 7 ] = seq_num;
+        __print_clock__( pkg + 8 );
+        
+        fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
+    }else if( type == __CB_BYE__ ){
+        pkg[ 7 ] = seq_num;
+        __print_clock__( pkg + 8 );
+        
+        pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
+        fill = __TIMESLICE_PLACE__ + 1;
+    }else if( type == __PROMPT__ ){
+        __print_clock__( pkg + 8 );
         
-        case __REGISTRY__  :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
-                break;
-        }
-        case __BOOT__ :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
-                break;
-        }
-        case __CB_BYE__ :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
-                fill = __TIMESLICE_PLACE__ + 1;
-                break;
-        }
-        case __PROMPT__ :{
-                strcpy( ( char * )( pkg + __VZ_HEADER_OFFSET__ ), cb_buffer );
-                fill = strlen( cb_buffer ) + __VZ_HEADER_OFFSET__;
-                break;
-        }
-        case __TELEMETRY__ :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                //FIXME a header manda pacotes de tele pro cbx ?
-                pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
-                fill = __TIMESLICE_PLACE__ + 1;
-                break;
-        }
-        case __BOOTLOADER_CBX__ :{
-                pkg[ 7 ] = seq_num;
-                __print_clock__( pkg + 8 );
-                fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
-                break;
-        }
-    }//fim switch
+        xmemcpy((pkg+7), (uint8_t*)cb_buffer, 293);
+        //FIXME isso forca qualquer cmd ter < 100 chars
+        fill = 100;
+    }else if( type == __TELEMETRY__ ){
+        pkg[ 7 ] = seq_num;
+        __print_clock__( pkg + 8 );
+        
+        pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ];
+        fill = __TIMESLICE_PLACE__ + 1;
+    }else if( type == __BOOTLOADER_CBX__ ){
+        pkg[ 7 ] = seq_num;
+        __print_clock__( pkg + 8 );
+        
+        fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__;
+    }else{
+        fill = __VZ_HEADER_OFFSET__;
+    }
     
-    //preenche o final do pacote com 0
-    for( register uint16_t i = fill; i < CB_BUFFER_SIZE; i++ ) pkg[ i ] = 0x00;
+    for( register uint16_t i = fill; i < __CB_BUFFER_SIZE__; i++ ) pkg[ i ] = 0x00;
     
-    //colocando o terminador de pacotes estabelecido pelo protocolo
-    pkg[ CB_BUFFER_SIZE - 4 ] = 0x5a;
-    pkg[ CB_BUFFER_SIZE - 3 ] = 0x56;
-    pkg[ CB_BUFFER_SIZE - 2 ] = 0x5a;
-    pkg[ CB_BUFFER_SIZE - 1 ] = 0xe1;
+    pkg[ __CB_BUFFER_SIZE__ - 4 ] = 0x5a;
+    pkg[ __CB_BUFFER_SIZE__ - 3 ] = 0x56;
+    pkg[ __CB_BUFFER_SIZE__ - 2 ] = 0x5a;
+    pkg[ __CB_BUFFER_SIZE__ - 1 ] = 0xe1;
     
-    //calcula e preenche o checksum
-    uint16_t cc = __checksum__( pkg, CB_BUFFER_SIZE );
+    uint16_t cc = __checksum__( pkg, __CB_BUFFER_SIZE__ );
     pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ;
     pkg[ 5 ] =( uint8_t )( cc & 0x00FF );
     
-    //enable/disable de exibicao de pacotes prontos para envio
-    if( debug_show_cpld ) {
-        char str[ 1024 ];
-        strcpy( str, "TX :: \n\r " );
-        for( register uint16_t i = 0; i < CB_BUFFER_SIZE; i++ ) {
-            char tmp[ 16 ];
-            strcat( str, itoa( pkg[ i ], tmp, 16 ) );
-            if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " );
-
-            else strcat( str, " " );
-        }
-        send_msg( "%s", str );
-        //rx = false;
-    }
-    
-    //retorna o ultimo paramentro recebido com o pacote montado.
     return pkg;
 }