📄 test.c
字号:
#if LWIP_AUTOIP
autoip_set_struct(&netif, &netif_autoip);
#endif /* LWIP_AUTOIP */
#if LWIP_DHCP
dhcp_set_struct(&netif, &netif_dhcp);
dhcp_start(&netif);
#elif LWIP_AUTOIP
autoip_start(&netif);
#else /* LWIP_DHCP */
netif_set_up(&netif);
#endif /* LWIP_DHCP */
#else /* USE_ETHERNET_TCPIP */
/* Use ethernet for PPPoE only */
netif.flags &= ~(NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP); /* no ARP */
netif.flags |= NETIF_FLAG_ETHERNET; /* but pure ethernet */
#endif /* USE_ETHERNET_TCPIP */
#if PPP_SUPPORT && PPPOE_SUPPORT
/* start PPPoE after ethernet netif is added! */
ppp_desc = pppOverEthernetOpen(&netif, NULL, NULL, pppLinkStatusCallback, NULL);
#endif /* PPP_SUPPORT && PPPOE_SUPPORT */
#endif /* USE_ETHERNET */
}
#if LWIP_DNS_APP && LWIP_DNS
void dns_found(const char *name, ip_addr_t *addr, void *arg)
{
LWIP_UNUSED_ARG(arg);
printf("%s: %s\n", name, addr ? ip_ntoa(addr) : "<not found>");
}
void dns_dorequest(void *arg)
{
char* dnsname = "3com.com";
ip_addr_t dnsresp;
LWIP_UNUSED_ARG(arg);
if (dns_gethostbyname(dnsname, &dnsresp, dns_found, 0) == ERR_OK) {
dns_found(dnsname, &dnsresp, 0);
}
}
#endif /* LWIP_DNS_APP && LWIP_DNS */
/* This function initializes applications */
static void
apps_init()
{
#if LWIP_DNS_APP && LWIP_DNS
/* wait until the netif is up (for dhcp, autoip or ppp) */
sys_timeout(5000, dns_dorequest, NULL);
#endif /* LWIP_DNS_APP && LWIP_DNS */
#if LWIP_CHARGEN_APP && LWIP_SOCKET
chargen_init();
#endif /* LWIP_CHARGEN_APP && LWIP_SOCKET */
#if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP
ping_init();
#endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */
#if LWIP_NETBIOS_APP && LWIP_UDP
netbios_init();
#endif /* LWIP_NETBIOS_APP && LWIP_UDP */
#if LWIP_HTTPD_APP && LWIP_TCP
#ifdef LWIP_HTTPD_APP_NETCONN
http_server_netconn_init();
#else /* LWIP_HTTPD_APP_NETCONN */
httpd_init();
#endif /* LWIP_HTTPD_APP_NETCONN */
#endif /* LWIP_HTTPD_APP && LWIP_TCP */
#if LWIP_NETIO_APP && LWIP_TCP
netio_init();
#endif /* LWIP_NETIO_APP && LWIP_TCP */
#if LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP
rtp_init();
#endif /* LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP */
#if LWIP_SNTP_APP && LWIP_SOCKET
sntp_init();
#endif /* LWIP_SNTP_APP && LWIP_SOCKET */
#if LWIP_SHELL_APP && LWIP_NETCONN
shell_init();
#endif /* LWIP_SHELL_APP && LWIP_NETCONN */
#if LWIP_TCPECHO_APP
#if LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN)
tcpecho_init();
#else /* LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) */
echo_init();
#endif
#endif /* LWIP_TCPECHO_APP && LWIP_NETCONN */
#if LWIP_UDPECHO_APP && LWIP_NETCONN
udpecho_init();
#endif /* LWIP_UDPECHO_APP && LWIP_NETCONN */
#if LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET
socket_examples_init();
#endif /* LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET */
}
/* This function initializes this lwIP test. When NO_SYS=1, this is done in
* the main_loop context (there is no other one), when NO_SYS=0, this is done
* in the tcpip_thread context */
static void
test_init(void * arg)
{ /* remove compiler warning */
#if NO_SYS
LWIP_UNUSED_ARG(arg);
#else /* NO_SYS */
sys_sem_t *init_sem;
LWIP_ASSERT("arg != NULL", arg != NULL);
init_sem = (sys_sem_t*)arg;
#endif /* NO_SYS */
/* init network interfaces */
msvc_netif_init();
/* init apps */
apps_init();
#if !NO_SYS
sys_sem_signal(init_sem);
#endif /* !NO_SYS */
}
#if PPP_SUPPORT
static void pppCloseCallback(void *arg)
{
int pd = (int)arg;
pppClose(pd);
}
#endif /* PPP_SUPPORT */
/* This is somewhat different to other ports: we have a main loop here:
* a dedicated task that waits for packets to arrive. This would normally be
* done from interrupt context with embedded hardware, but we don't get an
* interrupt in windows for that :-) */
void main_loop()
{
#if !NO_SYS
err_t err;
sys_sem_t init_sem;
#endif /* NO_SYS */
#if PPP_SUPPORT
#if !USE_ETHERNET
int count;
u8_t rxbuf[1024];
#endif
volatile int callClosePpp = 0;
#endif /* PPP_SUPPORT */
/* initialize lwIP stack, network interfaces and applications */
#if NO_SYS
lwip_init();
test_init(NULL);
#else /* NO_SYS */
err = sys_sem_new(&init_sem, 0);
tcpip_init(test_init, &init_sem);
/* we have to wait for initialization to finish before
* calling update_adapter()! */
sys_sem_wait(&init_sem);
sys_sem_free(&init_sem);
#endif /* NO_SYS */
/* MAIN LOOP for driver update (and timers if NO_SYS) */
while (!_kbhit()) {
#if NO_SYS
/* handle timers (already done in tcpip.c when NO_SYS=0) */
sys_check_timeouts();
#endif /* NO_SYS */
#if USE_ETHERNET
#if !PCAPIF_RX_USE_THREAD
/* check for packets and link status*/
pcapif_poll(&netif);
/* When pcapif_poll comes back, there are not packets, so sleep to
prevent 100% CPU load. Don't do this in an embedded system since it
increases latency! */
sys_msleep(1);
#else /* !PCAPIF_RX_USE_THREAD */
sys_msleep(50);
#endif /* !PCAPIF_RX_USE_THREAD */
#else /* USE_ETHERNET */
#if 0 /* set this to 1 if PPP_INPROC_OWNTHREAD==0 or not defined (see ppp.c) */
/* try to read characters from serial line and pass them to PPPoS */
count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024);
if(count > 0) {
pppos_input(ppp_desc, rxbuf, count);
} else
#endif
{
/* nothing received, give other tasks a chance to run */
sys_msleep(1);
}
#endif /* USE_ETHERNET */
#if !LWIP_NETIF_LOOPBACK_MULTITHREADING
/* check for loopback packets on all netifs */
netif_poll_all();
#endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
#if PPP_SUPPORT
{
int do_hup = 0;
if(do_hup) {
pppSigHUP(ppp_desc);
do_hup = 0;
}
}
if(callClosePpp && (ppp_desc >= 0)) {
/* make sure to disconnect PPP before stopping the program... */
callClosePpp = 0;
#if NO_SYS
pppClose(ppp_desc);
#else
tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0);
#endif
ppp_desc = -1;
}
#endif /* PPP_SUPPORT */
}
#if PPP_SUPPORT
if(ppp_desc >= 0) {
u32_t started;
printf("Closing PPP connection...\n");
/* make sure to disconnect PPP before stopping the program... */
#if NO_SYS
pppClose(ppp_desc);
#else
tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0);
#endif
ppp_desc = -1;
/* Wait for some time to let PPP finish... */
started = sys_now();
do
{
#if USE_ETHERNET
pcapif_poll(&netif);
#else /* USE_ETHERNET */
sys_msleep(50);
#endif /* USE_ETHERNET */
/* @todo: need a better check here: only wait until PPP is down */
} while(sys_now() - started < 5000);
}
#endif /* PPP_SUPPORT */
#if USE_ETHERNET
/* release the pcap library... */
pcapif_shutdown(&netif);
#endif /* USE_ETHERNET */
}
#if PPP_SUPPORT && PPPOS_SUPPORT
int main(int argc, char **argv)
#else /* PPP_SUPPORT && PPPOS_SUPPORT */
int main(void)
#endif /* PPP_SUPPORT && PPPOS_SUPPORT */
{
#if PPP_SUPPORT && PPPOS_SUPPORT
if(argc > 1) {
sio_idx = (u8_t)atoi(argv[1]);
}
printf("Using serial port %d for PPP\n", sio_idx);
#endif /* PPP_SUPPORT && PPPOS_SUPPORT */
/* no stdio-buffering, please! */
setvbuf(stdout, NULL,_IONBF, 0);
main_loop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -