nova verzija tcp+udp->serial com

Revision:
7:7ea932eb31f6
Parent:
5:c9a908749d4c
Child:
8:c0f54b381346
--- a/doHTML.cpp	Tue Apr 14 13:32:02 2020 +0000
+++ b/doHTML.cpp	Sat Apr 18 22:32:13 2020 +0000
@@ -141,6 +141,7 @@
         p_rootHTML = dxml_parse_str(p_HTMLpage, strlen(p_HTMLpage));
         intern_init( );
      }
+
      C_HTMLparse::C_HTMLparse( const char *str )
      {
         p_HTMLpage = (char*)malloc(strlen(str)+1);
@@ -166,6 +167,7 @@
         auto td = dxml_child(tr, "td");
         load_udp_and_tcp(td);
         load_serial(td);    
+        for(int i = 0; i < sizeof( s_hpv)/sizeof( s_hpv[0]); i++) { s_hpv[i].name= NULL; s_hpv[i].value = NULL; }
     }
 
 
@@ -223,7 +225,7 @@
                           }
                           else  
                           {  
-                              printf("   k=%d\n\r",k); 
+//                              printf("   k=%d\n\r",k); 
                               dxml_set_attr( get_serial_option(serial[i],k), "selected", NULL );
                           }
                         }
@@ -238,46 +240,35 @@
         
     }
 
-
+#define COPY_IP_ATTR_VALUE( DXML_T, STR )  { char *s = (char *)dxml_attr( DXML_T, "value"); if( s ) {strcpy( s, STR );  dxml_set_attr( DXML_T, "value", s);  }  }
+#define COPY_PORT_ATTR_VALUE( DXML_T, STR )  { char *s = (char *)dxml_attr( DXML_T, "value"); if( s ) {strcpy( s, STR );  dxml_set_attr( DXML_T, "value", s);  }  }
 
-    void C_HTMLparse::set_myIP( const char* str )
-    {
-       if( test_ip( str ) )  dxml_set_attr( front_row[0], "value", str);
-    }
-    void C_HTMLparse::set_myMASK( const char* str )
-    {
-       if( test_ip( str ) )  dxml_set_attr( front_row[1], "value", str);
-    }
-    void C_HTMLparse::set_myGATE( const char* str )
-    {
-       if( test_ip( str ) )  dxml_set_attr( front_row[2], "value", str);
-    }
+    void C_HTMLparse::set_myIP( const char* str )    {  if( test_ip( str )) COPY_IP_ATTR_VALUE( front_row[0], str );     }
+    
+    void C_HTMLparse::set_myMASK( const char* str )  {  if( test_ip( str )) COPY_IP_ATTR_VALUE( front_row[1], str );     }
 
-    void C_HTMLparse::set_myUdpPort( const char* str )
-    {
-        if( test_num( str ) ) dxml_set_attr( UDPs[0], "value", str );
-    }
-    void C_HTMLparse::set_myTcpPort( const char* str ) 
-    {
-         if( test_num( str) ) dxml_set_attr( TCPs[0], "value", str);
-    }
+    void C_HTMLparse::set_myGATE( const char* str )  {  if( test_ip( str )) COPY_IP_ATTR_VALUE( front_row[2], str );     }
+
+    void C_HTMLparse::set_myUdpPort( const char* str )  { if( test_num( str ) ) COPY_PORT_ATTR_VALUE( UDPs[0], str );      } //dxml_set_attr( UDPs[0], "value", str );    }
+    
+    void C_HTMLparse::set_myTcpPort( const char* str )  { if( test_num( str ) ) COPY_PORT_ATTR_VALUE( TCPs[0], str );      } // if( test_num( str) ) dxml_set_attr( TCPs[0], "value", str);    }
 
     void C_HTMLparse::set_UdpIP( int i, const char* str )
     {
-        if( (i > 0) && (i <= 5 ) && test_ip( str ) )      dxml_set_attr( UDPs[i], "value", str); 
+        if( (i > 0) && (i <= 5 ) && test_ip( str ) )     COPY_IP_ATTR_VALUE( UDPs[i], str )         //// dxml_set_attr( UDPs[i], "value", str); 
     }    
     void C_HTMLparse::set_UdpPort( int i, const char* str )
     {
-        if( (i>0) && (i<= 4) && test_num( str ) )     dxml_set_attr( UDPs[i+6], "value", str) ;
+        if( (i>0) && (i<= 4) && test_num( str ) )        COPY_PORT_ATTR_VALUE( UDPs[i+5], str )     ////dxml_set_attr( UDPs[i+6], "value", str) ;
     }
     
     void C_HTMLparse::set_TcpIP( int i, const char* str )
     {
-        if( (i > 0) && (i <= 5 ) && test_ip( str ) )      dxml_set_attr( TCPs[i], "value", str); 
+        if( (i > 0) && (i <= 5 ) && test_ip( str ) )     COPY_IP_ATTR_VALUE( TCPs[i], str )       /////// dxml_set_attr( TCPs[i], "value", str); 
     }    
     void C_HTMLparse::set_TcpPort( int i, const char* str )
     {
-        if( (i>0) && (i<= 4) && test_num( str ) )     dxml_set_attr( TCPs[i+6], "value", str) ;
+        if( (i>0) && (i<= 4) && test_num( str ) )        COPY_PORT_ATTR_VALUE( TCPs[i+5], str )       ////dxml_set_attr( TCPs[i+6], "value", str) ;
     }
 
     const char *C_HTMLparse::get_myIP( void )
