1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Fix issues on initial connection and on socket close

This commit is contained in:
Mimmo La Fauci 2012-05-23 08:34:20 +02:00
parent 5e1624a5e4
commit 917e107c72
7 changed files with 88 additions and 25 deletions

Binary file not shown.

Binary file not shown.

View File

@ -75,6 +75,7 @@
#define _BUFFERSIZE 100 #define _BUFFERSIZE 100
extern void tcp_debug_print_pcbs(void); extern void tcp_debug_print_pcbs(void);
extern bool ifStatus;
static char buf[CMD_MAX_LEN]; static char buf[CMD_MAX_LEN];
static char reply[REPLY_MAX_LEN]; static char reply[REPLY_MAX_LEN];
@ -623,10 +624,17 @@ int start_server_tcp(uint16_t port, uint8_t sock)
if (_connected) if (_connected)
{ {
printk("Still connected...wait\n"); WARN("Still connected...wait\n");
return WIFI_SPI_ERR; return WIFI_SPI_ERR;
} }
if (!ifStatus)
{
WARN("IF down...wait\n");
return WIFI_SPI_ERR;
}
if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0) if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
{ {
INFO_SPI("Start Server [%d, %d] OK!\n", port, sock); INFO_SPI("Start Server [%d, %d] OK!\n", port, sock);
@ -1714,6 +1722,7 @@ int initSpi()
memset(reply, 0, sizeof(reply)); memset(reply, 0, sizeof(reply));
initMapSockTcp(); initMapSockTcp();
set_result(WL_IDLE_STATUS);
init_pBuf(); init_pBuf();

View File

@ -60,6 +60,8 @@ void showTTCPstatus();
int getSock(void * _ttcp); int getSock(void * _ttcp);
void* getTTCP(uint8_t sock);
void clearMapSockTcp(uint8_t sock); void clearMapSockTcp(uint8_t sock);
int start_server_tcp(uint16_t port, uint8_t sock); int start_server_tcp(uint16_t port, uint8_t sock);

View File

@ -29,6 +29,7 @@
#include "debug.h" #include "debug.h"
unsigned int startTime = 0; unsigned int startTime = 0;
extern bool ifStatus;
/** /**
* Clean up and free the ttcp structure * Clean up and free the ttcp structure
@ -49,7 +50,7 @@ static void ard_tcp_destroy(struct ttcp* ttcp) {
if (ttcp->lpcb) { if (ttcp->lpcb) {
tcp_arg(ttcp->lpcb, NULL); tcp_arg(ttcp->lpcb, NULL);
tcp_accept(ttcp->lpcb, NULL); tcp_accept(ttcp->lpcb, NULL);
tcp_close(ttcp->lpcb); err = tcp_close(ttcp->lpcb);
INFO_TCP("Closing lpcb: state:0x%x err:%d\n", ttcp->lpcb->state, err); INFO_TCP("Closing lpcb: state:0x%x err:%d\n", ttcp->lpcb->state, err);
} }
@ -141,12 +142,12 @@ static void tcp_send_data(struct ttcp *ttcp) {
if (err == ERR_OK){ if (err == ERR_OK){
tcp_output(ttcp->tpcb); tcp_output(ttcp->tpcb);
INFO_TCP("tcp_output: left=%d new left:%d\n", INFO_TCP_VER("tcp_output: left=%d new left:%d\n",
ttcp->left, ttcp->left-len); ttcp->left, ttcp->left-len);
ttcp->left -= len; ttcp->left -= len;
} }
else else
WARN("TTCP [%p]: tcp_write failed\n", ttcp); WARN("TTCP [%p]: tcp_write failed err:%d len:%d\n", ttcp, err, len);
// //
// ttcp->tid = timer_sched_timeout_cb(0, TIMEOUT_ONESHOT, // ttcp->tid = timer_sched_timeout_cb(0, TIMEOUT_ONESHOT,
// tcp_timeout_cb, ttcp); // tcp_timeout_cb, ttcp);
@ -220,20 +221,57 @@ static err_t tcp_connect_cb(void *arg, struct tcp_pcb *tpcb, err_t err) {
return ERR_OK; return ERR_OK;
} }
static void cleanSockState_cb(void *ctx) {
struct ttcp* ttcp = ctx;
int sock = getSock(ttcp);
if (sock != -1)
clearMapSockTcp(sock);
printk("TTCP [%p]: cleanSockState_cb %d\n", ttcp, sock);
_connected = false;
}
/** /**
* Only used in TCP mode. * Only used in TCP mode.
*/ */
static void atcp_conn_err_cb(void *arg, err_t err) { static void atcp_conn_err_cb(void *arg, err_t err) {
struct ttcp* ttcp = arg; struct ttcp* ttcp = arg;
printk("TTCP [%p]: connection error:0x%x\n", ttcp, err); printk("TTCP [%p]: connection error:%s [%d]\n",
ttcp, lwip_strerr(err), err);
ttcp->tpcb = NULL; /* free'd by lwip upon return */ if (ifStatus == false)
int sock = getSock(ttcp); printk("Abort connection\n");
if (sock) cleanSockState_cb(ttcp);
clearMapSockTcp(sock); //ttcp->tpcb = NULL; /* free'd by lwip upon return */
_connected = false; //ard_tcp_done(ttcp, err);
ard_tcp_done(ttcp, err); }
static void close_conn(struct ttcp *_ttcp) {
tcp_arg(_ttcp->tpcb, NULL);
tcp_sent(_ttcp->tpcb, NULL);
tcp_recv(_ttcp->tpcb, NULL);
err_t err = tcp_close(_ttcp->tpcb);
INFO_TCP("Closing tpcb[%p]: state:0x%x err:%d\n",_ttcp->tpcb, _ttcp->tpcb->state, err);
}
void closeConnections()
{
int i = 0;
for (; i<MAX_SOCK_NUM; i++)
{
void* p = getTTCP(i);
if (p)
{
ttcp_t* _ttcp = (ttcp_t* )p;
INFO_TCP("Closing connections tpcb[%p] state:0x%x - lpcb[%p] state: 0x%x\n",
_ttcp->tpcb, _ttcp->tpcb->state, _ttcp->lpcb, _ttcp->lpcb->state);
//tcp_close(_ttcp->tpcb);
ard_tcp_destroy(_ttcp);
cleanSockState_cb(_ttcp);
}
}
} }
/** /**
@ -245,7 +283,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
/* p will be NULL when remote end is done */ /* p will be NULL when remote end is done */
if (p == NULL) { if (p == NULL) {
INFO_TCP("atcp_recv_cb p=NULL\n"); INFO_TCP("atcp_recv_cb p=NULL\n");
ard_tcp_done(ttcp, 0); //ard_tcp_done(ttcp, 0);
close_conn(ttcp);
return ERR_OK; return ERR_OK;
} }
DATA_LED_ON(); DATA_LED_ON();
@ -260,8 +299,8 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
} }
insert_pBuf(p, ttcp->sock, (void*) pcb); insert_pBuf(p, ttcp->sock, (void*) pcb);
pbuf_free(p);
tcp_recved(pcb, p->tot_len); tcp_recved(pcb, p->tot_len);
pbuf_free(p);
DATA_LED_OFF(); DATA_LED_OFF();
return ERR_OK; return ERR_OK;
} }
@ -272,6 +311,8 @@ void ack_recved(void* pcb, int len) {
} }
static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) { static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
INFO_TCP("ARD TCP [%p] arg=%p\n", pcb, arg);
//tcp_abort(pcb);
return ERR_OK; return ERR_OK;
} }
@ -281,6 +322,8 @@ static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) { static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
struct ttcp* ttcp = arg; struct ttcp* ttcp = arg;
tcp_setprio(newpcb, TCP_PRIO_MIN);
ttcp->tpcb = newpcb; ttcp->tpcb = newpcb;
tcp_recv(ttcp->tpcb, atcp_recv_cb); tcp_recv(ttcp->tpcb, atcp_recv_cb);
tcp_err(ttcp->tpcb, atcp_conn_err_cb); tcp_err(ttcp->tpcb, atcp_conn_err_cb);
@ -332,7 +375,7 @@ static int atcp_start(struct ttcp* ttcp) {
err = tcp_bind(ttcp->tpcb, IP_ADDR_ANY, ttcp->port); err = tcp_bind(ttcp->tpcb, IP_ADDR_ANY, ttcp->port);
if (err != ERR_OK){ if (err != ERR_OK){
WARN("TTCP [%p]: bind failed err=%d\n", ttcp, err); WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
return -1; return -1;
} }
@ -543,25 +586,26 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
return 0; return 0;
fail: ard_tcp_abort(ttcp); //fail: ard_tcp_abort(ttcp);
fail: ard_tcp_destroy(ttcp);
return -1; return -1;
} }
static void close_conn(struct ttcp *_ttcp) {
ard_tcp_done(_ttcp, 0);
}
void ard_tcp_stop(void* ttcp) { void ard_tcp_stop(void* ttcp) {
struct ttcp* _ttcp = (struct ttcp*) ttcp; struct ttcp* _ttcp = (struct ttcp*) ttcp;
INFO_TCP("Closing connection...\n"); INFO_TCP("Closing connection...\n");
DUMP_TCP_STATE(_ttcp); DUMP_TCP_STATE(_ttcp);
if ((_ttcp)&&(_ttcp->tpcb)&&(_ttcp->tpcb->state!=LAST_ACK)&&(_ttcp->tpcb->state!=CLOSED)) if ((_ttcp)&&(_ttcp->tpcb)&&(_ttcp->tpcb->state!=LAST_ACK)&&(_ttcp->tpcb->state!=CLOSED))
{
close_conn(_ttcp); close_conn(_ttcp);
}
} }
uint8_t getStateTcp(void* p, bool client) { uint8_t getStateTcp(void* p, bool client) {
struct ttcp* _ttcp = (struct ttcp*) p; struct ttcp* _ttcp = (struct ttcp*) p;
if (ifStatus == false)
return CLOSED;
if ((_ttcp != NULL) && (_ttcp->tpcb != NULL)) { if ((_ttcp != NULL) && (_ttcp->tpcb != NULL)) {
//DUMP_TCP_STATE(_ttcp); //DUMP_TCP_STATE(_ttcp);
if (client) if (client)
@ -569,7 +613,7 @@ uint8_t getStateTcp(void* p, bool client) {
else else
return _ttcp->lpcb->state; return _ttcp->lpcb->state;
} else { } else {
INFO_TCP("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n", INFO_TCP_VER("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
_ttcp, ((_ttcp)?_ttcp->tpcb:0), ((_ttcp)?_ttcp->lpcb:0)); _ttcp, ((_ttcp)?_ttcp->tpcb:0), ((_ttcp)?_ttcp->lpcb:0));
} }
return CLOSED; return CLOSED;
@ -611,18 +655,18 @@ static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
if (_ttcp->left > 0) { if (_ttcp->left > 0) {
//send_data(pcb, hs); //send_data(pcb, hs);
INFO_TCP("data left: %d", _ttcp->left ); INFO_TCP_VER("data left: %d", _ttcp->left );
} }
if (_ttcp->buff_sent == 1) if (_ttcp->buff_sent == 1)
WARN("Previous packet already\n"); WARN("Previous packet already\n");
_ttcp->buff_sent = 1; _ttcp->buff_sent = 1;
INFO_TCP("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime); INFO_TCP_VER("Packet sent len:%d dur:%d\n", len, timer_get_ms() - startTime);
//INFO_TCP("%s: duration: %d\n", __FUNCTION__, timer_get_ms() - startTime); //INFO_TCP("%s: duration: %d\n", __FUNCTION__, timer_get_ms() - startTime);
return ERR_OK; return ERR_OK;
} }
int sendTcpData(void* p, uint8_t* buf, uint16_t len) { int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
INFO_TCP("buf:%p len:%d\n", buf, len); INFO_TCP_VER("buf:%p len:%d\n", buf, len);
DUMP_TCP(buf,len); DUMP_TCP(buf,len);
startTime = timer_get_ms(); startTime = timer_get_ms();
@ -637,7 +681,7 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
//pbuf_take(buf, len, _ttcp->); //pbuf_take(buf, len, _ttcp->);
memcpy(_ttcp->payload, buf, len); memcpy(_ttcp->payload, buf, len);
_ttcp->payload[len]='\0'; _ttcp->payload[len]='\0';
INFO_TCP("%s\n", _ttcp->payload); INFO_TCP_VER("%s\n", _ttcp->payload);
_ttcp->left = len; _ttcp->left = len;
tcp_sent(_ttcp->tpcb, tcp_data_sent); tcp_sent(_ttcp->tpcb, tcp_data_sent);
tcp_send_data(_ttcp); tcp_send_data(_ttcp);

View File

@ -66,4 +66,6 @@ uint8_t isDataSent(void* p );
cmd_state_t cmd_ttcp(int argc, char* argv[], void* ctx); cmd_state_t cmd_ttcp(int argc, char* argv[], void* ctx);
void closeConnections();
#endif #endif

View File

@ -83,6 +83,8 @@ struct ctx_server {
uint8_t wl_init_complete; uint8_t wl_init_complete;
}; };
bool ifStatus = false;
// to maintain the word alignment // to maintain the word alignment
//#define PAD_CTX_SIZE 0x18 //#define PAD_CTX_SIZE 0x18
//#define PAD_NETIF_SIZE 0x3c //#define PAD_NETIF_SIZE 0x3c
@ -101,6 +103,7 @@ uint16_t verboseDebug = 0;
static void static void
wl_cm_scan_cb(void* ctx) wl_cm_scan_cb(void* ctx)
{ {
INFO_INIT("Scan Completed!\n");
set_result(WL_SCAN_COMPLETED); set_result(WL_SCAN_COMPLETED);
} }
@ -174,8 +177,11 @@ ip_status_cb(struct netif* netif)
INFO_INIT("IP status cb...\n"); INFO_INIT("IP status cb...\n");
if (netif_is_up(netif)) { if (netif_is_up(netif)) {
set_result_cmd(WL_SUCCESS); set_result_cmd(WL_SUCCESS);
printk("bound to %s\n", ip2str(netif->ip_addr)); printk("bound to %s\n", ip2str(netif->ip_addr));
ifStatus = true;
}else{ }else{
ifStatus = false;
closeConnections();
WARN("Interface not up!\n"); WARN("Interface not up!\n");
} }
} }