diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index e11b086e2..56802fd14 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -11,6 +11,7 @@ extern "C" { #include "WiFiServer.h" #include "server_drv.h" + uint16_t WiFiClient::_srcport = 1024; WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { @@ -41,7 +42,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { _sock = getFirstSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartClient(uint32_t(ip), port, _sock); + ServerDrv::startClient(uint32_t(ip), port, _sock); WiFiClass::_state[_sock] = _sock; }else{ return 0; @@ -106,11 +107,13 @@ void WiFiClient::flush() { } void WiFiClient::stop() { + + INFO("1)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); if (_sock == 255) return; - - // attempt to close the connection gracefully (send a FIN to other side) - disconnect(WiFiClass::_state[_sock]); + + ServerDrv::stopClient(_sock); + unsigned long start = millis(); // wait a second for the connection to close @@ -119,9 +122,13 @@ void WiFiClient::stop() { // if it hasn't closed, close it forcefully if (status() != CLOSED) - close(_sock); + { + //TODO force close + //close(_sock); + } + - WiFiClass::_server_port[_sock] = 0; + INFO("2)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); _sock = 255; } @@ -130,6 +137,7 @@ uint8_t WiFiClient::connected() { return 0; } else { uint8_t s = status(); + INFO("Client status: %d", s); return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || (s == CLOSE_WAIT && !available())); } @@ -139,7 +147,7 @@ uint8_t WiFiClient::status() { if (_sock == 255) { return CLOSED; } else { - return ServerDrv::getState(_sock); + return ServerDrv::getClientState(_sock); } } diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index 860759383..cd0a0f5ab 100644 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -1,6 +1,10 @@ #include #include "server_drv.h" +extern "C" { + #include "utility/debug.h" +} + #include "WiFi.h" #include "WiFiClient.h" #include "WiFiServer.h" @@ -17,7 +21,7 @@ void WiFiServer::begin() uint8_t _sock = WiFiClass::getSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartServer(_port, _sock); + ServerDrv::startServer(_port, _sock); WiFiClass::_server_port[_sock] = _port; } } @@ -25,18 +29,34 @@ void WiFiServer::begin() WiFiClient WiFiServer::available(byte* status) { //accept(); + static byte tmp_cli_status = 0; + static byte tmp_ser_status = 0; + static int cycle = 0; for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - if (WiFiClass::_server_port[sock] != 0) + if (WiFiClass::_server_port[sock] == _port) { WiFiClient client(sock); - int _status = client.status(); + uint8_t _status = client.status(); + uint8_t _ser_status = this->status(); + if ((tmp_cli_status != _status)||(tmp_ser_status != _ser_status)) + { + INFO("%d)Sock: %d Client Status: %d Server Status: %d port: %d", cycle, sock, _status, _ser_status, WiFiClass::_server_port[sock]); + tmp_cli_status = _status; + tmp_ser_status = _ser_status; + cycle = 0; + }else{ + ++cycle; + } if (status != NULL) *status = _status; - if (WiFiClass::_server_port[sock] == _port && - _status == ESTABLISHED) + //server not in listen state, restart it + if (this->status()==0) + ServerDrv::startServer(_port, sock); + + if (_status == ESTABLISHED) { return client; //TODO } @@ -46,6 +66,11 @@ WiFiClient WiFiServer::available(byte* status) return WiFiClient(255); } +uint8_t WiFiServer::status() { + return ServerDrv::getServerState(0); +} + + size_t WiFiServer::write(uint8_t b) { return write(&b, 1); diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h index d93b0ced1..68b574c29 100755 --- a/WiFi/WiFiServer.h +++ b/WiFi/WiFiServer.h @@ -19,6 +19,7 @@ public: void begin(); virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buf, size_t size); + uint8_t status(); using Print::write; }; diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 0cc05d49c..089103cca 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -30,6 +30,12 @@ #ifdef _DEBUG_ +#define INFO(format, args...) do { \ + char buf[250]; \ + sprintf(buf, format, args); \ + Serial.println(buf); \ +} while(0); + #define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\ Serial.println(x); \ }while (0); @@ -39,11 +45,6 @@ Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ }while (0); -#define INFO(format, args...) do { \ - char buf[250]; \ - sprintf(buf, format, args); \ - Serial.println(buf); \ -} while(0); #else #define INFO1(x) do {} while(0); diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index b403a31b3..30bc5187c 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -12,7 +12,7 @@ extern "C" { // Start server TCP on port specified -void ServerDrv::StartServer(uint16_t port, uint8_t sock) +void ServerDrv::startServer(uint16_t port, uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -34,7 +34,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) } // Start server TCP on port specified -void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) +void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -56,7 +56,29 @@ void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) SpiDrv::spiSlaveDeselect(); } -uint8_t ServerDrv::getState(uint8_t sock) +// Start server TCP on port specified +void ServerDrv::stopClient(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + + +uint8_t ServerDrv::getServerState(uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -77,6 +99,26 @@ uint8_t ServerDrv::getState(uint8_t sock) return _data; } +uint8_t ServerDrv::getClientState(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} uint8_t ServerDrv::availData(uint8_t sock) { diff --git a/WiFi/utility/server_drv.h b/WiFi/utility/server_drv.h index 6d28d13f1..c7afcff23 100644 --- a/WiFi/utility/server_drv.h +++ b/WiFi/utility/server_drv.h @@ -8,11 +8,15 @@ class ServerDrv { public: // Start server TCP on port specified - static void StartServer(uint16_t port, uint8_t sock); + static void startServer(uint16_t port, uint8_t sock); - static void StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock); + static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock); + + static void stopClient(uint8_t sock); - static uint8_t getState(uint8_t sock); + static uint8_t getServerState(uint8_t sock); + + static uint8_t getClientState(uint8_t sock); static bool getData(uint8_t sock, uint8_t *data); diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 576c5fb11..cdcc02fea 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -39,6 +39,8 @@ enum { AVAIL_DATA_TCP_CMD = 0x2B, GET_DATA_TCP_CMD = 0x2C, START_CLIENT_TCP_CMD= 0x2D, + STOP_CLIENT_TCP_CMD = 0x2E, + GET_CLIENT_STATE_TCP_CMD= 0x2F, DISCONNECT_CMD = 0x30, // All command with DATA_FLAG 0x40 send a 16bit Len