Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
28:94aec56c6329
Parent:
27:98f824719d1c
Child:
29:7246460b73f8
diff -r 98f824719d1c -r 94aec56c6329 prompt.cpp
--- a/prompt.cpp	Mon Sep 22 21:15:07 2014 +0000
+++ b/prompt.cpp	Tue Sep 23 18:28:37 2014 +0000
@@ -4,23 +4,25 @@
 #include "vz_protocol.h"
 #include "parallelcpld.h"
 #include "flood.h"
+#include "debug.h"
 
 //#include "debug.h"
 
-uint8_t debug_alive = 1;
+uint8_t debug_alive = 0;
+uint8_t debug_sip = 0;
 uint8_t pcks_s = 0;
 uint8_t pshowcb = 0;
-uint8_t debug_sip = 0;
+
+uint8_t test_debug = 0;
 #define PVERSION 1          // Sempre atualizar a versao do prompt
 
 #define DEBUGBUFSIZE 50
 char debug_buf[DEBUGBUFSIZE];
-FILE *fip,*fmask,*fgate,*fport,*fsip,*fsport; 
+FILE *fip, *fmask, *fgate, *fport, *fsip, *fsport, *fext, *fserext;
 uint8_t dog = 1;
 uint8_t debug_prompt = 0;
 uint8_t bufptr = 0;
  
