![](/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
fw_handler.cpp@114:472502b31a12, 2015-04-20 (annotated)
- Committer:
- klauss
- Date:
- Mon Apr 20 14:40:09 2015 +0000
- Revision:
- 114:472502b31a12
- Parent:
- 113:db67ae00550e
- Child:
- 116:39a41ebb675c
antes de implementar os retrys da header -> cbx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 99:e80850c51106 | 1 | #include "fw.h" |
klauss | 99:e80850c51106 | 2 | |
klauss | 99:e80850c51106 | 3 | UDPSocket fw_sock; |
klauss | 99:e80850c51106 | 4 | Endpoint fw_server; |
klauss | 99:e80850c51106 | 5 | |
klauss | 99:e80850c51106 | 6 | void init_fw_handler( void ){ |
klauss | 99:e80850c51106 | 7 | char buff_ip[ 16 ] = ""; |
klauss | 99:e80850c51106 | 8 | char buff_port[ 8 ] = ""; |
klauss | 99:e80850c51106 | 9 | int fw_port = 0; |
klauss | 99:e80850c51106 | 10 | |
klauss | 99:e80850c51106 | 11 | FILE *fp = fopen( "/qspi/fw_ip.txt", "r"); |
klauss | 114:472502b31a12 | 12 | //debug_msg(""); |
klauss | 99:e80850c51106 | 13 | if( fp == NULL ){ |
klauss | 114:472502b31a12 | 14 | if( debug_file ) debug_msg("Failed to open /qspi/fw_ip.txt" ); |
klauss | 99:e80850c51106 | 15 | strncpy( buff_ip, __FW_SERVER_IP__, 20 ); |
klauss | 114:472502b31a12 | 16 | //debug_msg(""); |
klauss | 99:e80850c51106 | 17 | }else{ |
klauss | 114:472502b31a12 | 18 | //debug_msg(""); |
klauss | 99:e80850c51106 | 19 | int read = fread( buff_ip, 1, 512, fp ); |
klauss | 99:e80850c51106 | 20 | if( read > 0 ){ |
klauss | 99:e80850c51106 | 21 | for( int i = 0; i < read; i++ ) |
klauss | 99:e80850c51106 | 22 | if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){ |
klauss | 99:e80850c51106 | 23 | buff_ip[ i ] = '\0'; |
klauss | 99:e80850c51106 | 24 | break; |
klauss | 99:e80850c51106 | 25 | } |
klauss | 114:472502b31a12 | 26 | if( debug_file ) debug_msg("fw server ip %s", buff_ip ); |
klauss | 114:472502b31a12 | 27 | //debug_msg(""); |
klauss | 99:e80850c51106 | 28 | }else{ |
klauss | 114:472502b31a12 | 29 | if( debug_file ) debug_msg("Failed to open /qspi/fw_ip.txt" ); |
klauss | 99:e80850c51106 | 30 | strncpy( buff_ip, __FW_SERVER_IP__, 20 ); |
klauss | 114:472502b31a12 | 31 | //debug_msg(""); |
klauss | 99:e80850c51106 | 32 | } |
klauss | 99:e80850c51106 | 33 | } |
klauss | 99:e80850c51106 | 34 | if( fp != NULL ) fclose( fp ); |
klauss | 114:472502b31a12 | 35 | //debug_msg(""); |
klauss | 99:e80850c51106 | 36 | fp = fopen( "/qspi/fw_port.txt", "r"); |
klauss | 99:e80850c51106 | 37 | if( fp == NULL ) { |
klauss | 114:472502b31a12 | 38 | if( debug_fw ) debug_msg("Failed to open /qspi/fw_port.txt" ); |
klauss | 99:e80850c51106 | 39 | fw_port = __FW_SERVER_PORT__; |
klauss | 99:e80850c51106 | 40 | } else { |
klauss | 114:472502b31a12 | 41 | //debug_msg(""); |
klauss | 99:e80850c51106 | 42 | if( fread( (void *)buff_port, 1, 32, fp ) > 0 ) { |
klauss | 99:e80850c51106 | 43 | fw_port = atoi( buff_port ); |
klauss | 114:472502b31a12 | 44 | if( debug_fw ) send_msg( "fw server port %d", fw_port ); |
klauss | 99:e80850c51106 | 45 | } else { |
klauss | 114:472502b31a12 | 46 | if( debug_fw ) send_msg( "Failed to read /qspi/fw_port.txt" ); |
klauss | 99:e80850c51106 | 47 | fw_port = __FW_SERVER_PORT__; |
klauss | 99:e80850c51106 | 48 | } |
klauss | 99:e80850c51106 | 49 | } |
klauss | 114:472502b31a12 | 50 | if( fp != NULL ) fclose( fp ); |
klauss | 114:472502b31a12 | 51 | //debug_msg(""); |
klauss | 99:e80850c51106 | 52 | int addr = fw_server.set_address( buff_ip , fw_port ); |
klauss | 114:472502b31a12 | 53 | if( debug_fw ) send_msg(" Valor de retorno set_address -- %d ", addr ); |
klauss | 99:e80850c51106 | 54 | fw_sock.set_blocking( false, 0 ); |
klauss | 114:472502b31a12 | 55 | //debug_msg(""); |
klauss | 99:e80850c51106 | 56 | int bind = fw_sock.bind( __FW_HEADER_PORT__ ); |
klauss | 114:472502b31a12 | 57 | if( debug_fw ) send_msg(" Valor de retorno bind -- %d ", bind ); |
klauss | 114:472502b31a12 | 58 | //debug_msg(""); |
klauss | 99:e80850c51106 | 59 | } |
klauss | 99:e80850c51106 | 60 | |
klauss | 99:e80850c51106 | 61 | void re_start_fw( void ){ |
klauss | 99:e80850c51106 | 62 | int close = fw_sock.close(); |
klauss | 114:472502b31a12 | 63 | if( debug_fw ) send_msg(" Valor de retorno close-- %d ", close ); |
klauss | 99:e80850c51106 | 64 | |
klauss | 99:e80850c51106 | 65 | init_fw_handler(); |
klauss | 99:e80850c51106 | 66 | } |
klauss | 99:e80850c51106 | 67 | |
klauss | 99:e80850c51106 | 68 | void fw_cbx_pkg( int ext, int port, char * data ){ |
klauss | 99:e80850c51106 | 69 | static uint8_t fw_cbx_pkg_id = 0x00; |
klauss | 114:472502b31a12 | 70 | data[ __CB_BUFFER_SIZE__ - 1 ] = fw_cbx_pkg_id++; |
klauss | 99:e80850c51106 | 71 | |
klauss | 114:472502b31a12 | 72 | int8_t sent = 0; |
klauss | 114:472502b31a12 | 73 | do{ |
klauss | 114:472502b31a12 | 74 | sent = fw_sock.sendTo( fw_server, data, __CB_BUFFER_SIZE__ ); |
klauss | 114:472502b31a12 | 75 | }while( sent == 0 ); |
klauss | 99:e80850c51106 | 76 | } |