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