1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00

WiFi libraries: Bugfix on TestReport 12.01.2012

This commit is contained in:
Mimmo La Fauci 2012-02-04 12:29:12 +01:00
parent b61427ddc8
commit 00c36dc563
7 changed files with 106 additions and 23 deletions

View File

@ -11,6 +11,7 @@ extern "C" {
#include "WiFiServer.h" #include "WiFiServer.h"
#include "server_drv.h" #include "server_drv.h"
uint16_t WiFiClient::_srcport = 1024; uint16_t WiFiClient::_srcport = 1024;
WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) {
@ -41,7 +42,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
_sock = getFirstSocket(); _sock = getFirstSocket();
if (_sock != NO_SOCKET_AVAIL) if (_sock != NO_SOCKET_AVAIL)
{ {
ServerDrv::StartClient(uint32_t(ip), port, _sock); ServerDrv::startClient(uint32_t(ip), port, _sock);
WiFiClass::_state[_sock] = _sock; WiFiClass::_state[_sock] = _sock;
}else{ }else{
return 0; return 0;
@ -106,11 +107,13 @@ void WiFiClient::flush() {
} }
void WiFiClient::stop() { void WiFiClient::stop() {
INFO("1)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status());
if (_sock == 255) if (_sock == 255)
return; return;
// attempt to close the connection gracefully (send a FIN to other side) ServerDrv::stopClient(_sock);
disconnect(WiFiClass::_state[_sock]);
unsigned long start = millis(); unsigned long start = millis();
// wait a second for the connection to close // wait a second for the connection to close
@ -119,9 +122,13 @@ void WiFiClient::stop() {
// if it hasn't closed, close it forcefully // if it hasn't closed, close it forcefully
if (status() != CLOSED) 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; _sock = 255;
} }
@ -130,6 +137,7 @@ uint8_t WiFiClient::connected() {
return 0; return 0;
} else { } else {
uint8_t s = status(); uint8_t s = status();
INFO("Client status: %d", s);
return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 ||
(s == CLOSE_WAIT && !available())); (s == CLOSE_WAIT && !available()));
} }
@ -139,7 +147,7 @@ uint8_t WiFiClient::status() {
if (_sock == 255) { if (_sock == 255) {
return CLOSED; return CLOSED;
} else { } else {
return ServerDrv::getState(_sock); return ServerDrv::getClientState(_sock);
} }
} }

View File

@ -1,6 +1,10 @@
#include <string.h> #include <string.h>
#include "server_drv.h" #include "server_drv.h"
extern "C" {
#include "utility/debug.h"
}
#include "WiFi.h" #include "WiFi.h"
#include "WiFiClient.h" #include "WiFiClient.h"
#include "WiFiServer.h" #include "WiFiServer.h"
@ -17,7 +21,7 @@ void WiFiServer::begin()
uint8_t _sock = WiFiClass::getSocket(); uint8_t _sock = WiFiClass::getSocket();
if (_sock != NO_SOCKET_AVAIL) if (_sock != NO_SOCKET_AVAIL)
{ {
ServerDrv::StartServer(_port, _sock); ServerDrv::startServer(_port, _sock);
WiFiClass::_server_port[_sock] = _port; WiFiClass::_server_port[_sock] = _port;
} }
} }
@ -25,18 +29,34 @@ void WiFiServer::begin()
WiFiClient WiFiServer::available(byte* status) WiFiClient WiFiServer::available(byte* status)
{ {
//accept(); //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++) for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
{ {
if (WiFiClass::_server_port[sock] != 0) if (WiFiClass::_server_port[sock] == _port)
{ {
WiFiClient client(sock); 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) if (status != NULL)
*status = _status; *status = _status;
if (WiFiClass::_server_port[sock] == _port && //server not in listen state, restart it
_status == ESTABLISHED) if (this->status()==0)
ServerDrv::startServer(_port, sock);
if (_status == ESTABLISHED)
{ {
return client; //TODO return client; //TODO
} }
@ -46,6 +66,11 @@ WiFiClient WiFiServer::available(byte* status)
return WiFiClient(255); return WiFiClient(255);
} }
uint8_t WiFiServer::status() {
return ServerDrv::getServerState(0);
}
size_t WiFiServer::write(uint8_t b) size_t WiFiServer::write(uint8_t b)
{ {
return write(&b, 1); return write(&b, 1);

View File

@ -19,6 +19,7 @@ public:
void begin(); void begin();
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size); virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();
using Print::write; using Print::write;
}; };

View File

@ -30,6 +30,12 @@
#ifdef _DEBUG_ #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-");\ #define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\
Serial.println(x); \ Serial.println(x); \
}while (0); }while (0);
@ -39,11 +45,6 @@
Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ Serial.print(x,16);Serial.print(",");Serial.println(y,16); \
}while (0); }while (0);
#define INFO(format, args...) do { \
char buf[250]; \
sprintf(buf, format, args); \
Serial.println(buf); \
} while(0);
#else #else
#define INFO1(x) do {} while(0); #define INFO1(x) do {} while(0);

View File

@ -12,7 +12,7 @@ extern "C" {
// Start server TCP on port specified // 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(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
@ -34,7 +34,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock)
} }
// Start server TCP on port specified // 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(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
@ -56,7 +56,29 @@ void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock)
SpiDrv::spiSlaveDeselect(); 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(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
@ -77,6 +99,26 @@ uint8_t ServerDrv::getState(uint8_t sock)
return _data; 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) uint8_t ServerDrv::availData(uint8_t sock)
{ {

View File

@ -8,11 +8,15 @@ class ServerDrv
{ {
public: public:
// Start server TCP on port specified // 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 uint8_t getState(uint8_t sock); static void stopClient(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); static bool getData(uint8_t sock, uint8_t *data);

View File

@ -39,6 +39,8 @@ enum {
AVAIL_DATA_TCP_CMD = 0x2B, AVAIL_DATA_TCP_CMD = 0x2B,
GET_DATA_TCP_CMD = 0x2C, GET_DATA_TCP_CMD = 0x2C,
START_CLIENT_TCP_CMD= 0x2D, START_CLIENT_TCP_CMD= 0x2D,
STOP_CLIENT_TCP_CMD = 0x2E,
GET_CLIENT_STATE_TCP_CMD= 0x2F,
DISCONNECT_CMD = 0x30, DISCONNECT_CMD = 0x30,
// All command with DATA_FLAG 0x40 send a 16bit Len // All command with DATA_FLAG 0x40 send a 16bit Len