1 | /*
|
---|
2 | * Copyright (c) 2001,2002 Florian Schulze.
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * 1. Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * 3. Neither the name of the authors nor the names of the contributors
|
---|
15 | * may be used to endorse or promote products derived from this software
|
---|
16 | * without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
---|
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
---|
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
28 | * SUCH DAMAGE.
|
---|
29 | *
|
---|
30 | * test.c - This file is part of lwIP test
|
---|
31 | *
|
---|
32 | */
|
---|
33 |
|
---|
34 | /* C runtime includes */
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdarg.h>
|
---|
37 | #include <time.h>
|
---|
38 | #include <string.h>
|
---|
39 |
|
---|
40 | /* lwIP core includes */
|
---|
41 | #include "lwip/opt.h"
|
---|
42 |
|
---|
43 | #include "lwip/sys.h"
|
---|
44 | #include "lwip/timeouts.h"
|
---|
45 | #include "lwip/debug.h"
|
---|
46 | #include "lwip/stats.h"
|
---|
47 | #include "lwip/init.h"
|
---|
48 | #include "lwip/tcpip.h"
|
---|
49 | #include "lwip/netif.h"
|
---|
50 | #include "lwip/api.h"
|
---|
51 |
|
---|
52 | #include "lwip/tcp.h"
|
---|
53 | #include "lwip/udp.h"
|
---|
54 | #include "lwip/dns.h"
|
---|
55 | #include "lwip/dhcp.h"
|
---|
56 | #include "lwip/autoip.h"
|
---|
57 |
|
---|
58 | /* lwIP netif includes */
|
---|
59 | #include "lwip/etharp.h"
|
---|
60 | #include "netif/ethernet.h"
|
---|
61 |
|
---|
62 | /* applications includes */
|
---|
63 | #include "lwip/apps/netbiosns.h"
|
---|
64 | #include "lwip/apps/httpd.h"
|
---|
65 | #include "apps/httpserver/httpserver-netconn.h"
|
---|
66 | #include "apps/netio/netio.h"
|
---|
67 | #include "apps/ping/ping.h"
|
---|
68 | #include "apps/rtp/rtp.h"
|
---|
69 | #include "apps/chargen/chargen.h"
|
---|
70 | #include "apps/shell/shell.h"
|
---|
71 | #include "apps/tcpecho/tcpecho.h"
|
---|
72 | #include "apps/udpecho/udpecho.h"
|
---|
73 | #include "apps/tcpecho_raw/tcpecho_raw.h"
|
---|
74 | #include "apps/socket_examples/socket_examples.h"
|
---|
75 |
|
---|
76 | #include "examples/lwiperf/lwiperf_example.h"
|
---|
77 | #include "examples/mdns/mdns_example.h"
|
---|
78 | #include "examples/snmp/snmp_example.h"
|
---|
79 | #include "examples/tftp/tftp_example.h"
|
---|
80 | #include "examples/sntp/sntp_example.h"
|
---|
81 | #include "examples/mqtt/mqtt_example.h"
|
---|
82 |
|
---|
83 | #include "examples/httpd/cgi_example/cgi_example.h"
|
---|
84 | #include "examples/httpd/fs_example/fs_example.h"
|
---|
85 | #include "examples/httpd/ssi_example/ssi_example.h"
|
---|
86 |
|
---|
87 | #include "default_netif.h"
|
---|
88 |
|
---|
89 | #if NO_SYS
|
---|
90 | /* ... then we need information about the timer intervals: */
|
---|
91 | #include "lwip/ip4_frag.h"
|
---|
92 | #include "lwip/igmp.h"
|
---|
93 | #endif /* NO_SYS */
|
---|
94 |
|
---|
95 | #include "netif/ppp/ppp_opts.h"
|
---|
96 | #if PPP_SUPPORT
|
---|
97 | /* PPP includes */
|
---|
98 | #include "lwip/sio.h"
|
---|
99 | #include "netif/ppp/pppapi.h"
|
---|
100 | #include "netif/ppp/pppos.h"
|
---|
101 | #include "netif/ppp/pppoe.h"
|
---|
102 | #if !NO_SYS && !LWIP_PPP_API
|
---|
103 | #error With NO_SYS==0, LWIP_PPP_API==1 is required.
|
---|
104 | #endif
|
---|
105 | #endif /* PPP_SUPPORT */
|
---|
106 |
|
---|
107 | /* include the port-dependent configuration */
|
---|
108 | #include "lwipcfg.h"
|
---|
109 |
|
---|
110 | #ifndef LWIP_EXAMPLE_APP_ABORT
|
---|
111 | #define LWIP_EXAMPLE_APP_ABORT() 0
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | /** Define this to 1 to enable a port-specific ethernet interface as default interface. */
|
---|
115 | #ifndef USE_DEFAULT_ETH_NETIF
|
---|
116 | #define USE_DEFAULT_ETH_NETIF 1
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | /** Define this to 1 to enable a PPP interface. */
|
---|
120 | #ifndef USE_PPP
|
---|
121 | #define USE_PPP 0
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | /** Define this to 1 or 2 to support 1 or 2 SLIP interfaces. */
|
---|
125 | #ifndef USE_SLIPIF
|
---|
126 | #define USE_SLIPIF 0
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | /** Use an ethernet adapter? Default to enabled if port-specific ethernet netif or PPPoE are used. */
|
---|
130 | #ifndef USE_ETHERNET
|
---|
131 | #define USE_ETHERNET (USE_DEFAULT_ETH_NETIF || PPPOE_SUPPORT)
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | /** Use an ethernet adapter for TCP/IP? By default only if port-specific ethernet netif is used. */
|
---|
135 | #ifndef USE_ETHERNET_TCPIP
|
---|
136 | #define USE_ETHERNET_TCPIP (USE_DEFAULT_ETH_NETIF)
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | #if USE_SLIPIF
|
---|
140 | #include <netif/slipif.h>
|
---|
141 | #endif /* USE_SLIPIF */
|
---|
142 |
|
---|
143 | #ifndef USE_DHCP
|
---|
144 | #define USE_DHCP LWIP_DHCP
|
---|
145 | #endif
|
---|
146 | #ifndef USE_AUTOIP
|
---|
147 | #define USE_AUTOIP LWIP_AUTOIP
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | /* globales variables for netifs */
|
---|
151 | #if USE_ETHERNET
|
---|
152 | #if LWIP_DHCP
|
---|
153 | /* dhcp struct for the ethernet netif */
|
---|
154 | struct dhcp netif_dhcp;
|
---|
155 | #endif /* LWIP_DHCP */
|
---|
156 | #if LWIP_AUTOIP
|
---|
157 | /* autoip struct for the ethernet netif */
|
---|
158 | struct autoip netif_autoip;
|
---|
159 | #endif /* LWIP_AUTOIP */
|
---|
160 | #endif /* USE_ETHERNET */
|
---|
161 | #if USE_PPP
|
---|
162 | /* THE PPP PCB */
|
---|
163 | ppp_pcb *ppp;
|
---|
164 | /* THE PPP interface */
|
---|
165 | struct netif ppp_netif;
|
---|
166 | /* THE PPP descriptor */
|
---|
167 | u8_t sio_idx = 0;
|
---|
168 | sio_fd_t ppp_sio;
|
---|
169 | #endif /* USE_PPP */
|
---|
170 | #if USE_SLIPIF
|
---|
171 | struct netif slipif1;
|
---|
172 | #if USE_SLIPIF > 1
|
---|
173 | struct netif slipif2;
|
---|
174 | #endif /* USE_SLIPIF > 1 */
|
---|
175 | #endif /* USE_SLIPIF */
|
---|
176 |
|
---|
177 |
|
---|
178 | #if USE_PPP
|
---|
179 | static void
|
---|
180 | pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx)
|
---|
181 | {
|
---|
182 | struct netif *pppif = ppp_netif(pcb);
|
---|
183 | LWIP_UNUSED_ARG(ctx);
|
---|
184 |
|
---|
185 | switch(errCode) {
|
---|
186 | case PPPERR_NONE: { /* No error. */
|
---|
187 | printf("pppLinkStatusCallback: PPPERR_NONE\n");
|
---|
188 | #if LWIP_IPV4
|
---|
189 | printf(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif)));
|
---|
190 | printf(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif)));
|
---|
191 | printf(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif)));
|
---|
192 | #endif /* LWIP_IPV4 */
|
---|
193 | #if LWIP_DNS
|
---|
194 | printf(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0)));
|
---|
195 | printf(" dns2 = %s\n", ipaddr_ntoa(dns_getserver(1)));
|
---|
196 | #endif /* LWIP_DNS */
|
---|
197 | #if PPP_IPV6_SUPPORT
|
---|
198 | printf(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0)));
|
---|
199 | #endif /* PPP_IPV6_SUPPORT */
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | case PPPERR_PARAM: { /* Invalid parameter. */
|
---|
203 | printf("pppLinkStatusCallback: PPPERR_PARAM\n");
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | case PPPERR_OPEN: { /* Unable to open PPP session. */
|
---|
207 | printf("pppLinkStatusCallback: PPPERR_OPEN\n");
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */
|
---|
211 | printf("pppLinkStatusCallback: PPPERR_DEVICE\n");
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | case PPPERR_ALLOC: { /* Unable to allocate resources. */
|
---|
215 | printf("pppLinkStatusCallback: PPPERR_ALLOC\n");
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | case PPPERR_USER: { /* User interrupt. */
|
---|
219 | printf("pppLinkStatusCallback: PPPERR_USER\n");
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | case PPPERR_CONNECT: { /* Connection lost. */
|
---|
223 | printf("pppLinkStatusCallback: PPPERR_CONNECT\n");
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */
|
---|
227 | printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n");
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | case PPPERR_PROTOCOL: { /* Failed to meet protocol. */
|
---|
231 | printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n");
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | case PPPERR_PEERDEAD: { /* Connection timeout */
|
---|
235 | printf("pppLinkStatusCallback: PPPERR_PEERDEAD\n");
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | case PPPERR_IDLETIMEOUT: { /* Idle Timeout */
|
---|
239 | printf("pppLinkStatusCallback: PPPERR_IDLETIMEOUT\n");
|
---|
240 | break;
|
---|
241 | }
|
---|
242 | case PPPERR_CONNECTTIME: { /* Max connect time reached */
|
---|
243 | printf("pppLinkStatusCallback: PPPERR_CONNECTTIME\n");
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | case PPPERR_LOOPBACK: { /* Loopback detected */
|
---|
247 | printf("pppLinkStatusCallback: PPPERR_LOOPBACK\n");
|
---|
248 | break;
|
---|
249 | }
|
---|
250 | default: {
|
---|
251 | printf("pppLinkStatusCallback: unknown errCode %d\n", errCode);
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | #if PPPOS_SUPPORT
|
---|
258 | static u32_t
|
---|
259 | ppp_output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx)
|
---|
260 | {
|
---|
261 | LWIP_UNUSED_ARG(pcb);
|
---|
262 | LWIP_UNUSED_ARG(ctx);
|
---|
263 | return sio_write(ppp_sio, data, len);
|
---|
264 | }
|
---|
265 | #endif /* PPPOS_SUPPORT */
|
---|
266 | #endif /* USE_PPP */
|
---|
267 |
|
---|
268 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
269 | static void
|
---|
270 | status_callback(struct netif *state_netif)
|
---|
271 | {
|
---|
272 | if (netif_is_up(state_netif)) {
|
---|
273 | #if LWIP_IPV4
|
---|
274 | printf("status_callback==UP, local interface IP is %s\n", ip4addr_ntoa(netif_ip4_addr(state_netif)));
|
---|
275 | #else
|
---|
276 | printf("status_callback==UP\n");
|
---|
277 | #endif
|
---|
278 | } else {
|
---|
279 | printf("status_callback==DOWN\n");
|
---|
280 | }
|
---|
281 | }
|
---|
282 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
283 |
|
---|
284 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
285 | static void
|
---|
286 | link_callback(struct netif *state_netif)
|
---|
287 | {
|
---|
288 | if (netif_is_link_up(state_netif)) {
|
---|
289 | printf("link_callback==UP\n");
|
---|
290 | } else {
|
---|
291 | printf("link_callback==DOWN\n");
|
---|
292 | }
|
---|
293 | }
|
---|
294 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
295 |
|
---|
296 | /* This function initializes all network interfaces */
|
---|
297 | static void
|
---|
298 | test_netif_init(void)
|
---|
299 | {
|
---|
300 | #if LWIP_IPV4 && USE_ETHERNET
|
---|
301 | ip4_addr_t ipaddr, netmask, gw;
|
---|
302 | #endif /* LWIP_IPV4 && USE_ETHERNET */
|
---|
303 | #if USE_SLIPIF
|
---|
304 | u8_t num_slip1 = 0;
|
---|
305 | #if LWIP_IPV4
|
---|
306 | ip4_addr_t ipaddr_slip1, netmask_slip1, gw_slip1;
|
---|
307 | #endif
|
---|
308 | #if USE_SLIPIF > 1
|
---|
309 | u8_t num_slip2 = 1;
|
---|
310 | #if LWIP_IPV4
|
---|
311 | ip4_addr_t ipaddr_slip2, netmask_slip2, gw_slip2;
|
---|
312 | #endif
|
---|
313 | #endif /* USE_SLIPIF > 1 */
|
---|
314 | #endif /* USE_SLIPIF */
|
---|
315 | #if USE_DHCP || USE_AUTOIP
|
---|
316 | err_t err;
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | #if USE_PPP
|
---|
320 | const char *username = NULL, *password = NULL;
|
---|
321 | #ifdef PPP_USERNAME
|
---|
322 | username = PPP_USERNAME;
|
---|
323 | #endif
|
---|
324 | #ifdef PPP_PASSWORD
|
---|
325 | password = PPP_PASSWORD;
|
---|
326 | #endif
|
---|
327 | printf("ppp_connect: COM%d\n", (int)sio_idx);
|
---|
328 | #if PPPOS_SUPPORT
|
---|
329 | ppp_sio = sio_open(sio_idx);
|
---|
330 | if (ppp_sio == NULL) {
|
---|
331 | printf("sio_open error\n");
|
---|
332 | } else {
|
---|
333 | ppp = pppos_create(&ppp_netif, ppp_output_cb, pppLinkStatusCallback, NULL);
|
---|
334 | if (ppp == NULL) {
|
---|
335 | printf("pppos_create error\n");
|
---|
336 | } else {
|
---|
337 | ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password);
|
---|
338 | ppp_connect(ppp, 0);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | #endif /* PPPOS_SUPPORT */
|
---|
342 | #endif /* USE_PPP */
|
---|
343 |
|
---|
344 | #if USE_ETHERNET
|
---|
345 | #if LWIP_IPV4
|
---|
346 | ip4_addr_set_zero(&gw);
|
---|
347 | ip4_addr_set_zero(&ipaddr);
|
---|
348 | ip4_addr_set_zero(&netmask);
|
---|
349 | #if USE_ETHERNET_TCPIP
|
---|
350 | #if USE_DHCP
|
---|
351 | printf("Starting lwIP, local interface IP is dhcp-enabled\n");
|
---|
352 | #elif USE_AUTOIP
|
---|
353 | printf("Starting lwIP, local interface IP is autoip-enabled\n");
|
---|
354 | #else /* USE_DHCP */
|
---|
355 | LWIP_PORT_INIT_GW(&gw);
|
---|
356 | LWIP_PORT_INIT_IPADDR(&ipaddr);
|
---|
357 | LWIP_PORT_INIT_NETMASK(&netmask);
|
---|
358 | printf("Starting lwIP, local interface IP is %s\n", ip4addr_ntoa(&ipaddr));
|
---|
359 | #endif /* USE_DHCP */
|
---|
360 | #endif /* USE_ETHERNET_TCPIP */
|
---|
361 | #else /* LWIP_IPV4 */
|
---|
362 | printf("Starting lwIP, IPv4 disable\n");
|
---|
363 | #endif /* LWIP_IPV4 */
|
---|
364 |
|
---|
365 | #if LWIP_IPV4
|
---|
366 | init_default_netif(&ipaddr, &netmask, &gw);
|
---|
367 | #else
|
---|
368 | init_default_netif();
|
---|
369 | #endif
|
---|
370 | #if LWIP_IPV6
|
---|
371 | netif_create_ip6_linklocal_address(netif_default, 1);
|
---|
372 | #if LWIP_IPV6_AUTOCONFIG
|
---|
373 | netif_default->ip6_autoconfig_enabled = 1;
|
---|
374 | #endif
|
---|
375 | printf("ip6 linklocal address: %s\n", ip6addr_ntoa(netif_ip6_addr(netif_default, 0)));
|
---|
376 | #endif /* LWIP_IPV6 */
|
---|
377 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
378 | netif_set_status_callback(netif_default, status_callback);
|
---|
379 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
380 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
381 | netif_set_link_callback(netif_default, link_callback);
|
---|
382 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
383 |
|
---|
384 | #if USE_ETHERNET_TCPIP
|
---|
385 | #if LWIP_AUTOIP
|
---|
386 | autoip_set_struct(netif_default, &netif_autoip);
|
---|
387 | #endif /* LWIP_AUTOIP */
|
---|
388 | #if LWIP_DHCP
|
---|
389 | dhcp_set_struct(netif_default, &netif_dhcp);
|
---|
390 | #endif /* LWIP_DHCP */
|
---|
391 | netif_set_up(netif_default);
|
---|
392 | #if USE_DHCP
|
---|
393 | err = dhcp_start(netif_default);
|
---|
394 | LWIP_ASSERT("dhcp_start failed", err == ERR_OK);
|
---|
395 | #elif USE_AUTOIP
|
---|
396 | err = autoip_start(netif_default);
|
---|
397 | LWIP_ASSERT("autoip_start failed", err == ERR_OK);
|
---|
398 | #endif /* USE_DHCP */
|
---|
399 | #else /* USE_ETHERNET_TCPIP */
|
---|
400 | /* Use ethernet for PPPoE only */
|
---|
401 | netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */
|
---|
402 | netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */
|
---|
403 | #endif /* USE_ETHERNET_TCPIP */
|
---|
404 |
|
---|
405 | #if USE_PPP && PPPOE_SUPPORT
|
---|
406 | /* start PPPoE after ethernet netif is added! */
|
---|
407 | ppp = pppoe_create(&ppp_netif, netif_default, NULL, NULL, pppLinkStatusCallback, NULL);
|
---|
408 | if (ppp == NULL) {
|
---|
409 | printf("pppoe_create error\n");
|
---|
410 | } else {
|
---|
411 | ppp_set_auth(ppp, PPPAUTHTYPE_ANY, username, password);
|
---|
412 | ppp_connect(ppp, 0);
|
---|
413 | }
|
---|
414 | #endif /* USE_PPP && PPPOE_SUPPORT */
|
---|
415 |
|
---|
416 | #endif /* USE_ETHERNET */
|
---|
417 | #if USE_SLIPIF
|
---|
418 | #if LWIP_IPV4
|
---|
419 | #define SLIP1_ADDRS &ipaddr_slip1, &netmask_slip1, &gw_slip1,
|
---|
420 | LWIP_PORT_INIT_SLIP1_IPADDR(&ipaddr_slip1);
|
---|
421 | LWIP_PORT_INIT_SLIP1_GW(&gw_slip1);
|
---|
422 | LWIP_PORT_INIT_SLIP1_NETMASK(&netmask_slip1);
|
---|
423 | printf("Starting lwIP slipif, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip1));
|
---|
424 | #else
|
---|
425 | #define SLIP1_ADDRS
|
---|
426 | printf("Starting lwIP slipif\n");
|
---|
427 | #endif
|
---|
428 | #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT
|
---|
429 | num_slip1++; /* COM ports cannot be 0-based */
|
---|
430 | #endif
|
---|
431 | netif_add(&slipif1, SLIP1_ADDRS &num_slip1, slipif_init, ip_input);
|
---|
432 | #if !USE_ETHERNET
|
---|
433 | netif_set_default(&slipif1);
|
---|
434 | #endif /* !USE_ETHERNET */
|
---|
435 | #if LWIP_IPV6
|
---|
436 | netif_create_ip6_linklocal_address(&slipif1, 1);
|
---|
437 | printf("SLIP ip6 linklocal address: %s\n", ip6addr_ntoa(netif_ip6_addr(&slipif1, 0)));
|
---|
438 | #endif /* LWIP_IPV6 */
|
---|
439 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
440 | netif_set_status_callback(&slipif1, status_callback);
|
---|
441 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
442 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
443 | netif_set_link_callback(&slipif1, link_callback);
|
---|
444 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
445 | netif_set_up(&slipif1);
|
---|
446 |
|
---|
447 | #if USE_SLIPIF > 1
|
---|
448 | #if LWIP_IPV4
|
---|
449 | #define SLIP2_ADDRS &ipaddr_slip2, &netmask_slip2, &gw_slip2,
|
---|
450 | LWIP_PORT_INIT_SLIP2_IPADDR(&ipaddr_slip2);
|
---|
451 | LWIP_PORT_INIT_SLIP2_GW(&gw_slip2);
|
---|
452 | LWIP_PORT_INIT_SLIP2_NETMASK(&netmask_slip2);
|
---|
453 | printf("Starting lwIP SLIP if #2, local interface IP is %s\n", ip4addr_ntoa(&ipaddr_slip2));
|
---|
454 | #else
|
---|
455 | #define SLIP2_ADDRS
|
---|
456 | printf("Starting lwIP SLIP if #2\n");
|
---|
457 | #endif
|
---|
458 | #if defined(SIO_USE_COMPORT) && SIO_USE_COMPORT
|
---|
459 | num_slip2++; /* COM ports cannot be 0-based */
|
---|
460 | #endif
|
---|
461 | netif_add(&slipif2, SLIP2_ADDRS &num_slip2, slipif_init, ip_input);
|
---|
462 | #if LWIP_IPV6
|
---|
463 | netif_create_ip6_linklocal_address(&slipif1, 1);
|
---|
464 | printf("SLIP2 ip6 linklocal address: ");
|
---|
465 | ip6_addr_debug_print(0xFFFFFFFF & ~LWIP_DBG_HALT, netif_ip6_addr(&slipif2, 0));
|
---|
466 | printf("\n");
|
---|
467 | #endif /* LWIP_IPV6 */
|
---|
468 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
469 | netif_set_status_callback(&slipif2, status_callback);
|
---|
470 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
471 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
472 | netif_set_link_callback(&slipif2, link_callback);
|
---|
473 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
474 | netif_set_up(&slipif2);
|
---|
475 | #endif /* USE_SLIPIF > 1*/
|
---|
476 | #endif /* USE_SLIPIF */
|
---|
477 | }
|
---|
478 |
|
---|
479 | #if LWIP_DNS_APP && LWIP_DNS
|
---|
480 | static void
|
---|
481 | dns_found(const char *name, const ip_addr_t *addr, void *arg)
|
---|
482 | {
|
---|
483 | LWIP_UNUSED_ARG(arg);
|
---|
484 | printf("%s: %s\n", name, addr ? ipaddr_ntoa(addr) : "<not found>");
|
---|
485 | }
|
---|
486 |
|
---|
487 | static void
|
---|
488 | dns_dorequest(void *arg)
|
---|
489 | {
|
---|
490 | const char* dnsname = "3com.com";
|
---|
491 | ip_addr_t dnsresp;
|
---|
492 | LWIP_UNUSED_ARG(arg);
|
---|
493 |
|
---|
494 | if (dns_gethostbyname(dnsname, &dnsresp, dns_found, 0) == ERR_OK) {
|
---|
495 | dns_found(dnsname, &dnsresp, 0);
|
---|
496 | }
|
---|
497 | }
|
---|
498 | #endif /* LWIP_DNS_APP && LWIP_DNS */
|
---|
499 |
|
---|
500 | /* This function initializes applications */
|
---|
501 | static void
|
---|
502 | apps_init(void)
|
---|
503 | {
|
---|
504 | #if LWIP_DNS_APP && LWIP_DNS
|
---|
505 | /* wait until the netif is up (for dhcp, autoip or ppp) */
|
---|
506 | sys_timeout(5000, dns_dorequest, NULL);
|
---|
507 | #endif /* LWIP_DNS_APP && LWIP_DNS */
|
---|
508 |
|
---|
509 | #if LWIP_CHARGEN_APP && LWIP_SOCKET
|
---|
510 | chargen_init();
|
---|
511 | #endif /* LWIP_CHARGEN_APP && LWIP_SOCKET */
|
---|
512 |
|
---|
513 | #if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP
|
---|
514 | ping_init(&netif_default->gw);
|
---|
515 | #endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */
|
---|
516 |
|
---|
517 | #if LWIP_NETBIOS_APP && LWIP_UDP
|
---|
518 | netbiosns_init();
|
---|
519 | #ifndef NETBIOS_LWIP_NAME
|
---|
520 | #if LWIP_NETIF_HOSTNAME
|
---|
521 | netbiosns_set_name(netif_default->hostname);
|
---|
522 | #else
|
---|
523 | netbiosns_set_name("NETBIOSLWIPDEV");
|
---|
524 | #endif
|
---|
525 | #endif
|
---|
526 | #endif /* LWIP_NETBIOS_APP && LWIP_UDP */
|
---|
527 |
|
---|
528 | #if LWIP_HTTPD_APP && LWIP_TCP
|
---|
529 | #ifdef LWIP_HTTPD_APP_NETCONN
|
---|
530 | http_server_netconn_init();
|
---|
531 | #else /* LWIP_HTTPD_APP_NETCONN */
|
---|
532 | #if defined(LWIP_HTTPD_EXAMPLE_CUSTOMFILES) && LWIP_HTTPD_EXAMPLE_CUSTOMFILES && defined(LWIP_HTTPD_EXAMPLE_CUSTOMFILES_ROOTDIR)
|
---|
533 | fs_ex_init(LWIP_HTTPD_EXAMPLE_CUSTOMFILES_ROOTDIR);
|
---|
534 | #endif
|
---|
535 | httpd_init();
|
---|
536 | #if defined(LWIP_HTTPD_EXAMPLE_SSI_SIMPLE) && LWIP_HTTPD_EXAMPLE_SSI_SIMPLE
|
---|
537 | ssi_ex_init();
|
---|
538 | #endif
|
---|
539 | #if defined(LWIP_HTTPD_EXAMPLE_CGI_SIMPLE) && LWIP_HTTPD_EXAMPLE_CGI_SIMPLE
|
---|
540 | cgi_ex_init();
|
---|
541 | #endif
|
---|
542 | #endif /* LWIP_HTTPD_APP_NETCONN */
|
---|
543 | #endif /* LWIP_HTTPD_APP && LWIP_TCP */
|
---|
544 |
|
---|
545 | #if LWIP_NETIO_APP && LWIP_TCP
|
---|
546 | netio_init();
|
---|
547 | #endif /* LWIP_NETIO_APP && LWIP_TCP */
|
---|
548 |
|
---|
549 | #if LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP
|
---|
550 | rtp_init();
|
---|
551 | #endif /* LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP */
|
---|
552 |
|
---|
553 | #if LWIP_SHELL_APP && LWIP_NETCONN
|
---|
554 | shell_init();
|
---|
555 | #endif /* LWIP_SHELL_APP && LWIP_NETCONN */
|
---|
556 | #if LWIP_TCPECHO_APP
|
---|
557 | #if LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN)
|
---|
558 | tcpecho_init();
|
---|
559 | #else /* LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) */
|
---|
560 | tcpecho_raw_init();
|
---|
561 | #endif
|
---|
562 | #endif /* LWIP_TCPECHO_APP && LWIP_NETCONN */
|
---|
563 | #if LWIP_UDPECHO_APP && LWIP_NETCONN
|
---|
564 | udpecho_init();
|
---|
565 | #endif /* LWIP_UDPECHO_APP && LWIP_NETCONN */
|
---|
566 | #if LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET
|
---|
567 | socket_examples_init();
|
---|
568 | #endif /* LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET */
|
---|
569 | #if LWIP_MDNS_APP
|
---|
570 | mdns_example_init();
|
---|
571 | #endif
|
---|
572 | #if LWIP_SNMP_APP
|
---|
573 | snmp_example_init();
|
---|
574 | #endif
|
---|
575 | #if LWIP_SNTP_APP
|
---|
576 | sntp_example_init();
|
---|
577 | #endif
|
---|
578 | #if LWIP_TFTP_APP
|
---|
579 | tftp_example_init();
|
---|
580 | #endif
|
---|
581 | #if LWIP_LWIPERF_APP
|
---|
582 | lwiperf_example_init();
|
---|
583 | #endif
|
---|
584 | #if LWIP_MQTT_APP
|
---|
585 | mqtt_example_init();
|
---|
586 | #endif
|
---|
587 |
|
---|
588 | #ifdef LWIP_APP_INIT
|
---|
589 | LWIP_APP_INIT();
|
---|
590 | #endif
|
---|
591 | }
|
---|
592 |
|
---|
593 | /* This function initializes this lwIP test. When NO_SYS=1, this is done in
|
---|
594 | * the main_loop context (there is no other one), when NO_SYS=0, this is done
|
---|
595 | * in the tcpip_thread context */
|
---|
596 | static void
|
---|
597 | test_init(void * arg)
|
---|
598 | { /* remove compiler warning */
|
---|
599 | #if NO_SYS
|
---|
600 | LWIP_UNUSED_ARG(arg);
|
---|
601 | #else /* NO_SYS */
|
---|
602 | sys_sem_t *init_sem;
|
---|
603 | LWIP_ASSERT("arg != NULL", arg != NULL);
|
---|
604 | init_sem = (sys_sem_t*)arg;
|
---|
605 | #endif /* NO_SYS */
|
---|
606 |
|
---|
607 | /* init randomizer again (seed per thread) */
|
---|
608 | srand((unsigned int)time(0));
|
---|
609 |
|
---|
610 | /* init network interfaces */
|
---|
611 | test_netif_init();
|
---|
612 |
|
---|
613 | /* init apps */
|
---|
614 | apps_init();
|
---|
615 |
|
---|
616 | #if !NO_SYS
|
---|
617 | sys_sem_signal(init_sem);
|
---|
618 | #endif /* !NO_SYS */
|
---|
619 | }
|
---|
620 |
|
---|
621 | /* This is somewhat different to other ports: we have a main loop here:
|
---|
622 | * a dedicated task that waits for packets to arrive. This would normally be
|
---|
623 | * done from interrupt context with embedded hardware, but we don't get an
|
---|
624 | * interrupt in windows for that :-) */
|
---|
625 | static void
|
---|
626 | main_loop(void)
|
---|
627 | {
|
---|
628 | #if !NO_SYS
|
---|
629 | err_t err;
|
---|
630 | sys_sem_t init_sem;
|
---|
631 | #endif /* NO_SYS */
|
---|
632 | #if USE_PPP
|
---|
633 | #if !USE_ETHERNET
|
---|
634 | int count;
|
---|
635 | u8_t rxbuf[1024];
|
---|
636 | #endif
|
---|
637 | volatile int callClosePpp = 0;
|
---|
638 | #endif /* USE_PPP */
|
---|
639 |
|
---|
640 | /* initialize lwIP stack, network interfaces and applications */
|
---|
641 | #if NO_SYS
|
---|
642 | lwip_init();
|
---|
643 | test_init(NULL);
|
---|
644 | #else /* NO_SYS */
|
---|
645 | err = sys_sem_new(&init_sem, 0);
|
---|
646 | LWIP_ASSERT("failed to create init_sem", err == ERR_OK);
|
---|
647 | LWIP_UNUSED_ARG(err);
|
---|
648 | tcpip_init(test_init, &init_sem);
|
---|
649 | /* we have to wait for initialization to finish before
|
---|
650 | * calling update_adapter()! */
|
---|
651 | sys_sem_wait(&init_sem);
|
---|
652 | sys_sem_free(&init_sem);
|
---|
653 | #endif /* NO_SYS */
|
---|
654 |
|
---|
655 | #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
|
---|
656 | netconn_thread_init();
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | /* MAIN LOOP for driver update (and timers if NO_SYS) */
|
---|
660 | while (!LWIP_EXAMPLE_APP_ABORT()) {
|
---|
661 | #if NO_SYS
|
---|
662 | /* handle timers (already done in tcpip.c when NO_SYS=0) */
|
---|
663 | sys_check_timeouts();
|
---|
664 | #endif /* NO_SYS */
|
---|
665 |
|
---|
666 | #if USE_ETHERNET
|
---|
667 | default_netif_poll();
|
---|
668 | #else /* USE_ETHERNET */
|
---|
669 | /* try to read characters from serial line and pass them to PPPoS */
|
---|
670 | count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024);
|
---|
671 | if(count > 0) {
|
---|
672 | pppos_input(ppp, rxbuf, count);
|
---|
673 | } else {
|
---|
674 | /* nothing received, give other tasks a chance to run */
|
---|
675 | sys_msleep(1);
|
---|
676 | }
|
---|
677 |
|
---|
678 | #endif /* USE_ETHERNET */
|
---|
679 | #if USE_SLIPIF
|
---|
680 | slipif_poll(&slipif1);
|
---|
681 | #if USE_SLIPIF > 1
|
---|
682 | slipif_poll(&slipif2);
|
---|
683 | #endif /* USE_SLIPIF > 1 */
|
---|
684 | #endif /* USE_SLIPIF */
|
---|
685 | #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING
|
---|
686 | /* check for loopback packets on all netifs */
|
---|
687 | netif_poll_all();
|
---|
688 | #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */
|
---|
689 | #if USE_PPP
|
---|
690 | {
|
---|
691 | int do_hup = 0;
|
---|
692 | if(do_hup) {
|
---|
693 | ppp_close(ppp, 1);
|
---|
694 | do_hup = 0;
|
---|
695 | }
|
---|
696 | }
|
---|
697 | if(callClosePpp && ppp) {
|
---|
698 | /* make sure to disconnect PPP before stopping the program... */
|
---|
699 | callClosePpp = 0;
|
---|
700 | #if NO_SYS
|
---|
701 | ppp_close(ppp, 0);
|
---|
702 | #else
|
---|
703 | pppapi_close(ppp, 0);
|
---|
704 | #endif
|
---|
705 | ppp = NULL;
|
---|
706 | }
|
---|
707 | #endif /* USE_PPP */
|
---|
708 | }
|
---|
709 |
|
---|
710 | #if USE_PPP
|
---|
711 | if(ppp) {
|
---|
712 | u32_t started;
|
---|
713 | printf("Closing PPP connection...\n");
|
---|
714 | /* make sure to disconnect PPP before stopping the program... */
|
---|
715 | #if NO_SYS
|
---|
716 | ppp_close(ppp, 0);
|
---|
717 | #else
|
---|
718 | pppapi_close(ppp, 0);
|
---|
719 | #endif
|
---|
720 | ppp = NULL;
|
---|
721 | /* Wait for some time to let PPP finish... */
|
---|
722 | started = sys_now();
|
---|
723 | do
|
---|
724 | {
|
---|
725 | #if USE_ETHERNET
|
---|
726 | default_netif_poll();
|
---|
727 | #endif
|
---|
728 | /* @todo: need a better check here: only wait until PPP is down */
|
---|
729 | } while(sys_now() - started < 5000);
|
---|
730 | }
|
---|
731 | #endif /* USE_PPP */
|
---|
732 | #if (LWIP_SOCKET || LWIP_NETCONN) && LWIP_NETCONN_SEM_PER_THREAD
|
---|
733 | netconn_thread_cleanup();
|
---|
734 | #endif
|
---|
735 | #if USE_ETHERNET
|
---|
736 | default_netif_shutdown();
|
---|
737 | #endif /* USE_ETHERNET */
|
---|
738 | }
|
---|
739 |
|
---|
740 | #if USE_PPP && PPPOS_SUPPORT
|
---|
741 | int main(int argc, char **argv)
|
---|
742 | #else /* USE_PPP && PPPOS_SUPPORT */
|
---|
743 | int main(void)
|
---|
744 | #endif /* USE_PPP && PPPOS_SUPPORT */
|
---|
745 | {
|
---|
746 | #if USE_PPP && PPPOS_SUPPORT
|
---|
747 | if(argc > 1) {
|
---|
748 | sio_idx = (u8_t)atoi(argv[1]);
|
---|
749 | }
|
---|
750 | printf("Using serial port %d for PPP\n", sio_idx);
|
---|
751 | #endif /* USE_PPP && PPPOS_SUPPORT */
|
---|
752 | /* no stdio-buffering, please! */
|
---|
753 | setvbuf(stdout, NULL,_IONBF, 0);
|
---|
754 |
|
---|
755 | main_loop();
|
---|
756 |
|
---|
757 | return 0;
|
---|
758 | }
|
---|