1 /* This NetX test concentrates on the basic ARP operation.  */
2 
3 #include   "tx_api.h"
4 #include   "nx_api.h"
5 
6 extern void    test_control_return(UINT status);
7 
8 #if !defined(NX_DISABLE_IPV4)
9 
10 #define     DEMO_STACK_SIZE         2048
11 
12 
13 /* Define the ThreadX and NetX object control blocks...  */
14 
15 static TX_THREAD               ntest_0;
16 static TX_THREAD               ntest_1;
17 
18 static NX_PACKET_POOL          pool_0;
19 static NX_IP                   ip_0;
20 static NX_IP                   ip_1;
21 static NX_TCP_SOCKET           client_socket;
22 static NX_TCP_SOCKET           server_socket;
23 
24 
25 
26 /* Define the counters used in the test application...  */
27 
28 static ULONG                   error_counter;
29 
30 
31 /* Define thread prototypes.  */
32 
33 static void    ntest_0_entry(ULONG thread_input);
34 static void    ntest_1_entry(ULONG thread_input);
35 static void    ntest_1_connect_received(NX_TCP_SOCKET *server_socket, UINT port);
36 static void    ntest_1_disconnect_received(NX_TCP_SOCKET *server_socket);
37 extern void    _nx_ram_network_driver_256(struct NX_IP_DRIVER_STRUCT *driver_req);
38 
39 
40 /* Define what the initial system looks like.  */
41 
42 #ifdef CTEST
test_application_define(void * first_unused_memory)43 VOID test_application_define(void *first_unused_memory)
44 #else
45 void    netx_arp_basic_test_application_define(void *first_unused_memory)
46 #endif
47 {
48 
49 CHAR    *pointer;
50 UINT    status;
51 
52 
53     /* Setup the working pointer.  */
54     pointer =  (CHAR *) first_unused_memory;
55 
56     error_counter =  0;
57 
58     /* Create the main thread.  */
59     tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,
60             pointer, DEMO_STACK_SIZE,
61             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
62 
63     pointer =  pointer + DEMO_STACK_SIZE;
64 
65     /* Create the main thread.  */
66     tx_thread_create(&ntest_1, "thread 1", ntest_1_entry, 0,
67             pointer, DEMO_STACK_SIZE,
68             3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
69 
70     pointer =  pointer + DEMO_STACK_SIZE;
71 
72 
73     /* Initialize the NetX system.  */
74     nx_system_initialize();
75 
76     /* Create a packet pool.  */
77     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 8192);
78     pointer = pointer + 8192;
79 
80     if (status)
81         error_counter++;
82 
83     /* Create an IP instance.  */
84     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_256,
85                     pointer, 2048, 1);
86     pointer =  pointer + 2048;
87 
88     /* Create another IP instance.  */
89     status += nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 5), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_256,
90                     pointer, 2048, 2);
91     pointer =  pointer + 2048;
92     if (status)
93         error_counter++;
94 
95     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
96     status =  nx_arp_enable(&ip_0, (void *) pointer, 1024);
97     pointer = pointer + 1024;
98     if (status)
99         error_counter++;
100 
101     /* Enable ARP and supply ARP cache memory for IP Instance 1.  */
102     status  =  nx_arp_enable(&ip_1, (void *) pointer, 1024);
103     pointer = pointer + 1024;
104     if (status)
105         error_counter++;
106 
107     /* Enable TCP processing for both IP instances.  */
108     status =  nx_tcp_enable(&ip_0);
109     status += nx_tcp_enable(&ip_1);
110 
111     /* Check TCP enable status.  */
112     if (status)
113         error_counter++;
114 }
115 
116 
117 
118 /* Define the test threads.  */
119 
ntest_0_entry(ULONG thread_input)120 static void    ntest_0_entry(ULONG thread_input)
121 {
122 
123 UINT        status;
124 NX_PACKET   *my_packet;
125 
126 
127     printf("NetX Test:   ARP Basic Processing Test.................................");
128 
129     /* Check for earlier error.  */
130     if (error_counter)
131     {
132 
133         printf("ERROR!\n");
134         test_control_return(1);
135     }
136 
137     /* Create a socket.  */
138     status =  nx_tcp_socket_create(&ip_0, &client_socket, "Client Socket",
139                                 NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 200,
140                                 NX_NULL, NX_NULL);
141 
142     /* Check for error.  */
143     if (status)
144         error_counter++;
145 
146     /* Bind the socket.  */
147     status =  nx_tcp_client_socket_bind(&client_socket, 12, NX_WAIT_FOREVER);
148 
149     /* Check for error.  */
150     if (status)
151         error_counter++;
152 
153     /* Attempt to connect the socket.  */
154     status =  nx_tcp_client_socket_connect(&client_socket, IP_ADDRESS(1, 2, 3, 5), 12, 5 * NX_IP_PERIODIC_RATE);
155 
156     /* Check for error.  */
157     if (status)
158         error_counter++;
159 
160     /* Allocate a packet.  */
161     status =  nx_packet_allocate(&pool_0, &my_packet, NX_TCP_PACKET, NX_WAIT_FOREVER);
162 
163     /* Check status.  */
164     if (status != NX_SUCCESS)
165         error_counter++;
166 
167     /* Write ABCs into the packet payload!  */
168     memcpy(my_packet -> nx_packet_prepend_ptr, "ABCDEFGHIJKLMNOPQRSTUVWXYZ  ", 28);
169 
170     /* Adjust the write pointer.  */
171     my_packet -> nx_packet_length =  28;
172     my_packet -> nx_packet_append_ptr =  my_packet -> nx_packet_prepend_ptr + 28;
173 
174     /* Send the packet out!  */
175     status =  nx_tcp_socket_send(&client_socket, my_packet, 5 * NX_IP_PERIODIC_RATE);
176 
177     /* Determine if the status is valid.  */
178     if (status)
179     {
180         error_counter++;
181         nx_packet_release(my_packet);
182     }
183 
184     /* Disconnect this socket.  */
185     status =  nx_tcp_socket_disconnect(&client_socket, 5 * NX_IP_PERIODIC_RATE);
186 
187     /* Determine if the status is valid.  */
188     if (status)
189         error_counter++;
190 
191     /* Unbind the socket.  */
192     status =  nx_tcp_client_socket_unbind(&client_socket);
193 
194     /* Check for error.  */
195     if (status)
196         error_counter++;
197 
198     /* Delete the socket.  */
199     status =  nx_tcp_socket_delete(&client_socket);
200 
201     /* Check for error.  */
202     if (status)
203         error_counter++;
204 
205     /* Determine if the test was successful.  */
206     if (error_counter)
207     {
208 
209         printf("ERROR!\n");
210         test_control_return(1);
211     }
212     else
213     {
214 
215         printf("SUCCESS!\n");
216         test_control_return(0);
217     }
218 }
219 
220 
ntest_1_entry(ULONG thread_input)221 static void    ntest_1_entry(ULONG thread_input)
222 {
223 
224 UINT            status;
225 NX_PACKET       *packet_ptr;
226 ULONG           actual_status;
227 
228 
229     /* Ensure the IP instance has been initialized.  */
230     status =  nx_ip_status_check(&ip_1, NX_IP_INITIALIZE_DONE, &actual_status, NX_IP_PERIODIC_RATE);
231 
232     /* Check status...  */
233     if (status != NX_SUCCESS)
234     {
235 
236         printf("ERROR!\n");
237         test_control_return(1);
238     }
239 
240     /* Create a socket.  */
241     status =  nx_tcp_socket_create(&ip_1, &server_socket, "Server Socket",
242                                 NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 100,
243                                 NX_NULL, ntest_1_disconnect_received);
244 
245     /* Check for error.  */
246     if (status)
247         error_counter++;
248 
249     /* Setup this thread to listen.  */
250     status =  nx_tcp_server_socket_listen(&ip_1, 12, &server_socket, 5, ntest_1_connect_received);
251 
252     /* Check for error.  */
253     if (status)
254         error_counter++;
255 
256     /* Accept a client socket connection.  */
257     status =  nx_tcp_server_socket_accept(&server_socket, 5 * NX_IP_PERIODIC_RATE);
258 
259     /* Check for error.  */
260     if (status)
261         error_counter++;
262 
263     /* Receive a TCP message from the socket.  */
264     status =  nx_tcp_socket_receive(&server_socket, &packet_ptr, 5 * NX_IP_PERIODIC_RATE);
265 
266     /* Check for error.  */
267     if ((status) || (packet_ptr -> nx_packet_length != 28))
268         error_counter++;
269     else
270         /* Release the packet.  */
271         nx_packet_release(packet_ptr);
272 
273     /* Disconnect the server socket.  */
274     status =  nx_tcp_socket_disconnect(&server_socket, 5 * NX_IP_PERIODIC_RATE);
275 
276     /* Check for error.  */
277     if (status)
278         error_counter++;
279 
280     /* Unaccept the server socket.  */
281     status =  nx_tcp_server_socket_unaccept(&server_socket);
282 
283     /* Check for error.  */
284     if (status)
285         error_counter++;
286 
287     /* Setup server socket for listening again.  */
288     status =  nx_tcp_server_socket_relisten(&ip_1, 12, &server_socket);
289 
290     /* Check for error.  */
291     if (status)
292         error_counter++;
293 }
294 
295 
ntest_1_connect_received(NX_TCP_SOCKET * socket_ptr,UINT port)296 static void  ntest_1_connect_received(NX_TCP_SOCKET *socket_ptr, UINT port)
297 {
298 
299     /* Check for the proper socket and port.  */
300     if ((socket_ptr != &server_socket) || (port != 12))
301         error_counter++;
302 }
303 
304 
ntest_1_disconnect_received(NX_TCP_SOCKET * socket)305 static void  ntest_1_disconnect_received(NX_TCP_SOCKET *socket)
306 {
307 
308     /* Check for proper disconnected socket.  */
309     if (socket != &server_socket)
310         error_counter++;
311 }
312 #else
313 
314 #ifdef CTEST
test_application_define(void * first_unused_memory)315 VOID test_application_define(void *first_unused_memory)
316 #else
317 void    netx_arp_basic_test_application_define(void *first_unused_memory)
318 #endif
319 {
320 
321     /* Print out test information banner.  */
322     printf("NetX Test:   ARP Basic Processing Test.................................N/A\n");
323 
324     test_control_return(3);
325 }
326 #endif
327 
328