Skip to content

Commit 457de85

Browse files
committed
Fix issues when tcp error occurred
1 parent 2645a5b commit 457de85

File tree

5 files changed

+65
-46
lines changed

5 files changed

+65
-46
lines changed

binary/wifiHD.elf

1.19 KB
Binary file not shown.

wifiHD/Release/wifiHD.elf

1.19 KB
Binary file not shown.

wifiHD/src/ard_spi.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,14 @@ void* getTTCP(uint8_t sock)
182182

183183
int getSock(void * _ttcp)
184184
{
185-
int i = 0;
186-
for (; i<MAX_SOCK_NUM; i++)
185+
if (_ttcp != NULL)
187186
{
188-
if (_ttcp == mapSockTCP[i])
189-
return i;
187+
int i = 0;
188+
for (; i<MAX_SOCK_NUM; i++)
189+
{
190+
if (_ttcp == mapSockTCP[i])
191+
return i;
192+
}
190193
}
191194
return -1;
192195
}

wifiHD/src/ard_tcp.c

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131
unsigned int startTime = 0;
3232
extern bool ifStatus;
3333
static uint8_t tcp_poll_retries = 0;
34+
static int isDataSentCount = 0;
3435

3536
bool pending_close = false;
3637

38+
static void atcp_init()
39+
{
40+
pending_close = false;
41+
}
42+
3743
/**
3844
* Clean up and free the ttcp structure
3945
*/
@@ -127,7 +133,7 @@ static void tcp_send_data(struct ttcp *ttcp) {
127133
uint32_t len;
128134

129135
len = ttcp->left;
130-
INFO_TCP("left=%d len:%d tcp_sndbuf:%d\n", ttcp->left, len, tcp_sndbuf(ttcp->tpcb));
136+
INFO_TCP_VER("left=%d len:%d tcp_sndbuf:%d\n", ttcp->left, len, tcp_sndbuf(ttcp->tpcb));
131137

132138

133139
/* don't send more than we have in the payload */
@@ -141,14 +147,16 @@ static void tcp_send_data(struct ttcp *ttcp) {
141147

142148
uint8_t count = 0;
143149
do {
150+
startTime = timer_get_ms();
144151
err = tcp_write(ttcp->tpcb, ttcp->payload, len, TCP_WRITE_FLAG_COPY);
145-
INFO_TCP("%d) tcp_write len:%d err:%d\n", count++, len, err);
152+
INFO_TCP_VER("%d) tcp_write len:%d err:%d\n", count++, len, err);
146153
if (err == ERR_MEM)
147154
{
148155
len /= 2;
149156
ttcp->buff_sent = 0;
150157
}else{
151158
ttcp->buff_sent = 1;
159+
isDataSentCount = 0;
152160
}
153161
} while (err == ERR_MEM && len > 1);
154162

@@ -239,24 +247,30 @@ static void cleanSockState_cb(void *ctx) {
239247
int sock = getSock(ttcp);
240248
if (sock != -1)
241249
clearMapSockTcp(sock);
242-
printk("TTCP [%p]: cleanSockState_cb %d\n", ttcp, sock);
250+
INFO_TCP("TTCP [%p]: cleanSockState_cb %d\n", ttcp, sock);
243251
_connected = false;
244252
}
245253

254+
static void cleanSockStateDelayed(void * arg)
255+
{
256+
INFO_TCP("arg %p\n", arg);
257+
timer_sched_timeout_cb(1000, TIMEOUT_ONESHOT,
258+
cleanSockState_cb, arg);
259+
}
260+
246261
/**
247262
* Only used in TCP mode.
248263
*/
249264
static void atcp_conn_err_cb(void *arg, err_t err) {
250265
struct ttcp* ttcp = arg;
251266

252-
printk("TTCP [%p]: connection error: %d\n",
253-
ttcp, err);
267+
WARN("TTCP [%p]: connection error: %d arg:%p\n",
268+
ttcp, err, arg);
254269

255270
if (ifStatus == false)
256271
printk("Abort connection\n");
257272
cleanSockState_cb(ttcp);
258-
//ttcp->tpcb = NULL; /* free'd by lwip upon return */
259-
//ard_tcp_done(ttcp, err);
273+
pending_close = false;
260274
}
261275

262276
static void close_conn(struct ttcp *_ttcp) {
@@ -294,28 +308,31 @@ void closeConnections()
294308
static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
295309
err_t err) {
296310
struct ttcp* ttcp = arg;
311+
312+
INFO_TCP("pcb:%p p: %p err:%d\n", pcb, p, err);
313+
if (err == ERR_OK && p != NULL) {
314+
DATA_LED_ON();
315+
/* for print_stats() */
316+
ttcp->recved += p->tot_len;
317+
318+
if ((ttcp->verbose)||(verboseDebug & INFO_TCP_FLAG)) {
319+
INFO_TCP("Recv:%d\n",p->tot_len);
320+
DUMP_TCP(p->payload, p->tot_len);
321+
ttcp->print_cnt++;
322+
}
323+
324+
insert_pBuf(p, ttcp->sock, (void*) pcb);
325+
tcp_recved(pcb, p->tot_len);
326+
pbuf_free(p);
327+
DATA_LED_OFF();
328+
}
329+
297330
/* p will be NULL when remote end is done */
298-
if (p == NULL) {
331+
if (err == ERR_OK && p == NULL) {
299332
INFO_TCP("atcp_recv_cb p=NULL\n");
300-
//ard_tcp_done(ttcp, 0);
301333
close_conn(ttcp);
302-
return ERR_OK;
303-
}
304-
DATA_LED_ON();
305-
/* for print_stats() */
306-
ttcp->recved += p->tot_len;
307-
308-
if ((ttcp->verbose)||(verboseDebug & INFO_TCP_FLAG)) {
309-
INFO_TCP("Recv:%d\n",p->tot_len);
310-
//INFO_TCP("%s\n",(char*)p->payload);
311-
DUMP_TCP(p->payload, p->tot_len);
312-
ttcp->print_cnt++;
313334
}
314335

315-
insert_pBuf(p, ttcp->sock, (void*) pcb);
316-
tcp_recved(pcb, p->tot_len);
317-
pbuf_free(p);
318-
DATA_LED_OFF();
319336
return ERR_OK;
320337
}
321338

@@ -328,6 +345,14 @@ static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
328345
struct ttcp* _ttcp = arg;
329346
++tcp_poll_retries;
330347

348+
if (tcp_poll_retries > 4) {
349+
INFO_TCP("ARD TCP [%p] arg=%p retries=%d\n",
350+
pcb, arg, tcp_poll_retries);
351+
tcp_poll_retries = 0;
352+
tcp_abort(pcb);
353+
return ERR_ABRT;
354+
}
355+
331356
if (pending_close)
332357
{
333358
err_t err = tcp_close(pcb);
@@ -336,14 +361,12 @@ static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
336361
else
337362
pending_close = false;
338363

339-
INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, _ttcp->tpcb, pending_close);
340-
}
341-
if (tcp_poll_retries > 4) {
342-
tcp_abort(pcb);
343-
return ERR_ABRT;
364+
INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, (_ttcp)?_ttcp->tpcb:0, pending_close);
365+
}else{
366+
INFO_TCP("ARD TCP [%p-%p] arg=%p retries=%d\n", (_ttcp)?_ttcp->tpcb:0, pcb, arg, tcp_poll_retries);
367+
if (_ttcp) tcp_send_data(_ttcp);
368+
else WARN("ttcp NULL!");
344369
}
345-
//tcp_send_data(_ttcp);
346-
INFO_TCP("ARD TCP [%p] arg=%p retries=%d\n", pcb, arg, tcp_poll_retries);
347370

348371
return ERR_OK;
349372
}
@@ -386,6 +409,7 @@ static int atcp_start(struct ttcp* ttcp) {
386409
}
387410

