A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
root@mbed.org 0:5e1631496985 1 #ifndef NETSERVER_H
root@mbed.org 0:5e1631496985 2 #define NETSERVER_H
root@mbed.org 0:5e1631496985 3
root@mbed.org 0:5e1631496985 4 #include "ipv4/lwip/ip_addr.h"
root@mbed.org 0:5e1631496985 5 #include "lwip/netif.h"
root@mbed.org 0:5e1631496985 6 #include "netif/etharp.h"
root@mbed.org 0:5e1631496985 7 #include "iputil.h"
root@mbed.org 0:5e1631496985 8 #include "dns.h"
root@mbed.org 0:5e1631496985 9 #include "mbed.h"
root@mbed.org 0:5e1631496985 10
root@mbed.org 0:5e1631496985 11 #include <list>
root@mbed.org 0:5e1631496985 12
root@mbed.org 0:5e1631496985 13 namespace mbed {
root@mbed.org 0:5e1631496985 14 class TCPItem;
root@mbed.org 0:5e1631496985 15 class TCPListener;
root@mbed.org 0:5e1631496985 16 class TCPCallbackListener;
root@mbed.org 0:5e1631496985 17 class TCPConnection;
root@mbed.org 0:5e1631496985 18
root@mbed.org 0:5e1631496985 19 /**
root@mbed.org 0:5e1631496985 20 * Network main class
root@mbed.org 0:5e1631496985 21 * provides the basic network features.
root@mbed.org 0:5e1631496985 22 */
root@mbed.org 0:5e1631496985 23 class NetServer {
root@mbed.org 0:5e1631496985 24 public:
root@mbed.org 0:5e1631496985 25 /**
root@mbed.org 0:5e1631496985 26 * Returns you the NetServer instance.
root@mbed.org 0:5e1631496985 27 * If there is no object it will create a new one.
root@mbed.org 0:5e1631496985 28 * But it will not initialise it.
root@mbed.org 0:5e1631496985 29 * Configure the object for DHCP.
root@mbed.org 0:5e1631496985 30 */
root@mbed.org 0:5e1631496985 31 static NetServer *create() {
root@mbed.org 0:5e1631496985 32 if(!NetServer::singleton) {
root@mbed.org 0:5e1631496985 33 NetServer::singleton = new NetServer();
root@mbed.org 0:5e1631496985 34 }
root@mbed.org 0:5e1631496985 35 return NetServer::singleton;
root@mbed.org 0:5e1631496985 36 }
root@mbed.org 0:5e1631496985 37
root@mbed.org 0:5e1631496985 38 /**
root@mbed.org 0:5e1631496985 39 * Returns you the NetServer instance.
root@mbed.org 0:5e1631496985 40 * If there is no object it will create a new one.
root@mbed.org 0:5e1631496985 41 * But it will not initialise it.
root@mbed.org 0:5e1631496985 42 * You have to insert ipaddres, netmask and gateway.
root@mbed.org 0:5e1631496985 43 */
root@mbed.org 0:5e1631496985 44 inline static NetServer *create(const struct ip_addr &ip, const struct ip_addr &netmask, const struct ip_addr &gateway) {
root@mbed.org 0:5e1631496985 45 if(!NetServer::singleton) {
root@mbed.org 0:5e1631496985 46 NetServer::singleton = new NetServer(ip, netmask, gateway);
root@mbed.org 0:5e1631496985 47 }
root@mbed.org 0:5e1631496985 48 return NetServer::singleton;
root@mbed.org 0:5e1631496985 49 }
root@mbed.org 0:5e1631496985 50
root@mbed.org 0:5e1631496985 51 /**
root@mbed.org 0:5e1631496985 52 * Returns you the NetServer instance.
root@mbed.org 0:5e1631496985 53 * If there is no object it will create a new one
root@mbed.org 0:5e1631496985 54 * and it will initialise it.
root@mbed.org 0:5e1631496985 55 * A new created object will ever use DHCP and the default MAC address
root@mbed.org 0:5e1631496985 56 * and default hostname.
root@mbed.org 0:5e1631496985 57 */
root@mbed.org 0:5e1631496985 58 inline static NetServer *ready() {
root@mbed.org 0:5e1631496985 59 if(!NetServer::singleton) {
root@mbed.org 0:5e1631496985 60 NetServer::singleton = new NetServer();
root@mbed.org 0:5e1631496985 61 }
root@mbed.org 0:5e1631496985 62 if(!NetServer::singleton->netif->hwaddr_len) {
root@mbed.org 0:5e1631496985 63 NetServer::singleton->init();
root@mbed.org 0:5e1631496985 64 NetServer::singleton->waitUntilReady();
root@mbed.org 0:5e1631496985 65 }
root@mbed.org 0:5e1631496985 66 return NetServer::singleton;
root@mbed.org 0:5e1631496985 67 }
root@mbed.org 0:5e1631496985 68
root@mbed.org 0:5e1631496985 69 /**
root@mbed.org 0:5e1631496985 70 * Returns you the NetServer instance.
root@mbed.org 0:5e1631496985 71 * Even if there is no one created.
root@mbed.org 0:5e1631496985 72 * That means use with care and in combination with NetServer::ready().
root@mbed.org 0:5e1631496985 73 * It is mutch quicker than NetServer::ready().
root@mbed.org 0:5e1631496985 74 * First call one time NetServer::ready() and then NetServer::get()
root@mbed.org 0:5e1631496985 75 * and you are save.
root@mbed.org 0:5e1631496985 76 */
root@mbed.org 0:5e1631496985 77 inline static NetServer *get() {
root@mbed.org 0:5e1631496985 78 return NetServer::singleton;
root@mbed.org 0:5e1631496985 79 }
root@mbed.org 0:5e1631496985 80
root@mbed.org 0:5e1631496985 81 /**
root@mbed.org 0:5e1631496985 82 * Polls one time on the NetServer and all registert Interfaces.
root@mbed.org 0:5e1631496985 83 * Even if there is no one created.
root@mbed.org 0:5e1631496985 84 * That means use with care and in combination with NetServer::ready().
root@mbed.org 0:5e1631496985 85 * It is mutch faster than NetServer::ready()->_poll().
root@mbed.org 0:5e1631496985 86 * First call one time NetServer::ready() and then NetServer::poll()
root@mbed.org 0:5e1631496985 87 * and you are save.
root@mbed.org 0:5e1631496985 88 */
root@mbed.org 0:5e1631496985 89 inline static void poll() {
root@mbed.org 0:5e1631496985 90 singleton->_poll();
root@mbed.org 0:5e1631496985 91 }
root@mbed.org 0:5e1631496985 92
root@mbed.org 0:5e1631496985 93 /**
root@mbed.org 0:5e1631496985 94 * Default destructor.
root@mbed.org 0:5e1631496985 95 */
root@mbed.org 0:5e1631496985 96 ~NetServer();
root@mbed.org 0:5e1631496985 97
root@mbed.org 0:5e1631496985 98 /**
root@mbed.org 0:5e1631496985 99 * Set MBed IP Address
root@mbed.org 0:5e1631496985 100 */
root@mbed.org 0:5e1631496985 101 void setIPAddr(const struct ip_addr &value) { netif->ip_addr = ipaddr = value; }
root@mbed.org 0:5e1631496985 102 /**
root@mbed.org 0:5e1631496985 103 * Get MBed IP Address
root@mbed.org 0:5e1631496985 104 */
root@mbed.org 0:5e1631496985 105 const struct ip_addr &getIPAddr() { return ipaddr = netif->ip_addr; }
root@mbed.org 0:5e1631496985 106
root@mbed.org 0:5e1631496985 107 /**
root@mbed.org 0:5e1631496985 108 * Set Netmask
root@mbed.org 0:5e1631496985 109 */
root@mbed.org 0:5e1631496985 110 void setNetmask(const struct ip_addr &value) { netif->netmask = netmask = value; }
root@mbed.org 0:5e1631496985 111
root@mbed.org 0:5e1631496985 112 /**
root@mbed.org 0:5e1631496985 113 * Get Netmask
root@mbed.org 0:5e1631496985 114 */
root@mbed.org 0:5e1631496985 115 const struct ip_addr &getNetmask() { return netmask = netif->netmask; }
root@mbed.org 0:5e1631496985 116
root@mbed.org 0:5e1631496985 117 /**
root@mbed.org 0:5e1631496985 118 * Set default Gateway
root@mbed.org 0:5e1631496985 119 */
root@mbed.org 0:5e1631496985 120 void setGateway(const struct ip_addr &value) { netif->gw = gateway = value; }
root@mbed.org 0:5e1631496985 121
root@mbed.org 0:5e1631496985 122 /**
root@mbed.org 0:5e1631496985 123 * Get default Gateway
root@mbed.org 0:5e1631496985 124 */
root@mbed.org 0:5e1631496985 125 const struct ip_addr &getGateway() { return gateway = netif->gw; }
root@mbed.org 0:5e1631496985 126
root@mbed.org 0:5e1631496985 127 /**
root@mbed.org 0:5e1631496985 128 * Set first Domain Name Server
root@mbed.org 0:5e1631496985 129 */
root@mbed.org 0:5e1631496985 130 void setDNS1(const struct ip_addr &value) { firstdns = value; dns_setserver(0, &firstdns); }
root@mbed.org 0:5e1631496985 131
root@mbed.org 0:5e1631496985 132 /**
root@mbed.org 0:5e1631496985 133 * Get first Domain Name Server
root@mbed.org 0:5e1631496985 134 */
root@mbed.org 0:5e1631496985 135 const struct ip_addr &getDNS1() { return firstdns = dns_getserver(0); }
root@mbed.org 0:5e1631496985 136
root@mbed.org 0:5e1631496985 137 /**
root@mbed.org 0:5e1631496985 138 * Set second Domain Name Server
root@mbed.org 0:5e1631496985 139 */
root@mbed.org 0:5e1631496985 140 void setDNS2(const struct ip_addr &value) { seconddns = value; dns_setserver(1, &firstdns); }
root@mbed.org 0:5e1631496985 141
root@mbed.org 0:5e1631496985 142 /**
root@mbed.org 0:5e1631496985 143 * Get second Domain Name Server
root@mbed.org 0:5e1631496985 144 */
root@mbed.org 0:5e1631496985 145 const struct ip_addr &getDNS2() { return seconddns = dns_getserver(1); }
root@mbed.org 0:5e1631496985 146
root@mbed.org 0:5e1631496985 147 /**
root@mbed.org 0:5e1631496985 148 * Set MBed Hostname
root@mbed.org 0:5e1631496985 149 */
root@mbed.org 0:5e1631496985 150 void setHostname(const char *value) { hostname = value; }
root@mbed.org 0:5e1631496985 151
root@mbed.org 0:5e1631496985 152 /**
root@mbed.org 0:5e1631496985 153 * Get MBed Hostname
root@mbed.org 0:5e1631496985 154 */
root@mbed.org 0:5e1631496985 155 const char *getHostname() const { return hostname; }
root@mbed.org 0:5e1631496985 156
root@mbed.org 0:5e1631496985 157 /**
root@mbed.org 0:5e1631496985 158 * Define if DHCP sould be used.
root@mbed.org 0:5e1631496985 159 * @param value Bool if true dhcp is used else a static ip setting is assumed.
root@mbed.org 0:5e1631496985 160 */
root@mbed.org 0:5e1631496985 161 void setUseDHCP(const bool &value) { dhcp = value; }
root@mbed.org 0:5e1631496985 162
root@mbed.org 0:5e1631496985 163 /**
root@mbed.org 0:5e1631496985 164 * Is the mbed board trying to use DHCP?
root@mbed.org 0:5e1631496985 165 */
root@mbed.org 0:5e1631496985 166 const bool &getUseDHCP() const { return dhcp; }
root@mbed.org 0:5e1631496985 167
root@mbed.org 0:5e1631496985 168 /**
root@mbed.org 0:5e1631496985 169 * Initialise the network environment. Set up all services.
root@mbed.org 0:5e1631496985 170 * Please call after configuration.
root@mbed.org 0:5e1631496985 171 */
root@mbed.org 0:5e1631496985 172 void init();
root@mbed.org 0:5e1631496985 173
root@mbed.org 0:5e1631496985 174 /**
root@mbed.org 0:5e1631496985 175 * Set the network interface up.
root@mbed.org 0:5e1631496985 176 * To enable the network interface after calling setDown()
root@mbed.org 0:5e1631496985 177 * Automaticly called from init().
root@mbed.org 0:5e1631496985 178 */
root@mbed.org 0:5e1631496985 179 void setUp() const;
root@mbed.org 0:5e1631496985 180
root@mbed.org 0:5e1631496985 181 /**
root@mbed.org 0:5e1631496985 182 * Set the network interface down.
root@mbed.org 0:5e1631496985 183 * To disable the network interface temporary.
root@mbed.org 0:5e1631496985 184 * To make the interface avalible again use setUp().
root@mbed.org 0:5e1631496985 185 */
root@mbed.org 0:5e1631496985 186 void setDown() const;
root@mbed.org 0:5e1631496985 187
root@mbed.org 0:5e1631496985 188 /**
root@mbed.org 0:5e1631496985 189 * Returns 1 if the network is up otherwise 0.
root@mbed.org 0:5e1631496985 190 */
root@mbed.org 0:5e1631496985 191 int isUp() const;
root@mbed.org 0:5e1631496985 192
root@mbed.org 0:5e1631496985 193 /**
root@mbed.org 0:5e1631496985 194 * This function waits until the network interface is Up.
root@mbed.org 0:5e1631496985 195 * To use to wait after init with DHCP. Helps continue work
root@mbed.org 0:5e1631496985 196 * after the network interface is completly up.
root@mbed.org 0:5e1631496985 197 */
root@mbed.org 0:5e1631496985 198 void waitUntilReady();
root@mbed.org 0:5e1631496985 199
root@mbed.org 0:5e1631496985 200 /**
root@mbed.org 0:5e1631496985 201 * Bind Callbackfunctions to a TCPPort.
root@mbed.org 0:5e1631496985 202 * It provides a clean lowlevel Interface to the TCPLayer.
root@mbed.org 0:5e1631496985 203 */
root@mbed.org 0:5e1631496985 204 TCPCallbackListener *bindTCPPort(u16_t, err_t (*)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const;
root@mbed.org 0:5e1631496985 205
root@mbed.org 0:5e1631496985 206 /**
root@mbed.org 0:5e1631496985 207 * Frees TCPItems because they cant do it directly.
root@mbed.org 0:5e1631496985 208 */
root@mbed.org 0:5e1631496985 209 void free(TCPItem *item) const;
root@mbed.org 0:5e1631496985 210
root@mbed.org 0:5e1631496985 211 static int time();
root@mbed.org 0:5e1631496985 212
root@mbed.org 0:5e1631496985 213 protected:
root@mbed.org 0:5e1631496985 214 void _poll() const;
root@mbed.org 0:5e1631496985 215
root@mbed.org 0:5e1631496985 216 /**
root@mbed.org 0:5e1631496985 217 * Default constructor tryes to bring the network interface up with dhcp.
root@mbed.org 0:5e1631496985 218 */
root@mbed.org 0:5e1631496985 219 NetServer();
root@mbed.org 0:5e1631496985 220
root@mbed.org 0:5e1631496985 221 /**
root@mbed.org 0:5e1631496985 222 * Constructor for fix ip setting
root@mbed.org 0:5e1631496985 223 */
root@mbed.org 0:5e1631496985 224 NetServer(struct ip_addr me_ip, struct ip_addr netmask, struct ip_addr gateway);
root@mbed.org 0:5e1631496985 225
root@mbed.org 0:5e1631496985 226 private:
root@mbed.org 0:5e1631496985 227 /**
root@mbed.org 0:5e1631496985 228 * This is a singleton class.
root@mbed.org 0:5e1631496985 229 * So we should not have a public copy constructor.
root@mbed.org 0:5e1631496985 230 */
root@mbed.org 0:5e1631496985 231 NetServer(NetServer const&) {}
root@mbed.org 0:5e1631496985 232 // NetServer &operator=(NetServer const&) {}
root@mbed.org 0:5e1631496985 233
root@mbed.org 0:5e1631496985 234 struct netif *netif;
root@mbed.org 0:5e1631496985 235 struct netif netif_data;
root@mbed.org 0:5e1631496985 236
root@mbed.org 0:5e1631496985 237 struct ip_addr ipaddr;
root@mbed.org 0:5e1631496985 238 struct ip_addr netmask;
root@mbed.org 0:5e1631496985 239 struct ip_addr gateway;
root@mbed.org 0:5e1631496985 240
root@mbed.org 0:5e1631496985 241 struct ip_addr firstdns;
root@mbed.org 0:5e1631496985 242 struct ip_addr seconddns;
root@mbed.org 0:5e1631496985 243
root@mbed.org 0:5e1631496985 244 bool dhcp;
root@mbed.org 0:5e1631496985 245
root@mbed.org 0:5e1631496985 246 list<TCPItem *> *del;
root@mbed.org 0:5e1631496985 247
root@mbed.org 0:5e1631496985 248 Ticker tickARP, /*eth_tick,*/ dns_tick, dhcp_coarse, dhcp_fine;
root@mbed.org 0:5e1631496985 249 const char *hostname;
root@mbed.org 0:5e1631496985 250 static NetServer *singleton;
root@mbed.org 0:5e1631496985 251 Timer _time;
root@mbed.org 0:5e1631496985 252 };
root@mbed.org 0:5e1631496985 253
root@mbed.org 0:5e1631496985 254 };
root@mbed.org 0:5e1631496985 255 #endif /* NETSERVER_H */