Skip to content

Commit 7c1824f

Browse files
committed
saved aprox. 120 bytes of flash by declaring UIPEthernet internals static
1 parent 06d8afb commit 7c1824f

File tree

5 files changed

+115
-93
lines changed

5 files changed

+115
-93
lines changed

UIPClient.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ UIPClient::connect(IPAddress ip, uint16_t port)
5757
{
5858
while((conn->tcpstateflags & UIP_TS_MASK) != UIP_CLOSED)
5959
{
60-
UIPEthernet.tick();
60+
UIPEthernetClass::tick();
6161
if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED)
6262
{
6363
data = (uip_userdata_t*) conn->appstate;
@@ -83,7 +83,7 @@ UIPClient::connect(const char *host, uint16_t port)
8383
DNSClient dns;
8484
IPAddress remote_addr;
8585

86-
dns.begin(UIPEthernet.dnsServerIP());
86+
dns.begin(UIPEthernetClass::_dnsServerAddress);
8787
ret = dns.getHostByName(host, remote_addr);
8888
if (ret == 1) {
8989
return connect(remote_addr, port);
@@ -122,7 +122,7 @@ UIPClient::stop()
122122
}
123123
#endif
124124
data = NULL;
125-
UIPEthernet.tick();
125+
UIPEthernetClass::tick();
126126
}
127127

128128
uint8_t
@@ -139,7 +139,7 @@ UIPClient::operator==(const UIPClient& rhs)
139139

140140
UIPClient::operator bool()
141141
{
142-
UIPEthernet.tick();
142+
UIPEthernetClass::tick();
143143
return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in[0] != NOBLOCK);
144144
}
145145

@@ -164,14 +164,14 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
164164
uint16_t attempts = UIP_ATTEMPTS_ON_WRITE;
165165
#endif
166166
repeat:
167-
UIPEthernet.tick();
167+
UIPEthernetClass::tick();
168168
if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
169169
{
170170
memhandle* p = _currentBlock(&u->packets_out[0]);
171171
if (*p == NOBLOCK)
172172
{
173173
newpacket:
174-
*p = UIPEthernet.network.allocBlock(UIP_SOCKET_DATALEN);
174+
*p = UIPEthernetClass::network.allocBlock(UIP_SOCKET_DATALEN);
175175
if (*p == NOBLOCK)
176176
{
177177
#if UIP_ATTEMPTS_ON_WRITE > 0
@@ -197,7 +197,7 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
197197
Serial.write((uint8_t*)buf+size-remain,remain);
198198
Serial.println(F("'"));
199199
#endif
200-
written = UIPEthernet.network.writePacket(*p,u->out_pos,(uint8_t*)buf+size-remain,remain);
200+
written = UIPEthernetClass::network.writePacket(*p,u->out_pos,(uint8_t*)buf+size-remain,remain);
201201
remain -= written;
202202
u->out_pos+=written;
203203
if (remain > 0)
@@ -240,7 +240,7 @@ UIPClient::_available(uip_userdata_t *u)
240240
{
241241
if(*p == NOBLOCK)
242242
break;
243-
len += UIPEthernet.network.blockSize(*p);
243+
len += UIPEthernetClass::network.blockSize(*p);
244244
}
245245
return len;
246246
}
@@ -257,8 +257,8 @@ UIPClient::read(uint8_t *buf, size_t size)
257257
unsigned int read;
258258
do
259259
{
260-
read = UIPEthernet.network.readPacket(*p,0,buf+size-remain,remain);
261-
if (read == UIPEthernet.network.blockSize(*p))
260+
read = UIPEthernetClass::network.readPacket(*p,0,buf+size-remain,remain);
261+
if (read == UIPEthernetClass::network.blockSize(*p))
262262
{
263263
remain -= read;
264264
_eatBlock(p);
@@ -276,7 +276,7 @@ UIPClient::read(uint8_t *buf, size_t size)
276276
}
277277
else
278278
{
279-
UIPEthernet.network.resizeBlock(*p,read);
279+
UIPEthernetClass::network.resizeBlock(*p,read);
280280
break;
281281
}
282282
}
@@ -304,7 +304,7 @@ UIPClient::peek()
304304
if (p != NOBLOCK)
305305
{
306306
uint8_t c;
307-
UIPEthernet.network.readPacket(p,0,&c,1);
307+
UIPEthernetClass::network.readPacket(p,0,&c,1);
308308
return c;
309309
}
310310
}
@@ -360,7 +360,7 @@ UIPClient::uip_callback()
360360
#endif
361361
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
362362
{
363-
memhandle newPacket = UIPEthernet.network.allocBlock(uip_len);
363+
memhandle newPacket = UIPEthernetClass::network.allocBlock(uip_len);
364364
if (newPacket != NOBLOCK)
365365
{
366366
memhandle* p = _currentBlock(&u->packets_in[0]);
@@ -379,12 +379,12 @@ UIPClient::uip_callback()
379379
goto reject_newdata;
380380
}
381381
}
382-
UIPEthernet.network.copyPacket(newPacket,0,UIPEthernet.in_packet,((uint8_t*)uip_appdata)-uip_buf,uip_len);
382+
UIPEthernetClass::network.copyPacket(newPacket,0,UIPEthernetClass::in_packet,((uint8_t*)uip_appdata)-uip_buf,uip_len);
383383
*p = newPacket;
384384
goto finish_newdata;
385385
}
386386
reject_newdata:
387-
UIPEthernet.packetstate &= ~UIPETHERNET_FREEPACKET;
387+
UIPEthernetClass::packetstate &= ~UIPETHERNET_FREEPACKET;
388388
uip_stop();
389389
}
390390
}
@@ -438,19 +438,19 @@ UIPClient::uip_callback()
438438
uip_len = u->out_pos;
439439
if (uip_len > 0)
440440
{
441-
UIPEthernet.network.resizeBlock(p,0,uip_len);
441+
UIPEthernetClass::network.resizeBlock(p,0,uip_len);
442442
}
443443
}
444444
else
445-
uip_len = UIPEthernet.network.blockSize(p);
445+
uip_len = UIPEthernetClass::network.blockSize(p);
446446
if (uip_len > 0)
447447
{
448-
UIPEthernet.uip_hdrlen = ((uint8_t*)uip_appdata)-uip_buf;
449-
UIPEthernet.uip_packet = UIPEthernet.network.allocBlock(UIPEthernet.uip_hdrlen+uip_len);
450-
if (UIPEthernet.uip_packet != NOBLOCK)
448+
UIPEthernetClass::uip_hdrlen = ((uint8_t*)uip_appdata)-uip_buf;
449+
UIPEthernetClass::uip_packet = UIPEthernetClass::network.allocBlock(UIPEthernetClass::uip_hdrlen+uip_len);
450+
if (UIPEthernetClass::uip_packet != NOBLOCK)
451451
{
452-
UIPEthernet.network.copyPacket(UIPEthernet.uip_packet,UIPEthernet.uip_hdrlen,p,0,uip_len);
453-
UIPEthernet.packetstate |= UIPETHERNET_SENDPACKET;
452+
UIPEthernetClass::network.copyPacket(UIPEthernetClass::uip_packet,UIPEthernetClass::uip_hdrlen,p,0,uip_len);
453+
UIPEthernetClass::packetstate |= UIPETHERNET_SENDPACKET;
454454
uip_send(uip_appdata,uip_len);
455455
}
456456
return;
@@ -484,7 +484,7 @@ UIPClient::uip_callback()
484484
}
485485
}
486486
nodata:
487-
UIPEthernet.uip_packet = NOBLOCK;
487+
UIPEthernetClass::uip_packet = NOBLOCK;
488488
uip_len=0;
489489
}
490490

