Skip to content

Commit 917e107

Browse files
committed
Fix issues on initial connection and on socket close
1 parent 5e1624a commit 917e107

File tree

7 files changed

+88
-25
lines changed

7 files changed

+88
-25
lines changed

binary/wifiHD.elf

2.78 KB
Binary file not shown.

wifiHD/Release/wifiHD.elf

2.78 KB
Binary file not shown.

wifiHD/src/ard_spi.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#define _BUFFERSIZE 100
7676

7777
extern void tcp_debug_print_pcbs(void);
78+
extern bool ifStatus;
7879

7980
static char buf[CMD_MAX_LEN];
8081
static char reply[REPLY_MAX_LEN];
@@ -623,10 +624,17 @@ int start_server_tcp(uint16_t port, uint8_t sock)
623624

624625
if (_connected)
625626
{
626-
printk("Still connected...wait\n");
627+
WARN("Still connected...wait\n");
627628
return WIFI_SPI_ERR;
628629
}
629630

631+
if (!ifStatus)
632+
{
633+
WARN("IF down...wait\n");
634+
return WIFI_SPI_ERR;
635+
}
636+
637+
630638
if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
631639
{
632640
INFO_SPI("Start Server [%d, %d] OK!\n", port, sock);
@@ -1714,6 +1722,7 @@ int initSpi()
17141722
memset(reply, 0, sizeof(reply));
17151723

17161724
initMapSockTcp();
1725+
set_result(WL_IDLE_STATUS);
17171726

17181727
init_pBuf();
17191728

wifiHD/src/ard_spi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void showTTCPstatus();
6060

6161
int getSock(void * _ttcp);
6262

63+
void* getTTCP(uint8_t sock);
64+
6365
void clearMapSockTcp(uint8_t sock);
6466

6567
int start_server_tcp(uint16_t port, uint8_t sock);

wifiHD/src/ard_tcp.c

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "debug.h"
3030

3131
unsigned int startTime = 0;
32+
extern bool ifStatus;
3233

3334
/**
3435
* Clean up and free the ttcp structure
@@ -49,7 +50,7 @@ static void ard_tcp_destroy(struct ttcp* ttcp) {
4950
if (ttcp->lpcb) {
5051
tcp_arg(ttcp->lpcb, NULL);
5152
tcp_accept(ttcp->lpcb, NULL);
52-
tcp_close(ttcp->lpcb);
53+
err = tcp_close(ttcp->lpcb);
5354
INFO_TCP("Closing lpcb: state:0x%x err:%d\n", ttcp->lpcb->state, err);
5455
}
5556

@@ -141,12 +142,12 @@ static void tcp_send_data(struct ttcp *ttcp) {
141142

142143
if (err == ERR_OK){
143144
tcp_output(ttcp->tpcb);
144-
INFO_TCP("tcp_output: left=%d new left:%d\n",
145+
INFO_TCP_VER("tcp_output: left=%d new left:%d\n",
145146
ttcp->left, ttcp->left-len);
146147
ttcp->left -= len;
147148
}
148149
else
149-
WARN("TTCP [%p]: tcp_write failed\n", ttcp);
150+
WARN("TTCP [%p]: tcp_write failed err:%d len:%d\n", ttcp, err, len);
150151
//
151152
// ttcp->tid = timer_sched_timeout_cb(0, TIMEOUT_ONESHOT,
152153
// tcp_timeout_cb, ttcp);
@@ -220,20 +221,57 @@ static err_t tcp_connect_cb(void *arg, struct tcp_pcb *tpcb, err_t err) {
220221
return ERR_OK;
221222
}
222223

224+
static void cleanSockState_cb(void *ctx) {
225+
struct ttcp* ttcp = ctx;
226+
227+
int sock = getSock(ttcp);
228+
if (sock != -1)
229+
clearMapSockTcp(sock);
230+
printk("TTCP [%p]: cleanSockState_cb %d\n", ttcp, sock);
231+
_connected = false;
232+
}
233+
223234
/**
224235
* Only used in TCP mode.
225236
*/
226237
static void atcp_conn_err_cb(void *arg, err_t err) {
227238
struct ttcp* ttcp = arg;
228239

229-
printk("TTCP [%p]: connection error:0x%x\n", ttcp, err);
240+
printk("TTCP [%p]: connection error:%s [%d]\n",
241+
ttcp, lwip_strerr(err), err);
230242

231-
ttcp->tpcb = NULL; /* free'd by lwip upon return */
232-
int sock = getSock(ttcp);
233-
if (sock)
234-
clearMapSockTcp(sock);
235-
_connected = false;
236-
ard_tcp_done(ttcp, err);
243+
if (ifStatus == false)
244+
printk("Abort connection\n");
245+
cleanSockState_cb(ttcp);
246+
//ttcp->tpcb = NULL; /* free'd by lwip upon return */
247+
//ard_tcp_done(ttcp, err);
248+
}
249+
250+
static void close_conn(struct ttcp *_ttcp) {
251+
tcp_arg(_ttcp->tpcb, NULL);
252+
tcp_sent(_ttcp->tpcb, NULL);
253+
tcp_recv(_ttcp->tpcb, NULL);
254+
err_t err = tcp_close(_ttcp->tpcb);
255+
INFO_TCP("Closing tpcb[%p]: state:0x%x err:%d\n",_ttcp->tpcb, _ttcp->tpcb->state, err);
256+
}
257+
258+
void closeConnections()
259+
{
260+
int i = 0;
261+
for (; i<MAX_SOCK_NUM; i++)
262+
{
263+
void* p = getTTCP(i);
264+
if (p)
265+
{
266+
ttcp_t* _ttcp = (ttcp_t* )p;
267+
268+
INFO_TCP("Closing connections tpcb[%p] state:0x%x - lpcb[%p] state: 0x%x\n",
269+
_ttcp->tpcb, _ttcp->tpcb->state, _ttcp->lpcb, _ttcp->lpcb->state);
270+
//tcp_close(_ttcp->tpcb);
271+
ard_tcp_destroy(_ttcp);
272+
cleanSockState_cb(_ttcp);
273+
}
274+
}
237275
}
238276

239277
/**
@@ -245,7 +283,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
245283
/* p will be NULL when remote end is done */
246284
if (p == NULL) {
247285
INFO_TCP("atcp_recv_cb p=NULL\n");
248-
ard_tcp_done(ttcp, 0);
286+
//ard_tcp_done(ttcp, 0);
287+
close_conn(ttcp);
249288
return ERR_OK;
250289
}
251290
DATA_LED_ON();
@@ -260,8 +299,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
260299
}
261300

