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.cpp@67:cdedc64d9921, 2014-10-30 (annotated)
- Committer:
- klauss
- Date:
- Thu Oct 30 19:26:41 2014 +0000
- Revision:
- 67:cdedc64d9921
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 | #include "bootloader_cbx_handler.h" | 
| klauss | 67:cdedc64d9921 | 2 | |
| klauss | 67:cdedc64d9921 | 3 | Timer bl_timer; | 
| klauss | 67:cdedc64d9921 | 4 | bool listen_udp = false; | 
| klauss | 67:cdedc64d9921 | 5 | char last_client_ip[ 16 ]; | 
| klauss | 67:cdedc64d9921 | 6 | int last_client_port; | 
| klauss | 67:cdedc64d9921 | 7 | UDPSocket bl_cbx_server; | 
| klauss | 67:cdedc64d9921 | 8 | Endpoint bl_cbx_client; | 
| klauss | 67:cdedc64d9921 | 9 | |
| klauss | 67:cdedc64d9921 | 10 | int init_bl_handler( void ){ | 
| klauss | 67:cdedc64d9921 | 11 | bl_timer.stop(); | 
| klauss | 67:cdedc64d9921 | 12 | bl_timer.reset(); | 
| klauss | 67:cdedc64d9921 | 13 | bl_timer.start(); | 
| klauss | 67:cdedc64d9921 | 14 | |
| klauss | 67:cdedc64d9921 | 15 | int bind_ret = bl_cbx_server.bind( UDP_BOOTLOADER_PORT ); | 
| klauss | 67:cdedc64d9921 | 16 | |
| klauss | 67:cdedc64d9921 | 17 | bl_cbx_server.set_blocking( false, 2 ); | 
| klauss | 67:cdedc64d9921 | 18 | |
| klauss | 67:cdedc64d9921 | 19 | return( bind_ret ); | 
| klauss | 67:cdedc64d9921 | 20 | } | 
| klauss | 67:cdedc64d9921 | 21 | |
| klauss | 67:cdedc64d9921 | 22 | char * listen_bl_socket( char * buffer, int *length ){ | 
| klauss | 67:cdedc64d9921 | 23 | *length = bl_cbx_server.receiveFrom( bl_cbx_client, buffer, __CB_BUFFER_SIZE__ ); | 
| klauss | 67:cdedc64d9921 | 24 | |
| klauss | 67:cdedc64d9921 | 25 | if( *length == -1 ){ | 
| klauss | 67:cdedc64d9921 | 26 | int ret = bl_cbx_server.bind( UDP_BOOTLOADER_PORT ); | 
| klauss | 67:cdedc64d9921 | 27 | |
| klauss | 67:cdedc64d9921 | 28 | if( ret == -1 ){ | 
| klauss | 67:cdedc64d9921 | 29 | bl_cbx_server.close(); | 
| klauss | 67:cdedc64d9921 | 30 | bl_cbx_server.bind( UDP_BOOTLOADER_PORT ); | 
| klauss | 67:cdedc64d9921 | 31 | //FIXME colocar alguma variavel de status pra nem mandar msgs eth caso nao tenha conectado | 
| klauss | 67:cdedc64d9921 | 32 | } | 
| klauss | 67:cdedc64d9921 | 33 | }else if( *length > 0){ | 
| klauss | 67:cdedc64d9921 | 34 | buffer[ *length ] = '\0'; | 
| klauss | 67:cdedc64d9921 | 35 | buffer[ strlen( buffer ) - 1 ] = '\0'; | 
| klauss | 67:cdedc64d9921 | 36 | bl_timer.reset(); | 
| klauss | 67:cdedc64d9921 | 37 | return( buffer ); | 
| klauss | 67:cdedc64d9921 | 38 | } | 
| klauss | 67:cdedc64d9921 | 39 | return( NULL ); | 
| klauss | 67:cdedc64d9921 | 40 | } | 
| klauss | 67:cdedc64d9921 | 41 | |
| klauss | 67:cdedc64d9921 | 42 | int bl_handler( void ){ | 
| klauss | 67:cdedc64d9921 | 43 | int length = 0; | 
| klauss | 67:cdedc64d9921 | 44 | char bl_msg[ 320 ]; | 
| klauss | 67:cdedc64d9921 | 45 | char * bl_data; | 
| klauss | 67:cdedc64d9921 | 46 | int bl_ext; | 
| klauss | 67:cdedc64d9921 | 47 | int bl_port; | 
| klauss | 67:cdedc64d9921 | 48 | static int id_msg = 0x11; | 
| klauss | 67:cdedc64d9921 | 49 | |
| klauss | 67:cdedc64d9921 | 50 | if( listen_udp ){ | 
| klauss | 67:cdedc64d9921 | 51 | listen_bl_socket( bl_msg, &length ); | 
| klauss | 67:cdedc64d9921 | 52 | if( bl_timer.read() > 60 ){ | 
| klauss | 67:cdedc64d9921 | 53 | listen_udp = false; | 
| klauss | 67:cdedc64d9921 | 54 | bl_timer.reset(); | 
| klauss | 67:cdedc64d9921 | 55 | } | 
| klauss | 67:cdedc64d9921 | 56 | }else if( bl_timer.read() > 7 && !listen_udp ){ | 
| klauss | 67:cdedc64d9921 | 57 | listen_bl_socket( bl_msg, &length ); | 
| klauss | 67:cdedc64d9921 | 58 | if( length > 0 ){ | 
| klauss | 67:cdedc64d9921 | 59 | listen_udp = true; | 
| klauss | 67:cdedc64d9921 | 60 | strcpy( last_client_ip, bl_cbx_client.get_address() ); | 
| klauss | 67:cdedc64d9921 | 61 | last_client_port = bl_cbx_client.get_port(); | 
| klauss | 67:cdedc64d9921 | 62 | }else bl_timer.reset(); | 
| klauss | 67:cdedc64d9921 | 63 | } | 
| klauss | 67:cdedc64d9921 | 64 | |
| klauss | 67:cdedc64d9921 | 65 | if( length > 0 ){ | 
| klauss | 67:cdedc64d9921 | 66 | uint8_t bl_msg_to_cbx[ __CB_BUFFER_SIZE__ ]; | 
| klauss | 67:cdedc64d9921 | 67 | send_msg("Bootloader order received"); | 
| klauss | 67:cdedc64d9921 | 68 | bl_data = strtok( bl_msg, " " ); | 
| klauss | 67:cdedc64d9921 | 69 | bl_ext = atoi( bl_data ); | 
| klauss | 67:cdedc64d9921 | 70 | bl_data += strlen( bl_data) + 1; | 
| klauss | 67:cdedc64d9921 | 71 | |
| klauss | 67:cdedc64d9921 | 72 | bl_port = convert_ext_to_port( bl_ext ); | 
| klauss | 67:cdedc64d9921 | 73 | |
| klauss | 67:cdedc64d9921 | 74 | send_msg("Received msg from :: %s:%d to CBx %i", last_client_ip, last_client_port, bl_ext ); | 
| klauss | 67:cdedc64d9921 | 75 | send_msg("Message received == %s--", bl_data ); | 
| klauss | 67:cdedc64d9921 | 76 | __send_to_cb__( __build_cb_package__( bl_ext, bl_port, __BOOTLOADER_CBX__, bl_data, id_msg++, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, bl_msg_to_cbx ) ); | 
| klauss | 67:cdedc64d9921 | 77 | |
| klauss | 67:cdedc64d9921 | 78 | //FIXME talvez nao seja preciso do strcat | 
| klauss | 67:cdedc64d9921 | 79 | strcat( bl_data, "\n\r" ); | 
| klauss | 67:cdedc64d9921 | 80 | int sent = bl_cbx_server.sendTo( bl_cbx_client, bl_data, strlen( bl_data ) ); | 
| klauss | 67:cdedc64d9921 | 81 | |
| klauss | 67:cdedc64d9921 | 82 | if( dbl ) send_msg("%i bytes de resposta", sent); | 
| klauss | 67:cdedc64d9921 | 83 | |
| klauss | 67:cdedc64d9921 | 84 | return( sent ); | 
| klauss | 67:cdedc64d9921 | 85 | } | 
| klauss | 67:cdedc64d9921 | 86 | return( 0 ); | 
| klauss | 67:cdedc64d9921 | 87 | } | 
| klauss | 67:cdedc64d9921 | 88 | |
| klauss | 67:cdedc64d9921 | 89 | int bl_cbx_reply_to_eth( int ext, char * eth_data ){ | 
| klauss | 67:cdedc64d9921 | 90 | int sent = bl_cbx_server.sendTo( bl_cbx_client, eth_data, strlen( eth_data ) ); | 
| klauss | 67:cdedc64d9921 | 91 | |
| klauss | 67:cdedc64d9921 | 92 | if( dbl ) send_msg( "%i bytes de resposta", sent ); | 
| klauss | 67:cdedc64d9921 | 93 | |
| klauss | 67:cdedc64d9921 | 94 | return( sent ); | 
| klauss | 67:cdedc64d9921 | 95 | } | 