@@ -529,7 +529,7 @@ UIPClient::_eatBlock(memhandle* block)
529529
Serial.print(F("-> "));
530530
#endif
531531
memhandle* end = block+(UIP_SOCKET_NUMPACKETS-1);
532-
UIPEthernet.network.freeBlock(*block);
532+
UIPEthernetClass::network.freeBlock(*block);
533533
while(block < end) {
534534
*block = *(block + 1);
535535
block++;
@@ -552,7 +552,7 @@ UIPClient::_flushBlocks(memhandle* block)
552552
{
553553
if(*block != NOBLOCK)
554554
{
555-
UIPEthernet.network.freeBlock(*block);
555+
UIPEthernetClass::network.freeBlock(*block);
556556
*block = NOBLOCK;
557557
}
558558
else

UIPEthernet.cpp

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,26 @@ extern "C"
3737

3838
#define ETH_HDR ((struct uip_eth_hdr *)&uip_buf[0])
3939

40+
memhandle UIPEthernetClass::in_packet(NOBLOCK);
41+
memhandle UIPEthernetClass::uip_packet(NOBLOCK);
42+
uint8_t UIPEthernetClass::uip_hdrlen(0);
43+
uint8_t UIPEthernetClass::packetstate(0);
44+
45+
IPAddress UIPEthernetClass::_dnsServerAddress;
46+
DhcpClass* UIPEthernetClass::_dhcp(NULL);
47+
48+
struct uip_timer UIPEthernetClass::periodic_timer;
49+
50+
Enc28J60Network UIPEthernetClass::network;
51+
4052
// Because uIP isn't encapsulated within a class we have to use global
4153
// variables, so we can only have one TCP/IP stack per program.
4254

43-
UIPEthernetClass::UIPEthernetClass() :
44-
in_packet(NOBLOCK),
45-
uip_packet(NOBLOCK),
46-
uip_hdrlen(0),
47-
packetstate(0),
48-
_dhcp(NULL)
55+
UIPEthernetClass::UIPEthernetClass()
4956
{
5057
}
5158

59+
#if UIP_UDP
5260
int
5361
UIPEthernetClass::begin(const uint8_t* mac)
5462
{
@@ -68,6 +76,7 @@ UIPEthernetClass::begin(const uint8_t* mac)
6876
}
6977
return ret;
7078
}
79+
#endif
7180

7281
void
7382
UIPEthernetClass::begin(const uint8_t* mac, IPAddress ip)
@@ -102,6 +111,7 @@ UIPEthernetClass::begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddre
102111
int UIPEthernetClass::maintain(){
103112
tick();
104113
int rc = DHCP_CHECK_NONE;
114+
#if UIP_UDP
105115
if(_dhcp != NULL){
106116
//we have a pointer to dhcp, use it
107117
rc = _dhcp->checkLease();
@@ -120,6 +130,7 @@ int UIPEthernetClass::maintain(){
120130
}
121131
}
122132
return rc;
133+
#endif
123134
}
124135

125136
IPAddress UIPEthernetClass::localIP()
@@ -277,7 +288,7 @@ boolean UIPEthernetClass::network_send()
277288
}
278289

