Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
Diff: vz_protocol.cpp
- Revision:
- 126:1f90756250fb
- Parent:
- 125:8ff4dc96ad58
- Child:
- 132:05cd37f7e007
diff -r 8ff4dc96ad58 -r 1f90756250fb vz_protocol.cpp --- a/vz_protocol.cpp Wed May 13 21:18:12 2015 +0000 +++ b/vz_protocol.cpp Wed May 20 19:50:31 2015 +0000 @@ -1,14 +1,13 @@ #include "vz_protocol.h" -#include "debug.h" - uint32_t pkg_ckserr = 0; uint32_t pkg_zero = 0; uint32_t pkg_cksok = 0; int begin = 0; int end = 0; -int init_ranges ( void ){ +int init_ranges ( void ) +{ FILE * begin_ext = fopen( "/qspi/minext.txt", "r" ); if( begin_ext == NULL ){ if( debug_out_of_range ) debug_msg("Failed to open /qspi/minext.txt" ); @@ -44,15 +43,17 @@ return ( 0 ); } -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 * parse_vz_pkg ( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ) +{ uint8_t e_lsb, e_msb; + uint8_t p_lsb, p_msb; - if( ( ext && port && type && cb_buffer ) == NULL ) return( NULL ); + if ( ( ext and port and type and cb_buffer ) == NULL ) return ( NULL ); //tratamento dos pacotes do tipo flood logo na chegada { - if( cb_buffer[ TYPE_PLACE ] == FLOOD ){ + if ( cb_buffer [ TYPE_PLACE ] == FLOOD ) + { flood_counter++; static uint8_t flood_cnt = 0; static uint8_t first_run_flag = 0; @@ -64,7 +65,7 @@ } else { if (cb_buffer[0] == 0x00){ if( debug_uart3 ) pc.printf("%d", t.read_us()); - if( tcp_session && !udp_query ){ + if( tcp_session and !udp_query ){ char tmp[ 16 ]; sprintf( tmp, "%d", t.read_us() ); tcp_client.send_all( tmp, strlen( tmp ) ); @@ -74,35 +75,35 @@ while (flood_cnt != cb_buffer[0]) { if( debug_uart3 ) pc.printf("0"); flood_cnt++; - if( tcp_session && !udp_query ){ + if( tcp_session and !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++) { + for (i=0, cmp=cb_buffer[0]; (i<300) and (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 ){ + if( tcp_session and !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 ){ + if( tcp_session and !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 ){ + if( tcp_session and !udp_query ){ char tmp[ 16 ]; sprintf( tmp, "%x", cb_buffer[ i ] ); tcp_client.send_all( tmp, strlen( tmp ) ); @@ -114,15 +115,15 @@ } }//fim tratamento do flood - uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ]; + uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 bitor cb_buffer[ 5 ]; - uint16_t cc_calculated = vz_checksum( cb_buffer, CB_BUFFER_SIZE ); + uint16_t cc_calculated = vz_checksum ( cb_buffer, CB_BUFFER_SIZE ); - if( cc != cc_calculated ) + if ( cc != cc_calculated ) { - if( debug_cks_err ) send_msg( "Received PKG, but -- CKS ERROR: %d ( %d )", cc_calculated, cc ); + if ( debug_cks_err ) send_msg ( "Received PKG, but -- CKS ERROR: %d ( %d )", cc_calculated, cc ); - if( cc_calculated == 30975 && cc == 0 ){ + if ( cc_calculated == 30975 and cc == 0 ){ pkg_zero++; }else{ pkg_ckserr++; @@ -134,41 +135,39 @@ pkg_cksok++; e_msb = cb_buffer[ 0 ]; e_lsb = cb_buffer[ 1 ]; - *ext = e_msb << 8 | e_lsb; + *ext = ( (uint16_t )e_msb ) << 8 bitor e_lsb; //restriçao de controle para cbx malucos - if( *ext < begin || *ext > end ){ - if( debug_out_of_range ) send_msg("Trying from %d", *ext ); + if ( *ext < begin or *ext > end ) + { + if ( debug_out_of_range ) send_msg ("Trying from %d", *ext ); out_of_range++; - return( NULL ); + return ( NULL ); } p_msb = cb_buffer[ 2 ]; p_lsb = cb_buffer[ 3 ]; - *port = p_msb << 8 | p_lsb; - - if( debug_port_match ) if( *ext == *port ) debug_msg("%u:%u", *ext, *port ); + *port = ( (uint16_t )p_msb ) << 8 bitor p_lsb; - *type = cb_buffer[ TYPE_PLACE ]; - - if( ( ( *port < BASE_PORT ) || ( *port >= ( BASE_PORT + MAX_CB_IN_A_BRANCH ) ) && ( *type == TELEMETRY ) ) ) return NULL; + *type = cb_buffer [ TYPE_PLACE ]; 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 ) + +uint8_t * build_cb_package ( const int ext, const int port, const uint8_t type, const char * cb_buffer, const uint8_t seq_num, const 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; + if( ( cb_buffer == NULL ) or ( 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 - pkg[ 0 ] = ( uint8_t )( ( ext & 0xFF00 ) >> 8 ); - pkg[ 1 ] = ( uint8_t )( ext & 0x00FF ); - pkg[ 2 ] = ( uint8_t )( ( port & 0xFF00 ) >> 8 ); - pkg[ 3 ] = ( uint8_t )( port & 0x00FF ); + pkg[ 0 ] = ( uint8_t )( ( ext bitand 0xFF00 ) >> 8 ); + pkg[ 1 ] = ( uint8_t )( ext bitand 0x00FF ); + pkg[ 2 ] = ( uint8_t )( ( port bitand 0xFF00 ) >> 8 ); + pkg[ 3 ] = ( uint8_t )( port bitand 0x00FF ); pkg[ TYPE_PLACE ] = type; @@ -241,8 +240,8 @@ //calcula e preenche o checksum uint16_t cc = vz_checksum( pkg, CB_BUFFER_SIZE ); - pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ; - pkg[ 5 ] =( uint8_t )( cc & 0x00FF ); + pkg[ 4 ] =( uint8_t )( ( cc bitand 0xFF00 ) >> 8) ; + pkg[ 5 ] =( uint8_t )( cc bitand 0x00FF ); //retorna o ultimo paramentro recebido com o pacote montado. return pkg; @@ -255,9 +254,9 @@ buffer[ 4 ] = buffer[ 5 ] = 0x5a; for( register int i = 0; i < length; i++ ){ cc += buffer[ i ]; - if( cc & BIT15 ){ + if( cc bitand BIT15 ){ cc <<= 1; - cc |= BIT0; + cc or_eq BIT0; }else{ cc <<= BIT0; } } cc ^= 0xffff;