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
65
70
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
124
125
126
127
128
129
130
131
132
137
138
139
140
141
142
143
148
149
150
151
152
153
154
155
160
161
162
163
164
165
166
167
172
173
174
175
176
177
178
183
184
185
186
187
188
189
190
196
197
198
199
200
201
202
207
208
209
210
211
212
217
218
219
220
221
222
227
228
229
230
231
232
237
238
239
240
241
242
247
248
249
250
251
252
253
258
259
260
261
262
263
264
265
266
271
272
273
274
275
276
280
281
282
283
284
285
286
290
291
292
293
294
295
300
301
302
303
304
305
306
307
313
318
319
320
321
322
323
324
325
326
327
332
333
334
335
336
337
345
346
347
348
349
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
375
376
377
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
402
407
408
409
410
411
412
419
424
425
/* ... */
#include "app_netxduo.h"
#include "main.h"
#include "nxd_dhcp_client.h"
#include "nx_web_http_server.h"
#include "app_filex.h"
#include "ux_api.h"
#include "ux_network_driver.h"
7 includes
TX_THREAD Main_thread;
TX_SEMAPHORE Semaphore;
NX_PACKET_POOL EthPool;
NX_PACKET_POOL WebServerPool;
NX_IP EthIP;
NX_DHCP DHCPClient;
NX_WEB_HTTP_SERVER HTTPServer;
ULONG IPAddress;
ULONG NetMask;
FX_MEDIA SDMedia;
uint32_t media_memory[512];
Private typedef
#define TOGGLE_GREEN_LED "led=ToggleGreen"
Private define
Private macro
static uint8_t nx_server_pool[SERVER_POOL_SIZE];
Private variables
static void nx_server_thread_entry(ULONG thread_input);
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr);
static UINT webserver_request_notify_callback(NX_WEB_HTTP_SERVER *server_ptr,
UINT request_type, CHAR *resource,
NX_PACKET *packet_ptr);
/* ... */
UINT MX_NetXDuo_Init(VOID *memory_ptr)
{
UINT ret = NX_SUCCESS;
TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;
UCHAR *web_app_pointer;
nx_system_initialize();
ret = tx_byte_allocate(byte_pool, (VOID **) &web_app_pointer,
WEB_STACK_SIZE, TX_NO_WAIT);
if (ret != TX_SUCCESS)
{
printf("Web stack allocation failed: 0x%02x\n", ret);
Error_Handler();
}if (ret != TX_SUCCESS) { ... }
ret = tx_thread_create(&Main_thread, "Main_thread", nx_server_thread_entry,
ENTRY_INPUT, web_app_pointer, WEB_STACK_SIZE,
DEFAULT_PRIORITY, THREAD_PREEMPT_THRESHOLD,
TX_NO_TIME_SLICE, TX_AUTO_START);
if (ret != TX_SUCCESS)
{
printf("Main Thread creation failed: 0x%02x\n", ret);
Error_Handler();
}if (ret != TX_SUCCESS) { ... }
ret = tx_byte_allocate(byte_pool, (VOID **) &web_app_pointer,
NX_PACKET_POOL_SIZE, TX_NO_WAIT);
if (ret != TX_SUCCESS)
{
printf("Packet pool allocation failed: 0x%02x\n", ret);
Error_Handler();
}if (ret != TX_SUCCESS) { ... }
ret = nx_packet_pool_create(&EthPool, "NetX Main Packet Pool",
PACKET_PAYLOAD_SIZE, web_app_pointer,
NX_PACKET_POOL_SIZE);
if (ret != NX_SUCCESS)
{
printf("Packed creation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_packet_pool_create(&WebServerPool, "HTTP Server Packet Pool",
SERVER_PACKET_SIZE, nx_server_pool,
SERVER_POOL_SIZE);
if (ret != NX_SUCCESS)
{
printf("Server pool creation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = tx_byte_allocate(byte_pool, (VOID **) &web_app_pointer,
IP_MEMORY_SIZE, TX_NO_WAIT);
if (ret != NX_SUCCESS)
{
printf("IP instance Allocation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_ip_create(&EthIP, "NetX IP Instance 0", NULL_IP_ADDRESS,
NULL_IP_ADDRESS, &EthPool, _ux_network_driver_entry,
web_app_pointer, IP_MEMORY_SIZE, DEFAULT_PRIORITY);
if (ret != NX_SUCCESS)
{
printf("IP Instance creation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = tx_byte_allocate(byte_pool, (VOID **) &web_app_pointer,
ARP_CACHE_SIZE, TX_NO_WAIT);
if (ret != NX_SUCCESS)
{
printf("IP instance Allocation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_arp_enable(&EthIP, (void *) web_app_pointer, ARP_CACHE_SIZE);
if (ret != NX_SUCCESS)
{
printf("ARP Enable for IP error : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_icmp_enable(&EthIP);
if (ret != NX_SUCCESS)
{
printf("ICMP enable for IP error : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_udp_enable(&EthIP);
if (ret != NX_SUCCESS)
{
printf("UDP enable for IP error : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_tcp_enable(&EthIP);
if (ret != NX_SUCCESS)
{
printf("TCP enable for IP error : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = tx_byte_allocate(byte_pool, (VOID **) &web_app_pointer,
SERVER_STACK, TX_NO_WAIT);
if (ret != NX_SUCCESS)
{
printf("Server stack Allocation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_web_http_server_create(&HTTPServer, "WEB HTTP Server", &EthIP,
CONNECTION_PORT, &SDMedia, web_app_pointer,
SERVER_STACK, &WebServerPool, NX_NULL,
webserver_request_notify_callback);
if (ret != NX_SUCCESS)
{
printf("HTTP WEB Server creation failed: 0x%02x\n", ret);
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = nx_dhcp_create(&DHCPClient, &EthIP, "dhcp_client");
if (ret != NX_SUCCESS)
{
printf("DHCP Instance creation failed : 0x%02x\n", ret);
}if (ret != NX_SUCCESS) { ... }
ret = nx_ip_address_change_notify(&EthIP, ip_address_change_notify_callback,
NULL);
if (ret != NX_SUCCESS)
{
Error_Handler();
}if (ret != NX_SUCCESS) { ... }
ret = tx_semaphore_create(&Semaphore, "App Semaphore", 0);
if (ret != TX_SUCCESS)
{
printf("Semaphore creation failed : 0x%02x\n", ret);
Error_Handler();
}if (ret != TX_SUCCESS) { ... }
return ret;
}MX_NetXDuo_Init (VOID *memory_ptr) { ... }
/* ... */
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr)
{
tx_semaphore_put(&Semaphore);
}ip_address_change_notify_callback (NX_IP *ip_instance, VOID *ptr) { ... }
UINT webserver_request_notify_callback(NX_WEB_HTTP_SERVER *server_ptr,
UINT request_type, CHAR *resource,
NX_PACKET *packet_ptr)
{
CHAR request_data[256] = {0};
UINT request_size = 0;
UINT status;
/* ... */
status = nx_web_http_server_content_get(server_ptr, packet_ptr, 0,
request_data, 100, &request_size);
if (status == NX_SUCCESS)
{
if (strncmp((char const *)request_data, TOGGLE_GREEN_LED, sizeof(TOGGLE_GREEN_LED)) == 0)
{
BSP_LED_Toggle(LED_GREEN);
}if (strncmp((char const *)request_data, TOGGLE_GREEN_LED, sizeof(TOGGLE_GREEN_LED)) == 0) { ... }
}if (status == NX_SUCCESS) { ... }
return NX_SUCCESS;
}webserver_request_notify_callback (NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr) { ... }
/* ... */
void nx_server_thread_entry(ULONG thread_input)
{
tx_thread_sleep(MS_TO_TICK(1000));
UINT status;
NX_PARAMETER_NOT_USED(thread_input);
printf("Starting DHCP client...\n");
status = nx_dhcp_start(&DHCPClient);
if (status != NX_SUCCESS)
{
printf("DHCP Instance Starting error : 0x%02x\n", status);
}if (status != NX_SUCCESS) { ... }
if (tx_semaphore_get(&Semaphore, TX_WAIT_FOREVER) != TX_SUCCESS)
{
Error_Handler();
}if (tx_semaphore_get(&Semaphore, TX_WAIT_FOREVER) != TX_SUCCESS) { ... }
nx_ip_address_get(&EthIP, &IPAddress, &NetMask);
PRINT_ADDRESS(IPAddress);
PRINT_ADDRESS(NetMask);
status = fx_media_open(&SDMedia, "STM32_SDIO_DISK", fx_stm32_sd_driver,
SD_DRIVER_INFO_POINTER, (VOID *) media_memory, sizeof(media_memory));
if (status != FX_SUCCESS)
{
printf("FX media opening failed : 0x%02x\n", status);
Error_Handler();
}if (status != FX_SUCCESS) { ... }
else
{
printf("Fx media successfully opened.\n");
}else { ... }
status = nx_web_http_server_start(&HTTPServer);
if (status != NX_SUCCESS)
{
printf("HTTP WEB Server Starting Failed, error: 0x%02x\n", status);
Error_Handler();
}if (status != NX_SUCCESS) { ... }
else
{
printf("HTTP WEB Server successfully started.\n");
}else { ... }
}nx_server_thread_entry (ULONG thread_input) { ... }
Private function prototypes