Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
68:b54993674190
Parent:
63:0d95da692bb4
Child:
69:65665afbad5d
diff -r cdedc64d9921 -r b54993674190 utils.cpp
--- a/utils.cpp	Thu Oct 30 19:26:41 2014 +0000
+++ b/utils.cpp	Fri Nov 07 17:57:55 2014 +0000
@@ -4,6 +4,9 @@
 EthernetInterface eth;
 uint8_t cb_tx_buffer[ __CB_BUFFER_SIZE__ ];
 
+UDPSocket t_sock;
+Endpoint t_server;
+
 void reverse( char str[], int length ){
     int start = 0;
     int end = length -1;
@@ -66,7 +69,8 @@
                 }
             if( debug_file ) debug_msg("Eth ip %s", buff_ip );
         }else{
-            if( debug_file ) if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" );
+            //if( debug_file ) if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" );
+            if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" );
             strncpy( buff_ip, __MY_IP__, 20 );
         }
     }
@@ -228,11 +232,19 @@
     strcat( aux, " " );
     
     //strncat( aux, (char * )data, __TELEMETRY_SIZE__ );
-    
+    /*
+    pc.printf("\n\r 1. ");
+    uint8_t count = 2;
+    for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ){
+        if( i > 0 && !( i % 15 )  ) pc.printf("\n\r%2i. ", count++ );
+        pc.printf("%3x ", data[ i ] );
+    }
+    pc.printf("\n\r");
+    */
+    //pulando o byte de id ( sequence number )
+    data += __SEQ_NUM_SIZE__;
     //pulando os bytes do clock
     data += __CLOCK_SYNC_SIZE__;
-    //pulando o byte de id ( sequence number )
-    data += __SEQ_NUM_SIZE__;
     
     uint8_t offset = strlen( aux );
     
@@ -240,6 +252,16 @@
     
     for( register uint16_t i = __TELEMETRY_SIZE__ + offset; i < __CB_BUFFER_SIZE__; i++ ) aux[ i ] = 0;
     
+    int sent = t_sock.sendTo( t_server, aux, __CB_BUFFER_SIZE__ );
+    if( debug_telemetry ) send_msg(" Valor de retorno sent-- %d ", sent );
+    
+    if( sent == -1 ){ 
+        re_start_telemetry();
+        sent = t_sock.sendTo( t_server, aux, __CB_BUFFER_SIZE__ );
+        
+        if( debug_telemetry ) send_msg(" Valor de retorno sent-- %d ", sent );
+    }
+    
     if( debug_telemetry ){
         pc.printf("\n\r 1. ");
         uint8_t count = 2;
@@ -249,24 +271,24 @@
         }
         pc.printf("\n\r");
         
-        if( tcp_session ) tcp_client.send_all( ( char *)aux, __CB_BUFFER_SIZE__ );
+        char msg_to_eth[ 1024 ];
+        strcpy( msg_to_eth, tmp );
+        strcat( msg_to_eth, " " );
+        for( register uint16_t i = 0; i < __TELEMETRY_SIZE__; i++ ){
+            strcat( msg_to_eth, itoa( data[ i ], tmp, 16 ) );
+            strcat( msg_to_eth, " " );
+        }
+        for( register uint16_t i = __TELEMETRY_SIZE__; i < __CB_BUFFER_SIZE__ - 1; i++ ){
+            strcat( msg_to_eth, itoa( 0, tmp, 16 ) );
+            strcat( msg_to_eth, " " );
+        }
+        strcat( msg_to_eth, itoa( 0, tmp, 16 ) );
+        
+        if( tcp_session ){ 
+            int debug_sent = tcp_client.send_all( msg_to_eth, strlen( msg_to_eth ) );
+            if( debug_telemetry ) send_msg("%d - bytes enviados pro prompt-eth", debug_sent );
+        }
     }
