My fork of the HTTPServer (working)
Embed:
(wiki syntax)
Show/hide line numbers
NetServer.cpp
00001 #include "lwip/opt.h" 00002 #include "lwip/stats.h" 00003 #include "lwip/sys.h" 00004 #include "lwip/pbuf.h" 00005 #include "lwip/udp.h" 00006 #include "lwip/tcp.h" 00007 #include "lwip/dns.h" 00008 #include "lwip/dhcp.h " 00009 #include "lwip/init.h" 00010 #include "lwip/netif.h" 00011 #include "netif/etharp.h" 00012 #include "netif/loopif.h" 00013 #include "device.h" 00014 #include "Ethernet.h" 00015 00016 #include "NetServer.h" 00017 #include "TCPListener.h" 00018 #include "TCPCallbackListener.h" 00019 #include "TCPConnection.h" 00020 00021 using namespace std; 00022 using namespace mbed; 00023 00024 NetServer *NetServer::singleton = NULL; 00025 00026 NetServer::NetServer() : netif(&netif_data), dhcp(true), hostname(HOSTNAME) { 00027 IP4_ADDR(&netmask, 255,255,255,255); 00028 IP4_ADDR(&gateway, 0,0,0,0); 00029 IP4_ADDR(&ipaddr, 0,0,0,0); 00030 netif->hwaddr_len = 0; 00031 del = new list<TCPItem *>(); 00032 } 00033 00034 NetServer::NetServer(struct ip_addr ip, struct ip_addr nm, struct ip_addr gw) 00035 : netif(&netif_data), ipaddr(ip), netmask(nm), gateway(gw), dhcp(false), hostname(HOSTNAME) { 00036 netif->hwaddr_len = 0; 00037 del = new list<TCPItem *>(); 00038 } 00039 00040 NetServer::~NetServer() { 00041 00042 } 00043 00044 void NetServer::_poll() const { 00045 while(!del->empty()) { 00046 TCPItem *item = del->front(); 00047 del->pop_front(); 00048 delete item; 00049 } 00050 device_poll(); 00051 tcp_tmr(); 00052 } 00053 00054 void NetServer::init() { 00055 lwip_init(); 00056 00057 netif->hwaddr_len = ETHARP_HWADDR_LEN; 00058 device_address((char *)netif->hwaddr); 00059 00060 netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); 00061 netif->hostname = this->hostname; 00062 netif_set_default(netif); 00063 if(!dhcp) { 00064 netif_set_up(netif); 00065 } else { 00066 dhcp_start(netif); 00067 } 00068 00069 tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); 00070 //eth_tick.attach_us<NetServer>(this,&emac_tmr, TCP_FAST_INTERVAL * 1000); 00071 dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); 00072 if(dhcp) { 00073 dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); 00074 dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); 00075 } 00076 } 00077 00078 void NetServer::setUp() const { 00079 netif_set_up(netif); 00080 } 00081 00082 void NetServer::setDown() const { 00083 netif_set_down(netif); 00084 } 00085 00086 void NetServer::waitUntilReady() { 00087 while(!netif_is_up(netif)) { 00088 _poll(); 00089 wait_ms(1); 00090 } 00091 ipaddr = netif->ip_addr; 00092 printf("IP: %hhu.%hhu.%hhu.%hhu\n", (ipaddr.addr)&0xFF, (ipaddr.addr>>8)&0xFF, (ipaddr.addr>>16)&0xFF, (ipaddr.addr>>24)&0xFF); 00093 } 00094 00095 TCPCallbackListener *NetServer::bindTCPPort(u16_t port, err_t (*accept)(TCPCallbackListener *, struct tcp_pcb *, err_t)) const { 00096 TCPCallbackListener *listener = new TCPCallbackListener(port, accept); 00097 listener->bind(); 00098 return listener; 00099 } 00100 00101 void NetServer::free(TCPItem *item) const { 00102 del->push_back(item); 00103 }
Generated on Tue Jul 12 2022 19:24:05 by 1.7.2