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 "device.h"
00014 #include "Ethernet.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 NetServer::NetServer() : netif(&netif_data), dhcp(true), hostname(HOSTNAME) {
00027   _time.start();
00028   IP4_ADDR(&netmask, 255,255,255,255);
00029   IP4_ADDR(&gateway, 0,0,0,0);
00030   IP4_ADDR(&ipaddr, 0,0,0,0);
00031   netif->hwaddr_len = 0;
00032   del = new list<TCPItem *>();
00033 }
00034 
00035 NetServer::NetServer(struct ip_addr ip, struct ip_addr nm, struct ip_addr gw)
00036  : netif(&netif_data), ipaddr(ip), netmask(nm), gateway(gw), dhcp(false), hostname(HOSTNAME) {
00037   _time.start();
00038   netif->hwaddr_len = 0;
00039   del = new list<TCPItem *>();
00040 }
00041 
00042 NetServer::~NetServer() {
00043   
00044 }
00045 
00046 void NetServer::_poll() const {
00047   while(!del->empty()) {
00048     TCPItem *item = del->front();
00049     del->pop_front();
00050     delete item;
00051   }
00052   device_poll();
00053   tcp_tmr();
00054 }
00055 
00056 void NetServer::init() {
00057   lwip_init();
00058   
00059   netif->hwaddr_len = ETHARP_HWADDR_LEN;
00060   device_address((char *)netif->hwaddr);
00061   
00062   netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input);
00063   netif->hostname = (char *)this->hostname;
00064   netif_set_default(netif);
00065   if(!dhcp) {
00066     netif_set_up(netif);
00067   } else {
00068     dhcp_start(netif);
00069   }
00070 
00071   tickARP.attach_us( &etharp_tmr,  ARP_TMR_INTERVAL  * 1000); 
00072   //eth_tick.attach_us<NetServer>(this,&emac_tmr, TCP_FAST_INTERVAL * 1000);
00073   dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000);
00074   if(dhcp) {
00075     dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000);
00076     dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000);
00077   }
00078 }
00079 
00080 void NetServer::setUp() const {
00081   netif_set_up(netif);
00082 }
00083 
00084 void NetServer::setDown() const {
00085   netif_set_down(netif);
00086 }
00087 
00088 int NetServer::isUp() const {
00089   return netif_is_up(netif);
00090 }
00091 
00092 void NetServer::waitUntilReady() {
00093   while(!netif_is_up(netif)) {
00094     _poll();
00095     wait_ms(1);
00096   }
00097   ipaddr = netif->ip_addr;
00098   printf("IP: %hhu.%hhu.%hhu.%hhu\n", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF);
00099 }
00100 
00101 TCPCallbackListener *NetServer::bindTCPPort(u16_t port, err_t (*accept)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const {
00102   TCPCallbackListener *listener = new TCPCallbackListener(port, accept);
00103   listener->bind();
00104   return listener;
00105 }
00106 
00107 void NetServer::free(TCPItem *item) const {
00108   del->push_back(item);
00109 }
00110 
00111 int NetServer::time() {
00112   return NetServer::get()->_time.read_ms();
00113 }