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