Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
44:cc4996469404
Parent:
43:455522f98de5
Child:
45:c80574f24dc3
--- a/prompt.cpp	Mon Oct 06 14:31:54 2014 +0000
+++ b/prompt.cpp	Mon Oct 06 19:26:36 2014 +0000
@@ -1,13 +1,7 @@
-#include "mbed.h"
-#include "stdint.h"
-#include "configs.h"
-#include "vz_protocol.h"
-#include "parallelcpld.h"
-#include "flood.h"
-#include "debug.h"
+#include "prompt.h"
 
 TCPSocketServer server;
-TCPSocketConnection client;
+TCPSocketConnection eth_client;
 Timer tcp_timer;
                     
 uint8_t debug_alive = 0;
@@ -27,7 +21,7 @@
 bool from_eth = false;
 bool udp_request = false;
 bool tcp_session = false;
-bool udp_pkg;
+bool udp_pkg = false;
 
 uint8_t test_debug = 1;
 #define PVERSION 3          // Sempre atualizar a versao do prompt
@@ -92,7 +86,7 @@
         buff[ num ] = '\0';
         pc.printf( buff );
         if( from_eth ){
-            if( tcp_session ) client.send_all( buff, strlen( buff ) );
+            if( tcp_session ) eth_client.send_all( buff, strlen( buff ) );
         }
     }
     fclose(fp);
