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