388411
tcp_arg(ttcp->tpcb, ttcp);
412+
atcp_init();
389413

390414
if (ttcp->mode == TTCP_MODE_TRANSMIT) {
391415
tcp_err(ttcp->tpcb, atcp_conn_err_cb);
@@ -662,11 +686,10 @@ uint8_t getModeTcp(void* p) {
662686

663687
uint8_t isDataSent(void* p) {
664688
struct ttcp *_ttcp = (struct ttcp *)p;
665-
static int isDataSentCount = 0;
666689

667690
if ((_ttcp)&&(!_ttcp->buff_sent))
668691
{
669-
INFO_TCP("%d) Wait to send data\n", ++isDataSentCount);
692+
INFO_TCP_VER("%d) Wait to send data\n", ++isDataSentCount);
670693
return 0;
671694
}
672695

@@ -695,7 +718,6 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
695718
INFO_TCP_VER("buf:%p len:%d\n", buf, len);
696719
DUMP_TCP(buf,len);
697720

698-
startTime = timer_get_ms();
699721
struct ttcp* _ttcp = (struct ttcp*) p;
700722
if ((_ttcp != NULL) && (_ttcp->tpcb != NULL) &&
701723
(buf != NULL) && (len != 0) && (_ttcp->payload != NULL)) {

wifiHD/src/main.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ struct ctx_server {
8686
bool ifStatus = false;
8787
bool scanNetCompleted = false;
8888

89-
// to maintain the word alignment
90-
//#define PAD_CTX_SIZE 0x18
91-
//#define PAD_NETIF_SIZE 0x3c
92-
#define PAD_CTX_SIZE 0
93-
#define PAD_NETIF_SIZE 0
94-
9589
static bool initSpiComplete = false;
9690

9791
// variable used as enable flag for debug prints
@@ -396,11 +390,11 @@ main(void)
396390
#else
397391
printk("Arduino Wifi Startup... [%s]\n", __TIMESTAMP__);
398392

399-
size_t size_ctx_server = sizeof(struct ctx_server)+PAD_CTX_SIZE;
393+
size_t size_ctx_server = sizeof(struct ctx_server);
400394
hs = calloc(1, size_ctx_server);
401395
ASSERT(hs, "out of memory");
402396

403-
size_t size_netif = sizeof(struct netif)+PAD_NETIF_SIZE;
397+
size_t size_netif = sizeof(struct netif);
404398
hs->net_cfg.netif = calloc(1, size_netif);
405399
ASSERT(hs->net_cfg.netif, "out of memory");
406400

0 commit comments

Comments
 (0)