aboutsummaryrefslogtreecommitdiff
path: root/firmwares/wifishield/wifiHD/src
diff options
context:
space:
mode:
Diffstat (limited to 'firmwares/wifishield/wifiHD/src')
-rw-r--r--firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h1
-rw-r--r--firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/include/lwip/opt.h2
-rw-r--r--firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/UTILS/DEBUG/debug.c14
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_spi.c450
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_spi.h24
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_tcp.c601
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_tcp.h28
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_utils.c232
-rw-r--r--firmwares/wifishield/wifiHD/src/ard_utils.h57
-rw-r--r--firmwares/wifishield/wifiHD/src/cmd_wl.c165
-rw-r--r--firmwares/wifishield/wifiHD/src/cmd_wl.h5
-rw-r--r--firmwares/wifishield/wifiHD/src/console.c2
-rw-r--r--firmwares/wifishield/wifiHD/src/debug.h187
-rw-r--r--firmwares/wifishield/wifiHD/src/lwipopts.h2
-rw-r--r--firmwares/wifishield/wifiHD/src/main.c19
-rw-r--r--firmwares/wifishield/wifiHD/src/wifi_spi.h3
-rw-r--r--firmwares/wifishield/wifiHD/src/wl_cm.c8
17 files changed, 1208 insertions, 592 deletions
diff --git a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h
index 2b78ab6..e687723 100644
--- a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h
+++ b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h
@@ -93,6 +93,7 @@
#define LED1_GPIO AVR32_PIN_PB20
#define LED2_GPIO AVR32_PIN_PB21
#define DEB_PIN_GPIO AVR32_PIN_PA20
+#define DEB2_PIN_GPIO AVR32_PIN_PB00
//! @}
/*! \name PWM Channels of LEDs
diff --git a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/include/lwip/opt.h b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/include/lwip/opt.h
index f70f646..a7cdbd8 100644
--- a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/include/lwip/opt.h
+++ b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/SERVICES/LWIP/lwip-1.3.2/src/include/lwip/opt.h
@@ -1226,7 +1226,7 @@
* in seconds. (does not require sockets.c, and will affect tcp.c)
*/
#ifndef LWIP_TCP_KEEPALIVE
-#define LWIP_TCP_KEEPALIVE 0
+#define LWIP_TCP_KEEPALIVE 1
#endif
/**
diff --git a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/UTILS/DEBUG/debug.c b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/UTILS/DEBUG/debug.c
index c7c0a03..fe8a2a0 100644
--- a/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/UTILS/DEBUG/debug.c
+++ b/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/UTILS/DEBUG/debug.c
@@ -46,6 +46,7 @@
#include "compiler.h"
#include "debug.h"
+#include "util.h"
#if (defined __GNUC__)
@@ -117,3 +118,16 @@ void uc3_round_trace(U32 val)
//Enable_global_interrupt();
}
+
+void dump(char* _buf, uint16_t _count) {
+
+ int i;
+ for (i = 0; i < _count; ++i)
+ {
+ printk("0x%x ", _buf[i]);
+ if ((i!=0)&&(i % 10 == 0))
+ printk("\n\t");
+ }
+ printk("\n");
+}
+
diff --git a/firmwares/wifishield/wifiHD/src/ard_spi.c b/firmwares/wifishield/wifiHD/src/ard_spi.c
index a5ef895..ae92a62 100644
--- a/firmwares/wifishield/wifiHD/src/ard_spi.c
+++ b/firmwares/wifishield/wifiHD/src/ard_spi.c
@@ -24,6 +24,8 @@
#include "timer.h"
#include "lwip/dns.h"
#include <board_init.h>
+#include "util.h"
+#include "lwip/udp.h"
extern const char* fwVersion;
@@ -92,7 +94,7 @@ bool end_write = false; //TODO only for debug
// Signal indicating a new command is coming from SPI interface
static volatile Bool startRecvCmdSignal = FALSE;
-#define MAX_CMD_NUM 30
+#define MAX_CMD_NUM 34
typedef struct sCmd_spi_list{
cmd_spi_cb_t cb;
char cmd_id;
@@ -136,6 +138,7 @@ void initStatSpi()
void printStatSpi()
{
+ printk("totSpiCmds\t: 0x%x\n", cmdCorr);
printk("lastCmd \t: 0x%x\n", statSpi.lastCmd);
printk("lastErr \t: 0x%x\n", statSpi.lastError);
printk("spiStatus\t: 0x%X\n", statSpi.status);
@@ -169,7 +172,28 @@ cmd_resetStatSpi(int argc, char* argv[], void* ctx)
int result = WL_CONNECT_FAILED; //Store the result of the last operation
-void* mapSockTCP[MAX_SOCK_NUM];
+void* mapSockTCP[MAX_SOCK_NUM][MAX_MODE_NUM];
+
+//Udp RemoteIp and remote Port
+static tRemoteClient remoteClients[MAX_SOCK_NUM] = {{0,0}};
+
+void setRemoteClient(uint16_t sock, uint32_t _ipaddr, uint16_t _port)
+{
+ if (sock < MAX_SOCK_NUM)
+ {
+ remoteClients[sock].ipaddr = _ipaddr;
+ remoteClients[sock].port = _port;
+ }
+}
+
+tRemoteClient* getRemoteClient(uint16_t sock)
+{
+ if (sock < MAX_SOCK_NUM)
+ {
+ return &remoteClients[sock];
+ }
+ return NULL;
+}
struct netif* ard_netif = NULL;
@@ -180,10 +204,10 @@ struct ip_addr _hostIpAddr;
static bool hostIpAddrFound = false;
-void* getTTCP(uint8_t sock)
+void* getTTCP(uint8_t sock, uint8_t mode)
{
if (sock < MAX_SOCK_NUM)
- return mapSockTCP[sock];
+ return mapSockTCP[sock][mode];
return NULL;
}
@@ -194,26 +218,31 @@ int getSock(void * _ttcp)
int i = 0;
for (; i<MAX_SOCK_NUM; i++)
{
- if (_ttcp == mapSockTCP[i])
+ if (_ttcp == mapSockTCP[i][GET_TCP_MODE(_ttcp)])
return i;
}
}
return -1;
}
+void setMapSockMode(uint8_t sock, void* _ttcp, uint8_t _tcp_mode)
+{
+ if ((IS_VALID_SOCK(sock))&&(_ttcp!=NULL))
+ mapSockTCP[sock][_tcp_mode]=_ttcp;
+ INFO_TCP("Map [%d, %p, %s]\n", sock, _ttcp, Mode2Str(_tcp_mode));
+}
+
void setMapSock(uint8_t sock, void* _ttcp)
{
- if (sock < MAX_SOCK_NUM)
- mapSockTCP[sock]=_ttcp;
- INFO_TCP("Map [%d, %p]\n", sock, _ttcp);
+ setMapSockMode(sock, _ttcp, GET_TCP_MODE(_ttcp));
}
-void clearMapSockTcp(uint8_t sock)
+void clearMapSockTcp(uint8_t sock, uint8_t mode)
{
if (sock < MAX_SOCK_NUM)
{
//printk("UnMap [%d, %p]\n", sock, mapSockTCP[sock]);
- mapSockTCP[sock] = NULL;
+ mapSockTCP[sock][mode] = NULL;
}
}
@@ -223,6 +252,7 @@ void initMapSockTcp()
memset(mapSockTCP, 0, sizeof(mapSockTCP));
}
+#if 0
/**
* Calculate bitrate based on number of bytes transmitted and elapsed time
*/
@@ -235,10 +265,10 @@ static void ard_tcp_print_stats(struct ttcp *ttcp) {
printk("\n");
printk("TTCP [%p]: %d bytes processed, %d.%d KB/s (%s/%s)\n", ttcp, bytes,
- bytes / ms, bytes % ms, ttcp->udp ? "udp" : "tcp", ttcp->mode
- == TTCP_MODE_TRANSMIT ? "tx" : "rx");
+ bytes / ms, bytes % ms, ProtMode2Str(ttcp->udp),
+ Mode2Str(ttcp->mode));
}
-
+#endif
void showTTCPstatus()
{
@@ -248,19 +278,45 @@ void showTTCPstatus()
int i = 0;
for (; i<MAX_SOCK_NUM; i++)
{
- void* p = getTTCP(i);
- if (p)
+ int ii=0;
+ for (; ii<MAX_MODE_NUM; ii++)
{
- ttcp_t* _ttcp = (ttcp_t* )p;
- printk("Socket n.:%d addr:0x%x port:%d\n", i, _ttcp->addr, _ttcp->port);
- if (_ttcp->tpcb){
- printk("[tpcp-%p]-Status:%d\n", _ttcp->tpcb, _ttcp->tpcb->state);
- }
- if (_ttcp->lpcb){
- printk("[tlcp-%p]-Status:%d\n", _ttcp->lpcb, _ttcp->lpcb->state);
+ void* p = getTTCP(i, ii);
+ if (p)
+ {
+ ttcp_t* _ttcp = (ttcp_t* )p;
+ printk("Socket n.:%d(%d) [0x%x] %s %s addr:%s port:%d\n", i, ii, _ttcp,
+ ProtMode2Str(_ttcp->udp), Mode2Str(_ttcp->mode), ip2str(_ttcp->addr), _ttcp->port);
+ if (_ttcp->udp == TCP_MODE)
+ {
+ int j = 0;
+ for (; j<MAX_CLIENT_ACCEPTED; ++j)
+ {
+ if (_ttcp->tpcb[j]){
+ printk("[%d tpcp-%p]-Status:%d\n", j, _ttcp->tpcb[j], _ttcp->tpcb[j]->state);
+ }
+ }
+
+ if (_ttcp->lpcb){
+ printk("[tlcp-%p]-Status:%d\n", _ttcp->lpcb, _ttcp->lpcb->state);
+ }
+ }else{
+ if (_ttcp->upcb){
+ struct ip_addr loc = _ttcp->upcb->local_ip;
+ printk("[upcp-%p] flags:0x%x local:%s[0x%x]-%d\n",
+ _ttcp->upcb, _ttcp->upcb->flags,
+ ip2str(loc), loc, _ttcp->upcb->local_port);
+ tRemoteClient remote = {0,0};;
+ getRemoteData(i, ii, &remote);
+ struct ip_addr ipaddr = { remote.ipaddr };
+ printk("remote:%s(0x%x)-%d\n", ip2str(ipaddr), remote.ipaddr, remote.port);
+ }
+ }
+ //ard_tcp_print_stats(_ttcp);
+ printk("Data avail:%s\n", isAvailTcpDataByte(i)?"YES":"NO");
+ printk("------------------------------\n");
}
- ard_tcp_print_stats(_ttcp);
- }
+ }
}
tcp_debug_print_pcbs();
@@ -323,8 +379,7 @@ void sendError()
volatile avr32_spi_t *spi = ARD_SPI; \
Bool global_interrupt_enabled = Is_global_interrupt_enabled(); \
if (global_interrupt_enabled) Disable_global_interrupt(); \
- spi->IER.rdrf = 1; \
- spi->IER.rxbuff = 1; spi->IER.endrx = 1; \
+ spi->IER.rdrf = 1; spi->IER.rxbuff = 1; spi->IER.endrx = 1; \
if (global_interrupt_enabled) Enable_global_interrupt(); \
}while(0);
@@ -340,54 +395,6 @@ void sendError()
eic_clear_interrupt_line(&AVR32_EIC, AVR32_SPI0_IRQ); \
}while(0);
-void dump(char* _buf, uint16_t _count) {
-
- int i;
- for (i = 0; i < _count; ++i)
- printk("0x%x ", _buf[i]);
- printk("\n");
-}
-#ifdef _APP_DEBUG_
-#define DUMP dump
-#endif
-
-#ifdef _APP_DEBUG_
-#define DUMP_SPI_DATA(BUF, COUNT) do { \
- if (verboseDebug & INFO_SPI_FLAG) { \
- int i = 0; \
- for (; i < COUNT; ++i) \
- { \
- printk("0x%x ", BUF[i]); \
- if (i % 20 == 0) \
- printk("\n"); \
- } \
- printk("\n"); \
- } \
-}while(0);
-#else
-#define DUMP_SPI_DATA(BUF, COUNT) do {}while(0);
-#endif
-
-
-#ifdef _APP_DEBUG_
-#define DUMP_SPI_CMD(BUF) do { \
- if (verboseDebug & INFO_SPI_FLAG) { \
- int i = 0; \
- for (; i < CMD_MAX_LEN; ++i) \
- { \
- printk("0x%x ", BUF[i]); \
- if (BUF[i] == END_CMD) \
- break; \
- } \
- printk("\n"); \
- } \
-}while(0);
-#else
-#define DUMP_SPI_CMD(BUF) do {}while(0);
-#endif
-
-
-
int spi_add_cmd(char _cmd_id, cmd_spi_cb_t cb, cmd_spi_rcb_t rcb, void* ctx,
char flag) {
U32 i;
@@ -578,7 +585,7 @@ extern int ttcp_start(struct ip_addr addr, uint16_t port, void *opaque,
void *done_cb, int mode, uint16_t nbuf, uint16_t buflen, int udp, int verbose);
-int start_server_tcp(uint16_t port, uint8_t sock)
+int start_server_tcp(uint16_t port, uint8_t sock, uint8_t protMode)
{
struct ip_addr addr = { 0 };
uint16_t buflen = 1024;
@@ -590,7 +597,7 @@ int start_server_tcp(uint16_t port, uint8_t sock)
#else
int verbose = 0;
#endif
- int udp = 0;
+ int udp = protMode;
int mode = 1; //RECEIVE
void* _ttcp = NULL;
@@ -605,20 +612,20 @@ int start_server_tcp(uint16_t port, uint8_t sock)
if (!ifStatus)
{
- WARN("IF down...wait\n");
+ WARN_VER("IF down...wait\n");
return WIFI_SPI_ERR;
}
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 %s [%d, %d] OK!\n", ProtMode2Str(protMode), port, sock);
setMapSock(sock, _ttcp);
err = WL_SUCCESS;
}else{
- WARN("Start Server [%d, %d] FAILED!\n", port, sock);
- clearMapSockTcp(sock);
+ WARN("Start Server %s [%d, %d] FAILED!\n", ProtMode2Str(protMode), port, sock);
+ clearMapSockTcp(sock, TTCP_MODE_RECEIVE);
}
return err;
}
@@ -627,59 +634,72 @@ int start_server_tcp(uint16_t port, uint8_t sock)
int start_server_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
wl_err_t err = WL_FAILURE;
tParam* params = (tParam*) buf;
- if (numParam == 2)
+ if (numParam == 3)
{
GET_PARAM_NEXT(INT, params, port);
GET_PARAM_NEXT(BYTE, params, sock);
- err = start_server_tcp(port, sock);
+ GET_PARAM_NEXT(BYTE, params, protMode);
+ err = start_server_tcp(port, sock, protMode);
}
return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}
-int start_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
- wl_err_t err = WL_FAILURE;
- tParam* params = (tParam*) buf;
- if (numParam == 3)
- {
- GET_PARAM_NEXT(LONG, params, _addr);
- GET_PARAM_NEXT(INT, params, port);
- GET_PARAM_NEXT(BYTE, params, sock);
+int start_client_tcp(uint32_t _addr, uint16_t port, uint8_t sock, uint8_t protMode)
+{
+ uint16_t buflen = 1024;
+ uint16_t nbuf = 1024;
+ wl_err_t err = WL_FAILURE;
+ struct ip_addr addr = { .addr = _addr};
- INFO_SPI("Addr:0x%x, port:%d, sock:%d\n", _addr, port, sock);
+ INFO_SPI("Addr:0x%x, port:%d, sock:%d, prot:%s\n", _addr, port, sock, ProtMode2Str(protMode));
- uint16_t buflen = 1024;
- uint16_t nbuf = 1024;
- struct ip_addr addr = { .addr = _addr};
-#ifdef _APP_DEBUG_
- int verbose = 1;
-#else
- int verbose = 0;
-#endif
- int udp = 0;
- int mode = 0; //TRANSMIT
- void* _ttcp = NULL;
+ #ifdef _APP_DEBUG_
+ int verbose = 1;
+ #else
+ int verbose = 0;
+ #endif
+
+ int udp = protMode;
+ int mode = 0; //TRANSMIT
+ void* _ttcp = NULL;
if (sock >= MAX_SOCK_NUM)
return WIFI_SPI_ERR;
- // Check previous connection
- _ttcp = getTTCP(sock);
- if (_ttcp != NULL)
- {
- WARN("Previous client %p not stopped !\n", _ttcp);
- ard_tcp_stop(_ttcp);
- clearMapSockTcp(sock);
- }
+ // Check previous connection
+ _ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
+ if (_ttcp != NULL)
+ {
+ WARN("Previous client %p not stopped !\n", _ttcp);
+ ard_tcp_stop(_ttcp);
+ clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
+ }
- if (ard_tcp_start((struct ip_addr)addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
- {
- INFO_SPI("Start Client [0x%x, %d, %d] OK!\n", addr, port, sock);
- setMapSock(sock, _ttcp);
- err = WL_SUCCESS;
- }else{
- INFO_SPI("Start Client [0x%x, %d, %d] FAILED!\n", addr, port, sock);
- clearMapSockTcp(sock);
- }
+ if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
+ {
+ INFO_SPI("Start Client %s %p [0x%x, %d, %d] OK!\n", ProtMode2Str(protMode),
+ _ttcp, addr, port, sock);
+ setMapSock(sock, _ttcp);
+ err = WL_SUCCESS;
+ }else{
+ INFO_SPI("Start Client %s %p [0x%x, %d, %d] FAILED!\n", ProtMode2Str(protMode),
+ _ttcp, addr, port, sock);
+ clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
+ }
+ return err;
+}
+
+
+int start_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
+ wl_err_t err = WL_FAILURE;
+ tParam* params = (tParam*) buf;
+ if (numParam == 4)
+ {
+ GET_PARAM_NEXT(LONG, params, _addr);
+ GET_PARAM_NEXT(INT, params, port);
+ GET_PARAM_NEXT(BYTE, params, sock);
+ GET_PARAM_NEXT(BYTE, params, protMode);
+ err = start_client_tcp(_addr, port, sock, protMode);
}
return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}
@@ -697,7 +717,7 @@ int stop_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
if (sock < MAX_SOCK_NUM)
{
- _ttcp = getTTCP(sock);
+ _ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
ard_tcp_stop(_ttcp);
err = WL_SUCCESS;
}
@@ -705,6 +725,37 @@ int stop_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}
+int insert_data_cmd_cb(int numParam, char* buf, void* ctx) {
+
+ tDataParam* msg = (tDataParam*) buf;
+ if ((numParam == 2)&&(msg->dataLen == 1))
+ {
+ GET_DATA_BYTE(sock, buf+2);
+ GET_DATA_INT(len, buf+3);
+ //printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
+ insertBuf(sock, (uint8_t*)(buf+5), len);
+ }
+ return WIFI_SPI_ACK;
+}
+
+int send_data_udp_cmd_cb(int numParam, char* buf, void* ctx) {
+ wl_err_t err = WL_FAILURE;
+
+ tParam* params = (tParam*) buf;
+ if ((numParam == 1)&&(params->paramLen == 1))
+ {
+ GET_PARAM_NEXT(BYTE, params, sock);
+ uint16_t len = 0;
+ uint8_t* p = mergeBuf(sock, NULL, &len);
+ err = sendUdpData(getTTCP(sock, TTCP_MODE_TRANSMIT), p, len);
+ clearBuf(sock);
+ free(p);
+ }
+
+ return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
+}
+
+
int send_data_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
wl_err_t err = WL_FAILURE;
DATA_LED_ON();
@@ -714,7 +765,7 @@ int send_data_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
GET_DATA_BYTE(sock, buf+2);
GET_DATA_INT(len, buf+3);
//printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
- err = sendTcpData(getTTCP(sock), (uint8_t*)(buf+5), len);
+ err = sendTcpData(getTTCP(sock, TTCP_MODE_TRANSMIT), (uint8_t*)(buf+5), len);
}
DATA_LED_OFF();
return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
@@ -726,7 +777,6 @@ int ack_cmd_cb(int numParam, char* buf, void* ctx) {
int get_result_cmd_cb(int numParam, char* buf, void* ctx) {
INFO_SPI("ifStatus:%d result:%d\n", ifStatus, result);
- *buf=(ifStatus)?WL_CONNECTED:result;
return WIFI_SPI_ACK;
}
@@ -744,7 +794,7 @@ cmd_spi_state_t get_reply_cb(char* recv, char* reply, void* ctx, uint16_t* count
if (ctx != NULL) {
reply[4] = (*(uint8_t*)ctx); //param
} else {
- reply[4] = result; //param
+ reply[4] = (ifStatus)?WL_CONNECTED:result; //param
}
END_HEADER_REPLY(reply, 5, *count);
@@ -784,6 +834,51 @@ cmd_spi_state_t get_reply_ipaddr_cb(char* recv, char* reply, void* ctx, uint16_t
return SPI_CMD_DONE;
}
+void getRemoteData(uint8_t sock, uint8_t mode, tRemoteClient* remoteData)
+{
+ if ((sock>=0) && (sock<MAX_SOCK_NUM))
+ {
+ void* p = getTTCP(sock, mode);
+ if (p)
+ {
+ ttcp_t* _ttcp = (ttcp_t* )p;
+ if ((_ttcp->udp == UDP_MODE))
+ {
+ if (_ttcp->mode == TTCP_MODE_RECEIVE)
+ {
+ remoteData->ipaddr = getRemoteClient(sock)->ipaddr;
+ remoteData->port = getRemoteClient(sock)->port;
+ }else{
+ remoteData->ipaddr = (_ttcp->upcb) ? _ttcp->upcb->remote_ip.addr : 0;
+ remoteData->port = (_ttcp->upcb) ? _ttcp->upcb->remote_port : 0;
+ }
+ }
+ }
+ }
+}
+
+
+cmd_spi_state_t get_reply_remote_data_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
+
+ CHECK_ARD_NETIF(recv, reply, count);
+ DUMP_SPI_CMD(recv);
+
+ GET_DATA_BYTE(sock, recv+4);
+
+ CREATE_HEADER_REPLY(reply, recv, 2);
+ tRemoteClient remoteData = {0,0};
+ //TODO pass the mode
+ getRemoteData(sock, TTCP_MODE_RECEIVE, &remoteData);
+
+ PUT_LONG_IN_BYTE_NO(remoteData.ipaddr, reply, 3);
+ PUT_DATA_INT(remoteData.port, reply, 8);
+
+ END_HEADER_REPLY(reply, 11, *count);
+
+ return SPI_CMD_DONE;
+}
+
+
void foundHostByName(const char *name, struct ip_addr *ipaddr, void *callback_arg)
{
_hostIpAddr.addr = (ipaddr)?ipaddr->addr:0xffffffff;
@@ -1071,11 +1166,11 @@ cmd_spi_state_t get_state_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_
uint8_t _state = CLOSED;
if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
{
- _state = getStateTcp(getTTCP((uint8_t)recv[4]), 0);
+ _state = getStateTcp(getTTCP((uint8_t)recv[4], TTCP_MODE_RECEIVE), 0);
}
PUT_DATA_BYTE(_state, reply, 3);
END_HEADER_REPLY(reply, 5, *count);
- INFO_SPI("state:%d\n", _state);
+ INFO_SPI_POLL("state:%d\n", _state);
return SPI_CMD_DONE;
}
@@ -1087,33 +1182,20 @@ cmd_spi_state_t get_client_state_tcp_cmd_cb(char* recv, char* reply, void* ctx,
CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
uint8_t _state = CLOSED;
- if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
+ uint8_t _sock = recv[4];
+ if ((recv[3]==1)&&(_sock>=0)&&(_sock<MAX_SOCK_NUM))
{
- void * p= getTTCP((uint8_t)recv[4]);
+ void * p= getTTCP(_sock, TTCP_MODE_TRANSMIT);
if (p!=NULL)
{
- // get if we are in server or Transmit mode (0)
- if (getModeTcp(p) == TTCP_MODE_TRANSMIT)
- {
-
- _state = getStateTcp(p, 1);
- INFO_SPI_VER("CLI> p=%p _ttcp=%p state(tpcb):%d state:%d\n",
- p, ((struct ttcp*) p)->tpcb,
- ((struct ttcp*) p)->tpcb->state,
- _state);
- }else {
- _state = getStateTcp(p, 1);
- INFO_SPI_VER("SER> p=%p _ttcp=%p state(tpcb):%d state(lpcb):%d state:%d\n",
- p, ((struct ttcp*) p)->tpcb,
- ((struct ttcp*) p)->tpcb->state,
- ((struct ttcp*) p)->lpcb->state,
- _state);
- }
+ _state = getStateTcp(p, 1);
+ }else{
+ WARN_VER("TTCP not found for sock:%d\n", _sock);
}
}
PUT_DATA_BYTE(_state, reply, 3);
END_HEADER_REPLY(reply, 5, *count);
- INFO_SPI("state:%d\n", _state);
+ INFO_SPI_POLL("sock:%d state:%d\n", _sock, _state);
return SPI_CMD_DONE;
}
@@ -1123,15 +1205,15 @@ cmd_spi_state_t avail_data_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16
CHECK_ARD_NETIF(recv, reply, count);
CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
- uint8_t dataAvail = 0;
+ uint16_t dataAvail = 0;
if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
{
- dataAvail = isAvailTcpDataByte((uint8_t)recv[4]) ? 1 : 0;
+ dataAvail = getAvailTcpDataByte((uint8_t)recv[4]);
}
- PUT_DATA_BYTE(dataAvail, reply, 3);
- END_HEADER_REPLY(reply, 5, *count);
+ PUT_DATA_INT_NO(dataAvail, reply, 3);
+ END_HEADER_REPLY(reply, 6, *count);
- INFO_SPI("dataAvail:%d\n", dataAvail);
+ INFO_SPI_POLL("dataAvail:%d\n", dataAvail);
return SPI_CMD_DONE;
}
@@ -1155,7 +1237,7 @@ cmd_spi_state_t data_sent_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_
uint8_t dataSent = 0;
if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
{
- dataSent = isDataSent(getTTCP((uint8_t)recv[4]));
+ dataSent = isDataSent(getTTCP((uint8_t)recv[4], TTCP_MODE_TRANSMIT));
}
PUT_DATA_BYTE(dataSent, reply, 3);
END_HEADER_REPLY(reply, 5, *count);
@@ -1199,14 +1281,15 @@ cmd_spi_state_t get_databuf_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint1
CHECK_ARD_NETIF(recv, reply, count);
- if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
+ GET_DATA_BYTE(sock, buf+5);
+ if ((sock>=0)&&(sock<MAX_SOCK_NUM))
{
- if (getTcpData((uint8_t)recv[4], (void**)&data, &len))
+ if (getTcpData((uint8_t)sock, (void**)&data, &len))
{
CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
PUT_BUFDATA_INT(data, len, reply, 3);
- END_HEADER_REPLY(reply, 3+len+2+1, *count);
- freeTcpData((uint8_t)recv[4]);
+ END_HEADER_REPLY(reply, 3+len+2, *count);
+ freeTcpData((uint8_t)sock);
}else{
CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_0);
END_HEADER_REPLY(reply, 3, *count);
@@ -1264,16 +1347,17 @@ int sendReply(int cmdIdx, char* recv, char* reply, void* resultCmd)
AVAIL_FOR_SPI();
_result = write_stream(ARD_SPI, &reply[0], _count);
#ifdef _SPI_STATS_
- if ( result != SPI_OK)
+ if ( _result != SPI_OK)
{
statSpi.lastCmd = cmd_spi_list[cmdIdx].cmd_id;
}
#endif
BUSY_FOR_SPI();
- //unsigned char status = spi_getStatus(ARD_SPI);
- //INFO_SPI("Status after write: 0x%x\n",status);
-
+ IF_SPI_DUMP(printk("==>"));
+ DUMP_SPI(recv, count);
+ IF_SPI_DUMP(printk("<=="));
+ DUMP_SPI(reply, _count);
replyCount = _count;
return _result;
}
@@ -1282,21 +1366,22 @@ unsigned char* getStartCmdSeq(unsigned char* _recv, int len, int *offset)
{
int i = 0;
*offset = 0;
- DEB_PIN_UP();
+ //DEB_PIN_UP();
for (; i<len; ++i)
{
if (_recv[i]==START_CMD)
{
if (i!=0)
{
- DEB_PIN_DN();
+ DEB_PIN_TRIGGER();
+ IF_WARN_VER(dump((char*)_recv, (uint16_t)len));
WARN("%d] Disall. %d/%d cmd:%d\n", cmdCorr, i, len,_recv[i+1]);
}
*offset = i;
return &_recv[i];
}
}
- DEB_PIN_DN();
+ //DEB_PIN_DN();
WARN("%d] Disall. %d\n", cmdCorr, i);
return NULL;
@@ -1411,6 +1496,9 @@ void init_spi_cmds() {
spi_add_cmd(GET_CLIENT_STATE_TCP_CMD, ack_cmd_cb, get_client_state_tcp_cmd_cb, NULL, CMD_GET_FLAG);
spi_add_cmd(GET_FW_VERSION_CMD, ack_cmd_cb, get_firmware_version_cmd_cb, NULL, CMD_GET_FLAG);
spi_add_cmd(GET_TEST_CMD, ack_cmd_cb, get_test_cmd_cb, NULL, CMD_GET_FLAG);
+ spi_add_cmd(INSERT_DATABUF_CMD, insert_data_cmd_cb, ack_reply_cb, NULL, CMD_IMM_SET_FLAG);
+ spi_add_cmd(SEND_DATA_UDP_CMD, send_data_udp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
+ spi_add_cmd(GET_REMOTE_DATA_CMD, ack_cmd_cb, get_reply_remote_data_cb, NULL, CMD_GET_FLAG);
}
@@ -1452,8 +1540,9 @@ bool checkMsgFormat(uint8_t* _recv, int len, int* offset)
unsigned char* recv = getStartCmdSeq(_recv, len, offset);
if ((recv == NULL)||(recv!=_recv))
{
- if ((enableDebug & INFO_WARN_FLAG)&&(len < 20)) //TODO stamp only short messages wrong
- dump((char*)_recv, len);
+ DEB_PIN_TRIGGER();
+
+ IF_WARN_VER(DUMP((char*)_recv, len));
STATSPI_DISALIGN_ERROR();
@@ -1468,7 +1557,7 @@ bool checkMsgFormat(uint8_t* _recv, int len, int* offset)
paramLenTot = checkMsgParam8(recv);
else
{
- DUMP_SPI_DATA(_recv, len);
+ DUMP_SPI(_recv, len);
paramLenTot = checkMsgParam16(recv);
}
@@ -1512,8 +1601,8 @@ void spi_poll(struct netif* netif) {
int err = call_reply_cb(buf, &reply[0]);
if (err != REPLY_NO_ERR)
{
- DUMP_SPI_DATA(buf, count);
- DUMP_SPI_DATA(reply, replyCount);
+ DUMP_SPI(buf, count);
+ DUMP_SPI(reply, replyCount);
}
receivedChars = 0;
count = 0;
@@ -1523,8 +1612,7 @@ void spi_poll(struct netif* netif) {
{
sendError();
WARN("%d] Check format msg failed!\n", cmdCorr);
- if (enableDebug & INFO_WARN_FLAG)
- dump((char*)_receiveBuffer, receivedChars);
+ IF_WARN_VER(dump((char*)_receiveBuffer, receivedChars));
state = SPI_CMD_IDLE;
count=0;
//mark as buffer used
@@ -1553,11 +1641,6 @@ inline int spi_slaveReceiveInt(volatile avr32_spi_t *spi)
int err = SPI_OK;
state = SPI_CMD_INPUT;
- if (_receiveBuffer[0] != 0)
- {
- STATSPI_OVERRIDE_ERROR();
- }
-
do {
unsigned int timeout = SPI_TIMEOUT;
err = SPI_OK;
@@ -1569,8 +1652,20 @@ inline int spi_slaveReceiveInt(volatile avr32_spi_t *spi)
break;
}
}
- _receiveBuffer[index] = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;
+ //DEB_PIN_TG();
+ #if 0
+#ifdef _SPI_STATS_
+ if (spi->sr & AVR32_SPI_SR_OVRES_MASK)
+ {
+ STATSPI_OVERRIDE_ERROR();
+ }
+#endif
+#endif
if (err == SPI_OK) {
+ _receiveBuffer[index] = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;
+ DEB_PIN_UP(2);
+ if ((index==0) && (_receiveBuffer[index] != START_CMD))
+ DEB_PIN_TRIGGER();
++index;
++receivedChars;
}else{
@@ -1597,8 +1692,7 @@ __interrupt
static void spi_int_handler(void)
{
volatile avr32_spi_t *spi = ARD_SPI;
- //DEB_PIN_DN();
- //AVAIL_FOR_SPI();
+ DEB_PIN_DN(2);
DISABLE_SPI_INT();
if ((spi->sr & AVR32_SPI_SR_RDRF_MASK) != 0)
@@ -1610,12 +1704,10 @@ static void spi_int_handler(void)
startReply=true;
++cmdCorr;
//maintain disable interrupt to send the reply command
- //DEB_PIN_UP();
return;
}
}
ENABLE_SPI_INT();
- //DEB_PIN_UP();
}
inline spi_status_t spi_read8(volatile avr32_spi_t *spi, unsigned char *data)
diff --git a/firmwares/wifishield/wifiHD/src/ard_spi.h b/firmwares/wifishield/wifiHD/src/ard_spi.h
index 628f115..6607961 100644
--- a/firmwares/wifishield/wifiHD/src/ard_spi.h
+++ b/firmwares/wifishield/wifiHD/src/ard_spi.h
@@ -37,14 +37,22 @@ typedef enum {
CMD_IMM_SET_FLAG = 0x04,
}cmd_flags;
+typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode;
+
#define TIMEOUT_SPI 200
#define SPI_ALIGN_ERROR 0xF0
#define SPI_OVERRIDE_ERROR 0xF1
+#define SPI_TIMEOUT_ERROR 0xF2
#define DUMMY_DATA 0xFF
typedef int (*cmd_spi_cb_t)(int numParam, char* buf, void* ctx);
typedef cmd_spi_state_t (*cmd_spi_rcb_t)(char* recv, char* reply, void* ctx, uint16_t* _count);
+typedef struct eRemoteClient{
+ uint32_t ipaddr;
+ uint16_t port;
+}tRemoteClient;
+
void set_result_cmd(int err) ;
void set_result(wl_status_t _status);
@@ -61,10 +69,20 @@ void showTTCPstatus();
int getSock(void * _ttcp);
-void* getTTCP(uint8_t sock);
+void* getTTCP(uint8_t sock, uint8_t mode);
+
+void setMapSockMode(uint8_t sock, void* _ttcp, uint8_t _tcp_mode);
+
+void clearMapSockTcp(uint8_t sock, uint8_t mode);
+
+int start_server_tcp(uint16_t port, uint8_t sock, uint8_t protMode);
+
+int start_client_tcp(uint32_t _addr, uint16_t port, uint8_t sock, uint8_t protMode);
+
+void setRemoteClient(uint16_t sock, uint32_t _ipaddr, uint16_t _port);
-void clearMapSockTcp(uint8_t sock);
+tRemoteClient* getRemoteClient(uint16_t sock);
-int start_server_tcp(uint16_t port, uint8_t sock);
+void getRemoteData(uint8_t sock, uint8_t mode, tRemoteClient* remoteData);
#endif /* ARD_SPI_H_ */
diff --git a/firmwares/wifishield/wifiHD/src/ard_tcp.c b/firmwares/wifishield/wifiHD/src/ard_tcp.c
index b70549a..0c1f72a 100644
--- a/firmwares/wifishield/wifiHD/src/ard_tcp.c
+++ b/firmwares/wifishield/wifiHD/src/ard_tcp.c
@@ -30,17 +30,20 @@
unsigned int startTime = 0;
extern bool ifStatus;
-static uint8_t tcp_poll_retries = 0;
+
static int isDataSentCount = 0;
-bool pending_close = false;
bool pending_accept = false;
static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len);
-static void atcp_init_pend_flags()
+static void atcp_init_pend_flags(struct ttcp* _ttcp)
{
- pending_close = false;
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ {
+ if (_ttcp) _ttcp->pending_close[i] = false;
+ }
pending_accept = false;
}
@@ -51,17 +54,23 @@ static void ard_tcp_destroy(struct ttcp* ttcp) {
err_t err = ERR_OK;
DUMP_TCP_STATE(ttcp);
- if (getSock(ttcp)==-1)
+ uint8_t sock = getSock(ttcp);
+ if (sock == -1)
WARN("ttcp already deallocated!\n");
- if (ttcp->tpcb) {
- tcp_arg(ttcp->tpcb, NULL);
- tcp_sent(ttcp->tpcb, NULL);
- tcp_recv(ttcp->tpcb, NULL);
- tcp_err(ttcp->tpcb, NULL);
- //TEMPORAQARY
- //err = tcp_close(ttcp->tpcb);
- INFO_TCP("Closing tpcb: state:0x%x err:%d\n", ttcp->tpcb->state, err);
+ freeAllTcpData(sock);
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ {
+ if (ttcp->tpcb[i]) {
+ tcp_arg(ttcp->tpcb[i], NULL);
+ tcp_sent(ttcp->tpcb[i], NULL);
+ tcp_recv(ttcp->tpcb[i], NULL);
+ tcp_err(ttcp->tpcb[i], NULL);
+ //TEMPORAQARY
+ //err = tcp_close(ttcp->tpcb);
+ INFO_TCP("Closing tpcb: state:0x%x err:%d\n", ttcp->tpcb[i]->state, err);
+ }
}
if (ttcp->lpcb) {
@@ -83,38 +92,6 @@ static void ard_tcp_destroy(struct ttcp* ttcp) {
}
/**
- * Clean up and free the ttcp structure
- */
-static void ard_tcp_abort(struct ttcp* ttcp) {
-
- INFO_TCP("Abort ttcb:%p tpcb:%p lpcb:%p\n", ttcp, ttcp->tpcb, ttcp->lpcb);
- if (ttcp->tpcb) {
- tcp_arg(ttcp->tpcb, NULL);
- tcp_sent(ttcp->tpcb, NULL);
- tcp_recv(ttcp->tpcb, NULL);
- tcp_err(ttcp->tpcb, NULL);
- tcp_abort(ttcp->tpcb);
- }
-
- if (ttcp->lpcb) {
- tcp_arg(ttcp->lpcb, NULL);
- tcp_accept(ttcp->lpcb, NULL);
- tcp_abort(ttcp->lpcb);
- }
-
- if (ttcp->upcb) {
- udp_disconnect(ttcp->upcb);
- udp_remove(ttcp->upcb);
- }
-
- if (ttcp->payload)
- free(ttcp->payload);
-
- free(ttcp);
-}
-
-
-/**
* Invoked when transfer is done or aborted (non-zero result).
*/
static void ard_tcp_done(struct ttcp* ttcp, int result) {
@@ -125,26 +102,25 @@ static void ard_tcp_done(struct ttcp* ttcp, int result) {
ttcp->done_cb(ttcp->opaque, result);
ard_tcp_destroy(ttcp);
- clearMapSockTcp(getSock(ttcp));
+ clearMapSockTcp(getSock(ttcp), GET_TCP_MODE(ttcp));
}
-static void
-tcp_timeout_cb(void *ctx);
-
/**
* Only used in TCP mode.
* Will transmit a maximum of pbuf->tot_len bytes.
* Called upon connect and when there's space available in the TCP send window
*
*/
-static void tcp_send_data(struct ttcp *ttcp) {
+static err_t tcp_send_data(struct ttcp *ttcp) {
err_t err = ERR_OK;
uint32_t len, orig_len;
len = ttcp->left;
ttcp->buff_sent = 0;
- INFO_TCP_VER("left=%d len:%d tcp_sndbuf:%d\n", ttcp->left, len, tcp_sndbuf(ttcp->tpcb));
+ if (len == 0) return;
+
+ INFO_TCP_VER("left=%d len:%d\n", ttcp->left, len);
/* don't send more than we have in the payload */
if (len > ttcp->buflen)
@@ -152,91 +128,92 @@ static void tcp_send_data(struct ttcp *ttcp) {
/* We cannot send more data than space available in the send
buffer. */
- if (len > tcp_sndbuf(ttcp->tpcb))
- len = tcp_sndbuf(ttcp->tpcb);
+ if (len > tcp_sndbuf(GET_FIRST_CLIENT_TCP(ttcp)))
+ len = tcp_sndbuf(GET_FIRST_CLIENT_TCP(ttcp));
orig_len = len;
- uint8_t count = 0;
- do {
- startTime = timer_get_ms();
- err = tcp_write(ttcp->tpcb, ttcp->payload, len, TCP_WRITE_FLAG_COPY);
- INFO_TCP_VER("%d) tcp_write %p state:%d len:%d err:%d\n", count++, ttcp->tpcb, ttcp->tpcb->state, len, err);
- if (err == ERR_MEM)
- {
- len /= 2;
- ttcp->buff_sent = 0;
- }else if (err == ERR_OK){
- ttcp->buff_sent = 1;
- isDataSentCount = 0;
- }
- } while (err == ERR_MEM && len > 1);
- if (err == ERR_OK){
- //tcp_output(ttcp->tpcb);
- INFO_TCP_VER("tcp_output: left=%d new left:%d\n",
- ttcp->left, ttcp->left-len);
+ IF_TCP(startTime = timer_get_ms());
+ err = tcp_write(GET_FIRST_CLIENT_TCP(ttcp), ttcp->payload, len, TCP_WRITE_FLAG_COPY);
+ if (err != ERR_OK)
+ {
+ INFO_TCP("tcp_write failed %p state:%d len:%d err:%d\n",
+ GET_FIRST_CLIENT_TCP(ttcp), GET_FIRST_CLIENT_TCP(ttcp)->state, len, err);
+ ttcp->buff_sent = 0;
+ }else{
+ ttcp->buff_sent = 1;
+ isDataSentCount = 0;
ttcp->left -= len;
}
- else
- WARN("TTCP [%p-%p]: tcp_write failed err:%d origLen:%d len:%d\n",
- ttcp, ttcp->tpcb, err, orig_len, len);
- //
- // ttcp->tid = timer_sched_timeout_cb(0, TIMEOUT_ONESHOT,
- // tcp_timeout_cb, ttcp);
+
+ return err;
}
/**
* Only used in TCP mode.
*/
static err_t tcp_connect_cb(void *arg, struct tcp_pcb *tpcb, err_t err) {
- struct ttcp* ttcp = arg;
+ struct ttcp* _ttcp = arg;
- INFO_TCP("TTCP [%p-%p]: connect %d %d\n", ttcp, tpcb, err, ttcp->tpcb->state);
+ if (_ttcp == NULL) return ERR_ARG;
- _connected = ( ttcp->tpcb->state == ESTABLISHED) ? 1 : 0;
- tcp_poll_retries = 0;
+ INFO_TCP("TTCP [%p-%p]: connect %d %d\n", _ttcp, tpcb, err, tpcb->state);
+
+ _connected = ( tpcb->state == ESTABLISHED) ? 1 : 0;
+ _ttcp->tcp_poll_retries = 0;
- ttcp->start_time = timer_get_ms();
+ _ttcp->start_time = timer_get_ms();
return ERR_OK;
}
static void cleanSockState_cb(void *ctx) {
- struct ttcp* ttcp = ctx;
+ struct ttcp* _ttcp = ctx;
+
+ if (_ttcp == NULL) return;
- int sock = getSock(ttcp);
+ int sock = getSock(_ttcp);
if (sock != -1)
- clearMapSockTcp(sock);
- INFO_TCP("TTCP [%p]: cleanSockState_cb %d\n", ttcp, sock);
+ clearMapSockTcp(sock, GET_TCP_MODE(_ttcp));
+ INFO_TCP("TTCP [%p]: cleanSockState_cb %d\n", _ttcp, sock);
_connected = false;
}
-static void cleanSockStateDelayed(void * arg)
-{
- INFO_TCP("arg %p\n", arg);
- timer_sched_timeout_cb(1000, TIMEOUT_ONESHOT,
- cleanSockState_cb, arg);
-}
-
/**
* Only used in TCP mode.
*/
+
+static err_t close_conn_pcb(struct tcp_pcb* tpcb) {
+
+ err_t err = tcp_close(tpcb);
+ if (err== ERR_OK)
+ {
+ tcp_arg(tpcb, NULL);
+ tcp_sent(tpcb, NULL);
+ tcp_recv(tpcb, NULL);
+ }
+
+ INFO_TCP("Closing tpcb[%p]: state:0x%x err:%d\n", tpcb, tpcb->state, err);
+ return err;
+}
+
static void atcp_conn_err_cb(void *arg, err_t err) {
struct ttcp* _ttcp = arg;
- WARN("TTCP [%p]: connection error: %d arg:%p\n",
- _ttcp, err, arg);
+ WARN("TTCP [%p]: connection error: %d\n",
+ _ttcp, err);
if (ifStatus == false)
printk("Abort connection\n");
- cleanSockState_cb(_ttcp);
- atcp_init_pend_flags();
+ atcp_init_pend_flags(_ttcp);
}
static void atcp_conn_cli_err_cb(void *arg, err_t err) {
struct ttcp* _ttcp = arg;
+ if (_ttcp == NULL) return;
+
WARN("TTCP [%p]: connection error: %d arg:%p\n",
_ttcp, err, arg);
@@ -246,47 +223,52 @@ static void atcp_conn_cli_err_cb(void *arg, err_t err) {
if ((_ttcp)&&(err == ERR_ABRT))
{
WARN("TTCP [%p]: free memory\n", _ttcp);
- tcp_poll_retries = 0;
+ _ttcp->tcp_poll_retries = 0;
cleanSockState_cb(_ttcp);
if (_ttcp->payload)
free(_ttcp->payload);
free(_ttcp);
}
- atcp_init_pend_flags();
+ atcp_init_pend_flags(_ttcp);
}
+static err_t close_conn(struct ttcp *_ttcp, struct tcp_pcb* tpcb) {
-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);
+ if (_ttcp == NULL) return;
+
+ int8_t id = getNewClientConnId(_ttcp, tpcb);
+ if (id == NO_VALID_ID) return;
+ err_t err = close_conn_pcb(_ttcp->tpcb[id]);
if (err == ERR_MEM)
- pending_close = true;
+ _ttcp->pending_close[id] = true;
else{
- atcp_init_pend_flags();
+ atcp_init_pend_flags(_ttcp);
+ removeNewClientConn(_ttcp, _ttcp->tpcb[id]);
WARN("----------------------\n");
}
+ return err;
}
void closeConnections()
{
- int i = 0;
- for (; i<MAX_SOCK_NUM; i++)
+ int ii=0;
+ for (; ii<MAX_MODE_NUM; ii++)
{
- void* p = getTTCP(i);
- if (p)
+ int i = 0;
+ for (; i<MAX_SOCK_NUM; i++)
{
- 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);
- clearMapSockTcp(getSock(_ttcp));
+ void* p = getTTCP(i, ii);
+ if (p)
+ {
+ ttcp_t* _ttcp = (ttcp_t* )p;
+ if (_ttcp->udp == TCP_MODE)
+ {
+ ard_tcp_destroy(_ttcp);
+ clearMapSockTcp(getSock(_ttcp), GET_TCP_MODE(_ttcp));
+ }
+ }
}
}
}
@@ -299,7 +281,6 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
struct ttcp* ttcp = arg;
if (err == ERR_OK && p != NULL) {
- INFO_TCP("pcb:%p pbuf: %p err:%d len:%d\n", pcb, p, err, p->tot_len);
DATA_LED_ON();
/* for print_stats() */
ttcp->recved += p->tot_len;
@@ -310,16 +291,17 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
ttcp->print_cnt++;
}
- insert_pBuf(p, ttcp->sock, (void*) pcb);
- tcp_recved(pcb, p->tot_len);
+ uint8_t* pBufferStore = insert_pBuf(p, ttcp->sock, (void*) pcb);
+ INFO_TCP("sock:%d pcb:%p pbuf:%p err:%d bufStore:%p len:%d\n",
+ ttcp->sock, pcb, p, err, pBufferStore, p->tot_len);
pbuf_free(p);
DATA_LED_OFF();
}
/* p will be NULL when remote end is done */
if (err == ERR_OK && p == NULL) {
- INFO_TCP("atcp_recv_cb p=NULL\n");
- close_conn(ttcp);
+ INFO_TCP("atcp_recv_cb p=NULL on sock:%d pcb:%p\n", ttcp->sock, pcb);
+ close_conn(ttcp, pcb);
}
if (err!=ERR_OK)
@@ -329,67 +311,90 @@ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
void ack_recved(void* pcb, int len) {
// Comment the call because it is activated on atcp_recv_cb
- //tcp_recved(pcb, len);
+ INFO_TCP("Received %p len:%d\n", pcb, len);
+ tcp_recved(pcb, len);
}
static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
struct ttcp* _ttcp = arg;
- if ((_ttcp) && (_ttcp->left>0))
- ++tcp_poll_retries;
- if (tcp_poll_retries > 4) {
+ if (_ttcp == NULL) return ERR_ARG;
+
+ if (_ttcp->left>0)
+ ++_ttcp->tcp_poll_retries;
+
+ if (_ttcp->tcp_poll_retries > 4) {
WARN("ARD TCP [%p] arg=%p retries=%d\n",
- pcb, arg, tcp_poll_retries);
- tcp_poll_retries = 0;
+ pcb, arg, _ttcp->tcp_poll_retries);
+ _ttcp->tcp_poll_retries = 0;
tcp_abort(pcb);
- atcp_init_pend_flags();
+ atcp_init_pend_flags(_ttcp);
return ERR_ABRT;
}
-
- WARN("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d\n", (_ttcp)?_ttcp->tpcb:0, pcb, arg,
- tcp_poll_retries, pending_close);
- if (_ttcp) tcp_send_data(_ttcp);
-
- if (pending_close)
- {
- err_t err = tcp_close(pcb);
- if (err == ERR_MEM)
- {
- pending_close = true;
- }
- else
- {
- atcp_init_pend_flags();
- }
-
- INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, (_ttcp)?_ttcp->tpcb:0, pending_close);
+
+ if (pcb)
+ INFO_TCP_POLL("keepAliveCnt:%d keep_idle:%d persist_cnt:%d\n",
+ pcb->keep_cnt_sent, pcb->keep_idle, pcb->persist_cnt);
+
+ if (_ttcp->left > 0)
+ INFO_TCP("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d len:%d\n",
+ (_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, pcb, arg,
+ _ttcp->tcp_poll_retries, _ttcp->pending_close, (_ttcp)?_ttcp->left:0);
+ tcp_send_data(_ttcp);
+
+ int8_t id = getNewClientConnId(_ttcp, pcb);
+ if ((id != NO_VALID_ID) && (_ttcp->pending_close[id]))
+ {
+ err_t err = ERR_OK;
+ if (id >=0){
+ err = tcp_close(pcb);
+ if (err == ERR_MEM)
+ {
+ _ttcp->pending_close[id] = true;
+ }
+ else
+ {
+ atcp_init_pend_flags(_ttcp);
+ }
+ }
+ INFO_TCP("ARD TCP [%p-%p] try to close pending:%d err:%d id:%d\n", pcb,
+ (_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, _ttcp->pending_close[id], err, id);
}
return ERR_OK;
}
static err_t atcp_poll_conn(void *arg, struct tcp_pcb *pcb) {
struct ttcp* _ttcp = arg;
- ++tcp_poll_retries;
- if (tcp_poll_retries > 8) {
+ if (_ttcp == NULL) return ERR_ARG;
+
+ int8_t id = getNewClientConnId(_ttcp, pcb);
+ if (id != NO_VALID_ID)
+ {
+ if (_ttcp->pending_close[id])
+ ++(_ttcp->tcp_poll_retries);
+ }
+
+ if (_ttcp->tcp_poll_retries > 8) {
WARN("ARD TCP [%p-%p] arg=%p retries=%d\n",
- pcb, _ttcp->tpcb, arg, tcp_poll_retries);
- tcp_poll_retries = 0;
+ pcb, GET_FIRST_CLIENT_TCP(_ttcp), arg, _ttcp->tcp_poll_retries);
+ _ttcp->tcp_poll_retries = 0;
tcp_abort(pcb);
return ERR_ABRT;
}
- WARN("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d conn:%d\n", (_ttcp)?_ttcp->tpcb:0, pcb, arg,
- tcp_poll_retries, pending_close, _connected);
+ INFO_TCP_POLL("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d conn:%d\n",
+ (_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, pcb, arg,
+ _ttcp->tcp_poll_retries, _ttcp->pending_close[id], _connected);
if ((_ttcp)&&(_connected)) tcp_send_data(_ttcp);
- if (pending_close)
+ if ((id != NO_VALID_ID) && (_ttcp->pending_close[id]))
{
err_t err = tcp_close(pcb);
if (err == ERR_MEM)
{
- pending_close = true;
+ _ttcp->pending_close[id] = true;
}
else
{
@@ -397,25 +402,99 @@ static err_t atcp_poll_conn(void *arg, struct tcp_pcb *pcb) {
if (_ttcp->payload)
free(_ttcp->payload);
free(_ttcp);
- pending_close = false;
+ _ttcp->pending_close[id] = false;
}
- INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, (_ttcp)?_ttcp->tpcb:0, pending_close);
+ INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, (_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, _ttcp->pending_close[id]);
}
return ERR_OK;
}
+int8_t getNewClientConnId(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
+{
+ if (_ttcp != NULL){
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ {
+ if (_ttcp->tpcb[i] == newpcb)
+ {
+ INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
+ return i;
+ }
+ }
+ }
+ WARN("No Valid Id for ttcp:%p pcb:%p\n");
+ return NO_VALID_ID;
+}
+
+int8_t setNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb, uint8_t id)
+{
+ if ((_ttcp != NULL)&&(id>=0)&&(id<MAX_CLIENT_ACCEPTED)){
+ INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, id, newpcb);
+ _ttcp->tpcb[id] = newpcb;
+ return id;
+ }
+ return NO_VALID_ID;
+}
+
+int8_t insertNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
+{
+ if (_ttcp != NULL){
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ {
+ if ((_ttcp->tpcb[i] == NULL)||(_ttcp->tpcb[i] == newpcb))
+ {
+ INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
+ _ttcp->tpcb[i] = newpcb;
+ return i;
+ }
+ }
+ }
+ return NO_VALID_ID;
+}
+
+int8_t removeNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
+{
+ if (_ttcp != NULL){
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ {
+ if (_ttcp->tpcb[i] == newpcb)
+ {
+ INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
+ _ttcp->tpcb[i] = NULL;
+ return i;
+ }
+ }
+ }
+ return NO_VALID_ID;
+}
+
+bool cleanNewClientConn(struct ttcp* _ttcp)
+{
+ if (_ttcp != NULL){
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
+ _ttcp->tpcb[i] = NULL;
+ return true;
+ }
+ return false;
+}
+
/**
* Only used in TCP mode.
*/
static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
- struct ttcp* ttcp = arg;
+ struct ttcp* _ttcp = arg;
- INFO_TCP("ARD TCP [%p]: accept new [%p]\n", ttcp, newpcb);
- INFO_TCP("local:%d remote:%d state:%d\n", newpcb->local_port, newpcb->remote_port, newpcb->state);
+ if (_ttcp == NULL) return ERR_ARG;
+ INFO_TCP("ARD TCP [%p]: accept new [%p]\n", _ttcp, newpcb);
+ INFO_TCP("local:%d remote:%d state:%d\n", newpcb->local_port, newpcb->remote_port, newpcb->state);
+ /*
if (pending_accept)
{
WARN("Accepting another connection: %p-%p\n", ttcp->tpcb, newpcb);
@@ -429,7 +508,23 @@ static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
tcp_err(ttcp->tpcb, atcp_conn_err_cb);
tcp_poll(ttcp->tpcb, atcp_poll, 4);
- ttcp->start_time = timer_get_ms();
+ _ttcp->tcp_poll_retries = 0;
+ /*
+ if (_ttcp->tpcb == NULL)
+ {
+ WARN("Replace previous tpcb=0x%x with the new one 0x%x\n", _ttcp->tpcb, newpcb);
+ }
+ */
+ int8_t id = insertNewClientConn(_ttcp, newpcb);
+ tcp_arg(_ttcp->tpcb[id], _ttcp);
+ tcp_recv(_ttcp->tpcb[id], atcp_recv_cb);
+ tcp_err(_ttcp->tpcb[id], atcp_conn_err_cb);
+ tcp_poll(_ttcp->tpcb[id], atcp_poll, 4);
+ // Copy the pointer to ttcp also to TRANSMIT mode for the clients connected to the server
+ int _sock = getSock(_ttcp);
+ if ((_sock != -1)&&(IS_VALID_SOCK(_sock)))
+ setMapSockMode(_sock, _ttcp, TTCP_MODE_TRANSMIT);
+ _ttcp->start_time = timer_get_ms();
return ERR_OK;
}
@@ -439,8 +534,9 @@ static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
static int atcp_start(struct ttcp* ttcp) {
err_t err = ERR_OK;
- ttcp->tpcb = tcp_new();
- if (ttcp->tpcb == NULL) {
+ struct tcp_pcb * p = tcp_new();
+
+ if (p == NULL) {
WARN("TTCP [%p]: could not allocate pcb\n", ttcp);
return -1;
}
@@ -451,43 +547,38 @@ static int atcp_start(struct ttcp* ttcp) {
return -1;
}
- tcp_arg(ttcp->tpcb, ttcp);
- atcp_init_pend_flags();
+ tcp_arg(p, ttcp);
+ atcp_init_pend_flags(ttcp);
if (ttcp->mode == TTCP_MODE_TRANSMIT) {
- tcp_err(ttcp->tpcb, atcp_conn_cli_err_cb);
- tcp_recv(ttcp->tpcb, atcp_recv_cb);
- tcp_sent(ttcp->tpcb, tcp_data_sent);
- tcp_poll(ttcp->tpcb, atcp_poll_conn, 4);
+ setNewClientConn(ttcp, p, 0);
+ tcp_err(GET_FIRST_CLIENT_TCP(ttcp), atcp_conn_cli_err_cb);
+ tcp_recv(GET_FIRST_CLIENT_TCP(ttcp), atcp_recv_cb);
+ tcp_sent(GET_FIRST_CLIENT_TCP(ttcp), tcp_data_sent);
+ tcp_poll(GET_FIRST_CLIENT_TCP(ttcp), atcp_poll_conn, 4);
_connected = false;
- INFO_TCP("[tpcb]-%p payload:%p\n", ttcp->tpcb, ttcp->payload);
+ INFO_TCP("[tpcb]-%p payload:%p\n", GET_FIRST_CLIENT_TCP(ttcp), ttcp->payload);
DUMP_TCP_STATE(ttcp);
- if (tcp_connect(ttcp->tpcb, &ttcp->addr, ttcp->port, tcp_connect_cb)
+ if (tcp_connect(GET_FIRST_CLIENT_TCP(ttcp), &ttcp->addr, ttcp->port, tcp_connect_cb)
!= ERR_OK) {
WARN("TTCP [%p]: tcp connect failed\n", ttcp);
return -1;
}
} else {
- INFO_TCP("BEFORE BIND ttcp:%p lpcb:%p pcb:%p\n", ttcp, ttcp->lpcb, ttcp->tpcb);
- INFO_TCP("[tpcb]-local:%d remote:%d state:%d\n", ttcp->tpcb->local_port,
- ttcp->tpcb->remote_port, ttcp->tpcb->state);
+ INFO_TCP("BEFORE BIND ttcp:%p lpcb:%p pcb:%p\n", ttcp, ttcp->lpcb, GET_FIRST_CLIENT_TCP(ttcp));
- err = tcp_bind(ttcp->tpcb, IP_ADDR_ANY, ttcp->port);
+ err = tcp_bind(p, IP_ADDR_ANY, ttcp->port);
if (err != ERR_OK){
WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
return -1;
}
- ttcp->lpcb = tcp_listen(ttcp->tpcb);
+ ttcp->lpcb = tcp_listen(p);
if (ttcp->lpcb == NULL) {
WARN("TTCP [%p]: listen failed\n", ttcp);
return -1;
}
- if (ttcp->lpcb == ttcp->tpcb ) {
- WARN("TTCP [%p]: listen failed tpcb [%p] in listen mode\n", ttcp, ttcp->tpcb);
- return -1;
- }
DUMP_TCP_STATE(ttcp);
tcp_accept(ttcp->lpcb, atcp_accept_cb);
@@ -566,60 +657,59 @@ static void udp_send_data(struct ttcp* ttcp) {
* Only used in UDP mode. Will finalize the ttcp process when an end marker
* is seen.
*/
-static void udp_recv_cb(void *arg, struct udp_pcb *upcb, struct pbuf *p,
+static void audp_recv_cb(void *arg, struct udp_pcb *upcb, struct pbuf *p,
struct ip_addr *addr, u16_t port) {
struct ttcp* ttcp = arg;
- /* got start marker? we might lose this so if we get it just reset
- * the timer
- */
- if (!ttcp->udp_started && p->tot_len <= 4) {
- ttcp->start_time = timer_get_ms();
- ttcp->udp_started = 1;
- goto out;
- }
-
- /* after receiving at least 1 byte, check end marker
- * don't check udp_started since we might have lost the start marker
- */
- if (ttcp->recved && p->tot_len <= 4) {
- ard_tcp_done(ttcp, 0);
- goto out;
- }
-
/* for print_stats() */
ttcp->recved += p->tot_len;
+ DUMP(p->payload,p->tot_len);
if (ttcp->verbose) {
printk(".");
if (ttcp->print_cnt % 80 == 0)
printk("\n");
ttcp->print_cnt++;
}
+ INFO_TCP("UDP Insert %p sock:%d addr:%s port:%d\n", p, ttcp->sock,
+ ip2str(*addr), port);
+ insert_pBuf(p, ttcp->sock, (void*) upcb);
+ setRemoteClient(ttcp->sock, addr->addr, port);
- out: pbuf_free(p);
+ pbuf_free(p);
}
/**
* Start UDP transfer.
*/
static int udp_start(struct ttcp* ttcp) {
+ err_t err = ERR_OK;
ttcp->udp_end_marker_left = 5;
ttcp->upcb = udp_new();
if (ttcp->upcb == NULL) {
- printk("TTCP [%p]: could not allocate pcb\n", ttcp);
+ WARN("TTCP [%p]: could not allocate pcb\n", ttcp);
return -1;
}
+ printk("%s, upcb:%p %s:%d\n", __FUNCTION__, ttcp->upcb, ip2str(ttcp->addr), ttcp->port);
if (ttcp->mode == TTCP_MODE_TRANSMIT) {
- if (udp_connect(ttcp->upcb, &ttcp->addr, ttcp->port) != ERR_OK) {
- printk("TTCP [%p]: udp connect failed\n", ttcp);
+ if (udp_connect(ttcp->upcb, &(ttcp->addr), ttcp->port) != ERR_OK) {
+ WARN("TTCP [%p]: udp connect failed\n", ttcp);
return -1;
}
- udp_send_data(ttcp);
} else {
- udp_recv(ttcp->upcb, udp_recv_cb, ttcp);
+ /* bind to any IP address on port specified */
+ err = udp_bind(ttcp->upcb, IP_ADDR_ANY, ttcp->port);
+ if (err!= ERR_OK) {
+ WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
+ return -1;
+ }
+ // clear remote client data
+ setRemoteClient(ttcp->sock, 0, 0);
+ udp_recv(ttcp->upcb, audp_recv_cb, ttcp);
}
-
+ printk("%s, loc:0x%x-%d rem:0x%x-%d\n", __FUNCTION__,
+ ttcp->upcb->local_ip.addr, ttcp->upcb->local_port,
+ ttcp->upcb->remote_ip.addr, ttcp->upcb->remote_port);
return 0;
}
@@ -660,12 +750,13 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
ttcp->port = port;
ttcp->nbuf = nbuf;
ttcp->mode = mode;
- ttcp->left = nbuf * buflen;
+ ttcp->left = 0;
ttcp->done_cb = done_cb;
ttcp->opaque = opaque;
ttcp->udp = udp;
ttcp->verbose = verbose;
ttcp->buflen = buflen;
+ cleanNewClientConn(ttcp);
if (ttcp->udp)
status = udp_start(ttcp);
@@ -677,8 +768,8 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
goto fail;
}
INFO_TCP("TTCP [%p-%p]: nbuf=%d, buflen=%d, port=%d (%s/%s)\n", ttcp,
- ttcp->tpcb, ttcp->nbuf, ttcp->buflen, ttcp->port, ttcp->udp ? "udp"
- : "tcp", ttcp->mode == TTCP_MODE_TRANSMIT ? "tx" : "rx");
+ ((ttcp->udp==1)?(void*)ttcp->upcb:GET_FIRST_CLIENT_TCP(ttcp)), ttcp->nbuf, ttcp->buflen,
+ ttcp->port, ProtMode2Str(ttcp->udp), Mode2Str(ttcp->mode));
*_ttcp = (void*) ttcp;
ttcp->sock = sock;
@@ -699,17 +790,25 @@ void ard_tcp_stop(void* ttcp) {
return;
}
if (_ttcp->mode == TTCP_MODE_TRANSMIT) {
- INFO_TCP("Destroy TCP connection...state:%d\n", _ttcp->tpcb->state);
ard_tcp_destroy(_ttcp);
- clearMapSockTcp(getSock(_ttcp));
- tcp_poll_retries = 0;
+ clearMapSockTcp(getSock(_ttcp), GET_TCP_MODE(_ttcp));
+ _ttcp->tcp_poll_retries = 0;
}else{
- INFO_TCP("Closing connection...state:%d\n", _ttcp->tpcb->state);
DUMP_TCP_STATE(_ttcp);
- if ((_ttcp)&&(_ttcp->tpcb)&&(_ttcp->tpcb->state!=LAST_ACK)&&(_ttcp->tpcb->state!=CLOSED))
+
+ int i = 0;
+ for (; i<MAX_CLIENT_ACCEPTED; ++i)
{
- close_conn(_ttcp);
+ if ((_ttcp)&&(_ttcp->tpcb[i])&&(_ttcp->tpcb[i]->state!=LAST_ACK)&&(_ttcp->tpcb[i]->state!=CLOSED))
+ {
+ // Flush all the data
+ err_t err=tcp_output(_ttcp->tpcb[i]);
+ INFO_TCP("flush data: tpcb:%p err:%d\n", _ttcp->tpcb[i], err);
+ // if any socket cannot be close stop the close connection
+ if (close_conn(_ttcp, _ttcp->tpcb[i]) != ERR_OK) break;
+ }
}
+
pending_accept = false;
}
}
@@ -719,15 +818,15 @@ uint8_t getStateTcp(void* p, bool client) {
if (ifStatus == false)
return CLOSED;
- if ((_ttcp != NULL) && (_ttcp->tpcb != NULL)) {
- //DUMP_TCP_STATE(_ttcp);
+ if ((_ttcp != NULL) && ((GET_FIRST_CLIENT_TCP(_ttcp) != NULL) || (client==0))) {
+ IF_SPI_POLL(DUMP_TCP_STATE(_ttcp));
if (client)
- return _ttcp->tpcb->state;
+ return GET_FIRST_CLIENT_TCP(_ttcp)->state;
else
return _ttcp->lpcb->state;
} else {
- INFO_TCP_VER("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
- _ttcp, ((_ttcp)?_ttcp->tpcb:0), ((_ttcp)?_ttcp->lpcb:0));
+ WARN_POLL("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
+ _ttcp, ((_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0), ((_ttcp)?_ttcp->lpcb:0));
}
return CLOSED;
}
@@ -759,7 +858,9 @@ static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
_ttcp = arg;
- tcp_poll_retries = 0;
+ if (_ttcp == NULL) return ERR_ARG;
+
+ _ttcp->tcp_poll_retries = 0;
if (_ttcp) _ttcp->buff_sent = 1;
@@ -775,10 +876,8 @@ static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
return ERR_OK;
}
-int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
- INFO_TCP("buf:%p len:%d\n", buf, len);
- DUMP_TCP(buf,len);
-
+int sendTcpData(void* p, uint8_t* buf, uint16_t len)
+{
struct ttcp* _ttcp = (struct ttcp*) p;
if (_ttcp==NULL)
@@ -786,24 +885,24 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
WARN("ttcp == NULL!\n");
return WL_FAILURE;
}
+
+ INFO_TCP_VER("ttcp:%p pcb:%p buf:%p len:%d\n", _ttcp,
+ GET_FIRST_CLIENT_TCP(_ttcp), buf, len);
+ DUMP_TCP(buf,len);
+ IF_TCP_VER(DUMP_TCP_STATE(_ttcp));
- INFO_TCP_VER("CLI> p=%p _ttcp=%p state(tpcb):%d state(lpcb):%d\n",
- p, ((struct ttcp*) p)->tpcb,
- ((struct ttcp*) p)->tpcb->state,
- ((struct ttcp*) p)->lpcb->state);
-
- if ((_ttcp != NULL) && (_ttcp->tpcb != NULL) &&
+ if ((_ttcp != NULL) && (GET_FIRST_CLIENT_TCP(_ttcp) != NULL) &&
(buf != NULL) && (len != 0) && (_ttcp->payload != NULL)) {
- if (_ttcp->tpcb->state == ESTABLISHED ||
- _ttcp->tpcb->state == CLOSE_WAIT ||
- _ttcp->tpcb->state == SYN_SENT ||
- _ttcp->tpcb->state == SYN_RCVD) {
+ if (GET_FIRST_CLIENT_TCP(_ttcp)->state == ESTABLISHED ||
+ GET_FIRST_CLIENT_TCP(_ttcp)->state == CLOSE_WAIT ||
+ GET_FIRST_CLIENT_TCP(_ttcp)->state == SYN_SENT ||
+ GET_FIRST_CLIENT_TCP(_ttcp)->state == SYN_RCVD) {
memcpy(_ttcp->payload, buf, len);
_ttcp->payload[len]='\0';
- INFO_TCP_VER("%s\n", _ttcp->payload);
+ INFO_TCP_VER("'%s'\n", _ttcp->payload);
_ttcp->left = len;
- tcp_sent(_ttcp->tpcb, tcp_data_sent);
+ tcp_sent(GET_FIRST_CLIENT_TCP(_ttcp), tcp_data_sent);
tcp_send_data(_ttcp);
return WL_SUCCESS;
@@ -813,6 +912,34 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len) {
return WL_FAILURE;
}
+int sendUdpData(void* ttcp, uint8_t* buf, uint16_t len) {
+ struct ttcp* _ttcp = (struct ttcp*) ttcp;
+ if ((_ttcp != NULL) && (buf != NULL) && (len != 0))
+ {
+ INFO_TCP("buf:%p len:%d\n", buf, len);
+ DUMP_TCP(buf,len);
+ }else{
+ return WL_FAILURE;
+ }
+
+ struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
+ if (p == NULL) {
+ WARN("TTCP [%p]: could not allocate pbuf\n", ttcp);
+ return WL_FAILURE;
+ }
+ memcpy(p->payload, buf, len);
+ if (udp_send(_ttcp->upcb, p) != ERR_OK) {
+ WARN("TTCP [%p]: udp_send() failed\n", _ttcp);
+ pbuf_free(p);
+ return WL_FAILURE;
+ }
+
+ pbuf_free(p);
+ return WL_SUCCESS;
+}
+
+
+
char
usage[] =
"Usage: ttcp -t/-r [-options] host\n\
diff --git a/firmwares/wifishield/wifiHD/src/ard_tcp.h b/firmwares/wifishield/wifiHD/src/ard_tcp.h
index 010b976..7d0bf7b 100644
--- a/firmwares/wifishield/wifiHD/src/ard_tcp.h
+++ b/firmwares/wifishield/wifiHD/src/ard_tcp.h
@@ -15,6 +15,18 @@ typedef void (ard_tcp_done_cb_t)(void *opaque, int result);
#define TTCP_MODE_TRANSMIT 0
#define TTCP_MODE_RECEIVE 1
+#define MAX_MODE_NUM 2
+
+#define GET_TCP_MODE(X) ((X!=NULL)?((struct ttcp*)(X))->mode:0)
+#define IS_VALID_SOCK(SOCK) ((SOCK>=0)&&(SOCK<MAX_SOCK_NUM))
+#define IS_UDP_SOCK(SOCK) ((getTTCP(SOCK, TTCP_MODE_RECEIVE)!=NULL)?((struct ttcp*)(getTTCP(SOCK, TTCP_MODE_RECEIVE)))->udp:0)
+
+// Maximum number of client connection accepted by server
+#define MAX_CLIENT_ACCEPTED 4
+#define NO_VALID_ID -1
+#define GET_FIRST_CLIENT_TCP(TTCP) ((TTCP!=NULL)?TTCP->tpcb[0] : NULL)
+#define GET_CLIENT_TCP(TTCP,ID) (((TTCP!=NULL)&&(ID>=0)&&(ID<MAX_CLIENT_ACCEPTED))?TTCP->tpcb[ID] : NULL)
+
typedef struct ttcp {
@@ -39,9 +51,11 @@ typedef struct ttcp {
uint32_t tid;
/* TCP specific */
- struct tcp_pcb* tpcb;
+ struct tcp_pcb* tpcb[MAX_CLIENT_ACCEPTED];
struct tcp_pcb* lpcb;
char* payload;
+ uint8_t tcp_poll_retries;
+ bool pending_close[MAX_CLIENT_ACCEPTED];
/* UDP specific */
int udp_started;
@@ -62,10 +76,22 @@ uint8_t getModeTcp(void* p);
int sendTcpData(void* p, uint8_t* buf, uint16_t len);
+int sendUdpData(void* p, uint8_t* buf, uint16_t len);
+
uint8_t isDataSent(void* p );
cmd_state_t cmd_ttcp(int argc, char* argv[], void* ctx);
+int8_t setNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb, uint8_t id);
+
+int8_t insertNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb);
+
+int8_t removeNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb);
+
+bool cleanNewClientConn(struct ttcp* _ttcp);
+
+int8_t getNewClientConnId(struct ttcp* _ttcp, struct tcp_pcb *newpcb);
+
void closeConnections();
#endif
diff --git a/firmwares/wifishield/wifiHD/src/ard_utils.c b/firmwares/wifishield/wifiHD/src/ard_utils.c
index 9c31f40..39c19dd 100644
--- a/firmwares/wifishield/wifiHD/src/ard_utils.c
+++ b/firmwares/wifishield/wifiHD/src/ard_utils.c
@@ -4,38 +4,142 @@
* Created on: Jul 4, 2010
* Author: mlf by Metodo2 srl
*/
-#undef _APP_DEBUG_
+//#define _APP_DEBUG_
#include "lwip/pbuf.h"
#include "wifi_spi.h"
#include "ard_utils.h"
#include "debug.h"
+#include "ard_spi.h"
+#include "ard_tcp.h"
#define MAX_PBUF_STORED 30
tData pBufStore[MAX_PBUF_STORED][MAX_SOCK_NUM];
-unsigned char headBuf = 0;
-unsigned char tailBuf = 0;
+unsigned char headBuf[MAX_SOCK_NUM] = {0};
+unsigned char tailBuf[MAX_SOCK_NUM] = {0};
-#define IS_BUF_AVAIL() (tailBuf!=headBuf)
-#define IS_BUF_EMPTY() ((tailBuf == 0) && (headBuf == 0))
+#define IS_BUF_AVAIL(x) (tailBuf[x] != headBuf[x])
+#define IS_BUF_EMPTY(x) ((tailBuf[x] == 0) && (headBuf[x] == 0))
void init_pBuf()
{
memset(pBufStore, 0, sizeof(pBufStore));
}
-void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
+uint8_t* insertBuf(uint8_t sock, uint8_t* buf, uint16_t len)
+{
+ DUMP(buf,len);
+ if (sock>= MAX_SOCK_NUM)
+ {
+ WARN("Sock out of range: sock=%d", sock);
+ return NULL;
+ }
+ if (pBufStore[headBuf[sock]][sock].data != NULL)
+ {
+ WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf[sock]][sock].data, headBuf[sock]);
+ // to avoid memory leak free the oldest buffer
+ freetDataIdx(headBuf[sock], sock);
+ }
+
+ u8_t* p = (u8_t*)calloc(len,sizeof(u8_t));
+ if(p != NULL) {
+ memcpy(p, buf, len);
+
+ pBufStore[headBuf[sock]][sock].data = p;
+ pBufStore[headBuf[sock]][sock].len = len;
+ pBufStore[headBuf[sock]][sock].idx = 0;
+ pBufStore[headBuf[sock]][sock].pcb = getTTCP(sock, TTCP_MODE_TRANSMIT);
+ headBuf[sock]++;
+
+ if (headBuf[sock] == MAX_PBUF_STORED)
+ headBuf[sock] = 0;
+ if (headBuf[sock] == tailBuf[sock])
+ {
+ WARN("Avoid to Overwrite data [%d-%d]!\n", headBuf[sock], tailBuf[sock]);
+ if (headBuf[sock] != 0)
+ --headBuf[sock];
+ else
+ headBuf[sock] = MAX_PBUF_STORED-1;
+ }
+ INFO_UTIL("Insert[%d]: %p:%d-%d [%d,%d]\n", sock, p, len, p[0], headBuf[sock], tailBuf[sock]);
+ }
+ return p;
+}
+
+
+uint16_t calcMergeLen(uint8_t sock)
+{
+ uint16_t len = 0;
+
+ unsigned char index = tailBuf[sock];
+ do {
+ if (pBufStore[index][sock].data != NULL)
+ {
+ len += pBufStore[index][sock].len;
+ }
+ ++index;
+ if (index == MAX_PBUF_STORED)
+ index = 0;
+ }while (index!=headBuf[sock]);
+ return len;
+}
+
+uint16_t clearBuf(uint8_t sock)
+{
+ uint16_t len = 0;
+
+ unsigned char index = tailBuf[sock];
+ do {
+ if (pBufStore[index][sock].data != NULL)
+ {
+ freetDataIdx(index,sock);
+ }
+ ++index;
+ if (index == MAX_PBUF_STORED)
+ index = 0;
+ }while (index!=headBuf[sock]);
+ tailBuf[sock]=index;
+ return len;
+}
+
+uint8_t* mergeBuf(uint8_t sock, uint8_t** buf, uint16_t* _len)
+{
+ uint16_t len = calcMergeLen(sock);
+ uint8_t* p = (u8_t*)calloc(len,sizeof(u8_t));
+ uint8_t* _p = p;
+ if(p != NULL) {
+ unsigned char index = tailBuf[sock];
+ do {
+ if (pBufStore[index][sock].data != NULL)
+ {
+ memcpy(p, pBufStore[index][sock].data, pBufStore[index][sock].len);
+ p += pBufStore[index][sock].len;
+ }
+ ++index;
+ if (index == MAX_PBUF_STORED)
+ index = 0;
+ }while (index!=headBuf[sock]);
+ }
+ DUMP(_p,len);
+ if (buf != NULL)
+ *buf = _p;
+ if (_len != NULL)
+ *_len = len;
+ return _p;
+}
+
+uint8_t* insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
{
if (q == NULL)
- return;
+ return NULL;
- if (pBufStore[headBuf][sock].data != NULL)
+ if (pBufStore[headBuf[sock]][sock].data != NULL)
{
- WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf][sock].data, headBuf);
+ WARN("Overwriting buffer %p idx:%d!\n", pBufStore[headBuf[sock]][sock].data, headBuf[sock]);
// to avoid memory leak free the oldest buffer
- freetDataIdx(headBuf, sock);
+ freetDataIdx(headBuf[sock], sock);
}
u8_t* p = (u8_t*)calloc(q->tot_len,sizeof(u8_t));
@@ -44,32 +148,54 @@ void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb)
WARN("pbuf_copy_partial failed: src:%p, dst:%p, len:%d\n", q, p, q->tot_len);
free(p);
p = NULL;
- return;
+ return p;
}
- pBufStore[headBuf][sock].data = p;
- pBufStore[headBuf][sock].len = q->tot_len;
- pBufStore[headBuf][sock].idx = 0;
- pBufStore[headBuf][sock].pcb = _pcb;
- headBuf++;
-
- if (headBuf == MAX_PBUF_STORED)
- headBuf = 0;
- if (headBuf == tailBuf)
- WARN("Overwriting data [%d-%d]!\n", headBuf, tailBuf);
- INFO_UTIL("Insert: %p:%d-%d [%d,%d]\n", p, q->tot_len, p[0], headBuf, tailBuf);
+ pBufStore[headBuf[sock]][sock].data = p;
+ pBufStore[headBuf[sock]][sock].len = q->tot_len;
+ pBufStore[headBuf[sock]][sock].idx = 0;
+ pBufStore[headBuf[sock]][sock].pcb = _pcb;
+ headBuf[sock]++;
+
+ if (headBuf[sock] == MAX_PBUF_STORED)
+ headBuf[sock] = 0;
+ if (headBuf[sock] == tailBuf[sock])
+ {
+ WARN("Avoid to Overwrite data [%d-%d]!\n", headBuf[sock], tailBuf[sock]);
+ if (headBuf[sock] != 0)
+ --headBuf[sock];
+ else
+ headBuf[sock] = MAX_PBUF_STORED-1;
+ }
+ INFO_UTIL("Insert[%d]: %p:%d-%d [%d,%d]\n", sock, p, q->tot_len, p[0], headBuf[sock], tailBuf[sock]);
}
+ return p;
+}
+
+void dumpPbuf(uint8_t sock)
+{
+ unsigned char index = tailBuf[sock];
+ printk("headBuf=%d tailBuf=%d\n", headBuf[sock], tailBuf[sock]);
+ do {
+ if (pBufStore[index][sock].data != NULL)
+ {
+ printk("Buf: %p Len:%d\n", pBufStore[index][sock].data, pBufStore[index][sock].len);
+ }
+ ++index;
+ if (index == MAX_PBUF_STORED)
+ index = 0;
+ }while (index!=headBuf[sock]);
}
tData* get_pBuf(uint8_t sock)
{
- if (IS_BUF_EMPTY())
+ if (IS_BUF_EMPTY(sock))
return NULL;
- if (IS_BUF_AVAIL())
+ if (IS_BUF_AVAIL(sock))
{
- tData* p = &(pBufStore[tailBuf][sock]);
- INFO_UTIL_VER("%p [%d,%d]\n", p, headBuf, tailBuf);
+ tData* p = &(pBufStore[tailBuf[sock]][sock]);
+ INFO_UTIL_VER("%p [%d,%d]\n", p, headBuf[sock], tailBuf[sock]);
return p;
}
return NULL;
@@ -83,14 +209,14 @@ void freetData(void * buf, uint8_t sock)
return;
}
- pBufStore[tailBuf][sock].data = NULL;
- pBufStore[tailBuf][sock].len = 0;
- pBufStore[tailBuf][sock].idx = 0;
- pBufStore[tailBuf][sock].pcb = 0;
+ pBufStore[tailBuf[sock]][sock].data = NULL;
+ pBufStore[tailBuf[sock]][sock].len = 0;
+ pBufStore[tailBuf[sock]][sock].idx = 0;
+ pBufStore[tailBuf[sock]][sock].pcb = 0;
- if (++tailBuf == MAX_PBUF_STORED)
- tailBuf = 0;
- INFO_UTIL("%p [%d,%d]\n", buf, headBuf, tailBuf);
+ if (++tailBuf[sock] == MAX_PBUF_STORED)
+ tailBuf[sock] = 0;
+ INFO_UTIL("%p [%d,%d]\n", buf, headBuf[sock], tailBuf[sock]);
free(buf);
}
@@ -117,6 +243,16 @@ void freetDataIdx(uint8_t idxBuf, uint8_t sock)
void ack_recved(void* pcb, int len);
+void ackAndFreeData(void* pcb, int len, uint8_t sock, uint8_t* data)
+{
+ INFO_UTIL("Ack pcb:%p len:%d sock:%d data:%p\n", pcb, len, sock, data);
+ if (!IS_UDP_SOCK(sock))
+ ack_recved(pcb, len);
+ if (data != NULL)
+ freetData(data, sock);
+}
+
+
bool isAvailTcpDataByte(uint8_t sock)
{
tData* p = get_pBuf(sock);
@@ -126,11 +262,10 @@ bool isAvailTcpDataByte(uint8_t sock)
INFO_UTIL_VER("check:%d %d %p\n",p->idx, p->len, p->data);
if (p->idx == p->len)
{
- freetData(p->data, sock);
- ack_recved(p->pcb, p->len);
INFO_UTIL("Free %p other buf %d tail:%d head:%d\n",
- p->data, IS_BUF_AVAIL(), tailBuf, headBuf);
- return (IS_BUF_AVAIL());
+ p->data, IS_BUF_AVAIL(sock), tailBuf[sock], headBuf[sock]);
+ ackAndFreeData(p->pcb, p->len, sock, p->data);
+ return (IS_BUF_AVAIL(sock));
}else{
return true;
}
@@ -138,6 +273,12 @@ bool isAvailTcpDataByte(uint8_t sock)
return false;
}
+uint16_t getAvailTcpDataByte(uint8_t sock)
+{
+ uint16_t len = calcMergeLen(sock);
+ INFO_UTIL("Availabled data: %d\n", len);
+ return len;
+}
bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
@@ -157,10 +298,7 @@ bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
INFO_UTIL_VER("get:%d %p %d\n",p->idx, p->data, *payload);
return true;
}else{
- //dealloc current buffer
- INFO_UTIL("Free %p\n", p->data);
- freetData(p->data, sock);
- ack_recved(p->pcb, p->len);
+ ackAndFreeData(p->pcb, p->len, sock, p->data);
}
}
return false;
@@ -185,12 +323,20 @@ bool freeTcpData(uint8_t sock)
p = get_pBuf(sock);
if (p != NULL)
{
- freetData(p->data, sock);
- ack_recved(p->pcb, p->len);
+ ackAndFreeData(p->pcb, p->len, sock, p->data);
return true;
}
return false;
}
+void freeAllTcpData(uint8_t sock)
+{
+ tData* p = NULL;
+ do{
+ p = get_pBuf(sock);
+ if (p != NULL)
+ freetData(p->data, sock);
+ }while(p!=NULL);
+}
diff --git a/firmwares/wifishield/wifiHD/src/ard_utils.h b/firmwares/wifishield/wifiHD/src/ard_utils.h
index 4b31c46..5dee3c0 100644
--- a/firmwares/wifishield/wifiHD/src/ard_utils.h
+++ b/firmwares/wifishield/wifiHD/src/ard_utils.h
@@ -36,9 +36,12 @@
#define SIGN2_DN LED2_DN
#define SIGN2_TL LED2_TL
-#define DEB_PIN_UP() gpio_set_gpio_pin(DEB_PIN_GPIO)
-#define DEB_PIN_DN() gpio_clr_gpio_pin(DEB_PIN_GPIO)
-#define DEB_PIN_ENA() gpio_enable_gpio_pin(DEB_PIN_GPIO);
+#define DEB_PIN_UP(X) gpio_set_gpio_pin(DEB##X##_PIN_GPIO)
+#define DEB_PIN_DN(X) gpio_clr_gpio_pin(DEB##X##_PIN_GPIO)
+#define DEB_PIN_ENA(X) gpio_enable_gpio_pin(DEB##X##_PIN_GPIO)
+#define DEB_PIN_TOGGLE(X) gpio_tgl_gpio_pin(DEB##X##_PIN_GPIO)
+#define DEB_PIN_TRIGGER(X) DEB_PIN_DN(X); DEB_PIN_UP(X);
+
#else
#define SIGN0_UP()
@@ -51,9 +54,11 @@
#define SIGN2_DN()
#define SIGN2_TL()
-#define DEB_PIN_UP()
-#define DEB_PIN_DN()
-#define DEB_PIN_ENA()
+#define DEB_PIN_UP(X)
+#define DEB_PIN_DN(X)
+#define DEB_PIN_ENA(X)
+#define DEB_PIN_TOGGLE(X)
+#define DEB_PIN_TRIGGER(X)
//#define TOGGLE_SIG0
#endif
@@ -119,8 +124,16 @@
#define PUT_DATA_INT(INT, BYTE, IDX) { \
- uint16_t _int = INT; \
- BYTE[IDX] = (uint8_t)((_int & 0xff00)>>8); \
+ uint16_t _int = INT; \
+ BYTE[IDX] = 2; \
+ BYTE[IDX+1] = (uint8_t)((_int & 0xff00)>>8); \
+ BYTE[IDX+2] = (uint8_t)(_int & 0xff); \
+}
+
+#define PUT_DATA_INT_NO(INT, BYTE, IDX) { \
+ uint16_t _int = INT; \
+ BYTE[IDX] = 2; \
+ BYTE[IDX+2] = (uint8_t)((_int & 0xff00)>>8); \
BYTE[IDX+1] = (uint8_t)(_int & 0xff); \
}
@@ -201,7 +214,7 @@
#define STATSPI_TIMEOUT_ERROR() \
statSpi.timeoutIntErr++; \
statSpi.rxErr++; \
- statSpi.lastError = err; \
+ statSpi.lastError = SPI_TIMEOUT_ERROR; \
statSpi.status = spi_getStatus(ARD_SPI);
#define STATSPI_DISALIGN_ERROR() \
@@ -228,11 +241,15 @@
#define STATSPI_OVERRIDE_ERROR()
#endif
-#define DUMP_TCP_STATE(TTCP) \
- INFO_TCP("ttcp:%p tpcb:%p state:%d lpcb:%p state:%d\n", \
- TTCP, TTCP->tpcb, (TTCP->tpcb)?TTCP->tpcb->state:0, \
- TTCP->lpcb, (TTCP->lpcb)?TTCP->lpcb->state:0);
-
+#define DUMP_TCP_STATE(TTCP) do {\
+ INFO_TCP("ttcp:%p tpcb:%p state:%d lpcb:%p state:%d left:%d sent:%d\n", \
+ TTCP, TTCP->tpcb[0], (TTCP->tpcb[0])?TTCP->tpcb[0]->state:0, \
+ TTCP->lpcb, (TTCP->lpcb)?TTCP->lpcb->state:0, \
+ (TTCP)?TTCP->left:0, (TTCP)?TTCP->buff_sent:0); \
+ } while(0);
+
+#define Mode2Str(_Mode) ((_Mode==0)?"TRANSMIT":"RECEIVE")
+#define ProtMode2Str(_protMode) ((_protMode==0)?"TCP":"UDP")
typedef struct sData
{
@@ -246,7 +263,13 @@ struct pbuf;
void init_pBuf();
-void insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb);
+uint8_t* insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb);
+
+uint8_t* insertBuf(uint8_t sock, uint8_t* buf, uint16_t len);
+
+uint8_t* mergeBuf(uint8_t sock, uint8_t** buf, uint16_t* _len);
+
+uint16_t clearBuf(uint8_t sock);
tData* get_pBuf(uint8_t sock);
@@ -260,8 +283,12 @@ bool getTcpData(uint8_t sock, void** payload, uint16_t* len);
bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek);
+uint16_t getAvailTcpDataByte(uint8_t sock);
+
bool isAvailTcpDataByte(uint8_t sock);
uint8_t freeTcpData(uint8_t sock);
+void freeAllTcpData(uint8_t sock);
+
#endif /* ARD_UTILS_H_ */
diff --git a/firmwares/wifishield/wifiHD/src/cmd_wl.c b/firmwares/wifishield/wifiHD/src/cmd_wl.c
index 61634ef..3356931 100644
--- a/firmwares/wifishield/wifiHD/src/cmd_wl.c
+++ b/firmwares/wifishield/wifiHD/src/cmd_wl.c
@@ -43,24 +43,11 @@
#include "lwip/dns.h"
#include "debug.h"
#include "ard_spi.h"
+#include "ard_tcp.h"
+#include "ard_utils.h"
extern void showTTCPstatus();
-#define ENABLE_DEBUG_LEVEL 1
-#define VERBOSE_DEBUG_LEVEL 2
-
-#define CHECK_ENA_DEBUG(LEVEL, FLAG) \
- do{ \
- if (LEVEL >= ENABLE_DEBUG_LEVEL) enableDebug |= FLAG; \
- else enableDebug &= ~FLAG; \
- }while(0);
-
-#define CHECK_VERB_DEBUG(LEVEL, FLAG) \
- do{ \
- if (LEVEL >= VERBOSE_DEBUG_LEVEL) verboseDebug |= FLAG; \
- else verboseDebug &= ~FLAG; \
- }while(0);
-
#define _DNS_CMD_
/**
@@ -373,14 +360,15 @@ cmd_setDnsServer(int argc, char* argv[], void* ctx)
*
*/
cmd_state_t
-cmd_startTcpSrv(int argc, char* argv[], void* ctx)
+cmd_startSrv(int argc, char* argv[], void* ctx)
{
- const char *usage = "usage: startTcpSrv <port> <sock>\n";
+ const char *usage = "usage: startSrv <port> <sock> <tcp(0)/udp(1)>\n";
int port = 0;
int sock = 0;
+ int protMode = 0;
- if (argc < 3) {
+ if (argc < 4) {
printk(usage);
return CMD_DONE;
}
@@ -389,15 +377,50 @@ cmd_startTcpSrv(int argc, char* argv[], void* ctx)
port = atoi(argv[1]);
/* socket index */
sock = atoi(argv[2]);
+ /* Protocol Mode */
+ protMode = atoi(argv[3]);
- printk("Start TCP server on port %d sock %d\n", port, sock);
- if (start_server_tcp(port, sock) != -1)
+ printk("Start %s server on port %d sock %d\n", ProtMode2Str(protMode), port, sock);
+ if (start_server_tcp(port, sock, protMode) == -1)
{
- printk("Start TCP server on port %d sock %d FAILED\n", port, sock);
+ WARN("Start %s server on port %d sock %d FAILED\n", ProtMode2Str(protMode), port, sock);
}
return CMD_DONE;
}
+/**
+ *
+ */
+cmd_state_t
+cmd_startCli(int argc, char* argv[], void* ctx)
+{
+ const char *usage = "usage: startCli <ipaddr> <port> <sock> <tcp(0)/udp(1)>\n";
+ struct ip_addr addr = {0};
+ int port = 0;
+ int sock = 0;
+ int protMode = 0;
+
+ if (argc < 5) {
+ printk(usage);
+ return CMD_DONE;
+ }
+
+ /* IP address */
+ addr = str2ip(argv[1]);
+ /* TCP port */
+ port = atoi(argv[2]);
+ /* socket index */
+ sock = atoi(argv[3]);
+ /* Protocol Mode */
+ protMode = atoi(argv[4]);
+
+ printk("Start client on addr 0x%x, port %d sock %d mode %d\n", addr, port, sock, protMode);
+ if (start_client_tcp(addr.addr, port, sock, protMode) == -1)
+ {
+ WARN("Start client on port %d sock %d prot %d mode %d FAILED\n", port, sock, protMode);
+ }
+ return CMD_DONE;
+}
#endif
@@ -595,16 +618,14 @@ cmd_debug(int argc, char* argv[], void* ctx)
if (argc == 2 && strcmp(argv[1], "off") == 0) {
printk("Debug OFF\n");
- enableDebug = DEFAULT_INFO_FLAG;
- verboseDebug = 0;
+ INIT_DEBUG_VARIABLES()
return CMD_DONE;
}else if (argc == 2 && strcmp(argv[1], "print") == 0) {
- printk("Debug enabled: 0x%x\n", enableDebug);
- printk("Verbose enabled: 0x%x\n", verboseDebug);
+ PRINT_DEBUG_VARIABLES()
return CMD_DONE;
}else if (argc == 2 && strcmp(argv[1], "on") == 0) {
printk("Debug ON\n");
- enableDebug = 0xff;
+ TURNON_DEBUG_VARIABLES();
return CMD_DONE;
}
if (argc < 3) {
@@ -613,23 +634,91 @@ cmd_debug(int argc, char* argv[], void* ctx)
}
level = atoi(argv[2]);
if (argc == 3 && strcmp(argv[1], "init") == 0) {
- CHECK_ENA_DEBUG(level, INFO_INIT_FLAG);
- CHECK_VERB_DEBUG(level, INFO_INIT_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_INIT_FLAG);
}else if (argc == 3 && strcmp(argv[1], "spi") == 0) {
- CHECK_ENA_DEBUG(level, INFO_SPI_FLAG);
- CHECK_VERB_DEBUG(level, INFO_SPI_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_SPI_FLAG);
}else if (argc == 3 && strcmp(argv[1], "tcp") == 0) {
- CHECK_ENA_DEBUG(level, INFO_TCP_FLAG);
- CHECK_VERB_DEBUG(level, INFO_TCP_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_TCP_FLAG);
}else if (argc == 3 && strcmp(argv[1], "cm") == 0) {
- CHECK_ENA_DEBUG(level, INFO_CM_FLAG);
- CHECK_VERB_DEBUG(level, INFO_CM_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_CM_FLAG);
}else if (argc == 3 && strcmp(argv[1], "util") == 0) {
- CHECK_ENA_DEBUG(level, INFO_UTIL_FLAG);
- CHECK_VERB_DEBUG(level, INFO_UTIL_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_UTIL_FLAG);
}else if (argc == 3 && strcmp(argv[1], "warn") == 0) {
- CHECK_ENA_DEBUG(level, INFO_WARN_FLAG);
- CHECK_VERB_DEBUG(level, INFO_WARN_FLAG);
+ CHECK_DEBUG_LEVEL(level, INFO_WARN_FLAG);
}
return CMD_DONE;
}
+
+extern void dumpPbuf(uint8_t sock);
+
+/**
+ *
+ */
+cmd_state_t
+cmd_dumpBuf(int argc, char* argv[], void* ctx)
+{
+ const char *usage = "usage: dumpPbuf [sock]\n\t"\
+ "sock: socket Number\n";
+
+ if (argc == 2 && strcmp(argv[1], "all") == 0) {
+ printk("Dump All Buffers\n");
+ int i = 0;
+ for (; i<MAX_SOCK_NUM; ++i)
+ {
+ printk("Socket: %d\n", i);
+ dumpPbuf(i);
+ }
+ }else if (argc == 2) {
+ uint8_t sock = atoi(argv[1]);
+ printk("Socket: %d\n", sock);
+ dumpPbuf(sock);
+ }else {
+ printk(usage);
+ }
+ return CMD_DONE;
+}
+
+
+
+/**
+ *
+ */
+cmd_state_t
+cmd_sendUdpData(int argc, char* argv[], void* ctx)
+{
+ const char pattern[]={'M', 'I', 'M', 'L', 'F', 'D'};
+ const char* pattern2[]={"Prova", "1234567890","FineTest"};
+ const char *usage = "usage: sendUdp [sock]\n\t"\
+ "sock: socket Number\n";
+
+ if (argc < 2)
+ printk(usage);
+
+ if (argc >= 2) {
+
+ uint8_t sock = atoi(argv[1]);
+ printk("Socket: %d\n", sock);
+
+ if (argc >= 3) {
+ uint8_t patternType = atoi(argv[2]);
+ printk("PatternType: %d\n", patternType);
+ if (patternType == 1)
+ {
+ insertBuf(sock, (uint8_t*)pattern2[0], strlen(pattern2[0]));
+ insertBuf(sock, (uint8_t*)pattern2[1], strlen(pattern2[1]));
+ insertBuf(sock, (uint8_t*)pattern2[2], strlen(pattern2[2]));
+ }
+ if (patternType == 2)
+ {
+ mergeBuf(sock, NULL, NULL);
+ }
+ }else{
+ if (sock < MAX_SOCK_NUM)
+ {
+ sendUdpData(getTTCP(sock, TTCP_MODE_TRANSMIT), (uint8_t*)pattern, sizeof(pattern)/sizeof(char));
+ }
+ }
+
+ }
+ return CMD_DONE;
+}
diff --git a/firmwares/wifishield/wifiHD/src/cmd_wl.h b/firmwares/wifishield/wifiHD/src/cmd_wl.h
index 3953b95..a1d1a0f 100644
--- a/firmwares/wifishield/wifiHD/src/cmd_wl.h
+++ b/firmwares/wifishield/wifiHD/src/cmd_wl.h
@@ -54,7 +54,10 @@ cmd_state_t cmd_statSpi(int argc, char* argv[], void* ctx);
cmd_state_t cmd_resetStatSpi(int argc, char* argv[], void* ctx);
cmd_state_t cmd_gethostbyname(int argc, char* argv[], void* ctx);
cmd_state_t cmd_setDnsServer(int argc, char* argv[], void* ctx);
-cmd_state_t cmd_startTcpSrv(int argc, char* argv[], void* ctx);
+cmd_state_t cmd_startSrv(int argc, char* argv[], void* ctx);
+cmd_state_t cmd_startCli(int argc, char* argv[], void* ctx);
+cmd_state_t cmd_dumpBuf(int argc, char* argv[], void* ctx);
+cmd_state_t cmd_sendUdpData(int argc, char* argv[], void* ctx);
#ifdef WFE_6_12
cmd_state_t cmd_ibss(int argc, char* argv[], void* ctx);
#endif
diff --git a/firmwares/wifishield/wifiHD/src/console.c b/firmwares/wifishield/wifiHD/src/console.c
index 5ead91e..e54943f 100644
--- a/firmwares/wifishield/wifiHD/src/console.c
+++ b/firmwares/wifishield/wifiHD/src/console.c
@@ -35,7 +35,7 @@
#include <board_init.h>
#include <usart.h>
-#define MAX_CMD_CONSOLE_NUM 12
+#define MAX_CMD_CONSOLE_NUM 15
struct {
cmd_cb_t cb;
const char* str;
diff --git a/firmwares/wifishield/wifiHD/src/debug.h b/firmwares/wifishield/wifiHD/src/debug.h
index 18608ff..e48da64 100644
--- a/firmwares/wifishield/wifiHD/src/debug.h
+++ b/firmwares/wifishield/wifiHD/src/debug.h
@@ -18,92 +18,169 @@
#define INFO_SPI_FLAG 4
#define INFO_CM_FLAG 8
#define INFO_UTIL_FLAG 16
-#define INFO_5 32
+
#define INFO_D (1<<0xD) // Debug
#define INFO_E (1<<0xE) // Error
#define INFO_WARN_FLAG (1<<0xF) // Warning
#define DEFAULT_INFO_FLAG 0 //INFO_WARN_FLAG
-extern uint16_t enableDebug;
-extern uint16_t verboseDebug;
-
-#ifdef _INFO_DEBUG_
-#define INFO_INIT(msg, args...) do { \
-if (enableDebug & INFO_INIT_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
-} while (0)
-
-#define INFO_TCP(msg, args...) do { \
-if (enableDebug & INFO_TCP_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
-} while (0)
+#ifdef _DEBUG_
+#define DEFINE_DEBUG_VARIABLES() \
+uint16_t enableDebug = DEFAULT_INFO_FLAG | INFO_WARN_FLAG; \
+uint16_t verboseDebug = 0; \
+uint16_t dumpDebug = 0; \
+uint16_t pollDebug = 0;
+#else
+#define DEFINE_DEBUG_VARIABLES() \
+uint16_t enableDebug = DEFAULT_INFO_FLAG; \
+uint16_t verboseDebug = 0; \
+uint16_t dumpDebug = 0; \
+uint16_t pollDebug = 0;
+#endif
-#define INFO_TCP_VER(msg, args...) do { \
-if ((enableDebug & INFO_TCP_FLAG)&&(verboseDebug & INFO_TCP_FLAG)) \
- printk("I-[%s] " msg , __func__ , ##args ); \
-} while (0)
+#define INIT_DEBUG_VARIABLES() \
+ enableDebug = DEFAULT_INFO_FLAG | INFO_WARN_FLAG; \
+ verboseDebug = 0; \
+ dumpDebug = 0; pollDebug = 0;
-#define INFO_SPI(msg, args...) do { \
-if (enableDebug & INFO_SPI_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
-} while (0)
+#define PRINT_DEBUG_VARIABLES() \
+ printk("Debug enabled: 0x%x\n", enableDebug); \
+ printk("Verbose enabled: 0x%x\n", verboseDebug); \
+ printk("Dump enabled: 0x%x\n", dumpDebug); \
+ printk("POoll enabled: 0x%x\n", pollDebug);
-#define INFO_SPI_VER(msg, args...) do { \
-if ((enableDebug & INFO_SPI_FLAG)&&(verboseDebug & INFO_SPI_FLAG)) \
- printk("I-[%s] " msg , __func__ , ##args ); \
-} while (0)
+#define TURNON_DEBUG_VARIABLES() \
+ enableDebug = 0xff;
+extern uint16_t enableDebug;
+extern uint16_t verboseDebug;
+extern uint16_t dumpDebug;
+extern uint16_t pollDebug;
+
+#define ENABLE_DEBUG_LEVEL 1
+#define VERBOSE_DEBUG_LEVEL 2
+#define DUMP_DEBUG_LEVEL 3
+#define POLL_DEBUG_LEVEL 4
+
+#define CHECK_DEBUG(VAR, LEVEL, LEVEL_LIMIT, FLAG) \
+ do{ \
+ if (LEVEL >= LEVEL_LIMIT) VAR |= FLAG; \
+ else VAR &= ~FLAG; \
+ }while(0);
+
+#define CHECK_ENA_DEBUG(LEVEL, FLAG) \
+ CHECK_DEBUG(enableDebug, LEVEL, ENABLE_DEBUG_LEVEL, FLAG)
+#define CHECK_VERB_DEBUG(LEVEL, FLAG) \
+ CHECK_DEBUG(verboseDebug, LEVEL, VERBOSE_DEBUG_LEVEL, FLAG)
+#define CHECK_DUMP_DEBUG(LEVEL, FLAG) \
+ CHECK_DEBUG(dumpDebug, LEVEL, DUMP_DEBUG_LEVEL, FLAG)
+#define CHECK_POLL_DEBUG(LEVEL, FLAG) \
+ CHECK_DEBUG(pollDebug, LEVEL, POLL_DEBUG_LEVEL, FLAG)
+
+
+#define CHECK_DEBUG_LEVEL(LEVEL, INFO_FLAG) \
+ CHECK_ENA_DEBUG(LEVEL, INFO_FLAG) \
+ CHECK_VERB_DEBUG(LEVEL, INFO_FLAG) \
+ CHECK_DUMP_DEBUG(LEVEL, INFO_FLAG) \
+ CHECK_POLL_DEBUG(LEVEL, INFO_FLAG)
-#define INFO_UTIL(msg, args...) do { \
-if (enableDebug & INFO_UTIL_FLAG) printk("I-[%s] " msg , __func__ , ##args ); \
+#ifdef _INFO_DEBUG_
+#define PRINT_DEBUG(msg, args...) do { \
+ printk("[%s] " msg , __func__ , ##args ); \
} while (0)
-#define INFO_UTIL_VER(msg, args...) do { \
-if ((enableDebug & INFO_UTIL_FLAG)&&(verboseDebug & INFO_UTIL_FLAG)) \
+#define INFO_DEBUG(msg, args...) do { \
printk("I-[%s] " msg , __func__ , ##args ); \
} while (0)
+#define WARN_DEBUG(msg, args...) do { \
+ printk("W-[%s] " msg , __func__ , ##args ); \
+} while (0)
#else
-#define INFO_INIT(msg, args...) do {}while(0);
-#define INFO_TCP(msg, args...) do {}while(0);
-#define INFO_TCP_VER(msg, args...) do { }while(0);
-#define INFO_SPI(msg, args...) do {}while(0);
-#define INFO_SPI_VER(msg, args...) do { }while(0);
-#define INFO_UTIL(msg, args...) do {}while(0);
-#define INFO_UTIL_VER(msg, args...) do { }while(0);
+do { }while(0);
#endif
-#ifdef _APP_DEBUG_
-#define INFO(msg, args...) do { \
-printk("I-[%s] " msg , __func__ , ##args ); \
+#define IF_DEBUG(X,Y) do { \
+if (enableDebug & INFO_##X##_FLAG) \
+Y; \
} while (0)
-#else /* !defined(_DEBUG_) */
-//#define INFO(msg, args...) do {} while (0)
-#endif /* !defined(_DEBUG_) */
+#define IF_DEBUG_VER(X,Y) do { \
+if (verboseDebug & INFO_##X##_FLAG) \
+Y; \
+} while (0)
-#if 1
-#define WARN(msg, args...) do { \
- if (enableDebug & INFO_WARN_FLAG) printk("W-[%s] " msg , __func__ , ##args ); \
- } while (0)
-#else
-#define WARN(msg, args...) do { } while (0)
-#endif
+#define IF_DEBUG_DUMP(X,Y) do { \
+if (dumpDebug & INFO_##X##_FLAG) \
+Y; \
+} while (0)
+
+#define IF_DEBUG_POLL(X,Y) do { \
+if (pollDebug & INFO_##X##_FLAG) {\
+Y; \
+}} while (0)
+
+
+
+#define IF_WARN(Y) IF_DEBUG(WARN,Y)
+#define IF_WARN_VER(Y) IF_DEBUG_VER(WARN,Y)
+#define IF_TCP(Y) IF_DEBUG(TCP,Y)
+#define IF_TCP_VER(Y) IF_DEBUG_VER(TCP,Y)
+#define IF_TCP_POLL(Y) IF_DEBUG_POLL(TCP,Y)
+#define IF_TCP_DUMP(Y) IF_DEBUG_DUMP(TCP,Y)
+#define IF_SPI(Y) IF_DEBUG(SPI,Y)
+#define IF_SPI_VER(Y) IF_DEBUG_VER(SPI,Y)
+#define IF_SPI_DUMP(Y) IF_DEBUG_DUMP(SPI,Y)
+#define IF_SPI_POLL(Y) IF_DEBUG_POLL(SPI,Y)
+#define IF_UTIL(Y) IF_DEBUG(UTIL,Y)
+#define IF_UTIL_VER(Y) IF_DEBUG_VER(UTIL,Y)
+
+#define WARN(msg, args...) IF_DEBUG(WARN,WARN_DEBUG(msg, ##args))
+#define WARN_VER(msg, args...) IF_DEBUG_VER(WARN,WARN_DEBUG(msg, ##args))
+#define WARN_POLL(msg, args...) IF_DEBUG_POLL(WARN,WARN_DEBUG(msg, ##args))
+#define INFO_INIT(msg, args...) IF_DEBUG(INIT,PRINT_DEBUG(msg, ##args))
+#define INFO_INIT_VER(msg, args...) IF_DEBUG_VER(INIT,PRINT_DEBUG(msg, ##args))
+#define INFO_TCP(msg, args...) IF_DEBUG(TCP,PRINT_DEBUG(msg, ##args))
+#define INFO_TCP_VER(msg, args...) IF_DEBUG_VER(TCP,PRINT_DEBUG(msg, ##args))
+#define INFO_TCP_DUMP(msg, args...) IF_DEBUG_DUMP(TCP,PRINT_DEBUG(msg, ##args))
+#define INFO_TCP_POLL(msg, args...) IF_DEBUG_POLL(TCP,PRINT_DEBUG(msg, ##args))
+#define INFO_SPI(msg, args...) IF_DEBUG(SPI,PRINT_DEBUG(msg, ##args))
+#define INFO_SPI_VER(msg, args...) IF_DEBUG_VER(SPI,PRINT_DEBUG(msg, ##args))
+#define INFO_SPI_DUMP(msg, args...) IF_DEBUG_DUMP(SPI,PRINT_DEBUG(msg, ##args))
+#define INFO_SPI_POLL(msg, args...) IF_DEBUG_POLL(SPI,PRINT_DEBUG(msg, ##args))
+#define INFO_UTIL(msg, args...) IF_DEBUG(UTIL,PRINT_DEBUG(msg, ##args))
+#define INFO_UTIL_VER(msg, args...) IF_DEBUG_VER(UTIL,PRINT_DEBUG(msg, ##args))
+#define CM_DPRINTF(msg, args...) IF_DEBUG(CM,PRINT_DEBUG(msg, ##args))
extern void dump(char* _buf, uint16_t _count);
-#ifdef _APP_DEBUG_
-#define DUMP(BUF, COUNT) do { \
- printk("[%s]\n", __func__); \
+#define _DUMP(BUF, COUNT) do { \
+ printk("[%s]: ", __func__); \
dump((char*)BUF, COUNT); \
} while (0)
+
+#ifdef _APP_DEBUG_
+#define DUMP(BUF, COUNT) _DUMP(BUF, COUNT)
#else
#define DUMP(BUF, COUNT) do {} while (0)
#endif
#endif
-#define DUMP_TCP(BUF, COUNT) do { \
- if (verboseDebug & INFO_TCP_FLAG) { \
- printk("[%s]\n", __func__); \
- dump((char*)BUF, COUNT); \
- }} while (0)
+#define DUMP_TCP(BUF, COUNT) IF_TCP_DUMP(_DUMP(BUF, COUNT))
+#define DUMP_SPI(BUF, COUNT) IF_SPI_DUMP(_DUMP(BUF, COUNT))
+
+#define DUMP_SPI_CMD(BUF) do { \
+ if (dumpDebug & INFO_SPI_FLAG) { \
+ int i = 0; \
+ for (; i < CMD_MAX_LEN; ++i) \
+ { \
+ printk("0x%x ", BUF[i]); \
+ if (BUF[i] == END_CMD) \
+ break; \
+ } \
+ printk("\n"); \
+ } \
+}while(0);
diff --git a/firmwares/wifishield/wifiHD/src/lwipopts.h b/firmwares/wifishield/wifiHD/src/lwipopts.h
index 64965bf..7b08b84 100644
--- a/firmwares/wifishield/wifiHD/src/lwipopts.h
+++ b/firmwares/wifishield/wifiHD/src/lwipopts.h
@@ -111,7 +111,7 @@
* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP connections.
* (requires the LWIP_TCP option)
*/
-#define MEMP_NUM_TCP_PCB 2
+#define MEMP_NUM_TCP_PCB 4
/**
* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP connections.
diff --git a/firmwares/wifishield/wifiHD/src/main.c b/firmwares/wifishield/wifiHD/src/main.c
index 8961e35..297e015 100644
--- a/firmwares/wifishield/wifiHD/src/main.c
+++ b/firmwares/wifishield/wifiHD/src/main.c
@@ -39,7 +39,7 @@
#include <lwip_setup.h>
/* FIRMWARE version */
-const char* fwVersion = "1.0.0";
+const char* fwVersion = "1.1.0";
#if BOARD == ARDUINO
#if !defined(DATAFLASH)
@@ -89,14 +89,7 @@ bool scanNetCompleted = false;
static bool initSpiComplete = false;
// variable used as enable flag for debug prints
-#ifdef _DEBUG_
-uint16_t enableDebug = DEFAULT_INFO_FLAG | INFO_WARN_FLAG;// | INFO_SPI_FLAG;
-uint16_t verboseDebug = 0;
-#else
-uint16_t enableDebug = DEFAULT_INFO_FLAG;
-uint16_t verboseDebug = 0;
-#endif
-
+DEFINE_DEBUG_VARIABLES();
/**
*
@@ -287,6 +280,7 @@ void initShell()
console_add_cmd("setkey", cmd_setkey, NULL);
console_add_cmd("status", cmd_status, NULL);
console_add_cmd("debug", cmd_debug, NULL);
+ console_add_cmd("dumpBuf", cmd_dumpBuf, NULL);
#ifdef ADD_CMDS
console_add_cmd("powersave", cmd_power, NULL);
@@ -307,8 +301,11 @@ void initShell()
#ifdef _DNS_CMD_
console_add_cmd("getHost", cmd_gethostbyname, NULL);
console_add_cmd("setDNS", cmd_setDnsServer, NULL);
- console_add_cmd("startTcpSrv", cmd_startTcpSrv, NULL);
#endif
+ console_add_cmd("startSrv", cmd_startSrv, NULL);
+ console_add_cmd("startCli", cmd_startCli, NULL);
+ console_add_cmd("sendUdp", cmd_sendUdpData, NULL);
+
}
/**
@@ -359,7 +356,9 @@ void startup_init(void)
// if DEBUG enabled use DEB_PIN_GPIO for debug purposes
DEB_PIN_ENA();
+ DEB_PIN_ENA(2);
DEB_PIN_UP();
+ DEB_PIN_UP(2);
}
/**
diff --git a/firmwares/wifishield/wifiHD/src/wifi_spi.h b/firmwares/wifishield/wifiHD/src/wifi_spi.h
index 6e6b4bb..48da984 100644
--- a/firmwares/wifishield/wifiHD/src/wifi_spi.h
+++ b/firmwares/wifishield/wifiHD/src/wifi_spi.h
@@ -56,11 +56,14 @@ enum {
START_SCAN_NETWORKS = 0x36,
GET_FW_VERSION_CMD = 0x37,
GET_TEST_CMD = 0x38,
+ SEND_DATA_UDP_CMD = 0x39,
+ GET_REMOTE_DATA_CMD = 0x3A,
// All command with DATA_FLAG 0x40 send a 16bit Len
SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45,
+ INSERT_DATABUF_CMD = 0x46,
};
diff --git a/firmwares/wifishield/wifiHD/src/wl_cm.c b/firmwares/wifishield/wifiHD/src/wl_cm.c
index bef1afc..ebc0e45 100644
--- a/firmwares/wifishield/wifiHD/src/wl_cm.c
+++ b/firmwares/wifishield/wifiHD/src/wl_cm.c
@@ -59,15 +59,9 @@
*/
#define ROAMING_RSSI_DIFF 10
-
-#if 1
# include "printf-stdarg.h"
#include "ard_utils.h"
-# define CM_DPRINTF(fmt...) if (enableDebug & INFO_CM_FLAG) printk(fmt)
-#else
-# define CM_DPRINTF(fmt...)
-#endif
-
+#include "debug.h"
/** \defgroup wl_cm Connection Manager
*