-
 uint8_t xmemmatch(const uint8_t * s1, const uint8_t * s2, uint16_t size) {      // presente da lib.h pode retirar da versao final
   while (size--) {
     if (*s1++ != *s2++) return 0; /* does not match */
@@ -63,7 +65,7 @@
     
     FILE *fp = fopen(fname, "r");
     if (fp == NULL) {
-        pc.printf("Failed to open %s\n", fname);
+        pc.printf("Failed to open %s", fname);
         return;
     }
     
@@ -76,65 +78,86 @@
     fclose(fp);
 }
 
-
-void files (const char type) {                                          // operantion with the system config files
-       
+// operantion with the system config files
+void files (const char type) {                                                 
     if (type == 's' ){                // show files
         pc.printf("\n\r");
-        pc.printf("1)IP, 2)MASK, 3)GATEWAY, 4)SIP PORT, 5)SERVER IP, 6)SERVER SIP PORT\n\r");
+        pc.printf("Header IP " );
         cat("/qspi/myip.txt");
+        pc.printf("\n\rHeader ext ");
+        cat("/qspi/myext.txt");
+        pc.printf("\n\rHeader port ");
+        cat("/qspi/mysipport.txt");
+        
+        pc.printf("\n\rServer ip ");
+        cat("/qspi/serverip.txt");
+        pc.printf("\n\rServer ext ");
+        cat("/qspi/peerext.txt");
+        pc.printf("\n\rServer port ");
+        cat("/qspi/serverport.txt");
+        
+        pc.printf("\n\rMascara de rede ");
         cat("/qspi/mymask.txt");
+        pc.printf("\n\rGateway IP ");
         cat("/qspi/mygate.txt");
-        cat("/qspi/mysipport.txt");
-        cat("/qspi/serverip.txt");
-        cat("/qspi/serverport.txt");
     }
     
-    
     if (type == 'c' ){                // close all files
-        fclose(fip);
-        fclose(fmask);
-        fclose(fgate);
-        fclose(fport);
-        fclose(fsip);
-        fclose(fsport);
-        
+        fclose( fip );
+        fclose( fmask );
+        fclose( fgate );
+        fclose( fport );
+        fclose( fsip );
+        fclose( fsport );
+        fclose( fext );
+        fclose( fserext );
     }    
     
     if (type == 'i' ){                // Check if files exist, if not create the files
         fip = fopen("/qspi/myip.txt", "r");
         if (fip == NULL){
             fip = fopen("/qspi/myip.txt", "w");
-            //fprintf(fip, "192.168.120.169\n\r");   //myip
             fprintf(fip, "%s\n\r",__MY_IP__);   //myip
         }
+        
+        fserext = fopen("/qspi/myext.txt", "r");
+        if ( fserext == NULL ){
+             fserext = fopen("/qspi/myext.txt", "w");
+             fprintf( fserext, "%i\n\r" ,__MY_EXT__ );  //asterisk ext
+        }
+        fport = fopen("/qspi/mysipport.txt", "r");
+        if (fport == NULL){
+            fport = fopen("/qspi/mysipport.txt", "w");
+            fprintf(fport, "%i\n\r",__MY_PORT__);  //mysipport
+        }
+        fsip = fopen("/qspi/serverip.txt", "r");
+        if (fsip == NULL){
+             fsip = fopen("/qspi/serverip.txt", "w");
+             fprintf(fsip, "%s\n\r",__SERVER_IP__ );  //asterisk ip
+        }
+        
+        fext = fopen("/qspi/peerext.txt", "r");
+        if ( fext == NULL ){
+             fext = fopen("/qspi/peerext.txt", "w");
+             fprintf( fext, "%d\n\r",__PEER_EXT__ );  //asterisk ip
+        }
+        fsport = fopen("/qspi/serverport.txt", "r");
+        if (fsport == NULL){
+            fsport = fopen("/qspi/serverport.txt", "w");
+            fprintf(fsport, "%i\n\r",__SERVER_PORT__ );  //asterisk port
+        pc.printf("\n\r Default configurations set! \n\r");
+        }
+        
         fmask = fopen("/qspi/mymask.txt", "r");
         if (fmask == NULL){
             fmask = fopen("/qspi/mymask.txt", "w");
-            fprintf(fmask, "255.255.255.0\n\r");     //mymask
+            fprintf(fmask, "%s\n\r",__MY_MSK__);     //mymask
         }
         fgate = fopen("/qspi/mygateway.txt", "r");
         if (fgate == NULL){
             fgate = fopen("/qspi/mygate.txt", "w");
-            fprintf(fgate, "192.168.2.254\n\r");    //mygateway
-        }
-        fport = fopen("/qspi/mysipport.txt", "r");
-        if (fport == NULL){
-            fport = fopen("/qspi/mysipport.txt", "w");
-            fprintf(fport, "5075\n\r");  //mysipport
+            fprintf(fgate, "%s\n\r",__MY_GTW__);    //mygateway
         }
-        fsip = fopen("/qspi/serverip.txt", "r");
-        if (fsip == NULL){
-             fsip = fopen("/qspi/serverip.txt", "w");
-             fprintf(fsip, "192.168.30.25\n\r");  //asterisk ip
-        }
-        fsport = fopen("/qspi/serverport.txt", "r");
-        if (fsport == NULL){
-            fsport = fopen("/qspi/serverport.txt", "w");
-            fprintf(fsport, "5060\n\r");  //asterisk port
-        pc.printf("\n\r Default configurations set! \n\r");
-        }
-        
     }
     
     if (type == 'r' ){                // Just open for read
@@ -144,6 +167,8 @@
         fport = fopen("/qspi/mysipport.txt", "r");
         fsip = fopen("/qspi/serverip.txt", "r");
         fsport = fopen("/qspi/serverport.txt", "r");
+        fext = fopen( "/qspi/myext.txt", "r" );
+        fserext = fopen( "/qspi/peerext.txt", "r" );
     }
         
     if (type == 'w'){                 // Create and write the default configs
@@ -153,13 +178,20 @@
         fport = fopen("/qspi/mysipport.txt", "w");
         fsip = fopen("/qspi/serverip.txt", "w");
         fsport = fopen("/qspi/serverport.txt", "w");   
+        fext = fopen( "/qspi/myext.txt", "w" );
+        fserext = fopen( "/qspi/peerext.txt", "w" );
         
-        fprintf(fip, "%s\n\r", __MY_IP__ );   //myip
-        fprintf(fmask, "255.255.255.0\n\r");     //mymask
-        fprintf(fgate, "192.168.2.254\n\r");    //mygateway
-        fprintf(fport, "5075\n\r");  //mysipport
-        fprintf(fsip, "192.168.30.25\n\r");  //asterisk ip
-        fprintf(fsport, "5060\n\r");  //asterisk port
+        fprintf( fip, "%s\n\r", __MY_IP__  );   //myip
+        fprintf( fport, "%d\n\r", __MY_PORT__ );  //mysipport
+        //fprintf( fext, "%d\n\r", __MY_EXT__ );  //myext
+        fprintf( fext, "%d\n\r", 820 );  //myext
+        
+        fprintf( fsip, "%s\n\r", __SERVER_IP__  );  //asterisk ip
+        fprintf( fserext, "%d\n\r",__PEER_EXT__ );  //asterisk port
+        fprintf( fsport, "%d\n\r",__SERVER_PORT__ );  //asterisk port
+        
+        fprintf( fmask, "%s\n\r",__MY_MSK__ );     //mymask
+        fprintf( fgate, "%s\n\r", __MY_GTW__ );    //mygateway
         
         files('c');
         
@@ -180,8 +212,10 @@
     pc.printf("*******System Ready*******\n\r");
 }
 
-
-void prompt_process(){              // main prompt process
+// main prompt process
+void prompt_process(){
+    //FIXME dar flush na serial
+    
     volatile char b = 0;
     static uint8_t bufret = 0;
     
@@ -199,26 +233,54 @@
             }
         }
         
-    if (bufret == 1) {                  // Prompt commands here
+    // Prompt commands here
+    if (bufret == 1) {     
+        bool miss_match = true;
+        
+        if( !bufptr ){
+            miss_match = false;
+        }     
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "sconfig" )) {
+            miss_match = false;
             pc.printf("\n\r");
             files('s');
         }
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "format" )) {
+            miss_match = false;
             pc.printf("\n Formatando o sistema de arquivos... espere o sistema reiniciar \n\r");
             //qspifs.format();
             NVIC_SystemReset();
         }
 
-        if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "ipset ",6 )) {
+        if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "ipset ", 6 )) {
+            miss_match = false;
             fip = fopen("/qspi/myip.txt", "w");
             fprintf(fip,"%s\n\r",(debug_buf+6));
             fclose(fip);
             pc.printf("\n\r");
             files('s');
         }
