![](/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@34:f19d9735428e, 2014-09-30 (annotated)
- Committer:
- klauss
- Date:
- Tue Sep 30 14:28:01 2014 +0000
- Revision:
- 34:f19d9735428e
- Parent:
- 33:735fd60e96d8
- Child:
- 35:96885a7931f0
implementacao da funcionalidade de listen de uma porta udo arbitraria
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 | |
klauss | 34:f19d9735428e | 37 | if( !data ) return( 0 ); |
klauss | 34:f19d9735428e | 38 | |
klauss | 34:f19d9735428e | 39 | if( is_stop_pkg( data ) == 1 ) count++; |
klauss | 34:f19d9735428e | 40 | |
klauss | 34:f19d9735428e | 41 | else count = 0; |
klauss | 34:f19d9735428e | 42 | |
klauss | 34:f19d9735428e | 43 | if( count == 10 ) return( 1 ); |
klauss | 34:f19d9735428e | 44 | |
klauss | 34:f19d9735428e | 45 | else return( 0 ); |
klauss | 34:f19d9735428e | 46 | } |
klauss | 34:f19d9735428e | 47 | |
klauss | 33:735fd60e96d8 | 48 | char * listen_eth_udp( char * buffer, int *length ){ |
klauss | 34:f19d9735428e | 49 | static UDPSocket server; |
klauss | 33:735fd60e96d8 | 50 | server.set_blocking( false, 1 ); |
klauss | 33:735fd60e96d8 | 51 | server.bind( UDP_PORT_LISTENER ); |
klauss | 33:735fd60e96d8 | 52 | Endpoint client; |
klauss | 34:f19d9735428e | 53 | bool valid_reboot = false; |
klauss | 34:f19d9735428e | 54 | uint8_t * data = NULL; |
klauss | 34:f19d9735428e | 55 | static int ext; |
klauss | 34:f19d9735428e | 56 | static int port; |
klauss | 34:f19d9735428e | 57 | static uint8_t type; |
klauss | 33:735fd60e96d8 | 58 | static Timer t; |
klauss | 34:f19d9735428e | 59 | static uint8_t status = udp_idle; |
klauss | 34:f19d9735428e | 60 | static uint8_t seq_num = 0; |
klauss | 33:735fd60e96d8 | 61 | |
klauss | 34:f19d9735428e | 62 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 33:735fd60e96d8 | 63 | |
klauss | 33:735fd60e96d8 | 64 | if( *length > 0){ |
klauss | 33:735fd60e96d8 | 65 | buffer[ *length ] = '\0'; |
klauss | 33:735fd60e96d8 | 66 | t.start(); |
klauss | 34:f19d9735428e | 67 | |
klauss | 34:f19d9735428e | 68 | data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); |
klauss | 34:f19d9735428e | 69 | |
klauss | 34:f19d9735428e | 70 | if( !data ){ |
klauss | 34:f19d9735428e | 71 | debug_msg("UDP pks parse failed"); |
klauss | 34:f19d9735428e | 72 | server.sendTo( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 73 | return( NULL ); |
klauss | 34:f19d9735428e | 74 | } |
klauss | 34:f19d9735428e | 75 | |
klauss | 34:f19d9735428e | 76 | if( status == udp_idle ){ |
klauss | 34:f19d9735428e | 77 | data[ 0 ] |= BIT7; |
klauss | 34:f19d9735428e | 78 | strcpy( (char *)&data[ 1 ], "Received listen request" ); |
klauss | 34:f19d9735428e | 79 | server.sendTo( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 80 | status = udp_waiting_start_pkg; |
klauss | 34:f19d9735428e | 81 | } |
klauss | 34:f19d9735428e | 82 | |
klauss | 34:f19d9735428e | 83 | if( status == udp_waiting_start_pkg ){ |
klauss | 34:f19d9735428e | 84 | static int count = 0; |
klauss | 34:f19d9735428e | 85 | if( is_start_pkg( data ) == 1 ) |
klauss | 34:f19d9735428e | 86 | if( count++ == __START_PKG_COUNT__ ) status = udp_listen; |
klauss | 33:735fd60e96d8 | 87 | |
klauss | 34:f19d9735428e | 88 | else{ |
klauss | 34:f19d9735428e | 89 | count = 0; |
klauss | 34:f19d9735428e | 90 | status = udp_idle; |
klauss | 34:f19d9735428e | 91 | } |
klauss | 34:f19d9735428e | 92 | } |
klauss | 34:f19d9735428e | 93 | |
klauss | 34:f19d9735428e | 94 | if( status == udp_listen ){ |
klauss | 34:f19d9735428e | 95 | debug_msg( "Received start pkgs" ); // mandar msg pro server |
klauss | 34:f19d9735428e | 96 | if( t.read() < 60 ){ |
klauss | 33:735fd60e96d8 | 97 | server.set_blocking( true, 10 ); |
klauss | 33:735fd60e96d8 | 98 | t.reset(); |
klauss | 34:f19d9735428e | 99 | do{ |
klauss | 34:f19d9735428e | 100 | wdt.kick(); |
klauss | 33:735fd60e96d8 | 101 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 102 | data = __parse_vz_pkg__( &ext, &port, &type, (uint8_t *)buffer ); |
klauss | 34:f19d9735428e | 103 | if( data ){ |
klauss | 34:f19d9735428e | 104 | if( !( is_start_pkg( data ) ) ) break; |
klauss | 34:f19d9735428e | 105 | }else{ |
klauss | 34:f19d9735428e | 106 | debug_msg("UDP pks parse failed"); |
klauss | 34:f19d9735428e | 107 | // reenvia pkg dizendo que podemos comecar com a transacao de dados |
klauss | 34:f19d9735428e | 108 | } |
klauss | 34:f19d9735428e | 109 | }while( t.read() < 60 ); |
klauss | 33:735fd60e96d8 | 110 | |
klauss | 34:f19d9735428e | 111 | if( t.read() < 60 ){ |
klauss | 33:735fd60e96d8 | 112 | //tratar o primeiro pkg depois dos zeros aqui |
klauss | 34:f19d9735428e | 113 | //a ideia é ir recebendo e colocando no arquivo "se for pra header" ou mandar |
klauss | 34:f19d9735428e | 114 | //pro cb depois, |
klauss | 34:f19d9735428e | 115 | if( !( ext || port ) ){ // pkg pra header |
klauss | 34:f19d9735428e | 116 | debug_msg("criando arquivo"); |
klauss | 34:f19d9735428e | 117 | }else{ |
klauss | 34:f19d9735428e | 118 | debug_msg("Mandando pkg pro cb"); |
klauss | 34:f19d9735428e | 119 | } |
klauss | 34:f19d9735428e | 120 | //FIXME mudar essa condicao de parada |
klauss | 34:f19d9735428e | 121 | while( !( is_time_to_stop( data ) == 1 ) ){ |
klauss | 33:735fd60e96d8 | 122 | wdt.kick(); |
klauss | 33:735fd60e96d8 | 123 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
klauss | 34:f19d9735428e | 124 | if( ( data[ 0 ] - seq_num ) != 1 ){ |
klauss | 34:f19d9735428e | 125 | //perdi algum pacote |
klauss | 34:f19d9735428e | 126 | //solicitar re-envio mandando o ultimo seq_num recebido talvez ... |
klauss | 34:f19d9735428e | 127 | continue; |
klauss | 34:f19d9735428e | 128 | }else{ |
klauss | 34:f19d9735428e | 129 | debug_msg("+(%d,%d)-%s+", ext, port, data ); |
klauss | 33:735fd60e96d8 | 130 | } |
klauss | 33:735fd60e96d8 | 131 | } |
klauss | 33:735fd60e96d8 | 132 | //pensar em como validar os pacotes recebidos, cks ... e o que mais ? |
klauss | 33:735fd60e96d8 | 133 | //se validado os pkgs, dar reset com os dados no arquivo bla, caso contrario, ignorar ... |
klauss | 33:735fd60e96d8 | 134 | if( valid_reboot ) NVIC_SystemReset(); |
klauss | 33:735fd60e96d8 | 135 | |
klauss | 33:735fd60e96d8 | 136 | else{ |
klauss | 33:735fd60e96d8 | 137 | server.set_blocking( false, 1 ); |
klauss | 33:735fd60e96d8 | 138 | t.stop(); |
klauss | 33:735fd60e96d8 | 139 | t.reset(); |
klauss | 33:735fd60e96d8 | 140 | debug_msg("Nao Resetando ... "); |
klauss | 33:735fd60e96d8 | 141 | } |
klauss | 33:735fd60e96d8 | 142 | } |
klauss | 33:735fd60e96d8 | 143 | } |
klauss | 33:735fd60e96d8 | 144 | }else return( buffer ); |
klauss | 33:735fd60e96d8 | 145 | }else return( NULL ); |
klauss | 33:735fd60e96d8 | 146 | return( NULL ); |
klauss | 33:735fd60e96d8 | 147 | } |
klauss | 33:735fd60e96d8 | 148 | |
klauss | 33:735fd60e96d8 | 149 | #endif |