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