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.cpp@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 | #include "lwip/opt.h" |
RodColeman | 0:0791c1fece8e | 2 | #include "lwip/stats.h" |
RodColeman | 0:0791c1fece8e | 3 | #include "lwip/sys.h" |
RodColeman | 0:0791c1fece8e | 4 | #include "lwip/pbuf.h" |
RodColeman | 0:0791c1fece8e | 5 | #include "lwip/udp.h" |
RodColeman | 0:0791c1fece8e | 6 | #include "lwip/tcp.h" |
RodColeman | 0:0791c1fece8e | 7 | #include "lwip/dns.h" |
RodColeman | 0:0791c1fece8e | 8 | #include "lwip/dhcp.h" |
RodColeman | 0:0791c1fece8e | 9 | #include "lwip/init.h" |
RodColeman | 0:0791c1fece8e | 10 | #include "lwip/netif.h" |
RodColeman | 0:0791c1fece8e | 11 | #include "netif/etharp.h" |
RodColeman | 0:0791c1fece8e | 12 | #include "netif/loopif.h" |
RodColeman | 0:0791c1fece8e | 13 | #include "device.h" |
RodColeman | 0:0791c1fece8e | 14 | #include "Ethernet.h" |
RodColeman | 0:0791c1fece8e | 15 | |
RodColeman | 0:0791c1fece8e | 16 | #include "NetServer.h" |
RodColeman | 0:0791c1fece8e | 17 | #include "TCPListener.h" |
RodColeman | 0:0791c1fece8e | 18 | #include "TCPCallbackListener.h" |
RodColeman | 0:0791c1fece8e | 19 | #include "TCPConnection.h" |
RodColeman | 0:0791c1fece8e | 20 | |
RodColeman | 0:0791c1fece8e | 21 | using namespace std; |
RodColeman | 0:0791c1fece8e | 22 | using namespace mbed; |
RodColeman | 0:0791c1fece8e | 23 | |
RodColeman | 0:0791c1fece8e | 24 | NetServer *NetServer::singleton = NULL; |
RodColeman | 0:0791c1fece8e | 25 | |
RodColeman | 0:0791c1fece8e | 26 | NetServer::NetServer() : netif(&netif_data), dhcp(true), hostname(HOSTNAME) { |
RodColeman | 0:0791c1fece8e | 27 | _time.start(); |
RodColeman | 0:0791c1fece8e | 28 | IP4_ADDR(&netmask, 255,255,255,255); |
RodColeman | 0:0791c1fece8e | 29 | IP4_ADDR(&gateway, 0,0,0,0); |
RodColeman | 0:0791c1fece8e | 30 | IP4_ADDR(&ipaddr, 0,0,0,0); |
RodColeman | 0:0791c1fece8e | 31 | netif->hwaddr_len = 0; |
RodColeman | 0:0791c1fece8e | 32 | del = new list<TCPItem *>(); |
RodColeman | 0:0791c1fece8e | 33 | } |
RodColeman | 0:0791c1fece8e | 34 | |
RodColeman | 0:0791c1fece8e | 35 | NetServer::NetServer(struct ip_addr ip, struct ip_addr nm, struct ip_addr gw) |
RodColeman | 0:0791c1fece8e | 36 | : netif(&netif_data), ipaddr(ip), netmask(nm), gateway(gw), dhcp(false), hostname(HOSTNAME) { |
RodColeman | 0:0791c1fece8e | 37 | _time.start(); |
RodColeman | 0:0791c1fece8e | 38 | netif->hwaddr_len = 0; |
RodColeman | 0:0791c1fece8e | 39 | del = new list<TCPItem *>(); |
RodColeman | 0:0791c1fece8e | 40 | } |
RodColeman | 0:0791c1fece8e | 41 | |
RodColeman | 0:0791c1fece8e | 42 | NetServer::~NetServer() { |
RodColeman | 0:0791c1fece8e | 43 | |
RodColeman | 0:0791c1fece8e | 44 | } |
RodColeman | 0:0791c1fece8e | 45 | |
RodColeman | 0:0791c1fece8e | 46 | void NetServer::_poll() const { |
RodColeman | 0:0791c1fece8e | 47 | while(!del->empty()) { |
RodColeman | 0:0791c1fece8e | 48 | TCPItem *item = del->front(); |
RodColeman | 0:0791c1fece8e | 49 | del->pop_front(); |
RodColeman | 0:0791c1fece8e | 50 | delete item; |
RodColeman | 0:0791c1fece8e | 51 | } |
RodColeman | 0:0791c1fece8e | 52 | device_poll(); |
RodColeman | 0:0791c1fece8e | 53 | tcp_tmr(); |
RodColeman | 0:0791c1fece8e | 54 | } |
RodColeman | 0:0791c1fece8e | 55 | |
RodColeman | 0:0791c1fece8e | 56 | void NetServer::init() { |
RodColeman | 0:0791c1fece8e | 57 | lwip_init(); |
RodColeman | 0:0791c1fece8e | 58 | |
RodColeman | 0:0791c1fece8e | 59 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
RodColeman | 0:0791c1fece8e | 60 | device_address((char *)netif->hwaddr); |
RodColeman | 0:0791c1fece8e | 61 | |
RodColeman | 0:0791c1fece8e | 62 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
RodColeman | 0:0791c1fece8e | 63 | netif->hostname = (char *)this->hostname; |
RodColeman | 0:0791c1fece8e | 64 | netif_set_default(netif); |
RodColeman | 0:0791c1fece8e | 65 | if(!dhcp) { |
RodColeman | 0:0791c1fece8e | 66 | netif_set_up(netif); |
RodColeman | 0:0791c1fece8e | 67 | } else { |
RodColeman | 0:0791c1fece8e | 68 | dhcp_start(netif); |
RodColeman | 0:0791c1fece8e | 69 | } |
RodColeman | 0:0791c1fece8e | 70 | |
RodColeman | 0:0791c1fece8e | 71 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
RodColeman | 0:0791c1fece8e | 72 | //eth_tick.attach_us<NetServer>(this,&emac_tmr, TCP_FAST_INTERVAL * 1000); |
RodColeman | 0:0791c1fece8e | 73 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
RodColeman | 0:0791c1fece8e | 74 | if(dhcp) { |
RodColeman | 0:0791c1fece8e | 75 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
RodColeman | 0:0791c1fece8e | 76 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
RodColeman | 0:0791c1fece8e | 77 | } |
RodColeman | 0:0791c1fece8e | 78 | } |
RodColeman | 0:0791c1fece8e | 79 | |
RodColeman | 0:0791c1fece8e | 80 | void NetServer::setUp() const { |
RodColeman | 0:0791c1fece8e | 81 | netif_set_up(netif); |
RodColeman | 0:0791c1fece8e | 82 | } |
RodColeman | 0:0791c1fece8e | 83 | |
RodColeman | 0:0791c1fece8e | 84 | void NetServer::setDown() const { |
RodColeman | 0:0791c1fece8e | 85 | netif_set_down(netif); |
RodColeman | 0:0791c1fece8e | 86 | } |
RodColeman | 0:0791c1fece8e | 87 | |
RodColeman | 0:0791c1fece8e | 88 | int NetServer::isUp() const { |
RodColeman | 0:0791c1fece8e | 89 | return netif_is_up(netif); |
RodColeman | 0:0791c1fece8e | 90 | } |
RodColeman | 0:0791c1fece8e | 91 | |
RodColeman | 0:0791c1fece8e | 92 | void NetServer::waitUntilReady() { |
RodColeman | 0:0791c1fece8e | 93 | while(!netif_is_up(netif)) { |
RodColeman | 0:0791c1fece8e | 94 | _poll(); |
RodColeman | 0:0791c1fece8e | 95 | wait_ms(1); |
RodColeman | 0:0791c1fece8e | 96 | } |
RodColeman | 0:0791c1fece8e | 97 | ipaddr = netif->ip_addr; |
RodColeman | 0:0791c1fece8e | 98 | printf("IP: %hhu.%hhu.%hhu.%hhu\n", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF); |
RodColeman | 0:0791c1fece8e | 99 | } |
RodColeman | 0:0791c1fece8e | 100 | |
RodColeman | 0:0791c1fece8e | 101 | TCPCallbackListener *NetServer::bindTCPPort(u16_t port, err_t (*accept)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const { |
RodColeman | 0:0791c1fece8e | 102 | TCPCallbackListener *listener = new TCPCallbackListener(port, accept); |
RodColeman | 0:0791c1fece8e | 103 | listener->bind(); |
RodColeman | 0:0791c1fece8e | 104 | return listener; |
RodColeman | 0:0791c1fece8e | 105 | } |
RodColeman | 0:0791c1fece8e | 106 | |
RodColeman | 0:0791c1fece8e | 107 | void NetServer::free(TCPItem *item) const { |
RodColeman | 0:0791c1fece8e | 108 | del->push_back(item); |
RodColeman | 0:0791c1fece8e | 109 | } |
RodColeman | 0:0791c1fece8e | 110 | |
RodColeman | 0:0791c1fece8e | 111 | int NetServer::time() { |
RodColeman | 0:0791c1fece8e | 112 | return NetServer::get()->_time.read_ms(); |
RodColeman | 0:0791c1fece8e | 113 | } |