![](/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
Diff: eth.h
- Revision:
- 34:f19d9735428e
- Parent:
- 33:735fd60e96d8
- Child:
- 35:96885a7931f0
--- a/eth.h Tue Sep 30 11:37:31 2014 +0000 +++ b/eth.h Tue Sep 30 14:28:01 2014 +0000 @@ -3,70 +3,142 @@ #include "prompt.h" #include "debug.h" +#include "vz_protocol.h" + +#define udp_idle 0x00 +#define udp_waiting_start_pkg 0x01 +#define udp_listen 0x02 + +#define __START_PKG_COUNT__ 100 #define UDP_PORT_LISTENER 11406 +int is_start_pkg( uint8_t * data ){ + //seq_num must to be 0s + if( !data ) return -1; + + for( register int i = __VZ_HEADER_OFFSET__; i < __UDP_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); + + return( 1 ); +} + +int is_stop_pkg( uint8_t * data ){ + //seq_num must to be 0s + if( !data ) return -1; + + for( register int i = __VZ_HEADER_OFFSET__; i < __UDP_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); + + return( 1 ); +} + +int is_time_to_stop( uint8_t * data ){ + static int count = 0; + + if( !data ) return( 0 ); + + if( is_stop_pkg( data ) == 1 ) count++; + + else count = 0; + + if( count == 10 ) return( 1 ); + + else return( 0 ); +} + char * listen_eth_udp( char * buffer, int *length ){ - UDPSocket server; + static UDPSocket server; server.set_blocking( false, 1 ); server.bind( UDP_PORT_LISTENER ); Endpoint client; + bool valid_reboot = false; + uint8_t * data = NULL; + static int ext; + static int port; + static uint8_t type; static Timer t; - static int count = 0; - static int param = 100; + static uint8_t status = udp_idle; + static uint8_t seq_num = 0; - *length = server.receiveFrom( client, buffer, sizeof(buffer ) ); + *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); if( *length > 0){ buffer[ *length ] = '\0'; t.start(); - if( count >= 0 ){ - if( count == 0 ); // enviar pacote de "entendi o request de volta" - - if( atoi( buffer ) == 0 ) count++; + + data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); + + if( !data ){ + debug_msg("UDP pks parse failed"); + server.sendTo( client, buffer, sizeof( buffer ) ); + return( NULL ); + } + + if( status == udp_idle ){ + data[ 0 ] |= BIT7; + strcpy( (char *)&data[ 1 ], "Received listen request" ); + server.sendTo( client, buffer, sizeof( buffer ) ); + status = udp_waiting_start_pkg; + } + + if( status == udp_waiting_start_pkg ){ + static int count = 0; + if( is_start_pkg( data ) == 1 ) + if( count++ == __START_PKG_COUNT__ ) status = udp_listen; - else count = 0; - } - if( count == param ){ - debug_msg( "Received start pkgs" ); - if( t.read() < 10 ){ - param = 1000; + else{ + count = 0; + status = udp_idle; + } + } + + if( status == udp_listen ){ + debug_msg( "Received start pkgs" ); // mandar msg pro server + if( t.read() < 60 ){ server.set_blocking( true, 10 ); t.reset(); - - count = 0; - while( atoi( buffer ) == 0 && t.read() < 10 ){ + do{ + wdt.kick(); *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); - wdt.kick(); - } + data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); + if( data ){ + if( !( is_start_pkg( data ) ) ) break; + }else{ + debug_msg("UDP pks parse failed"); + // reenvia pkg dizendo que podemos comecar com a transacao de dados + } + }while( t.read() < 60 ); - if( t.read() > 10 ) return( NULL ); - - else{ + if( t.read() < 60 ){ //tratar o primeiro pkg depois dos zeros aqui - count++; - while( count < 6000 ){ + //a ideia é ir recebendo e colocando no arquivo "se for pra header" ou mandar + //pro cb depois, + if( !( ext || port ) ){ // pkg pra header + debug_msg("criando arquivo"); + }else{ + debug_msg("Mandando pkg pro cb"); + } + //FIXME mudar essa condicao de parada + while( !( is_time_to_stop( data ) == 1 ) ){ wdt.kick(); *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); - if( ++count == param ){ - debug_msg(" Received %04d - Sent %04d - Missed %04d - %.3f", count, atoi( buffer ), atoi( buffer ) - count, ( (float)count )/( atoi( buffer ) ) ); - param += 1000; + if( ( data[ 0 ] - seq_num ) != 1 ){ + //perdi algum pacote + //solicitar re-envio mandando o ultimo seq_num recebido talvez ... + continue; + }else{ + debug_msg("+(%d,%d)-%s+", ext, port, data ); } } //pensar em como validar os pacotes recebidos, cks ... e o que mais ? //se validado os pkgs, dar reset com os dados no arquivo bla, caso contrario, ignorar ... - bool valid_reboot = false; if( valid_reboot ) NVIC_SystemReset(); else{ - count = 0; - param = 100; server.set_blocking( false, 1 ); t.stop(); t.reset(); debug_msg("Nao Resetando ... "); } - } } }else return( buffer );