Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
93:61d44636f020
Child:
96:ea056f6be2e8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/SmartRestSocket.cpp	Mon Apr 20 15:04:23 2015 +0000
@@ -0,0 +1,59 @@
+#include <string.h>
+#include "SmartRestSocket.h"
+#include "SmartRestConf.h"
+
+//memset((void*)SmartRestSocket::cachedIP, 0, sizeof(SmartRestSocket::cachedIP));
+char SmartRestSocket::cachedIP[16] = {0};
+
+int SmartRestSocket::connect()
+{
+        int n = -1;
+        ipLock.lock();
+        for (size_t i = 0; i < 3; ++i) {
+                if (cachedIP[0] == '\0') {
+                        MDMParser::IP ip = mdm.gethostbyname(getHost());
+                        if (ip == NOIP)
+                                continue;
+                        const unsigned char *c = (const unsigned char*)&ip;
+                        snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
+                }
+                n = TCPSocketConnection::connect(cachedIP, getPort());
+                if (n >= 0) {
+                        break;
+                } else {
+                        cachedIP[0] = '\0';
+                }
+        }
+        ipLock.unlock();
+        return n;
+}
+
+int SmartRestSocket::sendOnly(char *buf, int size)
+{
+        int l = connect();
+        if (l < 0)
+                return -3;
+        l = send(buf, size);
+        close();
+        if (l < 0) {
+                return -2;
+        } else {
+                return l;
+        }
+}
+
+int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
+{
+        int l = connect();
+        if (l < 0)
+                return -3;
+        l = send(buf, size);
+        if (l < 0) {
+                close();
+                return -2;
+        } else {
+                l = receive(buf, maxSize);
+                close();
+                return l;
+        }
+}
\ No newline at end of file