diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 0f10babec..9fd4bba98 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -1,8 +1,6 @@ #include "wifi_drv.h" #include "WiFi.h" -#define _DEBUG_ - extern "C" { #include "utility/wl_definitions.h" #include "utility/wl_types.h" @@ -101,6 +99,7 @@ int WiFiClass::begin(char* ssid, const char *passphrase) { delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); + INFO("Status: %d", status); } while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index c3d67a5b7..075af56c9 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -52,25 +52,29 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { } size_t WiFiClient::write(uint8_t b) { - if (_sock != 255) - { - START(); - ServerDrv::sendData(_sock, &b, 1); - while (!ServerDrv::isDataSent(_sock)); - END(); - return 1; - } - return 0; + return write(&b, 1); } size_t WiFiClient::write(const uint8_t *buf, size_t size) { - if (_sock != 255) + if (_sock >= MAX_SOCK_NUM) { - ServerDrv::sendData(_sock, buf, size); - while (!ServerDrv::isDataSent(_sock)); - return size; + setWriteError(); + return 0; } - return 0; + if (size==0) + { + setWriteError(); + return 0; + } + + + if ((!ServerDrv::sendData(_sock, buf, size)) || + (!ServerDrv::checkDataSent(_sock))) + { + setWriteError(); + return 0; + } + return size; } int WiFiClient::available() { @@ -86,6 +90,7 @@ int WiFiClient::read() { uint8_t b; if (!available()) return -1; + ServerDrv::getData(_sock, &b); return b; } @@ -109,7 +114,6 @@ 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; @@ -121,24 +125,16 @@ void WiFiClient::stop() { while (status() != CLOSED && millis() - start < 1000) delay(1); - // if it hasn't closed, close it forcefully - if (status() != CLOSED) - { - //TODO force close - //close(_sock); - } - - - INFO("2)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); _sock = 255; } uint8_t WiFiClient::connected() { + if (_sock == 255) { 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())); } diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index cd0a0f5ab..77dbac0b9 100644 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -9,8 +9,6 @@ extern "C" { #include "WiFiClient.h" #include "WiFiServer.h" - - WiFiServer::WiFiServer(uint16_t port) { _port = port; @@ -28,10 +26,8 @@ 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; + static int cycle_server_down = 0; + const int TH_SERVER_DOWN = 50; for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { @@ -40,21 +36,16 @@ WiFiClient WiFiServer::available(byte* status) WiFiClient client(sock); 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; //server not in listen state, restart it - if (this->status()==0) + if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) + { ServerDrv::startServer(_port, sock); + cycle_server_down = 0; + } if (_status == ESTABLISHED) { diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 089103cca..29d786f59 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -52,9 +52,13 @@ #define INFO(format, args...) do {} while(0); #endif +#if 0 #define WARN(args) do { PRINT_FILE_LINE() \ Serial.print("-W-"); Serial.println(args); \ }while (0); +#else +#define WARN(args) do {} while (0); +#endif #define DBG_PIN2 5 #define DBG_PIN 4 @@ -65,9 +69,9 @@ #define RST_TRIGGER() digitalWrite(DBG_PIN, LOW); #define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \ - pinMode(DBG_PIN2, OUTPUT); \ + pinMode(DBG_PIN2, OUTPUT); \ RST_TRIGGER() -#define TOGGLE_TRIGGER() SET_TRIGGER() \ +#define TOGGLE_TRIGGER() SET_TRIGGER() \ delayMicroseconds(2); \ RST_TRIGGER() diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 30bc5187c..7b5194089 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -3,8 +3,6 @@ #include "Arduino.h" #include "spi_drv.h" -#define _DEBUG_ - extern "C" { #include "wl_types.h" #include "debug.h" @@ -223,26 +221,41 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) } -uint8_t ServerDrv::isDataSent(uint8_t sock) +uint8_t ServerDrv::checkDataSent(uint8_t sock) { - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + const uint16_t TIMEOUT_DATA_SENT = 250; + static uint16_t timeout = 0; + uint8_t _data = 0; + uint8_t _dataLen = 0; - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); + do { + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse isDataSent"); - } - SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); - return _data; + // Wait for reply + if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse isDataSent"); + } + SpiDrv::spiSlaveDeselect(); + + if (_data) timeout = 0; + else{ + ++timeout; + if (timeout > TIMEOUT_DATA_SENT) + { + timeout = 0; + INFO1("Timeout wainting for data sent"); + } + } + }while((_data==0)&&(timeout