Skip to content

Commit

Permalink
#9
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizen8501 committed Mar 8, 2018
1 parent 082827f commit bd6c094
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
1 change: 1 addition & 0 deletions examples/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void loop()

// close the connection:
client.stop();
server.closeAllClientSocket();
Serial.println("Client disconnected");
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/WebServerAP/WebServerAP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void loop()

// close the connection
client.stop();
server.closeAllClientSocket();
Serial.println("Client disconnected");
}
}
Expand Down
19 changes: 4 additions & 15 deletions src/WizFi310Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,9 @@ int WiFiClient::read()
if (!available())
return -1;

bool connClose = false;
if( WizFi310Drv::getData(_sock, &b, false, &connClose) == false )
if( WizFi310Drv::getData(_sock, &b) == false )
return -1;

if (connClose)
{
WizFi310Drv::_state[_sock] = NA_STATE;
_sock = 255;
}

return b;
}

Expand All @@ -133,13 +126,8 @@ int WiFiClient::peek()
if (!available())
return -1;

bool connClose = false;
WizFi310Drv::getData(_sock, &b, true, &connClose);

if (connClose)
{
WizFi310Drv::_state[_sock] = NA_STATE;
_sock = 255;
while (available()){
WizFi310Drv::getData(_sock, &b);
}

return b;
Expand All @@ -157,6 +145,7 @@ void WiFiClient::stop()
return;

LOGINFO1(F("Disconnecting "), _sock);
peek();

WizFi310Drv::stopClient(_sock);

Expand Down
5 changes: 5 additions & 0 deletions src/WizFi310Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ uint8_t WiFiServer::getFirstSocket()
}
return SOCK_NOT_AVAIL;
}

uint8_t WiFiServer::closeAllClientSocket()
{
WizFi310Drv::closeAllClientSocket();
}
1 change: 1 addition & 0 deletions src/WizFi310Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class WiFiServer : public Server
uint8_t status();

uint8_t getFirstSocket();
uint8_t closeAllClientSocket();

using Print::write;

Expand Down
9 changes: 6 additions & 3 deletions src/WizFi310Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ int WiFiUDP::read()
if (!available())
return -1;

bool connClose = false;
WizFi310Drv::getData(_sock, &b, false, &connClose);
WizFi310Drv::getData(_sock, &b);

return b;
}
Expand All @@ -156,7 +155,11 @@ int WiFiUDP::peek()
{
uint8_t b;
if (!available())
return -1;
return -1;

while (available()){
WizFi310Drv::getData(_sock, &b);
}

return b;
}
Expand Down
53 changes: 43 additions & 10 deletions src/utility/WizFi310Drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void WizFi310Drv::stopClient(uint8_t sock)
return;

// Check socket
if (SendCmdWithTag("AT+SMGMT=?\r", "Number of Sockets : 0", "", 1000) == TAG_OK) {
if (SendCmdWithTag("AT+SMGMT=?\r", "Number of Sockets : 0", "[OK]\r\n", 1000) == TAG_OK) {
_state[sock] = NA_STATE;
m_esc_state = ESC_IDLE;
ringBuf.reset();
Expand Down Expand Up @@ -694,14 +694,14 @@ void WizFi310Drv::parsingData(uint8_t recv_data)
else m_esc_state = ESC_IDLE;
break;
case ESC_RECV_DATA:
// LOGDEBUG("ESC_RECV_DATA");
// if( ringBuf.available() >= (CMD_BUFFER_SIZE - 50) )
// {
// LOGDEBUG2("ringBuf threshold is over", (char)recv_data, ringBuf.available() );
// m_recved_len--;
// break;
// }

// For Debug
//LOGDEBUG("ESC_RECV_DATA");
//if( ringBuf.available() >= (CMD_BUFFER_SIZE - 50) )
//{
// LOGDEBUG2("ringBuf threshold is over", (char)recv_data, ringBuf.available() );
// m_recved_len--;
// break;
//}
ringBuf.write(recv_data);
m_recved_len--;

Expand Down Expand Up @@ -785,7 +785,7 @@ void WizFi310Drv::parsingData(uint8_t recv_data)
}
}

bool WizFi310Drv::getData(uint8_t connId, uint8_t *data, bool peek, bool* connClose)
bool WizFi310Drv::getData(uint8_t connId, uint8_t *data)
{
int ch;

Expand Down Expand Up @@ -966,6 +966,8 @@ int WizFi310Drv::SendCmdWithTag(const char* cmd, const char* tag, const char* ta

WizFi310Serial->print(cmd);
int idx = readUntil(timeout, tag, tag2);
// kaizen 20180308 For remove AT command response
ringBuf.reset();

LOGDEBUG1(F("---------------------------------------------- >"), idx);
LOGDEBUG();
Expand Down Expand Up @@ -1042,4 +1044,35 @@ void WizFi310Drv::wizfiEmptyBuf(bool warn)
}
}


void WizFi310Drv::closeAllClientSocket()
{
char buff[CMD_BUFFER_SIZE];
char *token;
int sock_cnt=0;
int server_sock[MAX_SOCK_NUM]= { NA_STATE, NA_STATE, NA_STATE, NA_STATE,
NA_STATE, NA_STATE, NA_STATE, NA_STATE };

sendCmd(F("AT+SMGMT=?\r"));
// Number of Sockets : 1 (SCID/Mode/Remote/Local/DataMode)
getResponse(buff,CMD_BUFFER_SIZE, 1);
token = strtok(buff, ":");
token = strtok(NULL, "(");
sock_cnt = atoi(token);

for(int i=0; i<sock_cnt; i++){
getResponse(buff,CMD_BUFFER_SIZE, 1);
if( strstr(buff,"TSN") > 0 ){
token = strtok(buff,"/");
server_sock[i] = atoi(token);
}
}

for(int i=0; i<sock_cnt; i++){
if(server_sock[i] == NA_STATE){
stopClient(i);
}
}
}

WizFi310Drv wizfi310Drv;
5 changes: 4 additions & 1 deletion src/utility/WizFi310Drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class WizFi310Drv
////////////////////////////////////////////////////////////////////////////
static uint16_t availData ();
static void parsingData(uint8_t recv_data);
static bool getData (uint8_t connId, uint8_t *data, bool peek, bool* connClose);
static bool getData (uint8_t connId, uint8_t *data);
static int getDataBuf (uint8_t connId, uint8_t *buf, uint16_t bufSize);
static bool sendData (uint8_t sock, const uint8_t *data, uint16_t len);
static bool sendData (uint8_t sock, const __FlashStringHelper *data, uint16_t len, bool appendCrLf=false);
Expand All @@ -137,6 +137,9 @@ class WizFi310Drv
static uint16_t getRemotePort();
static uint8_t getFirstSocket();

static void closeAllClientSocket();


static Stream *WizFi310Serial;

static long _bufPos;
Expand Down

0 comments on commit bd6c094

Please sign in to comment.