Skip to content

Commit 66cf74a

Browse files
committed
Fixed WebServer Issue
1 parent a5e4f5a commit 66cf74a

File tree

5 files changed

+59
-33
lines changed

5 files changed

+59
-33
lines changed

binary/wifiHD.elf

-3.19 KB
Binary file not shown.

wifiHD/Release/wifiHD.elf

-3.19 KB
Binary file not shown.

wifiHD/src/ard_spi.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ int start_server_tcp(uint16_t port, uint8_t sock)
642642
setMapSock(sock, _ttcp);
643643
err = WL_SUCCESS;
644644
}else{
645+
645646
INFO_SPI("Start Server [%d, %d] FAILED!\n", port, sock);
646647
clearMapSockTcp(sock);
647648
}
@@ -741,7 +742,8 @@ int ack_cmd_cb(int numParam, char* buf, void* ctx) {
741742
}
742743

743744
int get_result_cmd_cb(int numParam, char* buf, void* ctx) {
744-
*buf=result;
745+
INFO_SPI("ifStatus:%d result:%d\n", ifStatus, result);
746+
*buf=(ifStatus)?WL_CONNECTED:result;
745747
return WIFI_SPI_ACK;
746748
}
747749

wifiHD/src/ard_tcp.c

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
unsigned int startTime = 0;
3232
extern bool ifStatus;
33+
static uint8_t tcp_poll_retries = 0;
34+
35+
bool pending_close = false;
3336

