nova proba

Revision:
7:7ea932eb31f6
Parent:
6:5cf50f29a81f
Child:
9:893843262a1f
--- a/TcpServerToFlash.cpp	Tue Apr 14 13:32:02 2020 +0000
+++ b/TcpServerToFlash.cpp	Sat Apr 18 22:32:13 2020 +0000
@@ -5,6 +5,7 @@
 #include "dxml.h"
 #include "TcpServerToFlash.h"
 #include "doHTML.h"
+#include "dataFlash.h"
 
 #define SOCKET_PORT 11000
 
@@ -13,21 +14,25 @@
 
 extern EthernetInterface *gp_eth;
 
-void tcpServerRx_fun( void );
+/* globalni pointer na tcpServer RX Thread za parametarizaciju  */
 Thread *gp_tcpServerThread = new Thread;
-TCPSocket *gp_tcpSocket=NULL;
-volatile Thread *gp_thr = NULL;
+void tcpServerRx_fun( void );
+
+
+TCPSocket *gp_tcpSocket=NULL;   /* socket za prijem zahteva za konekciju */
+volatile Thread *gp_thr = NULL; /* soket za komunikaciju sa klijentom */
 
 
 const char sendHTTPheader[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html>";
 
 typedef void(*PF_PSTP)(struct SThreadParam *);
 
+// parametri thread-a za uspostavljenje konekcije
 struct SThreadParam
 {
-  Thread *p_Thread; 
-  void * p_param;  
-  PF_PSTP pf_pstp;  
+  Thread *p_Thread; // sopstveni pointer
+  void * p_param;  // pointer za parametre thread-a, u ovom slucaju na socket koji je accept-van
+  PF_PSTP pf_pstp;  // pointer na funkciju koju thread callback-uje
 };
 
 /******************  inicijalizacija u mainu  ************************/
@@ -39,6 +44,7 @@
 }
 
 /*****************  periodicna prozivka u mainu *************************/
+// zatvaranje thread-a nakon http razmene 
 
 void cleanTcpServerToFlashThread( void )
 {
@@ -71,10 +77,10 @@
  
     while(   (p_soc = gp_tcpSocket->accept( &err)) || (err == NSAPI_ERROR_NO_SOCKET)  )
     {
-        s.wait();
+        s.wait();     // semafor sprecava novu konekciju dok se stara ne zavrsi 
 
         Thread *p_thr = new Thread;
-         struct SThreadParam *ps_thp = new struct SThreadParam;
+         struct SThreadParam *ps_thp = new struct SThreadParam;  // alokacija ps_thp
          ps_thp->p_param = (void*)p_soc;
          ps_thp->p_Thread = p_thr;
          ps_thp->pf_pstp = rcpServerInstance_fun;
@@ -91,6 +97,7 @@
 
 
 /***************** privremeni TCPServer thread za klijenta  ******************/
+// thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
 
 void rcpServerInstance_fun( struct SThreadParam *ps_thp )
 {
@@ -99,15 +106,16 @@
 
       int n, nr, nt;
       printf("thread:  Otvoren socket %d\n\r",(int)p_soc);   
-
     
         while(true)
        {        
+         extern C_HTMLparse *gpc_html;
+ 
             printf("\n\rcekam prijem od klijenta \n\r");
             SocketAddress sockAddr;
             p_soc->getpeername( &sockAddr );
             
-            p_soc->set_timeout(10);
+            p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
             n=0;
             do
             {
@@ -120,29 +128,60 @@
             
             if(n>0)
             {
+
+             //u inbuff-u je http paket
               printf("PRIJEM tcpSOCKET: primljeno = %d bajtova \n\r",n);
-              {int i; for(i=0;i<n;i++)  printf("%c", inbuff[i] );}
-                printf("KRAJ PRIJEMA\n\r");
-                      fflush(stdout);
-             }
+              
+//              {   int i; for(i=0;i<n;i++)  printf("%c", inbuff[i] );}
+//                printf("KRAJ PRIJEMA\n\r");
+//                      fflush(stdout);
+                inbuff[n]=0;
+                //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
+                char * str = gpc_html->doHTTP( inbuff);
+                if( !strcmp( str, "GET_HTML") || !strcmp( str, "POST") )
+                {
+
+                    //    putHTMLintoFLASH( gpc_html );
+                    //putFLASHintoHTML( gpc_html );
+
+                    if(!strcmp( str, "POST") ) 
+                    { 
+                        gpc_html->htmlPOST_fun( );   // setovanje parametara u HTML format
+                         
+                        putHTMLintoFLASH( gpc_html );   // upis parametara iz HTMLa u FLASH
+                     }
     
-            p_soc->set_timeout(-1);
-            n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
-            printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
+    // ODGOVOR - slanje odgovora klijentu
+    
+                    p_soc->set_timeout(-1);  // blocking
+                    // slanje HTTP headera
+                    n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
+                    printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
+                    
+            //        putFLASHintoHTML( gpc_html );   // preuzimanje podataka iz flasha u html
+                    auto c = gpc_html->getHTML_free( );  // konvertovanje HTMLa u niz
+                    n = p_soc->send(c, strlen(c));  // slanje HTML-a
+                   
+                   // test - slanje HTMLa na serial com 
+                   //     extern UARTSerial *gp_scom;
+//                        extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val);
+//                        sendScom( gp_scom, c, strlen(c));
+     
+                    free(c); // oslobadjanje memorije HTML niza
+
+
+                    printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c));
+                    
+                }
+             }
             
-            extern C_HTMLparse *gpc_html;
-            auto c = gpc_html->getHTML_free( );
-            n = p_soc->send(c, strlen(c));
-            free(c);
-            printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c));
-            
-            
-            p_soc->close();
+            p_soc->close();    // zatvaranje socketa
            /* if(n<0)*/ {printf("BREAK posle predaje\n\r"); break;}
         }
-    
+      // zatvaranje thread-a
       gp_thr = ps_thp->p_Thread;//   poslati pointer da se delete-uje
-      delete ps_thp;
+      
+      delete ps_thp; // relokacija ps_thp
       
 }