-    
-    UDPSocket t_sock;
-    Endpoint t_server;
-    
-    int adr = t_server.set_address( __TELEMETRY_SERVER_IP__ , __TELEMETRY_SERVER_PORT__ );
-    if( debug_telemetry ) send_msg("Valor de retorno adr -- %d", adr );
-    t_sock.set_blocking( false, 1 );
-    
-    int bind = t_sock.bind( __TELEMETRY_HEADER_PORT__ );
-    if( debug_telemetry ) send_msg("Valor de retorno bind -- %d", bind );
-
-    int ret = t_sock.sendTo( t_server, aux, __CB_BUFFER_SIZE__ );
-    if( debug_telemetry ) send_msg("Valor de retorno sent-- %d", ret );
-    
-    int close = t_sock.close();
-    if( debug_telemetry ) send_msg("Valor de retorno close-- %d", close );
 }
 
 void __send_to_cb__( uint8_t * buffer ){
@@ -274,5 +296,62 @@
     xmemcpy( cb_tx_buffer, buffer, __CB_BUFFER_SIZE__ );
     send2callboxes();
 }
+void init_telemetry_handler( void ){
+    //FIXME isso deveria ler do arquivo e nao do define.
+    
+    char buff_ip[ 16 ] = "";
+    char buff_port[ 8 ] = "";
+    int telemetry_port = 0;
+    
+    FILE *fp = fopen( "/qspi/telemetry_ip.txt", "r");
+    if( fp == NULL ){
+        if( debug_file ) debug_msg("Failed to open /qspi/telemetry_ip.txt" );
+        strncpy( buff_ip, __TELEMETRY_SERVER_IP__, 20 );    
+    }else{
+        int read = fread( buff_ip, 1, 512, fp );
+        if( read > 0 ){
+            for( int i = 0; i < read; i++ ) 
+                if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){
+                    buff_ip[ i ] = '\0';
+                    break;
+                }
+            if( debug_file ) debug_msg("Telemetry server ip %s", buff_ip );
+        }else{
+            if( debug_file ) debug_msg("Failed to open /qspi/telemetry_ip.txt" );
+            strncpy( buff_ip, __TELEMETRY_SERVER_IP__, 20 );
+        }
+    }
+    fclose( fp );
+    
+    fp = fopen( "/qspi/telemetry_port.txt", "r");
+    if( fp == NULL ) {
+        if( debug_sip ) debug_msg("Failed to open /qspi/telemetry_port.txt" );
+        telemetry_port = __TELEMETRY_SERVER_PORT__;
+    } else {
+        if( fread( (void *)buff_port, 1, 32, fp ) > 0 ) {
+            telemetry_port = atoi( buff_port );
+            if( debug_sip ) debug_msg("Telemetry server port %d", buff_port );
+        } else {
+            if( debug_sip ) debug_msg("Failed to read /qspi/telemetry_port.txt" );
+            telemetry_port = __TELEMETRY_SERVER_PORT__;
+        }
+    }
+    fclose( fp );    
+    
+    //int addr = t_server.set_address( __TELEMETRY_SERVER_IP__ , __TELEMETRY_SERVER_PORT__ );
+    int addr = t_server.set_address( buff_ip , telemetry_port );
+    if( debug_telemetry ) send_msg(" Valor de retorno set_address -- %d ", addr );
+    t_sock.set_blocking( false, 1 );
+    
+    int bind = t_sock.bind( __TELEMETRY_HEADER_PORT__ );
+    if( debug_telemetry ) send_msg(" Valor de retorno bind -- %d ", bind );
+}
+
+void re_start_telemetry( void ){
+    int close = t_sock.close();
+    if( debug_telemetry ) send_msg(" Valor de retorno close-- %d ", close );
+    
+    init_telemetry_handler();
+}
 
 int ls_comp( const void * a, const void * b ){ return( *( int * )a - *( int * )b ); }
\ No newline at end of file