A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetServer.cpp Source File

NetServer.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 "netif/rmiiif.h"
00014 #include "netif/ethnetif.h"
00015 
00016 #include "NetServer.h"
00017 #include "TCPListener.h"
00018 #include "TCPCallbackListener.h"
00019 #include "TCPConnection.h"
00020 
00021 using namespace std;
00022 using namespace mbed;
00023 
00024 NetServer *NetServer::singleton = NULL;
00025 
00026 extern const char *ethernet_mac_address();
00027 
00028 NetServer::NetServer() : netif(&netif_data), dhcp(true), hostname(HOSTNAME) {
00029   IP4_ADDR(&netmask, 255,255,255,255);
00030   IP4_ADDR(&gateway, 0,0,0,0);
00031   IP4_ADDR(&ipaddr, 0,0,0,0);
00032   netif->hwaddr_len = 0;
00033   const char *m = ethernet_mac_address();
00034   mac.addr[0] = m[0];
00035   mac.addr[1] = m[1];
00036   mac.addr[2] = m[2];
00037   mac.addr[3] = m[3];
00038   mac.addr[4] = m[4];
00039   mac.addr[5] = m[5];
00040   del = new list<TCPItem *>();
00041 }
00042 
00043 NetServer::NetServer(struct ip_addr ip, struct ip_addr nm, struct ip_addr gw)
00044  : netif(&netif_data), ipaddr(ip), netmask(nm), gateway(gw), dhcp(false), hostname(HOSTNAME) {
00045   netif->hwaddr_len = 0;
00046   const char *m = ethernet_mac_address();
00047   mac.addr[0] = m[0];
00048   mac.addr[1] = m[1];
00049   mac.addr[2] = m[2];
00050   mac.addr[3] = m[3];
00051   mac.addr[4] = m[4];
00052   mac.addr[5] = m[5];
00053   del = new list<TCPItem *>();
00054 }
00055 
00056 NetServer::~NetServer() {
00057   
00058 }
00059 
00060 void NetServer::_poll() const {
00061   while(!del->empty()) {
00062     TCPItem *item = del->front();
00063     del->pop_front();
00064     delete item;
00065   }
00066   emac_input(netif);
00067   tcp_tmr();
00068 }
00069 
00070 void NetServer::init() {
00071   lwip_init();
00072   
00073   netif->hwaddr_len = ETHARP_HWADDR_LEN;
00074   *((struct eth_addr *)&(netif->hwaddr[0])) = mac;
00075   
00076   netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, emac_init, ip_input);
00077   netif->hostname = this->hostname;
00078   netif_set_default(netif);
00079   if(!dhcp) {
00080     netif_set_up(netif);
00081   } else {
00082     dhcp_start(netif);
00083   }
00084 
00085   tickARP.attach_us( &etharp_tmr,  ARP_TMR_INTERVAL  * 1000); 
00086   //eth_tick.attach_us<NetServer>(this,&emac_tmr, TCP_FAST_INTERVAL * 1000);
00087   dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000);
00088   if(dhcp) {
00089     dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000);
00090     dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000);
00091   }
00092 }
00093 
00094 void NetServer::setUp() const {
00095   netif_set_up(netif);
00096 }
00097 
00098 void NetServer::setDown() const {
00099   netif_set_down(netif);
00100 }
00101 
00102 void NetServer::waitUntilReady() {
00103   while(!netif_is_up(netif)) {
00104     _poll();
00105 //    wait(0.2);
00106   }
00107   ipaddr = netif->ip_addr;
00108   printf("IP: %hhu.%hhu.%hhu.%hhu\n", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF);
00109 }
00110 
00111 TCPCallbackListener *NetServer::bindTCPPort(u16_t port, err_t (*accept)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const {
00112   TCPCallbackListener *listener = new TCPCallbackListener(port, accept);
00113   listener->bind();
00114   return listener;
00115 }
00116 
00117 void NetServer::free(TCPItem *item) const {
00118   del->push_back(item);
00119 }