nova verzija tcp+udp->serial com

Committer:
bosko001
Date:
Sun Apr 12 17:59:57 2020 +0000
Revision:
5:c9a908749d4c
Parent:
4:7abcf4543282
Child:
7:7ea932eb31f6
12.04.2020; napravljena class C_HTMLparse; treba napraviti obradu POST upita

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 4:7abcf4543282 1 #include "mbed.h"
bosko001 3:1196befeace0 2 #include "clubbing.h"
bosko001 2:45b351b4fc2a 3 #include "string.h"
bosko001 2:45b351b4fc2a 4 #include "dxml.h"
bosko001 2:45b351b4fc2a 5
bosko001 2:45b351b4fc2a 6 #include "doHTML.h"
bosko001 3:1196befeace0 7 #include "dataFlash.h"
bosko001 2:45b351b4fc2a 8 #include "HTMLpage.h"
bosko001 2:45b351b4fc2a 9
bosko001 2:45b351b4fc2a 10
bosko001 2:45b351b4fc2a 11
bosko001 5:c9a908749d4c 12 /************************** Pocetak *********************/
bosko001 4:7abcf4543282 13
bosko001 4:7abcf4543282 14 void test_HTML(void)
bosko001 4:7abcf4543282 15 {
bosko001 5:c9a908749d4c 16 extern C_HTMLparse *gpc_html;
bosko001 3:1196befeace0 17
bosko001 5:c9a908749d4c 18 P_str( "selected bit rate" , (char*)gpc_html->get_selected_option( "baud_rate" ) );
bosko001 5:c9a908749d4c 19 P_str( "selected parity" , (char*)gpc_html->get_selected_option( "parity" ) );
bosko001 5:c9a908749d4c 20 P_str( "selected stop bits" , (char*)gpc_html->get_selected_option( "stop_bits" ) );
bosko001 5:c9a908749d4c 21 P_str( "selected data bits" , (char*)gpc_html->get_selected_option( "data_bits" ) );
bosko001 3:1196befeace0 22
bosko001 4:7abcf4543282 23 {
bosko001 4:7abcf4543282 24 extern UARTSerial *gp_scom;
bosko001 4:7abcf4543282 25 extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val);
bosko001 4:7abcf4543282 26
bosko001 5:c9a908749d4c 27 auto c = gpc_html->getHTML_free( );
bosko001 5:c9a908749d4c 28 // n = p_soc->send(c, strlen(c));
bosko001 5:c9a908749d4c 29 sendScom( gp_scom, c, strlen(c));
bosko001 5:c9a908749d4c 30 free(c);
bosko001 5:c9a908749d4c 31 }
bosko001 5:c9a908749d4c 32
bosko001 5:c9a908749d4c 33
bosko001 5:c9a908749d4c 34 P_str( "select bit rate 38400", gpc_html->set_selected_option( "baud_rate", "38400" ) );
bosko001 5:c9a908749d4c 35 P_str( "select bit parity ODD", gpc_html->set_selected_option( "parity", "odd" ) );
bosko001 5:c9a908749d4c 36 P_str( "select stop bits 1.5", gpc_html->set_selected_option( "stop_bits", "1.5" ) );
bosko001 5:c9a908749d4c 37 P_str( "select data bits 7", gpc_html->set_selected_option( "data_bits", "7" ) );
bosko001 5:c9a908749d4c 38
bosko001 5:c9a908749d4c 39 {
bosko001 5:c9a908749d4c 40 extern UARTSerial *gp_scom;
bosko001 5:c9a908749d4c 41 extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val);
bosko001 5:c9a908749d4c 42
bosko001 5:c9a908749d4c 43 auto c = gpc_html->getHTML_free( );
bosko001 4:7abcf4543282 44 // n = p_soc->send(c, strlen(c));
bosko001 4:7abcf4543282 45 sendScom( gp_scom, c, strlen(c));
bosko001 4:7abcf4543282 46 free(c);
bosko001 4:7abcf4543282 47 }
bosko001 4:7abcf4543282 48
bosko001 4:7abcf4543282 49
bosko001 5:c9a908749d4c 50 P_str( "selected bit rate" , (char*)gpc_html->get_selected_option( "baud_rate" ) );
bosko001 5:c9a908749d4c 51 P_str( "selected parity" , (char*)gpc_html->get_selected_option( "parity" ) );
bosko001 5:c9a908749d4c 52 P_str( "selected stop bits" , (char*)gpc_html->get_selected_option( "stop_bits" ) );
bosko001 5:c9a908749d4c 53 P_str( "selected data bits" , (char*)gpc_html->get_selected_option( "data_bits" ) );
bosko001 5:c9a908749d4c 54
bosko001 5:c9a908749d4c 55 }
bosko001 5:c9a908749d4c 56
bosko001 5:c9a908749d4c 57
bosko001 5:c9a908749d4c 58
bosko001 5:c9a908749d4c 59
bosko001 5:c9a908749d4c 60
bosko001 5:c9a908749d4c 61 /************************ definicija klase C_HTMLparse ********************/
bosko001 3:1196befeace0 62
bosko001 5:c9a908749d4c 63 void C_HTMLparse::load_front_row(void )
bosko001 5:c9a908749d4c 64 {
bosko001 5:c9a908749d4c 65 int i = 0;
bosko001 5:c9a908749d4c 66 for (auto td = dxml_child(tr, "td")/*, i = 0*/; td; td = td->next)
bosko001 5:c9a908749d4c 67 {
bosko001 5:c9a908749d4c 68 auto input = dxml_child(td, "input");
bosko001 5:c9a908749d4c 69 front_row[i] = input;
bosko001 5:c9a908749d4c 70 ++i;
bosko001 5:c9a908749d4c 71 }
bosko001 5:c9a908749d4c 72
bosko001 5:c9a908749d4c 73 tr = tr->next;
bosko001 5:c9a908749d4c 74 }
bosko001 5:c9a908749d4c 75
bosko001 5:c9a908749d4c 76 void C_HTMLparse::load_second_row()
bosko001 5:c9a908749d4c 77 {
bosko001 5:c9a908749d4c 78 int i = 0;
bosko001 5:c9a908749d4c 79 for (auto td = dxml_child(tr, "td")/*, i = 0*/; td; td = td->next)
bosko001 5:c9a908749d4c 80 {
bosko001 5:c9a908749d4c 81 auto input = dxml_child(td, "input");
bosko001 5:c9a908749d4c 82 if (i == 0)
bosko001 5:c9a908749d4c 83 UDPs[0] = input;
bosko001 5:c9a908749d4c 84 else if(i == 1)
bosko001 5:c9a908749d4c 85 TCPs[0] = input;
bosko001 5:c9a908749d4c 86 ++i;
bosko001 5:c9a908749d4c 87 }
bosko001 5:c9a908749d4c 88
bosko001 5:c9a908749d4c 89 tr = tr->next;
bosko001 5:c9a908749d4c 90 }
bosko001 5:c9a908749d4c 91
bosko001 5:c9a908749d4c 92 void C_HTMLparse::load_udp_and_tcp(dxml_t& td)
bosko001 4:7abcf4543282 93 {
bosko001 5:c9a908749d4c 94 for (int i = 0; i < 4; td = td->next)
bosko001 5:c9a908749d4c 95 {
bosko001 5:c9a908749d4c 96 dxml_t inputs[5];
bosko001 5:c9a908749d4c 97 int input_count = 0;
bosko001 5:c9a908749d4c 98 for (auto input = dxml_child(td, "input"); input; input = input->next)
bosko001 5:c9a908749d4c 99 inputs[input_count++] = input;
bosko001 5:c9a908749d4c 100
bosko001 5:c9a908749d4c 101 if (i < 2) // Load inputs into UDPs
bosko001 5:c9a908749d4c 102 {
bosko001 5:c9a908749d4c 103 for (int k = 0; k < input_count; ++k)
bosko001 5:c9a908749d4c 104 UDPs[i == 0 ? k + 1 : k + 6] = inputs[k];
bosko001 5:c9a908749d4c 105 }
bosko001 5:c9a908749d4c 106 else
bosko001 5:c9a908749d4c 107 {
bosko001 5:c9a908749d4c 108 for (int k = 0; k < input_count; ++k)
bosko001 5:c9a908749d4c 109 TCPs[i == 2 ? k + 1 : k + 6] = inputs[k];
bosko001 5:c9a908749d4c 110 }
bosko001 5:c9a908749d4c 111
bosko001 5:c9a908749d4c 112 ++i;
bosko001 5:c9a908749d4c 113 }
bosko001 5:c9a908749d4c 114 }
bosko001 5:c9a908749d4c 115
bosko001 5:c9a908749d4c 116 void C_HTMLparse::load_serial(dxml_t& td)
bosko001 5:c9a908749d4c 117 {
bosko001 5:c9a908749d4c 118 auto select = dxml_child(td, "select");
bosko001 5:c9a908749d4c 119 for (int i = 0; select; select = select->next)
bosko001 5:c9a908749d4c 120 serial[i++] = select;
bosko001 5:c9a908749d4c 121 }
bosko001 5:c9a908749d4c 122
bosko001 5:c9a908749d4c 123
bosko001 5:c9a908749d4c 124 dxml_t C_HTMLparse::get_serial_option(dxml_t s, int option_idx)
bosko001 5:c9a908749d4c 125 {
bosko001 5:c9a908749d4c 126 auto rv = dxml_child(s, "option");
bosko001 5:c9a908749d4c 127 for (int i = 0; i < option_idx; ++i)
bosko001 5:c9a908749d4c 128 {
bosko001 5:c9a908749d4c 129 if( rv->next == NULL ) return NULL;
bosko001 5:c9a908749d4c 130 else rv = rv->next;
bosko001 5:c9a908749d4c 131 }
bosko001 5:c9a908749d4c 132 return rv;
bosko001 5:c9a908749d4c 133 }
bosko001 4:7abcf4543282 134
bosko001 5:c9a908749d4c 135 C_HTMLparse::C_HTMLparse( void )
bosko001 5:c9a908749d4c 136 {
bosko001 5:c9a908749d4c 137
bosko001 5:c9a908749d4c 138 p_HTMLpage = (char*)malloc(strlen(HTMLpage)+1);
bosko001 5:c9a908749d4c 139 strcpy( p_HTMLpage, HTMLpage);
bosko001 5:c9a908749d4c 140 P_int("ucitavam html", strlen(HTMLpage));
bosko001 5:c9a908749d4c 141 p_rootHTML = dxml_parse_str(p_HTMLpage, strlen(p_HTMLpage));
bosko001 5:c9a908749d4c 142 intern_init( );
bosko001 5:c9a908749d4c 143 }
bosko001 5:c9a908749d4c 144 C_HTMLparse::C_HTMLparse( const char *str )
bosko001 5:c9a908749d4c 145 {
bosko001 5:c9a908749d4c 146 p_HTMLpage = (char*)malloc(strlen(str)+1);
bosko001 5:c9a908749d4c 147 strcpy( p_HTMLpage, str);
bosko001 5:c9a908749d4c 148 P_int("ucitavam html", strlen(str));
bosko001 5:c9a908749d4c 149 p_rootHTML = dxml_parse_str(p_HTMLpage, strlen(p_HTMLpage));
bosko001 5:c9a908749d4c 150 intern_init( );
bosko001 5:c9a908749d4c 151
bosko001 5:c9a908749d4c 152 }
bosko001 5:c9a908749d4c 153
bosko001 5:c9a908749d4c 154
bosko001 5:c9a908749d4c 155 void C_HTMLparse::intern_init( void )
bosko001 5:c9a908749d4c 156 {
bosko001 5:c9a908749d4c 157 html_body = dxml_child(p_rootHTML, "body");
bosko001 5:c9a908749d4c 158 html_form = dxml_child(html_body, "form");
bosko001 5:c9a908749d4c 159 html_table = dxml_child(html_form, "table");
bosko001 5:c9a908749d4c 160 tr = dxml_child(html_table, "tr");
bosko001 5:c9a908749d4c 161
bosko001 5:c9a908749d4c 162 load_front_row();
bosko001 5:c9a908749d4c 163
bosko001 5:c9a908749d4c 164 load_second_row();
bosko001 5:c9a908749d4c 165
bosko001 5:c9a908749d4c 166 auto td = dxml_child(tr, "td");
bosko001 5:c9a908749d4c 167 load_udp_and_tcp(td);
bosko001 5:c9a908749d4c 168 load_serial(td);
bosko001 4:7abcf4543282 169 }
bosko001 4:7abcf4543282 170
bosko001 4:7abcf4543282 171
bosko001 5:c9a908749d4c 172 char *C_HTMLparse::getHTML_free( void )
bosko001 5:c9a908749d4c 173 {
bosko001 5:c9a908749d4c 174 return dxml_toxml(p_rootHTML);
bosko001 5:c9a908749d4c 175 }
bosko001 3:1196befeace0 176
bosko001 3:1196befeace0 177
bosko001 5:c9a908749d4c 178 /****************** geteri - seteri *************************/
bosko001 3:1196befeace0 179
bosko001 3:1196befeace0 180
bosko001 3:1196befeace0 181
bosko001 3:1196befeace0 182
bosko001 5:c9a908749d4c 183 const char * C_HTMLparse::get_selected_option( const char *name_option )
bosko001 5:c9a908749d4c 184 {
bosko001 5:c9a908749d4c 185 for( int i=0; dxml_attr(serial[i], "name") != NULL; i++ )
bosko001 5:c9a908749d4c 186 {
bosko001 5:c9a908749d4c 187 char *ret;
bosko001 5:c9a908749d4c 188 if( !strcmp( (char*)dxml_attr(serial[i], "name"), name_option) )
bosko001 5:c9a908749d4c 189 {
bosko001 5:c9a908749d4c 190 for(int j=0; dxml_attr(get_serial_option(serial[i], j), "value") != NULL; j++)
bosko001 5:c9a908749d4c 191 {
bosko001 5:c9a908749d4c 192 if( dxml_attr( get_serial_option(serial[i],j), "selected" ) )
bosko001 5:c9a908749d4c 193 return dxml_attr(get_serial_option(serial[i], j), "value");
bosko001 5:c9a908749d4c 194 }
bosko001 5:c9a908749d4c 195 return dxml_attr(get_serial_option(serial[i], 0), "value");
bosko001 5:c9a908749d4c 196 }
bosko001 5:c9a908749d4c 197 }
bosko001 5:c9a908749d4c 198 return NULL;
bosko001 5:c9a908749d4c 199 }
bosko001 5:c9a908749d4c 200
bosko001 5:c9a908749d4c 201 const char * C_HTMLparse::set_selected_option( const char* name_option, const char* value_option )
bosko001 5:c9a908749d4c 202 {
bosko001 5:c9a908749d4c 203 for( int i=0; dxml_attr(serial[i], "name") != NULL; i++ )
bosko001 5:c9a908749d4c 204 {
bosko001 5:c9a908749d4c 205 // printf(" i = %d \n\r", i );
bosko001 5:c9a908749d4c 206 char *ret;
bosko001 5:c9a908749d4c 207 if( !strcmp( (char*)dxml_attr(serial[i], "name"), name_option) )
bosko001 5:c9a908749d4c 208 {
bosko001 5:c9a908749d4c 209 for(int j=0; dxml_attr(get_serial_option(serial[i], j), "value") != NULL; j++)
bosko001 5:c9a908749d4c 210 {
bosko001 5:c9a908749d4c 211 // printf(" j=%d \n\r", j);
bosko001 5:c9a908749d4c 212 if( !strcmp( dxml_attr( get_serial_option(serial[i],j), "value"), value_option) )
bosko001 5:c9a908749d4c 213 {
bosko001 5:c9a908749d4c 214 if( !dxml_attr( get_serial_option(serial[i],j), "selected") )
bosko001 5:c9a908749d4c 215 {
bosko001 5:c9a908749d4c 216 for(int k=0; dxml_attr(get_serial_option(serial[i], k), "value") != NULL; k++)
bosko001 5:c9a908749d4c 217 {
bosko001 5:c9a908749d4c 218 // printf(" k=%d\n\r",k);
bosko001 5:c9a908749d4c 219 if( !strcmp( dxml_attr( get_serial_option(serial[i],k), "value"), value_option) )
bosko001 5:c9a908749d4c 220 {
bosko001 5:c9a908749d4c 221 printf(" kk=%d\n\r",k);
bosko001 5:c9a908749d4c 222 dxml_set_attr( get_serial_option(serial[i],k), "selected", "ON" );
bosko001 5:c9a908749d4c 223 }
bosko001 5:c9a908749d4c 224 else
bosko001 5:c9a908749d4c 225 {
bosko001 5:c9a908749d4c 226 printf(" k=%d\n\r",k);
bosko001 5:c9a908749d4c 227 dxml_set_attr( get_serial_option(serial[i],k), "selected", NULL );
bosko001 5:c9a908749d4c 228 }
bosko001 5:c9a908749d4c 229 }
bosko001 5:c9a908749d4c 230 }
bosko001 5:c9a908749d4c 231 return value_option;
bosko001 5:c9a908749d4c 232 }
bosko001 5:c9a908749d4c 233 }
bosko001 5:c9a908749d4c 234 }
bosko001 5:c9a908749d4c 235 }
bosko001 5:c9a908749d4c 236
bosko001 5:c9a908749d4c 237 return NULL;
bosko001 5:c9a908749d4c 238
bosko001 5:c9a908749d4c 239 }
bosko001 5:c9a908749d4c 240
bosko001 5:c9a908749d4c 241
bosko001 5:c9a908749d4c 242
bosko001 5:c9a908749d4c 243 void C_HTMLparse::set_myIP( const char* str )
bosko001 5:c9a908749d4c 244 {
bosko001 5:c9a908749d4c 245 if( test_ip( str ) ) dxml_set_attr( front_row[0], "value", str);
bosko001 5:c9a908749d4c 246 }
bosko001 5:c9a908749d4c 247 void C_HTMLparse::set_myMASK( const char* str )
bosko001 5:c9a908749d4c 248 {
bosko001 5:c9a908749d4c 249 if( test_ip( str ) ) dxml_set_attr( front_row[1], "value", str);
bosko001 5:c9a908749d4c 250 }
bosko001 5:c9a908749d4c 251 void C_HTMLparse::set_myGATE( const char* str )
bosko001 5:c9a908749d4c 252 {
bosko001 5:c9a908749d4c 253 if( test_ip( str ) ) dxml_set_attr( front_row[2], "value", str);
bosko001 5:c9a908749d4c 254 }
bosko001 5:c9a908749d4c 255
bosko001 5:c9a908749d4c 256 void C_HTMLparse::set_myUdpPort( const char* str )
bosko001 5:c9a908749d4c 257 {
bosko001 5:c9a908749d4c 258 if( test_num( str ) ) dxml_set_attr( UDPs[0], "value", str );
bosko001 5:c9a908749d4c 259 }
bosko001 5:c9a908749d4c 260 void C_HTMLparse::set_myTcpPort( const char* str )
bosko001 5:c9a908749d4c 261 {
bosko001 5:c9a908749d4c 262 if( test_num( str) ) dxml_set_attr( TCPs[0], "value", str);
bosko001 5:c9a908749d4c 263 }
bosko001 5:c9a908749d4c 264
bosko001 5:c9a908749d4c 265 void C_HTMLparse::set_UdpIP( int i, const char* str )
bosko001 5:c9a908749d4c 266 {
bosko001 5:c9a908749d4c 267 if( (i > 0) && (i <= 5 ) && test_ip( str ) ) dxml_set_attr( UDPs[i], "value", str);
bosko001 5:c9a908749d4c 268 }
bosko001 5:c9a908749d4c 269 void C_HTMLparse::set_UdpPort( int i, const char* str )
bosko001 5:c9a908749d4c 270 {
bosko001 5:c9a908749d4c 271 if( (i>0) && (i<= 4) && test_num( str ) ) dxml_set_attr( UDPs[i+6], "value", str) ;
bosko001 5:c9a908749d4c 272 }
bosko001 5:c9a908749d4c 273
bosko001 5:c9a908749d4c 274 void C_HTMLparse::set_TcpIP( int i, const char* str )
bosko001 5:c9a908749d4c 275 {
bosko001 5:c9a908749d4c 276 if( (i > 0) && (i <= 5 ) && test_ip( str ) ) dxml_set_attr( TCPs[i], "value", str);
bosko001 5:c9a908749d4c 277 }
bosko001 5:c9a908749d4c 278 void C_HTMLparse::set_TcpPort( int i, const char* str )
bosko001 5:c9a908749d4c 279 {
bosko001 5:c9a908749d4c 280 if( (i>0) && (i<= 4) && test_num( str ) ) dxml_set_attr( TCPs[i+6], "value", str) ;
bosko001 5:c9a908749d4c 281 }
bosko001 5:c9a908749d4c 282
bosko001 5:c9a908749d4c 283 const char *C_HTMLparse::get_myIP( void )
bosko001 5:c9a908749d4c 284 {
bosko001 5:c9a908749d4c 285 return dxml_attr( front_row[0], "value");
bosko001 5:c9a908749d4c 286 }
bosko001 5:c9a908749d4c 287 const char *C_HTMLparse::get_myMASK(void)
bosko001 5:c9a908749d4c 288 {
bosko001 5:c9a908749d4c 289 return dxml_attr( front_row[1], "value");
bosko001 5:c9a908749d4c 290 }
bosko001 5:c9a908749d4c 291 const char *C_HTMLparse::get_myGATE( void )
bosko001 5:c9a908749d4c 292 {
bosko001 5:c9a908749d4c 293 return dxml_attr( front_row[2], "value");
bosko001 5:c9a908749d4c 294 }
bosko001 5:c9a908749d4c 295
bosko001 5:c9a908749d4c 296 const char *C_HTMLparse::get_myUdpPort( void )
bosko001 5:c9a908749d4c 297 {
bosko001 5:c9a908749d4c 298 return dxml_attr( UDPs[0], "value" );
bosko001 5:c9a908749d4c 299 }
bosko001 5:c9a908749d4c 300 const char *C_HTMLparse::get_myTcpPort( void )
bosko001 5:c9a908749d4c 301 {
bosko001 5:c9a908749d4c 302 return dxml_attr( TCPs[0], "value");
bosko001 5:c9a908749d4c 303 }
bosko001 5:c9a908749d4c 304
bosko001 5:c9a908749d4c 305 const char *C_HTMLparse::get_UdpIP( int i )
bosko001 5:c9a908749d4c 306 {
bosko001 5:c9a908749d4c 307 if( (i > 0) && (i <= 5 ) ) return dxml_attr( UDPs[i], "value");
bosko001 5:c9a908749d4c 308 return NULL;
bosko001 5:c9a908749d4c 309 }
bosko001 5:c9a908749d4c 310
bosko001 5:c9a908749d4c 311 const char *C_HTMLparse::get_UdpPort( int i )
bosko001 5:c9a908749d4c 312 {
bosko001 5:c9a908749d4c 313 if( (i>0) && (i<= 4) ) return dxml_attr( UDPs[i+6], "value") ;
bosko001 5:c9a908749d4c 314 return NULL;
bosko001 5:c9a908749d4c 315 }
bosko001 5:c9a908749d4c 316
bosko001 5:c9a908749d4c 317 const char *C_HTMLparse::get_TcpIP( int i )
bosko001 5:c9a908749d4c 318 {
bosko001 5:c9a908749d4c 319 if( (i > 0) && (i <= 5 ) ) return dxml_attr( TCPs[i], "value");
bosko001 5:c9a908749d4c 320 return NULL;
bosko001 5:c9a908749d4c 321 }
bosko001 5:c9a908749d4c 322
bosko001 5:c9a908749d4c 323 const char *C_HTMLparse::get_TcpPort( int i )
bosko001 5:c9a908749d4c 324 {
bosko001 5:c9a908749d4c 325 if( (i>0) && (i<= 4) ) return dxml_attr( TCPs[i+6], "value") ;
bosko001 5:c9a908749d4c 326 return NULL;
bosko001 5:c9a908749d4c 327 }
bosko001 5:c9a908749d4c 328
bosko001 5:c9a908749d4c 329
bosko001 5:c9a908749d4c 330