279290
void UIPEthernetClass::init(const uint8_t* mac) {
280-
uip_timer_set(&this->periodic_timer, CLOCK_SECOND / 4);
291+
uip_timer_set(&periodic_timer, CLOCK_SECOND / 4);
281292

282293
network.init((uint8_t*)mac);
283294
uip_seteth_addr(mac);
@@ -348,7 +359,11 @@ UIPEthernetClass::ipchksum(void)
348359

349360
/*---------------------------------------------------------------------------*/
350361
uint16_t
362+
#if UIP_UDP
351363
UIPEthernetClass::upper_layer_chksum(uint8_t proto)
364+
#else
365+
uip_tcpchksum(void)
366+
#endif
352367
{
353368
uint16_t upper_layer_len;
354369
uint16_t sum;
@@ -362,28 +377,34 @@ UIPEthernetClass::upper_layer_chksum(uint8_t proto)
362377
/* First sum pseudoheader. */
363378

364379
/* IP protocol and length fields. This addition cannot carry. */
380+
#if UIP_UDP
365381
sum = upper_layer_len + proto;
382+
#else
383+
sum = upper_layer_len + UIP_PROTO_TCP;
384+
#endif
366385
/* Sum IP source and destination addresses. */
367-
sum = chksum(sum, (u8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t));
386+
sum = UIPEthernetClass::chksum(sum, (u8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t));
368387

369388
uint8_t upper_layer_memlen;
389+
#if UIP_UDP
370390
switch(proto)
371391
{
372-
case UIP_PROTO_TCP:
373-
upper_layer_memlen = (BUF->tcpoffset >> 4) << 2;
374-
break;
375-
#if UIP_UDP
376-
case UIP_PROTO_UDP:
392+
// case UIP_PROTO_ICMP:
393+
// case UIP_PROTO_ICMP6:
394+
// upper_layer_memlen = upper_layer_len;
395+
// break;
396+
case UIP_PROTO_UDP:
377397
upper_layer_memlen = UIP_UDPH_LEN;
378398
break;
399+
default:
400+
// case UIP_PROTO_TCP:
379401
#endif
380-
// case UIP_PROTO_ICMP:
381-
// case UIP_PROTO_ICMP6:
382-
default:
383-
upper_layer_memlen = upper_layer_len;
384-
break;
402+
upper_layer_memlen = (BUF->tcpoffset >> 4) << 2;
403+
#if UIP_UDP
404+
break;
385405
}
386-
sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], upper_layer_memlen);
406+
#endif
407+
sum = UIPEthernetClass::chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], upper_layer_memlen);
387408
#ifdef UIPETHERNET_DEBUG_CHKSUM
388409
Serial.print(F("chksum uip_buf["));
389410
Serial.print(UIP_IPH_LEN + UIP_LLH_LEN);
@@ -394,9 +415,9 @@ UIPEthernetClass::upper_layer_chksum(uint8_t proto)
394415
#endif
395416
if (upper_layer_memlen < upper_layer_len)
396417
{
397-
sum = network.chksum(
418+
sum = UIPEthernetClass::network.chksum(
398419
sum,
399-
uip_packet,
420+
UIPEthernetClass::uip_packet,
400421
UIP_IPH_LEN + UIP_LLH_LEN + upper_layer_memlen,
401422
upper_layer_len - upper_layer_memlen
402423
);
@@ -420,14 +441,14 @@ uip_ipchksum(void)
420441
return UIPEthernet.ipchksum();
421442
}
422443

444+
#if UIP_UDP
423445
uint16_t
424446
uip_tcpchksum(void)
425447
{
426448
uint16_t sum = UIPEthernet.upper_layer_chksum(UIP_PROTO_TCP);
427449
return sum;
428450
}
429451

430-
#if UIP_UDP
431452
uint16_t
432453
uip_udpchksum(void)
433454
{

UIPEthernet.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@ class UIPEthernetClass
8282
IPAddress dnsServerIP();
8383

8484
private:
85-
memhandle in_packet;
86-
memhandle uip_packet;
87-
uint8_t uip_hdrlen;
88-
uint8_t packetstate;
85+
static memhandle in_packet;
86+
static memhandle uip_packet;
87+
static uint8_t uip_hdrlen;
88+
static uint8_t packetstate;
8989

90-
IPAddress _dnsServerAddress;
91-
DhcpClass* _dhcp;
90+
static IPAddress _dnsServerAddress;
91+
static DhcpClass* _dhcp;
9292

93-
struct uip_timer periodic_timer;
93+
static struct uip_timer periodic_timer;
9494

95-
Enc28J60Network network;
95+
static Enc28J60Network network;
9696

97-
void init(const uint8_t* mac);
98-
void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
97+
static void init(const uint8_t* mac);
98+
static void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);
9999

100-
void tick();
100+
static void tick();
101101

102-
boolean network_send();
102+
static boolean network_send();
103103

104104
friend class UIPServer;
105105

@@ -109,8 +109,9 @@ class UIPEthernetClass
109109

110110
static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len);
111111
static uint16_t ipchksum(void);
112-
uint16_t upper_layer_chksum(uint8_t proto);
113-
112+
#if UIP_UDP
113+
static uint16_t upper_layer_chksum(uint8_t proto);
114+
#endif
114115
friend uint16_t uip_ipchksum(void);
115116
friend uint16_t uip_tcpchksum(void);
116117
friend uint16_t uip_udpchksum(void);

UIPServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ UIPServer::UIPServer(uint16_t port) : _port(htons(port))
2828

2929
UIPClient UIPServer::available()
3030
{
31-
UIPEthernet.tick();
31+
UIPEthernetClass::tick();
3232
for ( uip_userdata_t* data = &UIPClient::all_data[0]; data < &UIPClient::all_data[UIP_CONNS]; data++ )
3333
{
3434
if (data->packets_in[0] != NOBLOCK
@@ -42,7 +42,7 @@ UIPClient UIPServer::available()
4242
void UIPServer::begin()
4343
{
4444
uip_listen(_port);
45-
UIPEthernet.tick();
45+
UIPEthernetClass::tick();
4646
}
4747

4848
size_t UIPServer::write(uint8_t c)

0 commit comments

Comments
 (0)