1 /* This NetX test concentrates on the UDP free port find operation.  */
2 
3 
4 #include   "tx_api.h"
5 #include   "nx_api.h"
6 
7 #define     DEMO_STACK_SIZE         2048
8 
9 
10 /* Define the ThreadX and NetX object control blocks...  */
11 
12 static TX_THREAD               thread_0;
13 
14 static NX_PACKET_POOL          pool_0;
15 static NX_IP                   ip_0;
16 
17 static NX_UDP_SOCKET           socket_0;
18 static NX_UDP_SOCKET           my_socket[NX_MAX_PORT];
19 
20 /* Define the counters used in the demo application...  */
21 
22 static ULONG                   error_counter;
23 
24 
25 /* Define thread prototypes.  */
26 
27 static void    thread_0_entry(ULONG thread_input);
28 extern void    _nx_ram_network_driver_256(struct NX_IP_DRIVER_STRUCT *driver_req);
29 
30 extern void  test_control_return(UINT status);
31 /* Define what the initial system looks like.  */
32 
33 #ifdef CTEST
test_application_define(void * first_unused_memory)34 VOID test_application_define(void *first_unused_memory)
35 #else
36 void netx_udp_socket_bind_test_application_define(void *first_unused_memory)
37 #endif
38 {
39 
40 CHAR    *pointer;
41 UINT    status;
42 
43 
44     /* Setup the working pointer.  */
45     pointer =  (CHAR *) first_unused_memory;
46 
47     error_counter =  0;
48 
49     /* Create the main thread.  */
50     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
51             pointer, DEMO_STACK_SIZE,
52             3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
53     pointer =  pointer + DEMO_STACK_SIZE;
54 
55     /* Initialize the NetX system.  */
56     nx_system_initialize();
57 
58     /* Create a packet pool.  */
59     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 2048);
60     pointer = pointer + 2048;
61 
62     /* Check for pool creation error.  */
63     if (status)
64         error_counter++;
65 
66     /* Create an IP instance.  */
67     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFF000UL, &pool_0, _nx_ram_network_driver_256,
68                     pointer, 2048, 1);
69     pointer =  pointer + 2048;
70 
71     /* Check for IP create errors.  */
72     if (status)
73         error_counter++;
74 
75     /* Enable UDP traffic.  */
76     status =  nx_udp_enable(&ip_0);
77 
78     /* Check for UDP enable errors.  */
79     if (status)
80         error_counter++;
81 }
82 
83 
84 
85 /* Define the test threads.  */
86 
thread_0_entry(ULONG thread_input)87 static void    thread_0_entry(ULONG thread_input)
88 {
89 
90 UINT        status;
91 UINT        i;
92 
93 
94     /* Print out some test information banners.  */
95     printf("NetX Test:   UDP Socket Bind Test......................................");
96 
97     /* Check for earlier error.  */
98     if (error_counter)
99     {
100         printf("ERROR!\n");
101         test_control_return(1);
102     }
103 
104     /* Create a UDP socket.  */
105     status = nx_udp_socket_create(&ip_0, &socket_0, "Socket 0", NX_IP_NORMAL, NX_FRAGMENT_OKAY, 0x80, 5);
106     if (status != NX_SUCCESS)
107         error_counter++;
108 
109     status = nx_udp_socket_bind(&socket_0, 1234, 2 * NX_IP_PERIODIC_RATE);
110     if(status != NX_SUCCESS)
111         error_counter++;
112 
113     /* Bind again. */
114     status = nx_udp_socket_bind(&socket_0, 1234, 2 * NX_IP_PERIODIC_RATE);
115     if(status != NX_ALREADY_BOUND)
116         error_counter++;
117 
118     /* Unbind the UDP socket.  */
119     status =  nx_udp_socket_unbind(&socket_0);
120     if (status != NX_SUCCESS)
121         error_counter++;
122 
123     /* Use up the ports. */
124     for(i = 0; i < NX_MAX_PORT; i++)
125     {
126         status = nx_udp_socket_create(&ip_0, &my_socket[i], "Socket Array", NX_IP_NORMAL, NX_FRAGMENT_OKAY, 0x80, 5);
127         if (status != NX_SUCCESS)
128         {
129             printf("ERROR!\n");
130             test_control_return(1);
131         }
132 
133         status = nx_udp_socket_bind(&my_socket[i], i+1, NX_IP_PERIODIC_RATE);
134         if (status != NX_SUCCESS)
135         {
136             printf("ERROR!\n");
137             test_control_return(1);
138         }
139     }
140 
141     /* Bind to any port. */
142     status = nx_udp_socket_bind(&socket_0, NX_ANY_PORT, 2 * NX_IP_PERIODIC_RATE);
143     if(status != NX_NO_FREE_PORTS)
144         error_counter++;
145 
146     /* Bind to 1234. */
147     status = nx_udp_socket_bind(&socket_0, 1234, NX_NO_WAIT);
148     if(status != NX_PORT_UNAVAILABLE)
149         error_counter++;
150 
151     /* Delete the UDP socket.  */
152     status =  nx_udp_socket_delete(&socket_0);
153     if (status)
154         error_counter++;
155 
156     /* Check status.  */
157     if (error_counter)
158     {
159         printf("ERROR!\n");
160         test_control_return(1);
161     }
162     else
163     {
164 
165         printf("SUCCESS!\n");
166         test_control_return(0);
167     }
168 }
169 
170 
171