![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
vz_protocol.cpp
- Committer:
- klauss
- Date:
- 2015-02-13
- Revision:
- 101:162c28286c29
- Parent:
- 100:09a23fcd3bdf
- Child:
- 102:98c7155e8bea
File content as of revision 101:162c28286c29:
#include "vz_protocol.h" uint32_t pkg_ckserr = 0; uint32_t pkg_zero = 0; uint32_t pkg_cksok = 0; int begin = 0; int end = 0; time_t current_time = 0; UDPSocket clock_sock; Endpoint clock_server; Timer external_time; void 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" ); begin = MIN_EXT; }else{ char inner_tmp[ 20 ]; if( fread( (void *)inner_tmp, 1, 20, begin_ext ) > 0 ){ begin = atoi( inner_tmp ); if( debug_out_of_range ) debug_msg("Ext Min %d", begin ); }else{ if( debug_out_of_range ) debug_msg("Failed to read /qspi/minext.txt" ); begin = MIN_EXT; } } fclose( begin_ext ); FILE * end_ext = fopen( "/qspi/maxext.txt", "r" ); if( end_ext == NULL ){ if( debug_out_of_range ) debug_msg("Failed to open /qspi/maxext.txt" ); end = MAX_EXT; }else{ char inner_tmp[ 20 ]; if( fread( (void *)inner_tmp, 1, 20, end_ext ) > 0 ){ end = atoi( inner_tmp ); if( debug_out_of_range ) debug_msg("Ext Min %d", end ); }else{ if( debug_out_of_range ) debug_msg("Failed to read /qspi/maxext.txt" ); begin = MAX_EXT; } } fclose( end_ext ); } 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 ); 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, "%x", cb_buffer[ i ] ); tcp_client.send_all( tmp, strlen( tmp ) ); } } } } return( NULL ); } uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ]; 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 ); if( cc_calculated == 30975 && cc == 0 ){ pkg_zero++; }else{ pkg_ckserr++; } return( NULL ); }else{ pkg_cksok++; e_msb = cb_buffer[ 0 ]; e_lsb = cb_buffer[ 1 ]; *ext = e_msb << 8 | 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 ); out_of_range++; 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; if( debug_port_match ) if( *ext == *port ) debug_msg("%u:%u", *ext, *port ); *type = cb_buffer[ 6 ]; if( ( ( *port < 5000 ) || ( *port >= ( 5000 + __MAX_CB_IN_A_BRANCH__ ) ) && ( *type == __TELEMETRY__ ) ) ) return NULL; 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 ){ 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[ 6 ] = type; 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 ); 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__; } for( register uint16_t i = fill; i < __CB_BUFFER_SIZE__; i++ ) pkg[ i ] = 0x00; pkg[ __CB_BUFFER_SIZE__ - 4 ] = 0x5a; pkg[ __CB_BUFFER_SIZE__ - 3 ] = 0x56; pkg[ __CB_BUFFER_SIZE__ - 2 ] = 0x5a; pkg[ __CB_BUFFER_SIZE__ - 1 ] = 0xe1; uint16_t cc = __checksum__( pkg, __CB_BUFFER_SIZE__ ); pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ; pkg[ 5 ] =( uint8_t )( cc & 0x00FF ); return pkg; } uint16_t __checksum__( uint8_t * buffer, size_t length ){ if( !buffer ) return( 0 ); uint16_t cc = 0x00; buffer[ 4 ] = buffer[ 5 ] = 0x5a; for( register int i = 0; i < length; i++ ){ cc += buffer[ i ]; if( cc & BIT15 ){ cc <<= 1; cc |= BIT0; }else{ cc <<= BIT0; } } cc ^= 0xffff; return cc; } void print_clock( uint8_t * buffer ) { if( buffer != NULL ) { struct tm tmp_tm, * result_tm; if( sizeof( time_t ) != sizeof( long ) ) debug_msg("sizeof( time_t ) : %lu -- sizeof( long int ) : %lu\n", sizeof( time_t ), sizeof( long ) ); debug_msg("current_time : %lu\t", current_time ); if( current_time != 0 ) { result_tm = localtime_r( ( const time_t *)¤t_time, ( struct tm * )&tmp_tm ); if (result_tm ) { char formated_time[ 16 ]; size_t formated_nbytes = strftime( formated_time, sizeof( formated_time ), "%Y%m%d%H%M%S", result_tm ); debug_msg("clock() : %s\n", formated_time ); if( formated_nbytes != __CLOCK_SYNC_SIZE__ ) debug_msg("( %lu )\n", formated_nbytes ); for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = formated_time[ i ]; } else { for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = 0xfa; } } else { for( register int i = 0; i < __CLOCK_SYNC_SIZE__; i++ ) buffer[ i ] = 0xab; } } } int request_clock_to_server( void ) { char clock_msg[ 16 ]; strcpy( clock_msg, "request_time" ); int send = clock_sock.sendTo( clock_server, clock_msg, strlen( clock_msg ) ); return send; } int check_clock( void ) { if( external_time.read() > EXTERNAL_TIME_REQUEST_WAIT_SECONDS ) { external_time.reset(); return request_clock_to_server(); } else { // nro arbitrario maior que strlen( request_time ) return 0x30; } } int update_clock( void ) { // rcv return 0; }