@@ -310,7 +301,7 @@
     
     const char *C_HTMLparse::get_UdpPort( int i )
     {
-        if( (i>0) && (i<= 4) )    return dxml_attr( UDPs[i+6], "value") ;
+        if( (i>0) && (i<= 4) )    return dxml_attr( UDPs[i+5], "value") ;
         return NULL; 
     }
     
@@ -322,9 +313,188 @@
     
     const char *C_HTMLparse::get_TcpPort( int i )
     {
-        if( (i>0) && (i<= 4) )    return dxml_attr( TCPs[i+6], "value") ;
+        if( (i>0) && (i<= 4) )    return dxml_attr( TCPs[i+5], "value") ;
         return NULL; 
     }
  
 
+/****************************************************************************/
 
+/***************   HTTP obrada *********************************************/
+
+
+int C_HTMLparse::httpSplitInLines( char *lines[], char * httpresp)
+{
+
+  char * pch;
+  int i=0;
+  pch = lines[i++]= strtok (httpresp,"\n\r");
+  while (pch != NULL)
+  {
+    pch=lines[i++] = strtok (NULL, "\n\r");
+  }
+   return i;
+}
+char * C_HTMLparse::httpFirstLine( char **descr, char *line)
+{
+  char * pch;
+
+  pch = strtok (line,"/ ");
+  *descr = strtok (NULL, "/ ");
+     return pch;
+}
+//
+//struct S_httpPostValues
+//{
+//    char *name;
+//    char *value;
+//} s_hpv[100];
+
+//struct S_httpPostValues *ps_hpv = s_hpv;
+
+int C_HTMLparse::extractPostPairs( struct S_httpPostValues ** ps_hpv, char *line)
+{
+ char *lines[100];
+ char * pch;
+
+  int i=0;
+  pch = lines[i++]= strtok (line,"&");
+  while (pch != NULL)
+  {
+    pch=lines[i++] = strtok (NULL, "&\n\r");
+  }
+
+printf("linija 26 %s\n\r", lines[26]);
+for(int i=0; i<15; i++) printf("%x ", *(lines[26]+i) ); printf("\n\r\n\r");
+
+  for( int j=0; j<i; j++)
+  {
+      char *s = strtok(lines[j], "=");
+      (*ps_hpv)[j].name = s;
+      (*ps_hpv)[j].value = strtok(NULL, "=");
+  }
+  return i;
+}
+
+
+char * C_HTMLparse::doHTTP( char * httpcontents)
+{
+char *lines[50];
+
+int http_i = httpSplitInLines( lines, httpcontents);
+
+char *descr;
+char *type = httpFirstLine( &descr, lines[0]);
+
+printf("type = %s  descr = %s\n\r", type, descr );
+
+int html_j=0;
+    if ( !strcmp( type, "POST") )
+    {
+        if( http_i >=14)
+        {
+            html_j = extractPostPairs( &ps_hpv, lines[14]);
+            return "POST" ;
+        }
+    }
+    else if( !strcmp(type, "GET"))
+    {
+        if( !strcmp(descr, "HTTP") ) return "GET_HTML";
+        else               return 0;
+    }
+    return 0;
+}
+
+/* TEST
+
+if( !strcmp(doHTTP( hG3), "POST") )
+
+    for( int k=0; k<10; k++ )
+        printf("name= %s   value= %s\n\r", (ps_hpv+k)->name, ps_hpv[k].value);
+
+    return 0;
+*/
+
+//   struct S_httpPostValues
+//    {
+//        char *name;
+//        char *value;
+//    } s_hpv[100];
+
+
+void C_HTMLparse::htmlPOST_fun( void )
+{
+  for(int i = 0; i<27; i++ )
+  {
+  //    printf(" rb = %d   name:%s = value:%s \n\r", i, s_hpv[i].name, s_hpv[i].value );
+        
+      if( !strcmp( s_hpv[i].name, "ip_adresa") )    set_myIP( s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "netmask") ) set_myMASK( s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "gateway") ) set_myGATE( s_hpv[i].value ); 
+
+      else if( !strcmp( s_hpv[i].name, "udp_server_port") ) set_myUdpPort( s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_server_port") ) set_myTcpPort( s_hpv[i].value ); 
+
+      else if( !strcmp( s_hpv[i].name, "udp_ip_1") ) set_UdpIP( 1, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_ip_2") ) set_UdpIP( 2, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_ip_3") ) set_UdpIP( 3, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_opseg_od") ) set_UdpIP( 4, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_opseg_do") ) set_UdpIP( 5, s_hpv[i].value ); 
+
+      else if( !strcmp( s_hpv[i].name, "udp_port_1") ) set_UdpPort( 1, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_port_2") ) set_UdpPort( 2, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_port_3") ) set_UdpPort( 3, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "udp_port_opseg") ) set_UdpPort( 4, s_hpv[i].value ); 
+
+      else if( !strcmp( s_hpv[i].name, "tcp_ip_1") ) set_TcpIP( 1, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_ip_2") ) set_TcpIP( 2, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_ip_3") ) set_TcpIP( 3, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_opseg_od") ) set_TcpIP( 4, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_opseg_do") ) set_TcpIP( 5, s_hpv[i].value ); 
+
+      else if( !strcmp( s_hpv[i].name, "tcp_port_1") ) set_TcpPort( 1, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_port_2") ) set_TcpPort( 2, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_port_3") ) set_TcpPort( 3, s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "tcp_port_opseg") ) set_TcpPort( 4, s_hpv[i].value ); 
+
+
+      else if( !strcmp( s_hpv[i].name, "baud_rate") ) set_selected_option(  "baud_rate" , s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "parity") ) set_selected_option(  "parity" , s_hpv[i].value ); 
+      else if( !strcmp( s_hpv[i].name, "data_bits") ) { set_selected_option(  "data_bits" , s_hpv[i].value ); } 
+      else if( !strcmp( s_hpv[i].name, "stop_bits") ) { set_selected_option(  "stop_bits" , s_hpv[i].value ); }
+    }
+
+}
+
+
+
+//ip_adresa=192.168.1.20&
+//netmask=255.255.255.0&
+//gateway=192.168.1.1&
+//udp_server_port=11000&
+//tcp_server_port=12000&
+//udp_ip_1=xxx.xxx.xxx.xxx&
+//udp_ip_2=xxx.xxx.xxx.xxx&
+//udp_ip_3=xxx.xxx.xxx.xxx&
+//udp_opseg_od=xxx.xxx.xxx.xxx&
+//udp_opseg_do=xxx.xxx.xxx.xxx&
+//udp_port_1=65535&
+//udp_port_2=&
+//udp_port_3=65535&
+//udp_port_opseg=65535&
+//tcp_ip_1=xxx.xxx.xxx.xxx&
+//tcp_ip_2=xxx.xxx.xxx.xxx&
+//tcp_ip_3=xxx.xxx.xxx.xxx&
+//tcp_opseg_od=xxx.xxx.xxx.xxx&
+//tcp_opseg_do=xxx.xxx.xxx.xxx&
+//tcp_port_1=65535&
+//tcp_port_2=&tcp_port_3=65535&
+//tcp_port_opseg=65535&
+//baud_rate=9600&
+//parity=none&data_bits=8&
+//stop_bits=1
+
+
+
+
+/*****************************************************************************/