Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
41:69bf7091c2ca
Parent:
38:af5ecd6997ab
Child:
42:480335bdde12
--- a/prompt.cpp	Thu Oct 02 18:19:54 2014 +0000
+++ b/prompt.cpp	Thu Oct 02 21:22:54 2014 +0000
@@ -6,8 +6,10 @@
 #include "flood.h"
 #include "debug.h"
 
-//#include "debug.h"
-
+TCPSocketServer server;
+TCPSocketConnection client;
+Timer tcp_timer;
+                    
 uint8_t debug_alive = 0;
 uint8_t debug_sip = 0;
 uint8_t debug_vector = 0;
@@ -22,6 +24,8 @@
 uint8_t debug_eth_tx = 0;
 uint8_t debug_file = 0;
 bool from_eth = false;
+bool udp_pkg;
+bool tcp_pkg;
 
 uint8_t test_debug = 1;
 #define PVERSION 1.1          // Sempre atualizar a versao do prompt
@@ -231,24 +235,63 @@
     }
     files('i');   // check if the files are created
     files('c');
+    
+    server.bind( TCP_PORT_LISTENER );
+    server.listen();
+    tcp_timer.start();
+    server.set_blocking( false, 1 );
     pc.printf("*******System Ready*******\n\r");
 }
 
 // main prompt process
-char * prompt_process( char * ethernet_msg ){
+char * prompt_process( char * msg_to_eth ){
     //FIXME dar flush na serial
     volatile char b = 0;
     static uint8_t bufret = 0;
     static Timer flood_timeout;
     bool miss_match = true;
+    char buffer[ 1024 ];
         
     if( flood_timeout.read() > 60 ){
         pflood = 0;
         flood_timeout.stop();
         flood_timeout.reset();
     }
-    
-    if( ethernet_msg == NULL ){
+    if( tcp_timer.read() > 3 ){
+        wdt.kick();
+        tcp_timer.reset();
+        if( !server.accept( client ) ){
+            //FIXME mudar essa condicao para parar depois de 30 sec for instance
+            while( tcp_timer.read() < 30 ){
+                client.set_blocking( false, 1 );
+                int n = client.receive(buffer, sizeof( buffer ));
+                if( n > 0 ){
+                    wdt.kick();
+                    tcp_timer.reset();
+                    
+                    last_bufptr = bufptr;
+                    debug_msg("%x -- %x", buffer[ n - 2 ], buffer[ n - 1 ] );
+                    bufptr = n - 2;//strlen( buffer );
+                    debug_msg("Sizeof pkg received (n, strlen( buffer ) ) (%d, %d)", n , bufptr );
+                    debug_buf = buffer;
+                    debug_buf[ n - 2 ] = 0;
+                    debug_msg("Command received %s::endl", debug_buf );
+                    from_eth = true;
+                    tcp_pkg = true;
+                    bufret = 1;
+                }
+            }
+            if( tcp_timer.read() >= 30 ){
+                tcp_timer.reset();
+                tcp_pkg = false;
+                debug_buf = __debug_buf__;
+                bufptr = last_bufptr;
+                bufret = 0;
+                from_eth = false;            
+                return( NULL );
+            }
+        }
+    }else if( msg_to_eth == NULL ){
         if ( pc.readable() ){
             // imput do teclado
             b = pc.getc();
@@ -269,8 +312,8 @@
         }
     }else{
         last_bufptr = bufptr;
-        bufptr = strlen( ethernet_msg );
-        debug_buf = ethernet_msg;
+        bufptr = strlen( msg_to_eth );
+        debug_buf = msg_to_eth;
         from_eth = true;
         bufret = 1;
     }
@@ -657,14 +700,23 @@
         else strcpy( last_debug_buf, debug_buf );
     }
     if( from_eth ){
-        ethernet_msg = debug_buf;
+        if( tcp_pkg ){
+            debug_msg("");
+            client.send_all( debug_buf, bufptr );
+            tcp_pkg = false;
+            tcp_timer.reset();
+            return( NULL );
+        }
+        
+        msg_to_eth = debug_buf;
         debug_buf = __debug_buf__;
         bufptr = last_bufptr;
         bufret = 0;
         from_eth = false;
+        
         if( miss_match ) return( NULL );
         
-        else return( ethernet_msg );
+        else return( msg_to_eth );
     }
     if ( b == 0x0D || bufret == 1 || bufptr > DEBUGBUFSIZE ) {
         bufptr = 0;