forked from eclipse-threadx/getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetworking.c
277 lines (228 loc) · 7.5 KB
/
networking.c
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
/* Copyright (c) Microsoft Corporation.
Licensed under the MIT License. */
#include "networking.h"
#include <stdint.h>
#include "nx_api.h"
#include "nx_secure_tls_api.h"
#include "nxd_dhcp_client.h"
#include "nxd_dns.h"
#include "sntp_client.h"
#define NETX_IP_STACK_SIZE 2048
#define NETX_PACKET_COUNT 60
#define NETX_PACKET_SIZE 1536
#define NETX_POOL_SIZE ((NETX_PACKET_SIZE + sizeof(NX_PACKET)) * NETX_PACKET_COUNT)
#define NETX_ARP_CACHE_SIZE 512
#define NETX_DNS_COUNT 6
#define NETX_IPV4_ADDRESS IP_ADDRESS(0, 0, 0, 0)
#define NETX_IPV4_MASK IP_ADDRESS(255, 255, 255, 0)
#define DHCP_WAIT_TIME_TICKS (30 * TX_TIMER_TICKS_PER_SECOND)
static UCHAR netx_ip_stack[NETX_IP_STACK_SIZE];
static UCHAR netx_ip_pool[NETX_POOL_SIZE];
static UCHAR netx_arp_cache_area[NETX_ARP_CACHE_SIZE];
static NX_DHCP nx_dhcp_client;
NX_IP nx_ip;
NX_PACKET_POOL nx_pool;
NX_DNS nx_dns_client;
// Print IPv4 address
static void print_address(CHAR* preable, ULONG address)
{
printf("\t%s: %d.%d.%d.%d\r\n",
preable,
(uint8_t)(address >> 24),
(uint8_t)(address >> 16 & 0xFF),
(uint8_t)(address >> 8 & 0xFF),
(uint8_t)(address & 0xFF));
}
static void print_mac()
{
const ULONG lsw = nx_ip.nx_ip_gateway_interface->nx_interface_physical_address_lsw;
const ULONG msw = nx_ip.nx_ip_gateway_interface->nx_interface_physical_address_msw;
printf("\tMAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
(uint8_t)(msw >> 8 & 0xFF),
(uint8_t)(msw & 0xFF),
(uint8_t)(lsw >> 24 & 0xFF),
(uint8_t)(lsw >> 16 & 0xFF),
(uint8_t)(lsw >> 8 & 0xFF),
(uint8_t)(lsw & 0xFF));
}
static UINT dhcp_connect()
{
UINT status;
ULONG actual_status;
ULONG ip_address;
ULONG network_mask;
ULONG gateway_address;
printf("\r\nInitializing DHCP\r\n");
if ((status = nx_dhcp_force_renew(&nx_dhcp_client)))
{
printf("ERROR: nx_dhcp_force_renew (0x%08x\r\n", status);
return status;
}
// Wait until address is solved
if ((status = nx_ip_status_check(&nx_ip, NX_IP_ADDRESS_RESOLVED, &actual_status, DHCP_WAIT_TIME_TICKS)))
{
// DHCP Failed... no IP address!
printf("ERROR: Can't resolve DHCP address (0x%08x\r\n", status);
return status;
}
// Get IP address and gateway address
nx_ip_address_get(&nx_ip, &ip_address, &network_mask);
nx_ip_gateway_address_get(&nx_ip, &gateway_address);
// Output MAC, IP address and gateway address
print_mac();
print_address("IP address", ip_address);
print_address("Mask", network_mask);
print_address("Gateway", gateway_address);
printf("SUCCESS: DHCP initialized\r\n");
return NX_SUCCESS;
}
static UINT dns_connect()
{
UINT status;
ULONG dns_server_address[NETX_DNS_COUNT] = {0};
UINT dns_server_address_size = sizeof(UINT) * NETX_DNS_COUNT;
printf("\r\nInitializing DNS client\r\n");
// Retrieve DNS server address
if ((status = nx_dhcp_interface_user_option_retrieve(
&nx_dhcp_client, 0, NX_DHCP_OPTION_DNS_SVR, (UCHAR*)dns_server_address, &dns_server_address_size)))
{
printf("ERROR: nx_dhcp_interface_user_option_retrieve (0x%08x)\r\n", status);
return status;
}
if ((status = nx_dns_server_remove_all(&nx_dns_client)))
{
printf("ERROR: nx_dns_server_remove_all (0x%08x)\r\n", status);
return status;
}
for (int i = 0; i < dns_server_address_size / sizeof(UINT); ++i)
{
print_address("DNS address", dns_server_address[i]);
// Add an IPv4 server address to the Client list
if ((status = nx_dns_server_add(&nx_dns_client, dns_server_address[i])))
{
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
return status;
}
}
printf("SUCCESS: DNS client initialized\r\n");
return NX_SUCCESS;
}
UINT network_init(VOID (*ip_link_driver)(struct NX_IP_DRIVER_STRUCT*))
{
UINT status;
// Initialize the NetX system.
nx_system_initialize();
// Create a packet pool.
if ((status = nx_packet_pool_create(&nx_pool, "NetX Packet Pool", NETX_PACKET_SIZE, netx_ip_pool, NETX_POOL_SIZE)))
{
printf("ERROR: nx_packet_pool_create (0x%08x)\r\n", status);
}
// Create an IP instance
else if ((status = nx_ip_create(&nx_ip,
"NetX IP Instance 0",
NETX_IPV4_ADDRESS,
NETX_IPV4_MASK,
&nx_pool,
ip_link_driver,
netx_ip_stack,
NETX_IP_STACK_SIZE,
1)))
{
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_ip_create (0x%08x)\r\n", status);
}
// Enable ARP and supply ARP cache memory
else if ((status = nx_arp_enable(&nx_ip, (VOID*)netx_arp_cache_area, NETX_ARP_CACHE_SIZE)))
{
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_arp_enable (0x%08x)\r\n", status);
}
// Enable TCP traffic
else if ((status = nx_tcp_enable(&nx_ip)))
{
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_tcp_enable (0x%08x)\r\n", status);
return status;
}
// Enable UDP traffic
else if ((status = nx_udp_enable(&nx_ip)))
{
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_udp_enable (0x%08x)\r\n", status);
}
// Enable ICMP traffic
else if ((status = nx_icmp_enable(&nx_ip)))
{
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_icmp_enable (0x%08x)\r\n", status);
}
// Create the DHCP instance.
else if ((status = nx_dhcp_create(&nx_dhcp_client, &nx_ip, "azure_iot")))
{
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_dhcp_create (0x%08x)\r\n", status);
}
// Start the DHCP Client
if ((status = nx_dhcp_start(&nx_dhcp_client)))
{
nx_dhcp_delete(&nx_dhcp_client);
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_dhcp_start (0x%08x)\r\n", status);
}
// Create DNS
else if ((status = nx_dns_create(&nx_dns_client, &nx_ip, (UCHAR*)"DNS Client")))
{
nx_dhcp_delete(&nx_dhcp_client);
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_dns_create (0x%08x)\r\n", status);
}
// Use the packet pool here
#ifdef NX_DNS_CLIENT_USER_CREATE_PACKET_POOL
else if ((status = nx_dns_packet_pool_set(&nx_dns_client, nx_ip.nx_ip_default_packet_pool)))
{
nx_dns_delete(&nx_dns_client);
nx_dhcp_delete(&nx_dhcp_client);
nx_ip_delete(&nx_ip);
nx_packet_pool_delete(&nx_pool);
printf("ERROR: nx_dns_packet_pool_set (%0x08)\r\n", status);
}
#endif
// Initialize the SNTP client
else if ((status = sntp_init()))
{
printf("ERROR: Failed to init the SNTP client (0x%08x)\r\n", status);
}
// Initialize TLS
else
{
nx_secure_tls_initialize();
}
return status;
}
UINT network_connect()
{
UINT status;
// Fetch IP details
if ((status = dhcp_connect()))
{
printf("ERROR: dhcp_connect\r\n");
}
// Create DNS
else if ((status = dns_connect()))
{
printf("ERROR: dns_connect\r\n");
}
// Wait for an SNTP sync
else if ((status = sntp_sync()))
{
printf("ERROR: Failed to sync SNTP time (0x%08x)\r\n", status);
}
return status;
}