Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
bootloader_cbx_handler.h@67:cdedc64d9921, 2014-10-30 (annotated)
- Committer:
- klauss
- Date:
- Thu Oct 30 19:26:41 2014 +0000
- Revision:
- 67:cdedc64d9921
- Child:
- 68:b54993674190
implementacao do protocolo de comunicacao eth->header->cbx_bootloader
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| klauss | 67:cdedc64d9921 | 1 | #ifndef __BOOTLOADER_CBX_HANDLER_H__ |
| klauss | 67:cdedc64d9921 | 2 | #define __BOOTLOADER_CBX_HANDLER_H__ |
| klauss | 67:cdedc64d9921 | 3 | |
| klauss | 67:cdedc64d9921 | 4 | #include "prompt.h" |
| klauss | 67:cdedc64d9921 | 5 | #include "debug.h" |
| klauss | 67:cdedc64d9921 | 6 | #include "vz_protocol.h" |
| klauss | 67:cdedc64d9921 | 7 | |
| klauss | 67:cdedc64d9921 | 8 | #define udp_idle 0x00 |
| klauss | 67:cdedc64d9921 | 9 | #define udp_waiting_start_pkg 0x01 |
| klauss | 67:cdedc64d9921 | 10 | #define udp_listen 0x02 |
| klauss | 67:cdedc64d9921 | 11 | |
| klauss | 67:cdedc64d9921 | 12 | extern bool listen_udp; |
| klauss | 67:cdedc64d9921 | 13 | extern Timer bl_timer; |
| klauss | 67:cdedc64d9921 | 14 | extern UDPSocket bl_cbx_server; |
| klauss | 67:cdedc64d9921 | 15 | extern Endpoint bl_cbx_client; |
| klauss | 67:cdedc64d9921 | 16 | |
| klauss | 67:cdedc64d9921 | 17 | extern char last_client_ip[ 16 ]; |
| klauss | 67:cdedc64d9921 | 18 | extern int last_client_port; |
| klauss | 67:cdedc64d9921 | 19 | |
| klauss | 67:cdedc64d9921 | 20 | int init_bl_handler( void ); |
| klauss | 67:cdedc64d9921 | 21 | char * listen_bl_socket( char * buffer, int *length ); |
| klauss | 67:cdedc64d9921 | 22 | int bl_handler( void ); |
| klauss | 67:cdedc64d9921 | 23 | int bl_cbx_reply_to_eth( int ext, char * data ); |
| klauss | 67:cdedc64d9921 | 24 | |
| klauss | 67:cdedc64d9921 | 25 | /* |
| klauss | 67:cdedc64d9921 | 26 | int is_start_pkg( uint8_t * data ){ |
| klauss | 67:cdedc64d9921 | 27 | //seq_num must to be 0s |
| klauss | 67:cdedc64d9921 | 28 | if( !data ) return -1; |
| klauss | 67:cdedc64d9921 | 29 | |
| klauss | 67:cdedc64d9921 | 30 | for( register int i = __VZ_HEADER_OFFSET__; i < __PROMPT_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); |
| klauss | 67:cdedc64d9921 | 31 | |
| klauss | 67:cdedc64d9921 | 32 | return( 1 ); |
| klauss | 67:cdedc64d9921 | 33 | } |
| klauss | 67:cdedc64d9921 | 34 | |
| klauss | 67:cdedc64d9921 | 35 | int is_stop_pkg( uint8_t * data ){ |
| klauss | 67:cdedc64d9921 | 36 | //seq_num must to be 0s |
| klauss | 67:cdedc64d9921 | 37 | if( !data ) return -1; |
| klauss | 67:cdedc64d9921 | 38 | |
| klauss | 67:cdedc64d9921 | 39 | for( register int i = __VZ_HEADER_OFFSET__; i < __PROMPT_ETH_BUFFER_SIZE__; i++ ) if( data[ i ] ) return( 0 ); |
| klauss | 67:cdedc64d9921 | 40 | |
| klauss | 67:cdedc64d9921 | 41 | return( 1 ); |
| klauss | 67:cdedc64d9921 | 42 | } |
| klauss | 67:cdedc64d9921 | 43 | |
| klauss | 67:cdedc64d9921 | 44 | int is_time_to_stop( uint8_t * data ){ |
| klauss | 67:cdedc64d9921 | 45 | static int count = 0; |
| klauss | 67:cdedc64d9921 | 46 | if( !data ) return( 0 ); |
| klauss | 67:cdedc64d9921 | 47 | |
| klauss | 67:cdedc64d9921 | 48 | if( is_stop_pkg( data ) == 1 ) count++; |
| klauss | 67:cdedc64d9921 | 49 | |
| klauss | 67:cdedc64d9921 | 50 | else count = 0; |
| klauss | 67:cdedc64d9921 | 51 | |
| klauss | 67:cdedc64d9921 | 52 | if( count == 10 ) return( 1 ); |
| klauss | 67:cdedc64d9921 | 53 | |
| klauss | 67:cdedc64d9921 | 54 | else return( 0 ); |
| klauss | 67:cdedc64d9921 | 55 | } |
| klauss | 67:cdedc64d9921 | 56 | |
| klauss | 67:cdedc64d9921 | 57 | */ |
| klauss | 67:cdedc64d9921 | 58 | |
| klauss | 67:cdedc64d9921 | 59 | |
| klauss | 67:cdedc64d9921 | 60 | /* |
| klauss | 67:cdedc64d9921 | 61 | char * listen_bl_socket( char * buffer, int *length ){ |
| klauss | 67:cdedc64d9921 | 62 | static UDPSocket server; |
| klauss | 67:cdedc64d9921 | 63 | server.set_blocking( false, 2 ); |
| klauss | 67:cdedc64d9921 | 64 | server.bind( UDP_BOOTLOADER_PORT ); |
| klauss | 67:cdedc64d9921 | 65 | Endpoint client; |
| klauss | 67:cdedc64d9921 | 66 | //bool valid_reboot = false; |
| klauss | 67:cdedc64d9921 | 67 | uint8_t * data = NULL; |
| klauss | 67:cdedc64d9921 | 68 | static int bl_ext; |
| klauss | 67:cdedc64d9921 | 69 | static int bl_port; |
| klauss | 67:cdedc64d9921 | 70 | //static uint8_t type; |
| klauss | 67:cdedc64d9921 | 71 | static Timer t; |
| klauss | 67:cdedc64d9921 | 72 | //static uint8_t status = udp_idle; |
| klauss | 67:cdedc64d9921 | 73 | //static uint8_t seq_num = 0; |
| klauss | 67:cdedc64d9921 | 74 | //OBS sizeof( buffer ) == 4 ???? |
| klauss | 67:cdedc64d9921 | 75 | //length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
| klauss | 67:cdedc64d9921 | 76 | *length = server.receiveFrom( client, buffer, __PROMPT_ETH_BUFFER_SIZE__ ); |
| klauss | 67:cdedc64d9921 | 77 | |
| klauss | 67:cdedc64d9921 | 78 | if( *length == -1 ){ |
| klauss | 67:cdedc64d9921 | 79 | int ret = server.bind( UDP_BOOTLOADER_PORT ); |
| klauss | 67:cdedc64d9921 | 80 | |
| klauss | 67:cdedc64d9921 | 81 | if( ret == -1 ){ |
| klauss | 67:cdedc64d9921 | 82 | server.close(); |
| klauss | 67:cdedc64d9921 | 83 | server.bind( UDP_BOOTLOADER_PORT ); |
| klauss | 67:cdedc64d9921 | 84 | } |
| klauss | 67:cdedc64d9921 | 85 | } |
| klauss | 67:cdedc64d9921 | 86 | |
| klauss | 67:cdedc64d9921 | 87 | if( *length > 0){ |
| klauss | 67:cdedc64d9921 | 88 | buffer[ *length ] = '\0'; |
| klauss | 67:cdedc64d9921 | 89 | buffer[ strlen( buffer ) - 1 ] = '\0'; |
| klauss | 67:cdedc64d9921 | 90 | t.start(); |
| klauss | 67:cdedc64d9921 | 91 | bl_timer.reset(); |
| klauss | 67:cdedc64d9921 | 92 | |
| klauss | 67:cdedc64d9921 | 93 | return( NULL ); |
| klauss | 67:cdedc64d9921 | 94 | } |
| klauss | 67:cdedc64d9921 | 95 | |
| klauss | 67:cdedc64d9921 | 96 | //data = __parse_vz_pkg__( &bl_ext, &bl_port, __BOOT_LOADER__, (uint8_t *)buffer ); |
| klauss | 67:cdedc64d9921 | 97 | |
| klauss | 67:cdedc64d9921 | 98 | if( !data ){ |
| klauss | 67:cdedc64d9921 | 99 | debug_msg("UDP pks parse failed"); |
| klauss | 67:cdedc64d9921 | 100 | server.sendTo( client, buffer, sizeof( buffer ) ); |
| klauss | 67:cdedc64d9921 | 101 | return( NULL ); |
| klauss | 67:cdedc64d9921 | 102 | } |
| klauss | 67:cdedc64d9921 | 103 | */ |
| klauss | 67:cdedc64d9921 | 104 | |
| klauss | 67:cdedc64d9921 | 105 | /* |
| klauss | 67:cdedc64d9921 | 106 | if( status == udp_idle ){ |
| klauss | 67:cdedc64d9921 | 107 | data[ 0 ] |= BIT7; |
| klauss | 67:cdedc64d9921 | 108 | strcpy( (char *)&data[ 1 ], "Received listen request" ); |
| klauss | 67:cdedc64d9921 | 109 | server.sendTo( client, buffer, sizeof( buffer ) ); |
| klauss | 67:cdedc64d9921 | 110 | status = udp_waiting_start_pkg; |
| klauss | 67:cdedc64d9921 | 111 | } |
| klauss | 67:cdedc64d9921 | 112 | */ |
| klauss | 67:cdedc64d9921 | 113 | |
| klauss | 67:cdedc64d9921 | 114 | /* |
| klauss | 67:cdedc64d9921 | 115 | if( status == udp_waiting_start_pkg ){ |
| klauss | 67:cdedc64d9921 | 116 | static int count = 0; |
| klauss | 67:cdedc64d9921 | 117 | if( is_start_pkg( data ) == 1 ) |
| klauss | 67:cdedc64d9921 | 118 | if( count++ == __START_PKG_COUNT__ ) status = udp_listen; |
| klauss | 67:cdedc64d9921 | 119 | |
| klauss | 67:cdedc64d9921 | 120 | else{ |
| klauss | 67:cdedc64d9921 | 121 | count = 0; |
| klauss | 67:cdedc64d9921 | 122 | status = udp_idle; |
| klauss | 67:cdedc64d9921 | 123 | } |
| klauss | 67:cdedc64d9921 | 124 | } |
| klauss | 67:cdedc64d9921 | 125 | */ |
| klauss | 67:cdedc64d9921 | 126 | /* |
| klauss | 67:cdedc64d9921 | 127 | if( status == udp_listen ){ |
| klauss | 67:cdedc64d9921 | 128 | debug_msg( "Received start pkgs" ); // mandar msg pro server |
| klauss | 67:cdedc64d9921 | 129 | if( t.read() < 60 ){ |
| klauss | 67:cdedc64d9921 | 130 | server.set_blocking( true, 10 ); |
| klauss | 67:cdedc64d9921 | 131 | t.reset(); |
| klauss | 67:cdedc64d9921 | 132 | do{ |
| klauss | 67:cdedc64d9921 | 133 | wdt.kick(); |
| klauss | 67:cdedc64d9921 | 134 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
| klauss | 67:cdedc64d9921 | 135 | //data = __parse_vz_pkg__( &bl_ext, &_blport, &type, (uint8_t *)buffer ); |
| klauss | 67:cdedc64d9921 | 136 | if( data ){ |
| klauss | 67:cdedc64d9921 | 137 | if( !( is_start_pkg( data ) ) ) break; |
| klauss | 67:cdedc64d9921 | 138 | }else{ |
| klauss | 67:cdedc64d9921 | 139 | debug_msg("UDP pks parse failed"); |
| klauss | 67:cdedc64d9921 | 140 | // reenvia pkg dizendo que podemos comecar com a transacao de dados |
| klauss | 67:cdedc64d9921 | 141 | } |
| klauss | 67:cdedc64d9921 | 142 | }while( t.read() < 60 ); |
| klauss | 67:cdedc64d9921 | 143 | |
| klauss | 67:cdedc64d9921 | 144 | if( t.read() < 60 ){ |
| klauss | 67:cdedc64d9921 | 145 | //tratar o primeiro pkg depois dos zeros aqui |
| klauss | 67:cdedc64d9921 | 146 | //a ideia é ir recebendo e colocando no arquivo "se for pra header" ou mandar |
| klauss | 67:cdedc64d9921 | 147 | //pro cb depois, |
| klauss | 67:cdedc64d9921 | 148 | if( !( ext || port ) ){ // pkg pra header |
| klauss | 67:cdedc64d9921 | 149 | debug_msg("criando arquivo"); |
| klauss | 67:cdedc64d9921 | 150 | }else{ |
| klauss | 67:cdedc64d9921 | 151 | debug_msg("Mandando pkg pro cb"); |
| klauss | 67:cdedc64d9921 | 152 | } |
| klauss | 67:cdedc64d9921 | 153 | //FIXME mudar essa condicao de parada |
| klauss | 67:cdedc64d9921 | 154 | while( !( is_time_to_stop( data ) == 1 ) ){ |
| klauss | 67:cdedc64d9921 | 155 | wdt.kick(); |
| klauss | 67:cdedc64d9921 | 156 | *length = server.receiveFrom( client, buffer, sizeof( buffer ) ); |
| klauss | 67:cdedc64d9921 | 157 | if( ( data[ 0 ] - seq_num ) != 1 ){ |
| klauss | 67:cdedc64d9921 | 158 | //perdi algum pacote |
| klauss | 67:cdedc64d9921 | 159 | //solicitar re-envio mandando o ultimo seq_num recebido talvez ... |
| klauss | 67:cdedc64d9921 | 160 | continue; |
| klauss | 67:cdedc64d9921 | 161 | }else{ |
| klauss | 67:cdedc64d9921 | 162 | debug_msg("+(%d,%d)-%s+", ext, port, data ); |
| klauss | 67:cdedc64d9921 | 163 | } |
| klauss | 67:cdedc64d9921 | 164 | } |
| klauss | 67:cdedc64d9921 | 165 | //pensar em como validar os pacotes recebidos, cks ... e o que mais ? |
| klauss | 67:cdedc64d9921 | 166 | //se validado os pkgs, dar reset com os dados no arquivo bla, caso contrario, ignorar ... |
| klauss | 67:cdedc64d9921 | 167 | //if( valid_reboot ) NVIC_SystemReset(); |
| klauss | 67:cdedc64d9921 | 168 | |
| klauss | 67:cdedc64d9921 | 169 | //else{ |
| klauss | 67:cdedc64d9921 | 170 | // server.set_blocking( false, 1 ); |
| klauss | 67:cdedc64d9921 | 171 | // t.stop(); |
| klauss | 67:cdedc64d9921 | 172 | // t.reset(); |
| klauss | 67:cdedc64d9921 | 173 | // debug_msg("Nao Resetando ... "); |
| klauss | 67:cdedc64d9921 | 174 | //} |
| klauss | 67:cdedc64d9921 | 175 | |
| klauss | 67:cdedc64d9921 | 176 | } |
| klauss | 67:cdedc64d9921 | 177 | } |
| klauss | 67:cdedc64d9921 | 178 | }else return( buffer ); |
| klauss | 67:cdedc64d9921 | 179 | *length = 0; |
| klauss | 67:cdedc64d9921 | 180 | return( NULL ); |
| klauss | 67:cdedc64d9921 | 181 | //return( NULL ); |
| klauss | 67:cdedc64d9921 | 182 | } |
| klauss | 67:cdedc64d9921 | 183 | */ |
| klauss | 67:cdedc64d9921 | 184 | #endif |
