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