![](/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@35:96885a7931f0, 2014-09-30 (annotated)
- Committer:
- klauss
- Date:
- Tue Sep 30 18:15:08 2014 +0000
- Revision:
- 35:96885a7931f0
- Parent:
- 34:f19d9735428e
- Child:
- 36:728498a78e1e
recebe instrucoes simples por porta udp, executa e devolve resultado na serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 33:735fd60e96d8 | 1 | #ifndef __ETH_H__ |
klauss | 33:735fd60e96d8 | 2 | #define __ETH_H__ |
klauss | 33:735fd60e96d8 | 3 | |
klauss | 33:735fd60e96d8 | 4 | #include "prompt.h" |
klauss | 33:735fd60e96d8 | 5 | #include "debug.h" |
klauss | 34:f19d9735428e | 6 | #include "vz_protocol.h" |
klauss | 33:735fd60e96d8 | 7 | |
klauss | 34:f19d9735428e | 8 | |
klauss | 34:f19d9735428e | 9 | #define udp_idle 0x00 |
klauss | 34:f19d9735428e | 10 | #define udp_waiting_start_pkg 0x01 |
klauss | 34:f19d9735428e | 11 | #define udp_listen 0x02 |
klauss | 34:f19d9735428e | 12 | |
klauss | 34:f19d9735428e | 13 | #define __START_PKG_COUNT__ 100 |
klauss | 33:735fd60e96d8 | 14 | #define UDP_PORT_LISTENER 11406 |
klauss | 33:735fd60e96d8 | 15 | |
klauss | 34:f19d9735428e | 16 | int is_start_pkg( uint8_t * data ){ |
klauss | 34:f19d9735428e | 17 | //seq_num must to be 0s |
klauss | 34:f19d9735428e | 18 | if( !data ) return -1; |
klauss | 34:f19d9735428e | 19 | |
klauss | 34:f19d9735428e | 20 | for( register int i = __VZ_HEADER_OFFSET__; i < __UDP_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); |
klauss | 34:f19d9735428e | 21 | |
klauss | 34:f19d9735428e | 22 | return( 1 ); |
klauss | 34:f19d9735428e | 23 | } |
klauss | 34:f19d9735428e | 24 | |
klauss | 34:f19d9735428e | 25 | int is_stop_pkg( uint8_t * data ){ |
klauss | 34:f19d9735428e | 26 | //seq_num must to be 0s |
klauss | 34:f19d9735428e | 27 | if( !data ) return -1; |
klauss | 34:f19d9735428e | 28 | |
klauss | 34:f19d9735428e | 29 | for( register int i = __VZ_HEADER_OFFSET__; i < __UDP_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); |
klauss | 34:f19d9735428e | 30 | |
klauss | 34:f19d9735428e | 31 | return( 1 ); |
klauss | 34:f19d9735428e | 32 | } |
klauss | 34:f19d9735428e | 33 | |
klauss | 34:f19d9735428e | 34 | int is_time_to_stop( uint8_t * data ){ |
klauss | 34:f19d9735428e | 35 | static int count = 0; |
klauss | 34:f19d9735428e | 36 | if( !data ) return( 0 ); |
klauss | 34:f19d9735428e | 37 | |
klauss | 34:f19d9735428e | 38 | if( is_stop_pkg( data ) == 1 ) count++; |
klauss | 34:f19d9735428e | 39 | |
klauss | 34:f19d9735428e | 40 | else count = 0; |
klauss | 34:f19d9735428e | 41 | |
klauss | 34:f19d9735428e | 42 | if( count == 10 ) return( 1 ); |
klauss | 34:f19d9735428e | 43 | |
klauss | 34:f19d9735428e | 44 | else return( 0 ); |
klauss | 34:f19d9735428e | 45 | } |
klauss | 34:f19d9735428e | 46 | |
klauss | 33:735fd60e96d8 | 47 | char * listen_eth_udp( char * buffer, int *length ){ |
klauss | 34:f19d9735428e | 48 | static UDPSocket server; |
klauss | 35:96885a7931f0 | 49 | server.set_blocking( false, 2 ); |
klauss | 33:735fd60e96d8 | 50 | server.bind( UDP_PORT_LISTENER ); |
klauss | 33:735fd60e96d8 | 51 | Endpoint client; |
klauss | 34:f19d9735428e | 52 | bool valid_reboot = false; |
klauss | 34:f19d9735428e | 53 | uint8_t * data = NULL; |
klauss | 34:f19d9735428e | 54 | static int ext; |
klauss | 34:f19d9735428e | 55 | static int port; |
klauss | 34:f19d9735428e | 56 | static uint8_t type; |
klauss | 33:735fd60e96d8 | 57 | static Timer t; |
klauss | 34:f19d9735428e | 58 | static uint8_t status = udp_idle; |
klauss | 34:f19d9735428e | 59 | static uint8_t seq_num = 0; |
klauss | 35:96885a7931f0 | 60 | // OBS sizeof( buffer ) == 4 ???? |
klauss | 33:735fd60e96d8 | 61 | |
klauss | 35:96885a7931f0 | 62 | //*length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 35:96885a7931f0 | 63 | *length = server.receiveFrom( client, buffer, __UDP_ETH_BUFFER_SIZE__ ); |
klauss | 35:96885a7931f0 | 64 | |
klauss | 33:735fd60e96d8 | 65 | if( *length > 0){ |
klauss | 33:735fd60e96d8 | 66 | buffer[ *length ] = '\0'; |
klauss | 35:96885a7931f0 | 67 | buffer[ strlen( buffer ) - 1 ] = '\0'; |
klauss | 33:735fd60e96d8 | 68 | t.start(); |
klauss | 34:f19d9735428e | 69 | |
klauss | 35:96885a7931f0 | 70 | debug_msg("%s", buffer ); |
klauss | 35:96885a7931f0 | 71 | prompt_process( buffer ); |
klauss | 35:96885a7931f0 | 72 | |
klauss | 35:96885a7931f0 | 73 | return( NULL ); |
klauss | 35:96885a7931f0 | 74 | |
klauss | 34:f19d9735428e | 75 | data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); |
klauss | 34:f19d9735428e | 76 | |
klauss | 34:f19d9735428e | 77 | if( !data ){ |
klauss | 34:f19d9735428e | 78 | debug_msg("UDP pks parse failed"); |
klauss | 34:f19d9735428e | 79 | server.sendTo( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 80 | return( NULL ); |
klauss | 34:f19d9735428e | 81 | } |
klauss | 34:f19d9735428e | 82 | |
klauss | 34:f19d9735428e | 83 | if( status == udp_idle ){ |
klauss | 34:f19d9735428e | 84 | data[ 0 ] |= BIT7; |
klauss | 34:f19d9735428e | 85 | strcpy( (char *)&data[ 1 ], "Received listen request" ); |
klauss | 34:f19d9735428e | 86 | server.sendTo( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 87 | status = udp_waiting_start_pkg; |
klauss | 34:f19d9735428e | 88 | } |
klauss | 34:f19d9735428e | 89 | |
klauss | 34:f19d9735428e | 90 | if( status == udp_waiting_start_pkg ){ |
klauss | 34:f19d9735428e | 91 | static int count = 0; |
klauss | 34:f19d9735428e | 92 | if( is_start_pkg( data ) == 1 ) |
klauss | 34:f19d9735428e | 93 | if( count++ == __START_PKG_COUNT__ ) status = udp_listen; |
klauss | 33:735fd60e96d8 | 94 | |
klauss | 34:f19d9735428e | 95 | else{ |
klauss | 34:f19d9735428e | 96 | count = 0; |
klauss | 34:f19d9735428e | 97 | status = udp_idle; |
klauss | 34:f19d9735428e | 98 | } |
klauss | 34:f19d9735428e | 99 | } |
klauss | 34:f19d9735428e | 100 | |
klauss | 34:f19d9735428e | 101 | if( status == udp_listen ){ |
klauss | 34:f19d9735428e | 102 | debug_msg( "Received start pkgs" ); // mandar msg pro server |
klauss | 34:f19d9735428e | 103 | if( t.read() < 60 ){ |
klauss | 33:735fd60e96d8 | 104 | server.set_blocking( true, 10 ); |
klauss | 33:735fd60e96d8 | 105 | t.reset(); |
klauss | 34:f19d9735428e | 106 | do{ |
klauss | 34:f19d9735428e | 107 | wdt.kick(); |
klauss | 33:735fd60e96d8 | 108 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 109 | data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); |
klauss | 34:f19d9735428e | 110 | if( data ){ |
klauss | 34:f19d9735428e | 111 | if( !( is_start_pkg( data ) ) ) break; |
klauss | 34:f19d9735428e | 112 | }else{ |
klauss | 34:f19d9735428e | 113 | debug_msg("UDP pks parse failed"); |
klauss | 34:f19d9735428e | 114 | // reenvia pkg dizendo que podemos comecar com a transacao de dados |
klauss | 34:f19d9735428e | 115 | } |
klauss | 34:f19d9735428e | 116 | }while( t.read() < 60 ); |
klauss | 33:735fd60e96d8 | 117 | |
klauss | 34:f19d9735428e | 118 | if( t.read() < 60 ){ |
klauss | 33:735fd60e96d8 | 119 | //tratar o primeiro pkg depois dos zeros aqui |
klauss | 34:f19d9735428e | 120 | //a ideia é ir recebendo e colocando no arquivo "se for pra header" ou mandar |
klauss | 34:f19d9735428e | 121 | //pro cb depois, |
klauss | 34:f19d9735428e | 122 | if( !( ext || port ) ){ // pkg pra header |
klauss | 34:f19d9735428e | 123 | debug_msg("criando arquivo"); |
klauss | 34:f19d9735428e | 124 | }else{ |
klauss | 34:f19d9735428e | 125 | debug_msg("Mandando pkg pro cb"); |
klauss | 34:f19d9735428e | 126 | } |
klauss | 34:f19d9735428e | 127 | //FIXME mudar essa condicao de parada |
klauss | 34:f19d9735428e | 128 | while( !( is_time_to_stop( data ) == 1 ) ){ |
klauss | 33:735fd60e96d8 | 129 | wdt.kick(); |
klauss | 33:735fd60e96d8 | 130 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 131 | if( ( data[ 0 ] - seq_num ) != 1 ){ |
klauss | 34:f19d9735428e | 132 | //perdi algum pacote |
klauss | 34:f19d9735428e | 133 | //solicitar re-envio mandando o ultimo seq_num recebido talvez ... |
klauss | 34:f19d9735428e | 134 | continue; |
klauss | 34:f19d9735428e | 135 | }else{ |
klauss | 34:f19d9735428e | 136 | debug_msg("+(%d,%d)-%s+", ext, port, data ); |
klauss | 33:735fd60e96d8 | 137 | } |
klauss | 33:735fd60e96d8 | 138 | } |
klauss | 33:735fd60e96d8 | 139 | //pensar em como validar os pacotes recebidos, cks ... e o que mais ? |
klauss | 33:735fd60e96d8 | 140 | //se validado os pkgs, dar reset com os dados no arquivo bla, caso contrario, ignorar ... |
klauss | 33:735fd60e96d8 | 141 | if( valid_reboot ) NVIC_SystemReset(); |
klauss | 33:735fd60e96d8 | 142 | |
klauss | 33:735fd60e96d8 | 143 | else{ |
klauss | 33:735fd60e96d8 | 144 | server.set_blocking( false, 1 ); |
klauss | 33:735fd60e96d8 | 145 | t.stop(); |
klauss | 33:735fd60e96d8 | 146 | t.reset(); |
klauss | 33:735fd60e96d8 | 147 | debug_msg("Nao Resetando ... "); |
klauss | 33:735fd60e96d8 | 148 | } |
klauss | 33:735fd60e96d8 | 149 | } |
klauss | 33:735fd60e96d8 | 150 | } |
klauss | 33:735fd60e96d8 | 151 | }else return( buffer ); |
klauss | 33:735fd60e96d8 | 152 | }else return( NULL ); |
klauss | 33:735fd60e96d8 | 153 | return( NULL ); |
klauss | 33:735fd60e96d8 | 154 | } |
klauss | 33:735fd60e96d8 | 155 | |
klauss | 33:735fd60e96d8 | 156 | #endif |