ACTUALIZED LIBRARY 0 Open (Default) 1 WEP-128 2 WPA1 3 Mixed WPA1 & WPA2-PSK 4 WPA2-PSK 5 Not Used 6 Adhoc, Join any Adhoc network

Dependents:   mbed_Arduino_sensor

Fork of WiflyInterface by Samuel Mokrani

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Fri Aug 17 08:28:04 2012 +0000
Child:
1:8f04181f9ad8
Commit message:
first commit

Changed in this revision

Helper/def.h Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
Wifly/CBuffer.h Show annotated file Show diff for this revision Revisions of this file
Wifly/Wifly.cpp Show annotated file Show diff for this revision Revisions of this file
Wifly/Wifly.h Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.cpp Show annotated file Show diff for this revision Revisions of this file
WiflyInterface.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Helper/def.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,28 @@
+/* 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 DEF_H
+#define DEF_H
+
+#include "cmsis.h"
+#define htons(x)      __REV16(x)
+#define ntohs(x)      __REV16(x)
+#define htonl(x)      __REV(x)
+#define ntohl(x)      __REV(x)
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,53 @@
+/* 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()  {
+    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
+    /*struct hostent *server = ::gethostbyname(host);
+    if (server == NULL)
+        return -1; //Could not resolve address*/
+    
+    reset_address();
+    std::memcpy(_ipAddress, host, strlen(host) + 1);
+    _port = port;
+    
+    return 0;
+}
+
+char* Endpoint::get_address() {
+    return _ipAddress;
+}
+
+int   Endpoint::get_port() {
+    return _port;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,64 @@
+/* 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
+
+
+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;
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,38 @@
+/* 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();
+}
+
+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
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,51 @@
+/* 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;
+    unsigned int _timeout;
+    Wifly * wifi;
+};
+
+
+#endif /* SOCKET_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,139 @@
+/* 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) {
+    //open the connection
+    char cmd[30];
+    char rcv[40];
+
+    // if we cannot enter in cmd mode, maybe we have a tcp connection opened.
+    // in this case we need to wait at least 0.25s to enter in cmd mode
+    if (!wifi->cmdMode()) {
+        wait(0.25);
+        if (!wifi->cmdMode()) {
+            wifi->send("\r", 1);
+            wait(0.1);
+            wifi->exit();
+            return -1;
+        }
+    }
+
+    sprintf(cmd, "open %s %d\r", host, port);
+    if (wifi->sendCommand(cmd, NULL, rcv)) {
+        if (strstr(rcv, "OPEN") == NULL) {
+            if (strstr(rcv, "Connected") != NULL) {
+                if (!wifi->sendCommand("close\r", "CLOS"))
+                    return -1;
+                if (!wifi->sendCommand(cmd, "OPEN"))
+                    return -1;
+            } else {
+                return -1;
+            }
+        }
+    } else {
+        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;
+
+    if (!_blocking) {
+        tmr.start();
+        while (tmr.read_ms() < _timeout) {
+            if (wifi->readable())
+                break;
+        }
+        if (tmr.read_ms() >= _timeout)
+            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;
+    
+    tmr.start();
+
+    while (tmr.read_ms() < _timeout || _blocking) {
+    
+        int nb_available = wifi->readable();
+        for (int i = 0; i < min(nb_available, length); i++) {
+            data[idx++] = wifi->getc();
+        }
+        
+        if (idx == length)
+            return idx;
+    }
+
+    return (idx == 0) ? -1 : idx;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,76 @@
+/* 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,72 @@
+/* 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>
+
+TCPSocketServer::TCPSocketServer() {}
+
+// Server initialization
+int TCPSocketServer::bind(int port) {
+    // use udp auto pairing
+    char cmd[20];
+    if (!wifi->sendCommand("set ip proto 2\r", "AOK"))
+        return -1;
+    if (!wifi->sendCommand("set ip flags 0x07\r", "AOK"))
+        return -1;
+    sprintf(cmd, "set ip local %d\r", port);
+    if (!wifi->sendCommand(cmd, "AOK"))
+        return -1;
+    if (!wifi->sendCommand("save\r", "Stor"))
+        return -1;
+
+    wifi->reboot();
+    return 0;
+}
+
+int TCPSocketServer::listen(int backlog) {
+    if (backlog != 1)
+        return -1;
+    return 0;
+}
+
+
+int TCPSocketServer::accept(TCPSocketConnection& connection) {
+    int nb_available = 0, pos = 0;
+    char c;
+    string str;
+    bool o_find = false;
+    while (1) {
+        while(!wifi->readable());
+        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') {
+                str += c;
+                pos = str.find("*OPEN*");
+                if (pos != string::npos) {
+                    wifi->flush();
+                    return 0;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,52 @@
+/* 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);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,160 @@
+/* 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) {
+    if (!wifi->sendCommand("set ip proto 1\r", "AOK"))
+        return -1;
+    wifi->exit();
+    return 0;
+}
+
+// Server initialization
+int UDPSocket::bind(int port) {
+    // use udp auto pairing
+    char cmd[20];
+    if (!wifi->sendCommand("set ip proto 1\r", "AOK"))
+        return -1;
+    if (!wifi->sendCommand("set ip flags 0x40\r", "AOK"))
+        return -1;
+    if (!wifi->sendCommand("set ip host 0.0.0.0\r", "AOK"))
+        return -1;
+    if (!wifi->sendCommand("set ip remote 0\r", "AOK"))
+        return -1;
+    sprintf(cmd, "set ip local %d\r", port);
+    if (!wifi->sendCommand(cmd, "AOK"))
+        return -1;
+    if (!wifi->sendCommand("save\r", "Stor"))
+        return -1;
+
+    wifi->reboot();
+    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;
+
+
+    if (_blocking) {
+        while (1) {
+            nb_available = wifi->readable();
+            if (nb_available != 0) {
+                break;
+            }
+        }
+    }
+    
+    tmr.start();
+
+    while (tmr.read_ms() < _timeout) {
+    
+        nb_available = wifi->readable();
+        for (int i = 0; i < min(nb_available, length); i++) {
+            buffer[idx++] = wifi->getc();
+        }
+        
+        if (idx == length)
+            break;
+    }
+
+    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') {
+            sprintf(cmd, "set ip host %s\r", host);
+            if (!wifi->sendCommand(cmd, "AOK"))
+                return false;
+            sprintf(cmd, "set ip remote %d\r", ep.get_port());
+            if (!wifi->sendCommand(cmd, "AOK"))
+                return false;
+            if (!wifi->sendCommand("save\r", "Stor"))
+                return false;
+            wifi->reboot();
+            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;
+                return true;
+            }
+        }
+    }
+    return false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/UDPSocket.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,75 @@
+/* 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wifly/CBuffer.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,75 @@
+/* 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 CIRCBUFFER_H_
+#define CIRCBUFFER_H_
+
+template <class T>
+class CircBuffer {
+public:
+    CircBuffer(int length) {
+        write = 0;
+        read = 0;
+        size = length + 1;
+        buf = (T *)malloc(size * sizeof(T));
+    };
+
+    bool isFull() {
+        return (((write + 1) % size) == read);
+    };
+
+    bool isEmpty() {
+        return (read == write);
+    };
+
+    void queue(T k) {
+        if (isFull()) {
+            read++;
+            read %= size;
+        }
+        buf[write++] = k;
+        write %= size;
+    }
+    
+    void flush() {
+        read = 0;
+        write = 0;
+    }
+    
+
+    uint32_t available() {
+        return (write >= read) ? write - read : size - read + write;
+    };
+
+    bool dequeue(T * c) {
+        bool empty = isEmpty();
+        if (!empty) {
+            *c = buf[read++];
+            read %= size;
+        }
+        return(!empty);
+    };
+
+private:
+    volatile uint32_t write;
+    volatile uint32_t read;
+    uint32_t size;
+    T * buf;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wifly/Wifly.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,394 @@
+/* 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 "mbed.h"
+#include "Wifly.h"
+#include <string>
+
+//Debug is disabled by default
+#if 0
+#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__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+#define INFO(x, ...) printf("[Wifly : INFO]"x"\r\n", ##__VA_ARGS__);
+
+Wifly * Wifly::inst;
+
+Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa):
+        wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(256) {
+    this->wpa = wpa;
+    this->phrase = phrase;
+    this->ssid = ssid;
+    inst = this;
+    attach_rx(false);
+    cmd_mode = false;
+}
+
+bool Wifly::join() {
+    char cmd[100];
+
+    if (!sendCommand("set comm time 20\r", "AOK"))
+        return false;
+
+    if (!sendCommand("set comm size 128\r", "AOK"))
+        return false;
+
+    if (!sendCommand("set sys iofunc 0x40\r", "AOK"))
+        return false;
+
+    // no string sent to the tcp client
+    if (!sendCommand("set comm remote 0\r", "AOK"))
+        return false;
+
+    // tcp protocol
+    if (!sendCommand("set ip proto 2\r", "AOK"))
+        return false;
+
+    // tcp retry
+    if (!sendCommand("set ip flags 0x7\r", "AOK"))
+        return false;
+
+    //no echo
+    if (!sendCommand("set u m 1\r", "AOK"))
+        return false;
+        
+    //dhcp
+    sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0);
+    if (!sendCommand(cmd, "AOK"))
+        return false;
+
+    if (dhcp) {
+        if (!sendCommand("get w\r", NULL, cmd))
+            return false;
+
+        // if the wlan parameters are already known, we just connect without resending all commands
+        if ((string(cmd).find(ssid) != string::npos) && (string(cmd).find(phrase) != string::npos) ) {
+            DBG("ssid found && phrase found\r\n");
+            Timer tmr;
+            tmr.start();
+            while (tmr.read() < 2) {
+                send("show c\r", 7, NULL, cmd);
+                DBG("join: state of conn: %s\r\n", cmd);
+                if ((cmd[2] - '0') & 0x01) {
+                    INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP");
+                    // save
+                    if (!sendCommand("save\r", "Stor"))
+                        return false;
+                    reboot();
+                    return true;
+                }
+                wait(0.2);
+            }
+        }
+
+        DBG("getw: %s\r\n", cmd);
+    }
+
+    // ssid
+    sprintf(cmd, "set w s %s\r", ssid);
+    if (!sendCommand(cmd, "AOK"))
+        return false;
+
+    //auth
+    sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1);
+    if (!sendCommand(cmd, "AOK"))
+        return false;
+
+    // if no dhcp, set ip, netmask and gateway
+    if (!dhcp) {
+        DBG("not dhcp\r");
+
+        sprintf(cmd, "set i a %s\r\n", ip);
+        if (!sendCommand(cmd, "AOK"))
+            return false;
+
+        sprintf(cmd, "set i n %s\r", netmask);
+        if (!sendCommand(cmd, "AOK"))
+            return false;
+
+        sprintf(cmd, "set i g %s\r", gateway);
+        if (!sendCommand(cmd, "AOK"))
+            return false;
+    }
+
+    //key step
+    if (wpa)
+        sprintf(cmd, "set w p %s\r", phrase);
+    else
+        sprintf(cmd, "set w k %s\r", phrase);
+
+    if (!sendCommand(cmd, "AOK"))
+        return false;
+
+    // auto join
+    if (!sendCommand("set w j 1\r", "AOK"))
+        return false;
+
+    // save
+    if (!sendCommand("save\r", "Stor"))
+        return false;
+
+    //join the network
+    sprintf(cmd, "join %s\r", ssid);
+    if (!sendCommand(cmd, "Associated"))
+        return false;
+
+    reboot();
+
+    INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP");
+    return true;
+}
+
+bool Wifly::reboot() {
+    if (!sendCommand("reboot\r", "boot"))
+        return false;
+    wait(0.2);
+    // wait that the module fetches an IP
+    if (dhcp) {
+        while(send("", 0, "DHCP=ON") == -1);
+    } else {
+        while(send("", 0, "Static") == -1);
+    }
+    flush();
+    cmd_mode = false;
+    return true;
+}
+
+
+void Wifly::flush() {
+    buf_wifly.flush();
+}
+
+bool Wifly::sendCommand(const char * cmd, const char * ack, char * res) {
+    if (!cmd_mode) {
+        cmdMode();
+    }
+    if (send(cmd, strlen(cmd), ack, res) == -1) {
+        ERR("sendCommand: cannot %s\r\n", cmd);
+        exit();
+        return false;
+    }
+    return true;
+}
+
+bool Wifly::cmdMode() {
+    if (send("$$$", 3, "CMD") == -1) {
+        ERR("cannot enter in cmd mode\r\n");
+        return false;
+    }
+    cmd_mode = true;
+    return true;
+}
+
+bool Wifly::leave() {
+    if (!sendCommand("leave\r", "DeAuth"))
+        return false;
+    exit();
+    return true;
+
+}
+
+bool Wifly::is_connected() {
+    return (tcp_status.read() ==  1) ? true : false;
+}
+
+
+void Wifly::reset() {
+    reset_pin = 0;
+    wait(0.2);
+    reset_pin = 1;
+    wait(0.2);
+}
+
+bool Wifly::close() {
+    wait(0.25);
+    if (!cmdMode())
+        return false;
+    if (send("close\r", 6, "CLOS") == -1)
+        return false;
+    exit();
+    return true;
+}
+
+
+int Wifly::putc(char c) {
+    while (!wifi.writeable());
+    return wifi.putc(c);
+}
+
+
+bool Wifly::read(char * str) {
+    int length = buf_wifly.available();
+    if (length == 0)
+        return false;
+    for (int i = 0; i < length; i++)
+        buf_wifly.dequeue(&str[i]);
+    str[length] = 0;
+    return true;
+}
+
+
+bool Wifly::exit() {
+    flush();
+    if (send("exit\r", 5, "EXIT") == -1)
+        return false;
+    cmd_mode = false;
+    return true;
+}
+
+
+int Wifly::readable() {
+    return buf_wifly.available();
+}
+
+int Wifly::writeable() {
+    return wifi.writeable();
+}
+
+char Wifly::getc() {
+    char c;
+    while (!buf_wifly.available());
+    buf_wifly.dequeue(&c);
+    return c;
+}
+
+void Wifly::handler_rx(void) {
+    //read characters
+    while (wifi.readable())
+        buf_wifly.queue(wifi.getc());
+}
+
+void Wifly::attach_rx(bool callback) {
+    if (!callback)
+        wifi.attach(NULL);
+    else
+        wifi.attach(this, &Wifly::handler_rx);
+}
+
+
+int Wifly::send(const char * str, int len, const char * ACK, char * res) {
+    char read;
+    size_t found = string::npos;
+    string checking;
+    Timer tmr;
+    int result = 0;
+
+    DBG("will send: %s\r\n",str);
+
+    attach_rx(false);
+
+    //We flush the buffer
+    while (wifi.readable())
+        wifi.getc();
+
+    if (!ACK || !strcmp(ACK, "NO")) {
+        for (int i = 0; i < len; i++)
+            result = (putc(str[i]) == str[i]) ? result + 1 : result;
+    } else {
+        //We flush the buffer
+        while (wifi.readable())
+            wifi.getc();
+
+        tmr.start();
+        for (int i = 0; i < len; i++)
+            result = (putc(str[i]) == str[i]) ? result + 1 : result;
+
+        while (1) {
+            if (tmr.read() > 3) {
+                //We flush the buffer
+                while (wifi.readable())
+                    wifi.getc();
+
+                DBG("check: %s\r\n", checking.c_str());
+
+                attach_rx(true);
+                return -1;
+            } else if (wifi.readable()) {
+                read = wifi.getc();
+                if ( read != '\r' && read != '\n') {
+                    checking += read;
+                    found = checking.find(ACK);
+                    if (found != string::npos) {
+                        wait(0.01);
+
+                        //We flush the buffer
+                        while (wifi.readable())
+                            wifi.getc();
+
+                        break;
+                    }
+                }
+            }
+        }
+        DBG("check: %s\r\n", checking.c_str());
+
+        attach_rx(true);
+        return result;
+    }
+
+    //the user wants the result from the command (ACK == NULL, res != NULL)
+    if ( res != NULL) {
+        int i = 0;
+        Timer timeout;
+        timeout.start();
+        tmr.reset();
+        while (1) {
+            if (timeout.read() > 3) {
+                if (i == 0) {
+                    res = NULL;
+                    break;
+                }
+                res[i] = '\0';
+                DBG("user str 1: %s\r\n", res);
+
+                break;
+            } else {
+                if (tmr.read_ms() > 1000) {
+                    res[i] = '\0';
+                    DBG("user str: %s\r\n", res);
+
+                    break;
+                }
+                if (wifi.readable()) {
+                    tmr.start();
+                    read = wifi.getc();
+
+                    // we drop \r and \n
+                    if ( read != '\r' && read != '\n') {
+                        res[i++] = read;
+                    }
+                }
+            }
+        }
+        DBG("user str: %s\r\n", res);
+    }
+
+    //We flush the buffer
+    while (wifi.readable())
+        wifi.getc();
+
+    attach_rx(true);
+    DBG("result: %d\r\n", result)
+    return result;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Wifly/Wifly.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,193 @@
+/* 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.
+ *
+ * @section DESCRIPTION
+ *
+ * Wifly RN131-C, wifi module
+ *
+ * Datasheet:
+ *
+ * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
+ */
+
+#ifndef WIFLY_H
+#define WIFLY_H
+
+#include "mbed.h"
+#include "CBuffer.h"
+
+class Wifly {
+
+public:
+    /*
+    * Constructor for joining open, wep or wpa secured networks
+    *
+    * @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 wpa true if wpa security false otherwise
+    */
+    Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa);
+
+    /*
+    * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
+    * Useful to send a command to the module and wait a response.
+    *
+    *
+    * @param str string to be sent
+    * @param len string length
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
+    *
+    * @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);
+
+    /*
+    * Connect the wifi module to the ssid contained in the constructor.
+    *
+    * @return true if connected, false otherwise
+    */
+    bool join();
+    
+    /*
+    * Close a connection with the access point
+    *
+    * @ returns true if successful
+    */
+    bool leave();
+
+    /*
+    * Read a string if available
+    *
+    *@param str pointer where will be stored the string read
+    */
+    bool read(char * str);
+
+    /*
+    * Reset the wifi module
+    */
+    void reset();
+
+    /*
+    * Check if characters are available
+    *
+    * @return number of available characters
+    */
+    int readable();
+    
+    /*
+    * Check if characters are available
+    *
+    * @return number of available characters
+    */
+    int writeable();
+    
+    /*
+    * Check if a tcp link is active
+    *
+    * @returns true if successful
+    */
+    bool is_connected();
+
+    /*
+    * Read a character
+    *
+    * @return the character read
+    */
+    char getc();
+
+    /*
+    * Flush the buffer
+    */
+    void flush();
+
+    /*
+    * Write a character
+    *
+    * @param the character which will be written
+    */
+    int putc(char c);
+
+
+    /*
+    * To enter in command mode (we can configure the module)
+    *
+    * @return true if successful, false otherwise
+    */
+    bool cmdMode();
+
+    /*
+    * To exit the command mode
+    *
+    * @return true if successful, false otherwise
+    */
+    bool exit();
+    
+    /*
+    * Close a tcp connection
+    *
+    * @ returns true if successful
+    */
+    bool close();
+    
+    /*
+    * Reboot the module
+    *
+    * @returns true if successful
+    */
+    bool reboot();
+    
+    /*
+    * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
+    *
+    * @param str string to be sent
+    * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
+    * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
+    *
+    * @returns true if successful
+    */
+    bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL);
+    
+    static Wifly * getInstance() {return inst;};
+
+protected:
+    Serial wifi;
+    DigitalOut reset_pin;
+    DigitalIn tcp_status;
+    bool wpa;
+    bool dhcp;
+    const char * phrase;
+    const char * ssid;
+    const char * ip;
+    const char * netmask;
+    const char * gateway;
+    int channel;
+    CircBuffer<char> buf_wifly;
+    
+    bool cmd_mode;
+    
+    static Wifly * inst;
+
+    void attach_rx(bool null);
+    void handler_rx(void);
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiflyInterface.cpp	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,51 @@
+#include "WiflyInterface.h"
+
+WiflyInterface::WiflyInterface(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa) : 
+    Wifly(tx, rx, reset, tcp_status, ssid, phrase, wpa) {ip_set = false;}
+    
+int WiflyInterface::init() {
+    dhcp = true;
+    reset();
+    return 0;
+}
+
+int WiflyInterface::init(const char* ip, const char* mask, const char* gateway) {
+    dhcp = false;
+    this->ip = ip;
+    strcpy(ip_string, ip);
+    ip_set = true;
+    this->netmask = mask;
+    this->gateway = gateway;
+    
+    reset();
+    return 0;
+}
+
+int WiflyInterface::connect() {
+    return join();
+}
+
+int WiflyInterface::disconnect() {
+    return leave();
+}
+
+char * WiflyInterface::getIPAddress() {
+    char * match = 0;
+    if (!ip_set) {
+        sendCommand("get ip a\r", NULL, ip_string);
+        exit();
+        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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiflyInterface.h	Fri Aug 17 08:28:04 2012 +0000
@@ -0,0 +1,75 @@
+/* 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"
+
+ /** Interface using Wifly to connect to an IP-based network
+ *
+ */
+class WiflyInterface: public Wifly {
+public:
+
+  WiflyInterface(PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa = true);
+
+  /** 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[30];
+    bool ip_set;
+};
+
+#include "TCPSocketConnection.h"
+#include "TCPSocketServer.h"
+#include "UDPSocket.h"
+
+#endif /* WIFLYINTERFACE_H_ */