1 
2 #include   "tx_api.h"
3 #include   "nx_api.h"
4 #include   "nx_tcp.h"
5 #include   "nx_udp.h"
6 
7 extern void    test_control_return(UINT status);
8 #if defined(__PRODUCT_NETXDUO__) && !defined(NX_DISABLE_IPV4)
9 #define     DEMO_STACK_SIZE         2048
10 
11 
12 /* Define the ThreadX and NetX object control blocks...  */
13 
14 static TX_THREAD               ntest_0;
15 static TX_THREAD               ntest_1;
16 static TX_THREAD               ntest_2;
17 
18 static NX_PACKET_POOL          pool_0;
19 static NX_IP                   ip_0;
20 static NX_IP                   ip_1;
21 
22 /* Define the counters used in the test application...  */
23 
24 static ULONG                   error_counter;
25 
26 /* Define thread prototypes.  */
27 
28 static void    ntest_0_entry(ULONG thread_input);
29 static void    ntest_1_entry(ULONG thread_input);
30 static void    ntest_2_entry(ULONG thread_input);
31 extern void    _nx_ram_network_driver_1500(struct NX_IP_DRIVER_STRUCT *driver_req);
32 
33 #ifdef FEATURE_NX_IPV6
34 static NXD_ADDRESS  ipv6_addr_0;
35 static NXD_ADDRESS  ipv6_addr_1;
36 #endif
37 
38 /* Define what the initial system looks like.  */
39 
40 #ifdef CTEST
test_application_define(void * first_unused_memory)41 VOID test_application_define(void *first_unused_memory)
42 #else
43 void    netx_raw_special_test_application_define(void *first_unused_memory)
44 #endif
45 {
46 
47 CHAR    *pointer;
48 UINT    status;
49 
50 
51     /* Setup the working pointer.  */
52     pointer = (CHAR *) first_unused_memory;
53 
54     error_counter = 0;
55 
56     /* Create the main thread.  */
57     tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,
58             pointer, DEMO_STACK_SIZE,
59             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
60     pointer = pointer + DEMO_STACK_SIZE;
61 
62     tx_thread_create(&ntest_1, "thread 1", ntest_1_entry, 0,
63             pointer, DEMO_STACK_SIZE,
64             4, 4, TX_NO_TIME_SLICE, TX_DONT_START);
65     pointer = pointer + DEMO_STACK_SIZE;
66 
67     tx_thread_create(&ntest_2, "thread 2", ntest_2_entry, 0,
68             pointer, DEMO_STACK_SIZE,
69             4, 4, TX_NO_TIME_SLICE, TX_DONT_START);
70     pointer = pointer + DEMO_STACK_SIZE;
71 
72     /* Initialize the NetX system.  */
73     nx_system_initialize();
74 
75     /* Create a packet pool.  */
76     status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 8192);
77     pointer = pointer + 8192;
78     if (status != NX_SUCCESS)
79         error_counter++;
80 
81     /* Create IP instances.  */
82     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 9), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500,
83                     pointer, 2048, 1);
84     pointer = pointer + 2048;
85     if (status != NX_SUCCESS)
86         error_counter++;
87 
88     status = nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 10), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500,
89                     pointer, 2048, 1);
90     pointer = pointer + 2048;
91     if (status != NX_SUCCESS)
92         error_counter++;
93 
94     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
95     status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
96     pointer = pointer + 1024;
97     if (status != NX_SUCCESS)
98         error_counter++;
99 
100     status =  nx_arp_enable(&ip_1, (void *) pointer, 1024);
101     pointer = pointer + 1024;
102     if (status != NX_SUCCESS)
103         error_counter++;
104 
105     /* Enable UDP for IP instances. */
106     status = nx_udp_enable(&ip_0);
107     if (status != NX_SUCCESS)
108         error_counter++;
109 
110     status = nx_udp_enable(&ip_1);
111     if (status != NX_SUCCESS)
112         error_counter++;
113 
114 #ifdef FEATURE_NX_IPV6
115     ipv6_addr_0.nxd_ip_version = NX_IP_VERSION_V6;
116     ipv6_addr_0.nxd_ip_address.v6[0] = 0x20010000;
117     ipv6_addr_0.nxd_ip_address.v6[1] = 0x00000000;
118     ipv6_addr_0.nxd_ip_address.v6[2] = 0x00000000;
119     ipv6_addr_0.nxd_ip_address.v6[3] = 0x00010001;
120 
121     ipv6_addr_1.nxd_ip_version = NX_IP_VERSION_V6;
122     ipv6_addr_1.nxd_ip_address.v6[0] = 0x30010000;
123     ipv6_addr_1.nxd_ip_address.v6[1] = 0x02300000;
124     ipv6_addr_1.nxd_ip_address.v6[2] = 0x00440000;
125     ipv6_addr_1.nxd_ip_address.v6[3] = 0x00010002;
126 
127     /* Enable IPv6 */
128     status = nxd_ipv6_enable(&ip_0);
129     if (status != NX_SUCCESS)
130         error_counter++;
131 
132     status = nxd_ipv6_enable(&ip_1);
133     if (status != NX_SUCCESS)
134         error_counter++;
135 
136     status = nxd_icmp_enable(&ip_0);
137     if(status != NX_SUCCESS)
138         error_counter++;
139 
140     status = nxd_icmp_enable(&ip_1);
141     if(status)
142         error_counter++;
143 
144     status = nxd_ipv6_address_set(&ip_0, 0, &ipv6_addr_0, 64, NX_NULL);
145     if(status != NX_SUCCESS)
146         error_counter++;
147 #endif
148 
149 
150 }
151 
ntest_0_entry(ULONG thread_input)152 static void    ntest_0_entry(ULONG thread_input)
153 {
154 
155 UINT        status;
156 NX_PACKET   *my_packet;
157 ULONG       value;
158 UINT        i;
159 
160 
161     /* Print out test information banner.  */
162     printf("NetX Test:   IPv6 Raw Special Test.....................................");
163 
164     /* Check for earlier error.  */
165     if (error_counter)
166     {
167         printf("ERROR!\n");
168         test_control_return(1);
169     }
170 
171 #ifdef FEATURE_NX_IPV6
172     /* DAD */
173     tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
174 #endif
175 
176     /* Check the status of the IP instances.  */
177     status =  nx_ip_status_check(&ip_0, NX_IP_INITIALIZE_DONE, &value, NX_IP_PERIODIC_RATE);
178     if ((status) || (value != NX_IP_INITIALIZE_DONE))
179         error_counter++;
180 
181     /* Allocate a packet.  */
182     status = nx_packet_allocate(&pool_0, &my_packet, NX_UDP_PACKET, 2 * NX_IP_PERIODIC_RATE);
183     if (status != NX_SUCCESS)
184         error_counter++;
185 
186     /* Write ABCs into the packet payload!  */
187     status = nx_packet_data_append(my_packet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ  ", 28, &pool_0, 2 * NX_IP_PERIODIC_RATE);
188     if (status != NX_SUCCESS)
189         error_counter++;
190 
191 #ifndef NX_DISABLE_ERROR_CHECKING
192     /* Send the raw IP packet Before enable.  */
193     status =  nx_ip_raw_packet_send(&ip_0, my_packet, IP_ADDRESS(1, 2, 3, 10), NX_IP_NORMAL);
194     if (status != NX_NOT_ENABLED)
195         error_counter++;
196 #endif /* NX_DISABLE_ERROR_CHECKING */
197 
198     /* Enable RAW. */
199     status =  nx_ip_raw_packet_enable(&ip_0);
200     if (status != NX_SUCCESS)
201         error_counter++;
202 
203     /* Send to a address that can't be routed. */
204     status =  nx_ip_raw_packet_send(&ip_0, my_packet, IP_ADDRESS(23, 42, 3, 10), NX_IP_NORMAL);
205     if (status != NX_IP_ADDRESS_ERROR)
206         error_counter++;
207 
208     /* Let ntest_1 do its job. */
209     tx_thread_resume(&ntest_1);
210     tx_thread_suspend(&ntest_0);
211 
212     /* Two threads ntest_1 and ntest_2 suspend on the packet now. This will cover some code in raw_packet_processing */
213     status =  nx_ip_raw_packet_send(&ip_0, my_packet, IP_ADDRESS(1, 2, 3, 10), NX_IP_NORMAL);
214     if(status != NX_SUCCESS)
215         error_counter++;
216 
217     /* Send packets to fill the raw packet receive queue. This will cover some code in raw_packet_processing */
218     for(i = 0;  i < ip_0.nx_ip_raw_received_packet_max + 5; i++)
219     {
220         /* Allocate a packet.  */
221         status = nx_packet_allocate(&pool_0, &my_packet, NX_UDP_PACKET, 2 * NX_IP_PERIODIC_RATE);
222         if (status != NX_SUCCESS)
223             error_counter++;
224 
225         /* Write ABCs into the packet payload!  */
226         status = nx_packet_data_append(my_packet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ  ", 28, &pool_0, 2 * NX_IP_PERIODIC_RATE);
227         if (status != NX_SUCCESS)
228             error_counter++;
229 
230         status =  nx_ip_raw_packet_send(&ip_0, my_packet, IP_ADDRESS(1, 2, 3, 10), NX_IP_NORMAL);
231         if(status != NX_SUCCESS)
232         {
233             nx_packet_release(my_packet);
234         }
235     }
236 
237     /* Disable RAW for ip_1.  To test the situation that there are packets in the RAW packet queue. */
238     status = nx_ip_raw_packet_disable(&ip_1);
239     if(status != NX_SUCCESS)
240         error_counter++;
241 
242     /* Enable again. */
243     status = nx_ip_raw_packet_enable(&ip_1);
244     if(status != NX_SUCCESS)
245         error_counter++;
246 
247     tx_thread_suspend(&ntest_0);
248 
249     /* When thread ends, raw_packet_cleanup will be called. */
250 
251     if(error_counter)
252     {
253         printf("ERROR!\n");
254         test_control_return(1);
255     }
256     else
257     {
258         printf("SUCCESS!\n");
259         test_control_return(0);
260     }
261 }
262 
ntest_1_entry(ULONG thread_input)263 static void  ntest_1_entry(ULONG thread_input)
264 {
265 UINT        status;
266 NX_PACKET   *my_packet;
267 ULONG       value;
268 
269     /* Check the status of the IP instances.  */
270     status =  nx_ip_status_check(&ip_1, NX_IP_INITIALIZE_DONE, &value, NX_IP_PERIODIC_RATE);
271     if ((status) || (value != NX_IP_INITIALIZE_DONE))
272         error_counter++;
273 
274     status = nx_ip_raw_packet_enable(&ip_1);
275     if (status != NX_SUCCESS)
276         error_counter++;
277 
278     tx_thread_resume(&ntest_2);
279     status =  nx_ip_raw_packet_receive(&ip_1, &my_packet, 5 * NX_IP_PERIODIC_RATE);
280     if(status == NX_SUCCESS)
281     {
282         status = nx_packet_release(my_packet);
283         if (status != NX_SUCCESS)
284             error_counter++;
285     }
286 
287     /* Suspend on the packet to test raw_packet_cleanup. */
288     status =  nx_ip_raw_packet_receive(&ip_1, &my_packet, 5 * NX_IP_PERIODIC_RATE);
289 
290 }
291 
ntest_2_entry(ULONG thread_input)292 static void  ntest_2_entry(ULONG thread_input)
293 {
294 UINT        status;
295 NX_PACKET   *my_packet;
296 
297     tx_thread_resume(&ntest_0);
298 
299     status =  nx_ip_raw_packet_receive(&ip_1, &my_packet, 5 * NX_IP_PERIODIC_RATE);
300     if(status == NX_SUCCESS)
301     {
302         status = nx_packet_release(my_packet);
303         if (status != NX_SUCCESS)
304             error_counter++;
305     }
306 
307     tx_thread_resume(&ntest_0);
308     /* Suspend on the packet to test raw_packet_cleanup. */
309     status =  nx_ip_raw_packet_receive(&ip_1, &my_packet, 5 * NX_IP_PERIODIC_RATE);
310 }
311 
312 #else
313 #ifdef CTEST
test_application_define(void * first_unused_memory)314 VOID test_application_define(void *first_unused_memory)
315 #else
316 void    netx_raw_special_test_application_define(void *first_unused_memory)
317 #endif
318 {
319 
320     /* Print out test information banner.  */
321     printf("NetX Test:   IPv6 Raw Special Test.....................................N/A\n");
322     test_control_return(3);
323 }
324 #endif /* __PRODUCT_NETXDUO__ */
325