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:
SolderSplashLabs
Date:
Wed Oct 02 15:00:07 2013 +0000
Parent:
11:5e3771b29385
Child:
14:28c8bbbea025
Commit message:
Now using a #define for debug printing control, allowing redirection and modification in a single place

Changed in this revision

Socket/Endpoint.cpp 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/TCPSocketConnection.cpp 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
cc3000.cpp Show annotated file Show diff for this revision Revisions of this file
cc3000.h Show annotated file Show diff for this revision Revisions of this file
cc3000_server.cpp 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/Socket/Endpoint.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/Socket/Endpoint.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -109,15 +109,14 @@
     _remote_host.sin_family = AF_INET;
     _remote_host.sin_port = htons(port);
 
-#if (CC3000_DEBUG == 1)
-    printf("DEBUG: remote host address (string): %s\r\n",get_address());
-    printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d\r\n",
+    DBG_SOCKET("remote host address (string): %s",get_address());
+    DBG_SOCKET("remote host address from s_addr : %d.%d.%d.%d",
             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));
-   printf("DEBUG: port: %d \r\n", port);
-#endif
+    DBG_SOCKET("port: %d", port);
+
     return 0;
 }
 
--- a/Socket/Socket.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/Socket/Socket.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -28,22 +28,17 @@
 
 int Socket::init_socket(int type, int protocol) {
     if (_sock_fd != -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Socket was initialized previously.\r\n");
-#endif
+        DBG_SOCKET("Socket was initialized previously");
         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).\r\n",type, protocol);
-#endif
+        DBG_SOCKET("Failed to create new socket (type: %d, protocol: %d)",type, protocol);
         return -1;
     }
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\r\n",fd, type, protocol);
-#endif
+    
+    DBG_SOCKET("Socket created (fd: %d type: %d, protocol: %d)",fd, type, protocol);
     _sock_fd = fd;
 
     return 0;
@@ -75,9 +70,9 @@
     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\r\n",_sock_fd, ret, fdSet);
-#endif
+
+    DBG_SOCKET("Select on sock_fd: %d, returns %d. fdSet: %d", _sock_fd, ret, fdSet);
+
     // TODO
     //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
     if (FD_ISSET(_sock_fd, &fdSet)) {
--- a/Socket/TCPSocketConnection.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -28,23 +28,17 @@
 
 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.\r\n");
-#endif
+        DBG_SOCKET("Failed to create tcp socket");
         return -1;
     }
 
     if (set_address(host, port) != 0) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to set address (tcp).\r\n");
-#endif
+        DBG_SOCKET("Failed to set address (tcp)");
         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).\r\n");
-#endif
+        DBG_SOCKET("Failed to connect (tcp)");
         close();
         return -1;
     }
--- a/Socket/UDPSocket.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/Socket/UDPSocket.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -42,9 +42,7 @@
     localHost.sin_addr.s_addr = 0;
 
     if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to bind a socket (udp). Closing socket.\r\n");
-#endif
+        DBG_SOCKET("Failed to bind a socket (udp). Closing socket");
         _cc3000_module->_socket.closesocket(_sock_fd);
         _sock_fd = -1;
         return -1;
@@ -81,9 +79,7 @@
     if (!_blocking) {
         TimeInterval timeout(_timeout);
         if (wait_readable(timeout) != 0) {
-#if (CC3000_DEBUG == 1)
-            printf("DEBUG: The socket is not readable. _sock_fd: %d.\r\n", _sock_fd);
-#endif
+            DBG_SOCKET("The socket is not readable. _sock_fd: %d", _sock_fd);
             return 0;
         }
     }
--- a/cc3000.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/cc3000.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -133,18 +133,17 @@
     // Start the Smart Config process with AES disabled
     _wlan.smart_config_start(0);
 
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Waiting for smartconfig to be completed.\n");
-#endif
+    DBG_HCI("Waiting for smartconfig to be completed");
+
     // Wait for Smart config finished
     while (_status.smart_config_complete == 0)
     {
         wait_ms(100);
 
     }
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Smartconfig finished.\n");
-#endif
+
+    DBG_HCI("Smartconfig finished");
+
 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
     // create new entry for AES encryption key
     _nvmem.create_entry(NVMEM_AES128_KEY_FILEID, 16);
@@ -205,9 +204,9 @@
         /* timeout 10 seconds */
         if (t.read_ms() > 10000){
             ret = false;
-#if (CC3000_DEBUG == 1)
-            printf("Connection to AP failed.\n");
-#endif
+
+            DBG_HCI("Connection to AP failed");
+
             break;
         }
     }
@@ -289,9 +288,7 @@
 
     tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (tcp_socket == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to create new socket (tcp).\n");
-#endif
+        DBG_HCI("Failed to create new socket (tcp)");
         return cc3000_client(*this);
     }
 
@@ -304,9 +301,7 @@
     socket_address.data[5] = ip_address;
 
     if (_socket.connect(tcp_socket, &socket_address, sizeof(socket_address)) == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to connect (tcp).\n");
-#endif
+        DBG_HCI("Failed to connect (tcp)");
         _socket.closesocket(tcp_socket);
         return cc3000_client(*this);
     }
