![](/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
eth.h
- Committer:
- klauss
- Date:
- 2014-10-02
- Revision:
- 40:990683572031
- Parent:
- 39:89e023ea5968
- Child:
- 42:480335bdde12
File content as of revision 40:990683572031:
#ifndef __ETH_H__ #define __ETH_H__ #include "prompt.h" #include "debug.h" #include "vz_protocol.h" #define udp_idle 0x00 #define udp_waiting_start_pkg 0x01 #define udp_listen 0x02 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 ){ static UDPSocket server; server.set_blocking( false, 2 ); 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 uint8_t status = udp_idle; static uint8_t seq_num = 0; // OBS sizeof( buffer ) == 4 ???? //*length = server.receiveFrom( client, buffer, sizeof( buffer ) ); *length = server.receiveFrom( client, buffer, __UDP_ETH_BUFFER_SIZE__ ); if( *length > 0){ buffer[ *length ] = '\0'; buffer[ strlen( buffer ) - 1 ] = '\0'; t.start(); debug_msg("%s", buffer ); buffer = prompt_process( buffer ); if( buffer ){ debug_msg("--%s--", buffer ); //server.sendTo( client, buffer, sizeof( buffer ) ); //int sent = server.sendTo( client, buffer, __UDP_ETH_BUFFER_SIZE__ ); int sent = server.sendTo( client, buffer, strlen( buffer ) ); debug_msg("Sent %d", sent ); } return( NULL ); 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; 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(); do{ wdt.kick(); *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); 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() < 60 ){ //tratar o primeiro pkg depois dos zeros aqui //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( ( 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 ... if( valid_reboot ) NVIC_SystemReset(); else{ server.set_blocking( false, 1 ); t.stop(); t.reset(); debug_msg("Nao Resetando ... "); } } } }else return( buffer ); }else return( NULL ); return( NULL ); } #endif