1 /* This case tests reconnection in non-blocking mode. */
2 #include "tx_api.h"
3 #include "nx_api.h"
4 #include "fx_api.h"
5 #include "nx_web_http_client.h"
6 #include "nx_web_http_server.h"
7
8 extern void test_control_return(UINT);
9
10 #if !defined(NX_DISABLE_IPV4) && defined(NX_ENABLE_EXTENDED_NOTIFY_SUPPORT)
11
12 #define DEMO_STACK_SIZE 4096
13
14 /* Set up FileX and file memory resources. */
15 static CHAR ram_disk_memory[4096];
16 static FX_MEDIA ram_disk;
17 static UCHAR media_memory[4096];
18
19 static UCHAR server_stack[16000];
20
21 /* Define device drivers. */
22 extern void _fx_ram_driver(FX_MEDIA *media_ptr);
23 extern void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
24
25 /* Set up the HTTP client global variables. */
26
27 #define CLIENT_PACKET_SIZE (NX_WEB_HTTP_CLIENT_MIN_PACKET_SIZE * 2)
28
29 static TX_THREAD client_thread;
30 static NX_PACKET_POOL client_pool;
31 static NX_WEB_HTTP_CLIENT my_client;
32 static NX_IP client_ip;
33 static UINT error_counter;
34 static TX_SEMAPHORE establish_semaphore;
35 static TX_SEMAPHORE data_semaphore;
36
37 /* Set up the HTTP server global variables */
38
39 #define SERVER_PACKET_SIZE (NX_WEB_HTTP_SERVER_MIN_PACKET_SIZE * 2)
40
41 static NX_WEB_HTTP_SERVER my_server;
42 static NX_PACKET_POOL server_pool;
43 static TX_THREAD server_thread;
44 static NX_IP server_ip;
45 static NXD_ADDRESS server_ip_address;
46 static UINT http_server_start = 0;
47 static UINT http_client_stop = 0;
48
49 static void thread_client_entry(ULONG thread_input);
50 static void thread_server_entry(ULONG thread_input);
51
52 #define HTTP_SERVER_ADDRESS IP_ADDRESS(1,2,3,4)
53 #define HTTP_CLIENT_ADDRESS IP_ADDRESS(1,2,3,5)
54
55 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr);
56
57 static char pkt[] = {
58 0x48, 0x54, /* ......HT */
59 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32, /* TP/1.0 2 */
60 0x30, 0x30, 0x20, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, /* 00 ..Con */
61 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, /* tent-Len */
62 0x67, 0x74, 0x68, 0x3a, 0x20, 0x20, 0x31, 0x32, /* gth: 12 */
63 0x31, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, /* 1..Conte */
64 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, /* nt-Type: */
65 0x20, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, /* text/pl */
66 0x61, 0x69, 0x6e, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, /* ain....< */
67 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0d, 0x0a, 0x0d, /* html>... */
68 };
69
70 static char pkt1[] = {
71 0x0a, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, /* .<head>. */
72 0x0a, 0x0d, 0x0a, 0x3c, 0x74, 0x69, 0x74, 0x6c, /* ...<titl */
73 0x65, 0x3e, 0x4d, 0x61, 0x69, 0x6e, 0x20, 0x57, /* e>Main W */
74 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x3c, 0x2f, 0x74, /* indow</t */
75 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x3c, /* itle>..< */
76 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, /* /head>.. */
77 0x0d, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, /* ..<body> */
78 };
79
80 static char pkt2[] = {
81 0x0a, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, /* .<head>. */
82 0x0a, 0x0d, 0x0a, 0x3c, 0x74, 0x69, 0x74, 0x6c, /* ...<titl */
83 0x65, 0x3e, 0x4d, 0x61, 0x69, 0x6e, 0x20, 0x57, /* e>Main W */
84 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x3c, 0x2f, 0x74, /* indow</t */
85 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x3c, /* itle>..< */
86 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, /* /head>.. */
87 0x0d, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, /* ..<body> */
88 };
89
90
91 #ifdef CTEST
test_application_define(void * first_unused_memory)92 VOID test_application_define(void *first_unused_memory)
93 #else
94 void netx_web_non_block_reconnect_test_application_define(void *first_unused_memory)
95 #endif
96 {
97 CHAR *pointer;
98 UINT status;
99
100
101 error_counter = 0;
102
103 /* Setup the working pointer. */
104 pointer = (CHAR *) first_unused_memory;
105
106 /* Create a helper thread for the server. */
107 tx_thread_create(&server_thread, "HTTP Server thread", thread_server_entry, 0,
108 pointer, DEMO_STACK_SIZE,
109 NX_WEB_HTTP_SERVER_PRIORITY, NX_WEB_HTTP_SERVER_PRIORITY, TX_NO_TIME_SLICE, TX_DONT_START);
110
111 pointer = pointer + DEMO_STACK_SIZE;
112
113 /* Initialize the NetX system. */
114 nx_system_initialize();
115
116 /* Create the server packet pool. */
117 status = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,
118 pointer, SERVER_PACKET_SIZE*8);
119 pointer = pointer + SERVER_PACKET_SIZE * 8;
120 if (status)
121 error_counter++;
122
123 /* Create an IP instance. */
124 status = nx_ip_create(&server_ip, "HTTP Server IP", HTTP_SERVER_ADDRESS,
125 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024,
126 pointer, 4096, 1);
127 pointer = pointer + 4096;
128 if (status)
129 error_counter++;
130
131 /* Enable ARP and supply ARP cache memory for the server IP instance. */
132 status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
133 pointer = pointer + 1024;
134 if (status)
135 error_counter++;
136
137
138 /* Enable TCP traffic. */
139 status = nx_tcp_enable(&server_ip);
140 if (status)
141 error_counter++;
142
143 /* Create the HTTP Client thread. */
144 status = tx_thread_create(&client_thread, "HTTP Client", thread_client_entry, 0,
145 pointer, DEMO_STACK_SIZE,
146 NX_WEB_HTTP_SERVER_PRIORITY + 2, NX_WEB_HTTP_SERVER_PRIORITY + 2, TX_NO_TIME_SLICE, TX_AUTO_START);
147 pointer = pointer + DEMO_STACK_SIZE;
148 if (status)
149 error_counter++;
150
151 /* Create the Client packet pool. */
152 status = nx_packet_pool_create(&client_pool, "HTTP Client Packet Pool", CLIENT_PACKET_SIZE,
153 pointer, CLIENT_PACKET_SIZE*8);
154 pointer = pointer + CLIENT_PACKET_SIZE * 8;
155 if (status)
156 error_counter++;
157
158 /* Create an IP instance. */
159 status = nx_ip_create(&client_ip, "HTTP Client IP", HTTP_CLIENT_ADDRESS,
160 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024,
161 pointer, 2048, 1);
162 pointer = pointer + 2048;
163 if (status)
164 error_counter++;
165
166 status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
167 pointer = pointer + 2048;
168 if (status)
169 error_counter++;
170
171 /* Enable TCP traffic. */
172 status = nx_tcp_enable(&client_ip);
173 if (status)
174 error_counter++;
175
176 /* Create the test control semaphore. */
177 tx_semaphore_create(&establish_semaphore, "Web Establish", 0);
178 tx_semaphore_create(&data_semaphore, "Web Data", 0);
179 }
180
http_response_callback(NX_WEB_HTTP_CLIENT * client_ptr,CHAR * field_name,UINT field_name_length,CHAR * field_value,UINT field_value_length)181 static VOID http_response_callback(NX_WEB_HTTP_CLIENT *client_ptr, CHAR *field_name, UINT field_name_length,
182 CHAR *field_value, UINT field_value_length)
183 {
184 if (memcmp(field_name, "Content-Type", field_name_length) == 0)
185 {
186 if (memcmp(field_value, "text/plain", field_value_length) != 0)
187 error_counter++;
188 }
189 else if(memcmp(field_name, "Content-Length", field_name_length) == 0)
190 {
191 if (memcmp(field_value, "121", field_value_length) != 0)
192 error_counter++;
193 }
194 #ifndef NX_WEB_HTTP_KEEPALIVE_DISABLE
195 else if(memcmp(field_name, "Connection", field_name_length) == 0)
196 {
197 if (memcmp(field_value, "keep-alive", field_value_length) != 0)
198 error_counter++;
199 }
200 #endif
201 }
202
tcp_establish_notify(NX_TCP_SOCKET * socket_ptr)203 static VOID tcp_establish_notify(NX_TCP_SOCKET *socket_ptr)
204 {
205
206 tx_semaphore_put(&establish_semaphore);
207 }
208
tcp_receive_callback(NX_TCP_SOCKET * socket_ptr)209 static VOID tcp_receive_callback(NX_TCP_SOCKET *socket_ptr)
210 {
211
212 tx_semaphore_put(&data_semaphore);
213 }
214
thread_client_entry(ULONG thread_input)215 void thread_client_entry(ULONG thread_input)
216 {
217 UINT status;
218 NX_PACKET *recv_packet;
219
220
221 /* Give IP task and driver a chance to initialize the system. */
222 tx_thread_sleep(NX_IP_PERIODIC_RATE);
223
224 /* Set server IP address. */
225 server_ip_address.nxd_ip_address.v4 = HTTP_SERVER_ADDRESS;
226 server_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
227
228 /* Create an HTTP client instance. */
229 status = nx_web_http_client_create(&my_client, "HTTP Client", &client_ip, &client_pool, 1536);
230
231 /* Check status. */
232 if (status)
233 error_counter++;
234
235 nx_tcp_socket_establish_notify(&my_client.nx_web_http_client_socket, tcp_establish_notify);
236 nx_tcp_socket_receive_notify(&my_client.nx_web_http_client_socket, tcp_receive_callback);
237
238 /* Set the header callback routine. */
239 nx_web_http_client_response_header_callback_set(&my_client, http_response_callback);
240
241 /* Connect to server. */
242 status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_NO_WAIT);
243
244 /* Check status. */
245 if (status != NX_IN_PROGRESS)
246 error_counter++;
247
248 status = tx_semaphore_get(&establish_semaphore, NX_IP_PERIODIC_RATE);
249
250 /* Server isn't stared, should be failed. */
251 if (status == TX_SUCCESS)
252 {
253 printf("ERROR!\n");
254 test_control_return(1);
255 }
256
257 tx_thread_resume(&server_thread);
258
259 /* Wait HTTP server started. */
260 while(!http_server_start)
261 {
262 tx_thread_sleep(NX_IP_PERIODIC_RATE);
263 }
264
265 /* Reconnect to server. */
266 status = nx_web_http_client_connect(&my_client, &server_ip_address, NX_WEB_HTTP_SERVER_PORT, NX_NO_WAIT);
267
268 /* Check status. */
269 if (status != NX_IN_PROGRESS)
270 {
271 printf("ERROR!\n");
272 test_control_return(1);
273 }
274
275 status = tx_semaphore_get(&establish_semaphore, NX_IP_PERIODIC_RATE);
276
277 /* Check status. */
278 if (status != TX_SUCCESS)
279 {
280 printf("ERROR!\n");
281 test_control_return(1);
282 }
283
284 /* Initialize the request. */
285 status = nx_web_http_client_request_initialize(&my_client, NX_WEB_HTTP_METHOD_GET, "/test.txt", "1.2.3.4",
286 0, NX_FALSE, "name", "password", NX_NO_WAIT);
287
288 /* Check status. */
289 if (status)
290 error_counter++;
291
292 /* Send the request. */
293 status = nx_web_http_client_request_send(&my_client, NX_NO_WAIT);
294
295 /* Check status. */
296 if (status)
297 error_counter++;
298
299 /* Get response from server. */
300 while (1)
301 {
302
303 status = tx_semaphore_get(&data_semaphore, 2 * NX_IP_PERIODIC_RATE);
304
305 /* Check status. */
306 if (status != TX_SUCCESS)
307 {
308 printf("ERROR!\n");
309 test_control_return(1);
310 }
311
312 while (1)
313 {
314
315 status = nx_web_http_client_response_body_get(&my_client, &recv_packet, NX_NO_WAIT);
316
317 if (status)
318 break;
319 else
320 nx_packet_release(recv_packet);
321 }
322
323 /* Check status. */
324 if (status == NX_WEB_HTTP_GET_DONE)
325 {
326 nx_packet_release(recv_packet);
327 break;
328 }
329 else if (status == NX_NO_PACKET)
330 {
331 continue;
332 }
333 else
334 {
335 error_counter++;
336 break;
337 }
338 }
339
340 status = nx_web_http_client_delete(&my_client);
341 if (status)
342 error_counter++;
343
344 /* Set the flag. */
345 http_client_stop = 1;
346 }
347
348 /* Define the helper HTTP server thread. */
thread_server_entry(ULONG thread_input)349 void thread_server_entry(ULONG thread_input)
350 {
351 UINT status;
352 FX_FILE my_file;
353 UINT server_port = NX_WEB_HTTP_SERVER_PORT;
354
355
356 /* Print out test information banner. */
357 printf("NetX Test: Web Non Block Reconnect Test..............................");
358
359 /* Check for earlier error. */
360 if(error_counter)
361 {
362 printf("ERROR!\n");
363 test_control_return(1);
364 }
365
366 fx_media_format(&ram_disk,
367 _fx_ram_driver, // Driver entry
368 ram_disk_memory, // RAM disk memory pointer
369 media_memory, // Media buffer pointer
370 sizeof(media_memory), // Media buffer size
371 "MY_RAM_DISK", // Volume Name
372 1, // Number of FATs
373 32, // Directory Entries
374 0, // Hidden sectors
375 256, // Total sectors
376 512, // Sector size
377 8, // Sectors per cluster
378 1, // Heads
379 1); // Sectors per track
380
381 /* Open the RAM disk. */
382 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, media_memory, sizeof(media_memory)) ;
383 status += fx_file_create(&ram_disk, "TEST.TXT");
384 status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
385 status += fx_file_write(&my_file, "https server", 12);
386 status += fx_file_close(&my_file);
387 if(status)
388 error_counter++;
389
390 /* Give NetX a chance to initialize the system. */
391 tx_thread_sleep(NX_IP_PERIODIC_RATE);
392
393 /* Create the HTTP Server. */
394 status = nx_web_http_server_create(&my_server, "My HTTP Server", &server_ip, server_port, &ram_disk,
395 &server_stack, sizeof(server_stack), &server_pool,
396 NX_NULL, server_request_callback);
397 if (status)
398 error_counter++;
399
400 /* OK to start the HTTP Server. */
401 status = nx_web_http_server_start(&my_server);
402 if (status)
403 error_counter++;
404
405 /* Set the flag. */
406 http_server_start = 1;
407
408 /* Wait HTTP test finished. */
409 while(!http_client_stop)
410 {
411 tx_thread_sleep(NX_IP_PERIODIC_RATE);
412 }
413
414 status = nx_web_http_server_delete(&my_server);
415 if (status)
416 error_counter++;
417
418 /* Check packet pool. */
419 if (server_pool.nx_packet_pool_available != server_pool.nx_packet_pool_total)
420 {
421 error_counter++;
422 }
423
424 if (client_pool.nx_packet_pool_available != client_pool.nx_packet_pool_total)
425 {
426 error_counter++;
427 }
428
429 if(error_counter)
430 {
431 printf("ERROR!\n");
432 test_control_return(1);
433 }
434 else
435 {
436 printf("SUCCESS!\n");
437 test_control_return(0);
438 }
439 }
440
441 /* Define the server request callback function. */
server_request_callback(NX_WEB_HTTP_SERVER * server_ptr,UINT request_type,CHAR * resource,NX_PACKET * packet_ptr)442 static UINT server_request_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
443 {
444 NX_PACKET *response_pkt;
445 UINT status;
446
447 /* Process multipart data. */
448 if(request_type == NX_WEB_HTTP_SERVER_GET_REQUEST)
449 {
450
451 /* Allocate a response packet. */
452 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
453
454 /* Check status. */
455 if (status)
456 {
457 error_counter++;
458 }
459
460 status = nx_packet_data_append(response_pkt, pkt, sizeof(pkt), &server_pool, NX_WAIT_FOREVER);
461
462 if(status == NX_SUCCESS)
463 {
464 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
465 {
466 nx_packet_release(response_pkt);
467 }
468 }
469
470 tx_thread_sleep(NX_IP_PERIODIC_RATE);
471
472 /* Allocate a response packet. */
473 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
474
475 /* Check status. */
476 if (status)
477 {
478 error_counter++;
479 }
480
481 status = nx_packet_data_append(response_pkt, pkt1, sizeof(pkt1), &server_pool, NX_WAIT_FOREVER);
482
483 if(status == NX_SUCCESS)
484 {
485 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
486 {
487 nx_packet_release(response_pkt);
488 }
489 }
490
491 tx_thread_sleep(NX_IP_PERIODIC_RATE);
492
493 /* Allocate a response packet. */
494 status = nx_web_http_server_response_packet_allocate(server_ptr, &response_pkt, TX_WAIT_FOREVER);
495
496 /* Check status. */
497 if (status)
498 {
499 error_counter++;
500 }
501
502 status = nx_packet_data_append(response_pkt, pkt2, sizeof(pkt2), &server_pool, NX_WAIT_FOREVER);
503
504 if(status == NX_SUCCESS)
505 {
506 if(nx_web_http_server_callback_packet_send(server_ptr, response_pkt) != NX_SUCCESS)
507 {
508 nx_packet_release(response_pkt);
509 }
510 }
511 }
512 else
513 {
514 /* Indicate we have not processed the response to client yet. */
515 return(NX_SUCCESS);
516 }
517
518 /* Indicate the response to client is transmitted. */
519 return(NX_WEB_HTTP_CALLBACK_COMPLETED);
520 }
521
522 #else
523
524 #ifdef CTEST
test_application_define(void * first_unused_memory)525 VOID test_application_define(void *first_unused_memory)
526 #else
527 void netx_web_non_block_reconnect_test_application_define(void *first_unused_memory)
528 #endif
529 {
530
531 /* Print out test information banner. */
532 printf("NetX Test: Web Non Block Reconenct Test..............................N/A\n");
533
534 test_control_return(3);
535 }
536 #endif
537
538