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