1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 Eclipse ThreadX contributors */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the MIT License which is available at */
/* https://opensource.org/licenses/MIT. */
/* */
/* SPDX-License-Identifier: MIT */
/***************************************************************************/
#include "tx_api.h"
#include "nx_api.h"
#include "nxd_dhcp_client.h"
#include "nxd_dhcp_server.h"
extern void test_control_return(UINT status);
#if !defined(NX_DISABLE_IPV4)
#define DEMO_STACK_SIZE 4096
#define NX_PACKET_SIZE 1536
#define NX_PACKET_POOL_SIZE NX_PACKET_SIZE * 8
#define NX_DHCP_SERVER_IP_ADDRESS_0 IP_ADDRESS(10,0,0,1)
#define START_IP_ADDRESS_LIST_0 IP_ADDRESS(10,0,0,10)
#define END_IP_ADDRESS_LIST_0 IP_ADDRESS(10,0,0,19)
#define NX_DHCP_SUBNET_MASK_0 IP_ADDRESS(255,255,255,0)
#define NX_DHCP_DEFAULT_GATEWAY_0 IP_ADDRESS(10,0,0,1)
#define NX_DHCP_DNS_SERVER_0 IP_ADDRESS(10,0,0,1)
/* If defined, the host requests a (previous) client IP address. */
/*
#define REQUEST_CLIENT_IP
*/
/* If defined the client requests to jump to the boot state and skip the DISCOVER message.
If REQUEST_CLIENT_IP is not defined, this has no effect. */
/*
#define SKIP_DISCOVER_MESSAGE
*/
/* Define the ThreadX and NetX object control blocks... */
static TX_THREAD client_thread;
static NX_PACKET_POOL client_pool;
static NX_IP client_ip;
static NX_DHCP dhcp_client;
static TX_THREAD server_thread;
static NX_PACKET_POOL server_pool;
static NX_IP server_ip;
static NX_DHCP_SERVER dhcp_server;
/* Define the counters used in the demo application... */
static ULONG state_changes;
static ULONG error_counter;
static CHAR *pointer;
static UCHAR message[50] = "My Ping Request!" ;
/* Define thread prototypes. */
static void server_thread_entry(ULONG thread_input);
static void client_thread_entry(ULONG thread_input);
static void dhcp_state_change(NX_DHCP *dhcp_ptr, UCHAR new_state);
/******** Optionally substitute your Ethernet driver here. ***********/
extern void _nx_ram_network_driver_1024(struct NX_IP_DRIVER_STRUCT *driver_req);
/* Define what the initial system looks like. */
#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void netx_dhcp_enable_test_application_define(void *first_unused_memory)
#endif
{
UINT status;
/* Setup the working pointer. */
pointer = (CHAR *) first_unused_memory;
/* Create the client thread. */
tx_thread_create(&client_thread, "thread client", client_thread_entry, 0,
pointer, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Create the server thread. */
tx_thread_create(&server_thread, "thread server", server_thread_entry, 0,
pointer, DEMO_STACK_SIZE,
3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Initialize the NetX system. */
nx_system_initialize();
/* Create the client packet pool. */
status = nx_packet_pool_create(&client_pool, "NetX Main Packet Pool", 1024, pointer, NX_PACKET_POOL_SIZE);
pointer = pointer + NX_PACKET_POOL_SIZE;
/* Check for pool creation error. */
if (status)
error_counter++;
/* Create the server packet pool. */
status = nx_packet_pool_create(&server_pool, "NetX Main Packet Pool", 1024, pointer, NX_PACKET_POOL_SIZE);
pointer = pointer + NX_PACKET_POOL_SIZE;
/* Check for pool creation error. */
if (status)
error_counter++;
/* Create an IP instance for the DHCP Client. */
status = nx_ip_create(&client_ip, "DHCP Client", IP_ADDRESS(0, 0, 0, 0), 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024, pointer, 2048, 1);
pointer = pointer + 2048;
/* Check for IP create errors. */
if (status)
error_counter++;
/* Create an IP instance for the DHCP Server. */
status = nx_ip_create(&server_ip, "DHCP Server", NX_DHCP_SERVER_IP_ADDRESS_0, 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024, pointer, 2048, 1);
pointer = pointer + 2048;
/* Check for IP create errors. */
if (status)
error_counter++;
/* Enable ARP and supply ARP cache memory for DHCP Client IP. */
status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
pointer = pointer + 1024;
/* Check for ARP enable errors. */
if (status)
error_counter++;
/* Enable ARP and supply ARP cache memory for DHCP Server IP. */
status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
pointer = pointer + 1024;
/* Check for ARP enable errors. */
if (status)
error_counter++;
/* Enable UDP traffic. */
status = nx_udp_enable(&client_ip);
/* Check for UDP enable errors. */
if (status)
error_counter++;
/* Enable UDP traffic. */
status = nx_udp_enable(&server_ip);
/* Check for UDP enable errors. */
if (status)
error_counter++;
/* Enable ICMP. */
status = nx_icmp_enable(&client_ip);
/* Check for errors. */
if (status)
error_counter++;
/* Enable ICMP. */
status = nx_icmp_enable(&server_ip);
/* Check for errors. */
if (status)
error_counter++;
return;
}
/* Define the test threads. */
void server_thread_entry(ULONG thread_input)
{
UINT status;
UINT iface_index;
UINT addresses_added;
printf("NetX Test: DHCP Enable Test...........................................");
/* Create the DHCP Server. */
status = nx_dhcp_server_create(&dhcp_server, &server_ip, pointer, DEMO_STACK_SIZE,
"DHCP Server", &server_pool);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for errors creating the DHCP Server. */
if (status)
error_counter++;
/* Load the assignable DHCP IP addresses for the first interface. */
iface_index = 0;
status = nx_dhcp_create_server_ip_address_list(&dhcp_server, iface_index, START_IP_ADDRESS_LIST_0,
END_IP_ADDRESS_LIST_0, &addresses_added);
/* Check for errors creating the list. */
if (status)
{
error_counter++;
}
/* Verify all the addresses were added to the list. */
if (addresses_added != 10)
{
error_counter++;
}
status = nx_dhcp_set_interface_network_parameters(&dhcp_server, iface_index, NX_DHCP_SUBNET_MASK_0,
NX_DHCP_DEFAULT_GATEWAY_0, NX_DHCP_DNS_SERVER_0);
/* Check for errors setting network parameters. */
if (status)
{
error_counter++;
}
/* Start DHCP Server task. */
status = nx_dhcp_server_start(&dhcp_server);
/* Check for errors starting up the DHCP server. */
if (status)
{
error_counter++;
}
tx_thread_sleep(20 * NX_IP_PERIODIC_RATE);
if(error_counter)
{
printf("ERROR!\n");
test_control_return(1);
}
else
{
printf("SUCCESS!\n");
test_control_return(0);
}
return;
}
UINT length;
#if defined(__PRODUCT_NETXDUO__) && defined(NX_ENABLE_IP_PACKET_FILTER)
static UINT packet_filter_extended(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT direction)
{
if ((direction == NX_IP_PACKET_OUT) &&
((packet_ptr -> nx_packet_ip_header == NX_NULL) ||
(packet_ptr -> nx_packet_ip_header_length == 0)))
{
error_counter++;
}
return(NX_SUCCESS);
}
#endif
/* Define the test threads. */
void client_thread_entry(ULONG thread_input)
{
UINT status;
NX_PACKET *my_packet;
UCHAR buffer[4];
UINT buffer_size = 4;
ULONG *long_ptr;
#ifdef REQUEST_CLIENT_IP
ULONG requested_ip;
UINT skip_discover_message = NX_FALSE;
#endif
/* Create the DHCP instance. */
status = nx_dhcp_create(&dhcp_client, &client_ip, "dhcp_client");
if (status)
error_counter++;
#ifdef NX_DHCP_CLIENT_USER_CREATE_PACKET_POOL
status = nx_dhcp_packet_pool_set(&dhcp_client, &client_pool);
if (status)
error_counter++;
#endif /* NX_DHCP_CLIENT_USER_CREATE_PACKET_POOL */
#if defined(__PRODUCT_NETXDUO__) && defined(NX_ENABLE_IP_PACKET_FILTER)
client_ip.nx_ip_packet_filter_extended = packet_filter_extended;
#endif
/* Set the client IP if the host is configured to do so. */
#ifdef REQUEST_CLIENT_IP
requested_ip = (ULONG)CLIENT_IP_ADDRESS;
/* Request a specific IP address using the DHCP client address option. */
status = nx_dhcp_request_client_ip(&dhcp_client, requested_ip, skip_discover_message);
if (status)
error_counter++;
#endif
/* Register state change variable. */
status = nx_dhcp_state_change_notify(&dhcp_client, dhcp_state_change);
if (status)
error_counter++;
/* Start the DHCP Client. */
status = nx_dhcp_start(&dhcp_client);
if (status)
error_counter++;
/* Check for address resolution. */
status = nx_ip_status_check(&client_ip, NX_IP_ADDRESS_RESOLVED, (ULONG *) &status, 20 * NX_IP_PERIODIC_RATE);
/* Check status. */
if (status)
error_counter++;
/* Get the DNS Server address. */
status = nx_dhcp_user_option_retrieve(&dhcp_client, NX_DHCP_OPTION_DNS_SVR, buffer, &buffer_size);
/* Check status. */
if (status == NX_SUCCESS)
{
/* Set the pointer. */
long_ptr = (ULONG *)(buffer);
/* Check the DNS address. */
if (*long_ptr != NX_DHCP_DNS_SERVER_0)
error_counter++;
}
else
{
error_counter++;
}
/* Get the lease time value. */
status = nx_dhcp_user_option_retrieve(&dhcp_client, NX_DHCP_OPTION_DHCP_LEASE, buffer, &buffer_size);
/* Check status. */
if (status == NX_SUCCESS)
{
/* Set the pointer. */
long_ptr = (ULONG *)(buffer);
/* Check the lease time value. */
if (*long_ptr != NX_DHCP_DEFAULT_LEASE_TIME)
error_counter++;
}
else
{
error_counter++;
}
length = sizeof(message);
/* Send pings to another host on the network... */
status = nx_icmp_ping(&client_ip, NX_DHCP_SERVER_IP_ADDRESS_0, (CHAR *)message, length, &my_packet, NX_IP_PERIODIC_RATE);
if (status)
error_counter++;
else
nx_packet_release(my_packet);
/* Now stop the DHCP Client 0. */
status = nx_dhcp_interface_disable(&dhcp_client, 0);
if (status)
error_counter++;
for (UINT i = 0; i < NX_DHCP_CLIENT_MAX_RECORDS; i++)
{
dhcp_client.nx_dhcp_interface_record[i].nx_dhcp_record_valid = NX_TRUE;
dhcp_client.nx_dhcp_interface_record[i].nx_dhcp_interface_index = 1;
}
nx_dhcp_interface_enable(&dhcp_client, 0);
/* Stopping the DHCP client. */
nx_dhcp_stop(&dhcp_client);
/* All done. Return resources to NetX and ThreadX. */
nx_dhcp_delete(&dhcp_client);
return;
}
void dhcp_state_change(NX_DHCP *dhcp_ptr, UCHAR new_state)
{
/* Increment state changes counter. */
state_changes++;
return;
}
#else
#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void netx_dhcp_enable_test_application_define(void *first_unused_memory)
#endif
{
/* Print out test information banner. */
printf("NetX Test: NetX DHCP Enable Test......................................N/A\n");
test_control_return(3);
}
#endif
|