1 #include   "tx_api.h"
2 #include   "nx_api.h"
3 
4 extern void    test_control_return(UINT status);
5 
6 #if defined __PRODUCT_NETXDUO__ && !defined NX_MDNS_DISABLE_SERVER && !defined NX_DISABLE_IPV4
7 #include   "nxd_mdns.h"
8 
9 #define     DEMO_STACK_SIZE             2048
10 #define     BUFFER_SIZE                 10240
11 #define     LOCAL_FULL_SERVICE_COUNT    16
12 #define     PEER_FULL_SERVICE_COUNT     16
13 #define     PEER_PARTIAL_SERVICE_COUNT  32
14 
15 /* Define the ThreadX and NetX object control blocks...  */
16 
17 static TX_THREAD               ntest_0;
18 
19 static NX_PACKET_POOL          pool_0;
20 static NX_IP                   ip_0;
21 
22 /* Define the NetX MDNS object control blocks.  */
23 
24 static NX_MDNS                 mdns_0;
25 static UCHAR                   buffer[BUFFER_SIZE];
26 static ULONG                   current_buffer_size;
27 static UCHAR                   mdns_stack[DEMO_STACK_SIZE];
28 static ULONG                   buffer_org_head;
29 static ULONG                   buffer_org_tail;
30 
31 /* Define the counters used in the test application...  */
32 
33 static ULONG                   error_counter;
34 static CHAR                   *pointer;
35 
36 /* Define thread prototypes.  */
37 
38 static void    ntest_0_entry(ULONG thread_input);
39 extern VOID    _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
40 static void    check_empty_buffer(UCHAR *buffer_ptr, ULONG buffer_size, UCHAR expect_empty);
41 static void    empty_buffer_init();
42 
43 /* Define what the initial system looks like.  */
44 
45 #ifdef CTEST
test_application_define(void * first_unused_memory)46 VOID test_application_define(void *first_unused_memory)
47 #else
48 void           netx_mdns_two_buffer_test(void *first_unused_memory)
49 #endif
50 {
51 
52 UINT       status;
53 
54     /* Setup the working pointer.  */
55     pointer = (CHAR *) first_unused_memory;
56     error_counter = 0;
57 
58     /* Initialize the NetX system.  */
59     nx_system_initialize();
60 
61     /* Create a packet pool.  */
62     status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 8192);
63     pointer = pointer + 8192;
64 
65     if(status)
66         error_counter++;
67 
68     /* Create an IP instance.  */
69     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(192,168,0,31), 0xFFFFFF00UL, &pool_0,
70                           _nx_ram_network_driver, pointer, 2048, 1);
71     pointer = pointer + 2048;
72 
73     if(status)
74         error_counter++;
75 
76     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
77     status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
78     pointer = pointer + 1024;
79     if(status)
80         error_counter++;
81 
82     /* Enable TCP processing for both IP instances.  */
83     status = nx_tcp_enable(&ip_0);
84 
85     /* Check TCP enable status.  */
86     if(status)
87         error_counter++;
88 
89     /* Enable UDP processing for both IP instances.  */
90     status = nx_udp_enable(&ip_0);
91 
92     /* Check UDP enable status.  */
93     if(status)
94         error_counter++;
95 
96     status = nx_igmp_enable(&ip_0);
97 
98     /* Check status. */
99     if(status)
100         error_counter++;
101 
102     /* Create the test thread.  */
103     tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, (ULONG)(pointer + DEMO_STACK_SIZE),
104                      pointer, DEMO_STACK_SIZE,
105                      3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
106 }
107 
108 /* Define the test threads.  */
109 
ntest_0_entry(ULONG thread_input)110 static void    ntest_0_entry(ULONG thread_input)
111 {
112 UINT       status;
113 ULONG      actual_status;
114 CHAR      *pointer = (CHAR*)thread_input;
115 
116     printf("NetX Test:   MDNS Two Buffer Test......................................");
117 
118     /* Ensure the IP instance has been initialized.  */
119     status = nx_ip_status_check(&ip_0, NX_IP_INITIALIZE_DONE, &actual_status, 100);
120 
121     /* Check status. */
122     if(status != NX_SUCCESS)
123     {
124         printf("ERROR!\n");
125         test_control_return(1);
126     }
127 
128     /* Set pointer. */
129     pointer = (CHAR*)thread_input;
130     /* Create */
131     current_buffer_size = 160;
132     status = nx_mdns_create(&mdns_0, &ip_0, &pool_0, 2, pointer, DEMO_STACK_SIZE, "NETX-MDNS",
133                             buffer, current_buffer_size, buffer + current_buffer_size, current_buffer_size, NX_NULL);
134 
135     /* Check status. */
136     if(status != NX_SUCCESS)
137         error_counter++;
138 
139     empty_buffer_init();
140 
141 #ifndef NX_MDNS_DISABLE_SERVER
142     /* Buffer is too small to add a service. */
143     nx_mdns_service_add(&mdns_0, (CHAR *)"ARMMDNSTest", (CHAR *)"_ipp._tcp", NX_NULL, NX_NULL, 100, 0, 0, 80, NX_MDNS_RR_SET_UNIQUE, 0);
144 
145     /* Check local buffer. It must be empty. */
146     check_empty_buffer(buffer, current_buffer_size, NX_TRUE);
147 
148     if(mdns_0.nx_mdns_local_rr_count != 0)
149         error_counter++;
150 #endif /* NX_MDNS_DISABLE_SERVER  */
151 
152 #ifndef NX_MDNS_DISABLE_CLIENT
153     /* Check remote buffer. It must be empty. */
154     check_empty_buffer(buffer + current_buffer_size, current_buffer_size, NX_TRUE);
155 
156     /* Check mdns information. */
157     if(mdns_0.nx_mdns_peer_rr_count != 0)
158         error_counter++;
159 #endif /* NX_MDNS_DISABLE_CLIENT  */
160 
161     /* Delete */
162     nx_mdns_delete(&mdns_0);
163 
164     /* Initialize the buffer. */
165     current_buffer_size = (BUFFER_SIZE >> 1);
166     status = nx_mdns_create(&mdns_0, &ip_0, &pool_0, 2, pointer, DEMO_STACK_SIZE, "NETX-MDNS",
167                             buffer, current_buffer_size, buffer + current_buffer_size, current_buffer_size, NX_NULL);
168 
169     /* Check status. */
170     if(status != NX_SUCCESS)
171         error_counter++;
172 
173     /* Enable mDNS. */
174     nx_mdns_enable(&mdns_0, 0);
175 
176     empty_buffer_init();
177 
178 #ifndef NX_MDNS_DISABLE_SERVER
179     /* Use local buffer. */
180     nx_mdns_service_add(&mdns_0, "ARMMDNSTest", "_ipp._tcp", NX_NULL, NX_NULL, 100, 0, 0, 80, NX_MDNS_RR_SET_UNIQUE, 0);
181 
182     /* Check local buffer. It must not be empty. */
183     check_empty_buffer(buffer, current_buffer_size, NX_FALSE);
184 
185     /* Delete the service. */
186     nx_mdns_service_delete(&mdns_0, "ARMMDNSTest", "_ipp._tcp", NX_NULL);
187 
188     /* Sleep 1 second for goodbye packet. */
189     tx_thread_sleep(NX_IP_PERIODIC_RATE);
190 
191     /* Check local buffer. It must be empty. */
192     check_empty_buffer(buffer, current_buffer_size, NX_TRUE);
193 
194     /* Check mdns information. */
195     if(mdns_0.nx_mdns_local_rr_count != 2)
196         error_counter++;
197 #endif /* NX_MDNS_DISABLE_SERVER  */
198 
199 #ifndef NX_MDNS_DISABLE_CLIENT
200     /* Check remote buffer. It must be empty. */
201     check_empty_buffer(buffer + current_buffer_size, current_buffer_size, NX_TRUE);
202 
203     if(mdns_0.nx_mdns_peer_rr_count != 0)
204         error_counter++;
205 
206     /* Use remote buffer. */
207     nx_mdns_service_continuous_query(&mdns_0, NX_NULL, "_printer._tcp", NX_NULL);
208 
209     /* Check local buffer. It must be empty. */
210     check_empty_buffer(buffer, current_buffer_size, NX_TRUE);
211 
212     /* Check remote buffer. It must be empty. */
213     check_empty_buffer(buffer + current_buffer_size, current_buffer_size, NX_FALSE);
214 
215     /* Check mdns information. */
216     if(mdns_0.nx_mdns_peer_rr_count != 1)
217         error_counter++;
218 
219     /* Clear the peer cache. */
220     nx_mdns_peer_cache_clear(&mdns_0);
221 
222     /* Check local buffer. It must be empty. */
223     check_empty_buffer(buffer, current_buffer_size, NX_TRUE);
224 
225     /* Check remote buffer. It must be empty. */
226     check_empty_buffer(buffer + current_buffer_size, current_buffer_size, NX_TRUE);
227 
228     /* Check mdns information. */
229     if(mdns_0.nx_mdns_peer_rr_count != 0)
230         error_counter++;
231 #endif /* NX_MDNS_DISABLE_CLIENT  */
232 
233     /* Determine if the test was successful.  */
234     if(error_counter)
235     {
236         printf("ERROR!\n");
237         test_control_return(1);
238     }
239     else
240     {
241         printf("SUCCESS!\n");
242         test_control_return(0);
243     }
244 }
245 
check_empty_buffer(UCHAR * buffer_ptr,ULONG buffer_size,UCHAR expect_empty)246 static void    check_empty_buffer(UCHAR *buffer_ptr, ULONG buffer_size, UCHAR expect_empty)
247 {
248 
249 ULONG     *tail, *head;
250 ULONG     expected_head, expected_tail;
251 
252     tx_mutex_get(&mdns_0.nx_mdns_mutex, TX_WAIT_FOREVER);
253 
254     head = (ULONG*)buffer_ptr;
255     tail = (ULONG*)buffer_ptr + (buffer_size >> 2) - 1;
256 
257     /* Get expected head and tail. */
258     if(buffer_ptr == buffer)
259     {
260         expected_head = buffer_org_head;
261         expected_tail = buffer_org_tail;
262     }
263     else
264     {
265         expected_head = (ULONG)(head + 1);
266         expected_tail = (ULONG)tail;
267     }
268 
269     /* Check head. */
270     if((*head == expected_head) && (expect_empty == NX_FALSE))
271         error_counter++;
272     else if((*head != expected_head) && (expect_empty == NX_TRUE))
273         error_counter++;
274 
275     /* Check tail. */
276     if((*tail == expected_tail) && (expect_empty == NX_FALSE))
277         error_counter++;
278     else if((*tail != expected_tail) && (expect_empty == NX_TRUE))
279         error_counter++;
280 
281     tx_mutex_put(&mdns_0.nx_mdns_mutex);
282 }
283 
empty_buffer_init()284 static void    empty_buffer_init()
285 {
286 ULONG     *tail, *head;
287 
288     head = (ULONG*)buffer;
289     buffer_org_head = *head;
290 
291     tail = (ULONG*)buffer + (current_buffer_size >> 2) - 1;
292     buffer_org_tail = *tail;
293 }
294 #else
295 #ifdef CTEST
test_application_define(void * first_unused_memory)296 VOID test_application_define(void *first_unused_memory)
297 #else
298 void           netx_mdns_two_buffer_test(void *first_unused_memory)
299 #endif
300 {
301     printf("NetX Test:   MDNS Two Buffer Test......................................N/A\n");
302     test_control_return(3);
303 }
304 #endif
305