diff --git a/UIPEthernet/src/UIPClient.cpp b/UIPEthernet/src/UIPClient.cpp index 4830b9d..aa2ac10 100644 --- a/UIPEthernet/src/UIPClient.cpp +++ b/UIPEthernet/src/UIPClient.cpp @@ -57,7 +57,7 @@ UIPClient::connect(IPAddress ip, uint16_t port) { while((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) { - UIPEthernet.tick(); + UIPEthernetClass::tick(); if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) { data = (uip_userdata_t*) conn->appstate; @@ -83,7 +83,7 @@ UIPClient::connect(const char *host, uint16_t port) DNSClient dns; IPAddress remote_addr; - dns.begin(UIPEthernet.dnsServerIP()); + dns.begin(UIPEthernetClass::_dnsServerAddress); ret = dns.getHostByName(host, remote_addr); if (ret == 1) { return connect(remote_addr, port); @@ -122,7 +122,7 @@ UIPClient::stop() } #endif data = NULL; - UIPEthernet.tick(); + UIPEthernetClass::tick(); } uint8_t @@ -139,7 +139,7 @@ UIPClient::operator==(const UIPClient& rhs) UIPClient::operator bool() { - UIPEthernet.tick(); + UIPEthernetClass::tick(); return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in[0] != NOBLOCK); } @@ -164,15 +164,15 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size) uint16_t attempts = UIP_ATTEMPTS_ON_WRITE; #endif repeat: - UIPEthernet.tick(); + UIPEthernetClass::tick(); if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) { - memhandle* p = _currentBlock(&u->packets_out[0]); - if (*p == NOBLOCK) + uint8_t p = _currentBlock(&u->packets_out[0]); + if (u->packets_out[p] == NOBLOCK) { newpacket: - *p = UIPEthernet.network.allocBlock(UIP_SOCKET_DATALEN); - if (*p == NOBLOCK) + u->packets_out[p] = UIPEthernetClass::network.allocBlock(UIP_SOCKET_DATALEN); + if (u->packets_out[p] == NOBLOCK) { #if UIP_ATTEMPTS_ON_WRITE > 0 if ((--attempts)>0) @@ -186,7 +186,7 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size) } #ifdef UIPETHERNET_DEBUG_CLIENT Serial.print(F("UIPClient.write: writePacket(")); - Serial.print(*p); + Serial.print(u->packets_out[p]); Serial.print(F(") pos: ")); Serial.print(u->out_pos); Serial.print(F(", buf[")); @@ -197,12 +197,12 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size) Serial.write((uint8_t*)buf+size-remain,remain); Serial.println(F("'")); #endif - written = UIPEthernet.network.writePacket(*p,u->out_pos,(uint8_t*)buf+size-remain,remain); + written = UIPEthernetClass::network.writePacket(u->packets_out[p],u->out_pos,(uint8_t*)buf+size-remain,remain); remain -= written; u->out_pos+=written; if (remain > 0) { - if (p==&u->packets_out[UIP_SOCKET_NUMPACKETS-1]) + if (p == UIP_SOCKET_NUMPACKETS-1) { #if UIP_ATTEMPTS_ON_WRITE > 0 if ((--attempts)>0) @@ -232,15 +232,10 @@ UIPClient::available() int UIPClient::_available(uip_userdata_t *u) { - memhandle* p = &u->packets_in[0]; - if (*p == NOBLOCK) - return 0; int len = 0; - for(memhandle* end = p+UIP_SOCKET_NUMPACKETS; p < end; p++) + for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { - if(*p == NOBLOCK) - break; - len += UIPEthernet.network.blockSize(*p); + len += UIPEthernetClass::network.blockSize(u->packets_in[i]); } return len; } @@ -250,21 +245,20 @@ UIPClient::read(uint8_t *buf, size_t size) { if (*this) { - int remain = size; - memhandle* p = &data->packets_in[0]; - if (*p == NOBLOCK) + uint16_t remain = size; + if (data->packets_in[0] == NOBLOCK) return 0; - int read; + uint16_t read; do { - read = UIPEthernet.network.readPacket(*p,0,buf+size-remain,remain); - if (read == UIPEthernet.network.blockSize(*p)) + read = UIPEthernetClass::network.readPacket(data->packets_in[0],0,buf+size-remain,remain); + if (read == UIPEthernetClass::network.blockSize(data->packets_in[0])) { remain -= read; - _eatBlock(p); + _eatBlock(&data->packets_in[0]); if (uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS]) && !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) data->state |= UIP_CLIENT_RESTART; - if (*p == NOBLOCK) + if (data->packets_in[0] == NOBLOCK) { if (data->state & UIP_CLIENT_REMOTECLOSED) { @@ -276,7 +270,7 @@ UIPClient::read(uint8_t *buf, size_t size) } else { - UIPEthernet.network.resizeBlock(*p,read); + UIPEthernetClass::network.resizeBlock(data->packets_in[0],read); break; } } @@ -300,11 +294,10 @@ UIPClient::peek() { if (*this) { - memhandle p = data->packets_in[0]; - if (p != NOBLOCK) + if (data->packets_in[0] != NOBLOCK) { uint8_t c; - UIPEthernet.network.readPacket(p,0,&c,1); + UIPEthernetClass::network.readPacket(data->packets_in[0],0,&c,1); return c; } } @@ -360,31 +353,22 @@ UIPClient::uip_callback() #endif if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED))) { - memhandle newPacket = UIPEthernet.network.allocBlock(uip_len); + memhandle newPacket = UIPEthernetClass::network.allocBlock(uip_len); if (newPacket != NOBLOCK) { - memhandle* p = _currentBlock(&u->packets_in[0]); - //if it's not the first packet - if (*p != NOBLOCK) + for (uint8_t i=0; i < UIP_SOCKET_NUMPACKETS; i++) { - uint8_t slot = p - &u->packets_in[0]; - if (slot < UIP_SOCKET_NUMPACKETS-1) - p++; - //if this is the last slot stop this connection - if (slot >= UIP_SOCKET_NUMPACKETS-2) + if (u->packets_in[i] == NOBLOCK) { - uip_stop(); - //if there's no free slot left omit loosing this packet and (again) stop this connection - if (slot == UIP_SOCKET_NUMPACKETS-1) - goto reject_newdata; + if (i == UIP_SOCKET_NUMPACKETS-1) + uip_stop(); + UIPEthernetClass::network.copyPacket(newPacket,0,UIPEthernetClass::in_packet,((uint8_t*)uip_appdata)-uip_buf,uip_len); + u->packets_in[i] = newPacket; + goto finish_newdata; } } - UIPEthernet.network.copyPacket(newPacket,0,UIPEthernet.in_packet,((uint8_t*)uip_appdata)-uip_buf,uip_len); - *p = newPacket; - goto finish_newdata; } -reject_newdata: - UIPEthernet.packetstate &= ~UIPETHERNET_FREEPACKET; + UIPEthernetClass::packetstate &= ~UIPETHERNET_FREEPACKET; uip_stop(); } } @@ -430,27 +414,26 @@ UIPClient::uip_callback() #ifdef UIPETHERNET_DEBUG_CLIENT //Serial.println(F("UIPClient uip_poll")); #endif - memhandle p = u->packets_out[0]; - if (p != NOBLOCK) + if (u->packets_out[0] != NOBLOCK) { if (u->packets_out[1] == NOBLOCK) { uip_len = u->out_pos; if (uip_len > 0) { - UIPEthernet.network.resizeBlock(p,0,uip_len); + UIPEthernetClass::network.resizeBlock(u->packets_out[0],0,uip_len); } } else - uip_len = UIPEthernet.network.blockSize(p); + uip_len = UIPEthernetClass::network.blockSize(u->packets_out[0]); if (uip_len > 0) { - UIPEthernet.uip_hdrlen = ((uint8_t*)uip_appdata)-uip_buf; - UIPEthernet.uip_packet = UIPEthernet.network.allocBlock(UIPEthernet.uip_hdrlen+uip_len); - if (UIPEthernet.uip_packet != NOBLOCK) + UIPEthernetClass::uip_hdrlen = ((uint8_t*)uip_appdata)-uip_buf; + UIPEthernetClass::uip_packet = UIPEthernetClass::network.allocBlock(UIPEthernetClass::uip_hdrlen+uip_len); + if (UIPEthernetClass::uip_packet != NOBLOCK) { - UIPEthernet.network.copyPacket(UIPEthernet.uip_packet,UIPEthernet.uip_hdrlen,p,0,uip_len); - UIPEthernet.packetstate |= UIPETHERNET_SENDPACKET; + UIPEthernetClass::network.copyPacket(UIPEthernetClass::uip_packet,UIPEthernetClass::uip_hdrlen,u->packets_out[0],0,uip_len); + UIPEthernetClass::packetstate |= UIPETHERNET_SENDPACKET; uip_send(uip_appdata,uip_len); } return; @@ -484,7 +467,7 @@ UIPClient::uip_callback() } } nodata: - UIPEthernet.uip_packet = NOBLOCK; + UIPEthernetClass::uip_packet = NOBLOCK; uip_len=0; } @@ -504,13 +487,15 @@ UIPClient::_allocateData() return NULL; } -memhandle* +uint8_t UIPClient::_currentBlock(memhandle* block) { - for(memhandle* end = block+UIP_SOCKET_NUMPACKETS-1; block < end; block++) - if(*(block+1) == NOBLOCK) - break; - return block; + for (uint8_t i = 1; i < UIP_SOCKET_NUMPACKETS; i++) + { + if (block[i] == NOBLOCK) + return i-1; + } + return UIP_SOCKET_NUMPACKETS-1; } void @@ -528,11 +513,12 @@ UIPClient::_eatBlock(memhandle* block) } Serial.print(F("-> ")); #endif - memhandle* end = block+(UIP_SOCKET_NUMPACKETS-1); - UIPEthernet.network.freeBlock(*block); - while(block < end) - *block = *((block++)+1); - *end = NOBLOCK; + UIPEthernetClass::network.freeBlock(block[0]); + for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS-1; i++) + { + block[i] = block[i+1]; + } + block[UIP_SOCKET_NUMPACKETS-1] = NOBLOCK; #ifdef UIPETHERNET_DEBUG_CLIENT for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { @@ -546,15 +532,9 @@ UIPClient::_eatBlock(memhandle* block) void UIPClient::_flushBlocks(memhandle* block) { - for(memhandle* end = block+UIP_SOCKET_NUMPACKETS; block < end; block++) + for (uint8_t i = 0; i < UIP_SOCKET_NUMPACKETS; i++) { - if(*block != NOBLOCK) - { - UIPEthernet.network.freeBlock(*block); - *block = NOBLOCK; - } - else - break; + UIPEthernetClass::network.freeBlock(block[i]); } } diff --git a/UIPEthernet/src/UIPClient.h b/UIPEthernet/src/UIPClient.h index 89f9930..9c57e38 100644 --- a/UIPEthernet/src/UIPClient.h +++ b/UIPEthernet/src/UIPClient.h @@ -88,7 +88,7 @@ class UIPClient : public Client { static size_t _write(uip_userdata_t *,const uint8_t *buf, size_t size); static int _available(uip_userdata_t *); - static memhandle * _currentBlock(memhandle* blocks); + static uint8_t _currentBlock(memhandle* blocks); static void _eatBlock(memhandle* blocks); static void _flushBlocks(memhandle* blocks); diff --git a/UIPEthernet/src/UIPEthernet.cpp b/UIPEthernet/src/UIPEthernet.cpp index 56c05e7..5413b6d 100644 --- a/UIPEthernet/src/UIPEthernet.cpp +++ b/UIPEthernet/src/UIPEthernet.cpp @@ -37,18 +37,26 @@ extern "C" #define ETH_HDR ((struct uip_eth_hdr *)&uip_buf[0]) +memhandle UIPEthernetClass::in_packet(NOBLOCK); +memhandle UIPEthernetClass::uip_packet(NOBLOCK); +uint8_t UIPEthernetClass::uip_hdrlen(0); +uint8_t UIPEthernetClass::packetstate(0); + +IPAddress UIPEthernetClass::_dnsServerAddress; +DhcpClass* UIPEthernetClass::_dhcp(NULL); + +struct uip_timer UIPEthernetClass::periodic_timer; + +Enc28J60Network UIPEthernetClass::network; + // Because uIP isn't encapsulated within a class we have to use global // variables, so we can only have one TCP/IP stack per program. -UIPEthernetClass::UIPEthernetClass() : - in_packet(NOBLOCK), - uip_packet(NOBLOCK), - uip_hdrlen(0), - packetstate(0), - _dhcp(NULL) +UIPEthernetClass::UIPEthernetClass() { } +#if UIP_UDP int UIPEthernetClass::begin(const uint8_t* mac) { @@ -68,6 +76,7 @@ UIPEthernetClass::begin(const uint8_t* mac) } return ret; } +#endif void UIPEthernetClass::begin(const uint8_t* mac, IPAddress ip) @@ -102,6 +111,7 @@ UIPEthernetClass::begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddre int UIPEthernetClass::maintain(){ tick(); int rc = DHCP_CHECK_NONE; +#if UIP_UDP if(_dhcp != NULL){ //we have a pointer to dhcp, use it rc = _dhcp->checkLease(); @@ -120,6 +130,7 @@ int UIPEthernetClass::maintain(){ } } return rc; +#endif } IPAddress UIPEthernetClass::localIP() @@ -277,7 +288,7 @@ boolean UIPEthernetClass::network_send() } void UIPEthernetClass::init(const uint8_t* mac) { - uip_timer_set(&this->periodic_timer, CLOCK_SECOND / 4); + uip_timer_set(&periodic_timer, CLOCK_SECOND / 4); network.init((uint8_t*)mac); uip_seteth_addr(mac); @@ -348,7 +359,11 @@ UIPEthernetClass::ipchksum(void) /*---------------------------------------------------------------------------*/ uint16_t +#if UIP_UDP UIPEthernetClass::upper_layer_chksum(uint8_t proto) +#else +uip_tcpchksum(void) +#endif { uint16_t upper_layer_len; uint16_t sum; @@ -362,27 +377,34 @@ UIPEthernetClass::upper_layer_chksum(uint8_t proto) /* First sum pseudoheader. */ /* IP protocol and length fields. This addition cannot carry. */ +#if UIP_UDP sum = upper_layer_len + proto; +#else + sum = upper_layer_len + UIP_PROTO_TCP; +#endif /* Sum IP source and destination addresses. */ - sum = chksum(sum, (u8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t)); + sum = UIPEthernetClass::chksum(sum, (u8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t)); uint8_t upper_layer_memlen; +#if UIP_UDP switch(proto) { - case UIP_PROTO_ICMP: - case UIP_PROTO_ICMP6: - upper_layer_memlen = upper_layer_len; +// case UIP_PROTO_ICMP: +// case UIP_PROTO_ICMP6: +// upper_layer_memlen = upper_layer_len; +// break; + case UIP_PROTO_UDP: + upper_layer_memlen = UIP_UDPH_LEN; break; - case UIP_PROTO_TCP: + default: +// case UIP_PROTO_TCP: +#endif upper_layer_memlen = (BUF->tcpoffset >> 4) << 2; - break; #if UIP_UDP - case UIP_PROTO_UDP: - upper_layer_memlen = UIP_UDPH_LEN; break; -#endif } - sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], upper_layer_memlen); +#endif + sum = UIPEthernetClass::chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], upper_layer_memlen); #ifdef UIPETHERNET_DEBUG_CHKSUM Serial.print(F("chksum uip_buf[")); Serial.print(UIP_IPH_LEN + UIP_LLH_LEN); @@ -393,9 +415,9 @@ UIPEthernetClass::upper_layer_chksum(uint8_t proto) #endif if (upper_layer_memlen < upper_layer_len) { - sum = network.chksum( + sum = UIPEthernetClass::network.chksum( sum, - uip_packet, + UIPEthernetClass::uip_packet, UIP_IPH_LEN + UIP_LLH_LEN + upper_layer_memlen, upper_layer_len - upper_layer_memlen ); @@ -419,6 +441,7 @@ uip_ipchksum(void) return UIPEthernet.ipchksum(); } +#if UIP_UDP uint16_t uip_tcpchksum(void) { @@ -426,7 +449,6 @@ uip_tcpchksum(void) return sum; } -#if UIP_UDP uint16_t uip_udpchksum(void) { diff --git a/UIPEthernet/src/UIPEthernet.h b/UIPEthernet/src/UIPEthernet.h index a00d3f7..561bf8a 100644 --- a/UIPEthernet/src/UIPEthernet.h +++ b/UIPEthernet/src/UIPEthernet.h @@ -82,24 +82,24 @@ class UIPEthernetClass IPAddress dnsServerIP(); private: - IPAddress _dnsServerAddress; - DhcpClass* _dhcp; + static memhandle in_packet; + static memhandle uip_packet; + static uint8_t uip_hdrlen; + static uint8_t packetstate; + + static IPAddress _dnsServerAddress; + static DhcpClass* _dhcp; - struct uip_timer periodic_timer; + static struct uip_timer periodic_timer; - memhandle in_packet; - memhandle uip_packet; - uint8_t uip_hdrlen; - uint8_t packetstate; + static Enc28J60Network network; - Enc28J60Network network; + static void init(const uint8_t* mac); + static void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet); - void init(const uint8_t* mac); - void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet); + static void tick(); - void tick(); - - boolean network_send(); + static boolean network_send(); friend class UIPServer; @@ -109,8 +109,9 @@ class UIPEthernetClass static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len); static uint16_t ipchksum(void); - uint16_t upper_layer_chksum(uint8_t proto); - +#if UIP_UDP + static uint16_t upper_layer_chksum(uint8_t proto); +#endif friend uint16_t uip_ipchksum(void); friend uint16_t uip_tcpchksum(void); friend uint16_t uip_udpchksum(void); diff --git a/UIPEthernet/src/UIPServer.cpp b/UIPEthernet/src/UIPServer.cpp index e6c9a81..21c7ef5 100644 --- a/UIPEthernet/src/UIPServer.cpp +++ b/UIPEthernet/src/UIPServer.cpp @@ -28,7 +28,7 @@ UIPServer::UIPServer(uint16_t port) : _port(htons(port)) UIPClient UIPServer::available() { - UIPEthernet.tick(); + UIPEthernetClass::tick(); for ( uip_userdata_t* data = &UIPClient::all_data[0]; data < &UIPClient::all_data[UIP_CONNS]; data++ ) { if (data->packets_in[0] != NOBLOCK @@ -42,7 +42,7 @@ UIPClient UIPServer::available() void UIPServer::begin() { uip_listen(_port); - UIPEthernet.tick(); + UIPEthernetClass::tick(); } size_t UIPServer::write(uint8_t c) diff --git a/UIPEthernet/src/UIPUdp.cpp b/UIPEthernet/src/UIPUdp.cpp index d38de84..f139a37 100644 --- a/UIPEthernet/src/UIPUdp.cpp +++ b/UIPEthernet/src/UIPUdp.cpp @@ -65,28 +65,13 @@ UIPUDP::stop() { if (_uip_udp_conn) { - flush(); uip_udp_remove(_uip_udp_conn); _uip_udp_conn->appstate = NULL; _uip_udp_conn=NULL; - if (appdata.packet_in != NOBLOCK) - { - UIPEthernet.network.freeBlock(appdata.packet_in); - appdata.packet_in = NOBLOCK; - } - uint8_t i = 0; - memhandle* packet = &appdata.packets_in[0]; - while (*packet != NOBLOCK && i < UIP_UDP_NUMPACKETS) - { - UIPEthernet.network.freeBlock(*packet); - *packet++ = NOBLOCK; - i++; - } - if (appdata.packet_out != NOBLOCK) - { - UIPEthernet.network.freeBlock(appdata.packet_out); - appdata.packet_out = NOBLOCK; - } + UIPEthernetClass::network.freeBlock(appdata.packet_in); + UIPEthernetClass::network.freeBlock(appdata.packet_next); + UIPEthernetClass::network.freeBlock(appdata.packet_out); + memset(&appdata,0,sizeof(appdata)); } } @@ -97,7 +82,7 @@ UIPUDP::stop() int UIPUDP::beginPacket(IPAddress ip, uint16_t port) { - UIPEthernet.tick(); + UIPEthernetClass::tick(); if (ip && port) { uip_ipaddr_t ripaddr; @@ -139,7 +124,7 @@ UIPUDP::beginPacket(IPAddress ip, uint16_t port) { if (appdata.packet_out == NOBLOCK) { - appdata.packet_out = UIPEthernet.network.allocBlock(UIP_UDP_MAXPACKETSIZE); + appdata.packet_out = UIPEthernetClass::network.allocBlock(UIP_UDP_MAXPACKETSIZE); appdata.out_pos = UIP_UDP_PHYH_LEN; if (appdata.packet_out != NOBLOCK) return 1; @@ -183,7 +168,7 @@ UIPUDP::endPacket() if (_uip_udp_conn && appdata.packet_out != NOBLOCK) { appdata.send = true; - UIPEthernet.network.resizeBlock(appdata.packet_out,0,appdata.out_pos); + UIPEthernetClass::network.resizeBlock(appdata.packet_out,0,appdata.out_pos); uip_udp_periodic_conn(_uip_udp_conn); if (uip_len > 0) { @@ -207,7 +192,7 @@ UIPUDP::write(const uint8_t *buffer, size_t size) { if (appdata.packet_out != NOBLOCK) { - size_t ret = UIPEthernet.network.writePacket(appdata.packet_out,appdata.out_pos,(uint8_t*)buffer,size); + size_t ret = UIPEthernetClass::network.writePacket(appdata.packet_out,appdata.out_pos,(uint8_t*)buffer,size); appdata.out_pos += ret; return ret; } @@ -219,61 +204,43 @@ UIPUDP::write(const uint8_t *buffer, size_t size) int UIPUDP::parsePacket() { - UIPEthernet.tick(); + UIPEthernetClass::tick(); +#ifdef UIPETHERNET_DEBUG_UDP if (appdata.packet_in != NOBLOCK) { -#ifdef UIPETHERNET_DEBUG_UDP Serial.print(F("udp parsePacket freeing previous packet: ")); Serial.println(appdata.packet_in); -#endif; - UIPEthernet.network.freeBlock(appdata.packet_in); } - memhandle *packet = &appdata.packets_in[0]; - appdata.packet_in = *packet; +#endif + UIPEthernetClass::network.freeBlock(appdata.packet_in); + + appdata.packet_in = appdata.packet_next; + appdata.packet_next = NOBLOCK; + +#ifdef UIPETHERNET_DEBUG_UDP if (appdata.packet_in != NOBLOCK) { -#ifdef UIPETHERNET_DEBUG_UDP Serial.print(F("udp parsePacket received packet: ")); Serial.print(appdata.packet_in); + } #endif - if (UIP_UDP_NUMPACKETS > 1) - { - uint8_t i = 1; - memhandle* p = packet+1; -freeloop: - *packet = *p; - if (*packet == NOBLOCK) - goto freeready; - packet++; - if (i < UIP_UDP_NUMPACKETS-1) - { - i++; - p++; - goto freeloop; - } - } - *packet = NOBLOCK; -freeready: - int size = UIPEthernet.network.blockSize(appdata.packet_in); + int size = UIPEthernetClass::network.blockSize(appdata.packet_in); #ifdef UIPETHERNET_DEBUG_UDP + if (appdata.packet_in != NOBLOCK) + { Serial.print(F(", size: ")); Serial.println(size); -#endif - return size; } - return 0; +#endif + return size; } // Number of bytes remaining in the current packet int UIPUDP::available() { - UIPEthernet.tick(); - if (appdata.packet_in != NOBLOCK) - { - return UIPEthernet.network.blockSize(appdata.packet_in); - } - return 0; + UIPEthernetClass::tick(); + return UIPEthernetClass::network.blockSize(appdata.packet_in); } // Read a single byte from the current packet @@ -293,11 +260,11 @@ UIPUDP::read() int UIPUDP::read(unsigned char* buffer, size_t len) { - UIPEthernet.tick(); + UIPEthernetClass::tick(); if (appdata.packet_in != NOBLOCK) { - int read = UIPEthernet.network.readPacket(appdata.packet_in,0,buffer,len); - UIPEthernet.network.resizeBlock(appdata.packet_in,read); + int read = UIPEthernetClass::network.readPacket(appdata.packet_in,0,buffer,len); + UIPEthernetClass::network.resizeBlock(appdata.packet_in,read); return read; } return 0; @@ -307,11 +274,11 @@ UIPUDP::read(unsigned char* buffer, size_t len) int UIPUDP::peek() { - UIPEthernet.tick(); + UIPEthernetClass::tick(); if (appdata.packet_in != NOBLOCK) { unsigned char c; - if (UIPEthernet.network.readPacket(appdata.packet_in,0,&c,1) == 1) + if (UIPEthernetClass::network.readPacket(appdata.packet_in,0,&c,1) == 1) return c; } return -1; @@ -321,12 +288,9 @@ UIPUDP::peek() void UIPUDP::flush() { - UIPEthernet.tick(); - if (appdata.packet_in != NOBLOCK) - { - UIPEthernet.network.freeBlock(appdata.packet_in); - appdata.packet_in = NOBLOCK; - } + UIPEthernetClass::tick(); + UIPEthernetClass::network.freeBlock(appdata.packet_in); + appdata.packet_in = NOBLOCK; } // Return the IP address of the host who sent the current incoming packet @@ -356,35 +320,24 @@ UIPUDP::uip_callback() { { if (uip_newdata()) { - uip_udp_conn->rport = UDPBUF->srcport; - uip_ipaddr_copy(uip_udp_conn->ripaddr,UDPBUF->srcipaddr); - memhandle *packet = &data->packets_in[0]; - uint8_t i = 0; - do + if (data->packet_next == NOBLOCK) { - if (*packet == NOBLOCK) - { - *packet = UIPEthernet.network.allocBlock(ntohs(UDPBUF->udplen)-UIP_UDPH_LEN); + uip_udp_conn->rport = UDPBUF->srcport; + uip_ipaddr_copy(uip_udp_conn->ripaddr,UDPBUF->srcipaddr); + data->packet_next = UIPEthernetClass::network.allocBlock(ntohs(UDPBUF->udplen)-UIP_UDPH_LEN); //if we are unable to allocate memory the packet is dropped. udp doesn't guarantee packet delivery - if (*packet != NOBLOCK) - { - //discard Linklevel and IP and udp-header and any trailing bytes: - UIPEthernet.network.copyPacket(*packet,0,UIPEthernet.in_packet,UIP_UDP_PHYH_LEN,UIPEthernet.network.blockSize(*packet)); + if (data->packet_next != NOBLOCK) + { + //discard Linklevel and IP and udp-header and any trailing bytes: + UIPEthernetClass::network.copyPacket(data->packet_next,0,UIPEthernetClass::in_packet,UIP_UDP_PHYH_LEN,UIPEthernetClass::network.blockSize(data->packet_next)); #ifdef UIPETHERNET_DEBUG_UDP - Serial.print(F("udp, uip_newdata received packet: ")); - Serial.print(*packet); - Serial.print(F(", slot: ")); - Serial.print(i); - Serial.print(F(", size: ")); - Serial.println(UIPEthernet.network.blockSize(*packet)); + Serial.print(F("udp, uip_newdata received packet: ")); + Serial.print(data->packet_next); + Serial.print(F(", size: ")); + Serial.println(UIPEthernetClass::network.blockSize(data->packet_next)); #endif - break; - } } - packet++; - i++; } - while (i < UIP_UDP_NUMPACKETS); } if (uip_poll() && data->send) { @@ -393,10 +346,10 @@ UIPUDP::uip_callback() { Serial.print(F("udp, uip_poll preparing packet to send: ")); Serial.print(data->packet_out); Serial.print(F(", size: ")); - Serial.println(UIPEthernet.network.blockSize(data->packet_out)); + Serial.println(UIPEthernetClass::network.blockSize(data->packet_out)); #endif - UIPEthernet.uip_packet = data->packet_out; - UIPEthernet.uip_hdrlen = UIP_UDP_PHYH_LEN; + UIPEthernetClass::uip_packet = data->packet_out; + UIPEthernetClass::uip_hdrlen = UIP_UDP_PHYH_LEN; uip_udp_send(data->out_pos - (UIP_UDP_PHYH_LEN)); } } @@ -407,8 +360,8 @@ UIPUDP::_send(uip_udp_userdata_t *data) { uip_arp_out(); //add arp if (uip_len == UIP_ARPHDRSIZE) { - UIPEthernet.uip_packet = NOBLOCK; - UIPEthernet.packetstate &= ~UIPETHERNET_SENDPACKET; + UIPEthernetClass::uip_packet = NOBLOCK; + UIPEthernetClass::packetstate &= ~UIPETHERNET_SENDPACKET; #ifdef UIPETHERNET_DEBUG_UDP Serial.println(F("udp, uip_poll results in ARP-packet")); #endif @@ -418,12 +371,12 @@ UIPUDP::_send(uip_udp_userdata_t *data) { { data->send = false; data->packet_out = NOBLOCK; - UIPEthernet.packetstate |= UIPETHERNET_SENDPACKET; + UIPEthernetClass::packetstate |= UIPETHERNET_SENDPACKET; #ifdef UIPETHERNET_DEBUG_UDP Serial.print(F("udp, uip_packet to send: ")); - Serial.println(UIPEthernet.uip_packet); + Serial.println(UIPEthernetClass::uip_packet); #endif } - UIPEthernet.network_send(); + UIPEthernetClass::network_send(); } #endif diff --git a/UIPEthernet/src/UIPUdp.h b/UIPEthernet/src/UIPUdp.h index aaae83b..78ce81d 100644 --- a/UIPEthernet/src/UIPUdp.h +++ b/UIPEthernet/src/UIPUdp.h @@ -25,19 +25,16 @@ #include #include "utility/mempool.h" extern "C" { - #include "utility/uip.h"; + #include "utility/uip.h" } #define UIP_UDP_MAXDATALEN 1500 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN+UIP_IPUDPH_LEN #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN+UIP_UDP_PHYH_LEN -#ifndef UIP_UDP_NUMPACKETS -#define UIP_UDP_NUMPACKETS 5 -#endif typedef struct { memaddress out_pos; - memhandle packets_in[UIP_UDP_NUMPACKETS]; + memhandle packet_next; memhandle packet_in; memhandle packet_out; boolean send; diff --git a/UIPEthernet/src/utility/Enc28J60Network.cpp b/UIPEthernet/src/utility/Enc28J60Network.cpp index 2670a6f..bdfc078 100644 --- a/UIPEthernet/src/utility/Enc28J60Network.cpp +++ b/UIPEthernet/src/utility/Enc28J60Network.cpp @@ -176,7 +176,7 @@ void Enc28J60Network::init(uint8_t* macaddr) memhandle Enc28J60Network::receivePacket() { - uint16_t rxstat; + uint8_t rxstat; uint16_t len; // check if a packet has been received and buffered //if( !(readReg(EIR) & EIR_PKTIF) ){ @@ -195,7 +195,7 @@ Enc28J60Network::receivePacket() len -= 4; //remove the CRC count // read the receive status (see datasheet page 43) rxstat = readOp(ENC28J60_READ_BUF_MEM, 0); - rxstat |= readOp(ENC28J60_READ_BUF_MEM, 0) << 8; + //rxstat |= readOp(ENC28J60_READ_BUF_MEM, 0) << 8; // decrement the packet counter indicate we are done with this packet writeOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC); // check CRC and symbol errors (see datasheet page 44, table 7-3): @@ -223,7 +223,7 @@ Enc28J60Network::setERXRDPT() memaddress Enc28J60Network::blockSize(memhandle handle) { - return handle == UIP_RECEIVEBUFFERHANDLE ? receivePkt.size : blocks[handle].size; + return handle == NOBLOCK ? 0 : handle == UIP_RECEIVEBUFFERHANDLE ? receivePkt.size : blocks[handle].size; } void diff --git a/UIPEthernet/src/utility/mempool.cpp b/UIPEthernet/src/utility/mempool.cpp index 01f17fb..2da5b05 100644 --- a/UIPEthernet/src/utility/mempool.cpp +++ b/UIPEthernet/src/utility/mempool.cpp @@ -118,6 +118,8 @@ MemoryPool::allocBlock(memaddress size) void MemoryPool::freeBlock(memhandle handle) { + if (handle == NOBLOCK) + return; memblock *b = &blocks[POOLSTART]; do diff --git a/UIPEthernet/src/utility/mempool_conf.h b/UIPEthernet/src/utility/mempool_conf.h index bd5a726..88b587a 100644 --- a/UIPEthernet/src/utility/mempool_conf.h +++ b/UIPEthernet/src/utility/mempool_conf.h @@ -15,8 +15,8 @@ typedef uint8_t memhandle; #define NUM_TCP_MEMBLOCKS 0 #endif -#if UIP_UDP and UIP_UDP_NUMPACKETS and UIP_UDP_CONNS -#define NUM_UDP_MEMBLOCKS (UIP_UDP_NUMPACKETS+1)*UIP_UDP_CONNS +#if UIP_UDP and UIP_UDP_CONNS +#define NUM_UDP_MEMBLOCKS 3*UIP_UDP_CONNS #else #define NUM_UDP_MEMBLOCKS 0 #endif diff --git a/UIPEthernet/src/utility/uip-neighbor.c b/UIPEthernet/src/utility/uip-neighbor.c deleted file mode 100644 index 739c03e..0000000 --- a/UIPEthernet/src/utility/uip-neighbor.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2006, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the uIP TCP/IP stack - * - * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $ - */ - -/** - * \file - * Database of link-local neighbors, used by IPv6 code and - * to be used by a future ARP code rewrite. - * \author - * Adam Dunkels - */ - -#include "uip-neighbor.h" - -#include - -#define MAX_TIME 128 - -#ifdef UIP_NEIGHBOR_CONF_ENTRIES -#define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES -#else /* UIP_NEIGHBOR_CONF_ENTRIES */ -#define ENTRIES 8 -#endif /* UIP_NEIGHBOR_CONF_ENTRIES */ - -struct neighbor_entry { - uip_ipaddr_t ipaddr; - struct uip_neighbor_addr addr; - u8_t time; -}; -static struct neighbor_entry entries[ENTRIES]; - -/*---------------------------------------------------------------------------*/ -void -uip_neighbor_init(void) -{ - int i; - - for(i = 0; i < ENTRIES; ++i) { - entries[i].time = MAX_TIME; - } -} -/*---------------------------------------------------------------------------*/ -void -uip_neighbor_periodic(void) -{ - int i; - - for(i = 0; i < ENTRIES; ++i) { - if(entries[i].time < MAX_TIME) { - entries[i].time++; - } - } -} -/*---------------------------------------------------------------------------*/ -void -uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr) -{ - int i, oldest; - u8_t oldest_time; - - printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", - addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3], - addr->addr.addr[4], addr->addr.addr[5]); - - /* Find the first unused entry or the oldest used entry. */ - oldest_time = 0; - oldest = 0; - for(i = 0; i < ENTRIES; ++i) { - if(entries[i].time == MAX_TIME) { - oldest = i; - break; - } - if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) { - oldest = i; - break; - } - if(entries[i].time > oldest_time) { - oldest = i; - oldest_time = entries[i].time; - } - } - - /* Use the oldest or first free entry (either pointed to by the - "oldest" variable). */ - entries[oldest].time = 0; - uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr); - memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr)); -} -/*---------------------------------------------------------------------------*/ -static struct neighbor_entry * -find_entry(uip_ipaddr_t ipaddr) -{ - int i; - - for(i = 0; i < ENTRIES; ++i) { - if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) { - return &entries[i]; - } - } - return NULL; -} -/*---------------------------------------------------------------------------*/ -void -uip_neighbor_update(uip_ipaddr_t ipaddr) -{ - struct neighbor_entry *e; - - e = find_entry(ipaddr); - if(e != NULL) { - e->time = 0; - } -} -/*---------------------------------------------------------------------------*/ -struct uip_neighbor_addr * -uip_neighbor_lookup(uip_ipaddr_t ipaddr) -{ - struct neighbor_entry *e; - - e = find_entry(ipaddr); - if(e != NULL) { - /* printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n", - e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3], - e->addr.addr.addr[4], e->addr.addr.addr[5]);*/ - - return &e->addr; - } - return NULL; -} -/*---------------------------------------------------------------------------*/ diff --git a/UIPEthernet/src/utility/uip-neighbor.h b/UIPEthernet/src/utility/uip-neighbor.h deleted file mode 100644 index d3b351c..0000000 --- a/UIPEthernet/src/utility/uip-neighbor.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2006, Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * This file is part of the uIP TCP/IP stack - * - * $Id: uip-neighbor.h,v 1.2 2006/06/12 08:00:30 adam Exp $ - */ - -/** - * \file - * Header file for database of link-local neighbors, used by - * IPv6 code and to be used by future ARP code. - * \author - * Adam Dunkels - */ - -#ifndef __UIP_NEIGHBOR_H__ -#define __UIP_NEIGHBOR_H__ - -#include "uip.h" - -struct uip_neighbor_addr { -#if UIP_NEIGHBOR_CONF_ADDRTYPE - UIP_NEIGHBOR_CONF_ADDRTYPE addr; -#else - struct uip_eth_addr addr; -#endif -}; - -void uip_neighbor_init(void); -void uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr); -void uip_neighbor_update(uip_ipaddr_t ipaddr); -struct uip_neighbor_addr *uip_neighbor_lookup(uip_ipaddr_t ipaddr); -void uip_neighbor_periodic(void); - -#endif /* __UIP-NEIGHBOR_H__ */ diff --git a/UIPEthernet/src/utility/uipethernet-conf.h b/UIPEthernet/src/utility/uipethernet-conf.h index 821df18..f25f7d6 100644 --- a/UIPEthernet/src/utility/uipethernet-conf.h +++ b/UIPEthernet/src/utility/uipethernet-conf.h @@ -9,7 +9,6 @@ #define UIP_CONF_UDP 1 #define UIP_CONF_BROADCAST 1 #define UIP_CONF_UDP_CONNS 4 -#define UIP_UDP_NUMPACKETS 5 /* number of attempts on write before returning number of bytes sent so far * set to -1 to block until connection is closed by timeout */