Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Files at this revision

API Documentation at this revision

Comitter:
xinlei
Date:
Wed May 20 14:30:26 2015 +0000
Parent:
121:d4d44550e087
Child:
123:3e4a1ed4bad5
Commit message:
fix for properly closing a socket when connect failed

Changed in this revision

MbedAgent.cpp Show annotated file Show diff for this revision Revisions of this file
config/ConfigSync.cpp Show annotated file Show diff for this revision Revisions of this file
operation/ReportThread.cpp Show annotated file Show diff for this revision Revisions of this file
util/SmartRestSocket.cpp Show annotated file Show diff for this revision Revisions of this file
util/SmartRestSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/MbedAgent.cpp	Wed May 20 13:17:19 2015 +0000
+++ b/MbedAgent.cpp	Wed May 20 14:30:26 2015 +0000
@@ -118,6 +118,7 @@
         }
         if (l) {
             l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
+            sock.setBlocking(3000);
             l = sock.sendOnly(buf, l);
             if (l < 0)
                 aWarning("%s\n", status);
--- a/config/ConfigSync.cpp	Wed May 20 13:17:19 2015 +0000
+++ b/config/ConfigSync.cpp	Wed May 20 14:30:26 2015 +0000
@@ -57,7 +57,6 @@
 {
         dict.clear();
         dict.set(INTERVAL_KEY, DEFAULT_INTERVAL);
-        aDebug("Reset conf\n");
         changed = true;
 }
 
--- a/operation/ReportThread.cpp	Wed May 20 13:17:19 2015 +0000
+++ b/operation/ReportThread.cpp	Wed May 20 14:30:26 2015 +0000
@@ -5,7 +5,6 @@
 
 void ReportThread::threadFunc()
 {
-    sock.setBlocking(3000);
     while (true) {
         dict.clear();
         osEvent e = ipool.get();
@@ -31,6 +30,7 @@
             }
             l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2);
             for (unsigned i = 0; i < 3; ++i) {
+                sock.setBlocking(3000);
                 int l2 = sock.sendOnly(buf, l);
                 if (l2 < 0) {
                     aError("Report: op state\n");
--- a/util/SmartRestSocket.cpp	Wed May 20 13:17:19 2015 +0000
+++ b/util/SmartRestSocket.cpp	Wed May 20 14:30:26 2015 +0000
@@ -16,11 +16,7 @@
                         snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
                 }
                 n = TCPSocketConnection::connect(cachedIP, srPort);
-                if (n >= 0) {
-                        if (timeout == -1)
-                            Socket::set_blocking(true);
-                        else
-                            Socket::set_blocking(false, timeout);
+                if (n == 0) {
                         break;
                 } else {
                         cachedIP[0] = 0;
@@ -32,8 +28,10 @@
 int SmartRestSocket::sendOnly(char *buf, int size)
 {
         int l = connect();
-        if (l < 0)
+        if (l < 0) {
+                close();
                 return -3;
+        }
         l = send(buf, size);
         close();
         if (l < 0) {
@@ -46,17 +44,19 @@
 int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
 {
         int l = connect();
-        if (l < 0)
+        if (l < 0) {
+                close();
                 return -3;
+        }
         l = send(buf, size);
         if (l < 0) {
                 close();
                 return -2;
         } else {
                 l = receive(buf, maxSize);
+                close();
                 if (l >= 0 && l < maxSize)
                         buf[l] = 0;
-                close();
                 return l;
         }
 }
\ No newline at end of file
--- a/util/SmartRestSocket.h	Wed May 20 13:17:19 2015 +0000
+++ b/util/SmartRestSocket.h	Wed May 20 14:30:26 2015 +0000
@@ -5,7 +5,7 @@
 class SmartRestSocket : private TCPSocketConnection
 {
 public:
-        SmartRestSocket(): TCPSocketConnection(), timeout(-1) {
+        SmartRestSocket(): TCPSocketConnection() {
                 memset(cachedIP, 0, sizeof(cachedIP));
         }
         virtual ~SmartRestSocket() {}
@@ -14,13 +14,15 @@
         /* Set the timeout for the socket.
            \param _timeout in milliseconds, -1 for no timeout.
          */
-        void setBlocking(int _timeout = -1) {
-                timeout = _timeout;
+        void setBlocking(int timeout = -1) {
+                if (timeout == -1)
+                        Socket::set_blocking(true);
+                else
+                        Socket::set_blocking(false, timeout);
         }
 private:
         int connect();
         char cachedIP[16];
-        int timeout;
 };
 
 #endif /* SMARTRESTSOCKET_H */
\ No newline at end of file