Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
Fork of WiflyInterface by
Revision 9:4f6f2f35a21a, committed 2013-06-05
- Comitter:
- leihen
- Date:
- Wed Jun 05 23:40:17 2013 +0000
- Parent:
- 8:ac134ae11893
- Child:
- 10:d84defb718ab
- Commit message:
- Improved 'Accept' handling.
; Added 'peek' functionality for CBuffer
Changed in this revision
--- a/Socket/TCPSocketConnection.cpp Sun Jun 02 00:26:25 2013 +0000 +++ b/Socket/TCPSocketConnection.cpp Wed Jun 05 23:40:17 2013 +0000 @@ -19,6 +19,7 @@ #include "TCPSocketConnection.h" #include <algorithm> + TCPSocketConnection::TCPSocketConnection() {} int TCPSocketConnection::connect(const char* host, const int port)
--- a/Socket/TCPSocketServer.cpp Sun Jun 02 00:26:25 2013 +0000 +++ b/Socket/TCPSocketServer.cpp Wed Jun 05 23:40:17 2013 +0000 @@ -19,7 +19,20 @@ #include "TCPSocketServer.h" #include <string> -TCPSocketServer::TCPSocketServer() {} +#define _DEBUG 0 + +#if (_DEBUG && !defined(TARGET_LPC11U24)) +#define INFO(x, ...) std::printf("[TCPSocketServer : INFO]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[TCPSocketServer : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[TCPSocketServer : ERR]"x"\r\n", ##__VA_ARGS__); +#else +#define INFO(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif + + +TCPSocketServer::TCPSocketServer() {connected = false;} // Server initialization int TCPSocketServer::bind(int port) { @@ -65,31 +78,44 @@ int TCPSocketServer::accept(TCPSocketConnection& connection) { - int nb_available = 0, pos = 0; char c; string str; bool o_find = false; Timer tm; while (1) { + INFO("Trying accept (%s)\n", (connected ? "Connected" : "Unconnected")); while(!wifi->readable()) { if (!_blocking && (tm.read_ms() > _timeout)) return 1; } - nb_available = wifi->readable(); - for (int i = 0; i < nb_available; i++) { - c = wifi->getc(); - if (c == '*') { - o_find = true; - } - if (o_find && c != '\r' && c != '\n') { + INFO("Data (\'%c\')\n", wifi->peek()); + if (!connected || (wifi->peek() == '*')) { + while (!o_find) { + c = wifi->getc(); + INFO("%c", c); str += c; - pos = str.find("*OPEN*"); - if (pos != string::npos) { - wifi->flush(); - return 0; + if (c == '*') { + if (str.find("*OPEN*") != string::npos) { + // connection found ! + INFO("Connection received !\n"); + connected = true; + return 0; + } + if (str.find("*CLOS*") != string::npos) { + // Connection closed ! + INFO("Connection closed !\n"); + str = ""; + connected = false; + break; + } } } } + else { + // We are still connected and there is new data, so just behave as if the accept was done. + INFO("Connection not yet closed, recycling because new data is available.\n"); + return 0; + } } } \ No newline at end of file
--- a/Socket/TCPSocketServer.h Sun Jun 02 00:26:25 2013 +0000 +++ b/Socket/TCPSocketServer.h Wed Jun 05 23:40:17 2013 +0000 @@ -47,6 +47,8 @@ \return 0 on success, -1 on failure. */ int accept(TCPSocketConnection& connection); + + bool connected; }; #endif
--- a/Wifly/CBuffer.h Sun Jun 02 00:26:25 2013 +0000 +++ b/Wifly/CBuffer.h Wed Jun 05 23:40:17 2013 +0000 @@ -51,6 +51,14 @@ write = 0; } + + bool peek(T * c) { + bool empty = isEmpty(); + if (!empty) { + *c = buf[read]; + } + return !empty; + } uint32_t available() { return (write >= read) ? write - read : size - read + write;
--- a/Wifly/Wifly.cpp Sun Jun 02 00:26:25 2013 +0000 +++ b/Wifly/Wifly.cpp Wed Jun 05 23:40:17 2013 +0000 @@ -64,7 +64,7 @@ } inst = this; - attach_rx(false); + attach_rx(false); state.cmd_mode = false; reset(); @@ -205,7 +205,7 @@ ERR("Failed to set time zone !"); } - sendCommand("time\r", NULL, NULL); + sendCommand("time\r", NULL, NULL, 1000); flush(); @@ -230,20 +230,23 @@ bool Wifly::setBaudRate(int baud) { char str[35]; - bool bfound = false; + bool bfound = true; INFO("Trying baudrates.\n"); sprintf(str, "set uart raw %d\r", baud); - for( int i = 0 ; i < sizeof(std_baudrates)/sizeof(int) ; i++) { - // try all standard baudrates until the correct one is found - INFO("Trying at %d baud\n", std_baudrates[i]); - wifi.baud(std_baudrates[i]); - if (sendCommand(str, "AOK")) { - bfound = true; - break; + + wifi.baud(baud); + if (!sendCommand(str, "AOK")) { + for( int i = 0 ; i < sizeof(std_baudrates)/sizeof(int) ; i++) { + // try all standard baudrates until the correct one is found + INFO("Trying at %d baud\n", std_baudrates[i]); + wifi.baud(std_baudrates[i]); + if (sendCommand(str, "AOK")) { + bfound = true; + break; + } } } - if (bfound) { wait(0.05); if (!sendCommand("save\r", "STOR")) @@ -461,7 +464,7 @@ wait(0.25); if (!sendCommand("close\r", "CLOS")) return false; - exit(); + exit(false); state.tcp = false; return true; @@ -475,15 +478,17 @@ } -bool Wifly::exit() +bool Wifly::exit(bool bflush) { - flush(); + if (bflush) + flush(); if (!state.cmd_mode) return true; if (!sendCommand("exit\r", "EXIT")) return false; state.cmd_mode = false; - flush(); + if (bflush) + flush(); return true; } @@ -506,6 +511,17 @@ return c; } + +char Wifly::peek() +{ + char c; + while (!buf_wifly.available()) + ; + buf_wifly.peek(&c); + return c; +} + + void Wifly::handler_rx(void) { //read characters @@ -536,7 +552,10 @@ //We flush the buffer while (wifi.readable()) - wifi.getc(); + { + char c = wifi.getc(); + INFO("Flushing : %c",c); + } if (!ACK || !strcmp(ACK, "NO")) { for (int i = 0; i < len; i++) @@ -544,7 +563,10 @@ } else { //We flush the buffer while (wifi.readable()) - wifi.getc(); + { + char c = wifi.getc(); + INFO("Flushing : %c",c); + } tmr.start(); for (int i = 0; i < len; i++)
--- a/Wifly/Wifly.h Sun Jun 02 00:26:25 2013 +0000 +++ b/Wifly/Wifly.h Wed Jun 05 23:40:17 2013 +0000 @@ -61,6 +61,7 @@ Wifly_921600 } WiflyBaudrate_t; + public: /* * Constructor @@ -146,6 +147,13 @@ char getc(); /* + * Peek a character + * + * @return the character but don't remove it from the queue + */ + char peek(); + + /* * Flush the buffer */ void flush(); @@ -170,7 +178,7 @@ * * @return true if successful, false otherwise */ - bool exit(); + bool exit(bool flush = true); /* * Close a tcp connection @@ -263,7 +271,6 @@ void attach_rx(bool null); void handler_rx(void); - typedef struct STATE { bool associated; bool tcp;