Skip to content

Commit 42e788b

Browse files
committed
Merge pull request #14 from ntruchsess/dev
Fix an overflow and add blocking write (configurable)
2 parents 0c83d7c + 39023b4 commit 42e788b

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

UIPClient.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,30 @@ size_t
158158
UIPClient::_write(struct uip_conn* conn, const uint8_t *buf, size_t size)
159159
{
160160
uip_userdata_t *u;
161+
int remain = size;
162+
uint16_t written;
163+
#if UIP_ATTEMPTS_ON_WRITE > 0
164+
uint16_t attempts = UIP_ATTEMPTS_ON_WRITE;
165+
#endif
166+
repeat:
161167
UIPEthernet.tick();
162168
if (conn && (u = (uip_userdata_t *)conn->appstate.user) && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_CLOSED)))
163169
{
164-
int remain = size;
165-
uint16_t written;
166170
memhandle* p = _currentBlock(&u->packets_out[0]);
167171
if (*p == NOBLOCK)
168172
{
169173
newpacket:
170174
*p = UIPEthernet.network.allocBlock(UIP_SOCKET_DATALEN);
171175
if (*p == NOBLOCK)
172-
goto ready;
176+
{
177+
#if UIP_ATTEMPTS_ON_WRITE > 0
178+
if ((--attempts)>0)
179+
#endif
180+
#if UIP_ATTEMPTS_ON_WRITE != 0
181+
goto repeat;
182+
#endif
183+
goto ready;
184+
}
173185
u->out_pos = 0;
174186
}
175187
#ifdef UIPETHERNET_DEBUG_CLIENT
@@ -191,7 +203,15 @@ UIPClient::_write(struct uip_conn* conn, const uint8_t *buf, size_t size)
191203
if (remain > 0)
192204
{
193205
if (p==&u->packets_out[UIP_SOCKET_NUMPACKETS-1])
194-
goto ready;
206+
{
207+
#if UIP_ATTEMPTS_ON_WRITE > 0
208+
if ((--attempts)>0)
209+
#endif
210+
#if UIP_ATTEMPTS_ON_WRITE != 0
211+
goto repeat;
212+
#endif
213+
goto ready;
214+
}
195215
p++;
196216
goto newpacket;
197217
}
@@ -316,11 +336,6 @@ UIPClient::uip_callback(uip_tcp_appstate_t *s)
316336
}
317337
if (u)
318338
{
319-
if (u->state & UIP_CLIENT_RESTART)
320-
{
321-
u->state &= ~UIP_CLIENT_RESTART;
322-
uip_restart();
323-
}
324339
if (uip_newdata())
325340
{
326341
#ifdef UIPETHERNET_DEBUG_CLIENT
@@ -358,6 +373,11 @@ UIPClient::uip_callback(uip_tcp_appstate_t *s)
358373
}
359374
}
360375
finish_newdata:
376+
if (u->state & UIP_CLIENT_RESTART)
377+
{
378+
u->state &= ~UIP_CLIENT_RESTART;
379+
uip_restart();
380+
}
361381
// If the connection has been closed, save received but unread data.
362382
if (uip_closed() || uip_timedout())
363383
{
@@ -451,7 +471,7 @@ UIPClient::uip_callback(uip_tcp_appstate_t *s)
451471
memhandle*
452472
UIPClient::_currentBlock(memhandle* block)
453473
{
454-
for(memhandle* end = block+UIP_SOCKET_NUMPACKETS; block < end; block++)
474+
for(memhandle* end = block+UIP_SOCKET_NUMPACKETS-1; block < end; block++)
455475
if(*(block+1) == NOBLOCK)
456476
break;
457477
return block;

utility/Enc28J60Network.cpp

+3-28
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ extern "C" {
4747

4848
Enc28J60Network::Enc28J60Network() :
4949
MemoryPool(TXSTART_INIT+1, TXSTOP_INIT-TXSTART_INIT), // 1 byte in between RX_STOP_INIT and pool to allow prepending of controlbyte
50-
status(0),
5150
bank(0xff)
5251
{
5352
}
@@ -268,7 +267,6 @@ uint16_t
268267
Enc28J60Network::readPacket(memhandle handle, memaddress position, uint8_t* buffer, uint16_t len)
269268
{
270269
len = setReadPtr(handle, position, len);
271-
checkDMA();
272270
readBuffer(len, buffer);
273271
return len;
274272
}
@@ -322,15 +320,14 @@ Enc28J60Network::copyPacket(memhandle dest_pkt, memaddress dest_pos, memhandle s
322320
memblock *dest = &blocks[dest_pkt];
323321
memblock *src = src_pkt == UIP_RECEIVEBUFFERHANDLE ? &receivePkt : &blocks[src_pkt];
324322
memblock_mv_cb(dest->begin+dest_pos,src->begin+src_pos,len);
325-
if (src_pkt == UIP_RECEIVEBUFFERHANDLE)
326-
status |= DMANEWPACKET;
323+
// Move the RX read pointer to the start of the next received packet
324+
// This frees the memory we just read out
325+
setERXRDPT();
327326
}
328327

329328
void
330329
Enc28J60Network::memblock_mv_cb(uint16_t dest, uint16_t src, uint16_t len)
331330
{
332-
checkDMA();
333-
334331
//as ENC28J60 DMA is unable to copy single bytes:
335332
if (len == 1)
336333
{
@@ -372,35 +369,14 @@ Enc28J60Network::memblock_mv_cb(uint16_t dest, uint16_t src, uint16_t len)
372369
/* 4. Start the DMA copy by setting ECON1.DMAST. */
373370
writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_DMAST);
374371

375-
status |= DMARUNNING;
376-
}
377-
}
378-
379-
void
380-
Enc28J60Network::checkDMA()
381-
{
382-
if (status & DMARUNNING)
383-
{
384372
// wait until runnig DMA is completed
385373
while (readOp(ENC28J60_READ_CTRL_REG, ECON1) & ECON1_DMAST);
386-
387-
if (status & DMANEWPACKET)
388-
{
389-
// Move the RX read pointer to the start of the next received packet
390-
// This frees the memory we just read out
391-
setERXRDPT();
392-
}
393-
status = 0;
394374
}
395375
}
396376

397377
void
398378
Enc28J60Network::freePacket()
399379
{
400-
// wait until runnig new-packet DMA is completed. This implicitly frees the packet.
401-
if (status & DMANEWPACKET)
402-
checkDMA();
403-
else
404380
setERXRDPT();
405381
}
406382

@@ -548,7 +524,6 @@ uint16_t
548524
Enc28J60Network::chksum(uint16_t sum, memhandle handle, memaddress pos, uint16_t len)
549525
{
550526
uint16_t t;
551-
checkDMA();
552527
len = setReadPtr(handle, pos, len)-1;
553528
CSACTIVE;
554529
// issue read command

utility/Enc28J60Network.h

-3
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ class Enc28J60Network : public MemoryPool
5252
{
5353

5454
private:
55-
uint8_t status;
5655
uint16_t nextPacketPtr;
5756
uint8_t bank;
5857

5958
struct memblock receivePkt;
6059

61-
void checkDMA();
62-
6360
uint8_t readOp(uint8_t op, uint8_t address);
6461
void writeOp(uint8_t op, uint8_t address, uint8_t data);
6562
uint16_t setReadPtr(memhandle handle, memaddress position, uint16_t len);

utility/uipethernet-conf.h

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
#define UIP_CONF_UDP_CONNS 4
1212
#define UIP_UDP_NUMPACKETS 5
1313

14+
/* number of attempts on write before returning number of bytes sent so far
15+
* set to -1 to block until connection is closed by timeout */
16+
#define UIP_ATTEMPTS_ON_WRITE -1
17+
1418
#endif

0 commit comments

Comments
 (0)