@@ -319,9 +314,7 @@
 
     udp_socket = _socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
     if (udp_socket == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to create new socket (udp).\n");
-#endif
+        DBG_HCI("Failed to create new socket (udp)");
         return cc3000_client(*this);
     }
 
@@ -334,9 +327,7 @@
     socket_address.data[5] = ip_address;
 
     if (_socket.connect(udp_socket, &socket_address, sizeof(socket_address)) == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to connect (udp).\n");
-#endif
+        DBG_HCI("Failed to connect (udp)");
         _socket.closesocket(udp_socket);
         return cc3000_client(*this);
     }
@@ -350,9 +341,7 @@
 
     tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (tcp_socket == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("Failed to create new socket.\n");
-#endif
+        DBG_HCI("Failed to create new socket.");
         return cc3000_server(*this, socket_address);
     }
 
@@ -365,15 +354,11 @@
     socket_address.data[5] = ip_address;
 
     if (_socket.bind(tcp_socket, &socket_address, sizeof(socket_address)) != 0) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to bind the new socket.\n");
-#endif
+        DBG_HCI("Failed to bind the new socket");
         return cc3000_server(*this, socket_address);
     }
     if (_socket.listen(tcp_socket, 1) != 0) { /* 1 client */
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to listen on the new socket.\n");
-#endif
+        DBG_HCI("Failed to listen on the new socket");
         return cc3000_server(*this, socket_address);
     }
 
@@ -408,21 +393,18 @@
 
     _ping_report.packets_received = 0;
     if (_netapp.ping_send(&reversed_ip, attempts, size, timeout) == -1) {
-#if (CC3000_DEBUG == 1)
-        printf("DEBUG: Failed to send ping.\n");
-#endif
+        DBG_HCI("Failed to send ping");
         return 0;
     }
     wait_ms(timeout*attempts*2);
 
     /* known issue of cc3000 - sent number is send + received */
-#if (CC3000_DEBUG == 1)
-    printf("DEBUG: Sent: %d \r\n",_ping_report.packets_sent);
-    printf("DEBUG: Received: %d \r\n",_ping_report.packets_received);
-    printf("DEBUG: Min time: %d \r\n",_ping_report.min_round_time);
-    printf("DEBUG: Max time: %d \r\n",_ping_report.max_round_time);
-    printf("DEBUG: Avg time: %d \r\n",_ping_report.avg_round_time);
-#endif
+    // TODO : Remove the Sent/recv'd counts until ti fix the firmware issue?
+    DBG_HCI("Sent: %d \r\n",_ping_report.packets_sent);
+    DBG_HCI("Received: %d \r\n",_ping_report.packets_received);
+    DBG_HCI("Min time: %d \r\n",_ping_report.min_round_time);
+    DBG_HCI("Max time: %d \r\n",_ping_report.max_round_time);
+    DBG_HCI("Avg time: %d \r\n",_ping_report.avg_round_time);
 
     return _ping_report.packets_received;
 }
--- a/cc3000.h	Tue Oct 01 21:17:44 2013 +0000
+++ b/cc3000.h	Wed Oct 02 15:00:07 2013 +0000
@@ -55,6 +55,14 @@
  */
 #define CC3000_DEBUG      1
 
+#if CC3000_DEBUG == 1
+    #define DBG_SOCKET(x, ...) std::printf("[CC3000 : SOCKET] "x"\r\n", ##__VA_ARGS__); 
+    #define DBG_HCI(x, ...) std::printf("[CC3000 : HCI] "x"\r\n", ##__VA_ARGS__); 
+#else
+    #define DBG_SOCKET(x, ...) 
+    #define DBG_HCI(x, ...)
+#endif
+
 namespace mbed_cc3000 {
 
 /** User info structure
--- a/cc3000_server.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/cc3000_server.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -56,16 +56,15 @@
     socklen_t address_length = sizeof(dest_address);
     _current_socket = 0;
 
-#if (CC3000_DEBUG == 1)
-    printf("Waiting for receiving a connection.\n");
-#endif
+    DBG_HCI("Waiting for receiving a connection");
+
     while((client_descriptor == -1) || (client_descriptor == -2))
     {
         client_descriptor = _cc3000._socket.accept(_current_socket,&dest_address, &address_length);
     }
-#if (CC3000_DEBUG == 1)
-    printf("Received.\n");
-#endif
+
+    DBG_HCI("Received");
+
     _current_socket = client_descriptor;
 
     return _current_socket;
--- a/cc3000_socket.cpp	Tue Oct 01 21:17:44 2013 +0000
+++ b/cc3000_socket.cpp	Wed Oct 02 15:00:07 2013 +0000
@@ -536,7 +536,7 @@
 #ifndef CC3000_TINY_DRIVER
 
 int32_t cc3000_socket::gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) {
-    get_host_by_name(hostname, name_length, out_ip_addr);
+    return (get_host_by_name(hostname, name_length, out_ip_addr));
 }
 
 int32_t cc3000_socket::get_host_by_name(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) {