VZTECH / Mbed 2 deprecated main_src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vz_protocol.cpp Source File

vz_protocol.cpp

00001 #include "vz_protocol.h"
00002 
00003 uint32_t pkg_ckserr = 0;
00004 uint32_t pkg_zero = 0;
00005 uint32_t pkg_cksok = 0;
00006 int begin = 0;
00007 int end = 0;
00008 
00009 int init_ranges ( void )
00010 {
00011     begin = cm -> get_min_ext ();
00012     end = cm -> get_max_ext ();
00013     
00014     return ( 0 );
00015 }
00016 
00017 uint8_t * 
00018 parse_vz_pkg ( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer )
00019 {   
00020     if ( ( ext and port and type and cb_buffer ) == NULL ) return ( NULL );
00021     
00022     //tratamento dos pacotes do tipo flood logo na chegada
00023     {    
00024         if ( cb_buffer [ TYPE_PLACE ] == FLOOD )
00025         {
00026             flood_counter++;
00027             static uint8_t flood_cnt = 0;
00028             static uint8_t first_run_flag = 0;
00029             static Timer t;
00030             if (first_run_flag == 0) {
00031                 flood_cnt = cb_buffer[0];
00032                 first_run_flag++;
00033                 t.start();
00034             } else {
00035                 if (cb_buffer[0] == 0x00){
00036                     if( debug_uart3 and enable_flood ) pc.printf("%d", t.read_us());
00037                     if( tcp_session and !udp_query and enable_flood ){
00038                         char tmp[ 16 ];
00039                         sprintf( tmp, "%d", t.read_us() );
00040                         tcp_client.send_all( tmp, strlen( tmp ) );
00041                     }
00042                 }
00043                 flood_cnt++;
00044                 while (flood_cnt != cb_buffer[0]) {
00045                     if( debug_uart3 and enable_flood ) pc.printf("0");
00046                     flood_cnt++;
00047                     if( tcp_session and !udp_query and enable_flood ){
00048                         tcp_client.send_all( "0", strlen( "0" ) );
00049                     }
00050                 }
00051                 int i;
00052                 int ok = 1;
00053                 uint8_t cmp;
00054                 for (i=0, cmp=cb_buffer[0]; (i<300) and (ok==1); i++, cmp++) {
00055                     if (i != 6) {
00056                         if (cb_buffer[i] != cmp) ok = 0;
00057                     }
00058                 }
00059                 if( ok ){
00060                     if( debug_uart3 and enable_flood ) pc.printf("+");
00061                     if( tcp_session and !udp_query and enable_flood ){
00062                         tcp_client.send_all( "+", strlen( "+" ) );
00063                     }
00064                 }else{
00065                     if( debug_uart3 and enable_flood ) pc.printf("%02x",cb_buffer[0] );
00066                     if( debug_uart3 and enable_flood ) pc.printf("#");
00067                     if( debug_uart3 and enable_flood ) pc.printf("\n\r");
00068                     if( tcp_session and !udp_query and enable_flood ){
00069                         char tmp[ 16 ];
00070                         sprintf( tmp, "%02x#\n\r", cb_buffer[ 0 ] );
00071                         tcp_client.send_all( tmp, strlen( tmp ) );
00072                     }
00073                     for( int i = 0; i < 300 ; i++ ){
00074                         if( debug_uart3 and enable_flood ) pc.printf("%x", cb_buffer[ i ]);
00075                         if( tcp_session and !udp_query and enable_flood ){
00076                             char tmp[ 16 ];
00077                             sprintf( tmp, "%x", cb_buffer[ i ] );
00078                             tcp_client.send_all( tmp, strlen( tmp ) );
00079                         }
00080                     }
00081                 }
00082             }
00083             return( NULL );
00084         }
00085     }//fim tratamento do flood
00086     
00087     uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 bitor cb_buffer[ 5 ];
00088     
00089     uint16_t cc_calculated = vz_checksum ( cb_buffer, CB_BUFFER_SIZE );
00090     
00091     if ( cc != cc_calculated )
00092     {
00093         if ( debug_cks_err ) vz_printf ( "Received PKG, but -- CKS ERROR: %d ( %d )", cc_calculated, cc );
00094 
00095         if ( cc_calculated == 30975 and cc == 0 ){
00096             pkg_zero++;
00097         }else{ 
00098             pkg_ckserr++;
00099         }
00100         return( NULL );
00101     } 
00102         else 
00103     {
00104         
00105         uint8_t e_lsb, e_msb;
00106         uint8_t p_lsb, p_msb;
00107         
00108         pkg_cksok ++;
00109         e_msb = cb_buffer [ 0 ];
00110         e_lsb = cb_buffer [ 1 ];
00111         *ext = ( (uint16_t )e_msb ) << 8  bitor e_lsb;
00112         
00113         //restriçao de controle para cbx malucos       
00114         if ( *ext < begin or *ext > end )
00115         {
00116             if ( debug_out_of_range ) vz_printf ("Trying from %d", *ext );
00117             out_of_range ++;
00118             return ( NULL );
00119         }
00120         
00121         p_msb = cb_buffer[ 2 ];
00122         p_lsb = cb_buffer[ 3 ];
00123         *port = ( (uint16_t )p_msb ) << 8 bitor p_lsb; 
00124         
00125         *type = cb_buffer [ TYPE_PLACE ];
00126 
00127         return ( cb_buffer + VZ_HEADER_OFFSET );    
00128     }
00129 }
00130 
00131 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 )
00132 {
00133     //checa se o fuffer de src e dst dos dados estao alocados
00134     if( ( cb_buffer == NULL ) or ( pkg == NULL ) ) return NULL;
00135     
00136     //apaga as posicoes de memoria onde serah montado o pacote
00137     for( register int i = 0; i < CB_BUFFER_SIZE; i++ ) pkg[ i ] = 0;
00138     
00139     //coloca ramal ( MSB ), ramal ( LSB ), porta ( MSB ), porta ( LSB ) e tipo nas posicoes destino
00140     pkg[ 0 ] = ( uint8_t )( ( ext bitand 0xFF00 ) >> 8 );                                                                                      
00141     pkg[ 1 ] = ( uint8_t )( ext bitand 0x00FF );                                                                                                                                                                                                                                 
00142     pkg[ 2 ] = ( uint8_t )( ( port bitand 0xFF00 ) >> 8 );                      
00143     pkg[ 3 ] = ( uint8_t )( port bitand 0x00FF );
00144                                                                                                                                         
00145     pkg[ TYPE_PLACE ] = type;
00146     
00147     //preenche os dados de acordo com o tipo do pacote transmitido
00148     size_t fill = VZ_HEADER_OFFSET;
00149     switch( type )
00150     {
00151         case AUDIO  :{
00152                 for( register int i = VZ_HEADER_OFFSET; i < length + VZ_HEADER_OFFSET; i++ )
00153                     pkg[ i ] = ( uint8_t )cb_buffer[ i - VZ_HEADER_OFFSET ];
00154                 
00155                 fill = length + VZ_HEADER_OFFSET;            
00156                 break;
00157         }
00158         case INVITE :{
00159                 pkg[ 7 ] = seq_num;
00160                 print_clock( pkg + 8 );
00161                 pkg[ TIMESLICE_PLACE ] = cb_buffer[ TIMESLICE_PLACE ];
00162                 fill = TIMESLICE_PLACE + 1;
00163                 break;
00164         }
00165         
00166         case REGISTRY  :{
00167                 pkg[ 7 ] = seq_num;
00168                 print_clock( pkg + 8 );
00169                 fill = VZ_HEADER_OFFSET + CLOCK_SYNC_SIZE + SEQ_NUM_SIZE;
00170                 break;
00171         }
00172         case BOOT :{
00173                 pkg[ 7 ] = seq_num;
00174                 print_clock( pkg + 8 );
00175                 fill = VZ_HEADER_OFFSET + CLOCK_SYNC_SIZE + SEQ_NUM_SIZE;
00176                 break;
00177         }
00178         case CB_BYE :{
00179                 pkg[ 7 ] = seq_num;
00180                 print_clock( pkg + 8 );
00181                 pkg[ TIMESLICE_PLACE ] = cb_buffer[ TIMESLICE_PLACE ];
00182                 fill = TIMESLICE_PLACE + 1;
00183                 break;
00184         }
00185         case PROMPT :{
00186                 strcpy( ( char * )( pkg + VZ_HEADER_OFFSET ), cb_buffer );
00187                 fill = strlen( cb_buffer ) + VZ_HEADER_OFFSET;
00188                 break;
00189         }
00190         case TELEMETRY :{
00191                 pkg[ 7 ] = seq_num;
00192                 print_clock( pkg + 8 );
00193                 //FIXME a header manda pacotes de tele pro cbx ?
00194                 pkg[ TIMESLICE_PLACE ] = cb_buffer[ TIMESLICE_PLACE ];
00195                 fill = TIMESLICE_PLACE + 1;
00196                 break;
00197         }
00198         case BOOTLOADER_CBX :{
00199                 xmemcpy((pkg + 7),(uint8_t *)cb_buffer,length);
00200                 fill = VZ_HEADER_OFFSET + length;
00201                 break;
00202         }
00203     }//fim switch
00204     
00205     //preenche o final do pacote com 0
00206     for( register uint16_t i = fill; i < CB_BUFFER_SIZE; i++ ) pkg[ i ] = 0x00;
00207     
00208     //colocando o terminador de pacotes estabelecido pelo protocolo
00209     pkg[ CB_BUFFER_SIZE - 4 ] = 0x5a;
00210     pkg[ CB_BUFFER_SIZE - 3 ] = 0x56;
00211     pkg[ CB_BUFFER_SIZE - 2 ] = 0x5a;
00212     pkg[ CB_BUFFER_SIZE - 1 ] = 0xe1;
00213     
00214     //calcula e preenche o checksum
00215     uint16_t cc = vz_checksum( pkg, CB_BUFFER_SIZE );
00216     pkg[ 4 ] =( uint8_t )( ( cc bitand 0xFF00 ) >> 8) ;
00217     pkg[ 5 ] =( uint8_t )( cc bitand 0x00FF );
00218     
00219     //retorna o ultimo paramentro recebido com o pacote montado.
00220     return pkg;
00221 }
00222 
00223 uint16_t vz_checksum ( uint8_t * buffer, size_t length )
00224 {
00225     if( !buffer ) return( 0 );
00226     uint16_t cc = 0x00;
00227     buffer[ 4 ] = buffer[ 5 ] = 0x5a;
00228     for( register int i = 0; i < length; i++ ){
00229         cc += buffer[ i ];
00230         if( cc bitand BIT15 ){
00231             cc <<= 1;
00232             cc or_eq BIT0;
00233         }else{ cc <<= BIT0; }
00234     }
00235     cc ^= 0xffff;
00236     return cc; 
00237 }
00238 
00239 void print_clock ( uint8_t * buffer )
00240 {
00241     if( buffer != NULL )
00242     {
00243         struct tm * result_tm;
00244         if( sizeof( time_t ) != sizeof( long ) )
00245         {
00246             if ( debug_print_clock ) vz_debug ( "sizeof( time_t ) : %lu -- sizeof( long int ) : %lu\n", sizeof( time_t ), sizeof( long ) );
00247         }
00248                                    
00249         if ( debug_print_clock ) vz_debug ( "current_time : %lu\t", current_time );
00250                                                                                                                                         
00251         if( current_time != 0 )
00252         {
00253             result_tm = localtime( ( const time_t *)&current_time );
00254             if ( result_tm )
00255             {
00256                 char formated_time[ 16 ];
00257                 
00258                 /* Correcao "manual" do (GMT -3:00) */
00259                 result_tm->tm_hour -= 3;
00260                 if ( result_tm->tm_hour < 0 ) result_tm->tm_hour = 24 + result_tm->tm_hour;
00261                 
00262                 size_t formated_nbytes = strftime( formated_time, sizeof( formated_time ), "%Y%m%d%H%M%S", result_tm );
00263                 
00264                 if ( debug_print_clock ) vz_debug ("clock() : %s\n", formated_time );
00265                 
00266                 if( formated_nbytes != CLOCK_SYNC_SIZE ) vz_debug ("( %lu )\n", formated_nbytes );
00267                 
00268                 for( register int i = 0; i < CLOCK_SYNC_SIZE; i++ ) buffer[ i ] = formated_time[ i ];
00269             }
00270                 else
00271             {
00272                 for( register int i = 0; i < CLOCK_SYNC_SIZE; i++ ) buffer[ i ] = 0xfa;
00273             }
00274         }
00275             else
00276         {
00277             for( register int i = 0; i < CLOCK_SYNC_SIZE; i++ ) buffer[ i ] = 0xab;
00278         }
00279     }
00280 }