Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of lwip by
main.cpp
00001 #include "lwip/opt.h" 00002 #include "lwip/stats.h" 00003 #include "lwip/sys.h" 00004 #include "lwip/pbuf.h" 00005 #include "lwip/udp.h" 00006 #include "lwip/tcp.h" 00007 #include "lwip/dns.h" 00008 #include "lwip/dhcp.h" 00009 #include "lwip/init.h" 00010 #include "lwip/netif.h" 00011 #include "netif/etharp.h" 00012 #include "netif/loopif.h" 00013 #include "device.h" 00014 00015 #include "mbed.h" 00016 00017 /* Some Lights */ 00018 DigitalOut led1(LED1); 00019 DigitalOut led2(LED2); 00020 int x = 0; 00021 00022 /* Struct with hold the Ethernet Data */ 00023 struct netif netif_data; 00024 00025 /* Two static html pages ready for serving, includeing http header */ 00026 const char *page[] = { 00027 "HTTP/1.1 200 OK\r\n" 00028 "Server:mbed embedded\r\n" 00029 "Content-Type: text/html\r\n" 00030 "Content-Length: 190\r\n" 00031 "\r\n" 00032 "<html>\r\n" 00033 " <header>\r\n" 00034 " <title>Whats up?<title>\r\n" 00035 " </header>\r\n" 00036 " <body>\r\n" 00037 " <h1>Hello World!!11elf</h1>\r\n" 00038 " <p>Have a greate day.</p>\r\n" 00039 " <a href=\"/1.html\">turn </a>\r\n" 00040 " </body>\r\n" 00041 "</html>\r\n" 00042 "\r\n" 00043 , 00044 "HTTP/1.1 200 OK\r\n" 00045 "Server:mbed embedded\r\n" 00046 "Content-Type: text/html\r\n" 00047 "Content-Length: 178\r\n" 00048 "\r\n" 00049 "<html>\r\n" 00050 " <header>\r\n" 00051 " <title>LED Power<title>\r\n" 00052 " </header>\r\n" 00053 " <body>\r\n" 00054 " <h1>Lets rotate!!11elf</h1>\r\n" 00055 " <p>Bam!!!!!!!!!!.</p>\r\n" 00056 " <a href=\"/\">back</a>\r\n" 00057 " </body>\r\n" 00058 "</html>\r\n" 00059 "\r\n" 00060 }; 00061 00062 /* Every time called if a packet is received for a */ 00063 /* TCPConnection which registerd this Callback. */ 00064 err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { 00065 int i; 00066 char *data; 00067 00068 /* Check if status is ok and data is arrived. */ 00069 if (err == ERR_OK && p != NULL) { 00070 /* Inform TCP that we have taken the data. */ 00071 tcp_recved(pcb, p->tot_len); 00072 data = static_cast<char *>(p->payload); 00073 00074 /* If the data is a GET request we can handle it. */ 00075 if (strncmp(data, "GET ", 4) == 0) { 00076 for (i = 0; i < 40; i++) { 00077 if (((char *)data + 4)[i] == ' ' || 00078 ((char *)data + 4)[i] == '\r' || 00079 ((char *)data + 4)[i] == '\n') { 00080 ((char *)data + 4)[i] = 0; 00081 } 00082 } 00083 00084 /* Check the URL */ 00085 /* We only know two URLs /1.html and default */ 00086 /* store the URL information in our global x variable to change twinkle behaviour. */ 00087 x = (strncmp((char *)(data + 4), "/1.html", 7) == 0)? 1: 0; 00088 /* Clean up the Memory */ 00089 pbuf_free(p); 00090 00091 /* Send the right page as answere. */ 00092 if (tcp_write(pcb, (void *)page[x], strlen(page[x]), 1) != ERR_OK) { 00093 //error("Could not write", 0); 00094 } 00095 } else { 00096 /* No data arrived */ 00097 /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */ 00098 /* We have to cleanup and destroy out TCPConnection. */ 00099 pbuf_free(p); 00100 } 00101 } 00102 00103 /* Don't panic! Everything is fine. */ 00104 return ERR_OK; 00105 } 00106 00107 /* Accept an incomming call on the registered port */ 00108 err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) { 00109 LWIP_UNUSED_ARG(arg); 00110 /* Subscribe a receive callback function */ 00111 tcp_recv(npcb, &recv_callback); 00112 00113 /* Don't panic! Everything is fine. */ 00114 return ERR_OK; 00115 } 00116 00117 int main(void) { 00118 /* Create and initialise variables */ 00119 struct netif *netif = &netif_data; 00120 struct ip_addr ipaddr; 00121 struct ip_addr netmask; 00122 struct ip_addr gateway; 00123 00124 Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; 00125 char *hostname = "mbed-c3p0"; 00126 x = 0; 00127 00128 /* Start Network with DHCP */ 00129 IP4_ADDR(&netmask, 255,255,255,255); 00130 IP4_ADDR(&gateway, 0,0,0,0); 00131 IP4_ADDR(&ipaddr, 0,0,0,0); 00132 00133 /* Initialise after configuration */ 00134 lwip_init(); 00135 00136 netif->hwaddr_len = ETHARP_HWADDR_LEN; 00137 device_address((char *)netif->hwaddr); 00138 00139 netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); 00140 netif->hostname = hostname; 00141 netif_set_default(netif); 00142 dhcp_start(netif); // <-- Use DHCP 00143 00144 /* Initialise all needed timers */ 00145 tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); 00146 tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); 00147 tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); 00148 dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); 00149 dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); 00150 dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); 00151 00152 /* Bind a function to a tcp port */ 00153 struct tcp_pcb *pcb = tcp_new(); 00154 if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) { 00155 pcb = tcp_listen(pcb); 00156 tcp_accept(pcb, &accept_callback); 00157 } 00158 00159 /* Give the signal that we are ready */ 00160 printf("entering while loop\n"); 00161 while(1) { 00162 device_poll(); 00163 if (x) { 00164 led1 = !led1; 00165 wait(0.2); 00166 } else { 00167 led2 = !led2; 00168 wait(0.2); 00169 } 00170 } 00171 } 00172
Generated on Tue Jul 12 2022 16:07:06 by
1.7.2