+        
+        if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "extset ", 7 )) {
+            miss_match = false;
+            fip = fopen("/qspi/myext.txt", "w");
+            fprintf(fip, "%s\n\r" , ( debug_buf + 7) );
+            fclose(fip);
+            pc.printf("\n\r");
+            files('s');
+        }
+        
+        if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "serextset ", 10 )) {
+            miss_match = false;
+            fip = fopen("/qspi/peerext.txt", "w");
+            fprintf(fip, "%s\n\r" , ( debug_buf + 10 ) );
+            fclose(fip);
+            pc.printf("\n\r");
+            files('s');
+        }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "maskset ",8 )) {
+            miss_match = false;
             fmask = fopen("/qspi/mymask.txt", "w");
             fprintf(fmask,"%s\n\r",(debug_buf+8));
             fclose(fmask);
@@ -227,6 +289,7 @@
         }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "gatewayset ",11 )) {
+            miss_match = false;
             fgate = fopen("/qspi/mygate.txt", "w");
             fprintf(fgate,"%s\n\r",(debug_buf+11));
             fclose(fgate);
@@ -236,6 +299,7 @@
 
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "msipport ",9 )) {
+            miss_match = false;
             fport = fopen("/qspi/mysipport.txt", "w");
             fprintf(fport,"%s\n\r",(debug_buf+9));
             fclose(fport);
@@ -244,6 +308,7 @@
         }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "serverip ",9 )) {
+            miss_match = false;
             fsip = fopen("/qspi/serverip.txt", "w");
             fprintf(fsip,"%s\n\r",(debug_buf+9));
             fclose(fsip);
@@ -252,6 +317,7 @@
         }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "ssport ",7 )) {
+            miss_match = false;
             fsport = fopen("/qspi/serverport.txt", "w");
             fprintf(fsport,"%s\n\r",(debug_buf+7));
             fclose(fsport);
@@ -261,24 +327,28 @@
 
 
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "dconfig" )) {
+            miss_match = false;
             files('w');
             files('s');
-
         }
 
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "reset" )) {
-            pc.printf("\n\r reset!!! \n\r");
+            miss_match = false;
+            //pc.printf("\n\r reset!!! \n\r");
+            pc.printf("\n\rJob is done\n\r");
             pc.putc(0x01);
             NVIC_SystemReset();
         }
 
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "dog" )) {
+            miss_match = false;
             pc.printf("\n\r MUUUUUUUuuuuUUUUUU - I'm not a dog!!!! \n\r");
             dog = 0;
         }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "flood ",6 )) {
             if (xmemmatch( (uint8_t*)(debug_buf+6), (uint8_t*) "on",2 )) {
+                miss_match = false;
                 pc.printf("\n\r\t Flood ON\n\r\t");
                 tflood.start();
                 tflood.reset();
@@ -286,6 +356,7 @@
                 pflood = 1;
             }
             if (xmemmatch( (uint8_t*)(debug_buf+6), (uint8_t*) "off",3 )) {
+                miss_match = false;
                 pc.printf("\n\r\t Flood OFF\n\r\t");
                 pflood = 0;
                 tflood.stop();
@@ -294,16 +365,32 @@
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "debug ",6 )) {
             if (xmemmatch( (uint8_t*)(debug_buf+6), (uint8_t*) "on",2 )) {
+                miss_match = false;
                 pc.printf("\n\r\tDebug ON\n\r\t");
                 debug_alive = 1;
             }
             if (xmemmatch( (uint8_t*)(debug_buf+6), (uint8_t*) "off",3 )) {
+                miss_match = false;
                 pc.printf("\n\r\tDebug OFF\n\r\t");
                 debug_alive = 0;
             }
         }