262301
insert_pBuf(p, ttcp->sock, (void*) pcb);
263-
pbuf_free(p);
264302
tcp_recved(pcb, p->tot_len);
303+
pbuf_free(p);
265304
DATA_LED_OFF();
266305
return ERR_OK;
267306
}
@@ -272,6 +311,8 @@ void ack_recved(void* pcb, int len) {
272311
}
273312

274313
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);
275316
return ERR_OK;
276317
}
277318

@@ -281,6 +322,8 @@ static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
281322
static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
282323
struct ttcp* ttcp = arg;
283324

325+
tcp_setprio(newpcb, TCP_PRIO_MIN);
326+
284327
ttcp->tpcb = newpcb;
285328
tcp_recv(ttcp->tpcb, atcp_recv_cb);
286329
tcp_err(ttcp->tpcb, atcp_conn_err_cb);
@@ -332,7 +375,7 @@ static int atcp_start(struct ttcp* ttcp) {
332375

333376
err = tcp_bind(ttcp->tpcb, IP_ADDR_ANY, ttcp->port);
334377
if (err != ERR_OK){
335-
WARN("TTCP [%p]: bind failed err=%d\n", ttcp, err);
378+
WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
336379
return -1;
337380
}
338381

@@ -543,33 +586,34 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
543586

544587
return 0;
545588

546-
fail: ard_tcp_abort(ttcp);
589+
//fail: ard_tcp_abort(ttcp);
590+
fail: ard_tcp_destroy(ttcp);
547591
return -1;
548592
}
549593

