cc3000 hostdriver with the mbed socket interface

Dependents:   cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more

Files at this revision

API Documentation at this revision

Comitter:
Kojto
Date:
Thu Sep 26 19:50:37 2013 +0000
Parent:
4:15b58c119a0a
Child:
6:ac602a158403
Commit message:
tcp implementation

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
cc3000_socket.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Helper/def.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Helper/def.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,28 +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)
-
+/* Copyright (C) 2013 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
--- a/Socket/Endpoint.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/Endpoint.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -72,13 +72,12 @@
 
 void Endpoint::reset_address(void) {
     _ipAddress[0] = '\0';
-    std::memset(&_remote_host,0, sizeof(sockaddr_in));
+    std::memset(&_remote_host, 0, sizeof(sockaddr_in));
 }
 
 int Endpoint::set_address(const char* host, const int port) {
     reset_address();
 
-    // IP Address
     char address[5];
     char *p_address = address;
 
@@ -89,6 +88,7 @@
     for (int i=0;i<4;i++) {
       address[i] = add[i];
     }
+    std::memset(_ipAddress,0,sizeof(_ipAddress));
 
     if (result != 4) {
         //Resolve DNS address or populate hard-coded IP address
@@ -97,36 +97,22 @@
 
         _remote_host.sin_addr.s_addr = address_integer;
         inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
-        // address[0] = (address_integer >> 24);
-        // address[1] = (address_integer >> 16);
-        // address[2] = (address_integer >> 8);
-        // address[3] = (address_integer >> 0);
-        // sprintf(_ipAddress,"%3u.%3u.%3u.%3u", address[0],address[1],address[2],address[3]);
-        // p_address = _ipAddress;
-        // _remote_host.sin_addr.s_addr = address_integer;
     } else {
         std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4);
     }
 
+    _remote_host.sin_family = AF_INET;
+    _remote_host.sin_port = htons(port);
+
 #if (CC3000_DEBUG == 1)
     printf("DEBUG: remote host address (string): %s\n",_ipAddress);
     printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d\n",
             int(_remote_host.sin_addr.s_addr & 0xFF),
-            int((_remote_host.sin_addr.s_addr & 0xFF00)>>8),
-            int((_remote_host.sin_addr.s_addr & 0xFF0000)>>16),
-            int((_remote_host.sin_addr.s_addr & 0xFF000000)>>24));
-    //     address[0] = (_remote_host.sin_addr.s_addr >> 24);
-    //     address[1] = (_remote_host.sin_addr.s_addr >> 16);
-    //     address[2] = (_remote_host.sin_addr.s_addr >> 8);
-    //     address[3] = (_remote_host.sin_addr.s_addr >> 0);
-    // printf("DEBUG: remote host address from s_addr: %3u.%3u.%3u.%3u\n",address[0],address[1],address[2],address[3]);
+            int((_remote_host.sin_addr.s_addr & 0xFF00) >> 8),
+            int((_remote_host.sin_addr.s_addr & 0xFF0000) >> 16),
+            int((_remote_host.sin_addr.s_addr & 0xFF000000) >> 24));
+    printf("DEBUG: port: %x \n", _remote_host.sin_port);
 #endif
-    /* store address*/
-    _remote_host.sin_family = AF_INET;
-
-    // Set port
-    _remote_host.sin_port = htons(port);
-
     return 0;
 }
 
--- a/Socket/Endpoint.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/Endpoint.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
--- a/Socket/Socket.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/Socket.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -19,7 +19,7 @@
 #include "Socket.h"
 #include <cstring>
 
-Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500)  {
+Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
     _cc3000_module = cc3000::get_instance();
     if (_cc3000_module == NULL) {
         error("Socket constructor error: no cc3000 instance available!\r\n");
@@ -37,7 +37,7 @@
     int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol);
     if (fd < -1) {
 #if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to create new socket (type:%d, protocol:%d).\n",type, protocol);
+        printf("DEBUG: Failed to create new socket (type: %d, protocol: %d).\n",type, protocol);
 #endif
         return -1;
     }
@@ -78,6 +78,7 @@
 #if (CC3000_DEBUG == 1)
     printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\n",_sock_fd, ret, fdSet);
 #endif
+    // TODO
     //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
     if (FD_ISSET(_sock_fd, &fdSet)) {
         return 0;
@@ -104,10 +105,124 @@
 }
 
 Socket::~Socket() {
-    close(); //Don't want to leak
+    close();
 }
 
 TimeInterval::TimeInterval(unsigned int ms) {
     _time.tv_sec = ms / 1000;
     _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
 }
+/* Copyright (C) 2013 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() : _sock_fd(-1), _blocking(true), _timeout(1500) {
+    _cc3000_module = cc3000::get_instance();
+    if (_cc3000_module == NULL) {
+        error("Socket constructor error: no cc3000 instance available!\r\n");
+    }
+}
+
+int Socket::init_socket(int type, int protocol) {
+    if (_sock_fd != -1) {
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Socket was initialized previously.\n");
+#endif
+        return -1;
+    }
+
+    int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol);
+    if (fd < -1) {
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Failed to create new socket (type: %d, protocol: %d).\n",type, protocol);
+#endif
+        return -1;
+    }
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\n",fd, type, protocol);
+#endif
+    _sock_fd = fd;
+
+    return 0;
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+    _blocking = blocking;
+    _timeout = timeout;
+}
+
+int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
+    return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen);
+}
+
+int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
+    return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen);
+}
+
+int Socket::select(struct timeval *timeout, bool read, bool write) {
+    if (_sock_fd < 0) {
+        return -1;
+    }
+
+    fd_set fdSet;
+    FD_ZERO(&fdSet);
+    FD_SET(_sock_fd, &fdSet);
+
+    fd_set* readset  = (read ) ? (&fdSet) : (NULL);
+    fd_set* writeset = (write) ? (&fdSet) : (NULL);
+
+    int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout);
+#if (CC3000_DEBUG == 1)
+    printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\n",_sock_fd, ret, fdSet);
+#endif
+    // TODO
+    //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
+    if (FD_ISSET(_sock_fd, &fdSet)) {
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+int Socket::wait_readable(TimeInterval& timeout) {
+    return select(&timeout._time, true, false);
+}
+
+int Socket::wait_writable(TimeInterval& timeout) {
+    return select(&timeout._time, false, true);
+}
+
+int Socket::close() {
+    if (_sock_fd < 0 ) {
+        return -1;
+    }
+
+    _cc3000_module->_socket.closesocket(_sock_fd);
+    return 0;
+}
+
+Socket::~Socket() {
+    close();
+}
+
+TimeInterval::TimeInterval(unsigned int ms) {
+    _time.tv_sec = ms / 1000;
+    _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
+}
--- a/Socket/Socket.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/Socket.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -15,8 +15,8 @@
  * 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_
+#ifndef SOCKET_H
+#define SOCKET_H
 
 #include "cc3000.h"
 
@@ -95,4 +95,4 @@
     struct timeval _time;
 };
 
-#endif /* SOCKET_H_ */
+#endif /* SOCKET_H */
--- a/Socket/TCPSocketConnection.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -19,38 +19,133 @@
 #include "TCPSocketConnection.h"
 #include <algorithm>
 
-TCPSocketConnection::TCPSocketConnection() {}
-
-int TCPSocketConnection::connect(const char* host, const int port)
-{
-
+TCPSocketConnection::TCPSocketConnection() : _is_connected(false) {
+    _cc3000_module = cc3000::get_instance();
+    if (_cc3000_module == NULL) {
+        error("Endpoint constructor error: no cc3000 instance available!\r\n");
+    }
 }
 
-bool TCPSocketConnection::is_connected(void)
-{
+int TCPSocketConnection::connect(const char *host, const int port) {
+    if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) {
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Failed to create tcp socket.\n");
+#endif
+        return -1;
+    }
 
+    if (set_address(host, port) != 0) {
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Failed to set address (tcp).\n");
+#endif
+        return -1;
+    }
+
+    if (_cc3000_module->_socket.connect(_sock_fd, (const sockaddr *)&_remote_host, sizeof(_remote_host)) < 0) {
+#if (CC3000_DEBUG == 1)
+        printf("DEBUG: Failed to connect (tcp).\n");
+#endif
+        close();
+        return -1;
+    }
+
+    _is_connected = true;
+
+    return 0;
 }
 
-int TCPSocketConnection::send(char* data, int length)
-{
+bool TCPSocketConnection::is_connected(void) {
+    return _is_connected;
+}
+
+int TCPSocketConnection::send(char* data, int length) {
+    if ((_sock_fd < 0) || !_is_connected) {
+        return -1;
+    }
 
+    if (!_blocking) {
+        TimeInterval timeout(_timeout);
+        if (wait_writable(timeout) != 0) {
+            return -1;
+        }
+    }
+
+    int n = _cc3000_module->_socket.send(_sock_fd, data, length, 0);
+    _is_connected = (n != 0);
+
+    return n;
 }
 
-// -1 if unsuccessful, else number of bytes written
-int TCPSocketConnection::send_all(char* data, int length)
-{
+int TCPSocketConnection::send_all(char *data, int length) {
+    if ((_sock_fd < 0) || !_is_connected) {
+        return -1;
+    }
 
+    int writtenLen = 0;
+    TimeInterval timeout(_timeout);
+    while (writtenLen < length) {
+        if (!_blocking) {
+            // Wait for socket to be writeable
+            if (wait_writable(timeout) != 0) {
+                return writtenLen;
+            }
+        }
+
+        int ret = _cc3000_module->_socket.send(_sock_fd, data + writtenLen, length - writtenLen, 0);
+        if (ret > 0) {
+            writtenLen += ret;
+            continue;
+        } else if (ret == 0) {
+            _is_connected = false;
+            return writtenLen;
+        } else {
+            return -1; //Connnection error
+        }
+    }
+
+    return writtenLen;
 }
 
-// -1 if unsuccessful, else number of bytes received
-int TCPSocketConnection::receive(char* data, int length)
-{
+int TCPSocketConnection::receive(char *data, int length) {
+    if ((_sock_fd < 0) || !_is_connected) {
+        return -1;
+    }
 
+    if (!_blocking) {
+        TimeInterval timeout(_timeout);
+        if (wait_readable(timeout) != 0)
+            return -1;
+    }
+
+    int n = _cc3000_module->_socket.recv(_sock_fd, data, length, 0);
+    _is_connected = (n != 0);
+
+    return n;
 }
 
+int TCPSocketConnection::receive_all(char *data, int length) {
+    if ((_sock_fd < 0) || !_is_connected) {
+        return -1;
+    }
 
-// -1 if unsuccessful, else number of bytes received
-int TCPSocketConnection::receive_all(char* data, int length)
-{
+    int readLen = 0;
+    TimeInterval timeout(_timeout);
+    while (readLen < length) {
+        if (!_blocking) {
+            //Wait for socket to be readable
+            if (wait_readable(timeout) != 0)
+                return readLen;
+        }
 
+        int ret = _cc3000_module->_socket.recv(_sock_fd, data + readLen, length - readLen, 0);
+        if (ret > 0) {
+            readLen += ret;
+        } else if (ret == 0) {
+            _is_connected = false;
+            return readLen;
+        } else {
+            return -1; //Connnection error
+        }
+    }
+    return readLen;
 }
--- a/Socket/TCPSocketConnection.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/TCPSocketConnection.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -26,51 +26,55 @@
 TCP socket connection
 */
 class TCPSocketConnection: public Socket, public Endpoint {
-    
+    friend class TCPSocketServer;
+
 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);
-    
+    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);
-    
+    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);
-    
+    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);
-    
+    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);
+    int receive_all(char *data, int length);
+private:
+    bool _is_connected;
+    cc3000 *_cc3000_module;
 };
 
 #endif
--- a/Socket/TCPSocketServer.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/TCPSocketServer.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -19,18 +19,64 @@
 #include "TCPSocketServer.h"
 #include <string>
 
-TCPSocketServer::TCPSocketServer() {}
-
-// Server initialization
-int TCPSocketServer::bind(int port) {
+TCPSocketServer::TCPSocketServer() {
 
 }
 
-int TCPSocketServer::listen(int backlog) {
+int TCPSocketServer::bind(int port) {
+    if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) {
+        return -1;
+    }
+
+    sockaddr_in localHost;
+    memset(&localHost, 0, sizeof(localHost));
+
+    localHost.sin_family = AF_INET;
+    localHost.sin_port = htons(port);
+    localHost.sin_addr.s_addr = 0;
 
+    if (_cc3000_module->_socket.bind(_sock_fd, (const sockaddr *)&localHost, sizeof(localHost)) < 0) {
+        close();
+        return -1;
+    }
+
+    return 0;
+}
+
+int TCPSocketServer::listen(int max) {
+    if (_sock_fd < 0) {
+        return -1;
+    }
+
+    if (_cc3000_module->_socket.listen(_sock_fd, max) < 0) {
+        close();
+        return -1;
+    }
+
+    return 0;
 }
 
 
 int TCPSocketServer::accept(TCPSocketConnection& connection) {
+    if (_sock_fd < 0) {
+        return -1;
+    }
 
+    if (!_blocking) {
+        TimeInterval timeout(_timeout);
+        if (wait_readable(timeout) != 0) {
+            return -1;
+        }
+    }
+
+    connection.reset_address();
+    socklen_t newSockRemoteHostLen = sizeof(connection._remote_host);
+    int fd = _cc3000_module->_socket.accept(_sock_fd, (sockaddr *) &connection._remote_host, &newSockRemoteHostLen);
+    if (fd < 0) {
+        return -1;
+    }
+    connection._sock_fd = fd;
+    connection._is_connected = true;
+
+    return 0;
 }
--- a/Socket/TCPSocketServer.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/TCPSocketServer.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -28,20 +28,20 @@
     /** 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.
--- a/Socket/UDPSocket.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/UDPSocket.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
@@ -29,7 +29,6 @@
     return init_socket(SOCK_DGRAM, IPPROTO_UDP);
 }
 
-// Server initialization
 int UDPSocket::bind(int port) {
     if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) {
         return -1;
@@ -41,14 +40,6 @@
     localHost.sin_family = AF_INET;
     localHost.sin_port = htons(port);
     localHost.sin_addr.s_addr = 0;
-    // sockaddr localHost;
-    // localHost.family = AF_INET;
-    // localHost.data[0] = (port & 0xFF00)>> 8;
-    // localHost.data[1] = (port & 0x00FF);
-    // localHost.data[2] = 0;
-    // localHost.data[3] = 0;
-    // localHost.data[4] = 0;
-    // localHost.data[5] = 0;
 
     if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) {
 #if (CC3000_DEBUG == 1)
@@ -58,13 +49,10 @@
         _sock_fd = -1;
         return -1;
     }
-// #if (CC3000_DEBUG == 1)
-//     printf("DEBUG: local add: %d\n",localHost.sin_addr.s_addr);
-// #endif
+
     return 0;
 }
 
-// -1 if unsuccessful, else number of bytes written
 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
 {
     if (_sock_fd < 0) {
@@ -84,7 +72,6 @@
     return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr));
 }
 
-// -1 if unsuccessful, else number of bytes received
 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
 {
     if (_sock_fd < 0) {
--- a/Socket/UDPSocket.h	Sat Sep 21 15:01:05 2013 +0000
+++ b/Socket/UDPSocket.h	Thu Sep 26 19:50:37 2013 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2012 mbed.org, MIT License
+/* Copyright (C) 2013 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,
--- a/cc3000_socket.cpp	Sat Sep 21 15:01:05 2013 +0000
+++ b/cc3000_socket.cpp	Thu Sep 26 19:50:37 2013 +0000
@@ -89,7 +89,7 @@
         _simple_link.set_transmit_error(0);
         return errno;
     }
-    if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd))
+    if(SOCKET_STATUS_ACTIVE != _event.get_socket_active_status(sd))
         return -1;
 
     // If there are no available buffers, return -2. It is recommended to use