Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 12:12:55 2015 +0000
Revision:
106:fc5f25f0e0d5
Parent:
105:c61f0d62b625
Child:
115:4eb3c7e945cf
OperationSupport revamped

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 93:61d44636f020 1 #include <string.h>
xinlei 93:61d44636f020 2 #include "SmartRestSocket.h"
xinlei 93:61d44636f020 3 #include "SmartRestConf.h"
xinlei 93:61d44636f020 4
xinlei 93:61d44636f020 5 char SmartRestSocket::cachedIP[16] = {0};
xinlei 93:61d44636f020 6
xinlei 93:61d44636f020 7 int SmartRestSocket::connect()
xinlei 93:61d44636f020 8 {
xinlei 96:ea056f6be2e8 9 extern MDMSerial *pMdm;
xinlei 93:61d44636f020 10 int n = -1;
xinlei 93:61d44636f020 11 ipLock.lock();
xinlei 93:61d44636f020 12 for (size_t i = 0; i < 3; ++i) {
xinlei 106:fc5f25f0e0d5 13 if (cachedIP[0] == 0) {
xinlei 98:e369fc75c000 14 MDMParser::IP ip = pMdm->gethostbyname(srHost);
xinlei 93:61d44636f020 15 if (ip == NOIP)
xinlei 93:61d44636f020 16 continue;
xinlei 93:61d44636f020 17 const unsigned char *c = (const unsigned char*)&ip;
xinlei 93:61d44636f020 18 snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
xinlei 93:61d44636f020 19 }
xinlei 98:e369fc75c000 20 n = TCPSocketConnection::connect(cachedIP, srPort);
xinlei 93:61d44636f020 21 if (n >= 0) {
xinlei 105:c61f0d62b625 22 if (timeout == -1)
xinlei 105:c61f0d62b625 23 Socket::set_blocking(true);
xinlei 105:c61f0d62b625 24 else
xinlei 105:c61f0d62b625 25 Socket::set_blocking(false, timeout);
xinlei 93:61d44636f020 26 break;
xinlei 93:61d44636f020 27 } else {
xinlei 106:fc5f25f0e0d5 28 cachedIP[0] = 0;
xinlei 93:61d44636f020 29 }
xinlei 93:61d44636f020 30 }
xinlei 93:61d44636f020 31 ipLock.unlock();
xinlei 93:61d44636f020 32 return n;
xinlei 93:61d44636f020 33 }
xinlei 93:61d44636f020 34
xinlei 93:61d44636f020 35 int SmartRestSocket::sendOnly(char *buf, int size)
xinlei 93:61d44636f020 36 {
xinlei 93:61d44636f020 37 int l = connect();
xinlei 93:61d44636f020 38 if (l < 0)
xinlei 93:61d44636f020 39 return -3;
xinlei 93:61d44636f020 40 l = send(buf, size);
xinlei 93:61d44636f020 41 close();
xinlei 93:61d44636f020 42 if (l < 0) {
xinlei 93:61d44636f020 43 return -2;
xinlei 93:61d44636f020 44 } else {
xinlei 93:61d44636f020 45 return l;
xinlei 93:61d44636f020 46 }
xinlei 93:61d44636f020 47 }
xinlei 93:61d44636f020 48
xinlei 93:61d44636f020 49 int SmartRestSocket::sendAndReceive(char *buf, int size, int maxSize)
xinlei 93:61d44636f020 50 {
xinlei 93:61d44636f020 51 int l = connect();
xinlei 93:61d44636f020 52 if (l < 0)
xinlei 93:61d44636f020 53 return -3;
xinlei 93:61d44636f020 54 l = send(buf, size);
xinlei 93:61d44636f020 55 if (l < 0) {
xinlei 93:61d44636f020 56 close();
xinlei 93:61d44636f020 57 return -2;
xinlei 93:61d44636f020 58 } else {
xinlei 93:61d44636f020 59 l = receive(buf, maxSize);
xinlei 98:e369fc75c000 60 if (l >= 0 && l < maxSize)
xinlei 98:e369fc75c000 61 buf[l] = 0;
xinlei 93:61d44636f020 62 close();
xinlei 93:61d44636f020 63 return l;
xinlei 93:61d44636f020 64 }
xinlei 93:61d44636f020 65 }