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 11:63a7dc9e45dd, committed 2013-06-26
- Comitter:
- leihen
- Date:
- Wed Jun 26 21:12:21 2013 +0000
- Parent:
- 10:d84defb718ab
- Child:
- 12:4f95e6a365db
- Commit message:
- Working Multithreaded HTTP Server Version
Changed in this revision
--- a/Socket/Endpoint.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#include "Socket/Socket.h" -#include "Socket/Endpoint.h" -#include <cstring> - -using std::memset; - -Endpoint::Endpoint() { - wifly = Wifly::getInstance(); - if (wifly == NULL) - error("Endpoint constructor error: no wifly instance available!\r\n"); - reset_address(); -} -Endpoint::~Endpoint() {} - -void Endpoint::reset_address(void) { - _ipAddress[0] = '\0'; - _port = 0; -} - -int Endpoint::set_address(const char* host, const int port) { - //Resolve DNS address or populate hard-coded IP address - wifly->gethostbyname(host, _ipAddress); - _port = port; - return 0; -} - -char* Endpoint::get_address() { - return _ipAddress; -} - -int Endpoint::get_port() { - return _port; -}
--- a/Socket/Endpoint.h Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef ENDPOINT_H -#define ENDPOINT_H - -#include "Wifly.h" - -class UDPSocket; - -/** -IP Endpoint (address, port) -*/ -class Endpoint { - friend class UDPSocket; - -public: - /** IP Endpoint (address, port) - */ - Endpoint(void); - - ~Endpoint(void); - - /** Reset the address of this endpoint - */ - void reset_address(void); - - /** Set the address of this endpoint - \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). - \param port The endpoint port - \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - int set_address(const char* host, const int port); - - /** Get the IP address of this endpoint - \return The IP address of this endpoint. - */ - char* get_address(void); - - /** Get the port of this endpoint - \return The port of this endpoint - */ - int get_port(void); - -protected: - char _ipAddress[16]; - int _port; - Wifly * wifly; -}; - -#endif
--- a/Socket/Socket.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "Socket.h" -#include <cstring> - -Socket::Socket() : _blocking(true), _timeout(1500) { - wifi = Wifly::getInstance(); - if (wifi == NULL) - error("Socket constructor error: no wifly instance available!\r\n"); -} - -void Socket::set_blocking(bool blocking, unsigned int timeout) { - _blocking = blocking; - _timeout = timeout; -} - -int Socket::close() { - return (wifi->close()) ? 0 : -1; -} - -Socket::~Socket() { - close(); //Don't want to leak -}
--- a/Socket/Socket.h Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef SOCKET_H_ -#define SOCKET_H_ - -#include "Wifly.h" - -/** Socket file descriptor and select wrapper - */ -class Socket { -public: - /** Socket - */ - Socket(); - - /** Set blocking or non-blocking mode of the socket and a timeout on - blocking socket operations - \param blocking true for blocking mode, false for non-blocking mode. - \param timeout timeout in ms [Default: (1500)ms]. - */ - void set_blocking(bool blocking, unsigned int timeout=1500); - - /** Close the socket file descriptor - */ - int close(); - - ~Socket(); - -protected: - bool _blocking; - int _timeout; - Wifly * wifi; -}; - - -#endif /* SOCKET_H_ */
--- a/Socket/TCPSocketConnection.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "TCPSocketConnection.h" -#include <algorithm> - - -TCPSocketConnection::TCPSocketConnection() {} - -int TCPSocketConnection::connect(const char* host, const int port) -{ - if (!wifi->connect(host, port)) - return -1; - wifi->flush(); - return 0; -} - -bool TCPSocketConnection::is_connected(void) -{ - return wifi->is_connected(); -} - -int TCPSocketConnection::send(char* data, int length) -{ - Timer tmr; - - if (!_blocking) { - tmr.start(); - while (tmr.read_ms() < _timeout) { - if (wifi->writeable()) - break; - } - if (tmr.read_ms() >= _timeout) { - return -1; - } - } - return wifi->send(data, length); -} - -// -1 if unsuccessful, else number of bytes written -int TCPSocketConnection::send_all(char* data, int length) -{ - Timer tmr; - int idx = 0; - tmr.start(); - - while ((tmr.read_ms() < _timeout) || _blocking) { - - idx += wifi->send(data, length); - - if (idx == length) - return idx; - } - return (idx == 0) ? -1 : idx; -} - -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive(char* data, int length) -{ - Timer tmr; - int time = -1; - - - if (!_blocking) { - tmr.start(); - while (time < _timeout + 20) { - if (wifi->readable()) { - break; - } - time = tmr.read_ms(); - } - if (time >= _timeout + 20) { - return -1; - } - } - - - while(!wifi->readable()); - int nb_available = wifi->readable(); - for (int i = 0; i < min(nb_available, length); i++) { - data[i] = wifi->getc(); - } - - return min(nb_available, length); -} - - -// -1 if unsuccessful, else number of bytes received -int TCPSocketConnection::receive_all(char* data, int length) -{ - Timer tmr; - int idx = 0; - int time = -1; - - tmr.start(); - - while (time < _timeout || _blocking) { - - int nb_available = wifi->readable(); - for (int i = 0; i < min(nb_available, length); i++) { - data[idx++] = wifi->getc(); - } - - if (idx == length) - break; - - time = tmr.read_ms(); - } - - return (idx == 0) ? -1 : idx; -}
--- a/Socket/TCPSocketConnection.h Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef TCPSOCKET_H -#define TCPSOCKET_H - -#include "Socket.h" -#include "Endpoint.h" - -/** -TCP socket connection -*/ -class TCPSocketConnection: public Socket, public Endpoint { - -public: - /** TCP socket connection - */ - TCPSocketConnection(); - - /** Connects this TCP socket to the server - \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. - \param port The host's port to connect to. - \return 0 on success, -1 on failure. - */ - int connect(const char* host, const int port); - - /** Check if the socket is connected - \return true if connected, false otherwise. - */ - bool is_connected(void); - - /** Send data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send(char* data, int length); - - /** Send all the data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send_all(char* data, int length); - - /** Receive data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive(char* data, int length); - - /** Receive all the data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive_all(char* data, int length); -}; - -#endif
--- a/Socket/TCPSocketServer.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "TCPSocketServer.h" -#include <string> - -#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) { - char cmd[20]; - - // set TCP protocol - wifi->setProtocol(TCP); - - // set local port - sprintf(cmd, "set i l %d\r", port); - if (!wifi->sendCommand(cmd, "AOK")) - return -1; - - // save - if (!wifi->sendCommand("save\r", "Stor")) - return -1; - - // reboot - wifi->reboot(); - - // connect the network - if (wifi->isDHCP()) { - if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000)) - return -1; - } else { - if (!wifi->sendCommand("join\r", "Associated", NULL, 10000)) - return -1; - } - - // exit - wifi->exit(); - - wait(0.2); - wifi->flush(); - return 0; -} - -int TCPSocketServer::listen(int backlog) { - if (backlog != 1) - return -1; - return 0; -} - - -int TCPSocketServer::accept(TCPSocketConnection& connection) { - 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; - } - INFO("Data (\'%c\')\n", wifi->peek()); - if (!connected || (wifi->peek() == '*')) { - while (!o_find) { - c = wifi->getc(); - INFO("%c", c); - str += c; - 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 Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef TCPSOCKETSERVER_H -#define TCPSOCKETSERVER_H - -#include "Socket/Socket.h" -#include "TCPSocketConnection.h" - -/** TCP Server. - */ -class TCPSocketServer : public Socket { - public: - /** Instantiate a TCP Server. - */ - TCPSocketServer(); - - /** Bind a socket to a specific port. - \param port The port to listen for incoming connections on. - \return 0 on success, -1 on failure. - */ - int bind(int port); - - /** Start listening for incoming connections. - \param backlog number of pending connections that can be queued up at any - one time [Default: 1]. - \return 0 on success, -1 on failure. - */ - int listen(int backlog=1); - - /** Accept a new connection. - \param connection A TCPSocketConnection instance that will handle the incoming connection. - \return 0 on success, -1 on failure. - */ - int accept(TCPSocketConnection& connection); - - bool connected; -}; - -#endif
--- a/Socket/UDPSocket.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "UDPSocket.h" - -#include <string> -#include <algorithm> - -UDPSocket::UDPSocket() -{ - endpoint_configured = false; - endpoint_read = false; -} - -int UDPSocket::init(void) -{ - wifi->setProtocol(UDP); - wifi->exit(); - return 0; -} - -// Server initialization -int UDPSocket::bind(int port) -{ - char cmd[17]; - - // set local port - sprintf(cmd, "set i l %d\r", port); - if (!wifi->sendCommand(cmd, "AOK")) - return -1; - - // save - if (!wifi->sendCommand("save\r", "Stor")) - return -1; - - // reboot - wifi->reboot(); - - // set udp protocol - wifi->setProtocol(UDP); - - // connect the network - if (wifi->isDHCP()) { - if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000)) - return -1; - } else { - if (!wifi->sendCommand("join\r", "Associated", NULL, 10000)) - return -1; - } - - // exit - wifi->exit(); - wifi->flush(); - return 0; -} - -// -1 if unsuccessful, else number of bytes written -int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) -{ - Timer tmr; - int idx = 0; - - confEndpoint(remote); - - tmr.start(); - - while ((tmr.read_ms() < _timeout) || _blocking) { - - idx += wifi->send(packet, length); - - if (idx == length) - return idx; - } - return (idx == 0) ? -1 : idx; -} - -// -1 if unsuccessful, else number of bytes received -int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) -{ - Timer tmr; - int idx = 0; - int nb_available = 0; - int time = -1; - - if (_blocking) { - while (1) { - nb_available = wifi->readable(); - if (nb_available != 0) { - break; - } - } - } - - tmr.start(); - - while (time < _timeout) { - - nb_available = wifi->readable(); - for (int i = 0; i < min(nb_available, length); i++) { - buffer[idx] = wifi->getc(); - idx++; - } - - if (idx == length) { - break; - } - - time = tmr.read_ms(); - } - - readEndpoint(remote); - return (idx == 0) ? -1 : idx; -} - -bool UDPSocket::confEndpoint(Endpoint & ep) -{ - char * host; - char cmd[30]; - if (!endpoint_configured) { - host = ep.get_address(); - if (host[0] != '\0') { - // set host - sprintf(cmd, "set i h %s\r", host); - if (!wifi->sendCommand(cmd, "AOK")) - return false; - - // set remote port - sprintf(cmd, "set i r %d\r", ep.get_port()); - if (!wifi->sendCommand(cmd, "AOK")) - return false; - - wifi->exit(); - endpoint_configured = true; - return true; - } - } - return true; -} - -bool UDPSocket::readEndpoint(Endpoint & ep) -{ - char recv[256]; - int begin = 0; - int end = 0; - string str; - string addr; - int port; - if (!endpoint_read) { - if (!wifi->sendCommand("get ip\r", NULL, recv)) - return false; - wifi->exit(); - str = recv; - begin = str.find("HOST="); - end = str.find("PROTO="); - if (begin != string::npos && end != string::npos) { - str = str.substr(begin + 5, end - begin - 5); - int pos = str.find(":"); - if (pos != string::npos) { - addr = str.substr(0, pos); - port = atoi(str.substr(pos + 1).c_str()); - ep.set_address(addr.c_str(), port); - endpoint_read = true; - wifi->flush(); - return true; - } - } - wifi->flush(); - } - return false; -}
--- a/Socket/UDPSocket.h Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef UDPSOCKET_H -#define UDPSOCKET_H - -#include "Endpoint.h" -#include "Socket.h" - -#include <cstdint> - -/** -UDP Socket -*/ -class UDPSocket: public Socket { - -public: - /** Instantiate an UDP Socket. - */ - UDPSocket(); - - /** Init the UDP Client Socket without binding it to any specific port - \return 0 on success, -1 on failure. - */ - int init(void); - - /** Bind a UDP Server Socket to a specific port - \param port The port to listen for incoming connections on - \return 0 on success, -1 on failure. - */ - int bind(int port = -1); - - /** Send a packet to a remote endpoint - \param remote The remote endpoint - \param packet The packet to be sent - \param length The length of the packet to be sent - \return the number of written bytes on success (>=0) or -1 on failure - */ - int sendTo(Endpoint &remote, char *packet, int length); - - /** Receive a packet from a remote endpoint - \param remote The remote endpoint - \param buffer The buffer for storing the incoming packet data. If a packet - is too long to fit in the supplied buffer, excess bytes are discarded - \param length The length of the buffer - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receiveFrom(Endpoint &remote, char *buffer, int length); - -private: - bool confEndpoint(Endpoint & ep); - bool readEndpoint(Endpoint & ep); - bool endpoint_configured; - bool endpoint_read; - -}; - -#include "def.h" - -#endif
--- a/Wifly/Wifly.cpp Sat Jun 08 07:07:05 2013 +0000 +++ b/Wifly/Wifly.cpp Wed Jun 26 21:12:21 2013 +0000 @@ -21,20 +21,18 @@ #include <string> #include <algorithm> +DigitalOut ledrx(LED3); + //Debug is disabled by default -#if (0 && !defined(TARGET_LPC11U24)) +#if (1 && !defined(TARGET_LPC11U24)) #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__); #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__); +#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__); #else #define DBG(x, ...) #define WARN(x, ...) #define ERR(x, ...) -#endif - -#if !defined(TARGET_LPC11U24) -#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__); -#else #define INFO(x, ...) #endif @@ -83,7 +81,7 @@ continue; //no echo - if (!sendCommand("set u m 1\r", "AOK")) + if (!sendCommand("set u m 17\r", "AOK")) continue; // set time @@ -458,10 +456,10 @@ bool Wifly::close() { // if not connected, return - if (!state.tcp) - return true; +// if (!state.tcp) +// return true; - wait(0.25); +// Thread::wait(250); if (!sendCommand("close\r", "CLOS")) return false; exit(false); @@ -527,6 +525,7 @@ while (wifi.readable()) { char c = LPC_UART3->RBR; buf_wifly.queue(c); + ledrx = !ledrx; } } @@ -538,7 +537,6 @@ wifi.attach(this, &Wifly::handler_rx); } - int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout) { char read; @@ -576,8 +574,10 @@ while (1) { if (tmr.read_ms() > timeout) { //We flush the buffer - while (wifi.readable()) - wifi.getc(); + while (wifi.readable()) { + char c = wifi.getc(); + INFO("Flushing : %c",c); + } DBG("check: %s\r\n", checking.c_str()); @@ -591,8 +591,10 @@ if (found != string::npos) { wait(0.01); //We flush the buffer - while (wifi.readable()) - wifi.getc(); + while (wifi.readable()) { + char c = wifi.getc(); + INFO("Flushing : %c",c); + } break; } @@ -643,10 +645,32 @@ } //We flush the buffer - while (wifi.readable()) - wifi.getc(); + while (wifi.readable()) { + char c = wifi.getc(); + INFO("Flushing : %c",c); + } attach_rx(true); DBG("result: %d\r\n", result) return result; +} + + +int Wifly::sendData(const char* data, int len, int _timeout) +{ + int result = 0; + Timer tmr; + + tmr.start(); + while (tmr.read_ms() < _timeout) { + if (wifi.writeable()) + break; + } + if (tmr.read_ms() >= _timeout) { + return -1; + } + for (int i = 0; i < len; i++) + result = (putc(data[i]) == data[i]) ? result + 1 : result; + + return result; } \ No newline at end of file
--- a/Wifly/Wifly.h Sat Jun 08 07:07:05 2013 +0000 +++ b/Wifly/Wifly.h Wed Jun 26 21:12:21 2013 +0000 @@ -30,9 +30,11 @@ #include "mbed.h" #include "CBuffer.h" #include <string> +#include "rtos.h" #define DEFAULT_WAIT_RESP_TIMEOUT 800 + enum Security { NONE = 0, WEP_128 = 1, @@ -81,7 +83,7 @@ * * @return true if connected, false otherwise */ - bool join(); + virtual bool join(); /* * Disconnect the wifly module from the access point @@ -199,8 +201,10 @@ * * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. */ - int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); + virtual int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); + virtual int sendData(const char* str, int len, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); + /* * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode * @@ -268,8 +272,8 @@ static Wifly * inst; - void attach_rx(bool null); - void handler_rx(void); + virtual void attach_rx(bool null) = 0 ; + virtual void handler_rx(void) = 0; typedef struct STATE { bool associated;
--- a/WiflyInterface.cpp Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#include "WiflyInterface.h" - -WiflyInterface::WiflyInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status, - const char * ssid, const char * phrase, Security sec) : - Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec) -{ - ip_set = false; -} - -int WiflyInterface::init() -{ - state.dhcp = true; - reset(); - return 0; -} - -int WiflyInterface::init(const char* ip, const char* mask, const char* gateway) -{ - state.dhcp = false; - this->ip = ip; - strcpy(ip_string, ip); - ip_set = true; - this->netmask = mask; - this->gateway = gateway; - reset(); - - return 0; -} - -int WiflyInterface::connect() -{ - int retval = join(); - return retval; -} - -int WiflyInterface::disconnect() -{ - return Wifly::disconnect(); -} - -char * WiflyInterface::getIPAddress() -{ - char * match = 0; - if (!ip_set) { - if (!sendCommand("get ip a\r", NULL, ip_string)) - return NULL; - exit(); - flush(); - match = strstr(ip_string, "<"); - if (match != NULL) { - *match = '\0'; - } - if (strlen(ip_string) < 6) { - match = strstr(ip_string, ">"); - if (match != NULL) { - int len = strlen(match + 1); - memcpy(ip_string, match + 1, len); - } - } - ip_set = true; - } - return ip_string; -} \ No newline at end of file
--- a/WiflyInterface.h Sat Jun 08 07:07:05 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,87 +0,0 @@ -/* WiflyInterface.h */ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef WIFLYINTERFACE_H_ -#define WIFLYINTERFACE_H_ - -#include "Wifly.h" -#include <string> - - /** Interface using Wifly to connect to an IP-based network - * - */ -class WiflyInterface: public Wifly { -public: - - /** - * Constructor - * - * \param tx mbed pin to use for tx line of Serial interface - * \param rx mbed pin to use for rx line of Serial interface - * \param reset reset pin of the wifi module () - * \param tcp_status connection status pin of the wifi module (GPIO 6) - * \param ssid ssid of the network - * \param phrase WEP or WPA key - * \param sec Security type (NONE, WEP_128 or WPA) - */ - WiflyInterface(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec = NONE); - - /** Initialize the interface with DHCP. - * Initialize the interface and configure it to use DHCP (no connection at this point). - * \return 0 on success, a negative number on failure - */ - int init(); //With DHCP - - /** Initialize the interface with a static IP address. - * Initialize the interface and configure it with the following static configuration (no connection at this point). - * \param ip the IP address to use - * \param mask the IP address mask - * \param gateway the gateway to use - * \return 0 on success, a negative number on failure - */ - int init(const char* ip, const char* mask, const char* gateway); - - /** Connect - * Bring the interface up, start DHCP if needed. - * \return 0 on success, a negative number on failure - */ - int connect(); - - /** Disconnect - * Bring the interface down - * \return 0 on success, a negative number on failure - */ - int disconnect(); - - /** Get IP address - * - * @ returns ip address - */ - char* getIPAddress(); - -private: - char ip_string[20]; - bool ip_set; -}; - -#include "TCPSocketConnection.h" -#include "TCPSocketServer.h" -#include "UDPSocket.h" - -#endif /* WIFLYINTERFACE_H_ */