3437
/**
3538
* Clean up and free the ttcp structure
@@ -120,10 +123,12 @@ tcp_timeout_cb(void *ctx);
120123
*
121124
*/
122125
static void tcp_send_data(struct ttcp *ttcp) {
123-
err_t err;
126+
err_t err = ERR_OK;
124127
uint32_t len;
125128

126129
len = ttcp->left;
130+
INFO_TCP("left=%d len:%d tcp_sndbuf:%d\n", ttcp->left, len, tcp_sndbuf(ttcp->tpcb));
131+
127132

128133
/* don't send more than we have in the payload */
129134
if (len > ttcp->buflen)
@@ -134,14 +139,21 @@ static void tcp_send_data(struct ttcp *ttcp) {
134139
if (len > tcp_sndbuf(ttcp->tpcb))
135140
len = tcp_sndbuf(ttcp->tpcb);
136141

142+
uint8_t count = 0;
137143
do {
138144
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);
139146
if (err == ERR_MEM)
147+
{
140148
len /= 2;
149+
ttcp->buff_sent = 0;
150+
}else{
151+
ttcp->buff_sent = 1;
152+
}
141153
} while (err == ERR_MEM && len > 1);
142154

143155
if (err == ERR_OK){
144-
tcp_output(ttcp->tpcb);
156+
//tcp_output(ttcp->tpcb);
145157
INFO_TCP_VER("tcp_output: left=%d new left:%d\n",
146158
ttcp->left, ttcp->left-len);
147159
ttcp->left -= len;
@@ -237,8 +249,8 @@ static void cleanSockState_cb(void *ctx) {
237249
static void atcp_conn_err_cb(void *arg, err_t err) {
238250
struct ttcp* ttcp = arg;
239251

240-
printk("TTCP [%p]: connection error:%s [%d]\n",
241-
ttcp, lwip_strerr(err), err);
252+
printk("TTCP [%p]: connection error: %d\n",
253+
ttcp, err);
242254

243255
if (ifStatus == false)
244256
printk("Abort connection\n");
@@ -252,6 +264,8 @@ static void close_conn(struct ttcp *_ttcp) {
252264
tcp_sent(_ttcp->tpcb, NULL);
253265
tcp_recv(_ttcp->tpcb, NULL);
254266
err_t err = tcp_close(_ttcp->tpcb);
267+
if (err == ERR_MEM)
268+
pending_close = true;
255269
INFO_TCP("Closing tpcb[%p]: state:0x%x err:%d\n",_ttcp->tpcb, _ttcp->tpcb->state, err);
256270
}
257271

@@ -311,8 +325,26 @@ void ack_recved(void* pcb, int len) {
311325
}
312326

313327
static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
314-
INFO_TCP("ARD TCP [%p] arg=%p\n", pcb, arg);
315-
//tcp_abort(pcb);
328+
struct ttcp* _ttcp = arg;
329+
++tcp_poll_retries;
330+
331+
if (pending_close)
332+
{
333+
err_t err = tcp_close(pcb);
334+
if (err == ERR_MEM)
335+
pending_close = true;
336+
else
337+
pending_close = false;
338+
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;
344+
}
345+
//tcp_send_data(_ttcp);
346+
INFO_TCP("ARD TCP [%p] arg=%p retries=%d\n", pcb, arg, tcp_poll_retries);
347+
316348
return ERR_OK;
317349
}
318350

@@ -323,7 +355,7 @@ static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
323355
struct ttcp* ttcp = arg;
324356

325357
tcp_setprio(newpcb, TCP_PRIO_MIN);
326-
358+
tcp_poll_retries = 0;
327359
ttcp->tpcb = newpcb;
328360
tcp_recv(ttcp->tpcb, atcp_recv_cb);
329361
tcp_err(ttcp->tpcb, atcp_conn_err_cb);
@@ -593,7 +625,8 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
593625

594626
void ard_tcp_stop(void* ttcp) {
595627
struct ttcp* _ttcp = (struct ttcp*) ttcp;
596-
INFO_TCP("Closing connection...\n");
628+
629+
INFO_TCP("Closing connection...state:%d\n", _ttcp->tpcb->state);
597630
DUMP_TCP_STATE(_ttcp);
598631
if ((_ttcp)&&(_ttcp->tpcb)&&(_ttcp->tpcb->state!=LAST_ACK)&&(_ttcp->tpcb->state!=CLOSED))
599632
{
@@ -627,23 +660,17 @@ uint8_t getModeTcp(void* p) {
627660
return 0;
628661
}
629662

630-
631663
uint8_t isDataSent(void* p) {
664+
struct ttcp *_ttcp = (struct ttcp *)p;
632665
static int isDataSentCount = 0;
633-
struct ttcp* _ttcp = (struct ttcp*) p;
634-
if ((_ttcp != NULL) && (_ttcp->tpcb != NULL)) {
635-
//INFO_TCP("ttcp:%p tpcb:%p sent:%d\n",p, _ttcp->tpcb, _ttcp->buff_sent);
636-
bool dataSent = _ttcp->buff_sent;
637-
if (dataSent) isDataSentCount = 0;
638-
else ++isDataSentCount;
639-
if (isDataSentCount == 250)
640-
WARN("data not sent\n");
641-
return dataSent;
642-
} else {
643-
WARN("TCP null!\n");
666+
667+
if ((_ttcp)&&(!_ttcp->buff_sent))
668+
{
669+
INFO_TCP("%d) Wait to send data\n", ++isDataSentCount);
670+
return 0;
644671
}
645672

646-
return -1;
673+
return 1;
647674
}
648675

649676
static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
@@ -653,15 +680,14 @@ static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
653680

654681
_ttcp = arg;
655682

683+
tcp_poll_retries = 0;
684+
656685
if (_ttcp->left > 0) {
657-
//send_data(pcb, hs);
658-
INFO_TCP_VER("data left: %d", _ttcp->left );
659-
}
660-
if (_ttcp->buff_sent == 1)
661-
WARN("Previous packet already\n");
662-
_ttcp->buff_sent = 1;
663-
INFO_TCP_VER("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime);
664-
//INFO_TCP("%s: duration: %d\n", __FUNCTION__, timer_get_ms() - startTime);
686+
INFO_TCP("data left: %d\n", _ttcp->left );
687+
tcp_send_data(_ttcp);
688+
}
689+
690+
INFO_TCP("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime);
665691
return ERR_OK;
666692
}
667693

@@ -677,8 +703,7 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
677703
_ttcp->tpcb->state == CLOSE_WAIT ||
678704
_ttcp->tpcb->state == SYN_SENT ||
679705
_ttcp->tpcb->state == SYN_RCVD) {
680-
_ttcp->buff_sent = 0;
681-
//pbuf_take(buf, len, _ttcp->);
706+
682707
memcpy(_ttcp->payload, buf, len);
683708
_ttcp->payload[len]='\0';
684709
INFO_TCP_VER("%s\n", _ttcp->payload);

wifiHD/src/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ static void
105105
wl_cm_scan_cb(void* ctx)
106106
{
107107
INFO_INIT("Scan Completed!\n");
108-
set_result(WL_SCAN_COMPLETED);
109108
scanNetCompleted=true;
110109
}
111110

0 commit comments

Comments
 (0)