-
+        
+        if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "debug_sip ", 10 )) {
+            if (xmemmatch( (uint8_t*)(debug_buf + 10 ), (uint8_t*) "on",2 )) {
+                miss_match = false;
+                debug_msg( "Sip Debug ON" );
+                debug_sip = 1;
+            }
+            if (xmemmatch( (uint8_t*)(debug_buf + 10 ), (uint8_t*) "off",3 )) {
+                miss_match = false;
+                debug_msg( "Sip Debug OFF" );
+                debug_sip = 0;
+            }
+        }
+        
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "promptcb ", 9)) {         //promptcb ramal porta comando
+            miss_match = false;
             static int id_msg = 0x10;
             uint8_t write_buffer[300];
             unsigned int ext,port;
@@ -315,32 +402,37 @@
             ant1 = str2uint( (debug_buf+9), &ext);
             ant2 = str2uint( ((debug_buf+9)+ant1+1), &port);
 
-            pc.printf("\r\next=%d port=%d\r\ncmd=%s\r\n",ext, port, debug_buf+9+ant1+2+ant2);
+            pc.printf("ext=%d port=%d\r\ncmd=%s",ext, port, debug_buf+9+ant1+2+ant2);
 
             __send_to_cb__( __build_cb_package__( ext, port, __PROMPT__, debug_buf+9+ant1+2+ant2, id_msg++, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
-            pc.printf("\n\rComando enviado\n\r");
+            pc.printf("Comando enviado");
         }
 
         if( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "cks",3 )) {
+            miss_match = false;
             pcks_s = 1;
         }
 
         if (xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "showcb",6 )) {
+            miss_match = false;
             pshowcb = 1;
         }
         
         if (xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "debugsip ", 9 )) {
             if (xmemmatch( (uint8_t*)( debug_buf + 9 ), (uint8_t*) "on",2 )) {
-                pc.printf("\n\r\tDebug Sip ON\n\r\t");
+                miss_match = false;
+                pc.printf("\tDebug Sip ON");
                 debug_sip = 1;
             }
             if (xmemmatch( (uint8_t*)( debug_buf + 9 ), (uint8_t*) "off",3 )) {
-                pc.printf("\n\r\tDebug Sip OFF\n\r\t");
+                miss_match = false;
+                pc.printf("Debug Sip OFF");
                 debug_sip = 0;
             }
         }
     
         if( xstrmatch( (uint8_t*)debug_buf, (uint8_t*) "help" )) {
+            miss_match = false;
             pc.printf("\n\r****************************PROMPT HELP******************\n\r");
             pc.printf("sconfig                         - mostra o arquivo de configuracao do sistema\n\r");
             pc.printf("dconfig                         - volta as configuracoes do sistema para o padrao de fabrica\n\r");
@@ -349,16 +441,18 @@
             pc.printf("format                          - formata o sistema de arquivos\n\r");
             pc.printf("reset                           - resta o sistema\n\r");
             pc.printf("ipset [ip]                      - Configura o IP da cabeceira\n\r");
+            pc.printf("extset [ext]                    - Configura a ext da cabeceira\n\r");
+            pc.printf("msipport [port]                 - Configura a porta SIP da cabeceira\n\r");
+            pc.printf("serverip [ip]                   - Configura o ip do servidor asterisk\n\r");
+            pc.printf("serextset [ext]                 - Configura a server ext da cabeceira\n\r");
+            pc.printf("ssport [port]                   - Configura a porta SIP do servidor asterisk\n\r");
             pc.printf("maskset [mask]                  - Configura a mascara da cabeceira\n\r");
             pc.printf("gatewayset [gateway]            - Configura o gateway da cabeceira\n\r");
-            pc.printf("msipport [port]                 - Configura a porta SIP da cabeceira\n\r");
-            pc.printf("serverip [ip]                   - Configura o ip do servidor asterisk\n\r");
-            pc.printf("ssport [port]                   - Configura a porta SIP do servidor asterisk\n\r");
             pc.printf("showcb                          - lista os Cbx registrados na header\n\r");
             pc.printf("cks                             - exibe estatisticas de check sum\n\r");
             pc.printf("debug [on|off]                  - enable debugs\n\r");
             pc.printf("flood [on|off]                  - simula envio de pacotes de audio\n\r");
-            pc.printf("\n\rPROMPT VERSION: V%d\n\r",PVERSION);
+            pc.printf("PROMPT VERSION: V%d\n\r",PVERSION);
         }
         /*
         if ( xmemmatch( (uint8_t*)debug_buf, (uint8_t*) "callme ", 7 )) {
@@ -403,9 +497,11 @@
 
         }
         */
+        if( miss_match ) pc.printf("--> %s: command not found\n\r", debug_buf );
+        pc.printf("> ");
     }
-
-    if (b == 0x0D || bufret == 1 || bufptr > DEBUGBUFSIZE ) {
+    
+    if ( b == 0x0D || bufret == 1 || bufptr > DEBUGBUFSIZE ) {
         bufptr = 0;
         for (uint8_t i =0; i < DEBUGBUFSIZE; i++) {
             debug_buf[i] = 0;
@@ -414,4 +510,4 @@
         pc.putc(0x0A);
         pc.putc(0x0D);
     }
-}
\ No newline at end of file
+}   
\ No newline at end of file