@@ -105,56 +99,56 @@
         pc.printf("\n\rHeader IP " );
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rHeader IP " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/myip.txt");
         
         pc.printf("\n\rHeader ext ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rHeader ext " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/myext.txt");
         
         pc.printf("\n\rHeader port ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rHeader port " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/mysipport.txt");
         
         pc.printf("\n\rServer IP ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rServer IP " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/serverip.txt");
         
         pc.printf("\n\rServer ext ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rServer ext " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/peerext.txt");
         
         pc.printf("\n\rServer port ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rServer port " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/serverport.txt");
         
         pc.printf("\n\rMascara de rede ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rMascara de rede " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/mymask.txt");
         
         pc.printf("\n\rGateway IP ");
         if( from_eth ){
             snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rGateway IP " );
-            if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+            if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
         }
         cat("/qspi/mygate.txt");
         
@@ -228,7 +222,18 @@
             fprintf(fgate, "%s\n\r",__MY_GTW__);    //mygateway
             exists = false;
         }
-        if( !exists ) pc.printf("\n\r Default configurations set! \n\r");
+        if( !exists ){ 
+            pc.printf("\n\rDefault configurations set!\n\r");
+            if( from_eth ){
+                snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Default configurations set!\n\r");
+                debug_buf[ strlen( debug_buf ) - 1 ] = '\0';
+                if( tcp_session ){
+                    eth_client.send_all( debug_buf, strlen( debug_buf ) );
+                    debug_buf[ 0 ] = 0;
+                    bufptr = 0;
+                }
+            }
+        }
     }
     
     if (type == 'r' ){                // Just open for read
@@ -263,24 +268,31 @@
         fprintf( fsport, "%i\n\r",__SERVER_PORT__ );  //asterisk port
         
         fprintf( fmask, "%s",__MY_MSK__ );     //mymask
-        fprintf( fgate, "%i\n\r", __MY_GTW__ );    //mygateway
+        fprintf( fgate, "%s\n\r", __MY_GTW__ );    //mygateway
         
         files('c');
         
-        pc.printf("\n\r Default configurations set! \n\r");
-             
+        pc.printf("\n\rDefault configurations set!\n\r");
+        if( from_eth ){
+            snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "\n\rDefault configurations set!\n\r");
+            debug_buf[ strlen( debug_buf ) - 1 ] = '\0';
+            if( tcp_session ){
+                eth_client.send_all( debug_buf, strlen( debug_buf ) );
+                debug_buf[ 0 ] = 0;
+                bufptr = 0;
+            }
+        } 
     }
-    
 }
 void init_fsystem(){                 
    //  init file system and check if files exist
     if (!qspifs.isformatted()) {
         qspifs.format();
-        pc.printf("File system configured!!\n\r");
+        pc.printf("\n\rFile system configured!!\n\r");
     }
     files('i');   // check if the files are created
     files('c');
-    pc.printf("*******System Ready*******\n\r");
+    pc.printf("\n\r*******File System Ready*******\n\r");
 }
 
 void init_prompt(){
@@ -299,7 +311,7 @@
     static Timer flood_timeout;
     bool miss_match = true;
     char buffer[ 1024 ];
-    do{    
+    //do{    
         if( flood_timeout.read() > 60 ){
             pflood = 0;
             flood_timeout.stop();
@@ -309,30 +321,30 @@
             wdt.kick();
             tcp_timer.reset();
             if( tcp_session ){            
-                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();                    
-                        buffer[ n - 2 ] = 0;
-                        bufptr = n - 2;
-                        if( !( strcmp( buffer, "quit" ) ) ){
-                            if( debug_prompt_eth ) pc.printf( "\n\rquit request\n\r" );
-                            debug_buf = __debug_buf__;
-                            bufptr = last_bufptr;
-                            tcp_session = false;
-                            client.close();
-                            bufret = 0;
-                            return( NULL );
-                        }
-                        from_eth = true;
-                        bufret = 1;
-                        break;
+            
+                eth_client.set_blocking( false, 1 );
+                int n = eth_client.receive( buffer, sizeof( buffer ) );
+                if( n > 0 ){
+                    wdt.kick();
+                    tcp_timer.reset();                    
+                    buffer[ n - 2 ] = 0;
+                    bufptr = n - 2;
+                    if( !( strcmp( buffer, "quit" ) ) ){
+                        if( debug_prompt_eth ) pc.printf( "\n\rquit request\n\r" );
+                        debug_buf = __debug_buf__;
+                        bufptr = last_bufptr;
+                        from_eth = false;
+                        tcp_session = false;
+                        eth_client.close();
+                        bufret = 0;
+                        return( NULL );
                     }
+                    from_eth = true;
+                    bufret = 1;
+                    //break;
                 }
             }else{ 
-                if( !server.accept( client ) ){
+                if( !server.accept( eth_client ) ){
                     tcp_session = true;
                     from_eth = true;
                     last_bufptr = bufptr;
@@ -342,14 +354,15 @@
                     bufret = 0;
                 }
             }
-            if( tcp_timer.read() >= 30 ){
+            if( tcp_timer.read() >= TCP_IDLE_MAX_TIME ){
                 tcp_timer.reset();
                 tcp_session = false;
                 debug_buf = __debug_buf__;
                 bufptr = last_bufptr;
                 bufret = 0;
                 from_eth = false;
-                tcp_session = false;      
+                tcp_session = false;
+                eth_client.close();
                 return( NULL );
             }
         }else if( !tcp_session ){
@@ -404,9 +417,10 @@
                 if( from_eth ){
                     snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Formatando o sistema de arquivos... espere o sistema reiniciar\n\r");
                     debug_buf[ strlen( debug_buf ) - 1 ] = '\0';
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
-                    client.close();
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
+                    eth_client.close();
                 }
+                files('w'); // pode dar pala
                 NVIC_SystemReset();
             }
     
@@ -495,8 +509,8 @@
                 if( from_eth ){
                     snprintf( debug_buf, __PROMPT_ETH_BUFFER_SIZE__, "Job is done\n\r");
                     debug_buf[ strlen( debug_buf ) - 1 ] = '\0';
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
-                    client.close();
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
+                    eth_client.close();
                 }
                 NVIC_SystemReset();
             }
@@ -732,102 +746,102 @@
                 pc.printf("\n\r****************************PROMPT HELP******************\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "\n\r****************************PROMPT HELP******************\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("ifconfig                        - mostra o arquivo de configuracao do sistema\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "ifconfig                        - mostra o arquivo de configuracao do sistema\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("dconfig                         - volta as configuracoes do sistema para o padrao de fabrica\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "dconfig                         - volta as configuracoes do sistema para o padrao de fabrica\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("callme [ramal porta]            - envia o pedido de ligacao para o callbox com o ramal e porta indicada\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "callme [ramal porta]            - envia o pedido de ligacao para o callbox com o ramal e porta indicada\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("promptcb ramal porta comando    - envia o <comando> para o cbx <ramal> e <porta> executar\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "promptcb ramal porta comando    - envia o <comando> para o cbx <ramal> e <porta> executar\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("format                          - formata o sistema de arquivos\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "format                          - formata o sistema de arquivos\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("reset                           - resta o sistema\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "reset                           - resta o sistema\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("ipset [ip]                      - Configura o IP da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "ipset [ip]                      - Configura o IP da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("extset [ext]                    - Configura a ext da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "extset [ext]                    - Configura a ext da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("msipport [port]                 - Configura a porta SIP da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "msipport [port]                 - Configura a porta SIP da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("serverip [ip]                   - Configura o ip do servidor asterisk\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "serverip [ip]                   - Configura o ip do servidor asterisk\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("serextset [ext]                 - Configura a server ext da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "serextset [ext]                 - Configura a server ext da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("ssport [port]                   - Configura a porta SIP do servidor asterisk\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "ssport [port]                   - Configura a porta SIP do servidor asterisk\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("maskset [mask]                  - Configura a mascara da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "maskset [mask]                  - Configura a mascara da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("gatewayset [gateway]            - Configura o gateway da cabeceira\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "gatewayset [gateway]            - Configura o gateway da cabeceira\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("showcb                          - lista os Cbx registrados na header\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "showcb                          - lista os Cbx registrados na header\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("cks                             - exibe estatisticas de check sum\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "cks                             - exibe estatisticas de check sum\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf(".                               - executa o comando anterior novamente\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, ".                               - executa o comando anterior novamente\n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("debug [on|off|show]             - seleciona debugs gerais | lista de debugs \n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "debug [on|off|show]             - seleciona debugs gerais | lista de debugs \n\r" );
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("flood [on|off]                  - simula envio de pacotes de audio\n\r");
                 if( from_eth ){
                     snprintf( debug_buf,__PROMPT_ETH_BUFFER_SIZE__, "flood [on|off]                  - simula envio de pacotes de audio\n\r");
-                    if( tcp_session ) client.send_all( debug_buf, strlen( debug_buf ) );
+                    if( tcp_session ) eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 }
                 pc.printf("PROMPT VERSION: V%d\n\r",PVERSION);
                 //Obs :: a ultima linha nao mandamos direto pro tcp porque ela eh enviada fora desse escopo
@@ -885,12 +899,14 @@
                 }
             }
             
-            else strcpy( last_debug_buf, debug_buf );
+            else{
+                if( debug_buf[ 0 ] != 0x0D ) strcpy( last_debug_buf, debug_buf );
+            }
         }
         /*
         if( from_eth ){
             if( tcp_session ){
-                client.send_all( debug_buf, strlen( debug_buf ) );
+                eth_client.send_all( debug_buf, strlen( debug_buf ) );
                 tcp_timer.reset();
                 bufret = 0;
                 from_eth = false;
@@ -906,25 +922,25 @@
                 else return( msg_to_eth );
             }
         }*/
-        
-        if( tcp_session ){
-            strcat( debug_buf, "\n\r> " );
-            client.send_all( debug_buf, strlen( debug_buf ) );
-            tcp_timer.reset();
-            bufret = 0;
-            from_eth = false;
-        }else if( udp_request ){
-            msg_to_eth = debug_buf;
-            debug_buf = __debug_buf__;
-            bufptr = last_bufptr;
-            bufret = 0;
-            from_eth = false;
-            
-            if( miss_match ) return( NULL );
-            
-            else return( msg_to_eth );
-        }       
+               
         if( b == 0x0D || bufret == 1 || bufptr > DEBUGBUFSIZE ){
+            if( tcp_session ){
+                strcat( debug_buf, "\n\r> " );
+                eth_client.send_all( debug_buf, strlen( debug_buf ) );
+                tcp_timer.reset();
+                bufret = 0;
+                //from_eth = false;
+            }else if( udp_request ){
+                msg_to_eth = debug_buf;
+                debug_buf = __debug_buf__;
+                bufptr = last_bufptr;
+                bufret = 0;
+                from_eth = false;
+                
+                if( miss_match ) return( NULL );
+                
+                else return( msg_to_eth );
+            }        
             bufptr = 0;
             for (uint8_t i =0; i < DEBUGBUFSIZE; i++) {
                 debug_buf[i] = 0;
@@ -934,7 +950,7 @@
             pc.putc(0x0D);
             pc.printf("> ");
         }    
-    }while( tcp_session );
+    //}while( tcp_session );
     
     return( NULL );
 }
\ No newline at end of file