Skip to content

Commit 1cec7c6

Browse files
committed
Merge branch 'master' into Arduino_1.5.5
2 parents 4bf3dd6 + 562dac0 commit 1cec7c6

File tree

4 files changed

+90
-24
lines changed

4 files changed

+90
-24
lines changed

UIPEthernet/src/UIPClient.cpp

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ UIPClient::UIPClient(uip_userdata_t* conn_data) :
4949
int
5050
UIPClient::connect(IPAddress ip, uint16_t port)
5151
{
52+
stop();
5253
uip_ipaddr_t ipaddr;
5354
uip_ip_addr(ipaddr, ip);
5455
struct uip_conn* conn = uip_connect(&ipaddr, htons(port));
@@ -61,7 +62,6 @@ UIPClient::connect(IPAddress ip, uint16_t port)
6162
{
6263
data = (uip_userdata_t*) conn->appstate;
6364
#ifdef UIPETHERNET_DEBUG_CLIENT
64-
6565
Serial.print(F("connected, state: "));
6666
Serial.print(data->state);
6767
Serial.print(F(", first packet in: "));
@@ -95,18 +95,32 @@ UIPClient::connect(const char *host, uint16_t port)
9595
void
9696
UIPClient::stop()
9797
{
98-
if (data)
98+
if (data && data->state)
9999
{
100+
#ifdef UIPETHERNET_DEBUG_CLIENT
101+
Serial.println(F("before stop(), with data"));
102+
_dumpAllData();
103+
#endif
100104
_flushBlocks(&data->packets_in[0]);
101-
if (data->state & UIP_CLIENT_CLOSED)
105+
if (data->state & UIP_CLIENT_REMOTECLOSED)
102106
{
103107
data->state = 0;
104108
}
105109
else
106110
{
107111
data->state |= UIP_CLIENT_CLOSE;
108112
}
113+
#ifdef UIPETHERNET_DEBUG_CLIENT
114+
Serial.println(F("after stop()"));
115+
_dumpAllData();
116+
#endif
109117
}
118+
#ifdef UIPETHERNET_DEBUG_CLIENT
119+
else
120+
{
121+
Serial.println(F("stop(), data: NULL"));
122+
}
123+
#endif
110124
data = NULL;
111125
UIPEthernet.tick();
112126
}
@@ -126,7 +140,7 @@ UIPClient::operator==(const UIPClient& rhs)
126140
UIPClient::operator bool()
127141
{
128142
UIPEthernet.tick();
129-
return data && (!(data->state & UIP_CLIENT_CLOSED) || data->packets_in[0] != NOBLOCK);
143+
return data && (!(data->state & UIP_CLIENT_REMOTECLOSED) || data->packets_in[0] != NOBLOCK);
130144
}
131145

132146
size_t
@@ -151,7 +165,7 @@ UIPClient::_write(uip_userdata_t* u, const uint8_t *buf, size_t size)
151165
#endif
152166
repeat:
153167
UIPEthernet.tick();
154-
if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_CLOSED)))
168+
if (u && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
155169
{
156170
memhandle* p = _currentBlock(&u->packets_out[0]);
157171
if (*p == NOBLOCK)
@@ -248,11 +262,11 @@ UIPClient::read(uint8_t *buf, size_t size)
248262
{
249263
remain -= read;
250264
_eatBlock(p);
251-
if (uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS]) && !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_CLOSED)))
265+
if (uip_stopped(&uip_conns[data->state & UIP_CLIENT_SOCKETS]) && !(data->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
252266
data->state |= UIP_CLIENT_RESTART;
253267
if (*p == NOBLOCK)
254268
{
255-
if (data->state & UIP_CLIENT_CLOSED)
269+
if (data->state & UIP_CLIENT_REMOTECLOSED)
256270
{
257271
data->state = 0;
258272
data = NULL;
@@ -320,14 +334,15 @@ UIPClient::uip_callback()
320334
{
321335
#ifdef UIPETHERNET_DEBUG_CLIENT
322336
Serial.println(F("UIPClient uip_connected"));
337+
_dumpAllData();
323338
#endif
324339
u = (uip_userdata_t*) _allocateData();
325340
if (u)
326341
{
327342
uip_conn->appstate = u;
328343
#ifdef UIPETHERNET_DEBUG_CLIENT
329344
Serial.print(F("UIPClient allocated state: "));
330-
Serial.println(u->state);
345+
Serial.println(u->state,BIN);
331346
#endif
332347
}
333348
#ifdef UIPETHERNET_DEBUG_CLIENT
@@ -343,7 +358,7 @@ UIPClient::uip_callback()
343358
Serial.print(F("UIPClient uip_newdata, uip_len:"));
344359
Serial.println(uip_len);
345360
#endif
346-
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_CLOSED)))
361+
if (uip_len && !(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
347362
{
348363
memhandle newPacket = UIPEthernet.network.allocBlock(uip_len);
349364
if (newPacket != NOBLOCK)
@@ -384,17 +399,22 @@ UIPClient::uip_callback()
384399
{
385400
#ifdef UIPETHERNET_DEBUG_CLIENT
386401
Serial.println(F("UIPClient uip_closed"));
402+
_dumpAllData();
387403
#endif
388404
// drop outgoing packets not sent yet:
389405
_flushBlocks(&u->packets_out[0]);
390406
if (u->packets_in[0] != NOBLOCK)
391407
{
392408
((uip_userdata_closed_t *)u)->lport = uip_conn->lport;
393-
u->state |= UIP_CLIENT_CLOSED;
409+
u->state |= UIP_CLIENT_REMOTECLOSED;
394410
}
395411
else
396412
u->state = 0;
397413
// disassociate appdata.
414+
#ifdef UIPETHERNET_DEBUG_CLIENT
415+
Serial.println(F("after UIPClient uip_closed"));
416+
_dumpAllData();
417+
#endif
398418
uip_conn->appstate = NULL;
399419
goto nodata;
400420
}
@@ -442,18 +462,25 @@ UIPClient::uip_callback()
442462
{
443463
#ifdef UIPETHERNET_DEBUG_CLIENT
444464
Serial.print(F("UIPClient state UIP_CLIENT_CLOSE"));
465+
_dumpAllData();
445466
#endif
446467
if (u->packets_out[0] == NOBLOCK)
447468
{
448-
#ifdef UIPETHERNET_DEBUG_CLIENT
449-
Serial.print(F("UIPClient state UIP_CLIENT_CLOSE -> free userdata"));
450-
#endif
451469
u->state = 0;
452470
uip_conn->appstate = NULL;
453471
uip_close();
472+
#ifdef UIPETHERNET_DEBUG_CLIENT
473+
Serial.print(F("no blocks out -> free userdata"));
474+
_dumpAllData();
475+
#endif
454476
}
455477
else
456-
uip_stop();
478+
{
479+
uip_stop();
480+
#ifdef UIPETHERNET_DEBUG_CLIENT
481+
Serial.print(F("blocks outstanding transfer -> uip_stop()"));
482+
#endif
483+
}
457484
}
458485
}
459486
nodata:
@@ -531,3 +558,39 @@ UIPClient::_flushBlocks(memhandle* block)
531558
}
532559
}
533560

561+
#ifdef UIPETHERNET_DEBUG_CLIENT
562+
void
563+
UIPClient::_dumpAllData() {
564+
for (uint8_t i=0; i < UIP_CONNS; i++)
565+
{
566+
Serial.print(F("UIPClient::all_data["));
567+
Serial.print(i);
568+
Serial.print(F("], state:"));
569+
Serial.println(all_data[i].state, BIN);
570+
Serial.print(F("packets_in: "));
571+
for (uint8_t j=0; j < UIP_SOCKET_NUMPACKETS; j++)
572+
{
573+
Serial.print(all_data[i].packets_in[j]);
574+
Serial.print(F(" "));
575+
}
576+
Serial.println();
577+
if (all_data[i].state & UIP_CLIENT_REMOTECLOSED)
578+
{
579+
Serial.print(F("state remote closed, local port: "));
580+
Serial.println(htons(((uip_userdata_closed_t *)(&all_data[i]))->lport));
581+
}
582+
else
583+
{
584+
Serial.print(F("packets_out: "));
585+
for (uint8_t j=0; j < UIP_SOCKET_NUMPACKETS; j++)
586+
{
587+
Serial.print(all_data[i].packets_out[j]);
588+
Serial.print(F(" "));
589+
}
590+
Serial.println();
591+
Serial.print(F("out_pos: "));
592+
Serial.println(all_data[i].out_pos);
593+
}
594+
}
595+
}
596+
#endif

UIPEthernet/src/UIPClient.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ extern "C" {
3636

3737
#define UIP_CLIENT_CONNECTED 0x10
3838
#define UIP_CLIENT_CLOSE 0x20
39-
#define UIP_CLIENT_CLOSED 0x40
39+
#define UIP_CLIENT_REMOTECLOSED 0x40
4040
#define UIP_CLIENT_RESTART 0x80
41-
#define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_CLOSED | UIP_CLIENT_RESTART)
41+
#define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
4242
#define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS
4343

4444
typedef uint8_t uip_socket_ptr;
@@ -84,15 +84,18 @@ class UIPClient : public Client {
8484

8585
static uip_userdata_t all_data[UIP_CONNS];
8686
static uip_userdata_t* _allocateData();
87-
static uip_userdata_t* _getData(struct uip_conn * conn);
88-
87+
8988
static size_t _write(uip_userdata_t *,const uint8_t *buf, size_t size);
9089
static int _available(uip_userdata_t *);
9190

9291
static memhandle * _currentBlock(memhandle* blocks);
9392
static void _eatBlock(memhandle* blocks);
9493
static void _flushBlocks(memhandle* blocks);
9594

95+
#ifdef UIPETHERNET_DEBUG_CLIENT
96+
static void _dumpAllData();
97+
#endif
98+
9699
friend class UIPEthernetClass;
97100
friend class UIPServer;
98101

UIPEthernet/src/UIPEthernet.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#ifndef UIPETHERNET_H
2121
#define UIPETHERNET_H
2222

23+
//#define UIPETHERNET_DEBUG
24+
//#define UIPETHERNET_DEBUG_CHKSUM
25+
//#define UIPETHERNET_DEBUG_UDP
26+
//#define UIPETHERNET_DEBUG_CLIENT
27+
2328
#include "ethernet_comp.h"
2429
#include <Arduino.h>
2530
#include "Dhcp.h"
@@ -35,11 +40,6 @@ extern "C"
3540
#include "utility/uip.h"
3641
}
3742

38-
//#define UIPETHERNET_DEBUG
39-
//#define UIPETHERNET_DEBUG_CHKSUM
40-
//#define UIPETHERNET_DEBUG_UDP
41-
//#define UIPETHERNET_DEBUG_CLIENT
42-
4343
#define UIPETHERNET_FREEPACKET 1
4444
#define UIPETHERNET_SENDPACKET 2
4545
#define UIPETHERNET_BUFFERREAD 4

UIPEthernet/src/UIPServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ UIPClient UIPServer::available()
3333
{
3434
if (data->packets_in[0] != NOBLOCK
3535
&& (((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport ==_port)
36-
|| ((data->state & UIP_CLIENT_CLOSED) && ((uip_userdata_closed_t *)data)->lport == _port)))
36+
|| ((data->state & UIP_CLIENT_REMOTECLOSED) && ((uip_userdata_closed_t *)data)->lport == _port)))
3737
return UIPClient(data);
3838
}
3939
return UIPClient();

0 commit comments

Comments
 (0)