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.h Source File

NetServer.h

00001 #ifndef NETSERVER_H
00002 #define NETSERVER_H
00003 
00004 #include "ipv4/lwip/ip_addr.h"
00005 #include "lwip/netif.h"
00006 #include "netif/etharp.h"
00007 #include "dns.h"
00008 
00009 #include <list>
00010 
00011 namespace mbed {
00012   class TCPItem;
00013   class TCPListener;
00014   class TCPCallbackListener;
00015   class TCPConnection;
00016 
00017   /**
00018    * Network main class
00019    * provides the basic network features.
00020    */
00021   class NetServer {
00022     public:
00023       /**
00024        * Returns you the NetServer instance. 
00025        * If there is no object it will create a new one.
00026        * But it will not initialise it.
00027        * Configure the object for DHCP.
00028        */
00029       static NetServer *create() {
00030         if(!NetServer::singleton) {
00031           NetServer::singleton = new NetServer();
00032         }
00033         return NetServer::singleton;
00034       }
00035 
00036       /**
00037        * Returns you the NetServer instance. 
00038        * If there is no object it will create a new one.
00039        * But it will not initialise it.
00040        * You have to insert ipaddres, netmask and gateway.
00041        */
00042       inline static NetServer *create(const struct ip_addr &ip, const struct ip_addr &netmask, const struct ip_addr &gateway) {
00043         if(!NetServer::singleton) {
00044           NetServer::singleton = new NetServer(ip, netmask, gateway);
00045         }
00046         return NetServer::singleton;
00047       }
00048       
00049       /**
00050        * Returns you the NetServer instance. 
00051        * If there is no object it will create a new one
00052        * and it will initialise it.
00053        * A new created object will ever use DHCP and the default MAC address
00054        * and default hostname.
00055        */
00056       inline static NetServer *ready() {
00057         if(!NetServer::singleton) {
00058           NetServer::singleton = new NetServer();
00059         }
00060         if(!NetServer::singleton->netif->hwaddr_len) {
00061           NetServer::singleton->init();
00062           NetServer::singleton->waitUntilReady();
00063         }
00064         return NetServer::singleton;
00065       }
00066 
00067       /**
00068        * Returns you the NetServer instance.
00069        * Even if there is no one created.
00070        * That means use with care and in combination with NetServer::ready().
00071        * It is mutch quicker than NetServer::ready().
00072        * First call one time NetServer::ready() and then NetServer::get()
00073        * and you are save.
00074        */
00075       inline static NetServer *get() {
00076         return NetServer::singleton;
00077       }
00078 
00079       /**
00080        * Polls one time on the NetServer and all registert Interfaces.
00081        * Even if there is no one created.
00082        * That means use with care and in combination with NetServer::ready().
00083        * It is mutch faster than NetServer::ready()->_poll().
00084        * First call one time NetServer::ready() and then NetServer::poll()
00085        * and you are save.
00086        */
00087       inline static void poll() {
00088         singleton->_poll();
00089       }
00090       
00091       /**
00092        * Default destructor.
00093        */
00094       ~NetServer();
00095       
00096       /**
00097        * Set a diffrent ethernet address
00098        */
00099       void setMAC(const struct eth_addr &value) { mac = value; }
00100       
00101       /**
00102        * Get the current ethernet address
00103        */
00104       const struct eth_addr &getMAC() const { return mac; }
00105       
00106       /**
00107        * Set MBed IP Address
00108        */
00109       void setIPAddr(const struct ip_addr &value) { netif->ip_addr = ipaddr = value; }
00110       /**
00111        * Get MBed IP Address
00112        */
00113       const struct ip_addr &getIPAddr() { return ipaddr = netif->ip_addr; }
00114       
00115       /**
00116        * Set Netmask
00117        */
00118       void setNetmask(const struct ip_addr &value) { netif->netmask = netmask = value; }
00119       
00120       /**
00121        * Get Netmask
00122        */
00123       const struct ip_addr &getNetmask() { return netmask = netif->netmask; }
00124 
00125       /**
00126        * Set default Gateway
00127        */
00128       void setGateway(const struct ip_addr &value) { netif->gw = gateway = value; }
00129       
00130       /**
00131        * Get default Gateway
00132        */
00133       const struct ip_addr &getGateway() { return gateway = netif->gw; }
00134       
00135       /**
00136        * Set first Domain Name Server
00137        */
00138       void setDNS1(const struct ip_addr &value) { firstdns = value; dns_setserver(0, &firstdns); }
00139       
00140       /**
00141        * Get first Domain Name Server
00142        */
00143       const struct ip_addr &getDNS1() { return firstdns = dns_getserver(0); }
00144       
00145       /**
00146        * Set second Domain Name Server
00147        */
00148       void setDNS2(const struct ip_addr &value) { seconddns = value; dns_setserver(1, &firstdns); }
00149       
00150       /**
00151        * Get second Domain Name Server
00152        */
00153       const struct ip_addr &getDNS2() { return seconddns = dns_getserver(1); }
00154       
00155       /**
00156        * Set MBed Hostname
00157        */
00158       void setHostname(char *value) { hostname = value; }
00159       
00160       /**
00161        * Get MBed Hostname
00162        */
00163       const char *getHostname() const { return hostname; }
00164       
00165       /**
00166        * Define if DHCP sould be used.
00167        * @param value Bool if true dhcp is used else a static ip setting is assumed.
00168        */
00169       void setUseDHCP(const bool &value) { dhcp = value; }
00170       
00171       /**
00172        * Is the mbed board trying to use DHCP?
00173        */
00174       const bool &getUseDHCP() const { return dhcp; }
00175 
00176       /**
00177        * Initialise the network environment. Set up all services.
00178        * Please call after configuration.
00179        */
00180       void init();
00181 
00182       /**
00183        * Set the network interface up.
00184        * To enable the network interface after calling setDown()
00185        * Automaticly called from init().
00186        */
00187       void setUp() const;
00188       
00189       /**
00190        * Set the network interface down.
00191        * To disable the network interface temporary.
00192        * To make the interface avalible again use setUp().
00193        */
00194       void setDown() const;
00195       
00196       /**
00197        * This function waits until the network interface is Up.
00198        * To use to wait after init with DHCP. Helps continue work 
00199        * after the network interface is completly up.
00200        */
00201       void waitUntilReady();
00202 
00203       /**
00204        * Bind Callbackfunctions to a TCPPort.
00205        * It provides a clean lowlevel Interface to the TCPLayer.
00206        */      
00207       TCPCallbackListener *bindTCPPort(u16_t, err_t (*)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const;
00208 
00209       /**
00210        * Frees TCPItems because they cant do it directly.
00211        */
00212       void free(TCPItem *item) const;
00213 
00214     protected:
00215       void _poll() const;
00216 
00217       /**
00218        * Default constructor tryes to bring the network interface up with dhcp.
00219        */
00220       NetServer();
00221 
00222       /**
00223        * Constructor for fix ip setting
00224        */
00225       NetServer(struct ip_addr me_ip, struct ip_addr netmask, struct ip_addr gateway);
00226 
00227     private:
00228       /**
00229        * This is a singleton class.
00230        * So we should not have a public copy constructor.
00231        */
00232       NetServer(NetServer const&)            {}
00233 //      NetServer &operator=(NetServer const&) {}
00234       
00235       struct netif   *netif;
00236       struct netif    netif_data;
00237 
00238       struct eth_addr mac;
00239       
00240       struct ip_addr  ipaddr;
00241       struct ip_addr  netmask;
00242       struct ip_addr  gateway;
00243       
00244       struct ip_addr  firstdns;
00245       struct ip_addr  seconddns;
00246       
00247       bool            dhcp;
00248       
00249       list<TCPItem *> *del;
00250       
00251       Ticker tickARP, /*eth_tick,*/ dns_tick, dhcp_coarse, dhcp_fine;
00252       char *hostname;
00253       static NetServer *singleton;
00254   };
00255 
00256 };
00257 #endif /* NETSERVER_H */