550-
static void close_conn(struct ttcp *_ttcp) {
551-
ard_tcp_done(_ttcp, 0);
552-
}
553-
554594
void ard_tcp_stop(void* ttcp) {
555595
struct ttcp* _ttcp = (struct ttcp*) ttcp;
556596
INFO_TCP("Closing connection...\n");
557597
DUMP_TCP_STATE(_ttcp);
558598
if ((_ttcp)&&(_ttcp->tpcb)&&(_ttcp->tpcb->state!=LAST_ACK)&&(_ttcp->tpcb->state!=CLOSED))
599+
{
559600
close_conn(_ttcp);
601+
}
560602
}
561603

562604
uint8_t getStateTcp(void* p, bool client) {
563605
struct ttcp* _ttcp = (struct ttcp*) p;
564606

607+
if (ifStatus == false)
608+
return CLOSED;
565609
if ((_ttcp != NULL) && (_ttcp->tpcb != NULL)) {
566610
//DUMP_TCP_STATE(_ttcp);
567611
if (client)
568612
return _ttcp->tpcb->state;
569613
else
570614
return _ttcp->lpcb->state;
571615
} else {
572-
INFO_TCP("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
616+
INFO_TCP_VER("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
573617
_ttcp, ((_ttcp)?_ttcp->tpcb:0), ((_ttcp)?_ttcp->lpcb:0));
574618
}
575619
return CLOSED;
@@ -611,18 +655,18 @@ static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
611655

612656
if (_ttcp->left > 0) {
613657
//send_data(pcb, hs);
614-
INFO_TCP("data left: %d", _ttcp->left );
658+
INFO_TCP_VER("data left: %d", _ttcp->left );
615659
}
616660
if (_ttcp->buff_sent == 1)
617661
WARN("Previous packet already\n");
618662
_ttcp->buff_sent = 1;
619-
INFO_TCP("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime);
663+
INFO_TCP_VER("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime);
620664
//INFO_TCP("%s: duration: %d\n", __FUNCTION__, timer_get_ms() - startTime);
621665
return ERR_OK;
622666
}
623667

624668
int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
625-
INFO_TCP("buf:%p len:%d\n", buf, len);
669+
INFO_TCP_VER("buf:%p len:%d\n", buf, len);
626670
DUMP_TCP(buf,len);
627671

628672
startTime = timer_get_ms();
@@ -637,7 +681,7 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
637681
//pbuf_take(buf, len, _ttcp->);
638682
memcpy(_ttcp->payload, buf, len);
639683
_ttcp->payload[len]='\0';
640-
INFO_TCP("%s\n", _ttcp->payload);
684+
INFO_TCP_VER("%s\n", _ttcp->payload);
641685
_ttcp->left = len;
642686
tcp_sent(_ttcp->tpcb, tcp_data_sent);
643687
tcp_send_data(_ttcp);

wifiHD/src/ard_tcp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ uint8_t isDataSent(void* p );
6666

6767
cmd_state_t cmd_ttcp(int argc, char* argv[], void* ctx);
6868

69+
void closeConnections();
70+
6971
#endif

wifiHD/src/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ struct ctx_server {
8383
uint8_t wl_init_complete;
8484
};
8585

86+
bool ifStatus = false;
87+
8688
// to maintain the word alignment
8789
//#define PAD_CTX_SIZE 0x18
8890
//#define PAD_NETIF_SIZE 0x3c
@@ -101,6 +103,7 @@ uint16_t verboseDebug = 0;
101103
static void
102104
wl_cm_scan_cb(void* ctx)
103105
{
106+
INFO_INIT("Scan Completed!\n");
104107
set_result(WL_SCAN_COMPLETED);
105108
}
106109

@@ -174,8 +177,11 @@ ip_status_cb(struct netif* netif)
174177
INFO_INIT("IP status cb...\n");
175178
if (netif_is_up(netif)) {
176179
set_result_cmd(WL_SUCCESS);
177-
printk("bound to %s\n", ip2str(netif->ip_addr));
180+
printk("bound to %s\n", ip2str(netif->ip_addr));
181+
ifStatus = true;
178182
}else{
183+
ifStatus = false;
184+
closeConnections();
179185
WARN("Interface not up!\n");
180186
}
181187
}

0 commit comments

Comments
 (0)