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